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
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
22879
22880
22881
22882
22883
22884
22885
22886
22887
22888
22889
22890
22891
22892
22893
22894
22895
22896
22897
22898
22899
22900
22901
22902
22903
22904
22905
22906
22907
22908
22909
22910
22911
22912
22913
22914
22915
22916
22917
22918
22919
22920
22921
22922
22923
22924
22925
22926
22927
22928
22929
22930
22931
22932
22933
22934
22935
22936
22937
22938
22939
22940
22941
22942
22943
22944
22945
22946
22947
22948
22949
22950
22951
22952
22953
22954
22955
22956
22957
22958
22959
22960
22961
22962
22963
22964
22965
22966
22967
22968
22969
22970
22971
22972
22973
22974
22975
22976
22977
22978
22979
22980
22981
22982
22983
22984
22985
22986
22987
22988
22989
22990
22991
22992
22993
22994
22995
22996
22997
22998
22999
23000
23001
23002
23003
23004
23005
23006
23007
23008
23009
23010
23011
23012
23013
23014
23015
23016
23017
23018
23019
23020
23021
23022
23023
23024
23025
23026
23027
23028
23029
23030
23031
23032
23033
23034
23035
23036
23037
23038
23039
23040
23041
23042
23043
23044
23045
23046
23047
23048
23049
23050
23051
23052
23053
23054
23055
23056
23057
23058
23059
23060
23061
23062
23063
23064
23065
23066
23067
23068
23069
23070
23071
23072
23073
23074
23075
23076
23077
23078
23079
23080
23081
23082
23083
23084
23085
23086
23087
23088
23089
23090
23091
23092
23093
23094
23095
23096
23097
23098
23099
23100
23101
23102
23103
23104
23105
23106
23107
23108
23109
23110
23111
23112
23113
23114
23115
23116
23117
23118
23119
23120
23121
23122
23123
23124
23125
23126
23127
23128
23129
23130
23131
23132
23133
23134
23135
23136
23137
23138
23139
23140
23141
23142
23143
23144
23145
23146
23147
23148
23149
23150
23151
23152
23153
23154
23155
23156
23157
23158
23159
23160
23161
23162
23163
23164
23165
23166
23167
23168
23169
23170
23171
23172
23173
23174
23175
23176
23177
23178
23179
23180
23181
23182
23183
23184
23185
23186
23187
23188
23189
23190
23191
23192
23193
23194
23195
23196
23197
23198
23199
23200
23201
23202
23203
23204
23205
23206
23207
23208
23209
23210
23211
23212
23213
23214
23215
23216
23217
23218
23219
23220
23221
23222
23223
23224
23225
23226
23227
23228
23229
23230
23231
23232
23233
23234
23235
23236
23237
23238
23239
23240
23241
23242
23243
23244
23245
23246
23247
23248
23249
23250
23251
23252
23253
23254
23255
23256
23257
23258
23259
23260
23261
23262
23263
23264
23265
23266
23267
23268
23269
23270
23271
23272
23273
23274
23275
23276
23277
23278
23279
23280
23281
23282
23283
23284
23285
23286
23287
23288
23289
23290
23291
23292
23293
23294
23295
23296
23297
23298
23299
23300
23301
23302
23303
23304
23305
23306
23307
23308
23309
23310
23311
23312
23313
23314
23315
23316
23317
23318
23319
23320
23321
23322
23323
23324
23325
23326
23327
23328
23329
23330
23331
23332
23333
23334
23335
23336
23337
23338
23339
23340
23341
23342
23343
23344
23345
23346
23347
23348
23349
23350
23351
23352
23353
23354
23355
23356
23357
23358
23359
23360
23361
23362
23363
23364
23365
23366
23367
23368
23369
23370
23371
23372
23373
23374
23375
23376
23377
23378
23379
23380
23381
23382
23383
23384
23385
23386
23387
23388
23389
23390
23391
23392
23393
23394
23395
23396
23397
23398
23399
23400
23401
23402
23403
23404
23405
23406
23407
23408
23409
23410
23411
23412
23413
23414
23415
23416
23417
23418
23419
23420
23421
23422
23423
23424
23425
23426
23427
23428
23429
23430
23431
23432
23433
23434
23435
23436
23437
23438
23439
23440
23441
23442
23443
23444
23445
23446
23447
23448
23449
23450
23451
23452
23453
23454
23455
23456
23457
23458
23459
23460
23461
23462
23463
23464
23465
23466
23467
23468
23469
23470
23471
23472
23473
23474
23475
23476
23477
23478
23479
23480
23481
23482
23483
23484
23485
23486
23487
23488
23489
23490
23491
23492
23493
23494
23495
23496
23497
23498
23499
23500
23501
23502
23503
23504
23505
23506
23507
23508
23509
23510
23511
23512
23513
23514
23515
23516
23517
23518
23519
23520
23521
23522
23523
23524
23525
23526
23527
23528
23529
23530
23531
23532
23533
23534
23535
23536
23537
23538
23539
23540
23541
23542
23543
23544
23545
23546
23547
23548
23549
23550
23551
23552
23553
23554
23555
23556
23557
23558
23559
23560
23561
23562
23563
23564
23565
23566
23567
23568
23569
23570
23571
23572
23573
23574
23575
23576
23577
23578
23579
23580
23581
23582
23583
23584
23585
23586
23587
23588
23589
23590
23591
23592
23593
23594
23595
23596
23597
23598
23599
23600
23601
23602
23603
23604
23605
23606
23607
23608
23609
23610
23611
23612
23613
23614
23615
23616
23617
23618
23619
23620
23621
23622
23623
23624
23625
23626
23627
23628
23629
23630
23631
23632
23633
23634
23635
23636
23637
23638
23639
23640
23641
23642
23643
23644
23645
23646
23647
23648
23649
23650
23651
23652
23653
23654
23655
23656
23657
23658
23659
23660
23661
23662
23663
23664
23665
23666
23667
23668
23669
23670
23671
23672
23673
23674
23675
23676
23677
23678
23679
23680
23681
23682
23683
23684
23685
23686
23687
23688
23689
23690
23691
23692
23693
23694
23695
23696
23697
23698
23699
23700
23701
23702
23703
23704
23705
23706
23707
23708
23709
23710
23711
23712
23713
23714
23715
23716
23717
23718
23719
23720
23721
23722
23723
23724
23725
23726
23727
23728
23729
23730
23731
23732
23733
23734
23735
23736
23737
23738
23739
23740
23741
23742
23743
23744
23745
23746
23747
23748
23749
23750
23751
23752
23753
23754
23755
23756
23757
23758
23759
23760
23761
23762
23763
23764
23765
23766
23767
23768
23769
23770
23771
23772
23773
23774
23775
23776
23777
23778
23779
23780
23781
23782
23783
23784
23785
23786
23787
23788
23789
23790
23791
23792
23793
23794
23795
23796
23797
23798
23799
23800
23801
23802
23803
23804
23805
23806
23807
23808
23809
23810
23811
23812
23813
23814
23815
23816
23817
23818
23819
23820
23821
23822
23823
23824
23825
23826
23827
23828
23829
23830
23831
23832
23833
23834
23835
23836
23837
23838
23839
23840
23841
23842
23843
23844
23845
23846
23847
23848
23849
23850
23851
23852
23853
23854
23855
23856
23857
23858
23859
23860
23861
23862
23863
23864
23865
23866
23867
23868
23869
23870
23871
23872
23873
23874
23875
23876
23877
23878
23879
23880
23881
23882
23883
23884
23885
23886
23887
23888
23889
23890
23891
23892
23893
23894
23895
23896
23897
23898
23899
23900
23901
23902
23903
23904
23905
23906
23907
23908
23909
23910
23911
23912
23913
23914
23915
23916
23917
23918
23919
23920
23921
23922
23923
23924
23925
23926
23927
23928
23929
23930
23931
23932
23933
23934
23935
23936
23937
23938
23939
23940
23941
23942
23943
23944
23945
23946
23947
23948
23949
23950
23951
23952
23953
23954
23955
23956
23957
23958
23959
23960
23961
23962
23963
23964
23965
23966
23967
23968
23969
23970
23971
23972
23973
23974
23975
23976
23977
23978
23979
23980
23981
23982
23983
23984
23985
23986
23987
23988
23989
23990
23991
23992
23993
23994
23995
23996
23997
23998
23999
24000
24001
24002
24003
24004
24005
24006
24007
24008
24009
24010
24011
24012
24013
24014
24015
24016
24017
24018
24019
24020
24021
24022
24023
24024
24025
24026
24027
24028
24029
24030
24031
24032
24033
24034
24035
24036
24037
24038
24039
24040
24041
24042
24043
24044
24045
24046
24047
24048
24049
24050
24051
24052
24053
24054
24055
24056
24057
24058
24059
24060
24061
24062
24063
24064
24065
24066
24067
24068
24069
24070
24071
24072
24073
24074
24075
24076
24077
24078
24079
24080
24081
24082
24083
24084
24085
24086
24087
24088
24089
24090
24091
24092
24093
24094
24095
24096
24097
24098
24099
24100
24101
24102
24103
24104
24105
24106
24107
24108
24109
24110
24111
24112
24113
24114
24115
24116
24117
24118
24119
24120
24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
24131
24132
24133
24134
24135
24136
24137
24138
24139
24140
24141
24142
24143
24144
24145
24146
24147
24148
24149
24150
24151
24152
24153
24154
24155
24156
24157
24158
24159
24160
24161
24162
24163
24164
24165
24166
24167
24168
24169
24170
24171
24172
24173
24174
24175
24176
24177
24178
24179
24180
24181
24182
24183
24184
24185
24186
24187
24188
24189
24190
24191
24192
24193
24194
24195
24196
24197
24198
24199
24200
24201
24202
24203
24204
24205
24206
24207
24208
24209
24210
24211
24212
24213
24214
24215
24216
24217
24218
24219
24220
24221
24222
24223
24224
24225
24226
24227
24228
24229
24230
24231
24232
24233
24234
24235
24236
24237
24238
24239
24240
24241
24242
24243
24244
24245
24246
24247
24248
24249
24250
24251
24252
24253
24254
24255
24256
24257
24258
24259
24260
24261
24262
24263
24264
24265
24266
24267
24268
24269
24270
24271
24272
24273
24274
24275
24276
24277
24278
24279
24280
24281
24282
24283
24284
24285
24286
24287
24288
24289
24290
24291
24292
24293
24294
24295
24296
24297
24298
24299
24300
24301
24302
24303
24304
24305
24306
24307
24308
24309
24310
24311
24312
24313
24314
24315
24316
24317
24318
24319
24320
24321
24322
24323
24324
24325
24326
24327
24328
24329
24330
24331
24332
24333
24334
24335
24336
24337
24338
24339
24340
24341
24342
24343
24344
24345
24346
24347
24348
24349
24350
24351
24352
24353
24354
24355
24356
24357
24358
24359
24360
24361
24362
24363
24364
24365
24366
24367
24368
24369
24370
24371
24372
24373
24374
24375
24376
24377
24378
24379
24380
24381
24382
24383
24384
24385
24386
24387
24388
24389
24390
24391
24392
24393
24394
24395
24396
24397
24398
24399
24400
24401
24402
24403
24404
24405
24406
24407
24408
24409
24410
24411
24412
24413
24414
24415
24416
24417
24418
24419
24420
24421
24422
24423
24424
24425
24426
24427
24428
24429
24430
24431
24432
24433
24434
24435
24436
24437
24438
24439
24440
24441
24442
24443
24444
24445
24446
24447
24448
24449
24450
24451
24452
24453
24454
24455
24456
24457
24458
24459
24460
24461
24462
24463
24464
24465
24466
24467
24468
24469
24470
24471
24472
24473
24474
24475
24476
24477
24478
24479
24480
24481
24482
24483
24484
24485
24486
24487
24488
24489
24490
24491
24492
24493
24494
24495
24496
24497
24498
24499
24500
24501
24502
24503
24504
24505
24506
24507
24508
24509
24510
24511
24512
24513
24514
24515
24516
24517
24518
24519
24520
24521
24522
24523
24524
24525
24526
24527
24528
24529
24530
24531
24532
24533
24534
24535
24536
24537
24538
24539
24540
24541
24542
24543
24544
24545
24546
24547
24548
24549
24550
24551
24552
24553
24554
24555
24556
24557
24558
24559
24560
24561
24562
24563
24564
24565
24566
24567
24568
24569
24570
24571
24572
24573
24574
24575
24576
24577
24578
24579
24580
24581
24582
24583
24584
24585
24586
24587
24588
24589
24590
24591
24592
24593
24594
24595
24596
24597
24598
24599
24600
24601
24602
24603
24604
24605
24606
24607
24608
24609
24610
24611
24612
24613
24614
24615
24616
24617
24618
24619
24620
24621
24622
24623
24624
24625
24626
24627
24628
24629
24630
24631
24632
24633
24634
24635
24636
24637
24638
24639
24640
24641
24642
24643
24644
24645
24646
24647
24648
24649
24650
24651
24652
24653
24654
24655
24656
24657
24658
24659
24660
24661
24662
24663
24664
24665
24666
24667
24668
24669
24670
24671
24672
24673
24674
24675
24676
24677
24678
24679
24680
24681
24682
24683
24684
24685
24686
24687
24688
24689
24690
24691
24692
24693
24694
24695
24696
24697
24698
24699
24700
24701
24702
24703
24704
24705
24706
24707
24708
24709
24710
24711
24712
24713
24714
24715
24716
24717
24718
24719
24720
24721
24722
24723
24724
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/config.h>
#include <deque>
#include <optional>
#include <stack>
#include <string.h>
#include <string_view>
#include <dndhelper.hxx>
#include <osl/process.h>
#include <unx/gtk/gtkdata.hxx>
#include <unx/gtk/gtkinst.hxx>
#include <unx/genprn.h>
#include <unx/salobj.h>
#include <unx/gtk/gtkgdi.hxx>
#include <unx/gtk/gtkframe.hxx>
#include <unx/gtk/gtkobject.hxx>
#include <unx/gtk/atkbridge.hxx>
#include <unx/gtk/gtksalmenu.hxx>
#include <headless/svpvd.hxx>
#include <headless/svpbmp.hxx>
#include <utility>
#include <vcl/builder.hxx>
#include <vcl/inputtypes.hxx>
#include <vcl/specialchars.hxx>
#include <vcl/sysdata.hxx>
#include <vcl/transfer.hxx>
#include <vcl/toolkit/floatwin.hxx>
#include <unx/genpspgraphics.h>
#include <rtl/strbuf.hxx>
#include <sal/log.hxx>
#include <rtl/uri.hxx>
#include <vcl/settings.hxx>
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#if !GTK_CHECK_VERSION(4, 0, 0)
#include "a11y/atkwrapper.hxx"
#endif
#include <com/sun/star/awt/XVclWindowPeer.hpp>
#include <com/sun/star/datatransfer/XTransferable.hpp>
#include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
#include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
#include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
#include <com/sun/star/datatransfer/clipboard/XClipboardListener.hpp>
#include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
#include <com/sun/star/datatransfer/clipboard/XSystemClipboard.hpp>
#include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <comphelper/lok.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertyvalue.hxx>
#include <comphelper/sequence.hxx>
#include <comphelper/string.hxx>
#include <cppuhelper/compbase.hxx>
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <officecfg/Office/Common.hxx>
#include <rtl/bootstrap.hxx>
#include <o3tl/unreachable.hxx>
#include <o3tl/string_view.hxx>
#include <svl/zforlist.hxx>
#include <svl/zformat.hxx>
#include <tools/helpers.hxx>
#include <tools/fract.hxx>
#include <tools/stream.hxx>
#include <unotools/resmgr.hxx>
#include <unotools/tempfile.hxx>
#include <unx/gstsink.hxx>
#include <vcl/ImageTree.hxx>
#include <vcl/abstdlg.hxx>
#include <vcl/event.hxx>
#include <vcl/i18nhelp.hxx>
#include <vcl/quickselectionengine.hxx>
#include <vcl/mnemonic.hxx>
#include <vcl/filter/PngImageWriter.hxx>
#include <vcl/stdtext.hxx>
#include <vcl/syswin.hxx>
#include <vcl/virdev.hxx>
#include <vcl/weld.hxx>
#include <vcl/wrkwin.hxx>
#include "customcellrenderer.hxx"
#include <strings.hrc>
#include <window.h>
#include <numeric>
#include <boost/property_tree/ptree.hpp>
#include <opengl/zone.hxx>
using namespace com::sun::star;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
extern "C"
{
#define GET_YIELD_MUTEX() static_cast<GtkYieldMutex*>(GetSalInstance()->GetYieldMutex())
#if !GTK_CHECK_VERSION(4, 0, 0)
static void GdkThreadsEnter()
{
GtkYieldMutex *pYieldMutex = GET_YIELD_MUTEX();
pYieldMutex->ThreadsEnter();
}
static void GdkThreadsLeave()
{
GtkYieldMutex *pYieldMutex = GET_YIELD_MUTEX();
pYieldMutex->ThreadsLeave();
}
#endif
VCLPLUG_GTK_PUBLIC SalInstance* create_SalInstance()
{
SAL_INFO(
"vcl.gtk",
"create vcl plugin instance with gtk version " << gtk_get_major_version()
<< " " << gtk_get_minor_version() << " " << gtk_get_micro_version());
if (gtk_get_major_version() == 3 && gtk_get_minor_version() < 18)
{
g_warning("require gtk >= 3.18 for theme expectations");
return nullptr;
}
// for gtk2 it is always built with X support, so this is always called
// for gtk3 it is normally built with X and Wayland support, if
// X is supported GDK_WINDOWING_X11 is defined and this is always
// called, regardless of if we're running under X or Wayland.
// We can't use (DLSYM_GDK_IS_X11_DISPLAY(pDisplay)) to only do it under
// X, because we need to do it earlier than we have a display
#if defined(GDK_WINDOWING_X11)
/* #i92121# workaround deadlocks in the X11 implementation
*/
static const char* pNoXInitThreads = getenv( "SAL_NO_XINITTHREADS" );
/* #i90094#
from now on we know that an X connection will be
established, so protect X against itself
*/
if( ! ( pNoXInitThreads && *pNoXInitThreads ) )
XInitThreads();
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
// init gdk thread protection
bool const sup = g_thread_supported();
// extracted from the 'if' to avoid Clang -Wunreachable-code
if ( !sup )
g_thread_init( nullptr );
gdk_threads_set_lock_functions (GdkThreadsEnter, GdkThreadsLeave);
SAL_INFO("vcl.gtk", "Hooked gdk threads locks");
#endif
auto pYieldMutex = std::make_unique<GtkYieldMutex>();
#if !GTK_CHECK_VERSION(4, 0, 0)
gdk_threads_init();
#endif
GtkInstance* pInstance = new GtkInstance( std::move(pYieldMutex) );
SAL_INFO("vcl.gtk", "creating GtkInstance " << pInstance);
// Create SalData, this does not leak
new GtkSalData();
return pInstance;
}
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static VclInputFlags categorizeEvent(const GdkEvent *pEvent)
{
VclInputFlags nType = VclInputFlags::NONE;
switch (gdk_event_get_event_type(pEvent))
{
case GDK_MOTION_NOTIFY:
case GDK_BUTTON_PRESS:
#if !GTK_CHECK_VERSION(4, 0, 0)
case GDK_2BUTTON_PRESS:
case GDK_3BUTTON_PRESS:
#endif
case GDK_BUTTON_RELEASE:
case GDK_ENTER_NOTIFY:
case GDK_LEAVE_NOTIFY:
case GDK_SCROLL:
nType = VclInputFlags::MOUSE;
break;
case GDK_KEY_PRESS:
// case GDK_KEY_RELEASE: //similar to the X11SalInstance one
nType = VclInputFlags::KEYBOARD;
break;
#if !GTK_CHECK_VERSION(4, 0, 0)
case GDK_EXPOSE:
nType = VclInputFlags::PAINT;
break;
#endif
default:
nType = VclInputFlags::OTHER;
break;
}
return nType;
}
#endif
GtkInstance::GtkInstance( std::unique_ptr<SalYieldMutex> pMutex )
: SvpSalInstance( std::move(pMutex) )
, m_pTimer(nullptr)
, bNeedsInit(true)
, m_pLastCairoFontOptions(nullptr)
{
m_bSupportsOpenGL = true;
}
//We want to defer initializing gtk until we are after uno has been
//bootstrapped so we can ask the config what the UI language is so that we can
//force that in as $LANGUAGE to get gtk to render widgets RTL if we have a RTL
//UI in a LTR locale
void GtkInstance::AfterAppInit()
{
EnsureInit();
}
void GtkInstance::EnsureInit()
{
if (!bNeedsInit)
return;
// initialize SalData
GtkSalData *pSalData = GetGtkSalData();
pSalData->Init();
GtkSalData::initNWF();
#if !GTK_CHECK_VERSION(4, 0, 0)
InitAtkBridge();
#endif
ImplSVData* pSVData = ImplGetSVData();
#ifdef GTK_TOOLKIT_NAME
pSVData->maAppData.mxToolkitName = OUString(GTK_TOOLKIT_NAME);
#else
pSVData->maAppData.mxToolkitName = OUString("gtk3");
#endif
bNeedsInit = false;
}
GtkInstance::~GtkInstance()
{
assert( nullptr == m_pTimer );
#if !GTK_CHECK_VERSION(4, 0, 0)
DeInitAtkBridge();
#endif
ResetLastSeenCairoFontOptions(nullptr);
}
SalFrame* GtkInstance::CreateFrame( SalFrame* pParent, SalFrameStyleFlags nStyle )
{
EnsureInit();
return new GtkSalFrame( pParent, nStyle );
}
SalFrame* GtkInstance::CreateChildFrame( SystemParentData* pParentData, SalFrameStyleFlags )
{
EnsureInit();
return new GtkSalFrame( pParentData );
}
SalObject* GtkInstance::CreateObject( SalFrame* pParent, SystemWindowData* pWindowData, bool bShow )
{
EnsureInit();
//FIXME: Missing CreateObject functionality ...
if (pWindowData && pWindowData->bClipUsingNativeWidget)
return new GtkSalObjectWidgetClip(static_cast<GtkSalFrame*>(pParent), bShow);
return new GtkSalObject(static_cast<GtkSalFrame*>(pParent), bShow);
}
extern "C"
{
typedef void*(* getDefaultFnc)();
typedef void(* addItemFnc)(void *, const char *);
}
void GtkInstance::AddToRecentDocumentList(const OUString& rFileUrl, const OUString&, const OUString&)
{
EnsureInit();
OString sGtkURL;
rtl_TextEncoding aSystemEnc = osl_getThreadTextEncoding();
if ((aSystemEnc == RTL_TEXTENCODING_UTF8) || !rFileUrl.startsWith( "file://" ))
sGtkURL = OUStringToOString(rFileUrl, RTL_TEXTENCODING_UTF8);
else
{
//Non-utf8 locales are a bad idea if trying to work with non-ascii filenames
//Decode %XX components
OUString sDecodedUri = rtl::Uri::decode(rFileUrl.copy(7), rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8);
//Convert back to system locale encoding
OString sSystemUrl = OUStringToOString(sDecodedUri, aSystemEnc);
//Encode to an escaped ASCII-encoded URI
gchar *g_uri = g_filename_to_uri(sSystemUrl.getStr(), nullptr, nullptr);
sGtkURL = OString(g_uri);
g_free(g_uri);
}
GtkRecentManager *manager = gtk_recent_manager_get_default ();
gtk_recent_manager_add_item (manager, sGtkURL.getStr());
}
SalInfoPrinter* GtkInstance::CreateInfoPrinter( SalPrinterQueueInfo* pQueueInfo,
ImplJobSetup* pSetupData )
{
EnsureInit();
mbPrinterInit = true;
// create and initialize SalInfoPrinter
PspSalInfoPrinter* pPrinter = new PspSalInfoPrinter;
configurePspInfoPrinter(pPrinter, pQueueInfo, pSetupData);
return pPrinter;
}
std::unique_ptr<SalPrinter> GtkInstance::CreatePrinter( SalInfoPrinter* pInfoPrinter )
{
EnsureInit();
mbPrinterInit = true;
return std::unique_ptr<SalPrinter>(new PspSalPrinter(pInfoPrinter));
}
/*
* These methods always occur in pairs
* A ThreadsEnter is followed by a ThreadsLeave
* We need to queue up the recursive lock count
* for each pair, so we can accurately restore
* it later.
*/
thread_local std::stack<sal_uInt32> GtkYieldMutex::yieldCounts;
void GtkYieldMutex::ThreadsEnter()
{
acquire();
if (yieldCounts.empty())
return;
auto n = yieldCounts.top();
yieldCounts.pop();
const bool bUndoingLeaveWithoutEnter = n == 0;
// if the ThreadsLeave bLeaveWithoutEnter of true condition occurred to
// create this entry then return early undoing the initial acquire of the
// function
if G_UNLIKELY(bUndoingLeaveWithoutEnter)
{
release();
return;
}
assert(n > 0);
n--;
if (n > 0)
acquire(n);
}
void GtkYieldMutex::ThreadsLeave()
{
const bool bLeaveWithoutEnter = m_nCount == 0;
SAL_WARN_IF(bLeaveWithoutEnter, "vcl.gtk", "gdk_threads_leave without matching gdk_threads_enter");
yieldCounts.push(m_nCount);
if G_UNLIKELY(bLeaveWithoutEnter) // this ideally shouldn't happen, but can due to the gtk3 file dialog
return;
release(true);
}
std::unique_ptr<SalVirtualDevice> GtkInstance::CreateVirtualDevice( SalGraphics &rG,
tools::Long &nDX, tools::Long &nDY,
DeviceFormat /*eFormat*/,
const SystemGraphicsData* pGd )
{
EnsureInit();
SvpSalGraphics *pSvpSalGraphics = dynamic_cast<SvpSalGraphics*>(&rG);
assert(pSvpSalGraphics);
// tdf#127529 see SvpSalInstance::CreateVirtualDevice for the rare case of a non-null pPreExistingTarget
cairo_surface_t* pPreExistingTarget = pGd ? static_cast<cairo_surface_t*>(pGd->pSurface) : nullptr;
std::unique_ptr<SalVirtualDevice> xNew(new SvpSalVirtualDevice(pSvpSalGraphics->getSurface(), pPreExistingTarget));
if (!xNew->SetSize(nDX, nDY))
xNew.reset();
return xNew;
}
std::shared_ptr<SalBitmap> GtkInstance::CreateSalBitmap()
{
EnsureInit();
return SvpSalInstance::CreateSalBitmap();
}
std::unique_ptr<SalMenu> GtkInstance::CreateMenu( bool bMenuBar, Menu* pVCLMenu )
{
EnsureInit();
GtkSalMenu* pSalMenu = new GtkSalMenu( bMenuBar );
pSalMenu->SetMenu( pVCLMenu );
return std::unique_ptr<SalMenu>(pSalMenu);
}
std::unique_ptr<SalMenuItem> GtkInstance::CreateMenuItem( const SalItemParams & rItemData )
{
EnsureInit();
return std::unique_ptr<SalMenuItem>(new GtkSalMenuItem( &rItemData ));
}
SalTimer* GtkInstance::CreateSalTimer()
{
EnsureInit();
assert( nullptr == m_pTimer );
if ( nullptr == m_pTimer )
m_pTimer = new GtkSalTimer();
return m_pTimer;
}
void GtkInstance::RemoveTimer ()
{
EnsureInit();
m_pTimer = nullptr;
}
bool GtkInstance::DoYield(bool bWait, bool bHandleAllCurrentEvents)
{
EnsureInit();
return GetGtkSalData()->Yield( bWait, bHandleAllCurrentEvents );
}
bool GtkInstance::IsTimerExpired()
{
EnsureInit();
return (m_pTimer && m_pTimer->Expired());
}
namespace
{
bool DisplayHasAnyInput()
{
GdkDisplay* pDisplay = gdk_display_get_default();
#if defined(GDK_WINDOWING_WAYLAND)
if (DLSYM_GDK_IS_WAYLAND_DISPLAY(pDisplay))
{
bool bRet = false;
wl_display* pWLDisplay = gdk_wayland_display_get_wl_display(pDisplay);
static auto wayland_display_get_fd = reinterpret_cast<int (*) (wl_display*)>(dlsym(nullptr, "wl_display_get_fd"));
if (wayland_display_get_fd)
{
GPollFD aPollFD;
aPollFD.fd = wayland_display_get_fd(pWLDisplay);
aPollFD.events = G_IO_IN | G_IO_ERR | G_IO_HUP;
bRet = g_poll(&aPollFD, 1, 0) > 0;
}
return bRet;
}
#endif
#if defined(GDK_WINDOWING_X11)
if (DLSYM_GDK_IS_X11_DISPLAY(pDisplay))
{
GPollFD aPollFD;
aPollFD.fd = ConnectionNumber(gdk_x11_display_get_xdisplay(pDisplay));
aPollFD.events = G_IO_IN;
return g_poll(&aPollFD, 1, 0) > 0;
}
#endif
return false;
}
}
bool GtkInstance::AnyInput( VclInputFlags nType )
{
EnsureInit();
if( (nType & VclInputFlags::TIMER) && IsTimerExpired() )
return true;
// strip timer bits now
nType = nType & ~VclInputFlags::TIMER;
static constexpr VclInputFlags ANY_INPUT_EXCLUDING_TIMER = VCL_INPUT_ANY & ~VclInputFlags::TIMER;
const bool bCheckForAnyInput = nType == ANY_INPUT_EXCLUDING_TIMER;
bool bRet = false;
if (bCheckForAnyInput)
bRet = DisplayHasAnyInput();
#if !GTK_CHECK_VERSION(4, 0, 0)
GdkDisplay* pDisplay = gdk_display_get_default();
if (!gdk_display_has_pending(pDisplay))
return bRet;
if (bCheckForAnyInput)
return true;
std::deque<GdkEvent*> aEvents;
GdkEvent *pEvent = nullptr;
while ((pEvent = gdk_display_get_event(pDisplay)))
{
aEvents.push_back(pEvent);
VclInputFlags nEventType = categorizeEvent(pEvent);
if ( (nEventType & nType) || ( nEventType == VclInputFlags::NONE && (nType & VclInputFlags::OTHER) ) )
{
bRet = true;
}
}
while (!aEvents.empty())
{
pEvent = aEvents.front();
gdk_display_put_event(pDisplay, pEvent);
gdk_event_free(pEvent);
aEvents.pop_front();
}
#endif
return bRet;
}
std::unique_ptr<GenPspGraphics> GtkInstance::CreatePrintGraphics()
{
EnsureInit();
return std::make_unique<GenPspGraphics>();
}
const cairo_font_options_t* GtkInstance::GetCairoFontOptions()
{
#if !GTK_CHECK_VERSION(4, 0, 0)
const cairo_font_options_t* pCairoFontOptions = gdk_screen_get_font_options(gdk_screen_get_default());
#else
auto pDefaultWin = ImplGetDefaultWindow();
assert(pDefaultWin);
SalFrame* pDefaultFrame = pDefaultWin->ImplGetFrame();
GtkSalFrame* pGtkFrame = dynamic_cast<GtkSalFrame*>(pDefaultFrame);
assert(pGtkFrame);
const cairo_font_options_t* pCairoFontOptions = pGtkFrame->get_font_options();
#endif
if (!m_pLastCairoFontOptions && pCairoFontOptions)
m_pLastCairoFontOptions = cairo_font_options_copy(pCairoFontOptions);
return pCairoFontOptions;
}
const cairo_font_options_t* GtkInstance::GetLastSeenCairoFontOptions() const
{
return m_pLastCairoFontOptions;
}
void GtkInstance::ResetLastSeenCairoFontOptions(const cairo_font_options_t* pCairoFontOptions)
{
if (m_pLastCairoFontOptions)
cairo_font_options_destroy(m_pLastCairoFontOptions);
if (pCairoFontOptions)
m_pLastCairoFontOptions = cairo_font_options_copy(pCairoFontOptions);
else
m_pLastCairoFontOptions = nullptr;
}
namespace
{
struct TypeEntry
{
const char* pNativeType; // string corresponding to nAtom for the case of nAtom being uninitialized
const char* pType; // Mime encoding on our side
};
const TypeEntry aConversionTab[] =
{
{ "ISO10646-1", "text/plain;charset=utf-16" },
{ "UTF8_STRING", "text/plain;charset=utf-8" },
{ "UTF-8", "text/plain;charset=utf-8" },
{ "text/plain;charset=UTF-8", "text/plain;charset=utf-8" },
// ISO encodings
{ "ISO8859-2", "text/plain;charset=iso8859-2" },
{ "ISO8859-3", "text/plain;charset=iso8859-3" },
{ "ISO8859-4", "text/plain;charset=iso8859-4" },
{ "ISO8859-5", "text/plain;charset=iso8859-5" },
{ "ISO8859-6", "text/plain;charset=iso8859-6" },
{ "ISO8859-7", "text/plain;charset=iso8859-7" },
{ "ISO8859-8", "text/plain;charset=iso8859-8" },
{ "ISO8859-9", "text/plain;charset=iso8859-9" },
{ "ISO8859-10", "text/plain;charset=iso8859-10" },
{ "ISO8859-13", "text/plain;charset=iso8859-13" },
{ "ISO8859-14", "text/plain;charset=iso8859-14" },
{ "ISO8859-15", "text/plain;charset=iso8859-15" },
// asian encodings
{ "JISX0201.1976-0", "text/plain;charset=jisx0201.1976-0" },
{ "JISX0208.1983-0", "text/plain;charset=jisx0208.1983-0" },
{ "JISX0208.1990-0", "text/plain;charset=jisx0208.1990-0" },
{ "JISX0212.1990-0", "text/plain;charset=jisx0212.1990-0" },
{ "GB2312.1980-0", "text/plain;charset=gb2312.1980-0" },
{ "KSC5601.1992-0", "text/plain;charset=ksc5601.1992-0" },
// eastern european encodings
{ "KOI8-R", "text/plain;charset=koi8-r" },
{ "KOI8-U", "text/plain;charset=koi8-u" },
// String (== iso8859-1)
{ "STRING", "text/plain;charset=iso8859-1" },
// special for compound text
{ "COMPOUND_TEXT", "text/plain;charset=compound_text" },
// PIXMAP
{ "PIXMAP", "image/bmp" }
};
class DataFlavorEq
{
private:
const css::datatransfer::DataFlavor& m_rData;
public:
explicit DataFlavorEq(const css::datatransfer::DataFlavor& rData) : m_rData(rData) {}
bool operator() (const css::datatransfer::DataFlavor& rData) const
{
return rData.MimeType == m_rData.MimeType &&
rData.DataType == m_rData.DataType;
}
};
}
#if GTK_CHECK_VERSION(4, 0, 0)
std::vector<css::datatransfer::DataFlavor> GtkTransferable::getTransferDataFlavorsAsVector(const char * const *targets, gint n_targets)
#else
std::vector<css::datatransfer::DataFlavor> GtkTransferable::getTransferDataFlavorsAsVector(GdkAtom *targets, gint n_targets)
#endif
{
std::vector<css::datatransfer::DataFlavor> aVector;
bool bHaveText = false, bHaveUTF16 = false;
for (gint i = 0; i < n_targets; ++i)
{
#if GTK_CHECK_VERSION(4, 0, 0)
const gchar* pName = targets[i];
#else
gchar* pName = gdk_atom_name(targets[i]);
#endif
const char* pFinalName = pName;
css::datatransfer::DataFlavor aFlavor;
// omit text/plain;charset=unicode since it is not well defined
if (rtl_str_compare(pName, "text/plain;charset=unicode") == 0)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
g_free(pName);
#endif
continue;
}
for (size_t j = 0; j < SAL_N_ELEMENTS(aConversionTab); ++j)
{
if (rtl_str_compare(pName, aConversionTab[j].pNativeType) == 0)
{
pFinalName = aConversionTab[j].pType;
break;
}
}
// There are more non-MIME-types reported that are not translated by
// aConversionTab, like "SAVE_TARGETS", "INTEGER", "ATOM"; just filter
// them out for now before they confuse this code's clients:
if (rtl_str_indexOfChar(pFinalName, '/') == -1)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
g_free(pName);
#endif
continue;
}
aFlavor.MimeType = OUString(pFinalName,
strlen(pFinalName),
RTL_TEXTENCODING_UTF8);
m_aMimeTypeToGtkType[aFlavor.MimeType] = targets[i];
aFlavor.DataType = cppu::UnoType<Sequence< sal_Int8 >>::get();
sal_Int32 nIndex(0);
if (o3tl::getToken(aFlavor.MimeType, 0, ';', nIndex) == u"text/plain")
{
bHaveText = true;
std::u16string_view aToken(o3tl::getToken(aFlavor.MimeType, 0, ';', nIndex));
if (aToken == u"charset=utf-16")
{
bHaveUTF16 = true;
aFlavor.DataType = cppu::UnoType<OUString>::get();
}
}
aVector.push_back(aFlavor);
#if !GTK_CHECK_VERSION(4, 0, 0)
g_free(pName);
#endif
}
//If we have text, but no UTF-16 format which is basically the only
//text-format LibreOffice supports for cnp then claim we do and we
//will convert on demand
if (bHaveText && !bHaveUTF16)
{
css::datatransfer::DataFlavor aFlavor;
aFlavor.MimeType = "text/plain;charset=utf-16";
aFlavor.DataType = cppu::UnoType<OUString>::get();
aVector.push_back(aFlavor);
}
return aVector;
}
css::uno::Sequence<css::datatransfer::DataFlavor> SAL_CALL GtkTransferable::getTransferDataFlavors()
{
return comphelper::containerToSequence(getTransferDataFlavorsAsVector());
}
sal_Bool SAL_CALL GtkTransferable::isDataFlavorSupported(const css::datatransfer::DataFlavor& rFlavor)
{
const std::vector<css::datatransfer::DataFlavor> aAll =
getTransferDataFlavorsAsVector();
return std::any_of(aAll.begin(), aAll.end(), DataFlavorEq(rFlavor));
}
#if GTK_CHECK_VERSION(4, 0, 0)
void read_transfer_result::read_block_async_completed(GObject* source, GAsyncResult* res, gpointer user_data)
{
GInputStream* stream = G_INPUT_STREAM(source);
read_transfer_result* pRes = static_cast<read_transfer_result*>(user_data);
gsize bytes_read = g_input_stream_read_finish(stream, res, nullptr);
bool bFinished = bytes_read == 0;
if (bFinished)
{
g_object_unref(stream);
pRes->aVector.resize(pRes->nRead);
pRes->bDone = true;
g_main_context_wakeup(nullptr);
return;
}
pRes->nRead += bytes_read;
pRes->aVector.resize(pRes->nRead + read_transfer_result::BlockSize);
g_input_stream_read_async(stream,
pRes->aVector.data() + pRes->nRead,
read_transfer_result::BlockSize,
G_PRIORITY_DEFAULT,
nullptr,
read_block_async_completed,
user_data);
}
OUString read_transfer_result::get_as_string() const
{
const char* pStr = reinterpret_cast<const char*>(aVector.data());
return OUString(pStr, aVector.size(), RTL_TEXTENCODING_UTF8).replaceAll("\r\n", "\n");
}
css::uno::Sequence<sal_Int8> read_transfer_result::get_as_sequence() const
{
return css::uno::Sequence<sal_Int8>(aVector.data(), aVector.size());
}
#endif
namespace {
GdkClipboard* clipboard_get(SelectionType eSelection)
{
#if GTK_CHECK_VERSION(4, 0, 0)
if (eSelection == SELECTION_CLIPBOARD)
return gdk_display_get_clipboard(gdk_display_get_default());
return gdk_display_get_primary_clipboard(gdk_display_get_default());
#else
return gtk_clipboard_get(eSelection == SELECTION_CLIPBOARD ? GDK_SELECTION_CLIPBOARD : GDK_SELECTION_PRIMARY);
#endif
}
#if GTK_CHECK_VERSION(4, 0, 0)
void read_clipboard_async_completed(GObject* source, GAsyncResult* res, gpointer user_data)
{
GdkClipboard* clipboard = GDK_CLIPBOARD(source);
read_transfer_result* pRes = static_cast<read_transfer_result*>(user_data);
GInputStream* pResult = gdk_clipboard_read_finish(clipboard, res, nullptr, nullptr);
if (!pResult)
{
pRes->bDone = true;
g_main_context_wakeup(nullptr);
return;
}
pRes->aVector.resize(read_transfer_result::BlockSize);
g_input_stream_read_async(pResult,
pRes->aVector.data(),
pRes->aVector.size(),
G_PRIORITY_DEFAULT,
nullptr,
read_transfer_result::read_block_async_completed,
user_data);
}
#endif
class GtkClipboardTransferable : public GtkTransferable
{
private:
SelectionType m_eSelection;
public:
explicit GtkClipboardTransferable(SelectionType eSelection)
: m_eSelection(eSelection)
{
}
/*
* XTransferable
*/
virtual css::uno::Any SAL_CALL getTransferData(const css::datatransfer::DataFlavor& rFlavor) override
{
css::uno::Any aRet;
css::datatransfer::DataFlavor aFlavor(rFlavor);
if (aFlavor.MimeType == "text/plain;charset=utf-16")
aFlavor.MimeType = "text/plain;charset=utf-8";
GdkClipboard* clipboard = clipboard_get(m_eSelection);
#if !GTK_CHECK_VERSION(4, 0, 0)
if (aFlavor.MimeType == "text/plain;charset=utf-8")
{
gchar *pText = gtk_clipboard_wait_for_text(clipboard);
OUString aStr(pText, pText ? strlen(pText) : 0, RTL_TEXTENCODING_UTF8);
g_free(pText);
aRet <<= aStr.replaceAll("\r\n", "\n");
return aRet;
}
#endif
auto it = m_aMimeTypeToGtkType.find(aFlavor.MimeType);
if (it == m_aMimeTypeToGtkType.end())
return css::uno::Any();
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard,
it->second);
if (!data)
{
return css::uno::Any();
}
gint length;
const guchar *rawdata = gtk_selection_data_get_data_with_length(data,
&length);
Sequence<sal_Int8> aSeq(reinterpret_cast<const sal_Int8*>(rawdata), length);
gtk_selection_data_free(data);
aRet <<= aSeq;
#else
SalInstance* pInstance = GetSalInstance();
read_transfer_result aRes;
const char *mime_types[] = { it->second.getStr(), nullptr };
gdk_clipboard_read_async(clipboard,
mime_types,
G_PRIORITY_DEFAULT,
nullptr,
read_clipboard_async_completed,
&aRes);
while (!aRes.bDone)
pInstance->DoYield(true, false);
if (aFlavor.MimeType == "text/plain;charset=utf-8")
aRet <<= aRes.get_as_string();
else
aRet <<= aRes.get_as_sequence();
#endif
return aRet;
}
std::vector<css::datatransfer::DataFlavor> getTransferDataFlavorsAsVector()
override
{
std::vector<css::datatransfer::DataFlavor> aVector;
GdkClipboard* clipboard = clipboard_get(m_eSelection);
#if GTK_CHECK_VERSION(4, 0, 0)
GdkContentFormats* pFormats = gdk_clipboard_get_formats(clipboard);
gsize n_targets;
const char * const *targets = gdk_content_formats_get_mime_types(pFormats, &n_targets);
aVector = GtkTransferable::getTransferDataFlavorsAsVector(targets, n_targets);
#else
GdkAtom *targets;
gint n_targets;
if (gtk_clipboard_wait_for_targets(clipboard, &targets, &n_targets))
{
aVector = GtkTransferable::getTransferDataFlavorsAsVector(targets, n_targets);
g_free(targets);
}
#endif
return aVector;
}
};
class VclGtkClipboard :
public cppu::WeakComponentImplHelper<
datatransfer::clipboard::XSystemClipboard,
datatransfer::clipboard::XFlushableClipboard,
XServiceInfo>
{
SelectionType m_eSelection;
osl::Mutex m_aMutex;
gulong m_nOwnerChangedSignalId;
ImplSVEvent* m_pSetClipboardEvent;
Reference<css::datatransfer::XTransferable> m_aContents;
Reference<css::datatransfer::clipboard::XClipboardOwner> m_aOwner;
std::vector< Reference<css::datatransfer::clipboard::XClipboardListener> > m_aListeners;
#if GTK_CHECK_VERSION(4, 0, 0)
std::vector<OString> m_aGtkTargets;
TransferableContent* m_pClipboardContent;
#else
std::vector<GtkTargetEntry> m_aGtkTargets;
#endif
VclToGtkHelper m_aConversionHelper;
DECL_LINK(AsyncSetGtkClipboard, void*, void);
#if GTK_CHECK_VERSION(4, 0, 0)
DECL_LINK(DetachClipboard, void*, void);
#endif
public:
explicit VclGtkClipboard(SelectionType eSelection);
virtual ~VclGtkClipboard() override;
/*
* XServiceInfo
*/
virtual OUString SAL_CALL getImplementationName() override;
virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
/*
* XClipboard
*/
virtual Reference< css::datatransfer::XTransferable > SAL_CALL getContents() override;
virtual void SAL_CALL setContents(
const Reference< css::datatransfer::XTransferable >& xTrans,
const Reference< css::datatransfer::clipboard::XClipboardOwner >& xClipboardOwner ) override;
virtual OUString SAL_CALL getName() override;
/*
* XClipboardEx
*/
virtual sal_Int8 SAL_CALL getRenderingCapabilities() override;
/*
* XFlushableClipboard
*/
virtual void SAL_CALL flushClipboard() override;
/*
* XClipboardNotifier
*/
virtual void SAL_CALL addClipboardListener(
const Reference< css::datatransfer::clipboard::XClipboardListener >& listener ) override;
virtual void SAL_CALL removeClipboardListener(
const Reference< css::datatransfer::clipboard::XClipboardListener >& listener ) override;
#if !GTK_CHECK_VERSION(4, 0, 0)
void ClipboardGet(GtkSelectionData *selection_data, guint info);
#endif
void OwnerPossiblyChanged(GdkClipboard *clipboard);
void ClipboardClear();
void SetGtkClipboard();
void SyncGtkClipboard();
};
}
OUString VclGtkClipboard::getImplementationName()
{
return "com.sun.star.datatransfer.VclGtkClipboard";
}
Sequence< OUString > VclGtkClipboard::getSupportedServiceNames()
{
Sequence<OUString> aRet { "com.sun.star.datatransfer.clipboard.SystemClipboard" };
return aRet;
}
sal_Bool VclGtkClipboard::supportsService( const OUString& ServiceName )
{
return cppu::supportsService(this, ServiceName);
}
Reference< css::datatransfer::XTransferable > VclGtkClipboard::getContents()
{
if (!m_aContents.is())
{
//tdf#93887 This is the system clipboard/selection. We fetch it when we are not
//the owner of the clipboard and have not already fetched it.
m_aContents = new GtkClipboardTransferable(m_eSelection);
#if GTK_CHECK_VERSION(4, 0, 0)
if (m_pClipboardContent)
transerable_content_set_transferable(m_pClipboardContent, m_aContents.get());
#endif
}
return m_aContents;
}
#if !GTK_CHECK_VERSION(4, 0, 0)
void VclGtkClipboard::ClipboardGet(GtkSelectionData *selection_data, guint info)
{
if (!m_aContents.is())
return;
// tdf#129809 take a reference in case m_aContents is replaced during this
// call
Reference<datatransfer::XTransferable> xCurrentContents(m_aContents);
m_aConversionHelper.setSelectionData(xCurrentContents, selection_data, info);
}
namespace
{
const OString& getPID()
{
static OString sPID;
if (!sPID.getLength())
{
oslProcessIdentifier aProcessId = 0;
oslProcessInfo info;
info.Size = sizeof (oslProcessInfo);
if (osl_getProcessInfo(nullptr, osl_Process_IDENTIFIER, &info) == osl_Process_E_None)
aProcessId = info.Ident;
sPID = OString::number(aProcessId);
}
return sPID;
}
void ClipboardGetFunc(GdkClipboard* /*clipboard*/, GtkSelectionData *selection_data,
guint info,
gpointer user_data_or_owner)
{
VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data_or_owner);
pThis->ClipboardGet(selection_data, info);
}
void ClipboardClearFunc(GdkClipboard* /*clipboard*/, gpointer user_data_or_owner)
{
VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data_or_owner);
pThis->ClipboardClear();
}
}
#endif
namespace
{
#if GTK_CHECK_VERSION(4, 0, 0)
void handle_owner_change(GdkClipboard *clipboard, gpointer user_data)
{
VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data);
pThis->OwnerPossiblyChanged(clipboard);
}
#else
void handle_owner_change(GdkClipboard *clipboard, GdkEvent* /*event*/, gpointer user_data)
{
VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data);
pThis->OwnerPossiblyChanged(clipboard);
}
#endif
}
void VclGtkClipboard::OwnerPossiblyChanged(GdkClipboard* clipboard)
{
SyncGtkClipboard(); // tdf#138183 do any pending SetGtkClipboard calls
if (!m_aContents.is())
return;
#if GTK_CHECK_VERSION(4, 0, 0)
bool bSelf = gdk_clipboard_is_local(clipboard);
#else
//if gdk_display_supports_selection_notification is not supported, e.g. like
//right now under wayland, then you only get owner-changed notifications at
//opportune times when the selection might have changed. So here
//we see if the selection supports a dummy selection type identifying
//our pid, in which case it's us.
bool bSelf = false;
//disconnect and reconnect after gtk_clipboard_wait_for_targets to
//avoid possible recursion
g_signal_handler_disconnect(clipboard, m_nOwnerChangedSignalId);
OString sTunnel = "application/x-libreoffice-internal-id-" + getPID();
GdkAtom *targets;
gint n_targets;
if (gtk_clipboard_wait_for_targets(clipboard, &targets, &n_targets))
{
for (gint i = 0; i < n_targets && !bSelf; ++i)
{
gchar* pName = gdk_atom_name(targets[i]);
if (strcmp(pName, sTunnel.getStr()) == 0)
{
bSelf = true;
}
g_free(pName);
}
g_free(targets);
}
m_nOwnerChangedSignalId = g_signal_connect(clipboard, "owner-change",
G_CALLBACK(handle_owner_change), this);
#endif
if (!bSelf)
{
//null out m_aContents to return control to the system-one which
//will be retrieved if getContents is called again
setContents(Reference<css::datatransfer::XTransferable>(),
Reference<css::datatransfer::clipboard::XClipboardOwner>());
}
}
void VclGtkClipboard::ClipboardClear()
{
if (m_pSetClipboardEvent)
{
Application::RemoveUserEvent(m_pSetClipboardEvent);
m_pSetClipboardEvent = nullptr;
}
#if !GTK_CHECK_VERSION(4, 0, 0)
for (auto &a : m_aGtkTargets)
g_free(a.target);
#endif
m_aGtkTargets.clear();
}
#if GTK_CHECK_VERSION(4, 0, 0)
IMPL_LINK_NOARG(VclGtkClipboard, DetachClipboard, void*, void)
{
ClipboardClear();
}
OString VclToGtkHelper::makeGtkTargetEntry(const css::datatransfer::DataFlavor& rFlavor)
{
OString aEntry = OUStringToOString(rFlavor.MimeType, RTL_TEXTENCODING_UTF8);
auto it = std::find_if(aInfoToFlavor.begin(), aInfoToFlavor.end(),
DataFlavorEq(rFlavor));
if (it == aInfoToFlavor.end())
aInfoToFlavor.push_back(rFlavor);
return aEntry;
}
#else
GtkTargetEntry VclToGtkHelper::makeGtkTargetEntry(const css::datatransfer::DataFlavor& rFlavor)
{
GtkTargetEntry aEntry;
aEntry.target =
g_strdup(OUStringToOString(rFlavor.MimeType, RTL_TEXTENCODING_UTF8).getStr());
aEntry.flags = 0;
auto it = std::find_if(aInfoToFlavor.begin(), aInfoToFlavor.end(),
DataFlavorEq(rFlavor));
if (it != aInfoToFlavor.end())
aEntry.info = std::distance(aInfoToFlavor.begin(), it);
else
{
aEntry.info = aInfoToFlavor.size();
aInfoToFlavor.push_back(rFlavor);
}
return aEntry;
}
#endif
#if GTK_CHECK_VERSION(4, 0, 0)
namespace
{
void write_mime_type_done(GObject* pStream, GAsyncResult* pResult, gpointer pTaskPtr)
{
GTask* pTask = static_cast<GTask*>(pTaskPtr);
GError* pError = nullptr;
if (!g_output_stream_write_all_finish(G_OUTPUT_STREAM(pStream),
pResult, nullptr, &pError))
{
g_task_return_error(pTask, pError);
}
else
{
g_task_return_boolean(pTask, true);
}
g_object_unref(pTask);
}
class MimeTypeEq
{
private:
const OUString& m_rMimeType;
public:
explicit MimeTypeEq(const OUString& rMimeType) : m_rMimeType(rMimeType) {}
bool operator() (const css::datatransfer::DataFlavor& rData) const
{
return rData.MimeType == m_rMimeType;
}
};
}
void VclToGtkHelper::setSelectionData(const Reference<css::datatransfer::XTransferable> &rTrans,
GdkContentProvider* provider,
const char* mime_type,
GOutputStream* stream,
int io_priority,
GCancellable* cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task = g_task_new(provider, cancellable, callback, user_data);
g_task_set_priority(task, io_priority);
OUString sMimeType(mime_type, strlen(mime_type), RTL_TEXTENCODING_UTF8);
auto it = std::find_if(aInfoToFlavor.begin(), aInfoToFlavor.end(),
MimeTypeEq(sMimeType));
if (it == aInfoToFlavor.end())
{
SAL_WARN( "vcl.gtk", "unknown mime-type request from clipboard");
g_task_return_new_error(task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"unknown mime-type “%s” request from clipboard", mime_type);
g_object_unref(task);
return;
}
css::datatransfer::DataFlavor aFlavor(*it);
if (aFlavor.MimeType == "UTF8_STRING" || aFlavor.MimeType == "STRING")
aFlavor.MimeType = "text/plain;charset=utf-8";
Sequence<sal_Int8> aData;
Any aValue;
try
{
aValue = rTrans->getTransferData(aFlavor);
}
catch (...)
{
}
if (aValue.getValueTypeClass() == TypeClass_STRING)
{
OUString aString;
aValue >>= aString;
aData = Sequence< sal_Int8 >( reinterpret_cast<sal_Int8 const *>(aString.getStr()), aString.getLength() * sizeof( sal_Unicode ) );
}
else if (aValue.getValueType() == cppu::UnoType<Sequence< sal_Int8 >>::get())
{
aValue >>= aData;
}
else if (aFlavor.MimeType == "text/plain;charset=utf-8")
{
//didn't have utf-8, try utf-16 and convert
aFlavor.MimeType = "text/plain;charset=utf-16";
aFlavor.DataType = cppu::UnoType<OUString>::get();
try
{
aValue = rTrans->getTransferData(aFlavor);
}
catch (...)
{
}
OUString aString;
aValue >>= aString;
OString aUTF8String(OUStringToOString(aString, RTL_TEXTENCODING_UTF8));
g_output_stream_write_all_async(stream, aUTF8String.getStr(), aUTF8String.getLength(),
io_priority, cancellable, write_mime_type_done, task);
return;
}
g_output_stream_write_all_async(stream, aData.getArray(), aData.getLength(),
io_priority, cancellable, write_mime_type_done, task);
}
#else
void VclToGtkHelper::setSelectionData(const Reference<css::datatransfer::XTransferable> &rTrans,
GtkSelectionData *selection_data, guint info)
{
GdkAtom type(gdk_atom_intern(OUStringToOString(aInfoToFlavor[info].MimeType,
RTL_TEXTENCODING_UTF8).getStr(),
false));
css::datatransfer::DataFlavor aFlavor(aInfoToFlavor[info]);
if (aFlavor.MimeType == "UTF8_STRING" || aFlavor.MimeType == "STRING")
aFlavor.MimeType = "text/plain;charset=utf-8";
Sequence<sal_Int8> aData;
Any aValue;
try
{
aValue = rTrans->getTransferData(aFlavor);
}
catch (...)
{
}
if (aValue.getValueTypeClass() == TypeClass_STRING)
{
OUString aString;
aValue >>= aString;
aData = Sequence< sal_Int8 >( reinterpret_cast<sal_Int8 const *>(aString.getStr()), aString.getLength() * sizeof( sal_Unicode ) );
}
else if (aValue.getValueType() == cppu::UnoType<Sequence< sal_Int8 >>::get())
{
aValue >>= aData;
}
else if (aFlavor.MimeType == "text/plain;charset=utf-8")
{
//didn't have utf-8, try utf-16 and convert
aFlavor.MimeType = "text/plain;charset=utf-16";
aFlavor.DataType = cppu::UnoType<OUString>::get();
try
{
aValue = rTrans->getTransferData(aFlavor);
}
catch (...)
{
}
OUString aString;
aValue >>= aString;
OString aUTF8String(OUStringToOString(aString, RTL_TEXTENCODING_UTF8));
gtk_selection_data_set(selection_data, type, 8,
reinterpret_cast<const guchar *>(aUTF8String.getStr()),
aUTF8String.getLength());
return;
}
gtk_selection_data_set(selection_data, type, 8,
reinterpret_cast<const guchar *>(aData.getArray()),
aData.getLength());
}
#endif
VclGtkClipboard::VclGtkClipboard(SelectionType eSelection)
: cppu::WeakComponentImplHelper<datatransfer::clipboard::XSystemClipboard,
datatransfer::clipboard::XFlushableClipboard, XServiceInfo>
(m_aMutex)
, m_eSelection(eSelection)
, m_pSetClipboardEvent(nullptr)
#if GTK_CHECK_VERSION(4, 0, 0)
, m_pClipboardContent(nullptr)
#endif
{
GdkClipboard* clipboard = clipboard_get(m_eSelection);
#if GTK_CHECK_VERSION(4, 0, 0)
m_nOwnerChangedSignalId = g_signal_connect(clipboard, "changed",
G_CALLBACK(handle_owner_change), this);
#else
m_nOwnerChangedSignalId = g_signal_connect(clipboard, "owner-change",
G_CALLBACK(handle_owner_change), this);
#endif
}
void VclGtkClipboard::flushClipboard()
{
#if !GTK_CHECK_VERSION(4, 0, 0)
SolarMutexGuard aGuard;
if (m_eSelection != SELECTION_CLIPBOARD)
return;
GdkClipboard* clipboard = clipboard_get(m_eSelection);
gtk_clipboard_store(clipboard);
#endif
}
VclGtkClipboard::~VclGtkClipboard()
{
GdkClipboard* clipboard = clipboard_get(m_eSelection);
g_signal_handler_disconnect(clipboard, m_nOwnerChangedSignalId);
if (!m_aGtkTargets.empty())
{
#if GTK_CHECK_VERSION(4, 0, 0)
gdk_clipboard_set_content(clipboard, nullptr);
m_pClipboardContent = nullptr;
#else
gtk_clipboard_clear(clipboard);
#endif
ClipboardClear();
}
assert(!m_pSetClipboardEvent);
assert(m_aGtkTargets.empty());
}
#if GTK_CHECK_VERSION(4, 0, 0)
std::vector<OString> VclToGtkHelper::FormatsToGtk(const css::uno::Sequence<css::datatransfer::DataFlavor> &rFormats)
#else
std::vector<GtkTargetEntry> VclToGtkHelper::FormatsToGtk(const css::uno::Sequence<css::datatransfer::DataFlavor> &rFormats)
#endif
{
#if GTK_CHECK_VERSION(4, 0, 0)
std::vector<OString> aGtkTargets;
#else
std::vector<GtkTargetEntry> aGtkTargets;
#endif
bool bHaveText(false), bHaveUTF8(false);
for (const css::datatransfer::DataFlavor& rFlavor : rFormats)
{
sal_Int32 nIndex(0);
if (o3tl::getToken(rFlavor.MimeType, 0, ';', nIndex) == u"text/plain")
{
bHaveText = true;
std::u16string_view aToken(o3tl::getToken(rFlavor.MimeType, 0, ';', nIndex));
if (aToken == u"charset=utf-8")
{
bHaveUTF8 = true;
}
}
aGtkTargets.push_back(makeGtkTargetEntry(rFlavor));
}
if (bHaveText)
{
css::datatransfer::DataFlavor aFlavor;
aFlavor.DataType = cppu::UnoType<Sequence< sal_Int8 >>::get();
if (!bHaveUTF8)
{
aFlavor.MimeType = "text/plain;charset=utf-8";
aGtkTargets.push_back(makeGtkTargetEntry(aFlavor));
}
aFlavor.MimeType = "UTF8_STRING";
aGtkTargets.push_back(makeGtkTargetEntry(aFlavor));
aFlavor.MimeType = "STRING";
aGtkTargets.push_back(makeGtkTargetEntry(aFlavor));
}
return aGtkTargets;
}
IMPL_LINK_NOARG(VclGtkClipboard, AsyncSetGtkClipboard, void*, void)
{
osl::ClearableMutexGuard aGuard( m_aMutex );
m_pSetClipboardEvent = nullptr;
SetGtkClipboard();
}
void VclGtkClipboard::SyncGtkClipboard()
{
osl::ClearableMutexGuard aGuard(m_aMutex);
if (m_pSetClipboardEvent)
{
Application::RemoveUserEvent(m_pSetClipboardEvent);
m_pSetClipboardEvent = nullptr;
SetGtkClipboard();
}
}
void VclGtkClipboard::SetGtkClipboard()
{
GdkClipboard* clipboard = clipboard_get(m_eSelection);
#if GTK_CHECK_VERSION(4, 0, 0)
m_pClipboardContent = TRANSFERABLE_CONTENT(transerable_content_new(&m_aConversionHelper, m_aContents.get()));
transerable_content_set_detach_clipboard_link(m_pClipboardContent, LINK(this, VclGtkClipboard, DetachClipboard));
gdk_clipboard_set_content(clipboard, GDK_CONTENT_PROVIDER(m_pClipboardContent));
#else
gtk_clipboard_set_with_data(clipboard, m_aGtkTargets.data(), m_aGtkTargets.size(),
ClipboardGetFunc, ClipboardClearFunc, this);
gtk_clipboard_set_can_store(clipboard, m_aGtkTargets.data(), m_aGtkTargets.size());
#endif
}
void VclGtkClipboard::setContents(
const Reference< css::datatransfer::XTransferable >& xTrans,
const Reference< css::datatransfer::clipboard::XClipboardOwner >& xClipboardOwner )
{
css::uno::Sequence<css::datatransfer::DataFlavor> aFormats;
if (xTrans.is())
{
aFormats = xTrans->getTransferDataFlavors();
}
osl::ClearableMutexGuard aGuard( m_aMutex );
Reference< datatransfer::clipboard::XClipboardOwner > xOldOwner( m_aOwner );
Reference< datatransfer::XTransferable > xOldContents( m_aContents );
m_aContents = xTrans;
#if GTK_CHECK_VERSION(4, 0, 0)
if (m_pClipboardContent)
transerable_content_set_transferable(m_pClipboardContent, m_aContents.get());
#endif
m_aOwner = xClipboardOwner;
std::vector< Reference< datatransfer::clipboard::XClipboardListener > > aListeners( m_aListeners );
datatransfer::clipboard::ClipboardEvent aEv;
GdkClipboard* clipboard = clipboard_get(m_eSelection);
if (!m_aGtkTargets.empty())
{
#if GTK_CHECK_VERSION(4, 0, 0)
gdk_clipboard_set_content(clipboard, nullptr);
m_pClipboardContent = nullptr;
#else
gtk_clipboard_clear(clipboard);
#endif
ClipboardClear();
}
assert(m_aGtkTargets.empty());
if (m_aContents.is())
{
#if GTK_CHECK_VERSION(4, 0, 0)
std::vector<OString> aGtkTargets(m_aConversionHelper.FormatsToGtk(aFormats));
#else
std::vector<GtkTargetEntry> aGtkTargets(m_aConversionHelper.FormatsToGtk(aFormats));
#endif
if (!aGtkTargets.empty())
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkTargetEntry aEntry;
OString sTunnel = "application/x-libreoffice-internal-id-" + getPID();
aEntry.target = g_strdup(sTunnel.getStr());
aEntry.flags = 0;
aEntry.info = 0;
aGtkTargets.push_back(aEntry);
#endif
m_aGtkTargets = aGtkTargets;
if (!m_pSetClipboardEvent)
m_pSetClipboardEvent = Application::PostUserEvent(LINK(this, VclGtkClipboard, AsyncSetGtkClipboard));
}
}
aEv.Contents = getContents();
aGuard.clear();
if (xOldOwner.is() && xOldOwner != xClipboardOwner)
xOldOwner->lostOwnership( this, xOldContents );
for (auto const& listener : aListeners)
{
listener->changedContents( aEv );
}
}
OUString VclGtkClipboard::getName()
{
return (m_eSelection == SELECTION_CLIPBOARD) ? OUString("CLIPBOARD") : OUString("PRIMARY");
}
sal_Int8 VclGtkClipboard::getRenderingCapabilities()
{
return 0;
}
void VclGtkClipboard::addClipboardListener( const Reference< datatransfer::clipboard::XClipboardListener >& listener )
{
osl::ClearableMutexGuard aGuard( m_aMutex );
m_aListeners.push_back( listener );
}
void VclGtkClipboard::removeClipboardListener( const Reference< datatransfer::clipboard::XClipboardListener >& listener )
{
osl::ClearableMutexGuard aGuard( m_aMutex );
m_aListeners.erase(std::remove(m_aListeners.begin(), m_aListeners.end(), listener), m_aListeners.end());
}
Reference< XInterface > GtkInstance::CreateClipboard(const Sequence< Any >& arguments)
{
if ( IsRunningUnitTest() )
return SalInstance::CreateClipboard( arguments );
OUString sel;
if (!arguments.hasElements()) {
sel = "CLIPBOARD";
} else if (arguments.getLength() != 1 || !(arguments[0] >>= sel)) {
throw css::lang::IllegalArgumentException(
"bad GtkInstance::CreateClipboard arguments",
css::uno::Reference<css::uno::XInterface>(), -1);
}
SelectionType eSelection = (sel == "CLIPBOARD") ? SELECTION_CLIPBOARD : SELECTION_PRIMARY;
if (m_aClipboards[eSelection].is())
return m_aClipboards[eSelection];
Reference<XInterface> xClipboard(static_cast<cppu::OWeakObject *>(new VclGtkClipboard(eSelection)));
m_aClipboards[eSelection] = xClipboard;
return xClipboard;
}
GtkInstDropTarget::GtkInstDropTarget()
: WeakComponentImplHelper(m_aMutex)
, m_pFrame(nullptr)
, m_pFormatConversionRequest(nullptr)
, m_bActive(false)
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_bInDrag(false)
#endif
, m_nDefaultActions(0)
{
}
OUString SAL_CALL GtkInstDropTarget::getImplementationName()
{
return "com.sun.star.datatransfer.dnd.VclGtkDropTarget";
}
sal_Bool SAL_CALL GtkInstDropTarget::supportsService(OUString const & ServiceName)
{
return cppu::supportsService(this, ServiceName);
}
css::uno::Sequence<OUString> SAL_CALL GtkInstDropTarget::getSupportedServiceNames()
{
Sequence<OUString> aRet { "com.sun.star.datatransfer.dnd.GtkDropTarget" };
return aRet;
}
GtkInstDropTarget::~GtkInstDropTarget()
{
if (m_pFrame)
m_pFrame->deregisterDropTarget(this);
}
void GtkInstDropTarget::deinitialize()
{
m_pFrame = nullptr;
m_bActive = false;
}
void GtkInstDropTarget::initialize(const Sequence<Any>& rArguments)
{
if (rArguments.getLength() < 2)
{
throw RuntimeException("DropTarget::initialize: Cannot install window event handler",
static_cast<OWeakObject*>(this));
}
sal_IntPtr nFrame = 0;
rArguments.getConstArray()[1] >>= nFrame;
if (!nFrame)
{
throw RuntimeException("DropTarget::initialize: missing SalFrame",
static_cast<OWeakObject*>(this));
}
m_pFrame = reinterpret_cast<GtkSalFrame*>(nFrame);
m_pFrame->registerDropTarget(this);
m_bActive = true;
}
void GtkInstDropTarget::addDropTargetListener( const Reference< css::datatransfer::dnd::XDropTargetListener >& xListener)
{
::osl::Guard< ::osl::Mutex > aGuard( m_aMutex );
m_aListeners.push_back( xListener );
}
void GtkInstDropTarget::removeDropTargetListener( const Reference< css::datatransfer::dnd::XDropTargetListener >& xListener)
{
::osl::Guard< ::osl::Mutex > aGuard( m_aMutex );
m_aListeners.erase(std::remove(m_aListeners.begin(), m_aListeners.end(), xListener), m_aListeners.end());
}
void GtkInstDropTarget::fire_drop(const css::datatransfer::dnd::DropTargetDropEvent& dtde)
{
osl::ClearableGuard<osl::Mutex> aGuard( m_aMutex );
std::vector<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(m_aListeners);
aGuard.clear();
for (auto const& listener : aListeners)
{
listener->drop( dtde );
}
}
void GtkInstDropTarget::fire_dragEnter(const css::datatransfer::dnd::DropTargetDragEnterEvent& dtde)
{
osl::ClearableGuard< ::osl::Mutex > aGuard( m_aMutex );
std::vector<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(m_aListeners);
aGuard.clear();
for (auto const& listener : aListeners)
{
listener->dragEnter( dtde );
}
}
void GtkInstDropTarget::fire_dragOver(const css::datatransfer::dnd::DropTargetDragEvent& dtde)
{
osl::ClearableGuard< ::osl::Mutex > aGuard( m_aMutex );
std::vector<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(m_aListeners);
aGuard.clear();
for (auto const& listener : aListeners)
{
listener->dragOver( dtde );
}
}
void GtkInstDropTarget::fire_dragExit(const css::datatransfer::dnd::DropTargetEvent& dte)
{
osl::ClearableGuard< ::osl::Mutex > aGuard( m_aMutex );
std::vector<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(m_aListeners);
aGuard.clear();
for (auto const& listener : aListeners)
{
listener->dragExit( dte );
}
}
sal_Bool GtkInstDropTarget::isActive()
{
return m_bActive;
}
void GtkInstDropTarget::setActive(sal_Bool bActive)
{
m_bActive = bActive;
}
sal_Int8 GtkInstDropTarget::getDefaultActions()
{
return m_nDefaultActions;
}
void GtkInstDropTarget::setDefaultActions(sal_Int8 nDefaultActions)
{
m_nDefaultActions = nDefaultActions;
}
Reference<XInterface> GtkInstance::ImplCreateDropTarget(const SystemEnvData* pSysEnv)
{
return vcl::X11DnDHelper(new GtkInstDropTarget(), pSysEnv->aShellWindow);
}
GtkInstDragSource::~GtkInstDragSource()
{
if (m_pFrame)
m_pFrame->deregisterDragSource(this);
if (GtkInstDragSource::g_ActiveDragSource == this)
{
SAL_WARN( "vcl.gtk", "dragEnd should have been called on GtkInstDragSource before dtor");
GtkInstDragSource::g_ActiveDragSource = nullptr;
}
}
void GtkInstDragSource::deinitialize()
{
m_pFrame = nullptr;
}
sal_Bool GtkInstDragSource::isDragImageSupported()
{
return true;
}
sal_Int32 GtkInstDragSource::getDefaultCursor( sal_Int8 )
{
return 0;
}
void GtkInstDragSource::initialize(const css::uno::Sequence<css::uno::Any >& rArguments)
{
if (rArguments.getLength() < 2)
{
throw RuntimeException("DragSource::initialize: Cannot install window event handler",
static_cast<OWeakObject*>(this));
}
sal_IntPtr nFrame = 0;
rArguments.getConstArray()[1] >>= nFrame;
if (!nFrame)
{
throw RuntimeException("DragSource::initialize: missing SalFrame",
static_cast<OWeakObject*>(this));
}
m_pFrame = reinterpret_cast<GtkSalFrame*>(nFrame);
m_pFrame->registerDragSource(this);
}
OUString SAL_CALL GtkInstDragSource::getImplementationName()
{
return "com.sun.star.datatransfer.dnd.VclGtkDragSource";
}
sal_Bool SAL_CALL GtkInstDragSource::supportsService(OUString const & ServiceName)
{
return cppu::supportsService(this, ServiceName);
}
css::uno::Sequence<OUString> SAL_CALL GtkInstDragSource::getSupportedServiceNames()
{
Sequence<OUString> aRet { "com.sun.star.datatransfer.dnd.GtkDragSource" };
return aRet;
}
Reference<XInterface> GtkInstance::ImplCreateDragSource(const SystemEnvData* pSysEnv)
{
return vcl::X11DnDHelper(new GtkInstDragSource(), pSysEnv->aShellWindow);
}
namespace {
class GtkOpenGLContext : public OpenGLContext
{
GLWindow m_aGLWin;
GtkWidget *m_pGLArea;
GdkGLContext *m_pContext;
gulong m_nDestroySignalId;
gulong m_nRenderSignalId;
guint m_nAreaFrameBuffer;
guint m_nFrameBuffer;
guint m_nRenderBuffer;
guint m_nDepthBuffer;
guint m_nFrameScratchBuffer;
guint m_nRenderScratchBuffer;
guint m_nDepthScratchBuffer;
public:
GtkOpenGLContext()
: m_pGLArea(nullptr)
, m_pContext(nullptr)
, m_nDestroySignalId(0)
, m_nRenderSignalId(0)
, m_nAreaFrameBuffer(0)
, m_nFrameBuffer(0)
, m_nRenderBuffer(0)
, m_nDepthBuffer(0)
, m_nFrameScratchBuffer(0)
, m_nRenderScratchBuffer(0)
, m_nDepthScratchBuffer(0)
{
}
virtual void initWindow() override
{
if( !m_pChildWindow )
{
SystemWindowData winData = generateWinData(mpWindow, mbRequestLegacyContext);
m_pChildWindow = VclPtr<SystemChildWindow>::Create(mpWindow, 0, &winData, false);
}
if (m_pChildWindow)
{
InitChildWindow(m_pChildWindow.get());
}
}
private:
virtual const GLWindow& getOpenGLWindow() const override { return m_aGLWin; }
virtual GLWindow& getModifiableOpenGLWindow() override { return m_aGLWin; }
static void signalDestroy(GtkWidget*, gpointer context)
{
GtkOpenGLContext* pThis = static_cast<GtkOpenGLContext*>(context);
pThis->m_pGLArea = nullptr;
pThis->m_nDestroySignalId = 0;
pThis->m_nRenderSignalId = 0;
}
static gboolean signalRender(GtkGLArea*, GdkGLContext*, gpointer window)
{
GtkOpenGLContext* pThis = static_cast<GtkOpenGLContext*>(window);
int scale = gtk_widget_get_scale_factor(pThis->m_pGLArea);
int width = pThis->m_aGLWin.Width * scale;
int height = pThis->m_aGLWin.Height * scale;
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBindFramebuffer(GL_READ_FRAMEBUFFER, pThis->m_nAreaFrameBuffer);
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
gdk_gl_context_make_current(pThis->m_pContext);
return true;
}
virtual void adjustToNewSize() override
{
if (!m_pGLArea)
return;
int scale = gtk_widget_get_scale_factor(m_pGLArea);
int width = m_aGLWin.Width * scale;
int height = m_aGLWin.Height * scale;
// seen in tdf#124729 width/height of 0 leading to GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
int allocwidth = std::max(width, 1);
int allocheight = std::max(height, 1);
gtk_gl_area_make_current(GTK_GL_AREA(m_pGLArea));
if (GError *pError = gtk_gl_area_get_error(GTK_GL_AREA(m_pGLArea)))
{
SAL_WARN("vcl.gtk", "gtk gl area error: " << pError->message);
return;
}
glBindRenderbuffer(GL_RENDERBUFFER, m_nRenderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB8, allocwidth, allocheight);
glBindRenderbuffer(GL_RENDERBUFFER, m_nDepthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, allocwidth, allocheight);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_nAreaFrameBuffer);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT, m_nRenderBuffer);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, m_nDepthBuffer);
gdk_gl_context_make_current(m_pContext);
glBindRenderbuffer(GL_RENDERBUFFER, m_nRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_nDepthBuffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_nFrameBuffer);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT, m_nRenderBuffer);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, m_nDepthBuffer);
glViewport(0, 0, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, m_nRenderScratchBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB8, allocwidth, allocheight);
glBindRenderbuffer(GL_RENDERBUFFER, m_nDepthScratchBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, allocwidth, allocheight);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_nFrameScratchBuffer);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT, m_nRenderScratchBuffer);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, m_nDepthScratchBuffer);
glViewport(0, 0, width, height);
}
// Use a throw away toplevel to determine the OpenGL version because once
// an GdkGLContext is created for a window then it seems that
// glGenVertexArrays will always be called when the window gets rendered.
static int GetOpenGLVersion()
{
int nMajorGLVersion(0);
GtkWidget* pWindow;
#if !GTK_CHECK_VERSION(4,0,0)
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
#else
pWindow = gtk_window_new();
#endif
gtk_widget_realize(pWindow);
if (GdkSurface* pSurface = widget_get_surface(pWindow))
{
if (GdkGLContext* pContext = surface_create_gl_context(pSurface))
{
if (gdk_gl_context_realize(pContext, nullptr))
{
OpenGLZone aZone;
gdk_gl_context_make_current(pContext);
gdk_gl_context_get_version(pContext, &nMajorGLVersion, nullptr);
gdk_gl_context_clear_current();
}
g_object_unref(pContext);
}
}
#if !GTK_CHECK_VERSION(4,0,0)
gtk_widget_destroy(pWindow);
#else
gtk_window_destroy(GTK_WINDOW(pWindow));
#endif
return nMajorGLVersion;
}
virtual bool ImplInit() override
{
static int nOpenGLVersion = GetOpenGLVersion();
if (nOpenGLVersion < 3)
{
SAL_WARN("vcl.gtk", "gtk GL requires glGenVertexArrays which is OpenGL 3, while system provides: " << nOpenGLVersion);
return false;
}
const SystemEnvData* pEnvData = m_pChildWindow->GetSystemData();
GtkWidget *pParent = static_cast<GtkWidget*>(pEnvData->pWidget);
m_pGLArea = gtk_gl_area_new();
m_nDestroySignalId = g_signal_connect(G_OBJECT(m_pGLArea), "destroy", G_CALLBACK(signalDestroy), this);
m_nRenderSignalId = g_signal_connect(G_OBJECT(m_pGLArea), "render", G_CALLBACK(signalRender), this);
gtk_gl_area_set_has_depth_buffer(GTK_GL_AREA(m_pGLArea), true);
gtk_gl_area_set_auto_render(GTK_GL_AREA(m_pGLArea), false);
gtk_widget_set_hexpand(m_pGLArea, true);
gtk_widget_set_vexpand(m_pGLArea, true);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_add(GTK_CONTAINER(pParent), m_pGLArea);
gtk_widget_show_all(pParent);
#else
gtk_grid_attach(GTK_GRID(pParent), m_pGLArea, 0, 0, 1, 1);
gtk_widget_show(pParent);
gtk_widget_show(m_pGLArea);
#endif
gtk_gl_area_make_current(GTK_GL_AREA(m_pGLArea));
if (GError *pError = gtk_gl_area_get_error(GTK_GL_AREA(m_pGLArea)))
{
SAL_WARN("vcl.gtk", "gtk gl area error: " << pError->message);
return false;
}
gtk_gl_area_attach_buffers(GTK_GL_AREA(m_pGLArea));
glGenFramebuffersEXT(1, &m_nAreaFrameBuffer);
GdkSurface* pWindow = widget_get_surface(pParent);
m_pContext = surface_create_gl_context(pWindow);
if (!m_pContext)
return false;
if (!gdk_gl_context_realize(m_pContext, nullptr))
return false;
gdk_gl_context_make_current(m_pContext);
glGenFramebuffersEXT(1, &m_nFrameBuffer);
glGenRenderbuffersEXT(1, &m_nRenderBuffer);
glGenRenderbuffersEXT(1, &m_nDepthBuffer);
glGenFramebuffersEXT(1, &m_nFrameScratchBuffer);
glGenRenderbuffersEXT(1, &m_nRenderScratchBuffer);
glGenRenderbuffersEXT(1, &m_nDepthScratchBuffer);
bool bRet = InitGL();
InitGLDebugging();
return bRet;
}
virtual void restoreDefaultFramebuffer() override
{
OpenGLContext::restoreDefaultFramebuffer();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_nFrameScratchBuffer);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT, m_nRenderScratchBuffer);
}
virtual void makeCurrent() override
{
if (isCurrent())
return;
clearCurrent();
if (m_pGLArea)
{
int scale = gtk_widget_get_scale_factor(m_pGLArea);
int width = m_aGLWin.Width * scale;
int height = m_aGLWin.Height * scale;
gdk_gl_context_make_current(m_pContext);
glBindRenderbuffer(GL_RENDERBUFFER, m_nRenderScratchBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_nDepthScratchBuffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_nFrameScratchBuffer);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT, m_nRenderScratchBuffer);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, m_nDepthScratchBuffer);
glViewport(0, 0, width, height);
}
registerAsCurrent();
}
virtual void destroyCurrentContext() override
{
gdk_gl_context_clear_current();
}
virtual bool isCurrent() override
{
return m_pGLArea && gdk_gl_context_get_current() == m_pContext;
}
virtual void sync() override
{
}
virtual void resetCurrent() override
{
clearCurrent();
gdk_gl_context_clear_current();
}
virtual void swapBuffers() override
{
int scale = gtk_widget_get_scale_factor(m_pGLArea);
int width = m_aGLWin.Width * scale;
int height = m_aGLWin.Height * scale;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_nFrameBuffer);
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_nFrameScratchBuffer);
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_nFrameScratchBuffer);
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
gtk_gl_area_queue_render(GTK_GL_AREA(m_pGLArea));
BuffersSwapped();
}
virtual ~GtkOpenGLContext() override
{
if (m_nDestroySignalId)
g_signal_handler_disconnect(m_pGLArea, m_nDestroySignalId);
if (m_nRenderSignalId)
g_signal_handler_disconnect(m_pGLArea, m_nRenderSignalId);
if (m_pContext)
g_clear_object(&m_pContext);
}
};
}
OpenGLContext* GtkInstance::CreateOpenGLContext()
{
return new GtkOpenGLContext;
}
// tdf#123800 avoid requiring wayland at runtime just because it existed at buildtime
bool DLSYM_GDK_IS_WAYLAND_DISPLAY(GdkDisplay* pDisplay)
{
static auto get_type = reinterpret_cast<GType (*) (void)>(dlsym(nullptr, "gdk_wayland_display_get_type"));
if (!get_type)
return false;
static bool bResult = G_TYPE_CHECK_INSTANCE_TYPE(pDisplay, get_type());
return bResult;
}
bool DLSYM_GDK_IS_X11_DISPLAY(GdkDisplay* pDisplay)
{
static auto get_type = reinterpret_cast<GType (*) (void)>(dlsym(nullptr, "gdk_x11_display_get_type"));
if (!get_type)
return false;
static bool bResult = G_TYPE_CHECK_INSTANCE_TYPE(pDisplay, get_type());
return bResult;
}
namespace
{
class GtkInstanceBuilder;
void set_help_id(const GtkWidget *pWidget, std::u16string_view rHelpId)
{
gchar *helpid = g_strdup(OUStringToOString(rHelpId, RTL_TEXTENCODING_UTF8).getStr());
g_object_set_data_full(G_OBJECT(pWidget), "g-lo-helpid", helpid, g_free);
}
OUString get_help_id(const GtkWidget *pWidget)
{
void* pData = g_object_get_data(G_OBJECT(pWidget), "g-lo-helpid");
const gchar* pStr = static_cast<const gchar*>(pData);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
KeyEvent CreateKeyEvent(guint keyval, guint16 hardware_keycode, guint state, guint8 group)
{
sal_uInt16 nKeyCode = GtkSalFrame::GetKeyCode(keyval);
#if !GTK_CHECK_VERSION(4, 0, 0)
if (nKeyCode == 0)
{
guint updated_keyval = GtkSalFrame::GetKeyValFor(gdk_keymap_get_default(), hardware_keycode, group);
nKeyCode = GtkSalFrame::GetKeyCode(updated_keyval);
}
#else
(void)hardware_keycode;
(void)group;
#endif
nKeyCode |= GtkSalFrame::GetKeyModCode(state);
return KeyEvent(gdk_keyval_to_unicode(keyval), nKeyCode, 0);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
KeyEvent GtkToVcl(const GdkEventKey& rEvent)
{
return CreateKeyEvent(rEvent.keyval, rEvent.hardware_keycode, rEvent.state, rEvent.group);
}
#endif
}
static MouseEventModifiers ImplGetMouseButtonMode(sal_uInt16 nButton, sal_uInt16 nCode)
{
MouseEventModifiers nMode = MouseEventModifiers::NONE;
if ( nButton == MOUSE_LEFT )
nMode |= MouseEventModifiers::SIMPLECLICK;
if ( (nButton == MOUSE_LEFT) && !(nCode & (MOUSE_MIDDLE | MOUSE_RIGHT)) )
nMode |= MouseEventModifiers::SELECT;
if ( (nButton == MOUSE_LEFT) && (nCode & KEY_MOD1) &&
!(nCode & (MOUSE_MIDDLE | MOUSE_RIGHT | KEY_SHIFT)) )
nMode |= MouseEventModifiers::MULTISELECT;
if ( (nButton == MOUSE_LEFT) && (nCode & KEY_SHIFT) &&
!(nCode & (MOUSE_MIDDLE | MOUSE_RIGHT | KEY_MOD1)) )
nMode |= MouseEventModifiers::RANGESELECT;
return nMode;
}
static MouseEventModifiers ImplGetMouseMoveMode(sal_uInt16 nCode)
{
MouseEventModifiers nMode = MouseEventModifiers::NONE;
if ( !nCode )
nMode |= MouseEventModifiers::SIMPLEMOVE;
if ( (nCode & MOUSE_LEFT) && !(nCode & KEY_MOD1) )
nMode |= MouseEventModifiers::DRAGMOVE;
if ( (nCode & MOUSE_LEFT) && (nCode & KEY_MOD1) )
nMode |= MouseEventModifiers::DRAGCOPY;
return nMode;
}
namespace
{
bool SwapForRTL(GtkWidget* pWidget)
{
GtkTextDirection eDir = gtk_widget_get_direction(pWidget);
if (eDir == GTK_TEXT_DIR_RTL)
return true;
if (eDir == GTK_TEXT_DIR_LTR)
return false;
return AllSettings::GetLayoutRTL();
}
GtkWidget* getPopupRect(GtkWidget* pWidget, const tools::Rectangle& rInRect, GdkRectangle& rOutRect)
{
if (GtkSalFrame* pFrame = GtkSalFrame::getFromWindow(pWidget))
{
// this is the relatively unusual case where pParent is the toplevel GtkSalFrame and not a stock GtkWidget
// so use the same style of logic as GtkSalMenu::ShowNativePopupMenu to get the right position
tools::Rectangle aFloatRect = FloatingWindow::ImplConvertToAbsPos(pFrame->GetWindow(), rInRect);
aFloatRect.Move(-pFrame->maGeometry.x(), -pFrame->maGeometry.y());
rOutRect = GdkRectangle{static_cast<int>(aFloatRect.Left()), static_cast<int>(aFloatRect.Top()),
static_cast<int>(aFloatRect.GetWidth()), static_cast<int>(aFloatRect.GetHeight())};
pWidget = pFrame->getMouseEventWidget();
}
else
{
rOutRect = GdkRectangle{static_cast<int>(rInRect.Left()), static_cast<int>(rInRect.Top()),
static_cast<int>(rInRect.GetWidth()), static_cast<int>(rInRect.GetHeight())};
if (SwapForRTL(pWidget))
rOutRect.x = gtk_widget_get_allocated_width(pWidget) - rOutRect.width - 1 - rOutRect.x;
}
return pWidget;
}
void replaceWidget(GtkWidget* pWidget, GtkWidget* pReplacement)
{
// remove the widget and replace it with pReplacement
GtkWidget* pParent = gtk_widget_get_parent(pWidget);
// if pWidget was un-parented then don't bother
if (!pParent)
return;
g_object_ref(pWidget);
gint nTopAttach(0), nLeftAttach(0), nHeight(1), nWidth(1);
if (GTK_IS_GRID(pParent))
{
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_child_get(GTK_CONTAINER(pParent), pWidget,
"left-attach", &nLeftAttach,
"top-attach", &nTopAttach,
"width", &nWidth,
"height", &nHeight,
nullptr);
#else
gtk_grid_query_child(GTK_GRID(pParent), pWidget,
&nLeftAttach, &nTopAttach,
&nWidth, &nHeight);
#endif
}
#if !GTK_CHECK_VERSION(4, 0, 0)
gboolean bExpand(false), bFill(false);
GtkPackType ePackType(GTK_PACK_START);
guint nPadding(0);
gint nPosition(0);
if (GTK_IS_BOX(pParent))
{
gtk_container_child_get(GTK_CONTAINER(pParent), pWidget,
"expand", &bExpand,
"fill", &bFill,
"pack-type", &ePackType,
"padding", &nPadding,
"position", &nPosition,
nullptr);
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
// for gtk3 remove before replacement inserted, or there are warnings
// from GTK_BIN about having two children
container_remove(pParent, pWidget);
#endif
gtk_widget_set_visible(pReplacement, gtk_widget_get_visible(pWidget));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_set_no_show_all(pReplacement, gtk_widget_get_no_show_all(pWidget));
#endif
int nReqWidth, nReqHeight;
gtk_widget_get_size_request(pWidget, &nReqWidth, &nReqHeight);
gtk_widget_set_size_request(pReplacement, nReqWidth, nReqHeight);
static GQuark quark_size_groups = g_quark_from_static_string("gtk-widget-size-groups");
GSList* pSizeGroups = static_cast<GSList*>(g_object_get_qdata(G_OBJECT(pWidget), quark_size_groups));
while (pSizeGroups)
{
GtkSizeGroup *pSizeGroup = static_cast<GtkSizeGroup*>(pSizeGroups->data);
pSizeGroups = pSizeGroups->next;
gtk_size_group_remove_widget(pSizeGroup, pWidget);
gtk_size_group_add_widget(pSizeGroup, pReplacement);
}
// tdf#135368 change the mnemonic to point to our replacement
GList* pLabels = gtk_widget_list_mnemonic_labels(pWidget);
for (GList* pLabel = g_list_first(pLabels); pLabel; pLabel = g_list_next(pLabel))
{
GtkWidget* pLabelWidget = static_cast<GtkWidget*>(pLabel->data);
if (!GTK_IS_LABEL(pLabelWidget))
continue;
gtk_label_set_mnemonic_widget(GTK_LABEL(pLabelWidget), pReplacement);
}
g_list_free(pLabels);
if (GTK_IS_GRID(pParent))
{
gtk_grid_attach(GTK_GRID(pParent), pReplacement, nLeftAttach, nTopAttach, nWidth, nHeight);
}
else if (GTK_IS_BOX(pParent))
{
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_box_pack_start(GTK_BOX(pParent), pReplacement, bExpand, bFill, nPadding);
gtk_container_child_set(GTK_CONTAINER(pParent), pReplacement,
"pack-type", ePackType,
"position", nPosition,
nullptr);
#else
gtk_box_insert_child_after(GTK_BOX(pParent), pReplacement, pWidget);
#endif
}
#if !GTK_CHECK_VERSION(4, 0, 0)
else
gtk_container_add(GTK_CONTAINER(pParent), pReplacement);
#endif
if (gtk_widget_get_hexpand_set(pWidget))
gtk_widget_set_hexpand(pReplacement, gtk_widget_get_hexpand(pWidget));
if (gtk_widget_get_vexpand_set(pWidget))
gtk_widget_set_vexpand(pReplacement, gtk_widget_get_vexpand(pWidget));
gtk_widget_set_halign(pReplacement, gtk_widget_get_halign(pWidget));
gtk_widget_set_valign(pReplacement, gtk_widget_get_valign(pWidget));
#if GTK_CHECK_VERSION(4, 0, 0)
// for gtk4 remove after replacement inserted so we could use gtk_box_insert_child_after
container_remove(pParent, pWidget);
#endif
// coverity[freed_arg : FALSE] - this does not free pWidget, it is reffed by pReplacement
g_object_unref(pWidget);
}
void insertAsParent(GtkWidget* pWidget, GtkWidget* pReplacement)
{
g_object_ref(pWidget);
replaceWidget(pWidget, pReplacement);
// coverity[pass_freed_arg : FALSE] - pWidget is not freed here due to initial g_object_ref
container_add(pReplacement, pWidget);
// coverity[freed_arg : FALSE] - this does not free pWidget, it is reffed by pReplacement
g_object_unref(pWidget);
}
GtkWidget* ensureEventWidget(GtkWidget* pWidget)
{
#if GTK_CHECK_VERSION(4, 0, 0)
return pWidget;
#else
if (!pWidget)
return nullptr;
GtkWidget* pMouseEventBox;
// not every widget has a GdkWindow and can get any event, so if we
// want an event it doesn't have, insert a GtkEventBox so we can get
// those
if (gtk_widget_get_has_window(pWidget))
pMouseEventBox = pWidget;
else
{
// remove the widget and replace it with an eventbox and put the old
// widget into it
pMouseEventBox = gtk_event_box_new();
gtk_event_box_set_above_child(GTK_EVENT_BOX(pMouseEventBox), false);
gtk_event_box_set_visible_window(GTK_EVENT_BOX(pMouseEventBox), false);
insertAsParent(pWidget, pMouseEventBox);
}
return pMouseEventBox;
#endif
}
}
namespace {
#if !GTK_CHECK_VERSION(4, 0, 0)
GdkDragAction VclToGdk(sal_Int8 dragOperation)
{
GdkDragAction eRet(static_cast<GdkDragAction>(0));
if (dragOperation & css::datatransfer::dnd::DNDConstants::ACTION_COPY)
eRet = static_cast<GdkDragAction>(eRet | GDK_ACTION_COPY);
if (dragOperation & css::datatransfer::dnd::DNDConstants::ACTION_MOVE)
eRet = static_cast<GdkDragAction>(eRet | GDK_ACTION_MOVE);
if (dragOperation & css::datatransfer::dnd::DNDConstants::ACTION_LINK)
eRet = static_cast<GdkDragAction>(eRet | GDK_ACTION_LINK);
return eRet;
}
#endif
GtkWindow* get_active_window()
{
GtkWindow* pFocus = nullptr;
GList* pList = gtk_window_list_toplevels();
for (GList* pEntry = pList; pEntry; pEntry = pEntry->next)
{
#if GTK_CHECK_VERSION(4, 0, 0)
if (gtk_window_is_active(GTK_WINDOW(pEntry->data)))
#else
if (gtk_window_has_toplevel_focus(GTK_WINDOW(pEntry->data)))
#endif
{
pFocus = GTK_WINDOW(pEntry->data);
break;
}
}
g_list_free(pList);
return pFocus;
}
void LocalizeDecimalSeparator(guint& keyval)
{
const bool bDecimalKey = keyval == GDK_KEY_KP_Decimal || keyval == GDK_KEY_KP_Separator;
// #i1820# (and tdf#154623) use locale specific decimal separator
if (bDecimalKey && Application::GetSettings().GetMiscSettings().GetEnableLocalizedDecimalSep())
{
GtkWindow* pFocusWin = get_active_window();
GtkWidget* pFocus = pFocusWin ? gtk_window_get_focus(pFocusWin) : nullptr;
// tdf#138932 except if the target is a GtkEntry used for passwords
// GTK4: TODO is it a GtkEntry or a child GtkText that has the focus in this situation?
if (!pFocus || !GTK_IS_ENTRY(pFocus) || gtk_entry_get_visibility(GTK_ENTRY(pFocus)))
{
OUString aSep(Application::GetSettings().GetLocaleDataWrapper().getNumDecimalSep());
keyval = aSep[0];
}
}
}
void set_cursor(GtkWidget* pWidget, const char *pName)
{
if (!gtk_widget_get_realized(pWidget))
gtk_widget_realize(pWidget);
GdkDisplay *pDisplay = gtk_widget_get_display(pWidget);
#if GTK_CHECK_VERSION(4, 0, 0)
GdkCursor *pCursor = pName ? gdk_cursor_new_from_name(pName, nullptr) : nullptr;
#else
GdkCursor *pCursor = pName ? gdk_cursor_new_from_name(pDisplay, pName) : nullptr;
#endif
widget_set_cursor(pWidget, pCursor);
gdk_display_flush(pDisplay);
if (pCursor)
g_object_unref(pCursor);
}
vcl::Font get_font(GtkWidget* pWidget)
{
PangoContext* pContext = gtk_widget_get_pango_context(pWidget);
return pango_to_vcl(pango_context_get_font_description(pContext),
Application::GetSettings().GetUILanguageTag().getLocale());
}
}
OUString get_buildable_id(GtkBuildable* pWidget)
{
#if GTK_CHECK_VERSION(4, 0, 0)
const gchar* pStr = gtk_buildable_get_buildable_id(pWidget);
#else
const gchar* pStr = gtk_buildable_get_name(pWidget);
#endif
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
void set_buildable_id(GtkBuildable* pWidget, const OUString& rId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
GtkBuildableIface *iface = GTK_BUILDABLE_GET_IFACE(pWidget);
(*iface->set_id)(pWidget, OUStringToOString(rId, RTL_TEXTENCODING_UTF8).getStr());
#else
gtk_buildable_set_name(pWidget, OUStringToOString(rId, RTL_TEXTENCODING_UTF8).getStr());
#endif
}
namespace {
class GtkInstanceWidget : public virtual weld::Widget
{
protected:
GtkWidget* m_pWidget;
GtkWidget* m_pMouseEventBox;
GtkInstanceBuilder* m_pBuilder;
#if !GTK_CHECK_VERSION(4, 0, 0)
DECL_LINK(async_drag_cancel, void*, void);
#endif
bool IsFirstFreeze() const { return m_nFreezeCount == 0; }
bool IsLastThaw() const { return m_nFreezeCount == 1; }
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalFocusIn(GtkEventControllerFocus*, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
pThis->signal_focus_in();
}
#else
static gboolean signalFocusIn(GtkWidget*, GdkEvent*, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
pThis->signal_focus_in();
return false;
}
#endif
void signal_focus_in()
{
GtkWidget* pTopLevel = widget_get_toplevel(m_pWidget);
// see commentary in GtkSalObjectWidgetClip::Show
if (pTopLevel && g_object_get_data(G_OBJECT(pTopLevel), "g-lo-BlockFocusChange"))
return;
m_aFocusInHdl.Call(*this);
}
static gboolean signalMnemonicActivate(GtkWidget*, gboolean, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
return pThis->signal_mnemonic_activate();
}
bool signal_mnemonic_activate()
{
return m_aMnemonicActivateHdl.Call(*this);
}
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalFocusOut(GtkEventControllerFocus*, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
pThis->signal_focus_in();
}
#else
static gboolean signalFocusOut(GtkWidget*, GdkEvent*, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
pThis->signal_focus_out();
return false;
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
void launch_drag_cancel(GdkDragContext* context)
{
// post our drag cancel to happen at the next available event cycle
if (m_pDragCancelEvent)
return;
g_object_ref(context);
m_pDragCancelEvent = Application::PostUserEvent(LINK(this, GtkInstanceWidget, async_drag_cancel), context);
}
#endif
void signal_focus_out()
{
GtkWidget* pTopLevel = widget_get_toplevel(m_pWidget);
// see commentary in GtkSalObjectWidgetClip::Show
if (pTopLevel && g_object_get_data(G_OBJECT(pTopLevel), "g-lo-BlockFocusChange"))
return;
m_aFocusOutHdl.Call(*this);
}
virtual void ensureMouseEventWidget()
{
if (!m_pMouseEventBox)
m_pMouseEventBox = ::ensureEventWidget(m_pWidget);
}
void ensureButtonPressSignal()
{
if (!m_nButtonPressSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
GtkEventController* pClickController = get_click_controller();
m_nButtonPressSignalId = g_signal_connect(pClickController, "pressed", G_CALLBACK(signalButtonPress), this);
#else
ensureMouseEventWidget();
m_nButtonPressSignalId = g_signal_connect(m_pMouseEventBox, "button-press-event", G_CALLBACK(signalButtonPress), this);
#endif
}
}
void ensureButtonReleaseSignal()
{
if (!m_nButtonReleaseSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
GtkEventController* pClickController = get_click_controller();
m_nButtonReleaseSignalId = g_signal_connect(pClickController, "released", G_CALLBACK(signalButtonRelease), this);
#else
ensureMouseEventWidget();
m_nButtonReleaseSignalId = g_signal_connect(m_pMouseEventBox, "button-release-event", G_CALLBACK(signalButtonRelease), this);
#endif
}
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalPopupMenu(GtkWidget* pWidget, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
//center it when we don't know where else to use
Point aPos(gtk_widget_get_allocated_width(pWidget) / 2,
gtk_widget_get_allocated_height(pWidget) / 2);
CommandEvent aCEvt(aPos, CommandEventId::ContextMenu, false);
return pThis->signal_popup_menu(aCEvt);
}
#endif
virtual void connect_style_updated(const Link<Widget&, void>& rLink) override
{
if (m_aStyleUpdatedHdl.IsSet())
ImplGetDefaultWindow()->RemoveEventListener(LINK(this, GtkInstanceWidget, SettingsChangedHdl));
weld::Widget::connect_style_updated(rLink);
if (m_aStyleUpdatedHdl.IsSet())
ImplGetDefaultWindow()->AddEventListener(LINK(this, GtkInstanceWidget, SettingsChangedHdl));
}
bool SwapForRTL() const
{
return ::SwapForRTL(m_pWidget);
}
void do_enable_drag_source(const rtl::Reference<TransferDataContainer>& rHelper, sal_uInt8 eDNDConstants)
{
ensure_drag_source();
#if !GTK_CHECK_VERSION(4, 0, 0)
auto aFormats = rHelper->getTransferDataFlavors();
std::vector<GtkTargetEntry> aGtkTargets(m_xDragSource->FormatsToGtk(aFormats));
m_eDragAction = VclToGdk(eDNDConstants);
drag_source_set(aGtkTargets, m_eDragAction);
for (auto &a : aGtkTargets)
g_free(a.target);
m_xDragSource->set_datatransfer(rHelper, rHelper);
#else
(void)rHelper;
(void)eDNDConstants;
#endif
}
void localizeDecimalSeparator()
{
// tdf#128867 if localize decimal separator is active we will always
// need to be able to change the output of the decimal key press
if (!m_nKeyPressSignalId && Application::GetSettings().GetMiscSettings().GetEnableLocalizedDecimalSep())
{
#if GTK_CHECK_VERSION(4, 0, 0)
m_nKeyPressSignalId = g_signal_connect(get_key_controller(), "key-pressed", G_CALLBACK(signalKeyPressed), this);
#else
m_nKeyPressSignalId = g_signal_connect(m_pWidget, "key-press-event", G_CALLBACK(signalKey), this);
#endif
}
}
void ensure_drag_begin_end()
{
if (!m_nDragBeginSignalId)
{
// using "after" due to https://gitlab.gnome.org/GNOME/pygobject/issues/251
#if GTK_CHECK_VERSION(4, 0, 0)
m_nDragBeginSignalId = g_signal_connect_after(get_drag_controller(), "drag-begin", G_CALLBACK(signalDragBegin), this);
#else
m_nDragBeginSignalId = g_signal_connect_after(m_pWidget, "drag-begin", G_CALLBACK(signalDragBegin), this);
#endif
}
if (!m_nDragEndSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
m_nDragEndSignalId = g_signal_connect(get_drag_controller(), "drag-end", G_CALLBACK(signalDragEnd), this);
#else
m_nDragEndSignalId = g_signal_connect(m_pWidget, "drag-end", G_CALLBACK(signalDragEnd), this);
#endif
}
}
void DisconnectMouseEvents()
{
if (m_nButtonPressSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_click_controller(), m_nButtonPressSignalId);
#else
g_signal_handler_disconnect(m_pMouseEventBox, m_nButtonPressSignalId);
#endif
m_nButtonPressSignalId = 0;
}
if (m_nMotionSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_motion_controller(), m_nMotionSignalId);
#else
g_signal_handler_disconnect(m_pMouseEventBox, m_nMotionSignalId);
#endif
m_nMotionSignalId = 0;
}
if (m_nLeaveSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_motion_controller(), m_nLeaveSignalId);
#else
g_signal_handler_disconnect(m_pMouseEventBox, m_nLeaveSignalId);
#endif
m_nLeaveSignalId = 0;
}
if (m_nEnterSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_motion_controller(), m_nEnterSignalId);
#else
g_signal_handler_disconnect(m_pMouseEventBox, m_nEnterSignalId);
#endif
m_nEnterSignalId = 0;
}
if (m_nButtonReleaseSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_click_controller(), m_nButtonReleaseSignalId);
#else
g_signal_handler_disconnect(m_pMouseEventBox, m_nButtonReleaseSignalId);
#endif
m_nButtonReleaseSignalId = 0;
}
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!m_pMouseEventBox || m_pMouseEventBox == m_pWidget)
return;
// GtkWindow replacement for GtkPopover case
if (!GTK_IS_EVENT_BOX(m_pMouseEventBox))
{
m_pMouseEventBox = nullptr;
return;
}
// put things back they way we found them
GtkWidget* pParent = gtk_widget_get_parent(m_pMouseEventBox);
g_object_ref(m_pWidget);
gtk_container_remove(GTK_CONTAINER(m_pMouseEventBox), m_pWidget);
gtk_widget_destroy(m_pMouseEventBox);
gtk_container_add(GTK_CONTAINER(pParent), m_pWidget);
// coverity[freed_arg : FALSE] - this does not free m_pWidget, it is reffed by pParent
g_object_unref(m_pWidget);
m_pMouseEventBox = m_pWidget;
#endif
}
private:
bool m_bTakeOwnership;
#if !GTK_CHECK_VERSION(4, 0, 0)
bool m_bDraggedOver;
#endif
int m_nWaitCount;
int m_nFreezeCount;
sal_uInt16 m_nLastMouseButton;
#if !GTK_CHECK_VERSION(4, 0, 0)
sal_uInt16 m_nLastMouseClicks;
#endif
int m_nPressedButton;
#if !GTK_CHECK_VERSION(4, 0, 0)
protected:
int m_nPressStartX;
int m_nPressStartY;
private:
#endif
ImplSVEvent* m_pDragCancelEvent;
GtkCssProvider* m_pBgCssProvider;
#if !GTK_CHECK_VERSION(4, 0, 0)
GdkDragAction m_eDragAction;
#endif
gulong m_nFocusInSignalId;
gulong m_nMnemonicActivateSignalId;
gulong m_nFocusOutSignalId;
gulong m_nKeyPressSignalId;
gulong m_nKeyReleaseSignalId;
protected:
gulong m_nSizeAllocateSignalId;
private:
gulong m_nButtonPressSignalId;
gulong m_nMotionSignalId;
gulong m_nLeaveSignalId;
gulong m_nEnterSignalId;
gulong m_nButtonReleaseSignalId;
gulong m_nDragMotionSignalId;
gulong m_nDragDropSignalId;
gulong m_nDragDropReceivedSignalId;
gulong m_nDragLeaveSignalId;
gulong m_nDragBeginSignalId;
gulong m_nDragEndSignalId;
gulong m_nDragFailedSignalId;
gulong m_nDragDataDeleteignalId;
gulong m_nDragGetSignalId;
#if GTK_CHECK_VERSION(4, 0, 0)
int m_nGrabCount;
GtkEventController* m_pFocusController;
GtkEventController* m_pClickController;
GtkEventController* m_pMotionController;
GtkEventController* m_pDragController;
GtkEventController* m_pKeyController;
#endif
rtl::Reference<GtkInstDropTarget> m_xDropTarget;
rtl::Reference<GtkInstDragSource> m_xDragSource;
static void signalSizeAllocate(GtkWidget*, GdkRectangle* allocation, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
pThis->signal_size_allocate(allocation->width, allocation->height);
}
#if GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalKeyPressed(GtkEventControllerKey*, guint keyval, guint keycode, GdkModifierType state, gpointer widget)
{
LocalizeDecimalSeparator(keyval);
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
return pThis->signal_key_press(keyval, keycode, state);
}
static gboolean signalKeyReleased(GtkEventControllerKey*, guint keyval, guint keycode, GdkModifierType state, gpointer widget)
{
LocalizeDecimalSeparator(keyval);
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
return pThis->signal_key_release(keyval, keycode, state);
}
#else
static gboolean signalKey(GtkWidget*, GdkEventKey* pEvent, gpointer widget)
{
LocalizeDecimalSeparator(pEvent->keyval);
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
if (pEvent->type == GDK_KEY_PRESS)
return pThis->signal_key_press(pEvent);
return pThis->signal_key_release(pEvent);
}
#endif
virtual bool signal_popup_menu(const CommandEvent&)
{
return false;
}
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalButtonPress(GtkGestureClick* pGesture, int n_press, gdouble x, gdouble y, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
pThis->signal_button(pGesture, SalEvent::MouseButtonDown, n_press, x, y);
}
static void signalButtonRelease(GtkGestureClick* pGesture, int n_press, gdouble x, gdouble y, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
pThis->signal_button(pGesture, SalEvent::MouseButtonUp, n_press, x, y);
}
void signal_button(GtkGestureClick* pGesture, SalEvent nEventType, int n_press, gdouble x, gdouble y)
{
m_nPressedButton = -1;
Point aPos(x, y);
if (SwapForRTL())
aPos.setX(gtk_widget_get_allocated_width(m_pWidget) - 1 - aPos.X());
if (n_press == 1)
{
GdkEventSequence* pSequence = gtk_gesture_single_get_current_sequence(GTK_GESTURE_SINGLE(pGesture));
GdkEvent* pEvent = gtk_gesture_get_last_event(GTK_GESTURE(pGesture), pSequence);
if (gdk_event_triggers_context_menu(pEvent))
{
//if handled for context menu, stop processing
CommandEvent aCEvt(aPos, CommandEventId::ContextMenu, true);
if (signal_popup_menu(aCEvt))
{
gtk_gesture_set_state(GTK_GESTURE(pGesture), GTK_EVENT_SEQUENCE_CLAIMED);
return;
}
}
}
GdkModifierType eType = gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(pGesture));
int nButton = gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(pGesture));
switch (nButton)
{
case 1:
m_nLastMouseButton = MOUSE_LEFT;
break;
case 2:
m_nLastMouseButton = MOUSE_MIDDLE;
break;
case 3:
m_nLastMouseButton = MOUSE_RIGHT;
break;
default:
return;
}
sal_uInt32 nModCode = GtkSalFrame::GetMouseModCode(eType);
// strip out which buttons are involved from the nModCode and replace with m_nLastMouseButton
sal_uInt16 nCode = m_nLastMouseButton | (nModCode & (KEY_SHIFT | KEY_MOD1 | KEY_MOD2));
MouseEvent aMEvt(aPos, n_press, ImplGetMouseButtonMode(m_nLastMouseButton, nModCode), nCode, nCode);
if (nEventType == SalEvent::MouseButtonDown && m_aMousePressHdl.Call(aMEvt))
gtk_gesture_set_state(GTK_GESTURE(pGesture), GTK_EVENT_SEQUENCE_CLAIMED);
if (nEventType == SalEvent::MouseButtonUp && m_aMouseReleaseHdl.Call(aMEvt))
gtk_gesture_set_state(GTK_GESTURE(pGesture), GTK_EVENT_SEQUENCE_CLAIMED);
}
#else
static gboolean signalButtonPress(GtkWidget*, GdkEventButton* pEvent, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
return pThis->signal_button(pEvent);
}
static gboolean signalButtonRelease(GtkWidget*, GdkEventButton* pEvent, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
return pThis->signal_button(pEvent);
}
bool signal_button(GdkEventButton* pEvent)
{
m_nPressedButton = -1;
Point aPos(pEvent->x, pEvent->y);
if (SwapForRTL())
aPos.setX(gtk_widget_get_allocated_width(m_pWidget) - 1 - aPos.X());
if (gdk_event_triggers_context_menu(reinterpret_cast<GdkEvent*>(pEvent)) && pEvent->type == GDK_BUTTON_PRESS)
{
//if handled for context menu, stop processing
CommandEvent aCEvt(aPos, CommandEventId::ContextMenu, true);
if (signal_popup_menu(aCEvt))
return true;
}
/* Save press to possibly begin a drag */
if (pEvent->type != GDK_BUTTON_RELEASE)
{
m_nPressedButton = pEvent->button;
m_nPressStartX = pEvent->x;
m_nPressStartY = pEvent->y;
}
if (!m_aMousePressHdl.IsSet() && !m_aMouseReleaseHdl.IsSet())
return false;
SalEvent nEventType = SalEvent::NONE;
switch (pEvent->type)
{
case GDK_BUTTON_PRESS:
if (GdkEvent* pPeekEvent = gdk_event_peek())
{
bool bSkip = pPeekEvent->type == GDK_2BUTTON_PRESS ||
pPeekEvent->type == GDK_3BUTTON_PRESS;
gdk_event_free(pPeekEvent);
if (bSkip)
{
return false;
}
}
nEventType = SalEvent::MouseButtonDown;
m_nLastMouseClicks = 1;
break;
case GDK_2BUTTON_PRESS:
m_nLastMouseClicks = 2;
nEventType = SalEvent::MouseButtonDown;
break;
case GDK_3BUTTON_PRESS:
m_nLastMouseClicks = 3;
nEventType = SalEvent::MouseButtonDown;
break;
case GDK_BUTTON_RELEASE:
nEventType = SalEvent::MouseButtonUp;
break;
default:
return false;
}
switch (pEvent->button)
{
case 1:
m_nLastMouseButton = MOUSE_LEFT;
break;
case 2:
m_nLastMouseButton = MOUSE_MIDDLE;
break;
case 3:
m_nLastMouseButton = MOUSE_RIGHT;
break;
default:
return false;
}
sal_uInt32 nModCode = GtkSalFrame::GetMouseModCode(pEvent->state);
// strip out which buttons are involved from the nModCode and replace with m_nLastMouseButton
sal_uInt16 nCode = m_nLastMouseButton | (nModCode & (KEY_SHIFT | KEY_MOD1 | KEY_MOD2));
MouseEvent aMEvt(aPos, m_nLastMouseClicks, ImplGetMouseButtonMode(m_nLastMouseButton, nModCode), nCode, nCode);
if (nEventType == SalEvent::MouseButtonDown)
{
if (!m_aMousePressHdl.IsSet())
return false;
return m_aMousePressHdl.Call(aMEvt);
}
if (!m_aMouseReleaseHdl.IsSet())
return false;
return m_aMouseReleaseHdl.Call(aMEvt);
}
#endif
bool simple_signal_motion(double x, double y, guint nState)
{
if (!m_aMouseMotionHdl.IsSet())
return false;
Point aPos(x, y);
if (SwapForRTL())
aPos.setX(gtk_widget_get_allocated_width(m_pWidget) - 1 - aPos.X());
sal_uInt32 nModCode = GtkSalFrame::GetMouseModCode(nState);
MouseEvent aMEvt(aPos, 0, ImplGetMouseMoveMode(nModCode), nModCode, nModCode);
return m_aMouseMotionHdl.Call(aMEvt);
}
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalMotion(GtkEventControllerMotion *pController, double x, double y, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
GdkModifierType eType = gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(pController));
SolarMutexGuard aGuard;
pThis->simple_signal_motion(x, y, eType);
}
#else
static gboolean signalMotion(GtkWidget*, GdkEventMotion* pEvent, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
return pThis->signal_motion(pEvent);
}
bool signal_motion(const GdkEventMotion* pEvent)
{
const bool bDragData = m_eDragAction != 0 && m_nPressedButton != -1 && m_xDragSource.is() && gtk_drag_source_get_target_list(m_pWidget);
bool bUnsetDragIcon(false);
if (bDragData && gtk_drag_check_threshold(m_pWidget, m_nPressStartX, m_nPressStartY, pEvent->x, pEvent->y) && !do_signal_drag_begin(bUnsetDragIcon))
{
GdkDragContext* pContext = gtk_drag_begin_with_coordinates(m_pWidget,
gtk_drag_source_get_target_list(m_pWidget),
m_eDragAction,
m_nPressedButton,
const_cast<GdkEvent*>(reinterpret_cast<const GdkEvent*>(pEvent)),
m_nPressStartX, m_nPressStartY);
if (pContext && bUnsetDragIcon)
{
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
gtk_drag_set_icon_surface(pContext, surface);
cairo_surface_destroy(surface);
}
m_nPressedButton = -1;
return false;
}
return simple_signal_motion(pEvent->x, pEvent->y, pEvent->state);
}
#endif
bool signal_crossing(double x, double y, guint nState, MouseEventModifiers eMouseEventModifiers)
{
if (!m_aMouseMotionHdl.IsSet())
return false;
Point aPos(x, y);
if (SwapForRTL())
aPos.setX(gtk_widget_get_allocated_width(m_pWidget) - 1 - aPos.X());
sal_uInt32 nModCode = GtkSalFrame::GetMouseModCode(nState);
MouseEventModifiers eModifiers = ImplGetMouseMoveMode(nModCode);
eModifiers = eModifiers | eMouseEventModifiers;
MouseEvent aMEvt(aPos, 0, eModifiers, nModCode, nModCode);
m_aMouseMotionHdl.Call(aMEvt);
return false;
}
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalEnter(GtkEventControllerMotion *pController, double x, double y, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
GdkModifierType eType = gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(pController));
SolarMutexGuard aGuard;
pThis->signal_crossing(x, y, eType, MouseEventModifiers::ENTERWINDOW);
}
static void signalLeave(GtkEventControllerMotion *pController, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
GdkModifierType eType = gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(pController));
SolarMutexGuard aGuard;
pThis->signal_crossing(-1, -1, eType, MouseEventModifiers::LEAVEWINDOW);
}
#else
static gboolean signalCrossing(GtkWidget*, GdkEventCrossing* pEvent, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
MouseEventModifiers eMouseEventModifiers = pEvent->type == GDK_ENTER_NOTIFY ? MouseEventModifiers::ENTERWINDOW : MouseEventModifiers::LEAVEWINDOW;
SolarMutexGuard aGuard;
return pThis->signal_crossing(pEvent->x, pEvent->y, pEvent->state, eMouseEventModifiers);
}
#endif
virtual void drag_started()
{
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalDragMotion(GtkWidget *pWidget, GdkDragContext *context, gint x, gint y, guint time, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
if (!pThis->m_bDraggedOver)
{
pThis->m_bDraggedOver = true;
pThis->drag_started();
}
return pThis->m_xDropTarget->signalDragMotion(pWidget, context, x, y, time);
}
static gboolean signalDragDrop(GtkWidget* pWidget, GdkDragContext* context, gint x, gint y, guint time, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
return pThis->m_xDropTarget->signalDragDrop(pWidget, context, x, y, time);
}
static void signalDragDropReceived(GtkWidget* pWidget, GdkDragContext* context, gint x, gint y, GtkSelectionData* data, guint ttype, guint time, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
pThis->m_xDropTarget->signalDragDropReceived(pWidget, context, x, y, data, ttype, time);
}
#endif
virtual void drag_ended()
{
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static void signalDragLeave(GtkWidget* pWidget, GdkDragContext*, guint /*time*/, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
pThis->m_xDropTarget->signalDragLeave(pWidget);
if (pThis->m_bDraggedOver)
{
pThis->m_bDraggedOver = false;
pThis->drag_ended();
}
}
#endif
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalDragBegin(GtkDragSource* context, GdkDrag*, gpointer widget)
#else
static void signalDragBegin(GtkWidget*, GdkDragContext* context, gpointer widget)
#endif
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
pThis->signal_drag_begin(context);
}
void ensure_drag_source()
{
if (!m_xDragSource)
{
m_xDragSource.set(new GtkInstDragSource);
#if !GTK_CHECK_VERSION(4, 0, 0)
m_nDragFailedSignalId = g_signal_connect(m_pWidget, "drag-failed", G_CALLBACK(signalDragFailed), this);
m_nDragDataDeleteignalId = g_signal_connect(m_pWidget, "drag-data-delete", G_CALLBACK(signalDragDelete), this);
m_nDragGetSignalId = g_signal_connect(m_pWidget, "drag-data-get", G_CALLBACK(signalDragDataGet), this);
#endif
ensure_drag_begin_end();
}
}
virtual bool do_signal_drag_begin(bool& rUnsetDragIcon)
{
rUnsetDragIcon = false;
return false;
}
#if GTK_CHECK_VERSION(4, 0, 0)
virtual void drag_set_icon(GtkDragSource*)
#else
virtual void drag_set_icon(GdkDragContext*)
#endif
{
}
#if GTK_CHECK_VERSION(4, 0, 0)
void signal_drag_begin(GtkDragSource* context)
#else
void signal_drag_begin(GdkDragContext* context)
#endif
{
bool bUnsetDragIcon(false);
if (do_signal_drag_begin(bUnsetDragIcon))
{
#if !GTK_CHECK_VERSION(4, 0, 0)
launch_drag_cancel(context);
#endif
return;
}
if (bUnsetDragIcon)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
gtk_drag_set_icon_surface(context, surface);
cairo_surface_destroy(surface);
#endif
}
else
{
drag_set_icon(context);
}
if (!m_xDragSource)
return;
m_xDragSource->setActiveDragSource();
}
virtual void do_signal_drag_end()
{
}
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalDragEnd(GtkGestureDrag* /*gesture*/, double /*offset_x*/, double /*offset_y*/, gpointer widget)
#else
static void signalDragEnd(GtkWidget* /*widget*/, GdkDragContext* context, gpointer widget)
#endif
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
pThis->do_signal_drag_end();
#if !GTK_CHECK_VERSION(4, 0, 0)
if (pThis->m_xDragSource.is())
pThis->m_xDragSource->dragEnd(context);
#endif
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalDragFailed(GtkWidget* /*widget*/, GdkDragContext* /*context*/, GtkDragResult /*result*/, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
pThis->m_xDragSource->dragFailed();
return false;
}
static void signalDragDelete(GtkWidget* /*widget*/, GdkDragContext* /*context*/, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
pThis->m_xDragSource->dragDelete();
}
static void signalDragDataGet(GtkWidget* /*widget*/, GdkDragContext* /*context*/, GtkSelectionData *data, guint info,
guint /*time*/, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
pThis->m_xDragSource->dragDataGet(data, info);
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
virtual void drag_source_set(const std::vector<GtkTargetEntry>& rGtkTargets, GdkDragAction eDragAction)
{
if (rGtkTargets.empty() && !eDragAction)
gtk_drag_source_unset(m_pWidget);
else
gtk_drag_source_set(m_pWidget, GDK_BUTTON1_MASK, rGtkTargets.data(), rGtkTargets.size(), eDragAction);
}
#endif
void do_set_background(const Color& rColor)
{
const bool bRemoveColor = rColor == COL_AUTO;
if (bRemoveColor && !m_pBgCssProvider)
return;
GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(GTK_WIDGET(m_pWidget));
if (m_pBgCssProvider)
{
gtk_style_context_remove_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pBgCssProvider));
m_pBgCssProvider = nullptr;
}
if (bRemoveColor)
return;
OUString sColor = rColor.AsRGBHexString();
m_pBgCssProvider = gtk_css_provider_new();
OUString aBuffer = "* { background-color: #" + sColor + "; }";
OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8);
css_provider_load_from_data(m_pBgCssProvider, aResult.getStr(), aResult.getLength());
gtk_style_context_add_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pBgCssProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
DECL_LINK(SettingsChangedHdl, VclWindowEvent&, void);
#if !GTK_CHECK_VERSION(4, 0, 0)
static void update_style(GtkWidget* pWidget, gpointer pData)
{
if (GTK_IS_CONTAINER(pWidget))
gtk_container_foreach(GTK_CONTAINER(pWidget), update_style, pData);
GtkWidgetClass* pWidgetClass = GTK_WIDGET_GET_CLASS(pWidget);
pWidgetClass->style_updated(pWidget);
}
#endif
public:
GtkInstanceWidget(GtkWidget* pWidget, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: m_pWidget(pWidget)
, m_pMouseEventBox(nullptr)
, m_pBuilder(pBuilder)
, m_bTakeOwnership(bTakeOwnership)
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_bDraggedOver(false)
#endif
, m_nWaitCount(0)
, m_nFreezeCount(0)
, m_nLastMouseButton(0)
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_nLastMouseClicks(0)
#endif
, m_nPressedButton(-1)
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_nPressStartX(-1)
, m_nPressStartY(-1)
#endif
, m_pDragCancelEvent(nullptr)
, m_pBgCssProvider(nullptr)
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_eDragAction(GdkDragAction(0))
#endif
, m_nFocusInSignalId(0)
, m_nMnemonicActivateSignalId(0)
, m_nFocusOutSignalId(0)
, m_nKeyPressSignalId(0)
, m_nKeyReleaseSignalId(0)
, m_nSizeAllocateSignalId(0)
, m_nButtonPressSignalId(0)
, m_nMotionSignalId(0)
, m_nLeaveSignalId(0)
, m_nEnterSignalId(0)
, m_nButtonReleaseSignalId(0)
, m_nDragMotionSignalId(0)
, m_nDragDropSignalId(0)
, m_nDragDropReceivedSignalId(0)
, m_nDragLeaveSignalId(0)
, m_nDragBeginSignalId(0)
, m_nDragEndSignalId(0)
, m_nDragFailedSignalId(0)
, m_nDragDataDeleteignalId(0)
, m_nDragGetSignalId(0)
#if GTK_CHECK_VERSION(4, 0, 0)
, m_nGrabCount(0)
, m_pFocusController(nullptr)
, m_pClickController(nullptr)
, m_pMotionController(nullptr)
, m_pDragController(nullptr)
, m_pKeyController(nullptr)
#endif
{
if (!bTakeOwnership)
g_object_ref(m_pWidget);
localizeDecimalSeparator();
}
virtual void connect_key_press(const Link<const KeyEvent&, bool>& rLink) override
{
if (!m_nKeyPressSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
m_nKeyPressSignalId = g_signal_connect(get_key_controller(), "key-pressed", G_CALLBACK(signalKeyPressed), this);
#else
m_nKeyPressSignalId = g_signal_connect(m_pWidget, "key-press-event", G_CALLBACK(signalKey), this);
#endif
}
weld::Widget::connect_key_press(rLink);
}
virtual void connect_key_release(const Link<const KeyEvent&, bool>& rLink) override
{
if (!m_nKeyReleaseSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
m_nKeyReleaseSignalId = g_signal_connect(get_key_controller(), "key-released", G_CALLBACK(signalKeyReleased), this);
#else
m_nKeyReleaseSignalId = g_signal_connect(m_pWidget, "key-release-event", G_CALLBACK(signalKey), this);
#endif
}
weld::Widget::connect_key_release(rLink);
}
virtual void connect_mouse_press(const Link<const MouseEvent&, bool>& rLink) override
{
ensureButtonPressSignal();
weld::Widget::connect_mouse_press(rLink);
}
virtual void connect_mouse_move(const Link<const MouseEvent&, bool>& rLink) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
GtkEventController* pMotionController = get_motion_controller();
if (!m_nMotionSignalId)
m_nMotionSignalId = g_signal_connect(pMotionController, "motion", G_CALLBACK(signalMotion), this);
if (!m_nLeaveSignalId)
m_nLeaveSignalId = g_signal_connect(pMotionController, "leave", G_CALLBACK(signalEnter), this);
if (!m_nEnterSignalId)
m_nEnterSignalId = g_signal_connect(pMotionController, "enter", G_CALLBACK(signalLeave), this);
#else
ensureMouseEventWidget();
if (!m_nMotionSignalId)
m_nMotionSignalId = g_signal_connect(m_pMouseEventBox, "motion-notify-event", G_CALLBACK(signalMotion), this);
if (!m_nLeaveSignalId)
m_nLeaveSignalId = g_signal_connect(m_pMouseEventBox, "leave-notify-event", G_CALLBACK(signalCrossing), this);
if (!m_nEnterSignalId)
m_nEnterSignalId = g_signal_connect(m_pMouseEventBox, "enter-notify-event", G_CALLBACK(signalCrossing), this);
#endif
weld::Widget::connect_mouse_move(rLink);
}
virtual void connect_mouse_release(const Link<const MouseEvent&, bool>& rLink) override
{
ensureButtonReleaseSignal();
weld::Widget::connect_mouse_release(rLink);
}
virtual void set_sensitive(bool sensitive) override
{
gtk_widget_set_sensitive(m_pWidget, sensitive);
}
virtual bool get_sensitive() const override
{
return gtk_widget_get_sensitive(m_pWidget);
}
virtual bool get_visible() const override
{
return gtk_widget_get_visible(m_pWidget);
}
virtual bool is_visible() const override
{
return gtk_widget_is_visible(m_pWidget);
}
virtual void set_can_focus(bool bCanFocus) override
{
gtk_widget_set_can_focus(m_pWidget, bCanFocus);
}
virtual void grab_focus() override
{
if (has_focus())
return;
gtk_widget_grab_focus(m_pWidget);
}
virtual bool has_focus() const override
{
return gtk_widget_has_focus(m_pWidget);
}
virtual bool is_active() const override
{
GtkWindow* pTopLevel = GTK_WINDOW(widget_get_toplevel(m_pWidget));
return pTopLevel && gtk_window_is_active(pTopLevel) && has_focus();
}
// is the focus in a child of this widget, where a transient popup attached
// to a widget is considered a child of that widget
virtual bool has_child_focus() const override
{
GtkWindow* pFocusWin = get_active_window();
if (!pFocusWin)
return false;
GtkWidget* pFocus = gtk_window_get_focus(pFocusWin);
if (pFocus && gtk_widget_is_ancestor(pFocus, m_pWidget))
return true;
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* pAttachedTo = gtk_window_get_attached_to(pFocusWin);
if (!pAttachedTo)
return false;
if (pAttachedTo == m_pWidget || gtk_widget_is_ancestor(pAttachedTo, m_pWidget))
return true;
#endif
return false;
}
virtual void show() override
{
gtk_widget_show(m_pWidget);
}
virtual void hide() override
{
gtk_widget_hide(m_pWidget);
}
virtual void set_size_request(int nWidth, int nHeight) override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_VIEWPORT(pParent))
pParent = gtk_widget_get_parent(pParent);
if (GTK_IS_SCROLLED_WINDOW(pParent))
{
gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(pParent), nWidth);
gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(pParent), nHeight);
}
gtk_widget_set_size_request(m_pWidget, nWidth, nHeight);
}
virtual Size get_size_request() const override
{
int nWidth, nHeight;
gtk_widget_get_size_request(m_pWidget, &nWidth, &nHeight);
return Size(nWidth, nHeight);
}
virtual Size get_preferred_size() const override
{
GtkRequisition size;
gtk_widget_get_preferred_size(m_pWidget, nullptr, &size);
return Size(size.width, size.height);
}
virtual float get_approximate_digit_width() const override
{
PangoContext* pContext = gtk_widget_get_pango_context(m_pWidget);
PangoFontMetrics* pMetrics = pango_context_get_metrics(pContext,
pango_context_get_font_description(pContext),
pango_context_get_language(pContext));
float nDigitWidth = pango_font_metrics_get_approximate_digit_width(pMetrics);
pango_font_metrics_unref(pMetrics);
return nDigitWidth / PANGO_SCALE;
}
virtual int get_text_height() const override
{
PangoContext* pContext = gtk_widget_get_pango_context(m_pWidget);
PangoFontMetrics* pMetrics = pango_context_get_metrics(pContext,
pango_context_get_font_description(pContext),
pango_context_get_language(pContext));
int nLineHeight = pango_font_metrics_get_ascent(pMetrics) + pango_font_metrics_get_descent(pMetrics);
pango_font_metrics_unref(pMetrics);
return nLineHeight / PANGO_SCALE;
}
virtual Size get_pixel_size(const OUString& rText) const override
{
OString aStr(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
PangoLayout* pLayout = gtk_widget_create_pango_layout(m_pWidget, aStr.getStr());
gint nWidth, nHeight;
pango_layout_get_pixel_size(pLayout, &nWidth, &nHeight);
g_object_unref(pLayout);
return Size(nWidth, nHeight);
}
virtual vcl::Font get_font() override
{
return ::get_font(m_pWidget);
}
virtual void set_grid_left_attach(int nAttach) override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
#if GTK_CHECK_VERSION(4, 0, 0)
int row, width, height;
gtk_grid_query_child(GTK_GRID(pParent), m_pWidget, nullptr, &row, &width, &height);
g_object_ref(m_pWidget);
gtk_grid_remove(GTK_GRID(pParent), m_pWidget);
gtk_grid_attach(GTK_GRID(pParent), m_pWidget, nAttach, row, width, height);
g_object_unref(m_pWidget);
#else
gtk_container_child_set(GTK_CONTAINER(pParent), m_pWidget, "left-attach", nAttach, nullptr);
#endif
}
virtual int get_grid_left_attach() const override
{
gint nAttach(0);
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_grid_query_child(GTK_GRID(pParent), m_pWidget, &nAttach, nullptr, nullptr, nullptr);
#else
gtk_container_child_get(GTK_CONTAINER(pParent), m_pWidget, "left-attach", &nAttach, nullptr);
#endif
return nAttach;
}
virtual void set_grid_width(int nCols) override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
#if GTK_CHECK_VERSION(4, 0, 0)
int col, row, height;
gtk_grid_query_child(GTK_GRID(pParent), m_pWidget, &col, &row, nullptr, &height);
g_object_ref(m_pWidget);
gtk_grid_remove(GTK_GRID(pParent), m_pWidget);
gtk_grid_attach(GTK_GRID(pParent), m_pWidget, col, row, nCols, height);
g_object_unref(m_pWidget);
#else
gtk_container_child_set(GTK_CONTAINER(pParent), m_pWidget, "width", nCols, nullptr);
#endif
}
virtual void set_grid_top_attach(int nAttach) override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
#if GTK_CHECK_VERSION(4, 0, 0)
int col, width, height;
gtk_grid_query_child(GTK_GRID(pParent), m_pWidget, &col, nullptr, &width, &height);
g_object_ref(m_pWidget);
gtk_grid_remove(GTK_GRID(pParent), m_pWidget);
gtk_grid_attach(GTK_GRID(pParent), m_pWidget, col, nAttach, width, height);
g_object_unref(m_pWidget);
#else
gtk_container_child_set(GTK_CONTAINER(pParent), m_pWidget, "top-attach", nAttach, nullptr);
#endif
}
virtual int get_grid_top_attach() const override
{
gint nAttach(0);
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_grid_query_child(GTK_GRID(pParent), m_pWidget, nullptr, &nAttach, nullptr, nullptr);
#else
gtk_container_child_get(GTK_CONTAINER(pParent), m_pWidget, "top-attach", &nAttach, nullptr);
#endif
return nAttach;
}
virtual void set_hexpand(bool bExpand) override
{
gtk_widget_set_hexpand(m_pWidget, bExpand);
}
virtual bool get_hexpand() const override
{
return gtk_widget_get_hexpand(m_pWidget);
}
virtual void set_vexpand(bool bExpand) override
{
gtk_widget_set_vexpand(m_pWidget, bExpand);
}
virtual bool get_vexpand() const override
{
return gtk_widget_get_vexpand(m_pWidget);
}
virtual void set_margin_top(int nMargin) override
{
gtk_widget_set_margin_top(m_pWidget, nMargin);
}
virtual void set_margin_bottom(int nMargin) override
{
gtk_widget_set_margin_bottom(m_pWidget, nMargin);
}
virtual void set_margin_start(int nMargin) override
{
gtk_widget_set_margin_start(m_pWidget, nMargin);
}
virtual void set_margin_end(int nMargin) override
{
gtk_widget_set_margin_end(m_pWidget, nMargin);
}
virtual int get_margin_top() const override
{
return gtk_widget_get_margin_top(m_pWidget);
}
virtual int get_margin_bottom() const override
{
return gtk_widget_get_margin_bottom(m_pWidget);
}
virtual int get_margin_start() const override
{
return gtk_widget_get_margin_start(m_pWidget);
}
virtual int get_margin_end() const override
{
return gtk_widget_get_margin_end(m_pWidget);
}
virtual void set_accessible_name(const OUString& rName) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_accessible_update_property(GTK_ACCESSIBLE(m_pWidget), GTK_ACCESSIBLE_PROPERTY_LABEL,
OUStringToOString(rName, RTL_TEXTENCODING_UTF8).getStr(), -1);
#else
AtkObject* pAtkObject = gtk_widget_get_accessible(m_pWidget);
if (!pAtkObject)
return;
atk_object_set_name(pAtkObject, OUStringToOString(rName, RTL_TEXTENCODING_UTF8).getStr());
#endif
}
virtual void set_accessible_description(const OUString& rDescription) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_accessible_update_property(GTK_ACCESSIBLE(m_pWidget), GTK_ACCESSIBLE_PROPERTY_DESCRIPTION,
OUStringToOString(rDescription, RTL_TEXTENCODING_UTF8).getStr(), -1);
#else
AtkObject* pAtkObject = gtk_widget_get_accessible(m_pWidget);
if (!pAtkObject)
return;
atk_object_set_description(pAtkObject, OUStringToOString(rDescription, RTL_TEXTENCODING_UTF8).getStr());
#endif
}
virtual OUString get_accessible_name() const override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
AtkObject* pAtkObject = gtk_widget_get_accessible(m_pWidget);
const char* pStr = pAtkObject ? atk_object_get_name(pAtkObject) : nullptr;
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
#else
char* pStr = gtk_test_accessible_check_property(GTK_ACCESSIBLE(m_pWidget), GTK_ACCESSIBLE_PROPERTY_LABEL, nullptr);
OUString sRet(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
g_free(pStr);
return sRet;
#endif
}
virtual OUString get_accessible_description() const override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
AtkObject* pAtkObject = gtk_widget_get_accessible(m_pWidget);
const char* pStr = pAtkObject ? atk_object_get_description(pAtkObject) : nullptr;
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
#else
char* pStr = gtk_test_accessible_check_property(GTK_ACCESSIBLE(m_pWidget), GTK_ACCESSIBLE_PROPERTY_DESCRIPTION, nullptr);
OUString sRet(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
g_free(pStr);
return sRet;
#endif
}
virtual void set_accessible_relation_labeled_by(weld::Widget* pLabel) override
{
GtkWidget* pGtkLabel = pLabel ? dynamic_cast<GtkInstanceWidget&>(*pLabel).getWidget() : nullptr;
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_accessible_update_relation(GTK_ACCESSIBLE(m_pWidget),
GTK_ACCESSIBLE_RELATION_LABELLED_BY,
pGtkLabel, nullptr,
-1);
#else
AtkObject* pAtkObject = gtk_widget_get_accessible(m_pWidget);
if (!pAtkObject)
return;
AtkObject *pAtkLabel = pGtkLabel ? gtk_widget_get_accessible(pGtkLabel) : nullptr;
AtkRelationSet *pRelationSet = atk_object_ref_relation_set(pAtkObject);
AtkRelation *pRelation = atk_relation_set_get_relation_by_type(pRelationSet, ATK_RELATION_LABELLED_BY);
if (pRelation)
{
// clear ATK_RELATION_LABEL_FOR from old label
GPtrArray* pOldLabelTarget = atk_relation_get_target(pRelation);
guint nElements = pOldLabelTarget ? pOldLabelTarget->len : 0;
for (guint i = 0; i < nElements; ++i)
{
gpointer pOldLabelObject = g_ptr_array_index(pOldLabelTarget, i);
AtkRelationSet *pOldLabelRelationSet = atk_object_ref_relation_set(ATK_OBJECT(pOldLabelObject));
if (AtkRelation *pOldLabelRelation = atk_relation_set_get_relation_by_type(pRelationSet, ATK_RELATION_LABEL_FOR))
atk_relation_set_remove(pOldLabelRelationSet, pOldLabelRelation);
g_object_unref(pOldLabelRelationSet);
}
atk_relation_set_remove(pRelationSet, pRelation);
}
if (pAtkLabel)
{
AtkObject *obj_array_labelled_by[1];
obj_array_labelled_by[0] = pAtkLabel;
pRelation = atk_relation_new(obj_array_labelled_by, 1, ATK_RELATION_LABELLED_BY);
atk_relation_set_add(pRelationSet, pRelation);
// add ATK_RELATION_LABEL_FOR to new label to match
AtkRelationSet *pNewLabelRelationSet = atk_object_ref_relation_set(pAtkLabel);
AtkRelation *pNewLabelRelation = atk_relation_set_get_relation_by_type(pNewLabelRelationSet, ATK_RELATION_LABEL_FOR);
if (pNewLabelRelation)
atk_relation_set_remove(pNewLabelRelationSet, pRelation);
AtkObject *obj_array_label_for[1];
obj_array_label_for[0] = pAtkObject;
pNewLabelRelation = atk_relation_new(obj_array_label_for, 1, ATK_RELATION_LABEL_FOR);
atk_relation_set_add(pNewLabelRelationSet, pNewLabelRelation);
g_object_unref(pNewLabelRelationSet);
}
g_object_unref(pRelationSet);
#endif
}
virtual bool get_extents_relative_to(const weld::Widget& rRelative, int& x, int &y, int& width, int &height) const override
{
//for toplevel windows this is sadly futile under wayland, so we can't tell where a dialog is in order to allow
//the document underneath to auto-scroll to place content in a visible location
gtk_coord fX(0.0), fY(0.0);
bool ret = gtk_widget_translate_coordinates(m_pWidget,
dynamic_cast<const GtkInstanceWidget&>(rRelative).getWidget(),
0, 0, &fX, &fY);
x = fX;
y = fY;
width = gtk_widget_get_allocated_width(m_pWidget);
height = gtk_widget_get_allocated_height(m_pWidget);
return ret;
}
virtual void set_tooltip_text(const OUString& rTip) override
{
gtk_widget_set_tooltip_text(m_pWidget, OUStringToOString(rTip, RTL_TEXTENCODING_UTF8).getStr());
}
virtual OUString get_tooltip_text() const override
{
const gchar* pStr = gtk_widget_get_tooltip_text(m_pWidget);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
virtual void set_cursor_data(void * /*pData*/) override {};
virtual std::unique_ptr<weld::Container> weld_parent() const override;
virtual OUString get_buildable_name() const override
{
return ::get_buildable_id(GTK_BUILDABLE(m_pWidget));
}
virtual void set_buildable_name(const OUString& rId) override
{
::set_buildable_id(GTK_BUILDABLE(m_pWidget), rId);
}
virtual void set_help_id(const OUString& rHelpId) override
{
::set_help_id(m_pWidget, rHelpId);
}
virtual OUString get_help_id() const override
{
OUString sRet = ::get_help_id(m_pWidget);
if (sRet.isEmpty())
sRet = "null";
return sRet;
}
GtkWidget* getWidget() const
{
return m_pWidget;
}
GtkWindow* getWindow() const
{
return GTK_WINDOW(widget_get_toplevel(m_pWidget));
}
#if GTK_CHECK_VERSION(4, 0, 0)
GtkEventController* get_focus_controller()
{
if (!m_pFocusController)
{
gtk_widget_set_focusable(m_pWidget, true);
m_pFocusController = gtk_event_controller_focus_new();
gtk_widget_add_controller(m_pWidget, m_pFocusController);
}
return m_pFocusController;
}
#if GTK_CHECK_VERSION(4, 0, 0)
GtkEventController* get_click_controller()
{
if (!m_pClickController)
{
GtkGesture *pClick = gtk_gesture_click_new();
gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(pClick), 0);
m_pClickController = GTK_EVENT_CONTROLLER(pClick);
gtk_widget_add_controller(m_pWidget, m_pClickController);
}
return m_pClickController;
}
GtkEventController* get_motion_controller()
{
if (!m_pMotionController)
{
m_pMotionController = gtk_event_controller_motion_new();
gtk_widget_add_controller(m_pWidget, m_pMotionController);
}
return m_pMotionController;
}
GtkEventController* get_drag_controller()
{
if (!m_pDragController)
{
GtkDragSource* pDrag = gtk_drag_source_new();
m_pDragController = GTK_EVENT_CONTROLLER(pDrag);
gtk_widget_add_controller(m_pWidget, m_pDragController);
}
return m_pDragController;
}
GtkEventController* get_key_controller()
{
if (!m_pKeyController)
{
m_pKeyController = gtk_event_controller_key_new();
gtk_widget_add_controller(m_pWidget, m_pKeyController);
}
return m_pKeyController;
}
#endif
#endif
virtual void connect_focus_in(const Link<Widget&, void>& rLink) override
{
if (!m_nFocusInSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
m_nFocusInSignalId = g_signal_connect(get_focus_controller(), "enter", G_CALLBACK(signalFocusIn), this);
#else
m_nFocusInSignalId = g_signal_connect(m_pWidget, "focus-in-event", G_CALLBACK(signalFocusIn), this);
#endif
}
weld::Widget::connect_focus_in(rLink);
}
virtual void connect_mnemonic_activate(const Link<Widget&, bool>& rLink) override
{
if (!m_nMnemonicActivateSignalId)
m_nMnemonicActivateSignalId = g_signal_connect(m_pWidget, "mnemonic-activate", G_CALLBACK(signalMnemonicActivate), this);
weld::Widget::connect_mnemonic_activate(rLink);
}
virtual void connect_focus_out(const Link<Widget&, void>& rLink) override
{
if (!m_nFocusOutSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
m_nFocusOutSignalId = g_signal_connect(get_focus_controller(), "leave", G_CALLBACK(signalFocusOut), this);
#else
m_nFocusOutSignalId = g_signal_connect(m_pWidget, "focus-out-event", G_CALLBACK(signalFocusOut), this);
#endif
}
weld::Widget::connect_focus_out(rLink);
}
virtual void connect_size_allocate(const Link<const Size&, void>& rLink) override
{
m_nSizeAllocateSignalId = g_signal_connect(m_pWidget, "size-allocate", G_CALLBACK(signalSizeAllocate), this);
weld::Widget::connect_size_allocate(rLink);
}
virtual void signal_size_allocate(guint nWidth, guint nHeight)
{
m_aSizeAllocateHdl.Call(Size(nWidth, nHeight));
}
#if GTK_CHECK_VERSION(4, 0, 0)
bool signal_key_press(guint keyval, guint keycode, GdkModifierType state)
{
if (m_aKeyPressHdl.IsSet())
{
SolarMutexGuard aGuard;
return m_aKeyPressHdl.Call(CreateKeyEvent(keyval, keycode, state, 0));
}
return false;
}
bool signal_key_release(guint keyval, guint keycode, GdkModifierType state)
{
if (m_aKeyReleaseHdl.IsSet())
{
SolarMutexGuard aGuard;
return m_aKeyReleaseHdl.Call(CreateKeyEvent(keyval, keycode, state, 0));
}
return false;
}
#else
virtual bool do_signal_key_press(const GdkEventKey* pEvent)
{
if (m_aKeyPressHdl.IsSet())
{
SolarMutexGuard aGuard;
return m_aKeyPressHdl.Call(GtkToVcl(*pEvent));
}
return false;
}
virtual bool do_signal_key_release(const GdkEventKey* pEvent)
{
if (m_aKeyReleaseHdl.IsSet())
{
SolarMutexGuard aGuard;
return m_aKeyReleaseHdl.Call(GtkToVcl(*pEvent));
}
return false;
}
bool signal_key_press(const GdkEventKey* pEvent)
{
return do_signal_key_press(pEvent);
}
bool signal_key_release(const GdkEventKey* pEvent)
{
return do_signal_key_release(pEvent);
}
#endif
virtual void grab_add() override
{
#if GTK_CHECK_VERSION(4, 0, 0)
++m_nGrabCount;
#else
gtk_grab_add(m_pWidget);
#endif
}
virtual bool has_grab() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
return m_nGrabCount != 0;
#else
return gtk_widget_has_grab(m_pWidget);
#endif
}
virtual void grab_remove() override
{
#if GTK_CHECK_VERSION(4, 0, 0)
--m_nGrabCount;
#else
gtk_grab_remove(m_pWidget);
#endif
}
virtual bool get_direction() const override
{
return gtk_widget_get_direction(m_pWidget) == GTK_TEXT_DIR_RTL;
}
virtual void set_direction(bool bRTL) override
{
gtk_widget_set_direction(m_pWidget, bRTL ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR);
}
virtual void freeze() override
{
++m_nFreezeCount;
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_freeze_child_notify(m_pWidget);
#endif
g_object_freeze_notify(G_OBJECT(m_pWidget));
}
virtual void thaw() override
{
--m_nFreezeCount;
g_object_thaw_notify(G_OBJECT(m_pWidget));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_thaw_child_notify(m_pWidget);
#endif
}
virtual void set_busy_cursor(bool bBusy) override
{
if (bBusy)
++m_nWaitCount;
else
--m_nWaitCount;
if (m_nWaitCount == 1)
set_cursor(m_pWidget, "progress");
else if (m_nWaitCount == 0)
set_cursor(m_pWidget, nullptr);
assert (m_nWaitCount >= 0);
}
virtual void queue_resize() override
{
gtk_widget_queue_resize(m_pWidget);
}
virtual css::uno::Reference<css::datatransfer::dnd::XDropTarget> get_drop_target() override
{
if (!m_xDropTarget)
{
m_xDropTarget.set(new GtkInstDropTarget);
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!gtk_drag_dest_get_track_motion(m_pWidget))
{
gtk_drag_dest_set(m_pWidget, GtkDestDefaults(0), nullptr, 0, GdkDragAction(0));
gtk_drag_dest_set_track_motion(m_pWidget, true);
}
m_nDragMotionSignalId = g_signal_connect(m_pWidget, "drag-motion", G_CALLBACK(signalDragMotion), this);
m_nDragDropSignalId = g_signal_connect(m_pWidget, "drag-drop", G_CALLBACK(signalDragDrop), this);
m_nDragDropReceivedSignalId = g_signal_connect(m_pWidget, "drag-data-received", G_CALLBACK(signalDragDropReceived), this);
m_nDragLeaveSignalId = g_signal_connect(m_pWidget, "drag-leave", G_CALLBACK(signalDragLeave), this);
#endif
}
return m_xDropTarget;
}
virtual css::uno::Reference<css::datatransfer::clipboard::XClipboard> get_clipboard() const override
{
// the gen backend can have per-frame clipboards which is (presumably) useful for LibreOffice Online
// but normal usage is the shared system clipboard
return GetSystemClipboard();
}
virtual void connect_get_property_tree(const Link<tools::JsonWriter&, void>& /*rLink*/) override
{
//not implemented for the gtk variant
}
virtual void get_property_tree(tools::JsonWriter& /*rJsonWriter*/) override
{
//not implemented for the gtk variant
}
virtual void call_attention_to() override
{
// Change the class name to restart the animation under
// its other name: https://css-tricks.com/restart-css-animation/
#if GTK_CHECK_VERSION(4, 0, 0)
if (gtk_widget_has_css_class(m_pWidget, "call_attention_1"))
{
gtk_widget_remove_css_class(m_pWidget, "call_attention_1");
gtk_widget_add_css_class(m_pWidget, "call_attention_2");
}
else
{
gtk_widget_remove_css_class(m_pWidget, "call_attention_2");
gtk_widget_add_css_class(m_pWidget, "call_attention_1");
}
#else
GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(m_pWidget);
if (gtk_style_context_has_class(pWidgetContext, "call_attention_1"))
{
gtk_style_context_remove_class(pWidgetContext, "call_attention_1");
gtk_style_context_add_class(pWidgetContext, "call_attention_2");
}
else
{
gtk_style_context_remove_class(pWidgetContext, "call_attention_2");
gtk_style_context_add_class(pWidgetContext, "call_attention_1");
}
#endif
}
virtual void set_stack_background() override
{
do_set_background(Application::GetSettings().GetStyleSettings().GetWindowColor());
}
virtual void set_title_background() override
{
do_set_background(Application::GetSettings().GetStyleSettings().GetShadowColor());
}
virtual void set_highlight_background() override
{
do_set_background(Application::GetSettings().GetStyleSettings().GetHighlightColor());
}
virtual void set_background(const Color& rColor) override
{
do_set_background(rColor);
}
virtual void set_toolbar_background() override
{
// no-op
}
virtual ~GtkInstanceWidget() override
{
if (m_aStyleUpdatedHdl.IsSet())
ImplGetDefaultWindow()->RemoveEventListener(LINK(this, GtkInstanceWidget, SettingsChangedHdl));
if (m_pDragCancelEvent)
Application::RemoveUserEvent(m_pDragCancelEvent);
if (m_nDragMotionSignalId)
g_signal_handler_disconnect(m_pWidget, m_nDragMotionSignalId);
if (m_nDragDropSignalId)
g_signal_handler_disconnect(m_pWidget, m_nDragDropSignalId);
if (m_nDragDropReceivedSignalId)
g_signal_handler_disconnect(m_pWidget, m_nDragDropReceivedSignalId);
if (m_nDragLeaveSignalId)
g_signal_handler_disconnect(m_pWidget, m_nDragLeaveSignalId);
if (m_nDragEndSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_drag_controller(), m_nDragEndSignalId);
#else
g_signal_handler_disconnect(m_pWidget, m_nDragEndSignalId);
#endif
}
if (m_nDragBeginSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_drag_controller(), m_nDragBeginSignalId);
#else
g_signal_handler_disconnect(m_pWidget, m_nDragBeginSignalId);
#endif
}
if (m_nDragFailedSignalId)
g_signal_handler_disconnect(m_pWidget, m_nDragFailedSignalId);
if (m_nDragDataDeleteignalId)
g_signal_handler_disconnect(m_pWidget, m_nDragDataDeleteignalId);
if (m_nDragGetSignalId)
g_signal_handler_disconnect(m_pWidget, m_nDragGetSignalId);
if (m_nKeyPressSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_key_controller(), m_nKeyPressSignalId);
#else
g_signal_handler_disconnect(m_pWidget, m_nKeyPressSignalId);
#endif
}
if (m_nKeyReleaseSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_key_controller(), m_nKeyReleaseSignalId);
#else
g_signal_handler_disconnect(m_pWidget, m_nKeyReleaseSignalId);
#endif
}
if (m_nFocusInSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_focus_controller(), m_nFocusInSignalId);
#else
g_signal_handler_disconnect(m_pWidget, m_nFocusInSignalId);
#endif
}
if (m_nMnemonicActivateSignalId)
g_signal_handler_disconnect(m_pWidget, m_nMnemonicActivateSignalId);
if (m_nFocusOutSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(get_focus_controller(), m_nFocusOutSignalId);
#else
g_signal_handler_disconnect(m_pWidget, m_nFocusOutSignalId);
#endif
}
if (m_nSizeAllocateSignalId)
g_signal_handler_disconnect(m_pWidget, m_nSizeAllocateSignalId);
do_set_background(COL_AUTO);
DisconnectMouseEvents();
if (m_bTakeOwnership)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_destroy(m_pWidget);
#else
gtk_window_destroy(GTK_WINDOW(m_pWidget));
#endif
}
else
g_object_unref(m_pWidget);
}
virtual void disable_notify_events()
{
if (m_nFocusInSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_block(get_focus_controller(), m_nFocusInSignalId);
#else
g_signal_handler_block(m_pWidget, m_nFocusInSignalId);
#endif
}
if (m_nMnemonicActivateSignalId)
g_signal_handler_block(m_pWidget, m_nMnemonicActivateSignalId);
if (m_nFocusOutSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_block(get_focus_controller(), m_nFocusOutSignalId);
#else
g_signal_handler_block(m_pWidget, m_nFocusOutSignalId);
#endif
}
if (m_nSizeAllocateSignalId)
g_signal_handler_block(m_pWidget, m_nSizeAllocateSignalId);
}
virtual void enable_notify_events()
{
if (m_nSizeAllocateSignalId)
g_signal_handler_unblock(m_pWidget, m_nSizeAllocateSignalId);
if (m_nFocusOutSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_unblock(get_focus_controller(), m_nFocusOutSignalId);
#else
g_signal_handler_unblock(m_pWidget, m_nFocusOutSignalId);
#endif
}
if (m_nMnemonicActivateSignalId)
g_signal_handler_unblock(m_pWidget, m_nMnemonicActivateSignalId);
if (m_nFocusInSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_unblock(get_focus_controller(), m_nFocusInSignalId);
#else
g_signal_handler_unblock(m_pWidget, m_nFocusInSignalId);
#endif
}
}
virtual void help_hierarchy_foreach(const std::function<bool(const OUString&)>& func) override;
virtual OUString strip_mnemonic(const OUString &rLabel) const override
{
return rLabel.replaceFirst("_", "");
}
virtual VclPtr<VirtualDevice> create_virtual_device() const override
{
// create with no separate alpha layer like everything sane does
auto xRet = VclPtr<VirtualDevice>::Create();
xRet->SetBackground(COL_TRANSPARENT);
return xRet;
}
virtual void draw(OutputDevice& rOutput, const Point& rPos, const Size& rPixelSize) override
{
// detect if we have to manually setup its size
bool bAlreadyRealized = gtk_widget_get_realized(m_pWidget);
// has to be visible for draw to work
bool bAlreadyVisible = gtk_widget_get_visible(m_pWidget);
// has to be mapped for draw to work
bool bAlreadyMapped = gtk_widget_get_mapped(m_pWidget);
if (!bAlreadyRealized)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
/*
tdf#141633 The "sample db" example (Mockup.odb) has multiline
entries used in its "Journal Entry" column. Those are painted by
taking snapshots of a never-really-shown textview widget.
Without this style_updated then the textview is always drawn
using its original default font size and changing the page zoom
has no effect on the size of text in the "Journal Entry" column.
*/
update_style(m_pWidget, nullptr);
#endif
gtk_widget_realize(m_pWidget);
}
if (!bAlreadyVisible)
gtk_widget_show(m_pWidget);
if (!bAlreadyMapped)
gtk_widget_map(m_pWidget);
assert(gtk_widget_is_drawable(m_pWidget)); // all that should result in this holding
// turn off animations, otherwise we get a frame of an animation sequence
gboolean bAnimations;
GtkSettings* pSettings = gtk_widget_get_settings(m_pWidget);
g_object_get(pSettings, "gtk-enable-animations", &bAnimations, nullptr);
if (bAnimations)
g_object_set(pSettings, "gtk-enable-animations", false, nullptr);
Size aSize(rPixelSize);
GtkAllocation aOrigAllocation;
gtk_widget_get_allocation(m_pWidget, &aOrigAllocation);
GtkAllocation aNewAllocation {aOrigAllocation.x,
aOrigAllocation.y,
static_cast<int>(aSize.Width()),
static_cast<int>(aSize.Height()) };
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_size_allocate(m_pWidget, &aNewAllocation);
#else
gtk_widget_size_allocate(m_pWidget, &aNewAllocation, 0);
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
if (GTK_IS_CONTAINER(m_pWidget))
gtk_container_resize_children(GTK_CONTAINER(m_pWidget));
#endif
VclPtr<VirtualDevice> xOutput(VclPtr<VirtualDevice>::Create(DeviceFormat::WITHOUT_ALPHA));
xOutput->SetOutputSizePixel(aSize);
switch (rOutput.GetOutDevType())
{
case OUTDEV_WINDOW:
case OUTDEV_VIRDEV:
xOutput->DrawOutDev(Point(), aSize, rPos, aSize, rOutput);
break;
case OUTDEV_PRINTER:
case OUTDEV_PDF:
xOutput->SetBackground(rOutput.GetBackground());
xOutput->Erase();
break;
}
cairo_surface_t* pSurface = get_underlying_cairo_surface(*xOutput);
cairo_t* cr = cairo_create(pSurface);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_draw(m_pWidget, cr);
#else
GtkSnapshot* pSnapshot = gtk_snapshot_new();
GtkWidgetClass* pWidgetClass = GTK_WIDGET_GET_CLASS(m_pWidget);
pWidgetClass->snapshot(m_pWidget, pSnapshot);
GskRenderNode* pNode = gtk_snapshot_free_to_node(pSnapshot);
gsk_render_node_draw(pNode, cr);
gsk_render_node_unref(pNode);
#endif
cairo_destroy(cr);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_set_allocation(m_pWidget, &aOrigAllocation);
gtk_widget_size_allocate(m_pWidget, &aOrigAllocation);
#else
gtk_widget_size_allocate(m_pWidget, &aOrigAllocation, 0);
#endif
switch (rOutput.GetOutDevType())
{
case OUTDEV_WINDOW:
case OUTDEV_VIRDEV:
rOutput.DrawOutDev(rPos, aSize, Point(), aSize, *xOutput);
break;
case OUTDEV_PRINTER:
case OUTDEV_PDF:
rOutput.DrawBitmapEx(rPos, xOutput->GetBitmapEx(Point(), aSize));
break;
}
if (bAnimations)
g_object_set(pSettings, "gtk-enable-animations", true, nullptr);
if (!bAlreadyMapped)
gtk_widget_unmap(m_pWidget);
if (!bAlreadyVisible)
gtk_widget_hide(m_pWidget);
if (!bAlreadyRealized)
gtk_widget_unrealize(m_pWidget);
}
};
}
IMPL_LINK(GtkInstanceWidget, SettingsChangedHdl, VclWindowEvent&, rEvent, void)
{
if (rEvent.GetId() != VclEventId::WindowDataChanged)
return;
DataChangedEvent* pData = static_cast<DataChangedEvent*>(rEvent.GetData());
if (pData->GetType() == DataChangedEventType::SETTINGS)
m_aStyleUpdatedHdl.Call(*this);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
IMPL_LINK(GtkInstanceWidget, async_drag_cancel, void*, arg, void)
{
m_pDragCancelEvent = nullptr;
GdkDragContext* context = static_cast<GdkDragContext*>(arg);
// tdf#132477 simply calling gtk_drag_cancel on the treeview dnd under X
// doesn't seem to work as hoped for (though under wayland all is well).
// Under X the next (allowed) drag effort doesn't work to drop anything,
// but a then repeated attempt does.
// emitting cancel to get gtk to cancel the drag for us does work as hoped for.
g_signal_emit_by_name(context, "cancel", 0, GDK_DRAG_CANCEL_USER_CANCELLED);
g_object_unref(context);
}
#endif
namespace
{
OString MapToGtkAccelerator(const OUString &rStr)
{
return OUStringToOString(rStr.replaceFirst("~", "_"), RTL_TEXTENCODING_UTF8);
}
OUString get_label(GtkLabel* pLabel)
{
const gchar* pStr = gtk_label_get_label(pLabel);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
void set_label(GtkLabel* pLabel, const OUString& rText)
{
gtk_label_set_label(pLabel, MapToGtkAccelerator(rText).getStr());
}
#if GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* find_label_widget(GtkWidget* pContainer)
{
GtkWidget* pLabel = nullptr;
for (GtkWidget* pChild = gtk_widget_get_first_child(pContainer);
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
if (GTK_IS_LABEL(pChild))
{
pLabel = pChild;
break;
}
else
{
pLabel = find_label_widget(pChild);
if (pLabel)
break;
}
}
return pLabel;
}
GtkWidget* find_image_widget(GtkWidget* pContainer)
{
GtkWidget* pImage = nullptr;
for (GtkWidget* pChild = gtk_widget_get_first_child(pContainer);
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
if (GTK_IS_IMAGE(pChild))
{
pImage = pChild;
break;
}
else
{
pImage = find_image_widget(pChild);
if (pImage)
break;
}
}
return pImage;
}
#else
GtkWidget* find_label_widget(GtkContainer* pContainer)
{
GList* pChildren = gtk_container_get_children(pContainer);
GtkWidget* pChild = nullptr;
for (GList* pCandidate = pChildren; pCandidate; pCandidate = pCandidate->next)
{
if (GTK_IS_LABEL(pCandidate->data))
{
pChild = GTK_WIDGET(pCandidate->data);
break;
}
else if (GTK_IS_CONTAINER(pCandidate->data))
{
pChild = find_label_widget(GTK_CONTAINER(pCandidate->data));
if (pChild)
break;
}
}
g_list_free(pChildren);
return pChild;
}
GtkWidget* find_image_widget(GtkContainer* pContainer)
{
GList* pChildren = gtk_container_get_children(pContainer);
GtkWidget* pChild = nullptr;
for (GList* pCandidate = pChildren; pCandidate; pCandidate = pCandidate->next)
{
if (GTK_IS_IMAGE(pCandidate->data))
{
pChild = GTK_WIDGET(pCandidate->data);
break;
}
else if (GTK_IS_CONTAINER(pCandidate->data))
{
pChild = find_image_widget(GTK_CONTAINER(pCandidate->data));
if (pChild)
break;
}
}
g_list_free(pChildren);
return pChild;
}
#endif
GtkLabel* get_label_widget(GtkWidget* pButton)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* pChild = gtk_bin_get_child(GTK_BIN(pButton));
if (GTK_IS_CONTAINER(pChild))
pChild = find_label_widget(GTK_CONTAINER(pChild));
else if (!GTK_IS_LABEL(pChild))
pChild = nullptr;
return GTK_LABEL(pChild);
#else
return GTK_LABEL(find_label_widget(pButton));
#endif
}
GtkImage* get_image_widget(GtkWidget *pButton)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* pChild = gtk_bin_get_child(GTK_BIN(pButton));
if (GTK_IS_CONTAINER(pChild))
pChild = find_image_widget(GTK_CONTAINER(pChild));
else if (!GTK_IS_IMAGE(pChild))
pChild = nullptr;
return GTK_IMAGE(pChild);
#else
return GTK_IMAGE(find_image_widget(pButton));
#endif
}
OUString button_get_label(GtkButton* pButton)
{
if (GtkLabel* pLabel = get_label_widget(GTK_WIDGET(pButton)))
return ::get_label(pLabel);
const gchar* pStr = gtk_button_get_label(pButton);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
void button_set_label(GtkButton* pButton, const OUString& rText)
{
if (GtkLabel* pLabel = get_label_widget(GTK_WIDGET(pButton)))
{
::set_label(pLabel, rText);
gtk_widget_set_visible(GTK_WIDGET(pLabel), true);
return;
}
gtk_button_set_label(pButton, MapToGtkAccelerator(rText).getStr());
}
#if GTK_CHECK_VERSION(4, 0, 0)
OUString get_label(GtkCheckButton* pButton)
{
const gchar* pStr = gtk_check_button_get_label(pButton);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
void set_label(GtkCheckButton* pButton, const OUString& rText)
{
gtk_check_button_set_label(pButton, MapToGtkAccelerator(rText).getStr());
}
#endif
OUString get_title(GtkWindow* pWindow)
{
const gchar* pStr = gtk_window_get_title(pWindow);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
void set_title(GtkWindow* pWindow, std::u16string_view rTitle)
{
gtk_window_set_title(pWindow, OUStringToOString(rTitle, RTL_TEXTENCODING_UTF8).getStr());
}
OUString get_primary_text(GtkMessageDialog* pMessageDialog)
{
gchar* pText = nullptr;
g_object_get(G_OBJECT(pMessageDialog), "text", &pText, nullptr);
return OUString(pText, pText ? strlen(pText) : 0, RTL_TEXTENCODING_UTF8);
}
void set_primary_text(GtkMessageDialog* pMessageDialog, std::u16string_view rText)
{
g_object_set(G_OBJECT(pMessageDialog), "text",
OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr(),
nullptr);
}
void set_secondary_text(GtkMessageDialog* pMessageDialog, std::u16string_view rText)
{
g_object_set(G_OBJECT(pMessageDialog), "secondary-text",
OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr(),
nullptr);
}
OUString get_secondary_text(GtkMessageDialog* pMessageDialog)
{
gchar* pText = nullptr;
g_object_get(G_OBJECT(pMessageDialog), "secondary-text", &pText, nullptr);
return OUString(pText, pText ? strlen(pText) : 0, RTL_TEXTENCODING_UTF8);
}
}
namespace
{
GdkPixbuf* load_icon_from_stream(SvMemoryStream& rStream)
{
auto nLength = rStream.TellEnd();
if (!nLength)
return nullptr;
const guchar* pData = static_cast<const guchar*>(rStream.GetData());
assert((*pData == 137 || *pData == '<') && "if we want to support more than png or svg this function must change");
// if we know the image type, it's a little faster to hand the type over and skip the type detection.
GdkPixbufLoader *pixbuf_loader = gdk_pixbuf_loader_new_with_type(*pData == 137 ? "png" : "svg", nullptr);
gdk_pixbuf_loader_write(pixbuf_loader, pData, nLength, nullptr);
gdk_pixbuf_loader_close(pixbuf_loader, nullptr);
GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(pixbuf_loader);
if (pixbuf)
g_object_ref(pixbuf);
g_object_unref(pixbuf_loader);
return pixbuf;
}
std::shared_ptr<SvMemoryStream> get_icon_stream_by_name_theme_lang(const OUString& rIconName, const OUString& rIconTheme, const OUString& rUILang)
{
return ImageTree::get().getImageStream(rIconName, rIconTheme, rUILang);
}
GdkPixbuf* load_icon_by_name_theme_lang(const OUString& rIconName, const OUString& rIconTheme, const OUString& rUILang)
{
auto xMemStm = get_icon_stream_by_name_theme_lang(rIconName, rIconTheme, rUILang);
if (!xMemStm)
return nullptr;
return load_icon_from_stream(*xMemStm);
}
std::unique_ptr<utl::TempFileNamed> get_icon_stream_as_file_by_name_theme_lang(const OUString& rIconName, const OUString& rIconTheme, const OUString& rUILang)
{
uno::Reference<io::XInputStream> xInputStream = ImageTree::get().getImageXInputStream(rIconName, rIconTheme, rUILang);
if (!xInputStream)
return nullptr;
std::unique_ptr<utl::TempFileNamed> xRet(new utl::TempFileNamed);
xRet->EnableKillingFile(true);
SvStream* pStream = xRet->GetStream(StreamMode::WRITE);
for (;;)
{
const sal_Int32 nSize(2048);
uno::Sequence<sal_Int8> aData(nSize);
sal_Int32 nRead = xInputStream->readBytes(aData, nSize);
pStream->WriteBytes(aData.getConstArray(), nRead);
if (nRead < nSize)
break;
}
xRet->CloseStream();
return xRet;
}
std::unique_ptr<utl::TempFileNamed> get_icon_stream_as_file(const OUString& rIconName)
{
OUString sIconTheme = Application::GetSettings().GetStyleSettings().DetermineIconTheme();
OUString sUILang = Application::GetSettings().GetUILanguageTag().getBcp47();
return get_icon_stream_as_file_by_name_theme_lang(rIconName, sIconTheme, sUILang);
}
}
GdkPixbuf* load_icon_by_name(const OUString& rIconName)
{
OUString sIconTheme = Application::GetSettings().GetStyleSettings().DetermineIconTheme();
OUString sUILang = Application::GetSettings().GetUILanguageTag().getBcp47();
return load_icon_by_name_theme_lang(rIconName, sIconTheme, sUILang);
}
namespace
{
Image mirrorImage(const Image& rImage)
{
BitmapEx aMirrBitmapEx(rImage.GetBitmapEx());
aMirrBitmapEx.Mirror(BmpMirrorFlags::Horizontal);
return Image(aMirrBitmapEx);
}
GdkPixbuf* getPixbuf(const css::uno::Reference<css::graphic::XGraphic>& rImage, bool bMirror = false)
{
Image aImage(rImage);
if (bMirror)
aImage = mirrorImage(aImage);
OUString sStock(aImage.GetStock());
if (!sStock.isEmpty())
return load_icon_by_name(sStock);
SvMemoryStream aMemStm;
// We "know" that this gets passed to zlib's deflateInit2_(). 1 means best speed.
css::uno::Sequence<css::beans::PropertyValue> aFilterData{ comphelper::makePropertyValue(
"Compression", sal_Int32(1)) };
auto aBitmapEx = aImage.GetBitmapEx();
vcl::PngImageWriter aWriter(aMemStm);
aWriter.setParameters(aFilterData);
aWriter.write(aBitmapEx);
return load_icon_from_stream(aMemStm);
}
// tdf#151898 as far as I can see only gtk_image_new_from_file (or gtk_image_new_from_resource) can support the use of a
// scalable input format to create a hidpi GtkImage, rather than an upscaled lodpi one so forced to go via a file here
std::unique_ptr<utl::TempFileNamed> getImageFile(const css::uno::Reference<css::graphic::XGraphic>& rImage, bool bMirror = false)
{
Image aImage(rImage);
if (bMirror)
aImage = mirrorImage(aImage);
OUString sStock(aImage.GetStock());
if (!sStock.isEmpty())
return get_icon_stream_as_file(sStock);
std::unique_ptr<utl::TempFileNamed> xRet(new utl::TempFileNamed);
xRet->EnableKillingFile(true);
SvStream* pStream = xRet->GetStream(StreamMode::WRITE);
// We "know" that this gets passed to zlib's deflateInit2_(). 1 means best speed.
css::uno::Sequence<css::beans::PropertyValue> aFilterData{ comphelper::makePropertyValue(
"Compression", sal_Int32(1)) };
auto aBitmapEx = aImage.GetBitmapEx();
vcl::PngImageWriter aWriter(*pStream);
aWriter.setParameters(aFilterData);
aWriter.write(aBitmapEx);
xRet->CloseStream();
return xRet;
}
GdkPixbuf* getPixbuf(const VirtualDevice& rDevice)
{
Size aSize(rDevice.GetOutputSizePixel());
cairo_surface_t* orig_surface = get_underlying_cairo_surface(rDevice);
double fXScale, fYScale;
dl_cairo_surface_get_device_scale(orig_surface, &fXScale, &fYScale);
cairo_surface_t* surface;
if (fXScale != 1.0 || fYScale != -1)
{
surface = cairo_surface_create_similar_image(orig_surface,
CAIRO_FORMAT_ARGB32,
aSize.Width(),
aSize.Height());
cairo_t* cr = cairo_create(surface);
cairo_set_source_surface(cr, orig_surface, 0, 0);
cairo_paint(cr);
cairo_destroy(cr);
}
else
surface = orig_surface;
GdkPixbuf* pRet = gdk_pixbuf_get_from_surface(surface, 0, 0, aSize.Width(), aSize.Height());
if (surface != orig_surface)
cairo_surface_destroy(surface);
return pRet;
}
#if GTK_CHECK_VERSION(4, 0, 0)
cairo_surface_t* render_paintable_to_surface(GdkPaintable *paintable, int nWidth, int nHeight)
{
cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, nWidth, nHeight);
GtkSnapshot* snapshot = gtk_snapshot_new();
gdk_paintable_snapshot(paintable, snapshot, nWidth, nHeight);
GskRenderNode* node = gtk_snapshot_free_to_node(snapshot);
cairo_t* cr = cairo_create(surface);
gsk_render_node_draw(node, cr);
cairo_destroy(cr);
gsk_render_node_unref(node);
return surface;
}
#endif
GdkPixbuf* getPixbuf(const OUString& rIconName)
{
if (rIconName.isEmpty())
return nullptr;
GdkPixbuf* pixbuf = nullptr;
if (rIconName.lastIndexOf('.') != rIconName.getLength() - 4)
{
assert((rIconName== "dialog-warning" || rIconName== "dialog-error" || rIconName== "dialog-information") &&
"unknown stock image");
#if GTK_CHECK_VERSION(4, 0, 0)
GtkIconTheme *icon_theme = gtk_icon_theme_get_for_display(gdk_display_get_default());
GtkIconPaintable *icon = gtk_icon_theme_lookup_icon(icon_theme,
OUStringToOString(rIconName, RTL_TEXTENCODING_UTF8).getStr(),
nullptr,
16,
1,
AllSettings::GetLayoutRTL() ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR,
static_cast<GtkIconLookupFlags>(0));
GdkPaintable* paintable = GDK_PAINTABLE(icon);
int nWidth = gdk_paintable_get_intrinsic_width(paintable);
int nHeight = gdk_paintable_get_intrinsic_height(paintable);
cairo_surface_t* surface = render_paintable_to_surface(paintable, nWidth, nHeight);
pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, nWidth, nHeight);
cairo_surface_destroy(surface);
#else
GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
GError *error = nullptr;
pixbuf = gtk_icon_theme_load_icon(icon_theme, OUStringToOString(rIconName, RTL_TEXTENCODING_UTF8).getStr(),
16, GTK_ICON_LOOKUP_USE_BUILTIN, &error);
#endif
}
else
{
const AllSettings& rSettings = Application::GetSettings();
pixbuf = load_icon_by_name_theme_lang(rIconName,
rSettings.GetStyleSettings().DetermineIconTheme(),
rSettings.GetUILanguageTag().getBcp47());
}
return pixbuf;
}
}
namespace
{
#if GTK_CHECK_VERSION(4, 0, 0)
SurfacePaintable* paintable_new_from_virtual_device(const VirtualDevice& rImageSurface)
{
cairo_surface_t* surface = get_underlying_cairo_surface(rImageSurface);
Size aSize(rImageSurface.GetOutputSizePixel());
cairo_surface_t* target = cairo_surface_create_similar(surface,
cairo_surface_get_content(surface),
aSize.Width(),
aSize.Height());
cairo_t* cr = cairo_create(target);
cairo_set_source_surface(cr, surface, 0, 0);
cairo_paint(cr);
cairo_destroy(cr);
SurfacePaintable* pPaintable = SURFACE_PAINTABLE(g_object_new(surface_paintable_get_type(), nullptr));
surface_paintable_set_source(pPaintable, target, aSize.Width(), aSize.Height());
return pPaintable;
}
GtkWidget* image_new_from_virtual_device(const VirtualDevice& rImageSurface)
{
SurfacePaintable* paintable = paintable_new_from_virtual_device(rImageSurface);
return gtk_image_new_from_paintable(GDK_PAINTABLE(paintable));
}
GtkWidget* picture_new_from_virtual_device(const VirtualDevice& rImageSurface)
{
SurfacePaintable* paintable = paintable_new_from_virtual_device(rImageSurface);
return gtk_picture_new_for_paintable(GDK_PAINTABLE(paintable));
}
#else
GtkWidget* image_new_from_virtual_device(const VirtualDevice& rImageSurface)
{
GtkWidget* pImage = nullptr;
cairo_surface_t* surface = get_underlying_cairo_surface(rImageSurface);
Size aSize(rImageSurface.GetOutputSizePixel());
cairo_surface_t* target = cairo_surface_create_similar(surface,
cairo_surface_get_content(surface),
aSize.Width(),
aSize.Height());
cairo_t* cr = cairo_create(target);
cairo_set_source_surface(cr, surface, 0, 0);
cairo_paint(cr);
cairo_destroy(cr);
pImage = gtk_image_new_from_surface(target);
cairo_surface_destroy(target);
return pImage;
}
#endif
GtkWidget* image_new_from_xgraphic(const css::uno::Reference<css::graphic::XGraphic>& rIcon, bool bMirror)
{
GtkWidget* pImage = nullptr;
if (auto xTempFile = getImageFile(rIcon, bMirror))
pImage = gtk_image_new_from_file(OUStringToOString(xTempFile->GetFileName(), osl_getThreadTextEncoding()).getStr());
return pImage;
}
GtkWidget* image_new_from_icon_name(const OUString& rIconName)
{
GtkWidget* pImage = nullptr;
if (auto xTempFile = get_icon_stream_as_file(rIconName))
pImage = gtk_image_new_from_file(OUStringToOString(xTempFile->GetFileName(), osl_getThreadTextEncoding()).getStr());
return pImage;
}
GtkWidget* image_new_from_icon_name_theme_lang(const OUString& rIconName, const OUString& rIconTheme, const OUString& rUILang)
{
GtkWidget* pImage = nullptr;
if (auto xTempFile = get_icon_stream_as_file_by_name_theme_lang(rIconName, rIconTheme, rUILang))
pImage = gtk_image_new_from_file(OUStringToOString(xTempFile->GetFileName(), osl_getThreadTextEncoding()).getStr());
return pImage;
}
void image_set_from_icon_name(GtkImage* pImage, const OUString& rIconName)
{
if (auto xTempFile = get_icon_stream_as_file(rIconName))
gtk_image_set_from_file(pImage, OUStringToOString(xTempFile->GetFileName(), osl_getThreadTextEncoding()).getStr());
else
gtk_image_set_from_pixbuf(pImage, nullptr);
}
void image_set_from_icon_name_theme_lang(GtkImage* pImage, const OUString& rIconName, const OUString& rIconTheme, const OUString& rUILang)
{
if (auto xTempFile = get_icon_stream_as_file_by_name_theme_lang(rIconName, rIconTheme, rUILang))
gtk_image_set_from_file(pImage, OUStringToOString(xTempFile->GetFileName(), osl_getThreadTextEncoding()).getStr());
else
gtk_image_set_from_pixbuf(pImage, nullptr);
}
void image_set_from_virtual_device(GtkImage* pImage, const VirtualDevice* pDevice)
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_image_set_from_paintable(pImage, pDevice ? GDK_PAINTABLE(paintable_new_from_virtual_device(*pDevice)) : nullptr);
#else
gtk_image_set_from_surface(pImage, pDevice ? get_underlying_cairo_surface(*pDevice) : nullptr);
#endif
}
void image_set_from_xgraphic(GtkImage* pImage, const css::uno::Reference<css::graphic::XGraphic>& rImage)
{
if (auto xTempFile = getImageFile(rImage, false))
gtk_image_set_from_file(pImage, OUStringToOString(xTempFile->GetFileName(), osl_getThreadTextEncoding()).getStr());
else
gtk_image_set_from_pixbuf(pImage, nullptr);
}
#if GTK_CHECK_VERSION(4, 0, 0)
void picture_set_from_icon_name(GtkPicture* pPicture, const OUString& rIconName)
{
if (auto xTempFile = get_icon_stream_as_file(rIconName))
gtk_picture_set_filename(pPicture, OUStringToOString(xTempFile->GetFileName(), osl_getThreadTextEncoding()).getStr());
else
gtk_picture_set_pixbuf(pPicture, nullptr);
}
void picture_set_from_icon_name_theme_lang(GtkPicture* pPicture, const OUString& rIconName, const OUString& rIconTheme, const OUString& rUILang)
{
if (auto xTempFile = get_icon_stream_as_file_by_name_theme_lang(rIconName, rIconTheme, rUILang))
gtk_picture_set_filename(pPicture, OUStringToOString(xTempFile->GetFileName(), osl_getThreadTextEncoding()).getStr());
else
gtk_picture_set_pixbuf(pPicture, nullptr);
}
void picture_set_from_virtual_device(GtkPicture* pPicture, const VirtualDevice* pDevice)
{
if (!pDevice)
gtk_picture_set_paintable(pPicture, nullptr);
else
gtk_picture_set_paintable(pPicture, GDK_PAINTABLE(paintable_new_from_virtual_device(*pDevice)));
}
void picture_set_from_xgraphic(GtkPicture* pPicture, const css::uno::Reference<css::graphic::XGraphic>& rPicture)
{
if (auto xTempFile = getImageFile(rPicture, false))
gtk_picture_set_filename(pPicture, OUStringToOString(xTempFile->GetFileName(), osl_getThreadTextEncoding()).getStr());
else
gtk_picture_set_pixbuf(pPicture, nullptr);
}
#endif
void button_set_from_icon_name(GtkButton* pButton, const OUString& rIconName)
{
if (GtkImage* pImage = get_image_widget(GTK_WIDGET(pButton)))
{
::image_set_from_icon_name(pImage, rIconName);
gtk_widget_set_visible(GTK_WIDGET(pImage), true);
return;
}
GtkWidget* pImage = image_new_from_icon_name(rIconName);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_button_set_child(pButton, pImage);
#else
gtk_button_set_image(pButton, pImage);
#endif
}
void button_set_image(GtkButton* pButton, const VirtualDevice* pDevice)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_button_set_always_show_image(pButton, true);
gtk_button_set_image_position(pButton, GTK_POS_LEFT);
#endif
GtkWidget* pImage = pDevice ? image_new_from_virtual_device(*pDevice) : nullptr;
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_button_set_child(pButton, pImage);
#else
gtk_button_set_image(pButton, pImage);
#endif
}
void button_set_image(GtkButton* pButton, const css::uno::Reference<css::graphic::XGraphic>& rImage)
{
if (GtkImage* pImage = get_image_widget(GTK_WIDGET(pButton)))
{
::image_set_from_xgraphic(pImage, rImage);
gtk_widget_set_visible(GTK_WIDGET(pImage), true);
return;
}
GtkWidget* pImage = image_new_from_xgraphic(rImage, false);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_button_set_child(pButton, pImage);
#else
gtk_button_set_image(pButton, pImage);
#endif
}
class MenuHelper
{
protected:
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkMenu* m_pMenu;
std::map<OUString, GtkMenuItem*> m_aMap;
#else
GtkPopoverMenu* m_pMenu;
o3tl::sorted_vector<OString> m_aInsertedActions; // must outlive m_aActionEntries
std::map<OUString, OString> m_aIdToAction;
std::set<OUString> m_aHiddenIds;
std::vector<GActionEntry> m_aActionEntries;
GActionGroup* m_pActionGroup;
// move 'invisible' entries to m_pHiddenActionGroup
GActionGroup* m_pHiddenActionGroup;
#endif
bool m_bTakeOwnership;
private:
virtual void signal_item_activate(const OUString& rIdent) = 0;
#if !GTK_CHECK_VERSION(4, 0, 0)
static void collect(GtkWidget* pItem, gpointer widget)
{
GtkMenuItem* pMenuItem = GTK_MENU_ITEM(pItem);
if (GtkWidget* pSubMenu = gtk_menu_item_get_submenu(pMenuItem))
gtk_container_foreach(GTK_CONTAINER(pSubMenu), collect, widget);
MenuHelper* pThis = static_cast<MenuHelper*>(widget);
pThis->add_to_map(pMenuItem);
}
static void signalActivate(GtkMenuItem* pItem, gpointer widget)
{
MenuHelper* pThis = static_cast<MenuHelper*>(widget);
SolarMutexGuard aGuard;
pThis->signal_item_activate(::get_buildable_id(GTK_BUILDABLE(pItem)));
}
#else
static std::pair<GMenuModel*, int> find_id(GMenuModel* pMenuModel, const OUString& rId)
{
for (int i = 0, nCount = g_menu_model_get_n_items(pMenuModel); i < nCount; ++i)
{
OUString sTarget;
char *id;
if (g_menu_model_get_item_attribute(pMenuModel, i, "target", "s", &id))
{
sTarget = OStringToOUString(id, RTL_TEXTENCODING_UTF8);
g_free(id);
}
if (sTarget == rId)
return std::make_pair(pMenuModel, i);
if (GMenuModel* pSectionModel = g_menu_model_get_item_link(pMenuModel, i, G_MENU_LINK_SECTION))
{
std::pair<GMenuModel*, int> aRet = find_id(pSectionModel, rId);
if (aRet.first)
return aRet;
}
if (GMenuModel* pSubMenuModel = g_menu_model_get_item_link(pMenuModel, i, G_MENU_LINK_SUBMENU))
{
std::pair<GMenuModel*, int> aRet = find_id(pSubMenuModel, rId);
if (aRet.first)
return aRet;
}
}
return std::make_pair(nullptr, -1);
}
void clear_actions()
{
for (const auto& rAction : m_aActionEntries)
{
g_action_map_remove_action(G_ACTION_MAP(m_pActionGroup), rAction.name);
g_action_map_remove_action(G_ACTION_MAP(m_pHiddenActionGroup), rAction.name);
}
m_aActionEntries.clear();
m_aInsertedActions.clear();
m_aIdToAction.clear();
}
static void action_activated(GSimpleAction*, GVariant* pParameter, gpointer widget)
{
gsize nLength(0);
const gchar* pStr = g_variant_get_string(pParameter, &nLength);
OUString aStr(pStr, nLength, RTL_TEXTENCODING_UTF8);
MenuHelper* pThis = static_cast<MenuHelper*>(widget);
SolarMutexGuard aGuard;
pThis->signal_item_activate(aStr);
}
#endif
public:
#if !GTK_CHECK_VERSION(4, 0, 0)
MenuHelper(GtkMenu* pMenu, bool bTakeOwnership)
#else
MenuHelper(GtkPopoverMenu* pMenu, bool bTakeOwnership)
#endif
: m_pMenu(pMenu)
, m_bTakeOwnership(bTakeOwnership)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!m_pMenu)
return;
gtk_container_foreach(GTK_CONTAINER(m_pMenu), collect, this);
#else
m_pActionGroup = G_ACTION_GROUP(g_simple_action_group_new());
m_pHiddenActionGroup = G_ACTION_GROUP(g_simple_action_group_new());
#endif
}
#if !GTK_CHECK_VERSION(4, 0, 0)
void add_to_map(GtkMenuItem* pMenuItem)
{
OUString id = ::get_buildable_id(GTK_BUILDABLE(pMenuItem));
m_aMap[id] = pMenuItem;
g_signal_connect(pMenuItem, "activate", G_CALLBACK(signalActivate), this);
}
void remove_from_map(GtkMenuItem* pMenuItem)
{
OUString id = ::get_buildable_id(GTK_BUILDABLE(pMenuItem));
auto iter = m_aMap.find(id);
g_signal_handlers_disconnect_by_data(pMenuItem, this);
m_aMap.erase(iter);
}
void disable_item_notify_events()
{
for (auto& a : m_aMap)
g_signal_handlers_block_by_func(a.second, reinterpret_cast<void*>(signalActivate), this);
}
void enable_item_notify_events()
{
for (auto& a : m_aMap)
g_signal_handlers_unblock_by_func(a.second, reinterpret_cast<void*>(signalActivate), this);
}
#endif
#if GTK_CHECK_VERSION(4, 0, 0)
/* LibreOffice likes to think of separators between menu entries, while gtk likes
to think of sections of menus with separators drawn between sections. We always
arrange to have a section in a menu so toplevel menumodels comprise of
sections and we move entries between sections on pretending to insert separators */
static std::pair<GMenuModel*, int> get_section_and_pos_for(GMenuModel* pMenuModel, int pos)
{
int nSectionCount = g_menu_model_get_n_items(pMenuModel);
assert(nSectionCount);
GMenuModel* pSectionModel = nullptr;
int nIndexWithinSection = 0;
int nExternalPos = 0;
for (int nSection = 0; nSection < nSectionCount; ++nSection)
{
pSectionModel = g_menu_model_get_item_link(pMenuModel, nSection, G_MENU_LINK_SECTION);
assert(pSectionModel);
int nCount = g_menu_model_get_n_items(pSectionModel);
for (nIndexWithinSection = 0; nIndexWithinSection < nCount; ++nIndexWithinSection)
{
if (pos == nExternalPos)
break;
++nExternalPos;
}
++nExternalPos;
}
return std::make_pair(pSectionModel, nIndexWithinSection);
}
static int count_immediate_children(GMenuModel* pMenuModel)
{
int nSectionCount = g_menu_model_get_n_items(pMenuModel);
assert(nSectionCount);
int nExternalPos = 0;
for (int nSection = 0; nSection < nSectionCount; ++nSection)
{
GMenuModel* pSectionModel = g_menu_model_get_item_link(pMenuModel, nSection, G_MENU_LINK_SECTION);
assert(pSectionModel);
int nCount = g_menu_model_get_n_items(pSectionModel);
for (int nIndexWithinSection = 0; nIndexWithinSection < nCount; ++nIndexWithinSection)
{
++nExternalPos;
}
++nExternalPos;
}
return nExternalPos - 1;
}
#endif
#if GTK_CHECK_VERSION(4, 0, 0)
void process_menu_model(GMenuModel* pMenuModel)
{
for (int i = 0, nCount = g_menu_model_get_n_items(pMenuModel); i < nCount; ++i)
{
OString sAction;
OUString sTarget;
char *id;
if (g_menu_model_get_item_attribute(pMenuModel, i, "action", "s", &id))
{
assert(o3tl::starts_with(id, "menu."));
sAction = OString(id + 5);
auto res = m_aInsertedActions.insert(sAction);
if (res.second)
{
// the const char* arg isn't copied by anything so it must continue to exist for the life time of
// the action group
if (sAction.startsWith("radio."))
m_aActionEntries.push_back({res.first->getStr(), action_activated, "s", "'none'", nullptr, {}});
else
m_aActionEntries.push_back({res.first->getStr(), action_activated, "s", nullptr, nullptr, {}});
}
g_free(id);
}
if (g_menu_model_get_item_attribute(pMenuModel, i, "target", "s", &id))
{
sTarget = OStringToOUString(id, RTL_TEXTENCODING_UTF8);
g_free(id);
}
m_aIdToAction[sTarget] = sAction;
if (GMenuModel* pSectionModel = g_menu_model_get_item_link(pMenuModel, i, G_MENU_LINK_SECTION))
process_menu_model(pSectionModel);
if (GMenuModel* pSubMenuModel = g_menu_model_get_item_link(pMenuModel, i, G_MENU_LINK_SUBMENU))
process_menu_model(pSubMenuModel);
}
}
// build an action group for the menu, "action" is the normal menu entry case
// the others are radiogroups
void update_action_group_from_popover_model()
{
clear_actions();
if (GMenuModel* pMenuModel = m_pMenu ? gtk_popover_menu_get_menu_model(m_pMenu) : nullptr)
{
process_menu_model(pMenuModel);
}
// move hidden entries to m_pHiddenActionGroup
g_action_map_add_action_entries(G_ACTION_MAP(m_pActionGroup), m_aActionEntries.data(), m_aActionEntries.size(), this);
for (const auto& id : m_aHiddenIds)
{
GAction* pAction = g_action_map_lookup_action(G_ACTION_MAP(m_pActionGroup), m_aIdToAction[id].getStr());
g_action_map_add_action(G_ACTION_MAP(m_pHiddenActionGroup), pAction);
g_action_map_remove_action(G_ACTION_MAP(m_pActionGroup), m_aIdToAction[id].getStr());
}
}
#endif
void insert_item(int pos, const OUString& rId, const OUString& rStr,
const OUString* pIconName, const VirtualDevice* pImageSurface,
TriState eCheckRadioFalse)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* pImage = nullptr;
if (pIconName && !pIconName->isEmpty())
pImage = image_new_from_icon_name(*pIconName);
else if (pImageSurface)
pImage = image_new_from_virtual_device(*pImageSurface);
GtkWidget *pItem;
if (pImage)
{
GtkBox *pBox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6));
GtkWidget *pLabel = gtk_label_new_with_mnemonic(MapToGtkAccelerator(rStr).getStr());
pItem = eCheckRadioFalse != TRISTATE_INDET ? gtk_check_menu_item_new() : gtk_menu_item_new();
gtk_box_pack_start(pBox, pImage, true, true, 0);
gtk_box_pack_start(pBox, pLabel, true, true, 0);
gtk_container_add(GTK_CONTAINER(pItem), GTK_WIDGET(pBox));
gtk_widget_show_all(pItem);
}
else
{
pItem = eCheckRadioFalse != TRISTATE_INDET ? gtk_check_menu_item_new_with_mnemonic(MapToGtkAccelerator(rStr).getStr())
: gtk_menu_item_new_with_mnemonic(MapToGtkAccelerator(rStr).getStr());
}
if (eCheckRadioFalse == TRISTATE_FALSE)
gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(pItem), true);
::set_buildable_id(GTK_BUILDABLE(pItem), rId);
gtk_menu_shell_append(GTK_MENU_SHELL(m_pMenu), pItem);
gtk_widget_show(pItem);
add_to_map(GTK_MENU_ITEM(pItem));
if (pos != -1)
gtk_menu_reorder_child(m_pMenu, pItem, pos);
#else
(void)pIconName; (void)pImageSurface;
if (GMenuModel* pMenuModel = m_pMenu ? gtk_popover_menu_get_menu_model(m_pMenu) : nullptr)
{
auto aSectionAndPos = get_section_and_pos_for(pMenuModel, pos);
GMenu* pMenu = G_MENU(aSectionAndPos.first);
// action with a target value ... the action name and target value are separated by a double
// colon ... For example: "app.action::target"
OUString sActionAndTarget;
if (eCheckRadioFalse == TRISTATE_INDET)
sActionAndTarget = "menu.normal." + rId + "::" + rId;
else
sActionAndTarget = "menu.radio." + rId + "::" + rId;
g_menu_insert(pMenu, aSectionAndPos.second, MapToGtkAccelerator(rStr).getStr(), sActionAndTarget.toUtf8().getStr());
assert(eCheckRadioFalse == TRISTATE_INDET); // come back to this later
// TODO not redo entire group
update_action_group_from_popover_model();
}
#endif
}
void insert_separator(int pos, const OUString& rId)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* pItem = gtk_separator_menu_item_new();
::set_buildable_id(GTK_BUILDABLE(pItem), rId);
gtk_menu_shell_append(GTK_MENU_SHELL(m_pMenu), pItem);
gtk_widget_show(pItem);
add_to_map(GTK_MENU_ITEM(pItem));
if (pos != -1)
gtk_menu_reorder_child(m_pMenu, pItem, pos);
#else
if (GMenuModel* pMenuModel = m_pMenu ? gtk_popover_menu_get_menu_model(m_pMenu) : nullptr)
{
auto aSectionAndPos = get_section_and_pos_for(pMenuModel, pos);
for (int nSection = 0, nSectionCount = g_menu_model_get_n_items(pMenuModel); nSection < nSectionCount; ++nSection)
{
GMenuModel* pSectionModel = g_menu_model_get_item_link(pMenuModel, nSection, G_MENU_LINK_SECTION);
assert(pSectionModel);
if (aSectionAndPos.first == pSectionModel)
{
GMenu* pNewSection = g_menu_new();
GMenuItem* pSectionItem = g_menu_item_new_section(nullptr, G_MENU_MODEL(pNewSection));
OUString sActionAndTarget = "menu.separator." + rId + "::" + rId;
g_menu_item_set_detailed_action(pSectionItem, sActionAndTarget.toUtf8().getStr());
g_menu_insert_item(G_MENU(pMenuModel), nSection + 1, pSectionItem);
int nOldSectionCount = g_menu_model_get_n_items(pSectionModel);
for (int i = nOldSectionCount - 1; i >= aSectionAndPos.second; --i)
{
GMenuItem* pMenuItem = g_menu_item_new_from_model(pSectionModel, i);
g_menu_prepend_item(pNewSection, pMenuItem);
g_menu_remove(G_MENU(pSectionModel), i);
g_object_unref(pMenuItem);
}
g_object_unref(pSectionItem);
g_object_unref(pNewSection);
}
}
}
#endif
}
void remove_item(const OUString& rIdent)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkMenuItem* pMenuItem = m_aMap[rIdent];
remove_from_map(pMenuItem);
gtk_widget_destroy(GTK_WIDGET(pMenuItem));
#else
if (GMenuModel* pMenuModel = m_pMenu ? gtk_popover_menu_get_menu_model(m_pMenu) : nullptr)
{
std::pair<GMenuModel*, int> aRes = find_id(pMenuModel, rIdent);
if (!aRes.first)
return;
g_menu_remove(G_MENU(aRes.first), aRes.second);
}
#endif
}
void set_item_sensitive(const OUString& rIdent, bool bSensitive)
{
#if GTK_CHECK_VERSION(4, 0, 0)
GActionGroup* pActionGroup = m_aHiddenIds.find(rIdent) == m_aHiddenIds.end() ? m_pActionGroup : m_pHiddenActionGroup;
GAction* pAction = g_action_map_lookup_action(G_ACTION_MAP(pActionGroup), m_aIdToAction[rIdent].getStr());
g_simple_action_set_enabled(G_SIMPLE_ACTION(pAction), bSensitive);
#else
gtk_widget_set_sensitive(GTK_WIDGET(m_aMap[rIdent]), bSensitive);
#endif
}
bool get_item_sensitive(const OUString& rIdent) const
{
#if GTK_CHECK_VERSION(4, 0, 0)
GActionGroup* pActionGroup = m_aHiddenIds.find(rIdent) == m_aHiddenIds.end() ? m_pActionGroup : m_pHiddenActionGroup;
GAction* pAction = g_action_map_lookup_action(G_ACTION_MAP(pActionGroup), m_aIdToAction.find(rIdent)->second.getStr());
return g_action_get_enabled(pAction);
#else
return gtk_widget_get_sensitive(GTK_WIDGET(m_aMap.find(rIdent)->second));
#endif
}
void set_item_active(const OUString& rIdent, bool bActive)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
disable_item_notify_events();
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(m_aMap[rIdent]), bActive);
enable_item_notify_events();
#else
GActionGroup* pActionGroup = m_aHiddenIds.find(rIdent) == m_aHiddenIds.end() ? m_pActionGroup : m_pHiddenActionGroup;
g_action_group_change_action_state(pActionGroup, m_aIdToAction[rIdent].getStr(),
g_variant_new_string(bActive ? OUStringToOString(rIdent, RTL_TEXTENCODING_UTF8).getStr() : "'none'"));
#endif
}
bool get_item_active(const OUString& rIdent) const
{
#if !GTK_CHECK_VERSION(4, 0, 0)
return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(m_aMap.find(rIdent)->second));
#else
GActionGroup* pActionGroup = m_aHiddenIds.find(rIdent) == m_aHiddenIds.end() ? m_pActionGroup : m_pHiddenActionGroup;
GVariant* pState = g_action_group_get_action_state(pActionGroup, m_aIdToAction.find(rIdent)->second.getStr());
if (!pState)
return false;
const char *pStateString = g_variant_get_string(pState, nullptr);
bool bInactive = g_strcmp0(pStateString, "'none'") == 0;
g_variant_unref(pState);
return bInactive;
#endif
}
void set_item_label(const OUString& rIdent, const OUString& rText)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_menu_item_set_label(m_aMap[rIdent], MapToGtkAccelerator(rText).getStr());
#else
if (GMenuModel* pMenuModel = m_pMenu ? gtk_popover_menu_get_menu_model(m_pMenu) : nullptr)
{
std::pair<GMenuModel*, int> aRes = find_id(pMenuModel, rIdent);
if (!aRes.first)
return;
// clone the original item, remove the original, insert the replacement at
// the original location
GMenuItem* pMenuItem = g_menu_item_new_from_model(aRes.first, aRes.second);
g_menu_remove(G_MENU(aRes.first), aRes.second);
g_menu_item_set_label(pMenuItem, MapToGtkAccelerator(rText).getStr());
g_menu_insert_item(G_MENU(aRes.first), aRes.second, pMenuItem);
g_object_unref(pMenuItem);
}
#endif
}
OUString get_item_label(const OUString& rIdent) const
{
#if !GTK_CHECK_VERSION(4, 0, 0)
const gchar* pText = gtk_menu_item_get_label(m_aMap.find(rIdent)->second);
return OUString(pText, pText ? strlen(pText) : 0, RTL_TEXTENCODING_UTF8);
#else
if (GMenuModel* pMenuModel = m_pMenu ? gtk_popover_menu_get_menu_model(m_pMenu) : nullptr)
{
std::pair<GMenuModel*, int> aRes = find_id(pMenuModel, rIdent);
if (!aRes.first)
return OUString();
// clone the original item to query its label
GMenuItem* pMenuItem = g_menu_item_new_from_model(aRes.first, aRes.second);
char *pLabel = nullptr;
g_menu_item_get_attribute(pMenuItem, G_MENU_ATTRIBUTE_LABEL, "&s", &pLabel);
OUString aRet(pLabel, pLabel ? strlen(pLabel) : 0, RTL_TEXTENCODING_UTF8);
g_free(pLabel);
g_object_unref(pMenuItem);
return aRet;
}
return OUString();
#endif
}
void set_item_visible(const OUString& rIdent, bool bShow)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* pWidget = GTK_WIDGET(m_aMap[rIdent]);
if (bShow)
gtk_widget_show(pWidget);
else
gtk_widget_hide(pWidget);
#else
bool bOldVisible = m_aHiddenIds.find(rIdent) == m_aHiddenIds.end();
if (bShow == bOldVisible)
return;
if (!bShow)
{
GAction* pAction = g_action_map_lookup_action(G_ACTION_MAP(m_pActionGroup), m_aIdToAction[rIdent].getStr());
g_action_map_add_action(G_ACTION_MAP(m_pHiddenActionGroup), pAction);
g_action_map_remove_action(G_ACTION_MAP(m_pActionGroup), m_aIdToAction[rIdent].getStr());
m_aHiddenIds.insert(rIdent);
}
else
{
GAction* pAction = g_action_map_lookup_action(G_ACTION_MAP(m_pHiddenActionGroup), m_aIdToAction[rIdent].getStr());
g_action_map_add_action(G_ACTION_MAP(m_pActionGroup), pAction);
g_action_map_remove_action(G_ACTION_MAP(m_pHiddenActionGroup), m_aIdToAction[rIdent].getStr());
m_aHiddenIds.erase(rIdent);
}
#endif
}
OUString get_item_id(int pos) const
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GList* pChildren = gtk_container_get_children(GTK_CONTAINER(m_pMenu));
gpointer pMenuItem = g_list_nth_data(pChildren, pos);
OUString id = ::get_buildable_id(GTK_BUILDABLE(pMenuItem));
g_list_free(pChildren);
return id;
#else
OUString sTarget;
if (GMenuModel* pMenuModel = m_pMenu ? gtk_popover_menu_get_menu_model(m_pMenu) : nullptr)
{
auto aSectionAndPos = get_section_and_pos_for(pMenuModel, pos);
char *id;
if (g_menu_model_get_item_attribute(aSectionAndPos.first, aSectionAndPos.second, "target", "s", &id))
{
sTarget = OStringToOUString(id, RTL_TEXTENCODING_UTF8);
g_free(id);
}
}
return sTarget;
#endif
}
int get_n_children() const
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GList* pChildren = gtk_container_get_children(GTK_CONTAINER(m_pMenu));
int nLen = g_list_length(pChildren);
g_list_free(pChildren);
return nLen;
#else
if (GMenuModel* pMenuModel = m_pMenu ? gtk_popover_menu_get_menu_model(m_pMenu) : nullptr)
return count_immediate_children(pMenuModel);
return 0;
#endif
}
void clear_items()
{
#if !GTK_CHECK_VERSION(4, 0, 0)
for (const auto& a : m_aMap)
{
GtkMenuItem* pMenuItem = a.second;
g_signal_handlers_disconnect_by_data(pMenuItem, this);
gtk_widget_destroy(GTK_WIDGET(pMenuItem));
}
m_aMap.clear();
#else
if (GMenuModel* pMenuModel = m_pMenu ? gtk_popover_menu_get_menu_model(m_pMenu) : nullptr)
{
GMenu* pMenu = G_MENU(pMenuModel);
g_menu_remove_all(pMenu);
g_menu_insert_section(pMenu, 0, nullptr, G_MENU_MODEL(g_menu_new()));
m_aHiddenIds.clear();
update_action_group_from_popover_model();
}
#endif
}
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkMenu* getMenu() const
#else
GtkPopoverMenu* getMenu() const
#endif
{
return m_pMenu;
}
virtual ~MenuHelper()
{
#if !GTK_CHECK_VERSION(4, 0, 0)
for (auto& a : m_aMap)
g_signal_handlers_disconnect_by_data(a.second, this);
if (m_bTakeOwnership)
gtk_widget_destroy(GTK_WIDGET(m_pMenu));
#else
g_object_unref(m_pActionGroup);
g_object_unref(m_pHiddenActionGroup);
#endif
}
};
class GtkInstanceSizeGroup : public weld::SizeGroup
{
private:
GtkSizeGroup* m_pGroup;
public:
GtkInstanceSizeGroup()
: m_pGroup(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL))
{
}
virtual void add_widget(weld::Widget* pWidget) override
{
GtkInstanceWidget* pVclWidget = dynamic_cast<GtkInstanceWidget*>(pWidget);
assert(pVclWidget);
gtk_size_group_add_widget(m_pGroup, pVclWidget->getWidget());
}
virtual void set_mode(VclSizeGroupMode eVclMode) override
{
GtkSizeGroupMode eGtkMode(GTK_SIZE_GROUP_NONE);
switch (eVclMode)
{
case VclSizeGroupMode::NONE:
eGtkMode = GTK_SIZE_GROUP_NONE;
break;
case VclSizeGroupMode::Horizontal:
eGtkMode = GTK_SIZE_GROUP_HORIZONTAL;
break;
case VclSizeGroupMode::Vertical:
eGtkMode = GTK_SIZE_GROUP_VERTICAL;
break;
case VclSizeGroupMode::Both:
eGtkMode = GTK_SIZE_GROUP_BOTH;
break;
}
gtk_size_group_set_mode(m_pGroup, eGtkMode);
}
virtual ~GtkInstanceSizeGroup() override
{
g_object_unref(m_pGroup);
}
};
class ChildFrame : public WorkWindow
{
private:
Idle maLayoutIdle;
DECL_LINK(ImplHandleLayoutTimerHdl, Timer*, void);
public:
ChildFrame(vcl::Window* pParent, WinBits nStyle)
: WorkWindow(pParent, nStyle)
, maLayoutIdle( "ChildFrame maLayoutIdle" )
{
maLayoutIdle.SetPriority(TaskPriority::RESIZE);
maLayoutIdle.SetInvokeHandler( LINK( this, ChildFrame, ImplHandleLayoutTimerHdl ) );
}
virtual void dispose() override
{
maLayoutIdle.Stop();
WorkWindow::dispose();
}
virtual void queue_resize(StateChangedType eReason = StateChangedType::Layout) override
{
WorkWindow::queue_resize(eReason);
if (maLayoutIdle.IsActive())
return;
maLayoutIdle.Start();
}
void Layout()
{
if (vcl::Window *pChild = GetWindow(GetWindowType::FirstChild))
pChild->SetPosSizePixel(Point(0, 0), GetSizePixel());
}
virtual void Resize() override
{
maLayoutIdle.Stop();
Layout();
WorkWindow::Resize();
}
};
IMPL_LINK_NOARG(ChildFrame, ImplHandleLayoutTimerHdl, Timer*, void)
{
Layout();
}
class GtkInstanceContainer : public GtkInstanceWidget, public virtual weld::Container
{
private:
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkContainer* m_pContainer;
#else
GtkWidget* m_pContainer;
#endif
gulong m_nSetFocusChildSignalId;
bool m_bChildHasFocus;
void signal_set_focus_child(bool bChildHasFocus)
{
if (m_bChildHasFocus != bChildHasFocus)
{
m_bChildHasFocus = bChildHasFocus;
signal_container_focus_changed();
}
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static void signalSetFocusChild(GtkContainer*, GtkWidget* pChild, gpointer widget)
{
GtkInstanceContainer* pThis = static_cast<GtkInstanceContainer*>(widget);
pThis->signal_set_focus_child(pChild != nullptr);
}
#endif
public:
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkInstanceContainer(GtkContainer* pContainer, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pContainer), pBuilder, bTakeOwnership)
#else
GtkInstanceContainer(GtkWidget* pContainer, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(pContainer, pBuilder, bTakeOwnership)
#endif
, m_pContainer(pContainer)
, m_nSetFocusChildSignalId(0)
, m_bChildHasFocus(false)
{
}
virtual void connect_container_focus_changed(const Link<Container&, void>& rLink) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!m_nSetFocusChildSignalId)
m_nSetFocusChildSignalId = g_signal_connect(G_OBJECT(m_pContainer), "set-focus-child", G_CALLBACK(signalSetFocusChild), this);
#endif
weld::Container::connect_container_focus_changed(rLink);
}
#if GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* getContainer() { return m_pContainer; }
#else
GtkContainer* getContainer() { return m_pContainer; }
#endif
virtual void child_grab_focus() override
{
gtk_widget_grab_focus(m_pWidget);
#if GTK_CHECK_VERSION(4, 0, 0)
bool bHasFocusChild = gtk_widget_get_focus_child(GTK_WIDGET(m_pContainer));
#else
bool bHasFocusChild = gtk_container_get_focus_child(m_pContainer);
#endif
if (!bHasFocusChild)
{
#if GTK_CHECK_VERSION(4, 0, 0)
if (GtkWidget* pChild = gtk_widget_get_first_child(m_pContainer))
{
gtk_widget_set_focus_child(m_pContainer, pChild);
bHasFocusChild = true;
}
#else
GList* pChildren = gtk_container_get_children(m_pContainer);
if (GList* pChild = g_list_first(pChildren))
{
gtk_container_set_focus_child(m_pContainer, static_cast<GtkWidget*>(pChild->data));
bHasFocusChild = true;
}
g_list_free(pChildren);
#endif
}
if (bHasFocusChild)
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_child_focus(gtk_widget_get_focus_child(m_pWidget), GTK_DIR_TAB_FORWARD);
#else
gtk_widget_child_focus(gtk_container_get_focus_child(GTK_CONTAINER(m_pWidget)), GTK_DIR_TAB_FORWARD);
#endif
}
}
virtual void move(weld::Widget* pWidget, weld::Container* pNewParent) override
{
GtkInstanceWidget* pGtkWidget = dynamic_cast<GtkInstanceWidget*>(pWidget);
assert(pGtkWidget);
GtkWidget* pChild = pGtkWidget->getWidget();
g_object_ref(pChild);
auto pOldContainer = getContainer();
container_remove(GTK_WIDGET(pOldContainer), pChild);
GtkInstanceContainer* pNewGtkParent = dynamic_cast<GtkInstanceContainer*>(pNewParent);
assert(!pNewParent || pNewGtkParent);
if (pNewGtkParent)
{
auto pNewContainer = pNewGtkParent->getContainer();
container_add(GTK_WIDGET(pNewContainer), pChild);
}
g_object_unref(pChild);
}
virtual css::uno::Reference<css::awt::XWindow> CreateChildFrame() override
{
// This will cause a GtkSalFrame to be created. With WB_SYSTEMCHILDWINDOW set it
// will create a toplevel GtkEventBox window
auto xEmbedWindow = VclPtr<ChildFrame>::Create(ImplGetDefaultWindow(), WB_SYSTEMCHILDWINDOW | WB_DIALOGCONTROL | WB_CHILDDLGCTRL);
SalFrame* pFrame = xEmbedWindow->ImplGetFrame();
GtkSalFrame* pGtkFrame = dynamic_cast<GtkSalFrame*>(pFrame);
assert(pGtkFrame);
// relocate that toplevel GtkEventBox into this widget
GtkWidget* pWindow = pGtkFrame->getWindow();
GtkWidget* pParent = gtk_widget_get_parent(pWindow);
g_object_ref(pWindow);
container_remove(pParent, pWindow);
container_add(GTK_WIDGET(m_pContainer), pWindow);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_child_set(m_pContainer, pWindow, "expand", true, "fill", true, nullptr);
#endif
gtk_widget_set_hexpand(pWindow, true);
gtk_widget_set_vexpand(pWindow, true);
gtk_widget_realize(pWindow);
gtk_widget_set_can_focus(pWindow, true);
g_object_unref(pWindow);
// NoActivate otherwise Show grab focus to this widget
xEmbedWindow->Show(true, ShowFlags::NoActivate);
css::uno::Reference<css::awt::XWindow> xWindow(xEmbedWindow->GetComponentInterface(), css::uno::UNO_QUERY);
return xWindow;
}
virtual ~GtkInstanceContainer() override
{
if (m_nSetFocusChildSignalId)
g_signal_handler_disconnect(m_pContainer, m_nSetFocusChildSignalId);
}
};
}
std::unique_ptr<weld::Container> GtkInstanceWidget::weld_parent() const
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (!pParent)
return nullptr;
#if !GTK_CHECK_VERSION(4, 0, 0)
return std::make_unique<GtkInstanceContainer>(GTK_CONTAINER(pParent), m_pBuilder, false);
#else
return std::make_unique<GtkInstanceContainer>(pParent, m_pBuilder, false);
#endif
}
namespace {
bool sortButtons(const GtkWidget* pA, const GtkWidget* pB)
{
//order within groups according to platform rules
return getButtonPriority(get_buildable_id(GTK_BUILDABLE(pA))) <
getButtonPriority(get_buildable_id(GTK_BUILDABLE(pB)));
}
void sort_native_button_order(GtkBox* pContainer)
{
std::vector<GtkWidget*> aChildren;
#if GTK_CHECK_VERSION(4, 0, 0)
for (GtkWidget* pChild = gtk_widget_get_first_child(GTK_WIDGET(pContainer));
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
aChildren.push_back(pChild);
}
#else
GList* pChildren = gtk_container_get_children(GTK_CONTAINER(pContainer));
for (GList* pChild = g_list_first(pChildren); pChild; pChild = g_list_next(pChild))
aChildren.push_back(static_cast<GtkWidget*>(pChild->data));
g_list_free(pChildren);
#endif
//sort child order within parent so that we match the platform button order
std::stable_sort(aChildren.begin(), aChildren.end(), sortButtons);
#if GTK_CHECK_VERSION(4, 0, 0)
for (size_t pos = 0; pos < aChildren.size(); ++pos)
gtk_box_reorder_child_after(pContainer, aChildren[pos], pos ? aChildren[pos - 1] : nullptr);
#else
for (size_t pos = 0; pos < aChildren.size(); ++pos)
gtk_box_reorder_child(pContainer, aChildren[pos], pos);
#endif
}
class GtkInstanceBox : public GtkInstanceContainer, public virtual weld::Box
{
private:
GtkBox* m_pBox;
public:
GtkInstanceBox(GtkBox* pBox, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
#if !GTK_CHECK_VERSION(4, 0, 0)
: GtkInstanceContainer(GTK_CONTAINER(pBox), pBuilder, bTakeOwnership)
#else
: GtkInstanceContainer(GTK_WIDGET(pBox), pBuilder, bTakeOwnership)
#endif
, m_pBox(pBox)
{
}
virtual void reorder_child(weld::Widget* pWidget, int nNewPosition) override
{
GtkInstanceWidget* pGtkWidget = dynamic_cast<GtkInstanceWidget*>(pWidget);
assert(pGtkWidget);
GtkWidget* pChild = pGtkWidget->getWidget();
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_box_reorder_child(m_pBox, pChild, nNewPosition);
#else
if (nNewPosition == 0)
gtk_box_reorder_child_after(m_pBox, pChild, nullptr);
else
{
int nNewSiblingPos = nNewPosition - 1;
int nChildPosition = 0;
for (GtkWidget* pNewSibling = gtk_widget_get_first_child(GTK_WIDGET(m_pBox));
pNewSibling; pNewSibling = gtk_widget_get_next_sibling(pNewSibling))
{
if (nChildPosition == nNewSiblingPos)
{
gtk_box_reorder_child_after(m_pBox, pChild, pNewSibling);
break;
}
++nChildPosition;
}
}
#endif
}
virtual void sort_native_button_order() override
{
::sort_native_button_order(m_pBox);
}
};
}
namespace
{
Point get_csd_offset(GtkWidget* pTopLevel)
{
// try and omit drawing CSD under wayland
GtkWidget* pChild = widget_get_first_child(pTopLevel);
gtk_coord x, y;
gtk_widget_translate_coordinates(pChild, pTopLevel, 0, 0, &x, &y);
#if !GTK_CHECK_VERSION(4, 0, 0)
int innerborder = gtk_container_get_border_width(GTK_CONTAINER(pChild));
int outerborder = gtk_container_get_border_width(GTK_CONTAINER(pTopLevel));
int totalborder = outerborder + innerborder;
x -= totalborder;
y -= totalborder;
#endif
return Point(x, y);
}
void do_collect_screenshot_data(GtkWidget* pItem, gpointer data)
{
GtkWidget* pTopLevel = widget_get_toplevel(pItem);
gtk_coord x, y;
gtk_widget_translate_coordinates(pItem, pTopLevel, 0, 0, &x, &y);
Point aOffset = get_csd_offset(pTopLevel);
GtkAllocation alloc;
gtk_widget_get_allocation(pItem, &alloc);
const basegfx::B2IPoint aCurrentTopLeft(x - aOffset.X(), y - aOffset.Y());
const basegfx::B2IRange aCurrentRange(aCurrentTopLeft, aCurrentTopLeft + basegfx::B2IPoint(alloc.width, alloc.height));
if (!aCurrentRange.isEmpty())
{
weld::ScreenShotCollection* pCollection = static_cast<weld::ScreenShotCollection*>(data);
pCollection->emplace_back(::get_help_id(pItem), aCurrentRange);
}
#if GTK_CHECK_VERSION(4, 0, 0)
for (GtkWidget* pChild = gtk_widget_get_first_child(pItem);
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
do_collect_screenshot_data(pChild, data);
}
#else
if (GTK_IS_CONTAINER(pItem))
gtk_container_forall(GTK_CONTAINER(pItem), do_collect_screenshot_data, data);
#endif
}
tools::Rectangle get_monitor_workarea(GtkWidget* pWindow)
{
GdkRectangle aRect;
#if !GTK_CHECK_VERSION(4, 0, 0)
GdkScreen* pScreen = gtk_widget_get_screen(pWindow);
gint nMonitor = gdk_screen_get_monitor_at_window(pScreen, widget_get_surface(pWindow));
gdk_screen_get_monitor_workarea(pScreen, nMonitor, &aRect);
#else
GdkDisplay* pDisplay = gtk_widget_get_display(pWindow);
GdkSurface* gdkWindow = widget_get_surface(pWindow);
GdkMonitor* pMonitor = gdk_display_get_monitor_at_surface(pDisplay, gdkWindow);
gdk_monitor_get_geometry(pMonitor, &aRect);
#endif
return tools::Rectangle(aRect.x, aRect.y, aRect.x + aRect.width, aRect.y + aRect.height);
}
class GtkInstanceWindow : public GtkInstanceContainer, public virtual weld::Window
{
private:
GtkWindow* m_pWindow;
rtl::Reference<SalGtkXWindow> m_xWindow; //uno api
gulong m_nToplevelFocusChangedSignalId;
protected:
std::optional<Point> m_aPosWhileInvis; //tdf#146648 store last known position when visible to return as pos if hidden
#if !GTK_CHECK_VERSION(4, 0, 0)
static void implResetDefault(GtkWidget *pWidget, gpointer user_data)
{
if (GTK_IS_BUTTON(pWidget))
g_object_set(G_OBJECT(pWidget), "has-default", false, nullptr);
if (GTK_IS_CONTAINER(pWidget))
gtk_container_forall(GTK_CONTAINER(pWidget), implResetDefault, user_data);
}
void recursively_unset_default_buttons()
{
implResetDefault(GTK_WIDGET(m_pWindow), nullptr);
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean help_pressed(GtkAccelGroup*, GObject*, guint, GdkModifierType, gpointer widget)
{
GtkInstanceWindow* pThis = static_cast<GtkInstanceWindow*>(widget);
pThis->help();
return true;
}
#endif
static void signalToplevelFocusChanged(GtkWindow*, GParamSpec*, gpointer widget)
{
GtkInstanceWindow* pThis = static_cast<GtkInstanceWindow*>(widget);
pThis->signal_container_focus_changed();
}
bool isPositioningAllowed() const
{
// no X/Y positioning under Wayland
GdkDisplay *pDisplay = gtk_widget_get_display(m_pWidget);
return !DLSYM_GDK_IS_WAYLAND_DISPLAY(pDisplay);
}
protected:
void help();
public:
GtkInstanceWindow(GtkWindow* pWindow, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
#if !GTK_CHECK_VERSION(4, 0, 0)
: GtkInstanceContainer(GTK_CONTAINER(pWindow), pBuilder, bTakeOwnership)
#else
: GtkInstanceContainer(GTK_WIDGET(pWindow), pBuilder, bTakeOwnership)
#endif
, m_pWindow(pWindow)
, m_nToplevelFocusChangedSignalId(0)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
const bool bIsFrameWeld = pBuilder == nullptr;
if (!bIsFrameWeld)
{
//hook up F1 to show help
GtkAccelGroup *pGroup = gtk_accel_group_new();
GClosure* closure = g_cclosure_new(G_CALLBACK(help_pressed), this, nullptr);
gtk_accel_group_connect(pGroup, GDK_KEY_F1, static_cast<GdkModifierType>(0), GTK_ACCEL_LOCKED, closure);
gtk_window_add_accel_group(pWindow, pGroup);
}
#endif
}
virtual void set_title(const OUString& rTitle) override
{
::set_title(m_pWindow, rTitle);
}
virtual OUString get_title() const override
{
return ::get_title(m_pWindow);
}
virtual css::uno::Reference<css::awt::XWindow> GetXWindow() override
{
if (!m_xWindow.is())
m_xWindow.set(new SalGtkXWindow(this, m_pWidget));
return m_xWindow;
}
virtual void set_modal(bool bModal) override
{
gtk_window_set_modal(m_pWindow, bModal);
}
virtual bool get_modal() const override
{
return gtk_window_get_modal(m_pWindow);
}
virtual void resize_to_request() override
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_window_set_default_size(m_pWindow, 1, 1);
#else
gtk_window_resize(m_pWindow, 1, 1);
#endif
}
virtual void window_move(int x, int y) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_window_move(m_pWindow, x, y);
#else
(void)x;
(void)y;
#endif
}
virtual SystemEnvData get_system_data() const override
{
GtkSalFrame* pFrame = GtkSalFrame::getFromWindow(GTK_WIDGET(m_pWindow));
assert(pFrame && "nothing should call this impl, yet anyway, if ever, except on result of GetFrameWeld()");
const SystemEnvData* pEnvData = pFrame->GetSystemData();
assert(pEnvData);
return *pEnvData;
}
virtual Size get_size() const override
{
int current_width, current_height;
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_window_get_size(m_pWindow, ¤t_width, ¤t_height);
#else
gtk_window_get_default_size(m_pWindow, ¤t_width, ¤t_height);
#endif
return Size(current_width, current_height);
}
virtual Point get_position() const override
{
if (m_aPosWhileInvis)
{
assert(!get_visible());
return *m_aPosWhileInvis;
}
int current_x(0), current_y(0);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_window_get_position(m_pWindow, ¤t_x, ¤t_y);
#endif
return Point(current_x, current_y);
}
virtual void show() override
{
m_aPosWhileInvis.reset();
GtkInstanceContainer::show();
}
virtual void hide() override
{
if (is_visible())
m_aPosWhileInvis = get_position();
GtkInstanceContainer::hide();
}
virtual tools::Rectangle get_monitor_workarea() const override
{
return ::get_monitor_workarea(GTK_WIDGET(m_pWindow));
}
virtual void set_centered_on_parent(bool bTrackGeometryRequests) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
if (bTrackGeometryRequests)
gtk_window_set_position(m_pWindow, GTK_WIN_POS_CENTER_ALWAYS);
else
gtk_window_set_position(m_pWindow, GTK_WIN_POS_CENTER_ON_PARENT);
#else
(void)bTrackGeometryRequests;
#endif
}
virtual bool get_resizable() const override
{
return gtk_window_get_resizable(m_pWindow);
}
virtual bool has_toplevel_focus() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
return gtk_window_is_active(m_pWindow);
#else
return gtk_window_has_toplevel_focus(m_pWindow);
#endif
}
virtual void present() override
{
gtk_window_present(m_pWindow);
}
virtual void change_default_widget(weld::Widget* pOld, weld::Widget* pNew) override
{
GtkInstanceWidget* pGtkNew = dynamic_cast<GtkInstanceWidget*>(pNew);
GtkWidget* pWidgetNew = pGtkNew ? pGtkNew->getWidget() : nullptr;
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_window_set_default_widget(m_pWindow, pWidgetNew);
(void)pOld;
#else
GtkInstanceWidget* pGtkOld = dynamic_cast<GtkInstanceWidget*>(pOld);
GtkWidget* pWidgetOld = pGtkOld ? pGtkOld->getWidget() : nullptr;
if (pWidgetOld)
g_object_set(G_OBJECT(pWidgetOld), "has-default", false, nullptr);
else
recursively_unset_default_buttons();
if (pWidgetNew)
g_object_set(G_OBJECT(pWidgetNew), "has-default", true, nullptr);
#endif
}
virtual bool is_default_widget(const weld::Widget* pCandidate) const override
{
const GtkInstanceWidget* pGtkCandidate = dynamic_cast<const GtkInstanceWidget*>(pCandidate);
GtkWidget* pWidget = pGtkCandidate ? pGtkCandidate->getWidget() : nullptr;
#if GTK_CHECK_VERSION(4, 0, 0)
return pWidget && gtk_window_get_default_widget(m_pWindow) == pWidget;
#else
gboolean has_default(false);
if (pWidget)
g_object_get(G_OBJECT(pWidget), "has-default", &has_default, nullptr);
return has_default;
#endif
}
virtual void set_window_state(const OUString& rStr) override
{
const vcl::WindowData aData(rStr);
const auto nMask = aData.mask();
const auto nState = aData.state() & vcl::WindowState::SystemMask;
if ((nMask & vcl::WindowDataMask::Size) == vcl::WindowDataMask::Size)
{
gtk_window_set_default_size(m_pWindow, aData.width(), aData.height());
}
if (nMask & vcl::WindowDataMask::State)
{
if (nState & vcl::WindowState::Maximized)
gtk_window_maximize(m_pWindow);
else
gtk_window_unmaximize(m_pWindow);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
if (isPositioningAllowed() && ((nMask & vcl::WindowDataMask::Pos) == vcl::WindowDataMask::Pos))
{
gtk_window_move(m_pWindow, aData.x(), aData.y());
}
#endif
}
virtual OUString get_window_state(vcl::WindowDataMask nMask) const override
{
bool bPositioningAllowed = isPositioningAllowed();
vcl::WindowData aData;
vcl::WindowDataMask nAvailable = vcl::WindowDataMask::State | vcl::WindowDataMask::Size;
if (bPositioningAllowed)
nAvailable |= vcl::WindowDataMask::Pos;
aData.setMask(nMask & nAvailable);
if (nMask & vcl::WindowDataMask::State)
{
vcl::WindowState nState = vcl::WindowState::Normal;
if (gtk_window_is_maximized(m_pWindow))
nState |= vcl::WindowState::Maximized;
aData.setState(nState);
}
if (bPositioningAllowed && (nMask & vcl::WindowDataMask::Pos))
aData.setPos(get_position());
if (nMask & vcl::WindowDataMask::Size)
aData.setSize(get_size());
return aData.toStr();
}
virtual void connect_container_focus_changed(const Link<Container&, void>& rLink) override
{
if (!m_nToplevelFocusChangedSignalId)
m_nToplevelFocusChangedSignalId = g_signal_connect(m_pWindow, "notify::has-toplevel-focus", G_CALLBACK(signalToplevelFocusChanged), this);
weld::Container::connect_container_focus_changed(rLink);
}
virtual void disable_notify_events() override
{
if (m_nToplevelFocusChangedSignalId)
g_signal_handler_block(m_pWidget, m_nToplevelFocusChangedSignalId);
GtkInstanceContainer::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceContainer::enable_notify_events();
if (m_nToplevelFocusChangedSignalId)
g_signal_handler_unblock(m_pWidget, m_nToplevelFocusChangedSignalId);
}
virtual VclPtr<VirtualDevice> screenshot() override
{
// detect if we have to manually setup its size
bool bAlreadyRealized = gtk_widget_get_realized(GTK_WIDGET(m_pWindow));
// has to be visible for draw to work
bool bAlreadyVisible = gtk_widget_get_visible(GTK_WIDGET(m_pWindow));
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!bAlreadyVisible)
{
if (GTK_IS_DIALOG(m_pWindow))
sort_native_button_order(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(m_pWindow))));
gtk_widget_show(GTK_WIDGET(m_pWindow));
}
#endif
if (!bAlreadyRealized)
{
GtkAllocation allocation;
gtk_widget_realize(GTK_WIDGET(m_pWindow));
gtk_widget_get_allocation(GTK_WIDGET(m_pWindow), &allocation);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_size_allocate(GTK_WIDGET(m_pWindow), &allocation);
#else
gtk_widget_size_allocate(GTK_WIDGET(m_pWindow), &allocation, 0);
#endif
}
VclPtr<VirtualDevice> xOutput(VclPtr<VirtualDevice>::Create(DeviceFormat::WITHOUT_ALPHA));
xOutput->SetOutputSizePixel(get_size());
cairo_surface_t* pSurface = get_underlying_cairo_surface(*xOutput);
cairo_t* cr = cairo_create(pSurface);
Point aOffset = get_csd_offset(GTK_WIDGET(m_pWindow));
cairo_translate(cr, -aOffset.X(), -aOffset.Y());
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_draw(GTK_WIDGET(m_pWindow), cr);
#else
GtkSnapshot* pSnapshot = gtk_snapshot_new();
GtkWidgetClass* pWidgetClass = GTK_WIDGET_GET_CLASS(GTK_WIDGET(m_pWindow));
pWidgetClass->snapshot(GTK_WIDGET(m_pWindow), pSnapshot);
GskRenderNode* pNode = gtk_snapshot_free_to_node(pSnapshot);
gsk_render_node_draw(pNode, cr);
gsk_render_node_unref(pNode);
#endif
cairo_destroy(cr);
if (!bAlreadyVisible)
gtk_widget_hide(GTK_WIDGET(m_pWindow));
if (!bAlreadyRealized)
gtk_widget_unrealize(GTK_WIDGET(m_pWindow));
return xOutput;
}
virtual weld::ScreenShotCollection collect_screenshot_data() override
{
weld::ScreenShotCollection aRet;
#if GTK_CHECK_VERSION(4, 0, 0)
for (GtkWidget* pChild = gtk_widget_get_first_child(GTK_WIDGET(m_pWindow));
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
do_collect_screenshot_data(pChild, &aRet);
}
#else
gtk_container_foreach(GTK_CONTAINER(m_pWindow), do_collect_screenshot_data, &aRet);
#endif
return aRet;
}
virtual const vcl::ILibreOfficeKitNotifier* GetLOKNotifier() override
{
// dummy implementation
return nullptr;
}
virtual ~GtkInstanceWindow() override
{
if (m_nToplevelFocusChangedSignalId)
g_signal_handler_disconnect(m_pWindow, m_nToplevelFocusChangedSignalId);
if (m_xWindow.is())
m_xWindow->clear();
}
};
class GtkInstanceDialog;
struct DialogRunner
{
GtkWindow* m_pDialog;
GtkInstanceDialog *m_pInstance;
gint m_nResponseId;
GMainLoop *m_pLoop;
VclPtr<vcl::Window> m_xFrameWindow;
int m_nModalDepth;
DialogRunner(GtkWindow* pDialog, GtkInstanceDialog* pInstance)
: m_pDialog(pDialog)
, m_pInstance(pInstance)
, m_nResponseId(GTK_RESPONSE_NONE)
, m_pLoop(nullptr)
, m_nModalDepth(0)
{
GtkWindow* pParent = gtk_window_get_transient_for(m_pDialog);
GtkSalFrame* pFrame = pParent ? GtkSalFrame::getFromWindow(GTK_WIDGET(pParent)) : nullptr;
m_xFrameWindow = pFrame ? pFrame->GetWindow() : nullptr;
}
bool loop_is_running() const
{
return m_pLoop && g_main_loop_is_running(m_pLoop);
}
void loop_quit()
{
if (g_main_loop_is_running(m_pLoop))
g_main_loop_quit(m_pLoop);
}
static void signal_response(GtkDialog*, gint nResponseId, gpointer data);
static void signal_cancel(GtkAssistant*, gpointer data);
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signal_delete(GtkDialog* pDialog, GdkEventAny*, gpointer data)
{
DialogRunner* pThis = static_cast<DialogRunner*>(data);
if (GTK_IS_ASSISTANT(pThis->m_pDialog))
{
// An assistant isn't a dialog, but we want to treat it like one
signal_response(pDialog, GTK_RESPONSE_DELETE_EVENT, data);
}
else
pThis->loop_quit();
return true; /* Do not destroy */
}
#endif
static void signal_destroy(GtkDialog*, gpointer data)
{
DialogRunner* pThis = static_cast<DialogRunner*>(data);
pThis->loop_quit();
}
void inc_modal_count()
{
if (m_xFrameWindow)
{
m_xFrameWindow->IncModalCount();
if (m_nModalDepth == 0)
m_xFrameWindow->ImplGetFrame()->NotifyModalHierarchy(true);
++m_nModalDepth;
}
}
void dec_modal_count()
{
if (m_xFrameWindow)
{
m_xFrameWindow->DecModalCount();
--m_nModalDepth;
if (m_nModalDepth == 0)
m_xFrameWindow->ImplGetFrame()->NotifyModalHierarchy(false);
}
}
// same as gtk_dialog_run except that unmap doesn't auto-respond
// so we can hide the dialog and restore it without a response getting
// triggered
gint run()
{
g_object_ref(m_pDialog);
inc_modal_count();
bool bWasModal = gtk_window_get_modal(m_pDialog);
if (!bWasModal)
gtk_window_set_modal(m_pDialog, true);
if (!gtk_widget_get_visible(GTK_WIDGET(m_pDialog)))
gtk_widget_show(GTK_WIDGET(m_pDialog));
gulong nSignalResponseId = GTK_IS_DIALOG(m_pDialog) ? g_signal_connect(m_pDialog, "response", G_CALLBACK(signal_response), this) : 0;
gulong nSignalCancelId = GTK_IS_ASSISTANT(m_pDialog) ? g_signal_connect(m_pDialog, "cancel", G_CALLBACK(signal_cancel), this) : 0;
#if !GTK_CHECK_VERSION(4, 0, 0)
gulong nSignalDeleteId = g_signal_connect(m_pDialog, "delete-event", G_CALLBACK(signal_delete), this);
#endif
gulong nSignalDestroyId = g_signal_connect(m_pDialog, "destroy", G_CALLBACK(signal_destroy), this);
m_pLoop = g_main_loop_new(nullptr, false);
m_nResponseId = GTK_RESPONSE_NONE;
main_loop_run(m_pLoop);
g_main_loop_unref(m_pLoop);
m_pLoop = nullptr;
if (!bWasModal)
gtk_window_set_modal(m_pDialog, false);
if (nSignalResponseId)
g_signal_handler_disconnect(m_pDialog, nSignalResponseId);
if (nSignalCancelId)
g_signal_handler_disconnect(m_pDialog, nSignalCancelId);
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pDialog, nSignalDeleteId);
#endif
g_signal_handler_disconnect(m_pDialog, nSignalDestroyId);
dec_modal_count();
g_object_unref(m_pDialog);
return m_nResponseId;
}
~DialogRunner()
{
if (m_xFrameWindow && m_nModalDepth)
{
// if, like the calc validation dialog does, the modality was
// toggled off during execution ensure that on cleanup the parent
// is left in the state it was found
while (m_nModalDepth++ < 0)
m_xFrameWindow->IncModalCount();
}
}
};
}
typedef std::set<GtkWidget*> winset;
namespace
{
#if GTK_CHECK_VERSION(4, 0, 0)
void collectVisibleChildren(GtkWidget* pTop, winset& rVisibleWidgets)
{
for (GtkWidget* pChild = gtk_widget_get_first_child(pTop);
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
if (!gtk_widget_get_visible(pChild))
continue;
rVisibleWidgets.insert(pChild);
collectVisibleChildren(pChild, rVisibleWidgets);
}
}
#endif
void hideUnless(GtkWidget* pTop, const winset& rVisibleWidgets,
std::vector<GtkWidget*> &rWasVisibleWidgets)
{
#if GTK_CHECK_VERSION(4, 0, 0)
for (GtkWidget* pChild = gtk_widget_get_first_child(pTop);
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
if (!gtk_widget_get_visible(pChild))
continue;
if (rVisibleWidgets.find(pChild) == rVisibleWidgets.end())
{
g_object_ref(pChild);
rWasVisibleWidgets.emplace_back(pChild);
gtk_widget_hide(pChild);
}
else
{
hideUnless(pChild, rVisibleWidgets, rWasVisibleWidgets);
}
}
#else
GList* pChildren = gtk_container_get_children(GTK_CONTAINER(pTop));
for (GList* pEntry = g_list_first(pChildren); pEntry; pEntry = g_list_next(pEntry))
{
GtkWidget* pChild = static_cast<GtkWidget*>(pEntry->data);
if (!gtk_widget_get_visible(pChild))
continue;
if (rVisibleWidgets.find(pChild) == rVisibleWidgets.end())
{
g_object_ref(pChild);
rWasVisibleWidgets.emplace_back(pChild);
gtk_widget_hide(pChild);
}
else if (GTK_IS_CONTAINER(pChild))
{
hideUnless(pChild, rVisibleWidgets, rWasVisibleWidgets);
}
}
g_list_free(pChildren);
#endif
}
class GtkInstanceButton;
class GtkInstanceDialog : public GtkInstanceWindow, public virtual weld::Dialog
{
private:
GtkWindow* m_pDialog;
DialogRunner m_aDialogRun;
std::shared_ptr<weld::DialogController> m_xDialogController;
// Used to keep ourself alive during a runAsync(when doing runAsync without a DialogController)
std::shared_ptr<weld::Dialog> m_xRunAsyncSelf;
std::function<void(sal_Int32)> m_aFunc;
gulong m_nCloseSignalId;
gulong m_nResponseSignalId;
gulong m_nCancelSignalId;
gulong m_nSignalDeleteId;
// for calc ref dialog that shrink to range selection widgets and resize back
GtkWidget* m_pRefEdit;
std::vector<GtkWidget*> m_aHiddenWidgets; // vector of hidden Controls
int m_nOldEditWidth; // Original width of the input field
int m_nOldEditWidthReq; // Original width request of the input field
#if !GTK_CHECK_VERSION(4, 0, 0)
int m_nOldBorderWidth; // border width for expanded dialog
#endif
void signal_close()
{
close(true);
}
static void signalClose(GtkWidget*, gpointer widget)
{
GtkInstanceDialog* pThis = static_cast<GtkInstanceDialog*>(widget);
pThis->signal_close();
}
static void signalAsyncResponse(GtkWidget*, gint ret, gpointer widget)
{
GtkInstanceDialog* pThis = static_cast<GtkInstanceDialog*>(widget);
pThis->asyncresponse(ret);
}
static void signalAsyncCancel(GtkAssistant*, gpointer widget)
{
GtkInstanceDialog* pThis = static_cast<GtkInstanceDialog*>(widget);
// make esc in an assistant act as if cancel button was pressed
pThis->close(false);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalAsyncDelete(GtkWidget* pDialog, GdkEventAny*, gpointer widget)
{
GtkInstanceDialog* pThis = static_cast<GtkInstanceDialog*>(widget);
if (GTK_IS_ASSISTANT(pThis->m_pDialog))
{
// An assistant isn't a dialog, but we want to treat it like one
signalAsyncResponse(pDialog, GTK_RESPONSE_DELETE_EVENT, widget);
}
return true; /* Do not destroy */
}
#endif
static int GtkToVcl(int ret)
{
if (ret == GTK_RESPONSE_OK)
ret = RET_OK;
else if (ret == GTK_RESPONSE_CANCEL)
ret = RET_CANCEL;
else if (ret == GTK_RESPONSE_DELETE_EVENT)
ret = RET_CANCEL;
else if (ret == GTK_RESPONSE_CLOSE)
ret = RET_CLOSE;
else if (ret == GTK_RESPONSE_YES)
ret = RET_YES;
else if (ret == GTK_RESPONSE_NO)
ret = RET_NO;
else if (ret == GTK_RESPONSE_HELP)
ret = RET_HELP;
return ret;
}
static int VclToGtk(int nResponse)
{
if (nResponse == RET_OK)
return GTK_RESPONSE_OK;
else if (nResponse == RET_CANCEL)
return GTK_RESPONSE_CANCEL;
else if (nResponse == RET_CLOSE)
return GTK_RESPONSE_CLOSE;
else if (nResponse == RET_YES)
return GTK_RESPONSE_YES;
else if (nResponse == RET_NO)
return GTK_RESPONSE_NO;
else if (nResponse == RET_HELP)
return GTK_RESPONSE_HELP;
return nResponse;
}
void asyncresponse(gint ret);
#if !GTK_CHECK_VERSION(4, 0, 0)
static void signalActivate(GtkMenuItem*, gpointer data)
{
bool* pActivate = static_cast<bool*>(data);
*pActivate = true;
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
bool signal_screenshot_popup_menu(const GdkEventButton* pEvent)
{
GtkWidget *pMenu = gtk_menu_new();
GtkWidget* pMenuItem = gtk_menu_item_new_with_mnemonic(MapToGtkAccelerator(VclResId(SV_BUTTONTEXT_SCREENSHOT)).getStr());
gtk_menu_shell_append(GTK_MENU_SHELL(pMenu), pMenuItem);
bool bActivate(false);
g_signal_connect(pMenuItem, "activate", G_CALLBACK(signalActivate), &bActivate);
gtk_widget_show(pMenuItem);
int button, event_time;
if (pEvent)
{
button = pEvent->button;
event_time = pEvent->time;
}
else
{
button = 0;
event_time = gtk_get_current_event_time();
}
gtk_menu_attach_to_widget(GTK_MENU(pMenu), GTK_WIDGET(m_pDialog), nullptr);
GMainLoop* pLoop = g_main_loop_new(nullptr, true);
gulong nSignalId = g_signal_connect_swapped(G_OBJECT(pMenu), "deactivate", G_CALLBACK(g_main_loop_quit), pLoop);
gtk_menu_popup(GTK_MENU(pMenu), nullptr, nullptr, nullptr, nullptr, button, event_time);
if (g_main_loop_is_running(pLoop))
main_loop_run(pLoop);
g_main_loop_unref(pLoop);
g_signal_handler_disconnect(pMenu, nSignalId);
gtk_menu_detach(GTK_MENU(pMenu));
if (bActivate)
{
// open screenshot annotation dialog
VclAbstractDialogFactory* pFact = VclAbstractDialogFactory::Create();
VclPtr<AbstractScreenshotAnnotationDlg> xTmp = pFact->CreateScreenshotAnnotationDlg(*this);
ScopedVclPtr<AbstractScreenshotAnnotationDlg> xDialog(xTmp);
xDialog->Execute();
}
return false;
}
#endif
static gboolean signalScreenshotPopupMenu(GtkWidget*, gpointer widget)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkInstanceDialog* pThis = static_cast<GtkInstanceDialog*>(widget);
return pThis->signal_screenshot_popup_menu(nullptr);
#else
(void)widget;
return false;
#endif
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalScreenshotButton(GtkWidget*, GdkEventButton* pEvent, gpointer widget)
{
GtkInstanceDialog* pThis = static_cast<GtkInstanceDialog*>(widget);
SolarMutexGuard aGuard;
return pThis->signal_screenshot_button(pEvent);
}
bool signal_screenshot_button(GdkEventButton* pEvent)
{
if (gdk_event_triggers_context_menu(reinterpret_cast<GdkEvent*>(pEvent)) && pEvent->type == GDK_BUTTON_PRESS)
{
//if handled for context menu, stop processing
return signal_screenshot_popup_menu(pEvent);
}
return false;
}
#endif
public:
GtkInstanceDialog(GtkWindow* pDialog, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWindow(pDialog, pBuilder, bTakeOwnership)
, m_pDialog(pDialog)
, m_aDialogRun(pDialog, this)
, m_nResponseSignalId(0)
, m_nCancelSignalId(0)
, m_nSignalDeleteId(0)
, m_pRefEdit(nullptr)
, m_nOldEditWidth(0)
, m_nOldEditWidthReq(0)
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_nOldBorderWidth(0)
#endif
{
if (GTK_IS_DIALOG(m_pDialog) || GTK_IS_ASSISTANT(m_pDialog))
m_nCloseSignalId = g_signal_connect(m_pDialog, "close", G_CALLBACK(signalClose), this);
else
m_nCloseSignalId = 0;
const bool bScreenshotMode(officecfg::Office::Common::Misc::ScreenshotMode::get());
if (bScreenshotMode)
{
g_signal_connect(m_pDialog, "popup-menu", G_CALLBACK(signalScreenshotPopupMenu), this);
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_connect(m_pDialog, "button-press-event", G_CALLBACK(signalScreenshotButton), this);
#endif
}
}
virtual bool runAsync(std::shared_ptr<weld::DialogController> rDialogController, const std::function<void(sal_Int32)>& func) override
{
assert(!m_nResponseSignalId && !m_nCancelSignalId && !m_nSignalDeleteId);
m_xDialogController = rDialogController;
m_aFunc = func;
if (get_modal())
m_aDialogRun.inc_modal_count();
show();
m_nResponseSignalId = GTK_IS_DIALOG(m_pDialog) ? g_signal_connect(m_pDialog, "response", G_CALLBACK(signalAsyncResponse), this) : 0;
m_nCancelSignalId = GTK_IS_ASSISTANT(m_pDialog) ? g_signal_connect(m_pDialog, "cancel", G_CALLBACK(signalAsyncCancel), this) : 0;
#if !GTK_CHECK_VERSION(4, 0, 0)
m_nSignalDeleteId = g_signal_connect(m_pDialog, "delete-event", G_CALLBACK(signalAsyncDelete), this);
#endif
return true;
}
virtual bool runAsync(std::shared_ptr<Dialog> const & rxSelf, const std::function<void(sal_Int32)>& func) override
{
assert( rxSelf.get() == this );
assert(!m_nResponseSignalId && !m_nCancelSignalId && !m_nSignalDeleteId);
// In order to store a shared_ptr to ourself, we have to have been constructed by make_shared,
// which is that rxSelf enforces.
m_xRunAsyncSelf = rxSelf;
m_aFunc = func;
if (get_modal())
m_aDialogRun.inc_modal_count();
show();
m_nResponseSignalId = GTK_IS_DIALOG(m_pDialog) ? g_signal_connect(m_pDialog, "response", G_CALLBACK(signalAsyncResponse), this) : 0;
m_nCancelSignalId = GTK_IS_ASSISTANT(m_pDialog) ? g_signal_connect(m_pDialog, "cancel", G_CALLBACK(signalAsyncCancel), this) : 0;
#if !GTK_CHECK_VERSION(4, 0, 0)
m_nSignalDeleteId = g_signal_connect(m_pDialog, "delete-event", G_CALLBACK(signalAsyncDelete), this);
#endif
return true;
}
GtkInstanceButton* has_click_handler(int nResponse);
virtual int run() override;
virtual void show() override
{
if (gtk_widget_get_visible(m_pWidget))
return;
#if !GTK_CHECK_VERSION(4, 0, 0)
if (GTK_IS_DIALOG(m_pDialog))
sort_native_button_order(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(m_pDialog))));
#endif
GtkInstanceWindow::show();
}
virtual void set_modal(bool bModal) override
{
if (get_modal() == bModal)
return;
GtkInstanceWindow::set_modal(bModal);
/* if change the dialog modality while its running, then also change the parent LibreOffice window
modal count, we typically expect the dialog modality to be restored to its original state
This change modality while running case is for...
a) the calc/chart dialogs which put up an extra range chooser
dialog, hides the original, the user can select a range of cells and
on completion the original dialog is restored
b) the validity dialog in calc
*/
// tdf#135567 we know we are running in the sync case if loop_is_running is true
// but for the async case we instead check for m_xDialogController which is set in
// runAsync and cleared in asyncresponse
if (m_aDialogRun.loop_is_running() || m_xDialogController)
{
if (bModal)
m_aDialogRun.inc_modal_count();
else
m_aDialogRun.dec_modal_count();
}
}
virtual void response(int nResponse) override;
virtual void add_button(const OUString& rText, int nResponse, const OUString& rHelpId) override
{
GtkWidget* pWidget = gtk_dialog_add_button(GTK_DIALOG(m_pDialog), MapToGtkAccelerator(rText).getStr(), VclToGtk(nResponse));
if (!rHelpId.isEmpty())
::set_help_id(pWidget, rHelpId);
}
virtual void set_default_response(int nResponse) override
{
gtk_dialog_set_default_response(GTK_DIALOG(m_pDialog), VclToGtk(nResponse));
}
virtual GtkButton* get_widget_for_response(int nGtkResponse)
{
return GTK_BUTTON(gtk_dialog_get_widget_for_response(GTK_DIALOG(m_pDialog), nGtkResponse));
}
virtual weld::Button* weld_widget_for_response(int nVclResponse) override;
virtual Container* weld_content_area() override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
return new GtkInstanceContainer(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(m_pDialog))), m_pBuilder, false);
#else
return new GtkInstanceContainer(gtk_dialog_get_content_area(GTK_DIALOG(m_pDialog)), m_pBuilder, false);
#endif
}
virtual void collapse(weld::Widget* pEdit, weld::Widget* pButton) override
{
GtkInstanceWidget* pVclEdit = dynamic_cast<GtkInstanceWidget*>(pEdit);
assert(pVclEdit);
GtkInstanceWidget* pVclButton = dynamic_cast<GtkInstanceWidget*>(pButton);
GtkWidget* pRefEdit = pVclEdit->getWidget();
GtkWidget* pRefBtn = pVclButton ? pVclButton->getWidget() : nullptr;
m_nOldEditWidth = gtk_widget_get_allocated_width(pRefEdit);
gtk_widget_get_size_request(pRefEdit, &m_nOldEditWidthReq, nullptr);
//We want just pRefBtn and pRefEdit to be shown
//mark widgets we want to be visible, starting with pRefEdit
//and all its direct parents.
winset aVisibleWidgets;
GtkWidget *pContentArea = gtk_dialog_get_content_area(GTK_DIALOG(m_pDialog));
for (GtkWidget *pCandidate = pRefEdit;
pCandidate && pCandidate != pContentArea && gtk_widget_get_visible(pCandidate);
pCandidate = gtk_widget_get_parent(pCandidate))
{
aVisibleWidgets.insert(pCandidate);
}
#if GTK_CHECK_VERSION(4, 0, 0)
collectVisibleChildren(pRefEdit, aVisibleWidgets);
#endif
if (pRefBtn)
{
#if GTK_CHECK_VERSION(4, 0, 0)
collectVisibleChildren(pRefBtn, aVisibleWidgets);
#endif
//same again with pRefBtn, except stop if there's a
//shared parent in the existing widgets
for (GtkWidget *pCandidate = pRefBtn;
pCandidate && pCandidate != pContentArea && gtk_widget_get_visible(pCandidate);
pCandidate = gtk_widget_get_parent(pCandidate))
{
if (aVisibleWidgets.insert(pCandidate).second)
break;
}
}
//hide everything except the aVisibleWidgets
hideUnless(pContentArea, aVisibleWidgets, m_aHiddenWidgets);
gtk_widget_set_size_request(pRefEdit, m_nOldEditWidth, -1);
#if !GTK_CHECK_VERSION(4, 0, 0)
m_nOldBorderWidth = gtk_container_get_border_width(GTK_CONTAINER(m_pDialog));
gtk_container_set_border_width(GTK_CONTAINER(m_pDialog), 0);
if (GtkWidget* pActionArea = gtk_dialog_get_action_area(GTK_DIALOG(m_pDialog)))
gtk_widget_hide(pActionArea);
gtk_widget_show_all(pRefEdit);
if (pRefBtn)
gtk_widget_show_all(pRefBtn);
#else
if (GtkWidget* pActionArea = gtk_dialog_get_header_bar(GTK_DIALOG(m_pDialog)))
gtk_widget_hide(pActionArea);
#endif
// calc's insert->function is springing back to its original size if the ref-button
// is used to shrink the dialog down and then the user clicks in the calc area to do
// the selection
bool bWorkaroundSizeSpringingBack = DLSYM_GDK_IS_WAYLAND_DISPLAY(gtk_widget_get_display(m_pWidget));
if (bWorkaroundSizeSpringingBack)
gtk_widget_unmap(GTK_WIDGET(m_pDialog));
resize_to_request();
if (bWorkaroundSizeSpringingBack)
gtk_widget_map(GTK_WIDGET(m_pDialog));
m_pRefEdit = pRefEdit;
}
virtual void undo_collapse() override
{
// All others: Show();
for (GtkWidget* pWindow : m_aHiddenWidgets)
{
gtk_widget_show(pWindow);
g_object_unref(pWindow);
}
m_aHiddenWidgets.clear();
gtk_widget_set_size_request(m_pRefEdit, m_nOldEditWidthReq, -1);
m_pRefEdit = nullptr;
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_set_border_width(GTK_CONTAINER(m_pDialog), m_nOldBorderWidth);
if (GtkWidget* pActionArea = gtk_dialog_get_action_area(GTK_DIALOG(m_pDialog)))
gtk_widget_show(pActionArea);
#else
if (GtkWidget* pActionArea = gtk_dialog_get_header_bar(GTK_DIALOG(m_pDialog)))
gtk_widget_show(pActionArea);
#endif
resize_to_request();
present();
}
void close(bool bCloseSignal);
virtual void SetInstallLOKNotifierHdl(const Link<void*, vcl::ILibreOfficeKitNotifier*>&) override
{
//not implemented for the gtk variant
}
virtual ~GtkInstanceDialog() override
{
if (!m_aHiddenWidgets.empty())
{
for (GtkWidget* pWindow : m_aHiddenWidgets)
g_object_unref(pWindow);
m_aHiddenWidgets.clear();
}
if (m_nCloseSignalId)
g_signal_handler_disconnect(m_pDialog, m_nCloseSignalId);
assert(!m_nResponseSignalId && !m_nCancelSignalId && !m_nSignalDeleteId);
}
};
}
void DialogRunner::signal_response(GtkDialog*, gint nResponseId, gpointer data)
{
DialogRunner* pThis = static_cast<DialogRunner*>(data);
// make GTK_RESPONSE_DELETE_EVENT act as if cancel button was pressed
if (nResponseId == GTK_RESPONSE_DELETE_EVENT)
{
pThis->m_pInstance->close(false);
return;
}
pThis->m_nResponseId = nResponseId;
pThis->loop_quit();
}
void DialogRunner::signal_cancel(GtkAssistant*, gpointer data)
{
DialogRunner* pThis = static_cast<DialogRunner*>(data);
// make esc in an assistant act as if cancel button was pressed
pThis->m_pInstance->close(false);
}
namespace {
class GtkInstanceMessageDialog : public GtkInstanceDialog, public virtual weld::MessageDialog
{
private:
GtkMessageDialog* m_pMessageDialog;
public:
GtkInstanceMessageDialog(GtkMessageDialog* pMessageDialog, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceDialog(GTK_WINDOW(pMessageDialog), pBuilder, bTakeOwnership)
, m_pMessageDialog(pMessageDialog)
{
}
virtual void set_primary_text(const OUString& rText) override
{
::set_primary_text(m_pMessageDialog, rText);
}
virtual OUString get_primary_text() const override
{
return ::get_primary_text(m_pMessageDialog);
}
virtual void set_secondary_text(const OUString& rText) override
{
::set_secondary_text(m_pMessageDialog, rText);
}
virtual OUString get_secondary_text() const override
{
return ::get_secondary_text(m_pMessageDialog);
}
virtual Container* weld_message_area() override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
return new GtkInstanceContainer(GTK_CONTAINER(gtk_message_dialog_get_message_area(m_pMessageDialog)), m_pBuilder, false);
#else
return new GtkInstanceContainer(gtk_message_dialog_get_message_area(m_pMessageDialog), m_pBuilder, false);
#endif
}
};
void set_label_wrap(GtkLabel* pLabel, bool bWrap)
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_label_set_wrap(pLabel, bWrap);
#else
gtk_label_set_line_wrap(pLabel, bWrap);
#endif
}
class GtkInstanceAssistant : public GtkInstanceDialog, public virtual weld::Assistant
{
private:
GtkAssistant* m_pAssistant;
GtkWidget* m_pSidebar;
GtkWidget* m_pSidebarEventBox;
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkButtonBox* m_pButtonBox;
#else
GtkBox* m_pButtonBox;
GtkEventController* m_pSidebarClickController;
#endif
GtkButton* m_pHelp;
GtkButton* m_pBack;
GtkButton* m_pNext;
GtkButton* m_pFinish;
GtkButton* m_pCancel;
gulong m_nButtonPressSignalId;
std::vector<std::unique_ptr<GtkInstanceContainer>> m_aPages;
std::map<OUString, bool> m_aNotClickable;
int find_page(std::u16string_view ident) const
{
int nPages = gtk_assistant_get_n_pages(m_pAssistant);
for (int i = 0; i < nPages; ++i)
{
GtkWidget* pPage = gtk_assistant_get_nth_page(m_pAssistant, i);
OUString sBuildableName = ::get_buildable_id(GTK_BUILDABLE(pPage));
if (sBuildableName == ident)
return i;
}
return -1;
}
static void wrap_sidebar_label(GtkWidget *pWidget, gpointer /*user_data*/)
{
if (GTK_IS_LABEL(pWidget))
{
::set_label_wrap(GTK_LABEL(pWidget), true);
gtk_label_set_width_chars(GTK_LABEL(pWidget), 22);
gtk_label_set_max_width_chars(GTK_LABEL(pWidget), 22);
}
}
static void find_sidebar(GtkWidget *pWidget, gpointer user_data)
{
OUString sBuildableName = ::get_buildable_id(GTK_BUILDABLE(pWidget));
if (sBuildableName == "sidebar")
{
GtkWidget **ppSidebar = static_cast<GtkWidget**>(user_data);
*ppSidebar = pWidget;
}
#if !GTK_CHECK_VERSION(4, 0, 0)
if (GTK_IS_CONTAINER(pWidget))
gtk_container_forall(GTK_CONTAINER(pWidget), find_sidebar, user_data);
#endif
}
static void signalHelpClicked(GtkButton*, gpointer widget)
{
GtkInstanceAssistant* pThis = static_cast<GtkInstanceAssistant*>(widget);
pThis->signal_help_clicked();
}
void signal_help_clicked()
{
help();
}
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalButton(GtkGestureClick* /*pGesture*/, int /*n_press*/, gdouble x, gdouble y, gpointer widget)
{
GtkInstanceAssistant* pThis = static_cast<GtkInstanceAssistant*>(widget);
SolarMutexGuard aGuard;
pThis->signal_button(x, y);
}
#else
static gboolean signalButton(GtkWidget*, GdkEventButton* pEvent, gpointer widget)
{
GtkInstanceAssistant* pThis = static_cast<GtkInstanceAssistant*>(widget);
SolarMutexGuard aGuard;
return pThis->signal_button(pEvent->x, pEvent->y);
}
#endif
bool signal_button(gtk_coord event_x, gtk_coord event_y)
{
int nNewCurrentPage = -1;
GtkAllocation allocation;
int nPageIndex = 0;
#if GTK_CHECK_VERSION(4, 0, 0)
for (GtkWidget* pWidget = gtk_widget_get_first_child(m_pSidebar);
pWidget; pWidget = gtk_widget_get_next_sibling(pWidget))
{
#else
GList* pChildren = gtk_container_get_children(GTK_CONTAINER(m_pSidebar));
for (GList* pChild = g_list_first(pChildren); pChild; pChild = g_list_next(pChild))
{
GtkWidget* pWidget = static_cast<GtkWidget*>(pChild->data);
#endif
if (!gtk_widget_get_visible(pWidget))
continue;
gtk_widget_get_allocation(pWidget, &allocation);
gtk_coord dest_x1, dest_y1;
gtk_widget_translate_coordinates(pWidget,
m_pSidebarEventBox,
0,
0,
&dest_x1,
&dest_y1);
gtk_coord dest_x2, dest_y2;
gtk_widget_translate_coordinates(pWidget,
m_pSidebarEventBox,
allocation.width,
allocation.height,
&dest_x2,
&dest_y2);
if (event_x >= dest_x1 && event_x <= dest_x2 && event_y >= dest_y1 && event_y <= dest_y2)
{
nNewCurrentPage = nPageIndex;
break;
}
++nPageIndex;
}
#if !GTK_CHECK_VERSION(4, 0, 0)
g_list_free(pChildren);
#endif
if (nNewCurrentPage != -1 && nNewCurrentPage != get_current_page())
{
OUString sIdent = get_page_ident(nNewCurrentPage);
if (!m_aNotClickable[sIdent] && !signal_jump_page(sIdent))
set_current_page(nNewCurrentPage);
}
return false;
}
public:
GtkInstanceAssistant(GtkAssistant* pAssistant, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceDialog(GTK_WINDOW(pAssistant), pBuilder, bTakeOwnership)
, m_pAssistant(pAssistant)
, m_pSidebar(nullptr)
#if GTK_CHECK_VERSION(4, 0, 0)
, m_pSidebarClickController(nullptr)
#endif
, m_nButtonPressSignalId(0)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
m_pButtonBox = GTK_BUTTON_BOX(gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL));
gtk_button_box_set_layout(m_pButtonBox, GTK_BUTTONBOX_END);
gtk_box_set_spacing(GTK_BOX(m_pButtonBox), 6);
#else
m_pButtonBox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6));
#endif
m_pBack = GTK_BUTTON(gtk_button_new_with_mnemonic(MapToGtkAccelerator(GetStandardText(StandardButtonType::Back)).getStr()));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_set_can_default(GTK_WIDGET(m_pBack), true);
#endif
::set_buildable_id(GTK_BUILDABLE(m_pBack), "previous");
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_box_append(GTK_BOX(m_pButtonBox), GTK_WIDGET(m_pBack));
#else
gtk_box_pack_end(GTK_BOX(m_pButtonBox), GTK_WIDGET(m_pBack), false, false, 0);
#endif
m_pNext = GTK_BUTTON(gtk_button_new_with_mnemonic(MapToGtkAccelerator(GetStandardText(StandardButtonType::Next)).getStr()));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_set_can_default(GTK_WIDGET(m_pNext), true);
#endif
::set_buildable_id(GTK_BUILDABLE(m_pNext), "next");
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_box_append(GTK_BOX(m_pButtonBox), GTK_WIDGET(m_pNext));
#else
gtk_box_pack_end(GTK_BOX(m_pButtonBox), GTK_WIDGET(m_pNext), false, false, 0);
#endif
m_pCancel = GTK_BUTTON(gtk_button_new_with_mnemonic(MapToGtkAccelerator(GetStandardText(StandardButtonType::Cancel)).getStr()));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_set_can_default(GTK_WIDGET(m_pCancel), true);
#endif
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_box_append(GTK_BOX(m_pButtonBox), GTK_WIDGET(m_pCancel));
#else
gtk_box_pack_end(GTK_BOX(m_pButtonBox), GTK_WIDGET(m_pCancel), false, false, 0);
#endif
m_pFinish = GTK_BUTTON(gtk_button_new_with_mnemonic(MapToGtkAccelerator(GetStandardText(StandardButtonType::Finish)).getStr()));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_set_can_default(GTK_WIDGET(m_pFinish), true);
#endif
::set_buildable_id(GTK_BUILDABLE(m_pFinish), "finish");
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_box_append(GTK_BOX(m_pButtonBox), GTK_WIDGET(m_pFinish));
#else
gtk_box_pack_end(GTK_BOX(m_pButtonBox), GTK_WIDGET(m_pFinish), false, false, 0);
#endif
#if GTK_CHECK_VERSION(4, 0, 0)
m_pHelp = GTK_BUTTON(gtk_button_new_from_icon_name("help-browser-symbolic"));
#else
m_pHelp = GTK_BUTTON(gtk_button_new_with_mnemonic(MapToGtkAccelerator(GetStandardText(StandardButtonType::Help)).getStr()));
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_set_can_default(GTK_WIDGET(m_pHelp), true);
#endif
g_signal_connect(m_pHelp, "clicked", G_CALLBACK(signalHelpClicked), this);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_box_prepend(GTK_BOX(m_pButtonBox), GTK_WIDGET(m_pHelp));
gtk_widget_set_hexpand(GTK_WIDGET(m_pHelp), true);
gtk_widget_set_halign(GTK_WIDGET(m_pHelp), GTK_ALIGN_START);
#else
gtk_box_pack_end(GTK_BOX(m_pButtonBox), GTK_WIDGET(m_pHelp), false, false, 0);
#endif
gtk_assistant_add_action_widget(pAssistant, GTK_WIDGET(m_pButtonBox));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_button_box_set_child_secondary(m_pButtonBox, GTK_WIDGET(m_pHelp), true);
#endif
gtk_widget_set_hexpand(GTK_WIDGET(m_pButtonBox), true);
GtkWidget* pParent = gtk_widget_get_parent(GTK_WIDGET(m_pButtonBox));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_child_set(GTK_CONTAINER(pParent), GTK_WIDGET(m_pButtonBox), "expand", true, "fill", true, nullptr);
#endif
gtk_widget_set_halign(pParent, GTK_ALIGN_FILL);
// Hide the built-in ones early so we get a nice optimal size for the width without
// including the unused contents
#if GTK_CHECK_VERSION(4, 0, 0)
for (GtkWidget* pChild = gtk_widget_get_first_child(pParent);
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
gtk_widget_hide(pChild);
}
#else
GList* pChildren = gtk_container_get_children(GTK_CONTAINER(pParent));
for (GList* pChild = g_list_first(pChildren); pChild; pChild = g_list_next(pChild))
{
GtkWidget* pWidget = static_cast<GtkWidget*>(pChild->data);
gtk_widget_hide(pWidget);
}
g_list_free(pChildren);
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_show_all(GTK_WIDGET(m_pButtonBox));
#else
gtk_widget_show(GTK_WIDGET(m_pButtonBox));
#endif
find_sidebar(GTK_WIDGET(m_pAssistant), &m_pSidebar);
m_pSidebarEventBox = ::ensureEventWidget(m_pSidebar);
if (m_pSidebarEventBox)
{
#if GTK_CHECK_VERSION(4, 0, 0)
GtkGesture *pClick = gtk_gesture_click_new();
gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(pClick), 0);
m_pSidebarClickController = GTK_EVENT_CONTROLLER(pClick);
gtk_widget_add_controller(m_pSidebarEventBox, m_pSidebarClickController);
m_nButtonPressSignalId = g_signal_connect(m_pSidebarClickController, "pressed", G_CALLBACK(signalButton), this);
#else
m_nButtonPressSignalId = g_signal_connect(m_pSidebarEventBox, "button-press-event", G_CALLBACK(signalButton), this);
#endif
}
}
virtual int get_current_page() const override
{
return gtk_assistant_get_current_page(m_pAssistant);
}
virtual int get_n_pages() const override
{
return gtk_assistant_get_n_pages(m_pAssistant);
}
virtual OUString get_page_ident(int nPage) const override
{
const GtkWidget* pWidget = gtk_assistant_get_nth_page(m_pAssistant, nPage);
return ::get_buildable_id(GTK_BUILDABLE(pWidget));
}
virtual OUString get_current_page_ident() const override
{
return get_page_ident(get_current_page());
}
virtual void set_current_page(int nPage) override
{
OString sDialogTitle(gtk_window_get_title(GTK_WINDOW(m_pAssistant)));
gtk_assistant_set_current_page(m_pAssistant, nPage);
// if the page doesn't have a title, then the dialog will now have no
// title, so restore the original title as a fallback
GtkWidget* pPage = gtk_assistant_get_nth_page(m_pAssistant, nPage);
if (!gtk_assistant_get_page_title(m_pAssistant, pPage))
gtk_window_set_title(GTK_WINDOW(m_pAssistant), sDialogTitle.getStr());
}
virtual void set_current_page(const OUString& rIdent) override
{
int nPage = find_page(rIdent);
if (nPage == -1)
return;
set_current_page(nPage);
}
virtual void set_page_title(const OUString& rIdent, const OUString& rTitle) override
{
int nIndex = find_page(rIdent);
if (nIndex == -1)
return;
GtkWidget* pPage = gtk_assistant_get_nth_page(m_pAssistant, nIndex);
gtk_assistant_set_page_title(m_pAssistant, pPage,
OUStringToOString(rTitle, RTL_TEXTENCODING_UTF8).getStr());
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_forall(GTK_CONTAINER(m_pSidebar), wrap_sidebar_label, nullptr);
#endif
}
virtual OUString get_page_title(const OUString& rIdent) const override
{
int nIndex = find_page(rIdent);
if (nIndex == -1)
return OUString();
GtkWidget* pPage = gtk_assistant_get_nth_page(m_pAssistant, nIndex);
const gchar* pStr = gtk_assistant_get_page_title(m_pAssistant, pPage);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
virtual void set_page_sensitive(const OUString& rIdent, bool bSensitive) override
{
m_aNotClickable[rIdent] = !bSensitive;
}
virtual void set_page_index(const OUString& rIdent, int nNewIndex) override
{
int nOldIndex = find_page(rIdent);
if (nOldIndex == -1)
return;
if (nOldIndex == nNewIndex)
return;
GtkWidget* pPage = gtk_assistant_get_nth_page(m_pAssistant, nOldIndex);
g_object_ref(pPage);
std::optional<OString> sTitle;
if (auto const title = gtk_assistant_get_page_title(m_pAssistant, pPage)) {
sTitle = title;
}
gtk_assistant_remove_page(m_pAssistant, nOldIndex);
gtk_assistant_insert_page(m_pAssistant, pPage, nNewIndex);
gtk_assistant_set_page_type(m_pAssistant, pPage, GTK_ASSISTANT_PAGE_CUSTOM);
gtk_assistant_set_page_title(m_pAssistant, pPage, sTitle ? sTitle->getStr() : nullptr);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_forall(GTK_CONTAINER(m_pSidebar), wrap_sidebar_label, nullptr);
#endif
g_object_unref(pPage);
}
virtual weld::Container* append_page(const OUString& rIdent) override
{
disable_notify_events();
GtkWidget *pChild = gtk_grid_new();
::set_buildable_id(GTK_BUILDABLE(pChild), rIdent);
gtk_assistant_append_page(m_pAssistant, pChild);
gtk_assistant_set_page_type(m_pAssistant, pChild, GTK_ASSISTANT_PAGE_CUSTOM);
gtk_widget_show(pChild);
enable_notify_events();
#if !GTK_CHECK_VERSION(4, 0, 0)
m_aPages.emplace_back(new GtkInstanceContainer(GTK_CONTAINER(pChild), m_pBuilder, false));
#else
m_aPages.emplace_back(new GtkInstanceContainer(pChild, m_pBuilder, false));
#endif
return m_aPages.back().get();
}
virtual void set_page_side_help_id(const OUString& rHelpId) override
{
if (!m_pSidebar)
return;
::set_help_id(m_pSidebar, rHelpId);
}
virtual GtkButton* get_widget_for_response(int nGtkResponse) override
{
GtkButton* pButton = nullptr;
if (nGtkResponse == GTK_RESPONSE_YES)
pButton = m_pNext;
else if (nGtkResponse == GTK_RESPONSE_NO)
pButton = m_pBack;
else if (nGtkResponse == GTK_RESPONSE_OK)
pButton = m_pFinish;
else if (nGtkResponse == GTK_RESPONSE_CANCEL)
pButton = m_pCancel;
else if (nGtkResponse == GTK_RESPONSE_HELP)
pButton = m_pHelp;
return pButton;
}
virtual void set_page_side_image(const OUString& /*rImage*/) override
{
// Since GTK+ 3.2, sidebar images are not shown anymore
}
virtual ~GtkInstanceAssistant() override
{
if (m_nButtonPressSignalId)
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pSidebarClickController, m_nButtonPressSignalId);
#else
g_signal_handler_disconnect(m_pSidebarEventBox, m_nButtonPressSignalId);
#endif
}
}
};
class GtkInstanceFrame : public GtkInstanceContainer, public virtual weld::Frame
{
private:
GtkFrame* m_pFrame;
public:
GtkInstanceFrame(GtkFrame* pFrame, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
#if !GTK_CHECK_VERSION(4, 0, 0)
: GtkInstanceContainer(GTK_CONTAINER(pFrame), pBuilder, bTakeOwnership)
#else
: GtkInstanceContainer(GTK_WIDGET(pFrame), pBuilder, bTakeOwnership)
#endif
, m_pFrame(pFrame)
{
}
virtual void set_label(const OUString& rText) override
{
gtk_label_set_label(GTK_LABEL(gtk_frame_get_label_widget(m_pFrame)), rText.replaceFirst("~", "").toUtf8().getStr());
}
virtual OUString get_label() const override
{
const gchar* pStr = gtk_frame_get_label(m_pFrame);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
virtual std::unique_ptr<weld::Label> weld_label_widget() const override;
};
class GtkInstancePaned : public GtkInstanceContainer, public virtual weld::Paned
{
private:
GtkPaned* m_pPaned;
public:
GtkInstancePaned(GtkPaned* pPaned, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
#if !GTK_CHECK_VERSION(4, 0, 0)
: GtkInstanceContainer(GTK_CONTAINER(pPaned), pBuilder, bTakeOwnership)
#else
: GtkInstanceContainer(GTK_WIDGET(pPaned), pBuilder, bTakeOwnership)
#endif
, m_pPaned(pPaned)
{
}
virtual void set_position(int nPos) override
{
gtk_paned_set_position(m_pPaned, nPos);
}
virtual int get_position() const override
{
return gtk_paned_get_position(m_pPaned);
}
};
}
static GType immobilized_viewport_get_type();
static gpointer immobilized_viewport_parent_class;
#ifndef NDEBUG
# define IMMOBILIZED_TYPE_VIEWPORT (immobilized_viewport_get_type())
# define IMMOBILIZED_IS_VIEWPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), IMMOBILIZED_TYPE_VIEWPORT))
#endif
namespace {
struct ImmobilizedViewportPrivate
{
GtkAdjustment *hadjustment;
GtkAdjustment *vadjustment;
};
}
#define IMMOBILIZED_VIEWPORT_PRIVATE_DATA "ImmobilizedViewportPrivateData"
enum
{
PROP_0,
PROP_HADJUSTMENT,
PROP_VADJUSTMENT,
PROP_HSCROLL_POLICY,
PROP_VSCROLL_POLICY,
PROP_SHADOW_TYPE
};
static void viewport_set_adjustment(GtkViewport *viewport,
GtkOrientation orientation,
GtkAdjustment *adjustment)
{
ImmobilizedViewportPrivate* priv =
static_cast<ImmobilizedViewportPrivate*>(g_object_get_data(G_OBJECT(viewport),
IMMOBILIZED_VIEWPORT_PRIVATE_DATA));
if (!adjustment)
adjustment = gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
if (priv->hadjustment)
g_object_unref(priv->hadjustment);
priv->hadjustment = adjustment;
}
else
{
if (priv->vadjustment)
g_object_unref(priv->vadjustment);
priv->vadjustment = adjustment;
}
g_object_ref_sink(adjustment);
}
static void
immobilized_viewport_set_property(GObject* object,
guint prop_id,
const GValue* value,
GParamSpec* /*pspec*/)
{
GtkViewport *viewport = GTK_VIEWPORT(object);
switch (prop_id)
{
case PROP_HADJUSTMENT:
viewport_set_adjustment(viewport, GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT(g_value_get_object(value)));
break;
case PROP_VADJUSTMENT:
viewport_set_adjustment(viewport, GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT(g_value_get_object(value)));
break;
case PROP_HSCROLL_POLICY:
case PROP_VSCROLL_POLICY:
break;
default:
SAL_WARN( "vcl.gtk", "unknown property\n");
break;
}
}
static void
immobilized_viewport_get_property(GObject* object,
guint prop_id,
GValue* value,
GParamSpec* /*pspec*/)
{
ImmobilizedViewportPrivate* priv =
static_cast<ImmobilizedViewportPrivate*>(g_object_get_data(object,
IMMOBILIZED_VIEWPORT_PRIVATE_DATA));
switch (prop_id)
{
case PROP_HADJUSTMENT:
g_value_set_object(value, priv->hadjustment);
break;
case PROP_VADJUSTMENT:
g_value_set_object(value, priv->vadjustment);
break;
case PROP_HSCROLL_POLICY:
g_value_set_enum(value, GTK_SCROLL_MINIMUM);
break;
case PROP_VSCROLL_POLICY:
g_value_set_enum(value, GTK_SCROLL_MINIMUM);
break;
default:
SAL_WARN( "vcl.gtk", "unknown property\n");
break;
}
}
static ImmobilizedViewportPrivate*
immobilized_viewport_new_private_data()
{
ImmobilizedViewportPrivate* priv = g_slice_new0(ImmobilizedViewportPrivate);
priv->hadjustment = nullptr;
priv->vadjustment = nullptr;
return priv;
}
static void
immobilized_viewport_instance_init(GTypeInstance *instance, gpointer /*klass*/)
{
GObject* object = G_OBJECT(instance);
g_object_set_data(object, IMMOBILIZED_VIEWPORT_PRIVATE_DATA,
immobilized_viewport_new_private_data());
}
static void
immobilized_viewport_finalize(GObject* object)
{
void* priv = g_object_get_data(object, IMMOBILIZED_VIEWPORT_PRIVATE_DATA);
if (priv)
{
g_slice_free(ImmobilizedViewportPrivate, priv);
g_object_set_data(object, IMMOBILIZED_VIEWPORT_PRIVATE_DATA, nullptr);
}
G_OBJECT_CLASS(immobilized_viewport_parent_class)->finalize(object);
}
static void immobilized_viewport_class_init(GtkWidgetClass* klass)
{
immobilized_viewport_parent_class = g_type_class_peek_parent(klass);
GObjectClass* o_class = G_OBJECT_CLASS(klass);
/* GObject signals */
o_class->finalize = immobilized_viewport_finalize;
o_class->set_property = immobilized_viewport_set_property;
o_class->get_property = immobilized_viewport_get_property;
/* Properties */
g_object_class_override_property(o_class, PROP_HADJUSTMENT, "hadjustment");
g_object_class_override_property(o_class, PROP_VADJUSTMENT, "vadjustment");
g_object_class_override_property(o_class, PROP_HSCROLL_POLICY, "hscroll-policy");
g_object_class_override_property(o_class, PROP_VSCROLL_POLICY, "vscroll-policy");
}
GType immobilized_viewport_get_type()
{
static GType type = 0;
if (!type)
{
GTypeQuery query;
g_type_query(gtk_viewport_get_type(), &query);
static const GTypeInfo tinfo =
{
static_cast<guint16>(query.class_size),
nullptr, /* base init */
nullptr, /* base finalize */
reinterpret_cast<GClassInitFunc>(immobilized_viewport_class_init), /* class init */
nullptr, /* class finalize */
nullptr, /* class data */
static_cast<guint16>(query.instance_size), /* instance size */
0, /* nb preallocs */
immobilized_viewport_instance_init, /* instance init */
nullptr /* value table */
};
type = g_type_register_static(GTK_TYPE_VIEWPORT, "ImmobilizedViewport",
&tinfo, GTypeFlags(0));
}
return type;
}
static VclPolicyType GtkToVcl(GtkPolicyType eType)
{
VclPolicyType eRet(VclPolicyType::NEVER);
switch (eType)
{
case GTK_POLICY_ALWAYS:
eRet = VclPolicyType::ALWAYS;
break;
case GTK_POLICY_AUTOMATIC:
eRet = VclPolicyType::AUTOMATIC;
break;
case GTK_POLICY_EXTERNAL:
case GTK_POLICY_NEVER:
eRet = VclPolicyType::NEVER;
break;
}
return eRet;
}
static GtkPolicyType VclToGtk(VclPolicyType eType)
{
GtkPolicyType eRet(GTK_POLICY_ALWAYS);
switch (eType)
{
case VclPolicyType::ALWAYS:
eRet = GTK_POLICY_ALWAYS;
break;
case VclPolicyType::AUTOMATIC:
eRet = GTK_POLICY_AUTOMATIC;
break;
case VclPolicyType::NEVER:
eRet = GTK_POLICY_NEVER;
break;
}
return eRet;
}
static GtkMessageType VclToGtk(VclMessageType eType)
{
GtkMessageType eRet(GTK_MESSAGE_INFO);
switch (eType)
{
case VclMessageType::Info:
eRet = GTK_MESSAGE_INFO;
break;
case VclMessageType::Warning:
eRet = GTK_MESSAGE_WARNING;
break;
case VclMessageType::Question:
eRet = GTK_MESSAGE_QUESTION;
break;
case VclMessageType::Error:
eRet = GTK_MESSAGE_ERROR;
break;
case VclMessageType::Other:
eRet = GTK_MESSAGE_OTHER;
break;
}
return eRet;
}
static GtkButtonsType VclToGtk(VclButtonsType eType)
{
GtkButtonsType eRet(GTK_BUTTONS_NONE);
switch (eType)
{
case VclButtonsType::NONE:
eRet = GTK_BUTTONS_NONE;
break;
case VclButtonsType::Ok:
eRet = GTK_BUTTONS_OK;
break;
case VclButtonsType::Close:
eRet = GTK_BUTTONS_CLOSE;
break;
case VclButtonsType::Cancel:
eRet = GTK_BUTTONS_CANCEL;
break;
case VclButtonsType::YesNo:
eRet = GTK_BUTTONS_YES_NO;
break;
case VclButtonsType::OkCancel:
eRet = GTK_BUTTONS_OK_CANCEL;
break;
}
return eRet;
}
static GtkSelectionMode VclToGtk(SelectionMode eType)
{
GtkSelectionMode eRet(GTK_SELECTION_NONE);
switch (eType)
{
case SelectionMode::NONE:
eRet = GTK_SELECTION_NONE;
break;
case SelectionMode::Single:
eRet = GTK_SELECTION_SINGLE;
break;
case SelectionMode::Range:
eRet = GTK_SELECTION_BROWSE;
break;
case SelectionMode::Multiple:
eRet = GTK_SELECTION_MULTIPLE;
break;
}
return eRet;
}
namespace {
class GtkInstanceScrolledWindow final : public GtkInstanceContainer, public virtual weld::ScrolledWindow
{
private:
GtkScrolledWindow* m_pScrolledWindow;
GtkWidget *m_pOrigViewport;
GtkCssProvider* m_pScrollBarCssProvider;
GtkAdjustment* m_pVAdjustment;
GtkAdjustment* m_pHAdjustment;
gulong m_nVAdjustChangedSignalId;
gulong m_nHAdjustChangedSignalId;
static void signalVAdjustValueChanged(GtkAdjustment*, gpointer widget)
{
GtkInstanceScrolledWindow* pThis = static_cast<GtkInstanceScrolledWindow*>(widget);
SolarMutexGuard aGuard;
pThis->signal_vadjustment_changed();
}
static void signalHAdjustValueChanged(GtkAdjustment*, gpointer widget)
{
GtkInstanceScrolledWindow* pThis = static_cast<GtkInstanceScrolledWindow*>(widget);
SolarMutexGuard aGuard;
pThis->signal_hadjustment_changed();
}
public:
GtkInstanceScrolledWindow(GtkScrolledWindow* pScrolledWindow, GtkInstanceBuilder* pBuilder, bool bTakeOwnership, bool bUserManagedScrolling)
#if !GTK_CHECK_VERSION(4, 0, 0)
: GtkInstanceContainer(GTK_CONTAINER(pScrolledWindow), pBuilder, bTakeOwnership)
#else
: GtkInstanceContainer(GTK_WIDGET(pScrolledWindow), pBuilder, bTakeOwnership)
#endif
, m_pScrolledWindow(pScrolledWindow)
, m_pOrigViewport(nullptr)
, m_pScrollBarCssProvider(nullptr)
, m_pVAdjustment(gtk_scrolled_window_get_vadjustment(m_pScrolledWindow))
, m_pHAdjustment(gtk_scrolled_window_get_hadjustment(m_pScrolledWindow))
, m_nVAdjustChangedSignalId(g_signal_connect(m_pVAdjustment, "value-changed", G_CALLBACK(signalVAdjustValueChanged), this))
, m_nHAdjustChangedSignalId(g_signal_connect(m_pHAdjustment, "value-changed", G_CALLBACK(signalHAdjustValueChanged), this))
{
if (bUserManagedScrolling)
set_user_managed_scrolling();
}
void set_user_managed_scrolling()
{
disable_notify_events();
//remove the original viewport and replace it with our bodged one which
//doesn't do any scrolling and expects its child to figure it out somehow
assert(!m_pOrigViewport);
#if GTK_CHECK_VERSION(4, 0, 0)
GtkWidget *pViewport = gtk_scrolled_window_get_child(m_pScrolledWindow);
#else
GtkWidget *pViewport = gtk_bin_get_child(GTK_BIN(m_pScrolledWindow));
#endif
assert(GTK_IS_VIEWPORT(pViewport));
#if GTK_CHECK_VERSION(4, 0, 0)
GtkWidget *pChild= gtk_viewport_get_child(GTK_VIEWPORT(pViewport));
#else
GtkWidget *pChild = gtk_bin_get_child(GTK_BIN(pViewport));
#endif
g_object_ref(pChild);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_viewport_set_child(GTK_VIEWPORT(pViewport), nullptr);
#else
gtk_container_remove(GTK_CONTAINER(pViewport), pChild);
#endif
g_object_ref(pViewport);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_scrolled_window_set_child(m_pScrolledWindow, nullptr);
#else
gtk_container_remove(GTK_CONTAINER(m_pScrolledWindow), pViewport);
#endif
GtkWidget* pNewViewport = GTK_WIDGET(g_object_new(immobilized_viewport_get_type(), nullptr));
gtk_widget_show(pNewViewport);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_scrolled_window_set_child(m_pScrolledWindow, pNewViewport);
gtk_viewport_set_child(GTK_VIEWPORT(pNewViewport), pChild);
#else
gtk_container_add(GTK_CONTAINER(m_pScrolledWindow), pNewViewport);
gtk_container_add(GTK_CONTAINER(pNewViewport), pChild);
#endif
g_object_unref(pChild);
m_pOrigViewport = pViewport;
enable_notify_events();
}
virtual void hadjustment_configure(int value, int lower, int upper,
int step_increment, int page_increment,
int page_size) override
{
disable_notify_events();
if (SwapForRTL())
value = upper - (value - lower + page_size);
gtk_adjustment_configure(m_pHAdjustment, value, lower, upper, step_increment, page_increment, page_size);
enable_notify_events();
}
virtual int hadjustment_get_value() const override
{
int value = gtk_adjustment_get_value(m_pHAdjustment);
if (SwapForRTL())
{
int upper = gtk_adjustment_get_upper(m_pHAdjustment);
int lower = gtk_adjustment_get_lower(m_pHAdjustment);
int page_size = gtk_adjustment_get_page_size(m_pHAdjustment);
value = lower + (upper - value - page_size);
}
return value;
}
virtual void hadjustment_set_value(int value) override
{
disable_notify_events();
if (SwapForRTL())
{
int upper = gtk_adjustment_get_upper(m_pHAdjustment);
int lower = gtk_adjustment_get_lower(m_pHAdjustment);
int page_size = gtk_adjustment_get_page_size(m_pHAdjustment);
value = upper - (value - lower + page_size);
}
gtk_adjustment_set_value(m_pHAdjustment, value);
enable_notify_events();
}
virtual int hadjustment_get_upper() const override
{
return gtk_adjustment_get_upper(m_pHAdjustment);
}
virtual void hadjustment_set_upper(int upper) override
{
disable_notify_events();
gtk_adjustment_set_upper(m_pHAdjustment, upper);
enable_notify_events();
}
virtual int hadjustment_get_page_size() const override
{
return gtk_adjustment_get_page_size(m_pHAdjustment);
}
virtual void hadjustment_set_page_size(int size) override
{
gtk_adjustment_set_page_size(m_pHAdjustment, size);
}
virtual void hadjustment_set_page_increment(int size) override
{
gtk_adjustment_set_page_increment(m_pHAdjustment, size);
}
virtual void hadjustment_set_step_increment(int size) override
{
gtk_adjustment_set_step_increment(m_pHAdjustment, size);
}
virtual void set_hpolicy(VclPolicyType eHPolicy) override
{
GtkPolicyType eGtkVPolicy;
gtk_scrolled_window_get_policy(m_pScrolledWindow, nullptr, &eGtkVPolicy);
gtk_scrolled_window_set_policy(m_pScrolledWindow, VclToGtk(eHPolicy), eGtkVPolicy);
}
virtual VclPolicyType get_hpolicy() const override
{
GtkPolicyType eGtkHPolicy;
gtk_scrolled_window_get_policy(m_pScrolledWindow, &eGtkHPolicy, nullptr);
return GtkToVcl(eGtkHPolicy);
}
virtual void vadjustment_configure(int value, int lower, int upper,
int step_increment, int page_increment,
int page_size) override
{
disable_notify_events();
gtk_adjustment_configure(m_pVAdjustment, value, lower, upper, step_increment, page_increment, page_size);
enable_notify_events();
}
virtual int vadjustment_get_value() const override
{
return gtk_adjustment_get_value(m_pVAdjustment);
}
virtual void vadjustment_set_value(int value) override
{
disable_notify_events();
gtk_adjustment_set_value(m_pVAdjustment, value);
enable_notify_events();
}
virtual int vadjustment_get_upper() const override
{
return gtk_adjustment_get_upper(m_pVAdjustment);
}
virtual void vadjustment_set_upper(int upper) override
{
disable_notify_events();
gtk_adjustment_set_upper(m_pVAdjustment, upper);
enable_notify_events();
}
virtual int vadjustment_get_lower() const override
{
return gtk_adjustment_get_lower(m_pVAdjustment);
}
virtual void vadjustment_set_lower(int lower) override
{
disable_notify_events();
gtk_adjustment_set_lower(m_pVAdjustment, lower);
enable_notify_events();
}
virtual int vadjustment_get_page_size() const override
{
return gtk_adjustment_get_page_size(m_pVAdjustment);
}
virtual void vadjustment_set_page_size(int size) override
{
gtk_adjustment_set_page_size(m_pVAdjustment, size);
}
virtual void vadjustment_set_page_increment(int size) override
{
gtk_adjustment_set_page_increment(m_pVAdjustment, size);
}
virtual void vadjustment_set_step_increment(int size) override
{
gtk_adjustment_set_step_increment(m_pVAdjustment, size);
}
virtual void set_vpolicy(VclPolicyType eVPolicy) override
{
GtkPolicyType eGtkHPolicy;
gtk_scrolled_window_get_policy(m_pScrolledWindow, &eGtkHPolicy, nullptr);
gtk_scrolled_window_set_policy(m_pScrolledWindow, eGtkHPolicy, VclToGtk(eVPolicy));
}
virtual VclPolicyType get_vpolicy() const override
{
GtkPolicyType eGtkVPolicy;
gtk_scrolled_window_get_policy(m_pScrolledWindow, nullptr, &eGtkVPolicy);
return GtkToVcl(eGtkVPolicy);
}
virtual int get_scroll_thickness() const override
{
if (gtk_scrolled_window_get_overlay_scrolling(m_pScrolledWindow))
return 0;
return gtk_widget_get_allocated_width(gtk_scrolled_window_get_vscrollbar(m_pScrolledWindow));
}
virtual void set_scroll_thickness(int nThickness) override
{
GtkWidget *pHorzBar = gtk_scrolled_window_get_hscrollbar(m_pScrolledWindow);
GtkWidget *pVertBar = gtk_scrolled_window_get_vscrollbar(m_pScrolledWindow);
gtk_widget_set_size_request(pHorzBar, -1, nThickness);
gtk_widget_set_size_request(pVertBar, nThickness, -1);
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pVAdjustment, m_nVAdjustChangedSignalId);
g_signal_handler_block(m_pHAdjustment, m_nHAdjustChangedSignalId);
GtkInstanceContainer::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceContainer::enable_notify_events();
g_signal_handler_unblock(m_pVAdjustment, m_nVAdjustChangedSignalId);
g_signal_handler_unblock(m_pHAdjustment, m_nHAdjustChangedSignalId);
}
virtual void customize_scrollbars(const Color& rBackgroundColor,
const Color& rShadowColor,
const Color& rFaceColor) override
{
GtkWidget *pHorzBar = gtk_scrolled_window_get_hscrollbar(m_pScrolledWindow);
GtkWidget *pVertBar = gtk_scrolled_window_get_vscrollbar(m_pScrolledWindow);
GtkStyleContext *pHorzContext = gtk_widget_get_style_context(pHorzBar);
GtkStyleContext *pVertContext = gtk_widget_get_style_context(pVertBar);
if (m_pScrollBarCssProvider)
{
gtk_style_context_remove_provider(pHorzContext, GTK_STYLE_PROVIDER(m_pScrollBarCssProvider));
gtk_style_context_remove_provider(pVertContext, GTK_STYLE_PROVIDER(m_pScrollBarCssProvider));
}
m_pScrollBarCssProvider = gtk_css_provider_new();
// intentionally 'trough' a long, narrow open container.
OUString aBuffer = "scrollbar contents trough { background-color: #" + rBackgroundColor.AsRGBHexString() + "; } "
"scrollbar contents trough slider { background-color: #" + rShadowColor.AsRGBHexString() + "; } "
"scrollbar contents button { background-color: #" + rFaceColor.AsRGBHexString() + "; } "
"scrollbar contents button { color: #000000; } "
"scrollbar contents button:disabled { color: #7f7f7f; }";
OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8);
css_provider_load_from_data(m_pScrollBarCssProvider, aResult.getStr(), aResult.getLength());
gtk_style_context_add_provider(pHorzContext, GTK_STYLE_PROVIDER(m_pScrollBarCssProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_style_context_add_provider(pVertContext, GTK_STYLE_PROVIDER(m_pScrollBarCssProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
virtual ~GtkInstanceScrolledWindow() override
{
// we use GtkInstanceContainer::[disable|enable]_notify_events later on
// to avoid touching these removed handlers
g_signal_handler_disconnect(m_pVAdjustment, m_nVAdjustChangedSignalId);
g_signal_handler_disconnect(m_pHAdjustment, m_nHAdjustChangedSignalId);
if (m_pScrollBarCssProvider)
{
GtkStyleContext *pHorzContext = gtk_widget_get_style_context(gtk_scrolled_window_get_hscrollbar(m_pScrolledWindow));
GtkStyleContext *pVertContext = gtk_widget_get_style_context(gtk_scrolled_window_get_vscrollbar(m_pScrolledWindow));
gtk_style_context_remove_provider(pHorzContext, GTK_STYLE_PROVIDER(m_pScrollBarCssProvider));
gtk_style_context_remove_provider(pVertContext, GTK_STYLE_PROVIDER(m_pScrollBarCssProvider));
m_pScrollBarCssProvider = nullptr;
}
//put it back the way it was
if (!m_pOrigViewport)
return;
GtkInstanceContainer::disable_notify_events();
// force in new adjustment to drop the built-in handlers on value-changed
// which are getting called eventually by the gtk_container_add call
// and which access the scrolled window indicators which, in the case
// of user-managed scrolling windows in toolbar popups during popdown
// are nullptr causing crashes when the scrolling windows is not at its
// initial 0,0 position
GtkAdjustment *pVAdjustment = gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
gtk_scrolled_window_set_vadjustment(m_pScrolledWindow, pVAdjustment);
GtkAdjustment *pHAdjustment = gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
gtk_scrolled_window_set_hadjustment(m_pScrolledWindow, pHAdjustment);
#if GTK_CHECK_VERSION(4, 0, 0)
GtkWidget *pViewport = gtk_scrolled_window_get_child(m_pScrolledWindow);
#else
GtkWidget *pViewport = gtk_bin_get_child(GTK_BIN(m_pScrolledWindow));
#endif
assert(IMMOBILIZED_IS_VIEWPORT(pViewport));
#if GTK_CHECK_VERSION(4, 0, 0)
GtkWidget *pChild= gtk_viewport_get_child(GTK_VIEWPORT(pViewport));
#else
GtkWidget *pChild = gtk_bin_get_child(GTK_BIN(pViewport));
#endif
g_object_ref(pChild);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_viewport_set_child(GTK_VIEWPORT(pViewport), nullptr);
#else
gtk_container_remove(GTK_CONTAINER(pViewport), pChild);
#endif
g_object_ref(pViewport);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_scrolled_window_set_child(m_pScrolledWindow, nullptr);
#else
gtk_container_remove(GTK_CONTAINER(m_pScrolledWindow), pViewport);
#endif
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_scrolled_window_set_child(m_pScrolledWindow, m_pOrigViewport);
#else
gtk_container_add(GTK_CONTAINER(m_pScrolledWindow), m_pOrigViewport);
#endif
// coverity[freed_arg : FALSE] - this does not free m_pOrigViewport, it is reffed by m_pScrolledWindow
g_object_unref(m_pOrigViewport);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_viewport_set_child(GTK_VIEWPORT(m_pOrigViewport), pChild);
#else
gtk_container_add(GTK_CONTAINER(m_pOrigViewport), pChild);
#endif
g_object_unref(pChild);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_destroy(pViewport);
#endif
g_object_unref(pViewport);
m_pOrigViewport = nullptr;
GtkInstanceContainer::enable_notify_events();
}
};
class GtkInstanceScrollbar final : public GtkInstanceWidget, public virtual weld::Scrollbar
{
private:
GtkScrollbar* m_pScrollbar;
GtkAdjustment* m_pAdjustment;
GtkCssProvider* m_pThicknessCssProvider;
gulong m_nAdjustChangedSignalId;
static void signalAdjustValueChanged(GtkAdjustment*, gpointer widget)
{
GtkInstanceScrollbar* pThis = static_cast<GtkInstanceScrollbar*>(widget);
SolarMutexGuard aGuard;
pThis->signal_adjustment_changed();
}
#if GTK_CHECK_VERSION(4, 0, 0)
// if the widget is inside a GtkSalFrame then ensure the event is processed by the GtkSalFrame and not the
// GtkScrollbar
static gboolean signalScroll(GtkEventControllerScroll* pController, double delta_x, double delta_y, gpointer widget)
{
GtkInstanceScrollbar* pThis = static_cast<GtkInstanceScrollbar*>(widget);
GtkWidget* pParent = widget_get_toplevel(GTK_WIDGET(pThis->m_pScrollbar));
GtkSalFrame* pFrame = pParent ? GtkSalFrame::getFromWindow(pParent) : nullptr;
return pFrame && pFrame->event_controller_scroll_forward(pController, delta_x, delta_y);
}
#else
static gboolean signalScroll(GtkWidget* pWidget, GdkEventScroll* /*pEvent*/, gpointer widget)
{
GtkInstanceScrollbar* pThis = static_cast<GtkInstanceScrollbar*>(widget);
GtkWidget* pParent = widget_get_toplevel(GTK_WIDGET(pThis->m_pScrollbar));
GtkSalFrame* pFrame = pParent ? GtkSalFrame::getFromWindow(pParent) : nullptr;
if (pFrame)
g_signal_stop_emission_by_name(pWidget, "scroll-event");
return false;
}
#endif
public:
GtkInstanceScrollbar(GtkScrollbar* pScrollbar, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pScrollbar), pBuilder, bTakeOwnership)
, m_pScrollbar(pScrollbar)
#if GTK_CHECK_VERSION(4, 0, 0)
, m_pAdjustment(gtk_scrollbar_get_adjustment(m_pScrollbar))
#else
, m_pAdjustment(gtk_range_get_adjustment(GTK_RANGE(m_pScrollbar)))
#endif
, m_pThicknessCssProvider(nullptr)
, m_nAdjustChangedSignalId(g_signal_connect(m_pAdjustment, "value-changed", G_CALLBACK(signalAdjustValueChanged), this))
{
#if GTK_CHECK_VERSION(4, 0, 0)
GtkEventController* pScrollController = gtk_event_controller_scroll_new(GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES);
gtk_event_controller_set_propagation_phase(pScrollController, GTK_PHASE_CAPTURE);
g_signal_connect(pScrollController, "scroll", G_CALLBACK(signalScroll), this);
gtk_widget_add_controller(GTK_WIDGET(pScrollbar), pScrollController);
#else
g_signal_connect(pScrollbar, "scroll-event", G_CALLBACK(signalScroll), this);
#endif
}
virtual void adjustment_configure(int value, int lower, int upper,
int step_increment, int page_increment,
int page_size) override
{
disable_notify_events();
gtk_adjustment_configure(m_pAdjustment, value, lower, upper, step_increment, page_increment, page_size);
enable_notify_events();
}
virtual int adjustment_get_value() const override
{
return gtk_adjustment_get_value(m_pAdjustment);
}
virtual void adjustment_set_value(int value) override
{
disable_notify_events();
gtk_adjustment_set_value(m_pAdjustment, value);
enable_notify_events();
}
virtual int adjustment_get_upper() const override
{
return gtk_adjustment_get_upper(m_pAdjustment);
}
virtual void adjustment_set_upper(int upper) override
{
disable_notify_events();
gtk_adjustment_set_upper(m_pAdjustment, upper);
enable_notify_events();
}
virtual int adjustment_get_lower() const override
{
return gtk_adjustment_get_lower(m_pAdjustment);
}
virtual void adjustment_set_lower(int lower) override
{
disable_notify_events();
gtk_adjustment_set_lower(m_pAdjustment, lower);
enable_notify_events();
}
virtual int adjustment_get_page_size() const override
{
return gtk_adjustment_get_page_size(m_pAdjustment);
}
virtual void adjustment_set_page_size(int size) override
{
gtk_adjustment_set_page_size(m_pAdjustment, size);
}
virtual int adjustment_get_page_increment() const override
{
return gtk_adjustment_get_page_increment(m_pAdjustment);
}
virtual void adjustment_set_page_increment(int size) override
{
gtk_adjustment_set_page_increment(m_pAdjustment, size);
}
virtual int adjustment_get_step_increment() const override
{
return gtk_adjustment_get_step_increment(m_pAdjustment);
}
virtual void adjustment_set_step_increment(int size) override
{
gtk_adjustment_set_step_increment(m_pAdjustment, size);
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pAdjustment, m_nAdjustChangedSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pAdjustment, m_nAdjustChangedSignalId);
}
virtual ScrollType get_scroll_type() const override
{
// tdf#153049 want a mousewheel spin to be treated as DontKnow
return has_grab() ? ScrollType::Drag : ScrollType::DontKnow;
}
virtual int get_scroll_thickness() const override
{
if (gtk_orientable_get_orientation(GTK_ORIENTABLE(m_pScrollbar)) == GTK_ORIENTATION_HORIZONTAL)
return gtk_widget_get_allocated_height(GTK_WIDGET(m_pScrollbar));
return gtk_widget_get_allocated_width(GTK_WIDGET(m_pScrollbar));
}
virtual void set_scroll_thickness(int nThickness) override
{
GtkStyleContext *pStyleContext = gtk_widget_get_style_context(GTK_WIDGET(m_pScrollbar));
if (m_pThicknessCssProvider)
{
gtk_style_context_remove_provider(pStyleContext, GTK_STYLE_PROVIDER(m_pThicknessCssProvider));
m_pThicknessCssProvider = nullptr;
}
m_pThicknessCssProvider = gtk_css_provider_new();
int nSlider = nThickness > 6 ? nThickness - 6 : 1;
const OString sData = "slider { min-height: " + OString::number(nSlider) + "px;"
" min-width: " + OString::number(nSlider) + "px; }";
css_provider_load_from_data(m_pThicknessCssProvider, sData.getStr(), sData.getLength());
gtk_style_context_add_provider(pStyleContext, GTK_STYLE_PROVIDER(m_pThicknessCssProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
if (gtk_orientable_get_orientation(GTK_ORIENTABLE(m_pScrollbar)) == GTK_ORIENTATION_HORIZONTAL)
gtk_widget_set_size_request(GTK_WIDGET(m_pScrollbar), -1, nThickness);
else
gtk_widget_set_size_request(GTK_WIDGET(m_pScrollbar), nThickness, -1);
}
virtual ~GtkInstanceScrollbar() override
{
g_signal_handler_disconnect(m_pAdjustment, m_nAdjustChangedSignalId);
if (m_pThicknessCssProvider)
{
GtkStyleContext *pStyleContext = gtk_widget_get_style_context(GTK_WIDGET(m_pScrollbar));
gtk_style_context_remove_provider(pStyleContext, GTK_STYLE_PROVIDER(m_pThicknessCssProvider));
}
}
};
}
namespace {
class GtkInstanceNotebook : public GtkInstanceWidget, public virtual weld::Notebook
{
private:
GtkNotebook* m_pNotebook;
GtkBox* m_pOverFlowBox;
GtkNotebook* m_pOverFlowNotebook;
gulong m_nSwitchPageSignalId;
gulong m_nOverFlowSwitchPageSignalId;
#if GTK_CHECK_VERSION(4, 0, 0)
NotifyingLayout* m_pLayout;
#else
gulong m_nNotebookSizeAllocateSignalId;
gulong m_nFocusSignalId;
#endif
gulong m_nChangeCurrentPageId;
guint m_nLaunchSplitTimeoutId;
bool m_bOverFlowBoxActive;
bool m_bOverFlowBoxIsStart;
bool m_bInternalPageChange;
int m_nStartTabCount;
int m_nEndTabCount;
mutable std::vector<std::unique_ptr<GtkInstanceContainer>> m_aPages;
static void signalSwitchPage(GtkNotebook*, GtkWidget*, guint nNewPage, gpointer widget)
{
GtkInstanceNotebook* pThis = static_cast<GtkInstanceNotebook*>(widget);
SolarMutexGuard aGuard;
pThis->signal_switch_page(nNewPage);
}
static gboolean launch_overflow_switch_page(GtkInstanceNotebook* pThis)
{
SolarMutexGuard aGuard;
pThis->signal_overflow_switch_page();
return false;
}
static void signalOverFlowSwitchPage(GtkNotebook*, GtkWidget*, guint, gpointer widget)
{
g_timeout_add_full(G_PRIORITY_HIGH_IDLE, 0, reinterpret_cast<GSourceFunc>(launch_overflow_switch_page), widget, nullptr);
}
void signal_switch_page(int nNewPage)
{
if (m_bOverFlowBoxIsStart)
{
auto nOverFlowLen = m_bOverFlowBoxActive ? gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1 : 0;
// add count of overflow pages, minus the extra tab
nNewPage += nOverFlowLen;
}
bool bAllow = m_bInternalPageChange || !m_aLeavePageHdl.IsSet() || m_aLeavePageHdl.Call(get_current_page_ident());
if (!bAllow)
{
g_signal_stop_emission_by_name(m_pNotebook, "switch-page");
return;
}
if (m_bOverFlowBoxActive)
gtk_notebook_set_current_page(m_pOverFlowNotebook, gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1);
OUString sNewIdent(get_page_ident(nNewPage));
if (!m_bInternalPageChange)
m_aEnterPageHdl.Call(sNewIdent);
}
void unsplit_notebooks()
{
int nOverFlowPages = gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1;
int nMainPages = gtk_notebook_get_n_pages(m_pNotebook);
int nPageIndex = 0;
if (!m_bOverFlowBoxIsStart)
nPageIndex += nMainPages;
// take the overflow pages, and put them back at the end of the normal one
int i = nMainPages;
while (nOverFlowPages)
{
OUString sIdent(get_page_ident(m_pOverFlowNotebook, 0));
OUString sLabel(get_tab_label_text(m_pOverFlowNotebook, 0));
remove_page(m_pOverFlowNotebook, sIdent);
GtkWidget* pPage = m_aPages[nPageIndex]->getWidget();
insert_page(m_pNotebook, sIdent, sLabel, pPage, -1);
GtkWidget* pTabWidget = gtk_notebook_get_tab_label(m_pNotebook,
gtk_notebook_get_nth_page(m_pNotebook, i));
gtk_widget_set_hexpand(pTabWidget, true);
--nOverFlowPages;
++i;
++nPageIndex;
}
// remove the dangling placeholder tab page
remove_page(m_pOverFlowNotebook, u"useless");
}
// a tab has been selected on the overflow notebook
void signal_overflow_switch_page()
{
int nNewPage = gtk_notebook_get_current_page(m_pOverFlowNotebook);
int nOverFlowPages = gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1;
if (nNewPage == nOverFlowPages)
{
// the useless tab which is there because there has to be an active tab
return;
}
// check if we are allowed leave before attempting to resplit the notebooks
bool bAllow = !m_aLeavePageHdl.IsSet() || m_aLeavePageHdl.Call(get_current_page_ident());
if (!bAllow)
return;
disable_notify_events();
// take the overflow pages, and put them back at the end of the normal one
unsplit_notebooks();
// now redo the split, the pages will be split the other way around this time
std::swap(m_nStartTabCount, m_nEndTabCount);
split_notebooks();
// coverity[pass_freed_arg : FALSE] - m_pNotebook is not freed here
gtk_notebook_set_current_page(m_pNotebook, nNewPage);
enable_notify_events();
// trigger main notebook switch-page callback
OUString sNewIdent(get_page_ident(m_pNotebook, nNewPage));
m_aEnterPageHdl.Call(sNewIdent);
}
static OUString get_page_ident(GtkNotebook *pNotebook, guint nPage)
{
const GtkWidget* pTabWidget = gtk_notebook_get_tab_label(pNotebook, gtk_notebook_get_nth_page(pNotebook, nPage));
return ::get_buildable_id(GTK_BUILDABLE(pTabWidget));
}
static gint get_page_number(GtkNotebook *pNotebook, std::u16string_view ident)
{
gint nPages = gtk_notebook_get_n_pages(pNotebook);
for (gint i = 0; i < nPages; ++i)
{
const GtkWidget* pTabWidget = gtk_notebook_get_tab_label(pNotebook, gtk_notebook_get_nth_page(pNotebook, i));
OUString sBuildableName = ::get_buildable_id(GTK_BUILDABLE(pTabWidget));
if (sBuildableName == ident)
return i;
}
return -1;
}
int remove_page(GtkNotebook *pNotebook, std::u16string_view ident)
{
disable_notify_events();
int nPageNumber = get_page_number(pNotebook, ident);
assert(nPageNumber != -1 && "asked to remove page that doesn't exist");
gtk_notebook_remove_page(pNotebook, nPageNumber);
enable_notify_events();
return nPageNumber;
}
static OUString get_tab_label_text(GtkNotebook *pNotebook, guint nPage)
{
const gchar* pStr = gtk_notebook_get_tab_label_text(pNotebook, gtk_notebook_get_nth_page(pNotebook, nPage));
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
static void set_tab_label_text(GtkNotebook *pNotebook, guint nPage, const OUString& rText)
{
OString sUtf8(rText.toUtf8());
GtkWidget* pPage = gtk_notebook_get_nth_page(pNotebook, nPage);
// tdf#128241 if there's already a label here, reuse it so the buildable
// name remains the same, gtk_notebook_set_tab_label_text will replace
// the label widget with a new one
GtkWidget* pTabWidget = gtk_notebook_get_tab_label(pNotebook, pPage);
if (pTabWidget && GTK_IS_LABEL(pTabWidget))
{
gtk_label_set_label(GTK_LABEL(pTabWidget), sUtf8.getStr());
return;
}
gtk_notebook_set_tab_label_text(pNotebook, pPage, sUtf8.getStr());
}
void append_useless_page(GtkNotebook *pNotebook)
{
disable_notify_events();
GtkWidget *pTabWidget = gtk_fixed_new();
::set_buildable_id(GTK_BUILDABLE(pTabWidget), "useless");
GtkWidget *pChild = gtk_grid_new();
gtk_notebook_append_page(pNotebook, pChild, pTabWidget);
gtk_widget_show(pChild);
gtk_widget_show(pTabWidget);
enable_notify_events();
}
void insert_page(GtkNotebook *pNotebook, const OUString& rIdent, const OUString& rLabel, GtkWidget *pChild, int nPos)
{
disable_notify_events();
GtkWidget *pTabWidget = gtk_label_new_with_mnemonic(MapToGtkAccelerator(rLabel).getStr());
::set_buildable_id(GTK_BUILDABLE(pTabWidget), rIdent);
gtk_notebook_insert_page(pNotebook, pChild, pTabWidget, nPos);
gtk_widget_show(pChild);
gtk_widget_show(pTabWidget);
if (nPos != -1)
{
unsigned int nPageIndex = static_cast<unsigned int>(nPos);
if (nPageIndex < m_aPages.size())
m_aPages.insert(m_aPages.begin() + nPageIndex, nullptr);
}
enable_notify_events();
}
void make_overflow_boxes()
{
m_pOverFlowBox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0));
GtkWidget* pParent = gtk_widget_get_parent(GTK_WIDGET(m_pNotebook));
container_add(pParent, GTK_WIDGET(m_pOverFlowBox));
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_box_append(m_pOverFlowBox, GTK_WIDGET(m_pOverFlowNotebook));
#else
gtk_box_pack_start(m_pOverFlowBox, GTK_WIDGET(m_pOverFlowNotebook), false, false, 0);
#endif
g_object_ref(m_pNotebook);
container_remove(pParent, GTK_WIDGET(m_pNotebook));
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_box_append(m_pOverFlowBox, GTK_WIDGET(m_pNotebook));
#else
gtk_box_pack_start(m_pOverFlowBox, GTK_WIDGET(m_pNotebook), true, true, 0);
#endif
// coverity[freed_arg : FALSE] - this does not free m_pNotebook , it is reffed by pParent
g_object_unref(m_pNotebook);
gtk_widget_show(GTK_WIDGET(m_pOverFlowBox));
}
void split_notebooks()
{
// get the original preferred size for the notebook, the sane width
// expected here depends on the notebooks all initially having
// scrollable tabs enabled
GtkAllocation alloc;
gtk_widget_get_allocation(GTK_WIDGET(m_pNotebook), &alloc);
// toggle the direction of the split since the last time
m_bOverFlowBoxIsStart = !m_bOverFlowBoxIsStart;
if (!m_pOverFlowBox)
make_overflow_boxes();
// don't scroll the tabs anymore
// coverity[pass_freed_arg : FALSE] - m_pNotebook is not freed here
gtk_notebook_set_scrollable(m_pNotebook, false);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_freeze_child_notify(GTK_WIDGET(m_pNotebook));
gtk_widget_freeze_child_notify(GTK_WIDGET(m_pOverFlowNotebook));
#else
g_object_freeze_notify(G_OBJECT(m_pNotebook));
g_object_freeze_notify(G_OBJECT(m_pOverFlowNotebook));
#endif
gtk_widget_show(GTK_WIDGET(m_pOverFlowNotebook));
gint nPages;
GtkRequisition size1, size2;
if (!m_nStartTabCount && !m_nEndTabCount)
{
nPages = gtk_notebook_get_n_pages(m_pNotebook);
std::vector<int> aLabelWidths;
//move tabs to the overflow notebook
for (int i = 0; i < nPages; ++i)
{
OUString sLabel(get_tab_label_text(m_pNotebook, i));
aLabelWidths.push_back(get_pixel_size(sLabel).Width());
}
int row_width = std::accumulate(aLabelWidths.begin(), aLabelWidths.end(), 0) / 2;
int count = 0;
for (int i = 0; i < nPages; ++i)
{
count += aLabelWidths[i];
if (count >= row_width)
{
m_nStartTabCount = i;
break;
}
}
m_nEndTabCount = nPages - m_nStartTabCount;
}
//move the tabs to the overflow notebook
int i = 0;
int nOverFlowPages = m_nStartTabCount;
while (nOverFlowPages)
{
OUString sIdent(get_page_ident(m_pNotebook, 0));
OUString sLabel(get_tab_label_text(m_pNotebook, 0));
remove_page(m_pNotebook, sIdent);
insert_page(m_pOverFlowNotebook, sIdent, sLabel, gtk_grid_new(), -1);
GtkWidget* pTabWidget = gtk_notebook_get_tab_label(m_pOverFlowNotebook,
gtk_notebook_get_nth_page(m_pOverFlowNotebook, i));
gtk_widget_set_hexpand(pTabWidget, true);
--nOverFlowPages;
++i;
}
for (i = 0; i < m_nEndTabCount; ++i)
{
GtkWidget* pTabWidget = gtk_notebook_get_tab_label(m_pNotebook,
gtk_notebook_get_nth_page(m_pNotebook, i));
gtk_widget_set_hexpand(pTabWidget, true);
}
// have to have some tab as the active tab of the overflow notebook
append_useless_page(m_pOverFlowNotebook);
gtk_notebook_set_current_page(m_pOverFlowNotebook, gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1);
if (gtk_widget_has_focus(GTK_WIDGET(m_pOverFlowNotebook)))
gtk_widget_grab_focus(GTK_WIDGET(m_pNotebook));
// add this temporarily to the normal notebook to measure how wide
// the row would be if switched to the other notebook
append_useless_page(m_pNotebook);
gtk_widget_get_preferred_size(GTK_WIDGET(m_pNotebook), nullptr, &size1);
gtk_widget_get_preferred_size(GTK_WIDGET(m_pOverFlowNotebook), nullptr, &size2);
auto nWidth = std::max(size1.width, size2.width);
gtk_widget_set_size_request(GTK_WIDGET(m_pNotebook), nWidth, alloc.height);
gtk_widget_set_size_request(GTK_WIDGET(m_pOverFlowNotebook), nWidth, -1);
// remove it once we've measured it
remove_page(m_pNotebook, u"useless");
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_thaw_child_notify(GTK_WIDGET(m_pOverFlowNotebook));
gtk_widget_thaw_child_notify(GTK_WIDGET(m_pNotebook));
#else
g_object_thaw_notify(G_OBJECT(m_pOverFlowNotebook));
g_object_thaw_notify(G_OBJECT(m_pNotebook));
#endif
m_bOverFlowBoxActive = true;
}
static gboolean launch_split_notebooks(GtkInstanceNotebook* pThis)
{
int nCurrentPage = pThis->get_current_page();
pThis->split_notebooks();
pThis->set_current_page(nCurrentPage);
pThis->m_nLaunchSplitTimeoutId = 0;
return false;
}
// tdf#120371
// https://developer.gnome.org/hig-book/unstable/controls-notebooks.html.en#controls-too-many-tabs
// if no of tabs > 6, but only if the notebook would auto-scroll, then split the tabs over
// two notebooks. Checking for the auto-scroll allows themes like Ambience under Ubuntu 16.04 to keep
// tabs in a single row when they would fit
void signal_notebook_size_allocate()
{
if (m_bOverFlowBoxActive || m_nLaunchSplitTimeoutId)
return;
disable_notify_events();
gint nPages = gtk_notebook_get_n_pages(m_pNotebook);
if (nPages > 6 && gtk_notebook_get_tab_pos(m_pNotebook) == GTK_POS_TOP)
{
for (gint i = 0; i < nPages; ++i)
{
GtkWidget* pTabWidget = gtk_notebook_get_tab_label(m_pNotebook, gtk_notebook_get_nth_page(m_pNotebook, i));
#if GTK_CHECK_VERSION(4, 0, 0)
bool bTabVisible = gtk_widget_get_child_visible(gtk_widget_get_parent(pTabWidget));
#else
bool bTabVisible = gtk_widget_get_child_visible(pTabWidget);
#endif
if (!bTabVisible)
{
m_nLaunchSplitTimeoutId = g_timeout_add_full(G_PRIORITY_HIGH_IDLE, 0, reinterpret_cast<GSourceFunc>(launch_split_notebooks), this, nullptr);
break;
}
}
}
enable_notify_events();
}
#if GTK_CHECK_VERSION(4, 0, 0)
DECL_LINK(SizeAllocateHdl, void*, void);
#else
static void signalSizeAllocate(GtkWidget*, GdkRectangle*, gpointer widget)
{
GtkInstanceNotebook* pThis = static_cast<GtkInstanceNotebook*>(widget);
pThis->signal_notebook_size_allocate();
}
#endif
bool signal_focus(GtkDirectionType direction)
{
if (!m_bOverFlowBoxActive)
return false;
int nPage = gtk_notebook_get_current_page(m_pNotebook);
if (direction == GTK_DIR_LEFT && nPage == 0)
{
auto nOverFlowLen = gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1;
gtk_notebook_set_current_page(m_pOverFlowNotebook, nOverFlowLen - 1);
return true;
}
else if (direction == GTK_DIR_RIGHT && nPage == gtk_notebook_get_n_pages(m_pNotebook) - 1)
{
gtk_notebook_set_current_page(m_pOverFlowNotebook, 0);
return true;
}
return false;
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalFocus(GtkNotebook* notebook, GtkDirectionType direction, gpointer widget)
{
// if the notebook widget itself has focus
if (gtk_widget_is_focus(GTK_WIDGET(notebook)))
{
GtkInstanceNotebook* pThis = static_cast<GtkInstanceNotebook*>(widget);
return pThis->signal_focus(direction);
}
return false;
}
#endif
// ctrl + page_up/ page_down
bool signal_change_current_page(gint arg1)
{
bool bHandled = signal_focus(arg1 < 0 ? GTK_DIR_LEFT : GTK_DIR_RIGHT);
if (bHandled)
g_signal_stop_emission_by_name(m_pNotebook, "change-current-page");
return false;
}
static gboolean signalChangeCurrentPage(GtkNotebook*, gint arg1, gpointer widget)
{
if (arg1 == 0)
return true;
GtkInstanceNotebook* pThis = static_cast<GtkInstanceNotebook*>(widget);
return pThis->signal_change_current_page(arg1);
}
public:
GtkInstanceNotebook(GtkNotebook* pNotebook, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pNotebook), pBuilder, bTakeOwnership)
, m_pNotebook(pNotebook)
, m_pOverFlowBox(nullptr)
, m_pOverFlowNotebook(GTK_NOTEBOOK(gtk_notebook_new()))
, m_nSwitchPageSignalId(g_signal_connect(pNotebook, "switch-page", G_CALLBACK(signalSwitchPage), this))
, m_nOverFlowSwitchPageSignalId(g_signal_connect(m_pOverFlowNotebook, "switch-page", G_CALLBACK(signalOverFlowSwitchPage), this))
#if GTK_CHECK_VERSION(4, 0, 0)
, m_pLayout(nullptr)
#else
, m_nNotebookSizeAllocateSignalId(0)
, m_nFocusSignalId(g_signal_connect(pNotebook, "focus", G_CALLBACK(signalFocus), this))
#endif
, m_nChangeCurrentPageId(g_signal_connect(pNotebook, "change-current-page", G_CALLBACK(signalChangeCurrentPage), this))
, m_nLaunchSplitTimeoutId(0)
, m_bOverFlowBoxActive(false)
, m_bOverFlowBoxIsStart(false)
, m_bInternalPageChange(false)
, m_nStartTabCount(0)
, m_nEndTabCount(0)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_add_events(GTK_WIDGET(pNotebook), GDK_SCROLL_MASK);
#endif
gint nPages = gtk_notebook_get_n_pages(m_pNotebook);
if (nPages > 6)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
m_nNotebookSizeAllocateSignalId = g_signal_connect_after(pNotebook, "size-allocate", G_CALLBACK(signalSizeAllocate), this);
#else
m_pLayout = NOTIFYING_LAYOUT(g_object_new(notifying_layout_get_type(), nullptr));
notifying_layout_start_watch(m_pLayout, GTK_WIDGET(pNotebook), LINK(this, GtkInstanceNotebook, SizeAllocateHdl));
#endif
}
gtk_notebook_set_show_border(m_pOverFlowNotebook, false);
// tdf#122623 it's nigh impossible to have a GtkNotebook without an active (checked) tab, so try and theme
// the unwanted tab into invisibility via the 'overflow' class themed by global CreateStyleProvider
GtkStyleContext *pNotebookContext = gtk_widget_get_style_context(GTK_WIDGET(m_pOverFlowNotebook));
gtk_style_context_add_class(pNotebookContext, "overflow");
}
virtual int get_current_page() const override
{
int nPage = gtk_notebook_get_current_page(m_pNotebook);
if (nPage == -1)
return nPage;
if (m_bOverFlowBoxIsStart)
{
auto nOverFlowLen = m_bOverFlowBoxActive ? gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1 : 0;
// add count of overflow pages, minus the extra tab
nPage += nOverFlowLen;
}
return nPage;
}
virtual OUString get_page_ident(int nPage) const override
{
auto nMainLen = gtk_notebook_get_n_pages(m_pNotebook);
auto nOverFlowLen = m_bOverFlowBoxActive ? gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1 : 0;
if (m_bOverFlowBoxIsStart)
{
if (nPage < nOverFlowLen)
return get_page_ident(m_pOverFlowNotebook, nPage);
nPage -= nOverFlowLen;
return get_page_ident(m_pNotebook, nPage);
}
else
{
if (nPage < nMainLen)
return get_page_ident(m_pNotebook, nPage);
nPage -= nMainLen;
return get_page_ident(m_pOverFlowNotebook, nPage);
}
}
virtual OUString get_current_page_ident() const override
{
const int nPage = get_current_page();
return nPage != -1 ? get_page_ident(nPage) : OUString();
}
virtual int get_page_index(const OUString& rIdent) const override
{
auto nMainIndex = get_page_number(m_pNotebook, rIdent);
auto nOverFlowIndex = get_page_number(m_pOverFlowNotebook, rIdent);
if (nMainIndex == -1 && nOverFlowIndex == -1)
return -1;
if (m_bOverFlowBoxIsStart)
{
if (nOverFlowIndex != -1)
return nOverFlowIndex;
else
{
auto nOverFlowLen = m_bOverFlowBoxActive ? gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1 : 0;
return nMainIndex + nOverFlowLen;
}
}
else
{
if (nMainIndex != -1)
return nMainIndex;
else
{
auto nMainLen = gtk_notebook_get_n_pages(m_pNotebook);
return nOverFlowIndex + nMainLen;
}
}
}
virtual weld::Container* get_page(const OUString& rIdent) const override
{
int nPage = get_page_index(rIdent);
if (nPage < 0)
return nullptr;
GtkWidget* pChild;
if (m_bOverFlowBoxIsStart)
{
auto nOverFlowLen = m_bOverFlowBoxActive ? gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1 : 0;
if (nPage < nOverFlowLen)
pChild = gtk_notebook_get_nth_page(m_pOverFlowNotebook, nPage);
else
{
nPage -= nOverFlowLen;
pChild = gtk_notebook_get_nth_page(m_pNotebook, nPage);
}
}
else
{
auto nMainLen = gtk_notebook_get_n_pages(m_pNotebook);
if (nPage < nMainLen)
pChild = gtk_notebook_get_nth_page(m_pNotebook, nPage);
else
{
nPage -= nMainLen;
pChild = gtk_notebook_get_nth_page(m_pOverFlowNotebook, nPage);
}
}
unsigned int nPageIndex = static_cast<unsigned int>(nPage);
if (m_aPages.size() < nPageIndex + 1)
m_aPages.resize(nPageIndex + 1);
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!m_aPages[nPageIndex])
m_aPages[nPageIndex].reset(new GtkInstanceContainer(GTK_CONTAINER(pChild), m_pBuilder, false));
#else
if (!m_aPages[nPageIndex])
m_aPages[nPageIndex].reset(new GtkInstanceContainer(pChild, m_pBuilder, false));
#endif
return m_aPages[nPageIndex].get();
}
virtual void set_current_page(int nPage) override
{
// normally we'd call disable_notify_events/enable_notify_events here,
// but the notebook is complicated by the need to support the
// double-decker hackery so for simplicity just flag that the page
// change is not a directly user-triggered one
bool bInternalPageChange = m_bInternalPageChange;
m_bInternalPageChange = true;
if (m_bOverFlowBoxIsStart)
{
auto nOverFlowLen = m_bOverFlowBoxActive ? gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1 : 0;
if (nPage < nOverFlowLen)
gtk_notebook_set_current_page(m_pOverFlowNotebook, nPage);
else
{
nPage -= nOverFlowLen;
gtk_notebook_set_current_page(m_pNotebook, nPage);
}
}
else
{
auto nMainLen = gtk_notebook_get_n_pages(m_pNotebook);
if (nPage < nMainLen)
gtk_notebook_set_current_page(m_pNotebook, nPage);
else
{
nPage -= nMainLen;
gtk_notebook_set_current_page(m_pOverFlowNotebook, nPage);
}
}
m_bInternalPageChange = bInternalPageChange;
}
virtual void set_current_page(const OUString& rIdent) override
{
gint nPage = get_page_index(rIdent);
set_current_page(nPage);
}
virtual int get_n_pages() const override
{
int nLen = gtk_notebook_get_n_pages(m_pNotebook);
if (m_bOverFlowBoxActive)
nLen += gtk_notebook_get_n_pages(m_pOverFlowNotebook) - 1;
return nLen;
}
virtual OUString get_tab_label_text(const OUString& rIdent) const override
{
gint nPageNum = get_page_number(m_pNotebook, rIdent);
if (nPageNum != -1)
return get_tab_label_text(m_pNotebook, nPageNum);
nPageNum = get_page_number(m_pOverFlowNotebook, rIdent);
if (nPageNum != -1)
return get_tab_label_text(m_pOverFlowNotebook, nPageNum);
return OUString();
}
virtual void set_tab_label_text(const OUString& rIdent, const OUString& rText) override
{
gint nPageNum = get_page_number(m_pNotebook, rIdent);
if (nPageNum != -1)
{
set_tab_label_text(m_pNotebook, nPageNum, rText);
return;
}
nPageNum = get_page_number(m_pOverFlowNotebook, rIdent);
if (nPageNum != -1)
{
set_tab_label_text(m_pOverFlowNotebook, nPageNum, rText);
}
}
virtual void set_show_tabs(bool bShow) override
{
if (m_bOverFlowBoxActive)
{
unsplit_notebooks();
reset_split_data();
}
gtk_notebook_set_show_tabs(m_pNotebook, bShow);
gtk_notebook_set_show_tabs(m_pOverFlowNotebook, bShow);
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pNotebook, m_nSwitchPageSignalId);
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_block(m_pNotebook, m_nFocusSignalId);
#endif
g_signal_handler_block(m_pNotebook, m_nChangeCurrentPageId);
g_signal_handler_block(m_pOverFlowNotebook, m_nOverFlowSwitchPageSignalId);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_freeze_child_notify(GTK_WIDGET(m_pOverFlowNotebook));
#endif
g_object_freeze_notify(G_OBJECT(m_pOverFlowNotebook));
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_object_thaw_notify(G_OBJECT(m_pOverFlowNotebook));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_thaw_child_notify(GTK_WIDGET(m_pOverFlowNotebook));
#endif
g_signal_handler_unblock(m_pOverFlowNotebook, m_nOverFlowSwitchPageSignalId);
g_signal_handler_unblock(m_pNotebook, m_nSwitchPageSignalId);
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_unblock(m_pNotebook, m_nFocusSignalId);
#endif
g_signal_handler_unblock(m_pNotebook, m_nChangeCurrentPageId);
}
void reset_split_data()
{
// reset overflow and allow it to be recalculated if necessary
gtk_widget_hide(GTK_WIDGET(m_pOverFlowNotebook));
m_bOverFlowBoxActive = false;
m_nStartTabCount = 0;
m_nEndTabCount = 0;
}
virtual void remove_page(const OUString& rIdent) override
{
if (m_bOverFlowBoxActive)
{
unsplit_notebooks();
reset_split_data();
}
unsigned int nPageIndex = remove_page(m_pNotebook, rIdent);
if (nPageIndex < m_aPages.size())
m_aPages.erase(m_aPages.begin() + nPageIndex);
}
virtual void insert_page(const OUString& rIdent, const OUString& rLabel, int nPos) override
{
if (m_bOverFlowBoxActive)
{
unsplit_notebooks();
reset_split_data();
}
// reset overflow and allow it to be recalculated if necessary
gtk_widget_hide(GTK_WIDGET(m_pOverFlowNotebook));
m_bOverFlowBoxActive = false;
insert_page(m_pNotebook, rIdent, rLabel, gtk_grid_new(), nPos);
}
virtual ~GtkInstanceNotebook() override
{
if (m_nLaunchSplitTimeoutId)
g_source_remove(m_nLaunchSplitTimeoutId);
#if !GTK_CHECK_VERSION(4, 0, 0)
if (m_nNotebookSizeAllocateSignalId)
g_signal_handler_disconnect(m_pNotebook, m_nNotebookSizeAllocateSignalId);
#else
if (m_pLayout)
{
// put it back how we found it initially
notifying_layout_stop_watch(m_pLayout);
}
#endif
g_signal_handler_disconnect(m_pNotebook, m_nSwitchPageSignalId);
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pNotebook, m_nFocusSignalId);
#endif
g_signal_handler_disconnect(m_pNotebook, m_nChangeCurrentPageId);
g_signal_handler_disconnect(m_pOverFlowNotebook, m_nOverFlowSwitchPageSignalId);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_destroy(GTK_WIDGET(m_pOverFlowNotebook));
#else
GtkWidget* pOverFlowWidget = GTK_WIDGET(m_pOverFlowNotebook);
g_clear_pointer(&pOverFlowWidget, gtk_widget_unparent);
#endif
if (!m_pOverFlowBox)
return;
// put it back to how we found it initially
GtkWidget* pParent = gtk_widget_get_parent(GTK_WIDGET(m_pOverFlowBox));
g_object_ref(m_pNotebook);
container_remove(GTK_WIDGET(m_pOverFlowBox), GTK_WIDGET(m_pNotebook));
container_add(GTK_WIDGET(pParent), GTK_WIDGET(m_pNotebook));
g_object_unref(m_pNotebook);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_destroy(GTK_WIDGET(m_pOverFlowBox));
#else
GtkWidget* pOverFlowBox = GTK_WIDGET(m_pOverFlowBox);
g_clear_pointer(&pOverFlowBox, gtk_widget_unparent);
#endif
}
};
#if GTK_CHECK_VERSION(4, 0, 0)
IMPL_LINK_NOARG(GtkInstanceNotebook, SizeAllocateHdl, void*, void)
{
signal_notebook_size_allocate();
}
#endif
OUString vcl_font_to_css(const vcl::Font& rFont)
{
OUStringBuffer sCSS(
"font-family: \"" + rFont.GetFamilyName() + "\"; "
"font-size: " + OUString::number(rFont.GetFontSize().Height()) + "pt; ");
switch (rFont.GetItalic())
{
case ITALIC_NONE:
sCSS.append("font-style: normal; ");
break;
case ITALIC_NORMAL:
sCSS.append("font-style: italic; ");
break;
case ITALIC_OBLIQUE:
sCSS.append("font-style: oblique; ");
break;
default:
break;
}
switch (rFont.GetWeight())
{
case WEIGHT_ULTRALIGHT:
sCSS.append("font-weight: 200; ");
break;
case WEIGHT_LIGHT:
sCSS.append("font-weight: 300; ");
break;
case WEIGHT_NORMAL:
sCSS.append("font-weight: 400; ");
break;
case WEIGHT_BOLD:
sCSS.append("font-weight: 700; ");
break;
case WEIGHT_ULTRABOLD:
sCSS.append("font-weight: 800; ");
break;
default:
break;
}
switch (rFont.GetWidthType())
{
case WIDTH_ULTRA_CONDENSED:
sCSS.append("font-stretch: ultra-condensed; ");
break;
case WIDTH_EXTRA_CONDENSED:
sCSS.append("font-stretch: extra-condensed; ");
break;
case WIDTH_CONDENSED:
sCSS.append("font-stretch: condensed; ");
break;
case WIDTH_SEMI_CONDENSED:
sCSS.append("font-stretch: semi-condensed; ");
break;
case WIDTH_NORMAL:
sCSS.append("font-stretch: normal; ");
break;
case WIDTH_SEMI_EXPANDED:
sCSS.append("font-stretch: semi-expanded; ");
break;
case WIDTH_EXPANDED:
sCSS.append("font-stretch: expanded; ");
break;
case WIDTH_EXTRA_EXPANDED:
sCSS.append("font-stretch: extra-expanded; ");
break;
case WIDTH_ULTRA_EXPANDED:
sCSS.append("font-stretch: ultra-expanded; ");
break;
default:
break;
}
return sCSS.toString();
}
void update_attr_list(PangoAttrList* pAttrList, const vcl::Font& rFont)
{
pango_attr_list_change(pAttrList, pango_attr_family_new(OUStringToOString(rFont.GetFamilyName(), RTL_TEXTENCODING_UTF8).getStr()));
pango_attr_list_change(pAttrList, pango_attr_size_new(rFont.GetFontSize().Height() * PANGO_SCALE));
switch (rFont.GetItalic())
{
case ITALIC_NONE:
pango_attr_list_change(pAttrList, pango_attr_style_new(PANGO_STYLE_NORMAL));
break;
case ITALIC_NORMAL:
pango_attr_list_change(pAttrList, pango_attr_style_new(PANGO_STYLE_ITALIC));
break;
case ITALIC_OBLIQUE:
pango_attr_list_change(pAttrList, pango_attr_style_new(PANGO_STYLE_OBLIQUE));
break;
default:
break;
}
switch (rFont.GetWeight())
{
case WEIGHT_ULTRALIGHT:
pango_attr_list_change(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_ULTRALIGHT));
break;
case WEIGHT_LIGHT:
pango_attr_list_change(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_LIGHT));
break;
case WEIGHT_NORMAL:
pango_attr_list_change(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_NORMAL));
break;
case WEIGHT_BOLD:
pango_attr_list_change(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_BOLD));
break;
case WEIGHT_ULTRABOLD:
pango_attr_list_change(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_ULTRABOLD));
break;
default:
break;
}
switch (rFont.GetWidthType())
{
case WIDTH_ULTRA_CONDENSED:
pango_attr_list_change(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_ULTRA_CONDENSED));
break;
case WIDTH_EXTRA_CONDENSED:
pango_attr_list_change(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_EXTRA_CONDENSED));
break;
case WIDTH_CONDENSED:
pango_attr_list_change(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_CONDENSED));
break;
case WIDTH_SEMI_CONDENSED:
pango_attr_list_change(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_SEMI_CONDENSED));
break;
case WIDTH_NORMAL:
pango_attr_list_change(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_NORMAL));
break;
case WIDTH_SEMI_EXPANDED:
pango_attr_list_change(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_SEMI_EXPANDED));
break;
case WIDTH_EXPANDED:
pango_attr_list_change(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_EXPANDED));
break;
case WIDTH_EXTRA_EXPANDED:
pango_attr_list_change(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_EXTRA_EXPANDED));
break;
case WIDTH_ULTRA_EXPANDED:
pango_attr_list_change(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_ULTRA_EXPANDED));
break;
default:
break;
}
}
gboolean filter_pango_attrs(PangoAttribute *attr, gpointer data)
{
PangoAttrType* pFilterAttrs = static_cast<PangoAttrType*>(data);
while (*pFilterAttrs)
{
if (attr->klass->type == *pFilterAttrs)
return true;
++pFilterAttrs;
}
return false;
}
void set_font(GtkLabel* pLabel, const vcl::Font& rFont)
{
PangoAttrList* pOrigList = gtk_label_get_attributes(pLabel);
PangoAttrList* pAttrList = pOrigList ? pango_attr_list_copy(pOrigList) : pango_attr_list_new();
if (pOrigList)
{
// tdf#143443 remove both PANGO_ATTR_ABSOLUTE_SIZE and PANGO_ATTR_SIZE
// because pango_attr_list_change(..., pango_attr_size_new...) isn't
// sufficient on its own to ensure a new size sticks.
PangoAttrType aFilterAttrs[] = {PANGO_ATTR_ABSOLUTE_SIZE, PANGO_ATTR_SIZE, PANGO_ATTR_INVALID};
PangoAttrList* pRemovedAttrs = pango_attr_list_filter(pAttrList, filter_pango_attrs, &aFilterAttrs);
pango_attr_list_unref(pRemovedAttrs);
}
update_attr_list(pAttrList, rFont);
gtk_label_set_attributes(pLabel, pAttrList);
pango_attr_list_unref(pAttrList);
}
}
namespace {
class WidgetBackground
{
private:
GtkWidget* m_pWidget;
GtkCssProvider* m_pCustomCssProvider;
std::unique_ptr<utl::TempFileNamed> m_xCustomImage;
public:
// See: https://developer.gnome.org/Buttons/
void use_custom_content(const VirtualDevice* pDevice)
{
GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(m_pWidget);
if (m_pCustomCssProvider)
{
gtk_style_context_remove_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pCustomCssProvider));
m_pCustomCssProvider = nullptr;
}
m_xCustomImage.reset();
if (!pDevice)
return;
m_xCustomImage.reset(new utl::TempFileNamed);
m_xCustomImage->EnableKillingFile(true);
cairo_surface_t* surface = get_underlying_cairo_surface(*pDevice);
Size aSize = pDevice->GetOutputSizePixel();
cairo_surface_write_to_png(surface, OUStringToOString(m_xCustomImage->GetFileName(), osl_getThreadTextEncoding()).getStr());
m_pCustomCssProvider = gtk_css_provider_new();
OUString aBuffer = "* { background-image: url(\"" + m_xCustomImage->GetURL() + "\"); "
"background-size: " + OUString::number(aSize.Width()) + "px " + OUString::number(aSize.Height()) + "px; "
"border-radius: 0; border-width: 0; }";
OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8);
css_provider_load_from_data(m_pCustomCssProvider, aResult.getStr(), aResult.getLength());
gtk_style_context_add_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pCustomCssProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
public:
WidgetBackground(GtkWidget* pWidget)
: m_pWidget(pWidget)
, m_pCustomCssProvider(nullptr)
{
}
~WidgetBackground()
{
if (m_pCustomCssProvider)
use_custom_content(nullptr);
assert(!m_pCustomCssProvider);
}
};
class WidgetFont
{
private:
GtkWidget* m_pWidget;
GtkCssProvider* m_pFontCssProvider;
std::unique_ptr<vcl::Font> m_xFont;
public:
WidgetFont(GtkWidget* pWidget)
: m_pWidget(pWidget)
, m_pFontCssProvider(nullptr)
{
}
void use_custom_font(const vcl::Font* pFont, std::u16string_view rCSSSelector)
{
GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(m_pWidget);
if (m_pFontCssProvider)
{
gtk_style_context_remove_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider));
m_pFontCssProvider = nullptr;
}
m_xFont.reset();
if (!pFont)
return;
m_xFont.reset(new vcl::Font(*pFont));
m_pFontCssProvider = gtk_css_provider_new();
OUString aBuffer = rCSSSelector + OUString::Concat(" { ") + vcl_font_to_css(*pFont) + OUString::Concat(" }");
OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8);
css_provider_load_from_data(m_pFontCssProvider, aResult.getStr(), aResult.getLength());
gtk_style_context_add_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
const vcl::Font* get_custom_font() const
{
return m_xFont.get();
}
~WidgetFont()
{
if (m_pFontCssProvider)
use_custom_font(nullptr, u"");
assert(!m_pFontCssProvider);
}
};
class GtkInstanceButton : public GtkInstanceWidget, public virtual weld::Button
{
private:
GtkButton* m_pButton;
gulong m_nSignalId;
std::optional<vcl::Font> m_xFont;
WidgetBackground m_aCustomBackground;
static void signalClicked(GtkButton*, gpointer widget)
{
GtkInstanceButton* pThis = static_cast<GtkInstanceButton*>(widget);
SolarMutexGuard aGuard;
pThis->signal_clicked();
}
virtual void ensureMouseEventWidget() override
{
// The GtkButton is sufficient to get mouse events without an intermediate GtkEventBox
if (!m_pMouseEventBox)
m_pMouseEventBox = m_pWidget;
}
public:
GtkInstanceButton(GtkButton* pButton, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pButton), pBuilder, bTakeOwnership)
, m_pButton(pButton)
, m_nSignalId(g_signal_connect(pButton, "clicked", G_CALLBACK(signalClicked), this))
, m_aCustomBackground(GTK_WIDGET(pButton))
{
g_object_set_data(G_OBJECT(m_pButton), "g-lo-GtkInstanceButton", this);
}
virtual void set_label(const OUString& rText) override
{
::button_set_label(m_pButton, rText);
}
virtual void set_image(VirtualDevice* pDevice) override
{
::button_set_image(m_pButton, pDevice);
}
virtual void set_from_icon_name(const OUString& rIconName) override
{
::button_set_from_icon_name(m_pButton, rIconName);
}
virtual void set_image(const css::uno::Reference<css::graphic::XGraphic>& rImage) override
{
::button_set_image(m_pButton, rImage);
}
virtual void set_custom_button(VirtualDevice* pDevice) override
{
m_aCustomBackground.use_custom_content(pDevice);
}
virtual OUString get_label() const override
{
return ::button_get_label(m_pButton);
}
virtual void set_font(const vcl::Font& rFont) override
{
m_xFont = rFont;
GtkLabel* pChild = ::get_label_widget(GTK_WIDGET(m_pButton));
::set_font(pChild, rFont);
}
virtual vcl::Font get_font() override
{
if (m_xFont)
return *m_xFont;
return GtkInstanceWidget::get_font();
}
// allow us to block buttons with click handlers making dialogs return a response
bool has_click_handler() const
{
return m_aClickHdl.IsSet();
}
void clear_click_handler()
{
m_aClickHdl = Link<Button&, void>();
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pButton, m_nSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pButton, m_nSignalId);
}
virtual ~GtkInstanceButton() override
{
g_object_steal_data(G_OBJECT(m_pButton), "g-lo-GtkInstanceButton");
g_signal_handler_disconnect(m_pButton, m_nSignalId);
}
};
}
void GtkInstanceDialog::asyncresponse(gint ret)
{
SolarMutexGuard aGuard;
if (ret == GTK_RESPONSE_HELP)
{
help();
return;
}
GtkInstanceButton* pClickHandler = has_click_handler(ret);
if (pClickHandler)
{
// make GTK_RESPONSE_DELETE_EVENT act as if cancel button was pressed
if (ret == GTK_RESPONSE_DELETE_EVENT)
close(false);
return;
}
if (get_modal())
m_aDialogRun.dec_modal_count();
hide();
// move the self pointer, otherwise it might be de-allocated by time we try to reset it
auto xRunAsyncSelf = std::move(m_xRunAsyncSelf);
auto xDialogController = std::move(m_xDialogController);
auto aFunc = std::move(m_aFunc);
auto nResponseSignalId = m_nResponseSignalId;
auto nCancelSignalId = m_nCancelSignalId;
auto nSignalDeleteId = m_nSignalDeleteId;
m_nResponseSignalId = 0;
m_nCancelSignalId = 0;
m_nSignalDeleteId = 0;
if (aFunc)
aFunc(GtkToVcl(ret));
if (nResponseSignalId)
g_signal_handler_disconnect(m_pDialog, nResponseSignalId);
if (nCancelSignalId)
g_signal_handler_disconnect(m_pDialog, nCancelSignalId);
if (nSignalDeleteId)
g_signal_handler_disconnect(m_pDialog, nSignalDeleteId);
xDialogController.reset();
xRunAsyncSelf.reset();
}
int GtkInstanceDialog::run()
{
// tdf#150723 "run" will make the dialog visible so drop m_aPosWhileInvis like show
m_aPosWhileInvis.reset();
#if !GTK_CHECK_VERSION(4, 0, 0)
if (GTK_IS_DIALOG(m_pDialog))
sort_native_button_order(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(m_pDialog))));
#endif
int ret;
while (true)
{
ret = m_aDialogRun.run();
if (ret == GTK_RESPONSE_HELP)
{
help();
continue;
}
else if (has_click_handler(ret))
continue;
break;
}
hide();
return GtkToVcl(ret);
}
weld::Button* GtkInstanceDialog::weld_widget_for_response(int nVclResponse)
{
GtkButton* pButton = get_widget_for_response(VclToGtk(nVclResponse));
if (!pButton)
return nullptr;
return new GtkInstanceButton(pButton, m_pBuilder, false);
}
void GtkInstanceDialog::response(int nResponse)
{
int nGtkResponse = VclToGtk(nResponse);
//unblock this response now when activated through code
if (GtkButton* pWidget = get_widget_for_response(nGtkResponse))
{
void* pData = g_object_get_data(G_OBJECT(pWidget), "g-lo-GtkInstanceButton");
GtkInstanceButton* pButton = static_cast<GtkInstanceButton*>(pData);
if (pButton)
pButton->clear_click_handler();
}
if (GTK_IS_DIALOG(m_pDialog))
gtk_dialog_response(GTK_DIALOG(m_pDialog), nGtkResponse);
else if (GTK_IS_ASSISTANT(m_pDialog))
{
if (!m_aDialogRun.loop_is_running())
asyncresponse(nGtkResponse);
else
{
m_aDialogRun.m_nResponseId = nGtkResponse;
m_aDialogRun.loop_quit();
}
}
}
void GtkInstanceDialog::close(bool bCloseSignal)
{
GtkInstanceButton* pClickHandler = has_click_handler(GTK_RESPONSE_CANCEL);
if (pClickHandler)
{
if (bCloseSignal)
g_signal_stop_emission_by_name(m_pDialog, "close");
// make esc (bCloseSignal == true) or window-delete (bCloseSignal == false)
// act as if cancel button was pressed
pClickHandler->clicked();
return;
}
response(RET_CANCEL);
}
GtkInstanceButton* GtkInstanceDialog::has_click_handler(int nResponse)
{
GtkInstanceButton* pButton = nullptr;
// e.g. map GTK_RESPONSE_DELETE_EVENT to GTK_RESPONSE_CANCEL
nResponse = VclToGtk(GtkToVcl(nResponse));
if (GtkButton* pWidget = get_widget_for_response(nResponse))
{
void* pData = g_object_get_data(G_OBJECT(pWidget), "g-lo-GtkInstanceButton");
pButton = static_cast<GtkInstanceButton*>(pData);
if (pButton && !pButton->has_click_handler())
pButton = nullptr;
}
return pButton;
}
namespace {
class GtkInstanceToggleButton : public GtkInstanceButton, public virtual weld::ToggleButton
{
protected:
GtkToggleButton* m_pToggleButton;
gulong m_nToggledSignalId;
private:
static void signalToggled(GtkToggleButton*, gpointer widget)
{
GtkInstanceToggleButton* pThis = static_cast<GtkInstanceToggleButton*>(widget);
SolarMutexGuard aGuard;
pThis->signal_toggled();
}
public:
GtkInstanceToggleButton(GtkToggleButton* pButton, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceButton(GTK_BUTTON(pButton), pBuilder, bTakeOwnership)
, m_pToggleButton(pButton)
, m_nToggledSignalId(g_signal_connect(m_pToggleButton, "toggled", G_CALLBACK(signalToggled), this))
{
}
virtual void set_active(bool active) override
{
disable_notify_events();
set_inconsistent(false);
gtk_toggle_button_set_active(m_pToggleButton, active);
enable_notify_events();
}
virtual bool get_active() const override
{
return gtk_toggle_button_get_active(m_pToggleButton);
}
virtual void set_inconsistent(bool inconsistent) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
if (inconsistent)
gtk_widget_set_state_flags(GTK_WIDGET(m_pToggleButton), GTK_STATE_FLAG_INCONSISTENT, false);
else
gtk_widget_unset_state_flags(GTK_WIDGET(m_pToggleButton), GTK_STATE_FLAG_INCONSISTENT);
#else
gtk_toggle_button_set_inconsistent(m_pToggleButton, inconsistent);
#endif
}
virtual bool get_inconsistent() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
return gtk_widget_get_state_flags(GTK_WIDGET(m_pToggleButton)) & GTK_STATE_FLAG_INCONSISTENT;
#else
return gtk_toggle_button_get_inconsistent(m_pToggleButton);
#endif
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pToggleButton, m_nToggledSignalId);
GtkInstanceButton::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceButton::enable_notify_events();
g_signal_handler_unblock(m_pToggleButton, m_nToggledSignalId);
}
virtual ~GtkInstanceToggleButton() override
{
g_signal_handler_disconnect(m_pToggleButton, m_nToggledSignalId);
}
};
}
#if !GTK_CHECK_VERSION(4, 0, 0)
namespace {
void do_grab(GtkWidget* pWidget)
{
GdkDisplay *pDisplay = gtk_widget_get_display(pWidget);
GdkSeat* pSeat = gdk_display_get_default_seat(pDisplay);
gdk_seat_grab(pSeat, widget_get_surface(pWidget),
GDK_SEAT_CAPABILITY_KEYBOARD, true, nullptr, nullptr, nullptr, nullptr);
}
void do_ungrab(GtkWidget* pWidget)
{
GdkDisplay *pDisplay = gtk_widget_get_display(pWidget);
GdkSeat* pSeat = gdk_display_get_default_seat(pDisplay);
gdk_seat_ungrab(pSeat);
}
GtkPositionType show_menu_older_gtk(GtkWidget* pMenuButton, GtkWindow* pMenu, const GdkRectangle& rAnchor,
weld::Placement ePlace, bool bTryShrink)
{
//place the toplevel just below its launcher button
GtkWidget* pToplevel = widget_get_toplevel(pMenuButton);
gtk_coord x, y, absx, absy;
gtk_widget_translate_coordinates(pMenuButton, pToplevel, rAnchor.x, rAnchor.y, &x, &y);
GdkSurface* pWindow = widget_get_surface(pToplevel);
gdk_window_get_position(pWindow, &absx, &absy);
x += absx;
y += absy;
gint nButtonHeight = rAnchor.height;
gint nButtonWidth = rAnchor.width;
if (ePlace == weld::Placement::Under)
y += nButtonHeight;
else
x += nButtonWidth;
gtk_window_group_add_window(gtk_window_get_group(GTK_WINDOW(pToplevel)), pMenu);
gtk_window_set_transient_for(pMenu, GTK_WINDOW(pToplevel));
gint nMenuWidth, nMenuHeight;
gtk_widget_get_size_request(GTK_WIDGET(pMenu), &nMenuWidth, &nMenuHeight);
if (nMenuWidth == -1 || nMenuHeight == -1)
{
GtkRequisition req;
gtk_widget_get_preferred_size(GTK_WIDGET(pMenu), nullptr, &req);
if (nMenuWidth == -1)
nMenuWidth = req.width;
if (nMenuHeight == -1)
nMenuHeight = req.height;
}
bool bSwapForRTL = SwapForRTL(pMenuButton);
if (bSwapForRTL)
{
if (ePlace == weld::Placement::Under)
x += nButtonWidth;
else
x -= nButtonWidth;
x -= nMenuWidth;
}
tools::Rectangle aWorkArea(::get_monitor_workarea(pMenuButton));
// shrink it a little, I find it reassuring to see a little margin with a
// long menu to know the menu is fully on screen
aWorkArea.AdjustTop(8);
aWorkArea.AdjustBottom(-8);
aWorkArea.AdjustLeft(8);
aWorkArea.AdjustRight(-8);
GtkPositionType ePosUsed;
if (ePlace == weld::Placement::Under)
{
gint endx = x + nMenuWidth;
if (endx > aWorkArea.Right())
x -= endx - aWorkArea.Right();
if (x < 0)
x = 0;
ePosUsed = GTK_POS_BOTTOM;
gint endy = y + nMenuHeight;
gint nMissingBelow = endy - aWorkArea.Bottom();
if (nMissingBelow > 0)
{
gint nNewY = y - (nButtonHeight + nMenuHeight);
gint nMissingAbove = aWorkArea.Top() - nNewY;
if (nMissingAbove > 0)
{
if (bTryShrink)
{
if (nMissingBelow <= nMissingAbove)
nMenuHeight -= nMissingBelow;
else
{
nMenuHeight -= nMissingAbove;
y = aWorkArea.Top();
ePosUsed = GTK_POS_TOP;
}
gtk_widget_set_size_request(GTK_WIDGET(pMenu), nMenuWidth, nMenuHeight);
}
else
{
if (nMissingBelow <= nMissingAbove)
y -= nMissingBelow;
else
{
y = aWorkArea.Top();
ePosUsed = GTK_POS_TOP;
}
}
}
else
{
y = nNewY;
ePosUsed = GTK_POS_TOP;
}
}
}
else
{
if (!bSwapForRTL)
{
ePosUsed = GTK_POS_RIGHT;
gint endx = x + nMenuWidth;
gint nMissingAfter = endx - aWorkArea.Right();
if (nMissingAfter > 0)
{
gint nNewX = x - (nButtonWidth + nMenuWidth);
if (nNewX >= aWorkArea.Left())
{
x = nNewX;
ePosUsed = GTK_POS_LEFT;
}
}
}
else
{
ePosUsed = GTK_POS_LEFT;
gint startx = x;
gint nMissingBefore = aWorkArea.Left() - startx;
if (nMissingBefore > 0)
{
gint nNewX = x + (nButtonWidth + nMenuWidth);
if (nNewX + nMenuWidth < aWorkArea.Right())
{
x = nNewX;
ePosUsed = GTK_POS_RIGHT;
}
}
}
}
gtk_window_move(pMenu, x, y);
return ePosUsed;
}
bool show_menu_newer_gtk(GtkWidget* pComboBox, GtkWindow* pMenu, const GdkRectangle &rAnchor,
weld::Placement ePlace, bool bTryShrink)
{
static auto window_move_to_rect = reinterpret_cast<void (*) (GdkWindow*, const GdkRectangle*, GdkGravity,
GdkGravity, GdkAnchorHints, gint, gint)>(
dlsym(nullptr, "gdk_window_move_to_rect"));
if (!window_move_to_rect)
return false;
// under wayland gdk_window_move_to_rect works great for me, but in my current
// gtk 3.24 under X it leaves part of long menus outside the work area
GdkDisplay *pDisplay = gtk_widget_get_display(pComboBox);
if (DLSYM_GDK_IS_X11_DISPLAY(pDisplay))
return false;
//place the toplevel just below its launcher button
GtkWidget* pToplevel = widget_get_toplevel(pComboBox);
gtk_coord x, y;
gtk_widget_translate_coordinates(pComboBox, pToplevel, rAnchor.x, rAnchor.y, &x, &y);
gtk_widget_realize(GTK_WIDGET(pMenu));
gtk_window_group_add_window(gtk_window_get_group(GTK_WINDOW(pToplevel)), pMenu);
gtk_window_set_transient_for(pMenu, GTK_WINDOW(pToplevel));
bool bSwapForRTL = SwapForRTL(GTK_WIDGET(pComboBox));
GdkGravity rect_anchor;
GdkGravity menu_anchor;
if (ePlace == weld::Placement::Under)
{
rect_anchor = !bSwapForRTL ? GDK_GRAVITY_SOUTH_WEST : GDK_GRAVITY_SOUTH_EAST;
menu_anchor = !bSwapForRTL ? GDK_GRAVITY_NORTH_WEST : GDK_GRAVITY_NORTH_EAST;
}
else
{
rect_anchor = !bSwapForRTL ? GDK_GRAVITY_NORTH_EAST : GDK_GRAVITY_NORTH_WEST;
menu_anchor = !bSwapForRTL ? GDK_GRAVITY_NORTH_WEST : GDK_GRAVITY_NORTH_EAST;
}
GdkAnchorHints anchor_hints = static_cast<GdkAnchorHints>(GDK_ANCHOR_FLIP | GDK_ANCHOR_SLIDE);
if (bTryShrink)
anchor_hints = static_cast<GdkAnchorHints>(anchor_hints | GDK_ANCHOR_RESIZE);
GdkRectangle rect {x, y, rAnchor.width, rAnchor.height};
GdkSurface* toplevel = widget_get_surface(GTK_WIDGET(pMenu));
window_move_to_rect(toplevel, &rect, rect_anchor, menu_anchor, anchor_hints,
0, 0);
return true;
}
GtkPositionType show_menu(GtkWidget* pMenuButton, GtkWindow* pMenu, const GdkRectangle& rAnchor,
weld::Placement ePlace, bool bTryShrink)
{
// we only use ePosUsed in the replacement-for-X-popover case of a
// MenuButton, so we only need it when show_menu_older_gtk is used
GtkPositionType ePosUsed = GTK_POS_BOTTOM;
// tdf#120764 It isn't allowed under wayland to have two visible popups that share
// the same top level parent. The problem is that since gtk 3.24 tooltips are also
// implemented as popups, which means that we cannot show any popup if there is a
// visible tooltip.
GtkWidget* pParent = widget_get_toplevel(pMenuButton);
GtkSalFrame* pFrame = pParent ? GtkSalFrame::getFromWindow(pParent) : nullptr;
if (pFrame)
{
// hide any current tooltip
pFrame->HideTooltip();
// don't allow any more to appear until menu is dismissed
pFrame->BlockTooltip();
}
// try with gdk_window_move_to_rect, but if that's not available, try without
if (!show_menu_newer_gtk(pMenuButton, pMenu, rAnchor, ePlace, bTryShrink))
ePosUsed = show_menu_older_gtk(pMenuButton, pMenu, rAnchor, ePlace, bTryShrink);
gtk_widget_show_all(GTK_WIDGET(pMenu));
gtk_widget_grab_focus(GTK_WIDGET(pMenu));
do_grab(GTK_WIDGET(pMenu));
return ePosUsed;
}
}
#endif
namespace {
#if !GTK_CHECK_VERSION(4, 0, 0)
bool button_event_is_outside(GtkWidget* pMenuHack, GdkEventButton* pEvent)
{
//we want to pop down if the button was released outside our popup
gdouble x = pEvent->x_root;
gdouble y = pEvent->y_root;
gint window_x, window_y;
GdkSurface* pWindow = widget_get_surface(pMenuHack);
gdk_window_get_position(pWindow, &window_x, &window_y);
GtkAllocation alloc;
gtk_widget_get_allocation(pMenuHack, &alloc);
gint x1 = window_x;
gint y1 = window_y;
gint x2 = x1 + alloc.width;
gint y2 = y1 + alloc.height;
if (x > x1 && x < x2 && y > y1 && y < y2)
return false;
return true;
}
GtkPositionType MovePopoverContentsToWindow(GtkWidget* pPopover, GtkWindow* pMenuHack, GtkWidget* pAnchor,
const GdkRectangle& rAnchor, weld::Placement ePlace)
{
//set border width
gtk_container_set_border_width(GTK_CONTAINER(pMenuHack), gtk_container_get_border_width(GTK_CONTAINER(pPopover)));
//steal popover contents and smuggle into toplevel display window
GtkWidget* pChild = gtk_bin_get_child(GTK_BIN(pPopover));
g_object_ref(pChild);
gtk_container_remove(GTK_CONTAINER(pPopover), pChild);
gtk_container_add(GTK_CONTAINER(pMenuHack), pChild);
g_object_unref(pChild);
GtkPositionType eRet = show_menu(pAnchor, pMenuHack, rAnchor, ePlace, false);
gtk_grab_add(GTK_WIDGET(pMenuHack));
GdkSurface* pSurface = widget_get_surface(GTK_WIDGET(pMenuHack));
g_object_set_data(G_OBJECT(pSurface), "g-lo-InstancePopup", GINT_TO_POINTER(true));
return eRet;
}
void MoveWindowContentsToPopover(GtkWindow* pMenuHack, GtkWidget* pPopover, GtkWidget* pAnchor)
{
bool bHadFocus = gtk_window_has_toplevel_focus(pMenuHack);
do_ungrab(GTK_WIDGET(pMenuHack));
gtk_grab_remove(GTK_WIDGET(pMenuHack));
gtk_widget_hide(GTK_WIDGET(pMenuHack));
//put contents back from where the came from
GtkWidget* pChild = gtk_bin_get_child(GTK_BIN(pMenuHack));
g_object_ref(pChild);
gtk_container_remove(GTK_CONTAINER(pMenuHack), pChild);
gtk_container_add(GTK_CONTAINER(pPopover), pChild);
g_object_unref(pChild);
GdkSurface* pSurface = widget_get_surface(GTK_WIDGET(pMenuHack));
g_object_set_data(G_OBJECT(pSurface), "g-lo-InstancePopup", GINT_TO_POINTER(false));
// so gdk_window_move_to_rect will work again the next time
gtk_widget_unrealize(GTK_WIDGET(pMenuHack));
gtk_widget_set_size_request(GTK_WIDGET(pMenuHack), -1, -1);
// undo show_menu tooltip blocking
GtkWidget* pParent = widget_get_toplevel(pAnchor);
GtkSalFrame* pFrame = pParent ? GtkSalFrame::getFromWindow(pParent) : nullptr;
if (pFrame)
pFrame->UnblockTooltip();
if (bHadFocus)
{
GdkSurface* pParentSurface = pParent ? widget_get_surface(pParent) : nullptr;
void* pParentIsPopover = pParentSurface ? g_object_get_data(G_OBJECT(pParentSurface), "g-lo-InstancePopup") : nullptr;
if (pParentIsPopover)
do_grab(pAnchor);
gtk_widget_grab_focus(pAnchor);
}
}
#endif
/* four types of uses of this
a) textual menubutton, always with pan-down symbol, e.g. math, format, font, modify
b) image + text, always with additional pan-down symbol, e.g. writer, format, watermark
c) gear menu, never with text and without pan-down symbol where there is a replacement
icon for pan-down, e.g. file, new, templates
d) image, always with additional pan-down symbol, e.g. calc, insert, header/footer */
#if !GTK_CHECK_VERSION(4, 0, 0)
class GtkInstanceMenuButton : public GtkInstanceToggleButton, public MenuHelper, public virtual weld::MenuButton
#else
class GtkInstanceMenuButton : public GtkInstanceWidget, public MenuHelper, public virtual weld::MenuButton
#endif
{
protected:
GtkMenuButton* m_pMenuButton;
private:
GtkBox* m_pBox;
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkImage* m_pImage;
#else
GtkPicture* m_pImage;
GtkToggleButton* m_pMenuButtonToggleButton;
#endif
GtkWidget* m_pLabel;
#if !GTK_CHECK_VERSION(4, 0, 0)
//popover cannot escape dialog under X so stick up own window instead
GtkWindow* m_pMenuHack;
//when doing so, if it's a toolbar menubutton align the menu to the full toolitem
GtkWidget* m_pMenuHackAlign;
bool m_nButtonPressSeen;
gulong m_nSignalId;
#endif
GtkWidget* m_pPopover;
#if GTK_CHECK_VERSION(4, 0, 0)
gulong m_nToggledSignalId;
std::optional<vcl::Font> m_xFont;
WidgetBackground m_aCustomBackground;
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
static void signalMenuButtonToggled(GtkWidget*, gpointer widget)
{
GtkInstanceMenuButton* pThis = static_cast<GtkInstanceMenuButton*>(widget);
SolarMutexGuard aGuard;
pThis->menu_toggled();
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
void menu_toggled()
{
if (!m_pMenuHack)
return;
if (!get_active())
{
m_nButtonPressSeen = false;
MoveWindowContentsToPopover(m_pMenuHack, m_pPopover, GTK_WIDGET(m_pMenuButton));
}
else
{
GtkWidget* pAnchor = m_pMenuHackAlign ? m_pMenuHackAlign : GTK_WIDGET(m_pMenuButton);
GdkRectangle aAnchor {0, 0, gtk_widget_get_allocated_width(pAnchor), gtk_widget_get_allocated_height(pAnchor) };
GtkPositionType ePosUsed = MovePopoverContentsToWindow(m_pPopover, m_pMenuHack, pAnchor, aAnchor, weld::Placement::Under);
// tdf#132540 keep the placeholder popover on this same side as the replacement menu
gtk_popover_set_position(gtk_menu_button_get_popover(m_pMenuButton), ePosUsed);
}
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
static void signalGrabBroken(GtkWidget*, GdkEventGrabBroken *pEvent, gpointer widget)
{
GtkInstanceMenuButton* pThis = static_cast<GtkInstanceMenuButton*>(widget);
pThis->grab_broken(pEvent);
}
void grab_broken(const GdkEventGrabBroken *event)
{
if (event->grab_window == nullptr)
{
set_active(false);
}
else if (!g_object_get_data(G_OBJECT(event->grab_window), "g-lo-InstancePopup")) // another LibreOffice popover took a grab
{
//try and regrab, so when we lose the grab to the menu of the color palette
//combobox we regain it so the color palette doesn't itself disappear on next
//click on the color palette combobox
do_grab(GTK_WIDGET(m_pMenuHack));
}
}
static gboolean signalButtonPress(GtkWidget* /*pWidget*/, GdkEventButton* /*pEvent*/, gpointer widget)
{
GtkInstanceMenuButton* pThis = static_cast<GtkInstanceMenuButton*>(widget);
pThis->m_nButtonPressSeen = true;
return false;
}
static gboolean signalButtonRelease(GtkWidget* /*pWidget*/, GdkEventButton* pEvent, gpointer widget)
{
GtkInstanceMenuButton* pThis = static_cast<GtkInstanceMenuButton*>(widget);
if (pThis->m_nButtonPressSeen && button_event_is_outside(GTK_WIDGET(pThis->m_pMenuHack), pEvent))
pThis->set_active(false);
return false;
}
static gboolean keyPress(GtkWidget*, GdkEventKey* pEvent, gpointer widget)
{
GtkInstanceMenuButton* pThis = static_cast<GtkInstanceMenuButton*>(widget);
return pThis->key_press(pEvent);
}
bool key_press(const GdkEventKey* pEvent)
{
if (pEvent->keyval == GDK_KEY_Escape)
{
set_active(false);
return true;
}
return false;
}
#endif
void ensure_image_widget()
{
if (m_pImage)
return;
#if !GTK_CHECK_VERSION(4, 0, 0)
m_pImage = GTK_IMAGE(gtk_image_new());
gtk_box_pack_start(m_pBox, GTK_WIDGET(m_pImage), false, false, 0);
gtk_box_reorder_child(m_pBox, GTK_WIDGET(m_pImage), 0);
#else
m_pImage = GTK_PICTURE(gtk_picture_new());
gtk_widget_set_halign(GTK_WIDGET(m_pImage), GTK_ALIGN_CENTER);
gtk_widget_set_valign(GTK_WIDGET(m_pImage), GTK_ALIGN_CENTER);
gtk_box_prepend(m_pBox, GTK_WIDGET(m_pImage));
gtk_widget_set_halign(m_pLabel, GTK_ALIGN_START);
#endif
gtk_widget_show(GTK_WIDGET(m_pImage));
}
static void signalFlagsChanged(GtkToggleButton* pToggleButton, GtkStateFlags flags, gpointer widget)
{
GtkInstanceMenuButton* pThis = static_cast<GtkInstanceMenuButton*>(widget);
bool bOldChecked = flags & GTK_STATE_FLAG_CHECKED;
bool bNewChecked = gtk_widget_get_state_flags(GTK_WIDGET(pToggleButton)) & GTK_STATE_FLAG_CHECKED;
if (bOldChecked == bNewChecked)
return;
if (bOldChecked && gtk_widget_get_focus_on_click(GTK_WIDGET(pToggleButton)))
{
// grab focus back to the toggle button if the menu was popped down
gtk_widget_grab_focus(GTK_WIDGET(pToggleButton));
}
SolarMutexGuard aGuard;
pThis->signal_toggled();
}
public:
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkInstanceMenuButton(GtkMenuButton* pMenuButton, GtkWidget* pMenuAlign, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceToggleButton(GTK_TOGGLE_BUTTON(pMenuButton), pBuilder, bTakeOwnership)
, MenuHelper(gtk_menu_button_get_popup(pMenuButton), false)
#else
GtkInstanceMenuButton(GtkMenuButton* pMenuButton, GtkWidget* pMenuAlign, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pMenuButton), pBuilder, bTakeOwnership)
, MenuHelper(GTK_POPOVER_MENU(gtk_menu_button_get_popover(pMenuButton)), false)
#endif
, m_pMenuButton(pMenuButton)
, m_pImage(nullptr)
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_pMenuHack(nullptr)
, m_pMenuHackAlign(pMenuAlign)
, m_nButtonPressSeen(true)
, m_nSignalId(0)
#endif
, m_pPopover(nullptr)
#if GTK_CHECK_VERSION(4, 0, 0)
, m_aCustomBackground(GTK_WIDGET(pMenuButton))
#endif
{
#if !GTK_CHECK_VERSION(4, 0, 0)
// tdf#142924 "toggled" is too late to use to populate changes to the menu,
// so use "state-flag-changed" on GTK_STATE_FLAG_CHECKED instead which
// happens before "toggled"
g_signal_handler_disconnect(m_pToggleButton, m_nToggledSignalId);
m_nToggledSignalId = g_signal_connect(m_pToggleButton, "state-flags-changed", G_CALLBACK(signalFlagsChanged), this);
m_pLabel = gtk_bin_get_child(GTK_BIN(m_pMenuButton));
m_pImage = get_image_widget(GTK_WIDGET(m_pMenuButton));
m_pBox = formatMenuButton(m_pLabel);
#else
GtkWidget* pToggleButton = gtk_widget_get_first_child(GTK_WIDGET(m_pMenuButton));
assert(GTK_IS_TOGGLE_BUTTON(pToggleButton));
m_pMenuButtonToggleButton = GTK_TOGGLE_BUTTON(pToggleButton);
m_nToggledSignalId = g_signal_connect(m_pMenuButtonToggleButton, "state-flags-changed", G_CALLBACK(signalFlagsChanged), this);
GtkWidget* pChild = gtk_button_get_child(GTK_BUTTON(pToggleButton));
m_pBox = GTK_IS_BOX(pChild) ? GTK_BOX(pChild) : nullptr;
m_pLabel = m_pBox ? gtk_widget_get_first_child(GTK_WIDGET(m_pBox)) : nullptr;
(void)pMenuAlign;
#endif
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_insert_action_group(GTK_WIDGET(m_pMenuButton), "menu", m_pActionGroup);
update_action_group_from_popover_model();
#endif
}
virtual void set_size_request(int nWidth, int nHeight) override
{
// tweak the label to get a narrower size to stick
if (GTK_IS_LABEL(m_pLabel))
gtk_label_set_ellipsize(GTK_LABEL(m_pLabel), PANGO_ELLIPSIZE_MIDDLE);
gtk_widget_set_size_request(m_pWidget, nWidth, nHeight);
}
virtual void set_label(const OUString& rText) override
{
::set_label(GTK_LABEL(m_pLabel), rText);
}
virtual OUString get_label() const override
{
return ::get_label(GTK_LABEL(m_pLabel));
}
virtual void set_image(VirtualDevice* pDevice) override
{
ensure_image_widget();
#if GTK_CHECK_VERSION(4, 0, 0)
picture_set_from_virtual_device(m_pImage, pDevice);
#else
image_set_from_virtual_device(m_pImage, pDevice);
#endif
}
virtual void set_image(const css::uno::Reference<css::graphic::XGraphic>& rImage) override
{
ensure_image_widget();
#if GTK_CHECK_VERSION(4, 0, 0)
picture_set_from_xgraphic(m_pImage, rImage);
#else
image_set_from_xgraphic(m_pImage, rImage);
#endif
}
#if GTK_CHECK_VERSION(4, 0, 0)
virtual void set_from_icon_name(const OUString& rIconName) override
{
ensure_image_widget();
picture_set_from_icon_name(m_pImage, rIconName);
}
virtual void set_custom_button(VirtualDevice* pDevice) override
{
m_aCustomBackground.use_custom_content(pDevice);
}
virtual void set_inconsistent(bool inconsistent) override
{
if (inconsistent)
gtk_widget_set_state_flags(GTK_WIDGET(m_pMenuButton), GTK_STATE_FLAG_INCONSISTENT, false);
else
gtk_widget_unset_state_flags(GTK_WIDGET(m_pMenuButton), GTK_STATE_FLAG_INCONSISTENT);
}
virtual bool get_inconsistent() const override
{
return gtk_widget_get_state_flags(GTK_WIDGET(m_pMenuButton)) & GTK_STATE_FLAG_INCONSISTENT;
}
virtual void set_active(bool active) override
{
disable_notify_events();
set_inconsistent(false);
if (active)
gtk_menu_button_popup(m_pMenuButton);
else
gtk_menu_button_popdown(m_pMenuButton);
enable_notify_events();
}
virtual bool get_active() const override
{
GtkPopover* pPopover = gtk_menu_button_get_popover(m_pMenuButton);
return pPopover && gtk_widget_get_visible(GTK_WIDGET(pPopover));
}
virtual void set_font(const vcl::Font& rFont) override
{
m_xFont = rFont;
GtkLabel* pChild = ::get_label_widget(GTK_WIDGET(m_pMenuButton));
::set_font(pChild, rFont);
}
virtual vcl::Font get_font() override
{
if (m_xFont)
return *m_xFont;
return GtkInstanceWidget::get_font();
}
#else
virtual void set_active(bool bActive) override
{
bool bWasActive = get_active();
GtkInstanceToggleButton::set_active(bActive);
if (bWasActive && !bActive && gtk_widget_get_focus_on_click(GTK_WIDGET(m_pMenuButton)))
{
// grab focus back to the toggle button if the menu was popped down
gtk_widget_grab_focus(GTK_WIDGET(m_pMenuButton));
}
}
#endif
virtual void insert_item(int pos, const OUString& rId, const OUString& rStr,
const OUString* pIconName, VirtualDevice* pImageSurface, TriState eCheckRadioFalse) override
{
MenuHelper::insert_item(pos, rId, rStr, pIconName, pImageSurface, eCheckRadioFalse);
}
virtual void insert_separator(int pos, const OUString& rId) override
{
MenuHelper::insert_separator(pos, rId);
}
virtual void remove_item(const OUString& rId) override
{
MenuHelper::remove_item(rId);
}
virtual void clear() override
{
MenuHelper::clear_items();
}
virtual void set_item_active(const OUString& rIdent, bool bActive) override
{
MenuHelper::set_item_active(rIdent, bActive);
}
virtual void set_item_sensitive(const OUString& rIdent, bool bSensitive) override
{
MenuHelper::set_item_sensitive(rIdent, bSensitive);
}
virtual void set_item_label(const OUString& rIdent, const OUString& rLabel) override
{
MenuHelper::set_item_label(rIdent, rLabel);
}
virtual OUString get_item_label(const OUString& rIdent) const override
{
return MenuHelper::get_item_label(rIdent);
}
virtual void set_item_visible(const OUString& rIdent, bool bVisible) override
{
MenuHelper::set_item_visible(rIdent, bVisible);
}
virtual void signal_item_activate(const OUString& rIdent) override
{
signal_selected(rIdent);
}
virtual void set_popover(weld::Widget* pPopover) override
{
GtkInstanceWidget* pPopoverWidget = dynamic_cast<GtkInstanceWidget*>(pPopover);
m_pPopover = pPopoverWidget ? pPopoverWidget->getWidget() : nullptr;
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_menu_button_set_popover(m_pMenuButton, m_pPopover);
update_action_group_from_popover_model();
return;
#else
if (!m_pPopover)
{
gtk_menu_button_set_popover(m_pMenuButton, nullptr);
return;
}
if (!m_pMenuHack)
{
//under wayland a Popover will work to "escape" the parent dialog, not
//so under X, so come up with this hack to use a raw GtkWindow
GdkDisplay *pDisplay = gtk_widget_get_display(m_pWidget);
if (DLSYM_GDK_IS_X11_DISPLAY(pDisplay) && gtk_popover_get_constrain_to(GTK_POPOVER(m_pPopover)) == GTK_POPOVER_CONSTRAINT_NONE)
{
m_pMenuHack = GTK_WINDOW(gtk_window_new(GTK_WINDOW_POPUP));
gtk_window_set_type_hint(m_pMenuHack, GDK_WINDOW_TYPE_HINT_COMBO);
// See writer "format, watermark" for true here. Can't interact with the replacement popover otherwise.
gtk_window_set_modal(m_pMenuHack, true);
gtk_window_set_resizable(m_pMenuHack, false);
m_nSignalId = g_signal_connect(GTK_TOGGLE_BUTTON(m_pMenuButton), "toggled", G_CALLBACK(signalMenuButtonToggled), this);
g_signal_connect(m_pMenuHack, "key-press-event", G_CALLBACK(keyPress), this);
g_signal_connect(m_pMenuHack, "grab-broken-event", G_CALLBACK(signalGrabBroken), this);
g_signal_connect(m_pMenuHack, "button-press-event", G_CALLBACK(signalButtonPress), this);
g_signal_connect(m_pMenuHack, "button-release-event", G_CALLBACK(signalButtonRelease), this);
}
}
if (m_pMenuHack)
{
GtkWidget* pPlaceHolder = gtk_popover_new(GTK_WIDGET(m_pMenuButton));
gtk_popover_set_transitions_enabled(GTK_POPOVER(pPlaceHolder), false);
// tdf#132540 theme the unwanted popover into invisibility
GtkStyleContext *pPopoverContext = gtk_widget_get_style_context(pPlaceHolder);
GtkCssProvider *pProvider = gtk_css_provider_new();
static const gchar data[] = "popover { box-shadow: none; padding: 0 0 0 0; margin: 0 0 0 0; border-image: none; border-image-width: 0 0 0 0; background-image: none; background-color: transparent; border-radius: 0 0 0 0; border-width: 0 0 0 0; border-style: none; border-color: transparent; opacity: 0; min-height: 0; min-width: 0; }";
css_provider_load_from_data(pProvider, data, -1);
gtk_style_context_add_provider(pPopoverContext, GTK_STYLE_PROVIDER(pProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_menu_button_set_popover(m_pMenuButton, pPlaceHolder);
}
else
{
gtk_menu_button_set_popover(m_pMenuButton, m_pPopover);
gtk_widget_show_all(m_pPopover);
}
#endif
}
void set_menu(weld::Menu* pMenu);
static GtkBox* formatMenuButton(GtkWidget* pLabel)
{
// format the GtkMenuButton "manually" so we can have the dropdown image in GtkMenuButtons shown
// on the right at the same time as an image is shown on the left
g_object_ref(pLabel);
GtkWidget* pContainer = gtk_widget_get_parent(pLabel);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_remove(GTK_CONTAINER(pContainer), pLabel);
#else
gtk_box_remove(GTK_BOX(pContainer), pLabel);
#endif
gint nImageSpacing(2);
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkStyleContext *pContext = gtk_widget_get_style_context(pContainer);
gtk_style_context_get_style(pContext, "image-spacing", &nImageSpacing, nullptr);
#endif
GtkBox* pBox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, nImageSpacing));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_box_pack_start(pBox, pLabel, true, true, 0);
#else
gtk_widget_set_halign(pLabel, GTK_ALIGN_START);
gtk_box_prepend(pBox, pLabel);
#endif
g_object_unref(pLabel);
#if !GTK_CHECK_VERSION(4, 0, 0)
if (gtk_toggle_button_get_mode(GTK_TOGGLE_BUTTON(pContainer)))
gtk_box_pack_end(pBox, gtk_image_new_from_icon_name("pan-down-symbolic", GTK_ICON_SIZE_BUTTON), false, false, 0);
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_add(GTK_CONTAINER(pContainer), GTK_WIDGET(pBox));
#else
gtk_box_prepend(GTK_BOX(pContainer), GTK_WIDGET(pBox));
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_show_all(GTK_WIDGET(pBox));
#else
gtk_widget_show(GTK_WIDGET(pBox));
#endif
return pBox;
}
#if GTK_CHECK_VERSION(4, 0, 0)
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pMenuButtonToggleButton, m_nToggledSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pMenuButtonToggleButton, m_nToggledSignalId);
}
#endif
virtual ~GtkInstanceMenuButton() override
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pMenuButtonToggleButton, m_nToggledSignalId);
gtk_widget_insert_action_group(GTK_WIDGET(m_pMenuButton), "menu", nullptr);
#else
if (m_pMenuHack)
{
g_signal_handler_disconnect(m_pMenuButton, m_nSignalId);
gtk_menu_button_set_popover(m_pMenuButton, nullptr);
gtk_widget_destroy(GTK_WIDGET(m_pMenuHack));
}
#endif
}
};
class GtkInstanceMenuToggleButton : public GtkInstanceToggleButton, public MenuHelper
, public virtual weld::MenuToggleButton
{
private:
GtkBox* m_pContainer;
GtkButton* m_pToggleMenuButton;
GtkMenuButton* m_pMenuButton;
gulong m_nMenuBtnClickedId;
gulong m_nToggleStateFlagsChangedId;
gulong m_nMenuBtnStateFlagsChangedId;
static void signalToggleStateFlagsChanged(GtkWidget* pWidget, GtkStateFlags /*eFlags*/, gpointer widget)
{
GtkInstanceMenuToggleButton* pThis = static_cast<GtkInstanceMenuToggleButton*>(widget);
// mirror togglebutton state to menubutton
gtk_widget_set_state_flags(GTK_WIDGET(pThis->m_pToggleMenuButton), gtk_widget_get_state_flags(pWidget), true);
}
static void signalMenuBtnStateFlagsChanged(GtkWidget* pWidget, GtkStateFlags /*eFlags*/, gpointer widget)
{
GtkInstanceMenuToggleButton* pThis = static_cast<GtkInstanceMenuToggleButton*>(widget);
// mirror menubutton to togglebutton, keeping depressed state of menubutton
GtkStateFlags eToggleFlags = gtk_widget_get_state_flags(GTK_WIDGET(pThis->m_pToggleButton));
GtkStateFlags eFlags = gtk_widget_get_state_flags(pWidget);
GtkStateFlags eFinalFlags = static_cast<GtkStateFlags>((eFlags & ~GTK_STATE_FLAG_ACTIVE) |
(eToggleFlags & GTK_STATE_FLAG_ACTIVE));
gtk_widget_set_state_flags(GTK_WIDGET(pThis->m_pToggleButton), eFinalFlags, true);
}
static void signalMenuBtnClicked(GtkButton*, gpointer widget)
{
GtkInstanceMenuToggleButton* pThis = static_cast<GtkInstanceMenuToggleButton*>(widget);
pThis->launch_menu();
}
void launch_menu()
{
gtk_widget_set_state_flags(GTK_WIDGET(m_pToggleMenuButton), gtk_widget_get_state_flags(GTK_WIDGET(m_pToggleButton)), true);
GtkWidget* pWidget = GTK_WIDGET(m_pToggleButton);
//run in a sub main loop because we need to keep vcl PopupMenu alive to use
//it during DispatchCommand, returning now to the outer loop causes the
//launching PopupMenu to be destroyed, instead run the subloop here
//until the gtk menu is destroyed
GMainLoop* pLoop = g_main_loop_new(nullptr, true);
#if GTK_CHECK_VERSION(4, 0, 0)
gulong nSignalId = g_signal_connect_swapped(G_OBJECT(m_pMenu), "closed", G_CALLBACK(g_main_loop_quit), pLoop);
g_object_ref(m_pMenu);
gtk_menu_button_set_popover(m_pMenuButton, nullptr);
gtk_widget_set_parent(GTK_WIDGET(m_pMenu), pWidget);
gtk_popover_set_position(GTK_POPOVER(m_pMenu), GTK_POS_BOTTOM);
gtk_popover_popup(GTK_POPOVER(m_pMenu));
#else
gulong nSignalId = g_signal_connect_swapped(G_OBJECT(m_pMenu), "deactivate", G_CALLBACK(g_main_loop_quit), pLoop);
#if GTK_CHECK_VERSION(3,22,0)
if (gtk_check_version(3, 22, 0) == nullptr)
{
// Send a keyboard event through gtk_main_do_event to toggle any active tooltip offs
// before trying to launch the menu
// https://gitlab.gnome.org/GNOME/gtk/issues/1785
// Fixed in GTK 2.34
GdkEvent *pKeyEvent = GtkSalFrame::makeFakeKeyPress(pWidget);
gtk_main_do_event(pKeyEvent);
GdkEvent *pTriggerEvent = gtk_get_current_event();
if (!pTriggerEvent)
pTriggerEvent = pKeyEvent;
gtk_menu_popup_at_widget(m_pMenu, pWidget, GDK_GRAVITY_SOUTH_WEST, GDK_GRAVITY_NORTH_WEST, pTriggerEvent);
gdk_event_free(pKeyEvent);
}
else
#endif
{
guint nButton;
guint32 nTime;
//typically there is an event, and we can then distinguish if this was
//launched from the keyboard (gets auto-mnemoniced) or the mouse (which
//doesn't)
GdkEvent *pEvent = gtk_get_current_event();
if (pEvent)
{
gdk_event_get_button(pEvent, &nButton);
nTime = gdk_event_get_time(pEvent);
}
else
{
nButton = 0;
nTime = GtkSalFrame::GetLastInputEventTime();
}
gtk_menu_popup(m_pMenu, nullptr, nullptr, nullptr, nullptr, nButton, nTime);
}
#endif
if (g_main_loop_is_running(pLoop))
main_loop_run(pLoop);
g_main_loop_unref(pLoop);
g_signal_handler_disconnect(m_pMenu, nSignalId);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_unparent(GTK_WIDGET(m_pMenu));
gtk_menu_button_set_popover(m_pMenuButton, GTK_WIDGET(m_pMenu));
g_object_unref(m_pMenu);
#endif
}
static gboolean signalMenuToggleButton(GtkWidget*, gboolean bGroupCycling, gpointer widget)
{
GtkInstanceMenuToggleButton* pThis = static_cast<GtkInstanceMenuToggleButton*>(widget);
return gtk_widget_mnemonic_activate(GTK_WIDGET(pThis->m_pToggleButton), bGroupCycling);
}
public:
GtkInstanceMenuToggleButton(GtkBuilder* pMenuToggleButtonBuilder, GtkMenuButton* pMenuButton,
GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceToggleButton(GTK_TOGGLE_BUTTON(gtk_builder_get_object(pMenuToggleButtonBuilder, "togglebutton")),
pBuilder, bTakeOwnership)
#if !GTK_CHECK_VERSION(4, 0, 0)
, MenuHelper(gtk_menu_button_get_popup(pMenuButton), false)
#else
, MenuHelper(GTK_POPOVER_MENU(gtk_menu_button_get_popover(pMenuButton)), false)
#endif
, m_pContainer(GTK_BOX(gtk_builder_get_object(pMenuToggleButtonBuilder, "box")))
, m_pToggleMenuButton(GTK_BUTTON(gtk_builder_get_object(pMenuToggleButtonBuilder, "menubutton")))
, m_pMenuButton(pMenuButton)
, m_nMenuBtnClickedId(g_signal_connect(m_pToggleMenuButton, "clicked", G_CALLBACK(signalMenuBtnClicked), this))
, m_nToggleStateFlagsChangedId(g_signal_connect(m_pToggleButton, "state-flags-changed", G_CALLBACK(signalToggleStateFlagsChanged), this))
, m_nMenuBtnStateFlagsChangedId(g_signal_connect(m_pToggleMenuButton, "state-flags-changed", G_CALLBACK(signalMenuBtnStateFlagsChanged), this))
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkInstanceMenuButton::formatMenuButton(gtk_bin_get_child(GTK_BIN(m_pMenuButton)));
#endif
insertAsParent(GTK_WIDGET(m_pMenuButton), GTK_WIDGET(m_pContainer));
gtk_widget_hide(GTK_WIDGET(m_pMenuButton));
// move the first GtkMenuButton child, as created by GtkInstanceMenuButton ctor, into the GtkToggleButton
// instead, leaving just the indicator behind in the GtkMenuButton
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* pButtonBox = gtk_bin_get_child(GTK_BIN(m_pMenuButton));
GList* pChildren = gtk_container_get_children(GTK_CONTAINER(pButtonBox));
int nGroup = 0;
for (GList* pChild = g_list_first(pChildren); pChild && nGroup < 2; pChild = g_list_next(pChild), ++nGroup)
{
GtkWidget* pWidget = static_cast<GtkWidget*>(pChild->data);
g_object_ref(pWidget);
gtk_container_remove(GTK_CONTAINER(pButtonBox), pWidget);
if (nGroup == 0)
gtk_container_add(GTK_CONTAINER(m_pToggleButton), pWidget);
else
gtk_container_add(GTK_CONTAINER(m_pToggleMenuButton), pWidget);
gtk_widget_show_all(pWidget);
g_object_unref(pWidget);
}
g_list_free(pChildren);
#else
GtkWidget* pChild;
if (gtk_check_version(4, 5, 0) == nullptr)
{
pChild = gtk_widget_get_first_child(GTK_WIDGET(m_pMenuButton));
pChild = gtk_widget_get_first_child(pChild);
pChild = gtk_widget_get_first_child(pChild);
}
else
pChild = gtk_widget_get_last_child(GTK_WIDGET(m_pMenuButton));
g_object_ref(pChild);
gtk_widget_unparent(pChild);
gtk_button_set_child(GTK_BUTTON(m_pToggleButton), pChild);
g_object_unref(pChild);
#endif
// match the GtkToggleButton relief to the GtkMenuButton
#if !GTK_CHECK_VERSION(4, 0, 0)
const GtkReliefStyle eStyle = gtk_button_get_relief(GTK_BUTTON(m_pMenuButton));
gtk_button_set_relief(GTK_BUTTON(m_pToggleButton), eStyle);
gtk_button_set_relief(GTK_BUTTON(m_pToggleMenuButton), eStyle);
#else
const bool bStyle = gtk_menu_button_get_has_frame(GTK_MENU_BUTTON(m_pMenuButton));
gtk_button_set_has_frame(GTK_BUTTON(m_pToggleButton), bStyle);
gtk_button_set_has_frame(GTK_BUTTON(m_pToggleMenuButton), bStyle);
#endif
// move the GtkMenuButton margins up to the new parent
gtk_widget_set_margin_top(GTK_WIDGET(m_pContainer),
gtk_widget_get_margin_top(GTK_WIDGET(m_pMenuButton)));
gtk_widget_set_margin_bottom(GTK_WIDGET(m_pContainer),
gtk_widget_get_margin_bottom(GTK_WIDGET(m_pMenuButton)));
gtk_widget_set_margin_start(GTK_WIDGET(m_pContainer),
gtk_widget_get_margin_start(GTK_WIDGET(m_pMenuButton)));
gtk_widget_set_margin_end(GTK_WIDGET(m_pContainer),
gtk_widget_get_margin_end(GTK_WIDGET(m_pMenuButton)));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_menu_detach(m_pMenu);
gtk_menu_attach_to_widget(m_pMenu, GTK_WIDGET(m_pToggleButton), nullptr);
#else
gtk_widget_insert_action_group(GTK_WIDGET(m_pContainer), "menu", m_pActionGroup);
update_action_group_from_popover_model();
#endif
g_signal_connect(m_pContainer, "mnemonic-activate", G_CALLBACK(signalMenuToggleButton), this);
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pToggleMenuButton, m_nMenuBtnClickedId);
GtkInstanceToggleButton::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceToggleButton::enable_notify_events();
g_signal_handler_unblock(m_pToggleMenuButton, m_nMenuBtnClickedId);
}
virtual ~GtkInstanceMenuToggleButton()
{
g_signal_handler_disconnect(m_pToggleButton, m_nToggleStateFlagsChangedId);
g_signal_handler_disconnect(m_pToggleMenuButton, m_nMenuBtnStateFlagsChangedId);
g_signal_handler_disconnect(m_pToggleMenuButton, m_nMenuBtnClickedId);
#if GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* pChild = gtk_button_get_child(GTK_BUTTON(m_pToggleButton));
g_object_ref(pChild);
gtk_button_set_child(GTK_BUTTON(m_pToggleButton), nullptr);
gtk_widget_unparent(pChild);
gtk_widget_set_parent(pChild, GTK_WIDGET(m_pMenuButton));
g_object_unref(pChild);
#endif
}
virtual void insert_item(int pos, const OUString& rId, const OUString& rStr,
const OUString* pIconName, VirtualDevice* pImageSurface, TriState eCheckRadioFalse) override
{
MenuHelper::insert_item(pos, rId, rStr, pIconName, pImageSurface, eCheckRadioFalse);
}
virtual void insert_separator(int pos, const OUString& rId) override
{
MenuHelper::insert_separator(pos, rId);
}
virtual void remove_item(const OUString& rId) override
{
MenuHelper::remove_item(rId);
}
virtual void clear() override
{
MenuHelper::clear_items();
}
virtual void set_item_active(const OUString& rIdent, bool bActive) override
{
MenuHelper::set_item_active(rIdent, bActive);
}
virtual void set_item_sensitive(const OUString& rIdent, bool bSensitive) override
{
MenuHelper::set_item_sensitive(rIdent, bSensitive);
}
virtual void set_item_label(const OUString& rIdent, const OUString& rLabel) override
{
MenuHelper::set_item_label(rIdent, rLabel);
}
virtual OUString get_item_label(const OUString& rIdent) const override
{
return MenuHelper::get_item_label(rIdent);
}
virtual void set_item_visible(const OUString& rIdent, bool bVisible) override
{
MenuHelper::set_item_visible(rIdent, bVisible);
}
virtual void signal_item_activate(const OUString& rIdent) override
{
signal_selected(rIdent);
}
virtual void set_popover(weld::Widget* /*pPopover*/) override
{
assert(false && "not implemented");
}
};
class GtkInstanceMenu : public MenuHelper, public virtual weld::Menu
{
protected:
#if !GTK_CHECK_VERSION(4, 0, 0)
std::vector<GtkMenuItem*> m_aExtraItems;
#endif
OUString m_sActivated;
#if !GTK_CHECK_VERSION(4, 0, 0)
MenuHelper* m_pTopLevelMenuHelper;
#endif
private:
virtual void signal_item_activate(const OUString& rIdent) override
{
m_sActivated = rIdent;
weld::Menu::signal_activate(m_sActivated);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
void clear_extras()
{
if (m_aExtraItems.empty())
return;
if (m_pTopLevelMenuHelper)
{
for (auto a : m_aExtraItems)
m_pTopLevelMenuHelper->remove_from_map(a);
}
m_aExtraItems.clear();
}
#endif
public:
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkInstanceMenu(GtkMenu* pMenu, bool bTakeOwnership)
#else
GtkInstanceMenu(GtkPopoverMenu* pMenu, bool bTakeOwnership)
#endif
: MenuHelper(pMenu, bTakeOwnership)
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_pTopLevelMenuHelper(nullptr)
#endif
{
g_object_set_data(G_OBJECT(m_pMenu), "g-lo-GtkInstanceMenu", this);
#if !GTK_CHECK_VERSION(4, 0, 0)
// tdf#122527 if we're welding a submenu of a menu of a MenuButton,
// then find that MenuButton parent so that when adding items to this
// menu we can inform the MenuButton of their addition
GtkMenu* pTopLevelMenu = pMenu;
while (true)
{
GtkWidget* pAttached = gtk_menu_get_attach_widget(pTopLevelMenu);
if (!pAttached || !GTK_IS_MENU_ITEM(pAttached))
break;
GtkWidget* pParent = gtk_widget_get_parent(pAttached);
if (!pParent || !GTK_IS_MENU(pParent))
break;
pTopLevelMenu = GTK_MENU(pParent);
}
if (pTopLevelMenu == pMenu)
return;
// maybe the toplevel is a menubutton
GtkWidget* pAttached = gtk_menu_get_attach_widget(pTopLevelMenu);
if (pAttached && GTK_IS_MENU_BUTTON(pAttached))
{
void* pData = g_object_get_data(G_OBJECT(pAttached), "g-lo-GtkInstanceButton");
m_pTopLevelMenuHelper = dynamic_cast<GtkInstanceMenuButton*>(static_cast<GtkInstanceButton*>(pData));
}
// or maybe a menu
if (!m_pTopLevelMenuHelper)
{
void* pData = g_object_get_data(G_OBJECT(pTopLevelMenu), "g-lo-GtkInstanceMenu");
m_pTopLevelMenuHelper = static_cast<GtkInstanceMenu*>(pData);
}
#else
update_action_group_from_popover_model();
#endif
}
virtual OUString popup_at_rect(weld::Widget* pParent, const tools::Rectangle& rRect, weld::Placement ePlace) override
{
m_sActivated.clear();
GtkInstanceWidget* pGtkWidget = dynamic_cast<GtkInstanceWidget*>(pParent);
assert(pGtkWidget);
GtkWidget* pWidget = pGtkWidget->getWidget();
//run in a sub main loop because we need to keep vcl PopupMenu alive to use
//it during DispatchCommand, returning now to the outer loop causes the
//launching PopupMenu to be destroyed, instead run the subloop here
//until the gtk menu is destroyed
GMainLoop* pLoop = g_main_loop_new(nullptr, true);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_insert_action_group(pWidget, "menu", m_pActionGroup);
gulong nSignalId = g_signal_connect_swapped(G_OBJECT(m_pMenu), "closed", G_CALLBACK(g_main_loop_quit), pLoop);
GdkRectangle aRect;
pWidget = getPopupRect(pWidget, rRect, aRect);
GtkWidget* pOrigParent = gtk_widget_get_parent(GTK_WIDGET(m_pMenu));
gtk_widget_set_parent(GTK_WIDGET(m_pMenu), pWidget);
gtk_popover_set_pointing_to(GTK_POPOVER(m_pMenu), &aRect);
if (ePlace == weld::Placement::Under)
gtk_popover_set_position(GTK_POPOVER(m_pMenu), GTK_POS_BOTTOM);
else
{
if (SwapForRTL(pWidget))
gtk_popover_set_position(GTK_POPOVER(m_pMenu), GTK_POS_LEFT);
else
gtk_popover_set_position(GTK_POPOVER(m_pMenu), GTK_POS_RIGHT);
}
gtk_popover_popup(GTK_POPOVER(m_pMenu));
#else
gulong nSignalId = g_signal_connect_swapped(G_OBJECT(m_pMenu), "deactivate", G_CALLBACK(g_main_loop_quit), pLoop);
#if GTK_CHECK_VERSION(3,22,0)
if (gtk_check_version(3, 22, 0) == nullptr)
{
GdkRectangle aRect;
pWidget = getPopupRect(pWidget, rRect, aRect);
gtk_menu_attach_to_widget(m_pMenu, pWidget, nullptr);
// Send a keyboard event through gtk_main_do_event to toggle any active tooltip offs
// before trying to launch the menu
// https://gitlab.gnome.org/GNOME/gtk/issues/1785
// Fixed in GTK 2.34
GdkEvent *pKeyEvent = GtkSalFrame::makeFakeKeyPress(pWidget);
gtk_main_do_event(pKeyEvent);
GdkEvent *pTriggerEvent = gtk_get_current_event();
if (!pTriggerEvent)
pTriggerEvent = pKeyEvent;
bool bSwapForRTL = SwapForRTL(pWidget);
if (ePlace == weld::Placement::Under)
{
if (bSwapForRTL)
gtk_menu_popup_at_rect(m_pMenu, widget_get_surface(pWidget), &aRect, GDK_GRAVITY_SOUTH_EAST, GDK_GRAVITY_NORTH_EAST, pTriggerEvent);
else
gtk_menu_popup_at_rect(m_pMenu, widget_get_surface(pWidget), &aRect, GDK_GRAVITY_SOUTH_WEST, GDK_GRAVITY_NORTH_WEST, pTriggerEvent);
}
else
{
if (bSwapForRTL)
gtk_menu_popup_at_rect(m_pMenu, widget_get_surface(pWidget), &aRect, GDK_GRAVITY_NORTH_WEST, GDK_GRAVITY_NORTH_EAST, pTriggerEvent);
else
gtk_menu_popup_at_rect(m_pMenu, widget_get_surface(pWidget), &aRect, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_NORTH_WEST, pTriggerEvent);
}
gdk_event_free(pKeyEvent);
}
else
#else
(void) rRect;
#endif
{
gtk_menu_attach_to_widget(m_pMenu, pWidget, nullptr);
guint nButton;
guint32 nTime;
//typically there is an event, and we can then distinguish if this was
//launched from the keyboard (gets auto-mnemoniced) or the mouse (which
//doesn't)
GdkEvent *pEvent = gtk_get_current_event();
if (pEvent)
{
if (!gdk_event_get_button(pEvent, &nButton))
nButton = 0;
nTime = gdk_event_get_time(pEvent);
}
else
{
nButton = 0;
nTime = GtkSalFrame::GetLastInputEventTime();
}
gtk_menu_popup(m_pMenu, nullptr, nullptr, nullptr, nullptr, nButton, nTime);
}
#endif
if (g_main_loop_is_running(pLoop))
main_loop_run(pLoop);
g_main_loop_unref(pLoop);
g_signal_handler_disconnect(m_pMenu, nSignalId);
#if GTK_CHECK_VERSION(4, 0, 0)
if (!pOrigParent)
gtk_widget_unparent(GTK_WIDGET(m_pMenu));
else
gtk_widget_set_parent(GTK_WIDGET(m_pMenu), pOrigParent);
gtk_widget_insert_action_group(pWidget, "menu", nullptr);
#else
gtk_menu_detach(m_pMenu);
#endif
return m_sActivated;
}
virtual void set_sensitive(const OUString& rIdent, bool bSensitive) override
{
set_item_sensitive(rIdent, bSensitive);
}
virtual bool get_sensitive(const OUString& rIdent) const override
{
return get_item_sensitive(rIdent);
}
virtual void set_active(const OUString& rIdent, bool bActive) override
{
set_item_active(rIdent, bActive);
}
virtual bool get_active(const OUString& rIdent) const override
{
return get_item_active(rIdent);
}
virtual void set_visible(const OUString& rIdent, bool bShow) override
{
set_item_visible(rIdent, bShow);
}
virtual void set_label(const OUString& rIdent, const OUString& rLabel) override
{
set_item_label(rIdent, rLabel);
}
virtual OUString get_label(const OUString& rIdent) const override
{
return get_item_label(rIdent);
}
virtual void insert_separator(int pos, const OUString& rId) override
{
MenuHelper::insert_separator(pos, rId);
}
virtual void clear() override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
clear_extras();
#endif
MenuHelper::clear_items();
}
virtual void insert(int pos, const OUString& rId, const OUString& rStr,
const OUString* pIconName, VirtualDevice* pImageSurface,
const css::uno::Reference<css::graphic::XGraphic>& rGraphic,
TriState eCheckRadioFalse) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* pImage = nullptr;
if (pIconName)
pImage = image_new_from_icon_name(*pIconName);
else if (pImageSurface)
{
pImage = image_new_from_virtual_device(*pImageSurface);
}
else if (rGraphic)
{
pImage = image_new_from_xgraphic(rGraphic, false);
}
GtkWidget *pItem;
if (pImage)
{
GtkBox *pBox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6));
GtkWidget *pLabel = gtk_label_new_with_mnemonic(MapToGtkAccelerator(rStr).getStr());
gtk_label_set_xalign(GTK_LABEL(pLabel), 0.0);
pItem = eCheckRadioFalse != TRISTATE_INDET ? gtk_check_menu_item_new() : gtk_menu_item_new();
gtk_box_pack_start(pBox, pImage, false, true, 0);
gtk_box_pack_start(pBox, pLabel, true, true, 0);
gtk_container_add(GTK_CONTAINER(pItem), GTK_WIDGET(pBox));
gtk_widget_show_all(pItem);
}
else
{
pItem = eCheckRadioFalse != TRISTATE_INDET ? gtk_check_menu_item_new_with_mnemonic(MapToGtkAccelerator(rStr).getStr())
: gtk_menu_item_new_with_mnemonic(MapToGtkAccelerator(rStr).getStr());
}
if (eCheckRadioFalse == TRISTATE_FALSE)
gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(pItem), true);
::set_buildable_id(GTK_BUILDABLE(pItem), rId);
gtk_menu_shell_append(GTK_MENU_SHELL(m_pMenu), pItem);
gtk_widget_show(pItem);
GtkMenuItem* pMenuItem = GTK_MENU_ITEM(pItem);
m_aExtraItems.push_back(pMenuItem);
add_to_map(pMenuItem);
if (m_pTopLevelMenuHelper)
m_pTopLevelMenuHelper->add_to_map(pMenuItem);
if (pos != -1)
gtk_menu_reorder_child(m_pMenu, pItem, pos);
#else
SAL_WARN("vcl.gtk", "needs to be implemented for gtk4");
(void)pIconName;
(void)pImageSurface;
(void)rGraphic;
if (GMenuModel* pMenuModel = m_pMenu ? gtk_popover_menu_get_menu_model(m_pMenu) : nullptr)
{
auto aSectionAndPos = get_section_and_pos_for(pMenuModel, pos);
GMenu* pMenu = G_MENU(aSectionAndPos.first);
// action with a target value ... the action name and target value are separated by a double
// colon ... For example: "app.action::target"
OUString sActionAndTarget;
if (eCheckRadioFalse == TRISTATE_INDET)
sActionAndTarget = "menu.normal." + rId + "::" + rId;
else
sActionAndTarget = "menu.radio." + rId + "::" + rId;
g_menu_insert(pMenu, aSectionAndPos.second, MapToGtkAccelerator(rStr).getStr(), sActionAndTarget.toUtf8().getStr());
assert(eCheckRadioFalse == TRISTATE_INDET); // come back to this later
// TODO not redo entire group
update_action_group_from_popover_model();
}
#endif
}
virtual OUString get_id(int pos) const override
{
return get_item_id(pos);
}
virtual int n_children() const override
{
return get_n_children();
}
void remove(const OUString& rIdent) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!m_aExtraItems.empty())
{
GtkMenuItem* pMenuItem = m_aMap[rIdent];
auto iter = std::find(m_aExtraItems.begin(), m_aExtraItems.end(), pMenuItem);
if (iter != m_aExtraItems.end())
{
if (m_pTopLevelMenuHelper)
m_pTopLevelMenuHelper->remove_from_map(pMenuItem);
m_aExtraItems.erase(iter);
}
}
#endif
MenuHelper::remove_item(rIdent);
}
virtual ~GtkInstanceMenu() override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
clear_extras();
#endif
g_object_steal_data(G_OBJECT(m_pMenu), "g-lo-GtkInstanceMenu");
}
};
#if !GTK_CHECK_VERSION(4, 0, 0)
vcl::ImageType GtkToVcl(GtkIconSize eSize)
{
vcl::ImageType eRet;
switch (eSize)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
case GTK_ICON_SIZE_MENU:
case GTK_ICON_SIZE_SMALL_TOOLBAR:
case GTK_ICON_SIZE_BUTTON:
eRet = vcl::ImageType::Size16;
break;
case GTK_ICON_SIZE_LARGE_TOOLBAR:
eRet = vcl::ImageType::Size26;
break;
case GTK_ICON_SIZE_DND:
case GTK_ICON_SIZE_DIALOG:
eRet = vcl::ImageType::Size32;
break;
default:
case GTK_ICON_SIZE_INVALID:
eRet = vcl::ImageType::Small;
break;
#else
case GTK_ICON_SIZE_LARGE:
eRet = vcl::ImageType::Size32;
break;
case GTK_ICON_SIZE_NORMAL:
default:
eRet = vcl::ImageType::Size16;
break;
#endif
}
return eRet;
}
GtkIconSize VclToGtk(vcl::ImageType eSize)
{
GtkIconSize eRet;
#if !GTK_CHECK_VERSION(4, 0, 0)
switch (eSize)
{
case vcl::ImageType::Size16:
eRet = GTK_ICON_SIZE_SMALL_TOOLBAR;
break;
case vcl::ImageType::Size26:
eRet = GTK_ICON_SIZE_LARGE_TOOLBAR;
break;
case vcl::ImageType::Size32:
eRet = GTK_ICON_SIZE_DIALOG;
break;
default:
O3TL_UNREACHABLE;
}
#else
switch (eSize)
{
case vcl::ImageType::Size26:
case vcl::ImageType::Size32:
eRet = GTK_ICON_SIZE_LARGE;
break;
case vcl::ImageType::Size16:
default:
eRet = GTK_ICON_SIZE_NORMAL;
break;
}
#endif
return eRet;
}
// tdf#153885 for wayland if the popover window is the application
// window, constrain it within the application window so it won't
// be cut off screen. Leave dialog hosted ones alone, like
// format, watermark, which are likely presented in the middle
// of the screen and are too small to constrain the popover inside.
void ConstrainApplicationWindowPopovers(GtkToggleButton* pItem)
{
#if defined(GDK_WINDOWING_WAYLAND)
GdkDisplay *pDisplay = gtk_widget_get_display(GTK_WIDGET(pItem));
if (DLSYM_GDK_IS_WAYLAND_DISPLAY(pDisplay) && GTK_IS_MENU_BUTTON(pItem))
{
GtkMenuButton* pMenuButton = GTK_MENU_BUTTON(pItem);
if (GtkPopover* pPopover = gtk_menu_button_get_popover(pMenuButton))
{
if (gtk_popover_get_constrain_to(pPopover) == GTK_POPOVER_CONSTRAINT_NONE)
{
GtkWidget* pTopLevel = widget_get_toplevel(GTK_WIDGET(pItem));
GtkSalFrame* pFrame = pTopLevel ? GtkSalFrame::getFromWindow(pTopLevel) : nullptr;
if (pFrame)
{
// the toplevel is an application window
gtk_popover_set_constrain_to(pPopover, GTK_POPOVER_CONSTRAINT_WINDOW);
}
}
}
}
#else
(void)pItem;
#endif
}
#endif
}
void GtkInstanceMenuButton::set_menu(weld::Menu* pMenu)
{
GtkInstanceMenu* pPopoverWidget = dynamic_cast<GtkInstanceMenu*>(pMenu);
m_pPopover = nullptr;
m_pMenu = pPopoverWidget ? pPopoverWidget->getMenu() : nullptr;
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_menu_button_set_popup(m_pMenuButton, GTK_WIDGET(m_pMenu));
#else
gtk_menu_button_set_popover(m_pMenuButton, GTK_WIDGET(m_pMenu));
update_action_group_from_popover_model();
#endif
}
namespace {
class GtkInstanceToolbar : public GtkInstanceWidget, public virtual weld::Toolbar
{
private:
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkToolbar* m_pToolbar;
#else
GtkBox* m_pToolbar;
vcl::ImageType m_eImageType;
#endif
GtkCssProvider *m_pMenuButtonProvider;
std::map<OUString, GtkWidget*> m_aMap;
std::map<OUString, std::unique_ptr<GtkInstanceMenuButton>> m_aMenuButtonMap;
std::map<OUString, bool> m_aMirroredMap;
#if !GTK_CHECK_VERSION(4, 0, 0)
// at the time of writing there is no gtk_menu_tool_button_set_popover available
// though there will be in the future
// https://gitlab.gnome.org/GNOME/gtk/commit/03e30431a8af9a947a0c4ccab545f24da16bfe17?w=1
static void find_menu_button(GtkWidget *pWidget, gpointer user_data)
{
if (g_strcmp0(gtk_widget_get_name(pWidget), "GtkMenuButton") == 0)
{
GtkWidget **ppToggleButton = static_cast<GtkWidget**>(user_data);
*ppToggleButton = pWidget;
}
else if (GTK_IS_CONTAINER(pWidget))
gtk_container_forall(GTK_CONTAINER(pWidget), find_menu_button, user_data);
}
static void find_menupeer_button(GtkWidget *pWidget, gpointer user_data)
{
if (g_strcmp0(gtk_widget_get_name(pWidget), "GtkButton") == 0)
{
GtkWidget **ppButton = static_cast<GtkWidget**>(user_data);
*ppButton = pWidget;
}
else if (GTK_IS_CONTAINER(pWidget))
gtk_container_forall(GTK_CONTAINER(pWidget), find_menupeer_button, user_data);
}
#endif
static void collect(GtkWidget* pItem, gpointer widget)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!GTK_IS_TOOL_ITEM(pItem))
return;
#endif
GtkInstanceToolbar* pThis = static_cast<GtkInstanceToolbar*>(widget);
GtkMenuButton* pMenuButton = nullptr;
#if !GTK_CHECK_VERSION(4, 0, 0)
if (GTK_IS_MENU_TOOL_BUTTON(pItem))
find_menu_button(pItem, &pMenuButton);
#else
if (GTK_IS_MENU_BUTTON(pItem))
pMenuButton = GTK_MENU_BUTTON(pItem);
#endif
pThis->add_to_map(pItem, pMenuButton);
}
void add_to_map(GtkWidget* pToolItem, GtkMenuButton* pMenuButton)
{
OUString id = ::get_buildable_id(GTK_BUILDABLE(pToolItem));
m_aMap[id] = pToolItem;
if (pMenuButton)
{
m_aMenuButtonMap[id] = std::make_unique<GtkInstanceMenuButton>(pMenuButton, GTK_WIDGET(pToolItem), m_pBuilder, false);
// so that, e.g. with focus initially in writer main document then
// after clicking the heading menu in the writer navigator focus is
// left in the main document and not in the toolbar
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_button_set_focus_on_click(GTK_BUTTON(pMenuButton), false);
g_signal_connect(pMenuButton, "toggled", G_CALLBACK(signalItemToggled), this);
#else
gtk_widget_set_focus_on_click(GTK_WIDGET(pMenuButton), false);
GtkWidget* pToggleButton = gtk_widget_get_first_child(GTK_WIDGET(pMenuButton));
assert(GTK_IS_TOGGLE_BUTTON(pToggleButton));
g_signal_connect(pToggleButton, "toggled", G_CALLBACK(signalItemToggled), this);
#endif
// by default the GtkMenuButton down arrow button is as wide as
// a normal button and LibreOffice's original ones are very
// narrow, that assumption is fairly baked into the toolbar and
// sidebar designs, try and minimize the width of the dropdown
// zone.
GtkStyleContext *pButtonContext = gtk_widget_get_style_context(GTK_WIDGET(pMenuButton));
if (!m_pMenuButtonProvider)
{
m_pMenuButtonProvider = gtk_css_provider_new();
static const gchar data[] = "* { "
"padding: 0;"
"margin-left: 0px;"
"margin-right: 0px;"
"min-width: 4px;"
"}";
css_provider_load_from_data(m_pMenuButtonProvider, data, -1);
}
gtk_style_context_add_provider(pButtonContext,
GTK_STYLE_PROVIDER(m_pMenuButtonProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!GTK_IS_TOOL_BUTTON(pToolItem))
#else
if (!GTK_IS_BUTTON(pToolItem))
#endif
{
return;
}
g_signal_connect(pToolItem, "clicked", G_CALLBACK(signalItemClicked), this);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static void signalItemClicked(GtkToolButton* pItem, gpointer widget)
#else
static void signalItemClicked(GtkButton* pItem, gpointer widget)
#endif
{
GtkInstanceToolbar* pThis = static_cast<GtkInstanceToolbar*>(widget);
SolarMutexGuard aGuard;
pThis->signal_item_clicked(pItem);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
void signal_item_clicked(GtkToolButton* pItem)
#else
void signal_item_clicked(GtkButton* pItem)
#endif
{
signal_clicked(::get_buildable_id(GTK_BUILDABLE(pItem)));
}
static void signalItemToggled(GtkToggleButton* pItem, gpointer widget)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
ConstrainApplicationWindowPopovers(pItem);
#endif
GtkInstanceToolbar* pThis = static_cast<GtkInstanceToolbar*>(widget);
SolarMutexGuard aGuard;
pThis->signal_item_toggled(pItem);
}
void signal_item_toggled(GtkToggleButton* pItem)
{
for (const auto& a : m_aMenuButtonMap)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
if (a.second->getWidget() == GTK_WIDGET(pItem))
#else
if (a.second->getWidget() == gtk_widget_get_parent(GTK_WIDGET(pItem)))
#endif
{
signal_toggle_menu(a.first);
break;
}
}
}
#if GTK_CHECK_VERSION(4, 0, 0)
static void set_item_image(GtkWidget* pItem, GtkWidget* pImage)
{
if (GTK_IS_BUTTON(pItem))
gtk_button_set_child(GTK_BUTTON(pItem), pImage);
else if (GTK_IS_MENU_BUTTON(pItem))
{
// TODO after gtk 4.6 is released require that version and drop this
static auto menu_button_set_child = reinterpret_cast<void (*) (GtkMenuButton*, GtkWidget*)>(dlsym(nullptr, "gtk_menu_button_set_child"));
if (menu_button_set_child)
menu_button_set_child(GTK_MENU_BUTTON(pItem), pImage);
}
// versions of gtk4 > 4.2.1 might do this on their own
gtk_widget_remove_css_class(pItem, "text-button");
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
static void set_item_image(GtkToolButton* pItem, const css::uno::Reference<css::graphic::XGraphic>& rIcon, bool bMirror)
#else
static void set_item_image(GtkWidget* pItem, const css::uno::Reference<css::graphic::XGraphic>& rIcon, bool bMirror)
#endif
{
GtkWidget* pImage = image_new_from_xgraphic(rIcon, bMirror);
if (pImage)
gtk_widget_show(pImage);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_tool_button_set_icon_widget(pItem, pImage);
#else
set_item_image(pItem, pImage);
#endif
}
#if !GTK_CHECK_VERSION(4, 0, 0)
void set_item_image(GtkToolButton* pItem, const VirtualDevice* pDevice)
#else
void set_item_image(GtkWidget* pItem, const VirtualDevice* pDevice)
#endif
{
GtkWidget* pImage = nullptr;
if (pDevice)
{
#if GTK_CHECK_VERSION(4, 0, 0)
pImage = picture_new_from_virtual_device(*pDevice);
#else
pImage = image_new_from_virtual_device(*pDevice);
#endif
gtk_widget_show(pImage);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_tool_button_set_icon_widget(pItem, pImage);
#else
set_item_image(pItem, pImage);
#endif
gtk_widget_queue_draw(GTK_WIDGET(m_pToolbar));
}
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* toolbar_get_nth_item(int nIndex) const
{
return GTK_WIDGET(gtk_toolbar_get_nth_item(m_pToolbar, nIndex));
}
#else
GtkWidget* toolbar_get_nth_item(int nIndex) const
{
int i = 0;
for (GtkWidget* pChild = gtk_widget_get_first_child(GTK_WIDGET(m_pToolbar));
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
if (i == nIndex)
return pChild;
++i;
}
return nullptr;
}
#endif
public:
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkInstanceToolbar(GtkToolbar* pToolbar, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
#else
GtkInstanceToolbar(GtkBox* pToolbar, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
#endif
: GtkInstanceWidget(GTK_WIDGET(pToolbar), pBuilder, bTakeOwnership)
, m_pToolbar(pToolbar)
#if GTK_CHECK_VERSION(4, 0, 0)
, m_eImageType(vcl::ImageType::Size16)
#endif
, m_pMenuButtonProvider(nullptr)
{
#if GTK_CHECK_VERSION(4, 0, 0)
for (GtkWidget* pChild = gtk_widget_get_first_child(GTK_WIDGET(pToolbar));
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
collect(pChild, this);
}
#else
gtk_container_foreach(GTK_CONTAINER(pToolbar), collect, this);
#endif
}
void disable_item_notify_events()
{
for (auto& a : m_aMap)
{
g_signal_handlers_block_by_func(a.second, reinterpret_cast<void*>(signalItemClicked), this);
}
}
void enable_item_notify_events()
{
for (auto& a : m_aMap)
{
g_signal_handlers_unblock_by_func(a.second, reinterpret_cast<void*>(signalItemClicked), this);
}
}
virtual void set_item_sensitive(const OUString& rIdent, bool bSensitive) override
{
disable_item_notify_events();
gtk_widget_set_sensitive(GTK_WIDGET(m_aMap[rIdent]), bSensitive);
enable_item_notify_events();
}
virtual bool get_item_sensitive(const OUString& rIdent) const override
{
return gtk_widget_get_sensitive(GTK_WIDGET(m_aMap.find(rIdent)->second));
}
virtual void set_item_visible(const OUString& rIdent, bool bVisible) override
{
disable_item_notify_events();
gtk_widget_set_visible(GTK_WIDGET(m_aMap[rIdent]), bVisible);
enable_item_notify_events();
}
virtual void set_item_help_id(const OUString& rIdent, const OUString& rHelpId) override
{
::set_help_id(GTK_WIDGET(m_aMap[rIdent]), rHelpId);
}
virtual bool get_item_visible(const OUString& rIdent) const override
{
return gtk_widget_get_visible(GTK_WIDGET(m_aMap.find(rIdent)->second));
}
virtual void set_item_active(const OUString& rIdent, bool bActive) override
{
disable_item_notify_events();
GtkWidget* pToolButton = m_aMap.find(rIdent)->second;
#if !GTK_CHECK_VERSION(4, 0, 0)
if (GTK_IS_TOGGLE_TOOL_BUTTON(pToolButton))
gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(pToolButton), bActive);
else
{
GtkButton* pButton = nullptr;
// there is no GtkMenuToggleToolButton so abuse the CHECKED state of the GtkMenuToolButton button
// to emulate one
find_menupeer_button(GTK_WIDGET(pToolButton), &pButton);
if (pButton)
{
auto eState = gtk_widget_get_state_flags(GTK_WIDGET(pButton)) & ~GTK_STATE_FLAG_CHECKED;
if (bActive)
eState |= GTK_STATE_FLAG_CHECKED;
gtk_widget_set_state_flags(GTK_WIDGET(pButton), static_cast<GtkStateFlags>(eState), true);
}
}
#else
GtkWidget* pWidget;
if (GTK_IS_MENU_BUTTON(pToolButton))
{
pWidget = gtk_widget_get_first_child(pToolButton);
assert(GTK_IS_TOGGLE_BUTTON(pWidget));
}
else
pWidget = pToolButton;
auto eState = gtk_widget_get_state_flags(pWidget) & ~GTK_STATE_FLAG_CHECKED;
if (bActive)
eState |= GTK_STATE_FLAG_CHECKED;
gtk_widget_set_state_flags(pWidget, static_cast<GtkStateFlags>(eState), true);
#endif
enable_item_notify_events();
}
virtual bool get_item_active(const OUString& rIdent) const override
{
GtkWidget* pToolButton = m_aMap.find(rIdent)->second;
#if !GTK_CHECK_VERSION(4, 0, 0)
if (GTK_IS_TOGGLE_TOOL_BUTTON(pToolButton))
return gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(pToolButton));
else
{
GtkButton* pButton = nullptr;
// there is no GtkMenuToggleToolButton so abuse the CHECKED state of the GtkMenuToolButton button
// to emulate one
find_menupeer_button(GTK_WIDGET(pToolButton), &pButton);
if (pButton)
{
return gtk_widget_get_state_flags(GTK_WIDGET(pButton)) & GTK_STATE_FLAG_CHECKED;
}
}
#else
GtkWidget* pWidget;
if (GTK_IS_MENU_BUTTON(pToolButton))
{
pWidget = gtk_widget_get_first_child(pToolButton);
assert(GTK_IS_TOGGLE_BUTTON(pWidget));
}
else
pWidget = pToolButton;
return gtk_widget_get_state_flags(pWidget) & GTK_STATE_FLAG_CHECKED;
#endif
return false;
}
virtual void set_menu_item_active(const OUString& rIdent, bool bActive) override
{
disable_item_notify_events();
auto aFind = m_aMenuButtonMap.find(rIdent);
assert (aFind != m_aMenuButtonMap.end());
aFind->second->set_active(bActive);
enable_item_notify_events();
}
virtual bool get_menu_item_active(const OUString& rIdent) const override
{
auto aFind = m_aMenuButtonMap.find(rIdent);
assert (aFind != m_aMenuButtonMap.end());
return aFind->second->get_active();
}
virtual void insert_item(int pos, const OUString& rId) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkToolItem* pItem = gtk_tool_button_new(nullptr, OUStringToOString(rId, RTL_TEXTENCODING_UTF8).getStr());
#else
GtkWidget* pItem = gtk_button_new();
#endif
::set_buildable_id(GTK_BUILDABLE(pItem), rId);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_toolbar_insert(m_pToolbar, pItem, pos);
#else
gtk_box_insert_child_after(m_pToolbar, pItem, toolbar_get_nth_item(pos - 1));
#endif
gtk_widget_show(GTK_WIDGET(pItem));
add_to_map(GTK_WIDGET(pItem), nullptr);
}
virtual void insert_separator(int pos, const OUString& rId) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkToolItem* pItem = gtk_separator_tool_item_new();
#else
GtkWidget* pItem = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
#endif
::set_buildable_id(GTK_BUILDABLE(pItem), rId);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_toolbar_insert(m_pToolbar, pItem, pos);
#else
gtk_box_insert_child_after(m_pToolbar, pItem, toolbar_get_nth_item(pos - 1));
#endif
gtk_widget_show(GTK_WIDGET(pItem));
}
virtual void set_item_popover(const OUString& rIdent, weld::Widget* pPopover) override
{
m_aMenuButtonMap[rIdent]->set_popover(pPopover);
}
virtual void set_item_menu(const OUString& rIdent, weld::Menu* pMenu) override
{
m_aMenuButtonMap[rIdent]->set_menu(pMenu);
}
virtual int get_n_items() const override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
return gtk_toolbar_get_n_items(m_pToolbar);
#else
int n_items = 0;
for (GtkWidget* pChild = gtk_widget_get_first_child(GTK_WIDGET(m_pToolbar));
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
++n_items;
}
return n_items;
#endif
}
virtual OUString get_item_ident(int nIndex) const override
{
auto* pItem = toolbar_get_nth_item(nIndex);
return ::get_buildable_id(GTK_BUILDABLE(pItem));
}
virtual void set_item_ident(int nIndex, const OUString& rIdent) override
{
OUString sOldIdent(get_item_ident(nIndex));
m_aMap.erase(m_aMap.find(sOldIdent));
auto* pItem = toolbar_get_nth_item(nIndex);
::set_buildable_id(GTK_BUILDABLE(pItem), rIdent);
// to keep the ids unique, if the new id is already in use by an item,
// change the id of that item to the now unused old ident of this item
auto aFind = m_aMap.find(rIdent);
if (aFind != m_aMap.end())
{
GtkWidget* pDupIdItem = aFind->second;
::set_buildable_id(GTK_BUILDABLE(pDupIdItem), sOldIdent);
m_aMap[sOldIdent] = pDupIdItem;
}
m_aMap[rIdent] = pItem;
}
virtual void set_item_label(int nIndex, const OUString& rLabel) override
{
auto* pItem = toolbar_get_nth_item(nIndex);
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!GTK_IS_TOOL_BUTTON(pItem))
return;
gtk_tool_button_set_label(GTK_TOOL_BUTTON(pItem), MapToGtkAccelerator(rLabel).getStr());
#else
if (!GTK_IS_BUTTON(pItem))
return;
::button_set_label(GTK_BUTTON(pItem), rLabel);
#endif
}
virtual void set_item_label(const OUString& rIdent, const OUString& rLabel) override
{
GtkWidget* pItem = m_aMap[rIdent];
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!pItem || !GTK_IS_TOOL_BUTTON(pItem))
return;
gtk_tool_button_set_label(GTK_TOOL_BUTTON(pItem), MapToGtkAccelerator(rLabel).getStr());
#else
if (!pItem || !GTK_IS_BUTTON(pItem))
return;
::button_set_label(GTK_BUTTON(pItem), rLabel);
#endif
}
OUString get_item_label(const OUString& rIdent) const override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
const gchar* pText = gtk_tool_button_get_label(GTK_TOOL_BUTTON(m_aMap.find(rIdent)->second));
#else
const gchar* pText = gtk_button_get_label(GTK_BUTTON(m_aMap.find(rIdent)->second));
#endif
return OUString(pText, pText ? strlen(pText) : 0, RTL_TEXTENCODING_UTF8);
}
virtual void set_item_icon_name(const OUString& rIdent, const OUString& rIconName) override
{
GtkWidget* pItem = m_aMap[rIdent];
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!pItem || !GTK_IS_TOOL_BUTTON(pItem))
return;
#else
if (!pItem || !GTK_IS_BUTTON(pItem))
return;
#endif
GtkWidget* pImage = image_new_from_icon_name(rIconName);
if (pImage)
gtk_widget_show(pImage);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(pItem), pImage);
#else
gtk_button_set_child(GTK_BUTTON(pItem), pImage);
// versions of gtk4 > 4.2.1 might do this on their own
gtk_widget_remove_css_class(GTK_WIDGET(pItem), "text-button");
#endif
}
virtual void set_item_image_mirrored(const OUString& rIdent, bool bMirrored) override
{
m_aMirroredMap[rIdent] = bMirrored;
}
virtual void set_item_image(const OUString& rIdent, const css::uno::Reference<css::graphic::XGraphic>& rIcon) override
{
GtkWidget* pItem = m_aMap[rIdent];
auto it = m_aMirroredMap.find(rIdent);
bool bMirrored = it != m_aMirroredMap.end() && it->second;
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!pItem || !GTK_IS_TOOL_BUTTON(pItem))
return;
set_item_image(GTK_TOOL_BUTTON(pItem), rIcon, bMirrored);
#else
if (!pItem)
return;
set_item_image(pItem, rIcon, bMirrored);
#endif
}
virtual void set_item_image(const OUString& rIdent, VirtualDevice* pDevice) override
{
GtkWidget* pItem = m_aMap[rIdent];
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!pItem || !GTK_IS_TOOL_BUTTON(pItem))
return;
set_item_image(GTK_TOOL_BUTTON(pItem), pDevice);
#else
if (!pItem)
return;
set_item_image(pItem, pDevice);
#endif
}
virtual void set_item_image(int nIndex, const css::uno::Reference<css::graphic::XGraphic>& rIcon) override
{
auto* pItem = toolbar_get_nth_item(nIndex);
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!GTK_IS_TOOL_BUTTON(pItem))
return;
set_item_image(GTK_TOOL_BUTTON(pItem), rIcon, false);
#else
set_item_image(pItem, rIcon, false);
#endif
}
virtual void set_item_tooltip_text(int nIndex, const OUString& rTip) override
{
auto* pItem = toolbar_get_nth_item(nIndex);
gtk_widget_set_tooltip_text(GTK_WIDGET(pItem), OUStringToOString(rTip, RTL_TEXTENCODING_UTF8).getStr());
}
virtual void set_item_tooltip_text(const OUString& rIdent, const OUString& rTip) override
{
GtkWidget* pItem = GTK_WIDGET(m_aMap[rIdent]);
gtk_widget_set_tooltip_text(pItem, OUStringToOString(rTip, RTL_TEXTENCODING_UTF8).getStr());
}
virtual OUString get_item_tooltip_text(const OUString& rIdent) const override
{
GtkWidget* pItem = GTK_WIDGET(m_aMap.find(rIdent)->second);
const gchar* pStr = gtk_widget_get_tooltip_text(pItem);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
virtual vcl::ImageType get_icon_size() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
return m_eImageType;
#else
return GtkToVcl(gtk_toolbar_get_icon_size(m_pToolbar));
#endif
}
virtual void set_icon_size(vcl::ImageType eType) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
m_eImageType = eType;
#else
gtk_toolbar_set_icon_size(m_pToolbar, VclToGtk(eType));
#endif
}
virtual sal_uInt16 get_modifier_state() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
GdkDisplay* pDisplay = gtk_widget_get_display(GTK_WIDGET(m_pToolbar));
GdkSeat* pSeat = gdk_display_get_default_seat(pDisplay);
GdkDevice* pDevice = gdk_seat_get_keyboard(pSeat);
guint nState = gdk_device_get_modifier_state(pDevice);
#else
GdkKeymap* pKeymap = gdk_keymap_get_default();
guint nState = gdk_keymap_get_modifier_state(pKeymap);
#endif
return GtkSalFrame::GetKeyModCode(nState);
}
virtual int get_drop_index(const Point& rPoint) const override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
return gtk_toolbar_get_drop_index(m_pToolbar, rPoint.X(), rPoint.Y());
#else
GtkWidget* pToolbar = GTK_WIDGET(m_pToolbar);
GtkWidget* pTarget = gtk_widget_pick(pToolbar, rPoint.X(), rPoint.Y(), GTK_PICK_DEFAULT);
if (!pTarget || pTarget == pToolbar)
return -1;
int i = 0;
for (GtkWidget* pChild = gtk_widget_get_first_child(GTK_WIDGET(m_pToolbar));
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
if (pChild == pTarget)
return i;
++i;
}
return -1;
#endif
}
virtual bool has_focus() const override
{
if (gtk_widget_has_focus(m_pWidget))
return true;
GtkWidget* pTopLevel = widget_get_toplevel(m_pWidget);
if (!GTK_IS_WINDOW(pTopLevel))
return false;
GtkWidget* pFocus = gtk_window_get_focus(GTK_WINDOW(pTopLevel));
if (!pFocus)
return false;
return gtk_widget_is_ancestor(pFocus, m_pWidget);
}
virtual void grab_focus() override
{
if (has_focus())
return;
gtk_widget_grab_focus(m_pWidget);
#if GTK_CHECK_VERSION(4, 0, 0)
bool bHasFocusChild = gtk_widget_get_focus_child(m_pWidget);
#else
bool bHasFocusChild = gtk_container_get_focus_child(GTK_CONTAINER(m_pWidget));
#endif
if (!bHasFocusChild)
{
if (auto* pItem = toolbar_get_nth_item(0))
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_set_focus_child(m_pWidget, GTK_WIDGET(pItem));
#else
gtk_container_set_focus_child(GTK_CONTAINER(m_pWidget), GTK_WIDGET(pItem));
#endif
bHasFocusChild = true;
}
}
if (bHasFocusChild)
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_child_focus(gtk_widget_get_focus_child(m_pWidget), GTK_DIR_TAB_FORWARD);
#else
gtk_widget_child_focus(gtk_container_get_focus_child(GTK_CONTAINER(m_pWidget)), GTK_DIR_TAB_FORWARD);
#endif
}
}
virtual ~GtkInstanceToolbar() override
{
for (auto& a : m_aMap)
g_signal_handlers_disconnect_by_data(a.second, this);
}
};
}
namespace {
class GtkInstanceLinkButton : public GtkInstanceWidget, public virtual weld::LinkButton
{
private:
GtkLinkButton* m_pButton;
gulong m_nSignalId;
static bool signalActivateLink(GtkButton*, gpointer widget)
{
GtkInstanceLinkButton* pThis = static_cast<GtkInstanceLinkButton*>(widget);
SolarMutexGuard aGuard;
return pThis->signal_activate_link();
}
public:
GtkInstanceLinkButton(GtkLinkButton* pButton, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pButton), pBuilder, bTakeOwnership)
, m_pButton(pButton)
, m_nSignalId(g_signal_connect(pButton, "activate-link", G_CALLBACK(signalActivateLink), this))
{
}
virtual void set_label(const OUString& rText) override
{
::button_set_label(GTK_BUTTON(m_pButton), rText);
}
virtual OUString get_label() const override
{
return ::button_get_label(GTK_BUTTON(m_pButton));
}
virtual void set_uri(const OUString& rText) override
{
gtk_link_button_set_uri(m_pButton, OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr());
}
virtual OUString get_uri() const override
{
const gchar* pStr = gtk_link_button_get_uri(m_pButton);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pButton, m_nSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pButton, m_nSignalId);
}
virtual ~GtkInstanceLinkButton() override
{
g_signal_handler_disconnect(m_pButton, m_nSignalId);
}
};
}
namespace {
class GtkInstanceCheckButton : public GtkInstanceWidget, public virtual weld::CheckButton
{
private:
GtkCheckButton* m_pCheckButton;
gulong m_nSignalId;
static void signalToggled(void*, gpointer widget)
{
GtkInstanceCheckButton* pThis = static_cast<GtkInstanceCheckButton*>(widget);
SolarMutexGuard aGuard;
pThis->signal_toggled();
}
public:
GtkInstanceCheckButton(GtkCheckButton* pButton, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pButton), pBuilder, bTakeOwnership)
, m_pCheckButton(pButton)
, m_nSignalId(g_signal_connect(m_pCheckButton, "toggled", G_CALLBACK(signalToggled), this))
{
}
virtual void set_active(bool active) override
{
disable_notify_events();
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_check_button_set_inconsistent(m_pCheckButton, false);
gtk_check_button_set_active(m_pCheckButton, active);
#else
gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_pCheckButton), false);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pCheckButton), active);
#endif
enable_notify_events();
}
virtual bool get_active() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
return gtk_check_button_get_active(m_pCheckButton);
#else
return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_pCheckButton));
#endif
}
virtual void set_inconsistent(bool inconsistent) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_check_button_set_inconsistent(m_pCheckButton, inconsistent);
#else
gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_pCheckButton), inconsistent);
#endif
}
virtual bool get_inconsistent() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
return gtk_check_button_get_inconsistent(m_pCheckButton);
#else
return gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_pCheckButton));
#endif
}
virtual void set_label(const OUString& rText) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_check_button_set_label(m_pCheckButton, MapToGtkAccelerator(rText).getStr());
#else
::button_set_label(GTK_BUTTON(m_pCheckButton), rText);
#endif
}
virtual OUString get_label() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
const gchar* pStr = gtk_check_button_get_label(m_pCheckButton);
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
#else
return ::button_get_label(GTK_BUTTON(m_pCheckButton));
#endif
}
virtual void set_label_wrap(bool bWrap) override
{
GtkLabel* pChild = ::get_label_widget(GTK_WIDGET(m_pCheckButton));
::set_label_wrap(pChild, bWrap);
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pCheckButton, m_nSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pCheckButton, m_nSignalId);
}
virtual ~GtkInstanceCheckButton() override
{
g_signal_handler_disconnect(m_pCheckButton, m_nSignalId);
}
};
class GtkInstanceRadioButton : public GtkInstanceCheckButton, public virtual weld::RadioButton
{
public:
#if GTK_CHECK_VERSION(4, 0, 0)
GtkInstanceRadioButton(GtkCheckButton* pButton, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceCheckButton(pButton, pBuilder, bTakeOwnership)
#else
GtkInstanceRadioButton(GtkRadioButton* pButton, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceCheckButton(GTK_CHECK_BUTTON(pButton), pBuilder, bTakeOwnership)
#endif
{
}
};
}
namespace {
class GtkInstanceScale : public GtkInstanceWidget, public virtual weld::Scale
{
private:
GtkScale* m_pScale;
gulong m_nValueChangedSignalId;
static void signalValueChanged(GtkScale*, gpointer widget)
{
GtkInstanceScale* pThis = static_cast<GtkInstanceScale*>(widget);
SolarMutexGuard aGuard;
pThis->signal_value_changed();
}
public:
GtkInstanceScale(GtkScale* pScale, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pScale), pBuilder, bTakeOwnership)
, m_pScale(pScale)
, m_nValueChangedSignalId(g_signal_connect(m_pScale, "value-changed", G_CALLBACK(signalValueChanged), this))
{
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pScale, m_nValueChangedSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pScale, m_nValueChangedSignalId);
}
virtual void set_value(int value) override
{
disable_notify_events();
gtk_range_set_value(GTK_RANGE(m_pScale), value);
enable_notify_events();
}
virtual void set_range(int min, int max) override
{
disable_notify_events();
gtk_range_set_range(GTK_RANGE(m_pScale), min, max);
enable_notify_events();
}
virtual void set_increments(int step, int page) override
{
disable_notify_events();
gtk_range_set_increments(GTK_RANGE(m_pScale), step, page);
enable_notify_events();
}
virtual void get_increments(int& step, int& page) const override
{
GtkAdjustment* pAdjustment = gtk_range_get_adjustment(GTK_RANGE(m_pScale));
step = gtk_adjustment_get_step_increment(pAdjustment);
page = gtk_adjustment_get_page_increment(pAdjustment);
}
virtual int get_value() const override
{
return gtk_range_get_value(GTK_RANGE(m_pScale));
}
virtual ~GtkInstanceScale() override
{
g_signal_handler_disconnect(m_pScale, m_nValueChangedSignalId);
}
};
class GtkInstanceProgressBar : public GtkInstanceWidget, public virtual weld::ProgressBar
{
private:
GtkProgressBar* m_pProgressBar;
public:
GtkInstanceProgressBar(GtkProgressBar* pProgressBar, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pProgressBar), pBuilder, bTakeOwnership)
, m_pProgressBar(pProgressBar)
{
}
virtual void set_percentage(int value) override
{
gtk_progress_bar_set_fraction(m_pProgressBar, value / 100.0);
}
virtual OUString get_text() const override
{
const gchar* pText = gtk_progress_bar_get_text(m_pProgressBar);
OUString sRet(pText, pText ? strlen(pText) : 0, RTL_TEXTENCODING_UTF8);
return sRet;
}
virtual void set_text(const OUString& rText) override
{
gtk_progress_bar_set_text(m_pProgressBar, OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr());
}
};
class GtkInstanceSpinner : public GtkInstanceWidget, public virtual weld::Spinner
{
private:
GtkSpinner* m_pSpinner;
public:
GtkInstanceSpinner(GtkSpinner* pSpinner, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pSpinner), pBuilder, bTakeOwnership)
, m_pSpinner(pSpinner)
{
}
virtual void start() override
{
gtk_spinner_start(m_pSpinner);
}
virtual void stop() override
{
gtk_spinner_stop(m_pSpinner);
}
};
class GtkInstanceImage : public GtkInstanceWidget, public virtual weld::Image
{
private:
GtkImage* m_pImage;
public:
GtkInstanceImage(GtkImage* pImage, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pImage), pBuilder, bTakeOwnership)
, m_pImage(pImage)
{
}
virtual void set_from_icon_name(const OUString& rIconName) override
{
image_set_from_icon_name(m_pImage, rIconName);
}
virtual void set_image(VirtualDevice* pDevice) override
{
image_set_from_virtual_device(m_pImage, pDevice);
}
virtual void set_image(const css::uno::Reference<css::graphic::XGraphic>& rImage) override
{
image_set_from_xgraphic(m_pImage, rImage);
}
};
#if GTK_CHECK_VERSION(4, 0, 0)
class GtkInstancePicture: public GtkInstanceWidget, public virtual weld::Image
{
private:
GtkPicture* m_pPicture;
public:
GtkInstancePicture(GtkPicture* pPicture, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pPicture), pBuilder, bTakeOwnership)
, m_pPicture(pPicture)
{
gtk_picture_set_can_shrink(m_pPicture, true);
}
virtual void set_from_icon_name(const OUString& rIconName) override
{
picture_set_from_icon_name(m_pPicture, rIconName);
}
virtual void set_image(VirtualDevice* pDevice) override
{
picture_set_from_virtual_device(m_pPicture, pDevice);
}
virtual void set_image(const css::uno::Reference<css::graphic::XGraphic>& rPicture) override
{
picture_set_from_xgraphic(m_pPicture, rPicture);
}
};
#endif
class GtkInstanceCalendar : public GtkInstanceWidget, public virtual weld::Calendar
{
private:
GtkCalendar* m_pCalendar;
#if GTK_CHECK_VERSION(4, 0, 0)
GtkEventController* m_pKeyController;
#endif
gulong m_nDaySelectedSignalId;
gulong m_nDaySelectedDoubleClickSignalId;
gulong m_nKeyPressEventSignalId;
#if !GTK_CHECK_VERSION(4, 0, 0)
gulong m_nButtonPressEventSignalId;
#endif
static void signalDaySelected(GtkCalendar*, gpointer widget)
{
GtkInstanceCalendar* pThis = static_cast<GtkInstanceCalendar*>(widget);
SolarMutexGuard aGuard;
pThis->signal_selected();
}
static void signalDaySelectedDoubleClick(GtkCalendar*, gpointer widget)
{
GtkInstanceCalendar* pThis = static_cast<GtkInstanceCalendar*>(widget);
SolarMutexGuard aGuard;
pThis->signal_activated();
}
bool signal_key_press(guint nKeyVal)
{
if (nKeyVal == GDK_KEY_Return || nKeyVal == GDK_KEY_KP_Enter)
{
SolarMutexGuard aGuard;
signal_activated();
return true;
}
return false;
}
#if GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalKeyPress(GtkEventControllerKey*, guint nKeyVal, guint /*nKeyCode*/, GdkModifierType, gpointer widget)
{
GtkInstanceCalendar* pThis = static_cast<GtkInstanceCalendar*>(widget);
return pThis->signal_key_press(nKeyVal);
}
#else
static gboolean signalKeyPress(GtkWidget*, GdkEventKey* pEvent, gpointer widget)
{
GtkInstanceCalendar* pThis = static_cast<GtkInstanceCalendar*>(widget);
return pThis->signal_key_press(pEvent->keyval);
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalButton(GtkWidget*, GdkEventButton*, gpointer)
{
// don't let button press get to parent window, for the case of the
// ImplCFieldFloatWin floating window belonging to CalendarField where
// the click on the calendar continues to the parent GtkWindow and
// closePopup is called by GtkSalFrame::signalButton because the click
// window isn't that of the floating parent GtkWindow
return true;
}
#endif
public:
GtkInstanceCalendar(GtkCalendar* pCalendar, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pCalendar), pBuilder, bTakeOwnership)
, m_pCalendar(pCalendar)
#if GTK_CHECK_VERSION(4, 0, 0)
, m_pKeyController(gtk_event_controller_key_new())
#endif
, m_nDaySelectedSignalId(g_signal_connect(pCalendar, "day-selected", G_CALLBACK(signalDaySelected), this))
, m_nDaySelectedDoubleClickSignalId(g_signal_connect(pCalendar, "day-selected-double-click", G_CALLBACK(signalDaySelectedDoubleClick), this))
#if GTK_CHECK_VERSION(4, 0, 0)
, m_nKeyPressEventSignalId(g_signal_connect(m_pKeyController, "key-pressed", G_CALLBACK(signalKeyPress), this))
#else
, m_nKeyPressEventSignalId(g_signal_connect(pCalendar, "key-press-event", G_CALLBACK(signalKeyPress), this))
, m_nButtonPressEventSignalId(g_signal_connect_after(pCalendar, "button-press-event", G_CALLBACK(signalButton), this))
#endif
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_add_controller(GTK_WIDGET(m_pCalendar), m_pKeyController);
#endif
}
virtual void set_date(const Date& rDate) override
{
if (!rDate.IsValidAndGregorian())
return;
disable_notify_events();
#if GTK_CHECK_VERSION(4, 0, 0)
GDateTime* pDateTime = g_date_time_new_local(rDate.GetYear(), rDate.GetMonth(), rDate.GetDay(), 0, 0, 0);
gtk_calendar_select_day(m_pCalendar, pDateTime);
g_date_time_unref(pDateTime);
#else
gtk_calendar_select_month(m_pCalendar, rDate.GetMonth() - 1, rDate.GetYear());
gtk_calendar_select_day(m_pCalendar, rDate.GetDay());
#endif
enable_notify_events();
}
virtual Date get_date() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
GDateTime* pDateTime = gtk_calendar_get_date(m_pCalendar);
Date aDate(g_date_time_get_day_of_month(pDateTime),
g_date_time_get_month(pDateTime),
g_date_time_get_year(pDateTime));
g_date_time_unref(pDateTime);
return aDate;
#else
guint year, month, day;
gtk_calendar_get_date(m_pCalendar, &year, &month, &day);
return Date(day, month + 1, year);
#endif
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pCalendar, m_nDaySelectedDoubleClickSignalId);
g_signal_handler_block(m_pCalendar, m_nDaySelectedSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pCalendar, m_nDaySelectedSignalId);
g_signal_handler_unblock(m_pCalendar, m_nDaySelectedDoubleClickSignalId);
}
virtual ~GtkInstanceCalendar() override
{
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pKeyController, m_nKeyPressEventSignalId);
#else
g_signal_handler_disconnect(m_pCalendar, m_nButtonPressEventSignalId);
g_signal_handler_disconnect(m_pCalendar, m_nKeyPressEventSignalId);
#endif
g_signal_handler_disconnect(m_pCalendar, m_nDaySelectedDoubleClickSignalId);
g_signal_handler_disconnect(m_pCalendar, m_nDaySelectedSignalId);
}
};
}
namespace
{
// CSS nodes: entry[.flat][.warning][.error]
void set_widget_css_message_type(GtkWidget* pWidget, weld::EntryMessageType eType)
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_remove_css_class(pWidget, "error");
gtk_widget_remove_css_class(pWidget, "warning");
#else
GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(pWidget);
gtk_style_context_remove_class(pWidgetContext, "error");
gtk_style_context_remove_class(pWidgetContext, "warning");
#endif
switch (eType)
{
case weld::EntryMessageType::Normal:
break;
case weld::EntryMessageType::Warning:
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_add_css_class(pWidget, "warning");
#else
gtk_style_context_add_class(pWidgetContext, "warning");
#endif
break;
case weld::EntryMessageType::Error:
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_add_css_class(pWidget, "error");
#else
gtk_style_context_add_class(pWidgetContext, "error");
#endif
break;
}
}
void set_entry_message_type(GtkEntry* pEntry, weld::EntryMessageType eType)
{
set_widget_css_message_type(GTK_WIDGET(pEntry), eType);
switch (eType)
{
case weld::EntryMessageType::Normal:
gtk_entry_set_icon_from_icon_name(pEntry, GTK_ENTRY_ICON_SECONDARY, nullptr);
break;
case weld::EntryMessageType::Warning:
gtk_entry_set_icon_from_icon_name(pEntry, GTK_ENTRY_ICON_SECONDARY, "dialog-warning");
break;
case weld::EntryMessageType::Error:
gtk_entry_set_icon_from_icon_name(pEntry, GTK_ENTRY_ICON_SECONDARY, "dialog-error");
break;
}
}
}
namespace
{
class GtkInstanceEditable : public GtkInstanceWidget, public virtual weld::Entry
{
protected:
GtkEditable* m_pEditable;
GtkWidget* m_pDelegate;
WidgetFont m_aCustomFont;
private:
gulong m_nChangedSignalId;
gulong m_nInsertTextSignalId;
gulong m_nCursorPosSignalId;
gulong m_nSelectionPosSignalId;
gulong m_nActivateSignalId;
static void signalChanged(GtkEditable*, gpointer widget)
{
GtkInstanceEditable* pThis = static_cast<GtkInstanceEditable*>(widget);
SolarMutexGuard aGuard;
pThis->signal_changed();
}
static void signalInsertText(GtkEditable* pEditable, const gchar* pNewText, gint nNewTextLength,
gint* position, gpointer widget)
{
GtkInstanceEditable* pThis = static_cast<GtkInstanceEditable*>(widget);
SolarMutexGuard aGuard;
pThis->signal_insert_text(pEditable, pNewText, nNewTextLength, position);
}
void signal_insert_text(GtkEditable* pEditable, const gchar* pNewText, gint nNewTextLength, gint* position)
{
if (!m_aInsertTextHdl.IsSet())
return;
OUString sText(pNewText, nNewTextLength, RTL_TEXTENCODING_UTF8);
const bool bContinue = m_aInsertTextHdl.Call(sText);
if (bContinue && !sText.isEmpty())
{
OString sFinalText(OUStringToOString(sText, RTL_TEXTENCODING_UTF8));
g_signal_handlers_block_by_func(pEditable, reinterpret_cast<gpointer>(signalInsertText), this);
gtk_editable_insert_text(pEditable, sFinalText.getStr(), sFinalText.getLength(), position);
g_signal_handlers_unblock_by_func(pEditable, reinterpret_cast<gpointer>(signalInsertText), this);
}
g_signal_stop_emission_by_name(pEditable, "insert-text");
}
static void signalCursorPosition(void*, GParamSpec*, gpointer widget)
{
GtkInstanceEditable* pThis = static_cast<GtkInstanceEditable*>(widget);
pThis->signal_cursor_position();
}
static void signalActivate(void*, gpointer widget)
{
GtkInstanceEditable* pThis = static_cast<GtkInstanceEditable*>(widget);
pThis->signal_activate();
}
virtual void ensureMouseEventWidget() override
{
// The GtkEntry is sufficient to get mouse events without an intermediate GtkEventBox
if (!m_pMouseEventBox)
m_pMouseEventBox = m_pDelegate;
}
protected:
virtual void signal_activate()
{
if (m_aActivateHdl.IsSet())
{
SolarMutexGuard aGuard;
if (m_aActivateHdl.Call(*this))
g_signal_stop_emission_by_name(m_pDelegate, "activate");
}
}
PangoAttrList* get_attributes()
{
#if GTK_CHECK_VERSION(4, 0, 0)
return gtk_text_get_attributes(GTK_TEXT(m_pDelegate));
#else
return gtk_entry_get_attributes(GTK_ENTRY(m_pDelegate));
#endif
}
void set_attributes(PangoAttrList* pAttrs)
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_text_set_attributes(GTK_TEXT(m_pDelegate), pAttrs);
#else
gtk_entry_set_attributes(GTK_ENTRY(m_pDelegate), pAttrs);
#endif
}
public:
GtkInstanceEditable(GtkWidget* pWidget, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(pWidget, pBuilder, bTakeOwnership)
, m_pEditable(GTK_EDITABLE(pWidget))
#if GTK_CHECK_VERSION(4, 0, 0)
, m_pDelegate(GTK_WIDGET(gtk_editable_get_delegate(m_pEditable)))
#else
, m_pDelegate(pWidget)
#endif
, m_aCustomFont(m_pWidget)
, m_nChangedSignalId(g_signal_connect(m_pEditable, "changed", G_CALLBACK(signalChanged), this))
, m_nInsertTextSignalId(g_signal_connect(m_pEditable, "insert-text", G_CALLBACK(signalInsertText), this))
, m_nCursorPosSignalId(g_signal_connect(m_pEditable, "notify::cursor-position", G_CALLBACK(signalCursorPosition), this))
, m_nSelectionPosSignalId(g_signal_connect(m_pEditable, "notify::selection-bound", G_CALLBACK(signalCursorPosition), this))
, m_nActivateSignalId(g_signal_connect(m_pDelegate, "activate", G_CALLBACK(signalActivate), this))
{
}
virtual void set_text(const OUString& rText) override
{
disable_notify_events();
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_editable_set_text(m_pEditable, OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr());
#else
gtk_entry_set_text(GTK_ENTRY(m_pDelegate), OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr());
#endif
enable_notify_events();
}
virtual OUString get_text() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
const gchar* pText = gtk_editable_get_text(m_pEditable);
#else
const gchar* pText = gtk_entry_get_text(GTK_ENTRY(m_pDelegate));
#endif
OUString sRet(pText, pText ? strlen(pText) : 0, RTL_TEXTENCODING_UTF8);
return sRet;
}
virtual void set_width_chars(int nChars) override
{
disable_notify_events();
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_editable_set_width_chars(m_pEditable, nChars);
gtk_editable_set_max_width_chars(m_pEditable, nChars);
#else
gtk_entry_set_width_chars(GTK_ENTRY(m_pDelegate), nChars);
gtk_entry_set_max_width_chars(GTK_ENTRY(m_pDelegate), nChars);
#endif
enable_notify_events();
}
virtual int get_width_chars() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
return gtk_editable_get_width_chars(m_pEditable);
#else
return gtk_entry_get_width_chars(GTK_ENTRY(m_pDelegate));
#endif
}
virtual void set_max_length(int nChars) override
{
disable_notify_events();
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_text_set_max_length(GTK_TEXT(m_pDelegate), nChars);
#else
gtk_entry_set_max_length(GTK_ENTRY(m_pDelegate), nChars);
#endif
enable_notify_events();
}
virtual void select_region(int nStartPos, int nEndPos) override
{
disable_notify_events();
gtk_editable_select_region(m_pEditable, nStartPos, nEndPos);
enable_notify_events();
}
bool get_selection_bounds(int& rStartPos, int& rEndPos) override
{
return gtk_editable_get_selection_bounds(m_pEditable, &rStartPos, &rEndPos);
}
virtual void replace_selection(const OUString& rText) override
{
disable_notify_events();
gtk_editable_delete_selection(m_pEditable);
OString sText(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
gint position = gtk_editable_get_position(m_pEditable);
gtk_editable_insert_text(m_pEditable, sText.getStr(), sText.getLength(),
&position);
enable_notify_events();
}
virtual void set_position(int nCursorPos) override
{
disable_notify_events();
gtk_editable_set_position(m_pEditable, nCursorPos);
enable_notify_events();
}
virtual int get_position() const override
{
return gtk_editable_get_position(m_pEditable);
}
virtual void set_editable(bool bEditable) override
{
gtk_editable_set_editable(m_pEditable, bEditable);
}
virtual bool get_editable() const override
{
return gtk_editable_get_editable(m_pEditable);
}
virtual void set_overwrite_mode(bool bOn) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_text_set_overwrite_mode(GTK_TEXT(m_pDelegate), bOn);
#else
gtk_entry_set_overwrite_mode(GTK_ENTRY(m_pDelegate), bOn);
#endif
}
virtual bool get_overwrite_mode() const override
{
#if GTK_CHECK_VERSION(4, 0, 0)
return gtk_text_get_overwrite_mode(GTK_TEXT(m_pDelegate));
#else
return gtk_entry_get_overwrite_mode(GTK_ENTRY(m_pDelegate));
#endif
}
virtual void set_message_type(weld::EntryMessageType eType) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
if (!GTK_IS_ENTRY(m_pDelegate))
{
::set_widget_css_message_type(m_pDelegate, eType);
return;
}
#endif
::set_entry_message_type(GTK_ENTRY(m_pDelegate), eType);
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pDelegate, m_nActivateSignalId);
g_signal_handler_block(m_pEditable, m_nSelectionPosSignalId);
g_signal_handler_block(m_pEditable, m_nCursorPosSignalId);
g_signal_handler_block(m_pEditable, m_nInsertTextSignalId);
g_signal_handler_block(m_pEditable, m_nChangedSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pEditable, m_nChangedSignalId);
g_signal_handler_unblock(m_pEditable, m_nInsertTextSignalId);
g_signal_handler_unblock(m_pEditable, m_nCursorPosSignalId);
g_signal_handler_unblock(m_pEditable, m_nSelectionPosSignalId);
g_signal_handler_unblock(m_pDelegate, m_nActivateSignalId);
}
virtual vcl::Font get_font() override
{
if (const vcl::Font* pFont = m_aCustomFont.get_custom_font())
return *pFont;
return GtkInstanceWidget::get_font();
}
void set_font_color(const Color& rColor) override
{
PangoAttrList* pOrigList = get_attributes();
if (rColor == COL_AUTO && !pOrigList) // nothing to do
return;
PangoAttrType aFilterAttrs[] = {PANGO_ATTR_FOREGROUND, PANGO_ATTR_INVALID};
PangoAttrList* pAttrs = pOrigList ? pango_attr_list_copy(pOrigList) : pango_attr_list_new();
PangoAttrList* pRemovedAttrs = pOrigList ? pango_attr_list_filter(pAttrs, filter_pango_attrs, &aFilterAttrs) : nullptr;
if (rColor != COL_AUTO)
pango_attr_list_insert(pAttrs, pango_attr_foreground_new(rColor.GetRed()/255.0, rColor.GetGreen()/255.0, rColor.GetBlue()/255.0));
set_attributes(pAttrs);
pango_attr_list_unref(pAttrs);
pango_attr_list_unref(pRemovedAttrs);
}
void fire_signal_changed()
{
signal_changed();
}
virtual void cut_clipboard() override
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_activate_action(m_pDelegate, "cut.clipboard", nullptr);
#else
gtk_editable_cut_clipboard(m_pEditable);
#endif
}
virtual void copy_clipboard() override
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_activate_action(m_pDelegate, "copy.clipboard", nullptr);
#else
gtk_editable_copy_clipboard(m_pEditable);
#endif
}
virtual void paste_clipboard() override
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_activate_action(m_pDelegate, "paste.clipboard", nullptr);
#else
gtk_editable_paste_clipboard(m_pEditable);
#endif
}
virtual void set_placeholder_text(const OUString& rText) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_text_set_placeholder_text(GTK_TEXT(m_pDelegate), rText.toUtf8().getStr());
#else
gtk_entry_set_placeholder_text(GTK_ENTRY(m_pDelegate), rText.toUtf8().getStr());
#endif
}
virtual void grab_focus() override
{
if (has_focus())
return;
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_text_grab_focus_without_selecting(GTK_TEXT(m_pDelegate));
#else
gtk_entry_grab_focus_without_selecting(GTK_ENTRY(m_pDelegate));
#endif
}
virtual void set_alignment(TxtAlign eXAlign) override
{
gfloat xalign = 0;
switch (eXAlign)
{
case TxtAlign::Left:
xalign = 0.0;
break;
case TxtAlign::Center:
xalign = 0.5;
break;
case TxtAlign::Right:
xalign = 1.0;
break;
}
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_editable_set_alignment(m_pEditable, xalign);
#else
gtk_entry_set_alignment(GTK_ENTRY(m_pDelegate), xalign);
#endif
}
virtual ~GtkInstanceEditable() override
{
g_signal_handler_disconnect(m_pDelegate, m_nActivateSignalId);
g_signal_handler_disconnect(m_pEditable, m_nSelectionPosSignalId);
g_signal_handler_disconnect(m_pEditable, m_nCursorPosSignalId);
g_signal_handler_disconnect(m_pEditable, m_nInsertTextSignalId);
g_signal_handler_disconnect(m_pEditable, m_nChangedSignalId);
}
};
class GtkInstanceEntry : public GtkInstanceEditable
{
private:
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkEntry* m_pEntry;
GtkOverlay* m_pPlaceHolderReplacement;
GtkLabel* m_pPlaceHolderLabel;
gulong m_nEntryFocusInSignalId;
gulong m_nEntryFocusOutSignalId;
gulong m_nEntryTextLengthSignalId;
gulong m_nEntryScrollOffsetSignalId;
guint m_nUpdatePlaceholderReplacementIdle;
static gboolean do_update_placeholder_replacement(gpointer widget)
{
GtkInstanceEntry* pThis = static_cast<GtkInstanceEntry*>(widget);
pThis->update_placeholder_replacement();
return false;
}
void update_placeholder_replacement()
{
m_nUpdatePlaceholderReplacementIdle = 0;
const char* placeholder_text = gtk_entry_get_placeholder_text(m_pEntry);
const bool bShow = placeholder_text && !gtk_entry_get_text_length(m_pEntry) &&
gtk_widget_has_focus(GTK_WIDGET(m_pEntry));
if (bShow)
{
GdkRectangle text_area;
gtk_entry_get_text_area(m_pEntry, &text_area);
gint x;
gtk_entry_get_layout_offsets(m_pEntry, &x, nullptr);
gtk_widget_set_margin_start(GTK_WIDGET(m_pPlaceHolderLabel), x);
gtk_widget_set_margin_end(GTK_WIDGET(m_pPlaceHolderLabel), x);
gtk_label_set_text(m_pPlaceHolderLabel, placeholder_text);
gtk_widget_show(GTK_WIDGET(m_pPlaceHolderLabel));
}
else
gtk_widget_hide(GTK_WIDGET(m_pPlaceHolderLabel));
}
void launch_update_placeholder_replacement()
{
// do it in the next event cycle so the GtkEntry has done its layout
// and gtk_entry_get_layout_offsets returns the right results
if (m_nUpdatePlaceholderReplacementIdle)
return;
// G_PRIORITY_LOW so gtk's idles are run before this
m_nUpdatePlaceholderReplacementIdle = g_idle_add_full(G_PRIORITY_LOW, do_update_placeholder_replacement, this, nullptr);
}
static gboolean signalEntryFocusIn(GtkWidget*, GdkEvent*, gpointer widget)
{
GtkInstanceEntry* pThis = static_cast<GtkInstanceEntry*>(widget);
pThis->launch_update_placeholder_replacement();
return false;
}
static gboolean signalEntryFocusOut(GtkWidget*, GdkEvent*, gpointer widget)
{
GtkInstanceEntry* pThis = static_cast<GtkInstanceEntry*>(widget);
pThis->launch_update_placeholder_replacement();
return false;
}
static void signalEntryTextLength(void*, GParamSpec*, gpointer widget)
{
GtkInstanceEntry* pThis = static_cast<GtkInstanceEntry*>(widget);
pThis->launch_update_placeholder_replacement();
}
static void signalEntryScrollOffset(void*, GParamSpec*, gpointer widget)
{
// this property affects the x-position of the text area
GtkInstanceEntry* pThis = static_cast<GtkInstanceEntry*>(widget);
pThis->launch_update_placeholder_replacement();
}
#endif
public:
GtkInstanceEntry(GtkEntry* pEntry, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceEditable(GTK_WIDGET(pEntry), pBuilder, bTakeOwnership)
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_pEntry(pEntry)
, m_pPlaceHolderReplacement(nullptr)
, m_pPlaceHolderLabel(nullptr)
, m_nEntryFocusInSignalId(0)
, m_nEntryFocusOutSignalId(0)
, m_nEntryTextLengthSignalId(0)
, m_nEntryScrollOffsetSignalId(0)
, m_nUpdatePlaceholderReplacementIdle(0)
#endif
{
#if !GTK_CHECK_VERSION(4, 0, 0)
// tdf#150810 fake getting placeholders visible even when GtkEntry has focus in gtk3.
// In gtk4 this works out of the box, for gtk3 fake it by having a GtkLabel in an
// overlay and show that label if the placeholder would be shown if there was
// no focus
const char* pPlaceHolderText = gtk_entry_get_placeholder_text(m_pEntry);
if (pPlaceHolderText ? strlen(pPlaceHolderText) : 0)
{
m_pPlaceHolderReplacement = GTK_OVERLAY(gtk_overlay_new());
m_pPlaceHolderLabel = GTK_LABEL(gtk_label_new(nullptr));
GtkStyleContext *pStyleContext = gtk_widget_get_style_context(GTK_WIDGET(m_pEntry));
GdkRGBA fg = { 0.5, 0.5, 0.5, 0.0 };
gtk_style_context_lookup_color(pStyleContext, "placeholder_text_color", &fg);
auto red = std::clamp(fg.red * 65535 + 0.5, 0.0, 65535.0);
auto green = std::clamp(fg.green * 65535 + 0.5, 0.0, 65535.0);
auto blue = std::clamp(fg.blue * 65535 + 0.5, 0.0, 65535.0);
PangoAttribute *pAttr = pango_attr_foreground_new(red, green, blue);
pAttr->start_index = 0;
pAttr->end_index = G_MAXINT;
PangoAttrList* pAttrList = pango_attr_list_new();
pango_attr_list_insert(pAttrList, pAttr);
gtk_label_set_attributes(m_pPlaceHolderLabel, pAttrList);
pango_attr_list_unref(pAttrList);
// The GtkEntry will have the placeholder as the text to analyze here, assumes there is no initial text, just placeholder
const bool bRTL = PANGO_DIRECTION_RTL == pango_context_get_base_dir(pango_layout_get_context(gtk_entry_get_layout(m_pEntry)));
SAL_WARN_IF(gtk_entry_get_text_length(m_pEntry), "vcl.gtk", "don't have a placeholder set, but also initial text");
gtk_label_set_xalign(m_pPlaceHolderLabel, bRTL ? 1.0 : 0.0);
gtk_overlay_add_overlay(m_pPlaceHolderReplacement, GTK_WIDGET(m_pPlaceHolderLabel));
insertAsParent(GTK_WIDGET(m_pEntry), GTK_WIDGET(m_pPlaceHolderReplacement));
m_nEntryFocusInSignalId = g_signal_connect_after(m_pEntry, "focus-in-event", G_CALLBACK(signalEntryFocusIn), this);
m_nEntryFocusOutSignalId = g_signal_connect_after(m_pEntry, "focus-out-event", G_CALLBACK(signalEntryFocusOut), this);
m_nEntryTextLengthSignalId = g_signal_connect(m_pEntry, "notify::text-length", G_CALLBACK(signalEntryTextLength), this);
m_nEntryScrollOffsetSignalId = g_signal_connect(m_pEntry, "notify::scroll-offset", G_CALLBACK(signalEntryScrollOffset), this);
}
#endif
}
virtual void set_font(const vcl::Font& rFont) override
{
m_aCustomFont.use_custom_font(&rFont, u"entry");
}
#if !GTK_CHECK_VERSION(4, 0, 0)
virtual void show() override
{
GtkInstanceEditable::show();
if (m_pPlaceHolderReplacement)
gtk_widget_show(GTK_WIDGET(m_pPlaceHolderReplacement));
}
virtual void hide() override
{
if (m_pPlaceHolderReplacement)
gtk_widget_hide(GTK_WIDGET(m_pPlaceHolderReplacement));
GtkInstanceEditable::hide();
}
virtual ~GtkInstanceEntry() override
{
if (m_nUpdatePlaceholderReplacementIdle)
g_source_remove(m_nUpdatePlaceholderReplacementIdle);
if (m_nEntryFocusInSignalId)
g_signal_handler_disconnect(m_pEntry, m_nEntryFocusInSignalId);
if (m_nEntryFocusOutSignalId)
g_signal_handler_disconnect(m_pEntry, m_nEntryFocusOutSignalId);
if (m_nEntryTextLengthSignalId)
g_signal_handler_disconnect(m_pEntry, m_nEntryTextLengthSignalId);
if (m_nEntryScrollOffsetSignalId)
g_signal_handler_disconnect(m_pEntry, m_nEntryScrollOffsetSignalId);
}
#endif
};
}
namespace
{
struct Search
{
OString str;
int index;
int col;
Search(std::u16string_view rText, int nCol)
: str(OUStringToOString(rText, RTL_TEXTENCODING_UTF8))
, index(-1)
, col(nCol)
{
}
};
gboolean foreach_find(GtkTreeModel* model, GtkTreePath* path, GtkTreeIter* iter, gpointer data)
{
Search* search = static_cast<Search*>(data);
gchar *pStr = nullptr;
gtk_tree_model_get(model, iter, search->col, &pStr, -1);
bool found = strcmp(pStr, search->str.getStr()) == 0;
if (found)
{
gint depth;
gint* indices = gtk_tree_path_get_indices_with_depth(path, &depth);
search->index = indices[depth-1];
}
g_free(pStr);
return found;
}
void insert_row(GtkListStore* pListStore, GtkTreeIter& iter, int pos, const OUString* pId, std::u16string_view rText, const OUString* pIconName, const VirtualDevice* pDevice)
{
if (!pIconName && !pDevice)
{
gtk_list_store_insert_with_values(pListStore, &iter, pos,
0, OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr(),
1, !pId ? nullptr : OUStringToOString(*pId, RTL_TEXTENCODING_UTF8).getStr(),
-1);
}
else
{
if (pIconName)
{
GdkPixbuf* pixbuf = getPixbuf(*pIconName);
gtk_list_store_insert_with_values(pListStore, &iter, pos,
0, OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr(),
1, !pId ? nullptr : OUStringToOString(*pId, RTL_TEXTENCODING_UTF8).getStr(),
2, pixbuf,
-1);
if (pixbuf)
g_object_unref(pixbuf);
}
else
{
cairo_surface_t* surface = get_underlying_cairo_surface(*pDevice);
Size aSize(pDevice->GetOutputSizePixel());
cairo_surface_t* target = cairo_surface_create_similar(surface,
cairo_surface_get_content(surface),
aSize.Width(),
aSize.Height());
cairo_t* cr = cairo_create(target);
cairo_set_source_surface(cr, surface, 0, 0);
cairo_paint(cr);
cairo_destroy(cr);
gtk_list_store_insert_with_values(pListStore, &iter, pos,
0, OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr(),
1, !pId ? nullptr : OUStringToOString(*pId, RTL_TEXTENCODING_UTF8).getStr(),
3, target,
-1);
cairo_surface_destroy(target);
}
}
}
}
namespace
{
gint default_sort_func(GtkTreeModel* pModel, GtkTreeIter* a, GtkTreeIter* b, gpointer data)
{
comphelper::string::NaturalStringSorter* pSorter = static_cast<comphelper::string::NaturalStringSorter*>(data);
gchar* pName1;
gchar* pName2;
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(pModel);
gint sort_column_id(0);
gtk_tree_sortable_get_sort_column_id(pSortable, &sort_column_id, nullptr);
gtk_tree_model_get(pModel, a, sort_column_id, &pName1, -1);
gtk_tree_model_get(pModel, b, sort_column_id, &pName2, -1);
gint ret = pSorter->compare(OUString(pName1, pName1 ? strlen(pName1) : 0, RTL_TEXTENCODING_UTF8),
OUString(pName2, pName2 ? strlen(pName2) : 0, RTL_TEXTENCODING_UTF8));
g_free(pName1);
g_free(pName2);
return ret;
}
int starts_with(GtkTreeModel* pTreeModel, const OUString& rStr, int col, int nStartRow, bool bCaseSensitive)
{
GtkTreeIter iter;
if (!gtk_tree_model_iter_nth_child(pTreeModel, &iter, nullptr, nStartRow))
return -1;
const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
int nRet = nStartRow;
do
{
gchar* pStr;
gtk_tree_model_get(pTreeModel, &iter, col, &pStr, -1);
OUString aStr(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
g_free(pStr);
const bool bMatch = !bCaseSensitive ? rI18nHelper.MatchString(rStr, aStr) : aStr.startsWith(rStr);
if (bMatch)
return nRet;
++nRet;
} while (gtk_tree_model_iter_next(pTreeModel, &iter));
return -1;
}
struct GtkInstanceTreeIter : public weld::TreeIter
{
GtkInstanceTreeIter(const GtkInstanceTreeIter* pOrig)
{
if (pOrig)
iter = pOrig->iter;
else
memset(&iter, 0, sizeof(iter));
}
GtkInstanceTreeIter(const GtkTreeIter& rOrig)
{
memcpy(&iter, &rOrig, sizeof(iter));
}
virtual bool equal(const TreeIter& rOther) const override
{
return memcmp(&iter, &static_cast<const GtkInstanceTreeIter&>(rOther).iter, sizeof(GtkTreeIter)) == 0;
}
GtkTreeIter iter;
};
class GtkInstanceTreeView;
}
static GtkInstanceTreeView* g_DragSource;
namespace {
struct CompareGtkTreePath
{
bool operator()(const GtkTreePath* lhs, const GtkTreePath* rhs) const
{
return gtk_tree_path_compare(lhs, rhs) < 0;
}
};
int get_height_row(GtkTreeView* pTreeView, GList* pColumns)
{
gint nMaxRowHeight = 0;
for (GList* pEntry = g_list_first(pColumns); pEntry; pEntry = g_list_next(pEntry))
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pEntry->data);
GList *pRenderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(pColumn));
for (GList* pRenderer = g_list_first(pRenderers); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
gint nRowHeight;
gtk_cell_renderer_get_preferred_height(pCellRenderer, GTK_WIDGET(pTreeView), nullptr, &nRowHeight);
nMaxRowHeight = std::max(nMaxRowHeight, nRowHeight);
}
g_list_free(pRenderers);
}
return nMaxRowHeight;
}
int get_height_row_separator(GtkTreeView* pTreeView)
{
// gtk4: _TREE_VIEW_VERTICAL_SEPARATOR define in gtk/gtktreeview.c
gint nVerticalSeparator = 2;
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_style_get(GTK_WIDGET(pTreeView), "vertical-separator", &nVerticalSeparator, nullptr);
#else
(void)pTreeView;
#endif
return nVerticalSeparator;
}
int get_height_rows(GtkTreeView* pTreeView, GList* pColumns, int nRows)
{
gint nMaxRowHeight = get_height_row(pTreeView, pColumns);
gint nVerticalSeparator = get_height_row_separator(pTreeView);
return (nMaxRowHeight * nRows) + (nVerticalSeparator * nRows) / 2;
}
#if !GTK_CHECK_VERSION(4, 0, 0)
int get_height_rows(int nRowHeight, int nSeparatorHeight, int nRows)
{
return (nRowHeight * nRows) + (nSeparatorHeight * (nRows + 1));
}
#endif
tools::Rectangle get_row_area(GtkTreeView* pTreeView, GList* pColumns, GtkTreePath* pPath)
{
tools::Rectangle aRet;
GdkRectangle aRect;
for (GList* pEntry = g_list_last(pColumns); pEntry; pEntry = g_list_previous(pEntry))
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pEntry->data);
gtk_tree_view_get_cell_area(pTreeView, pPath, pColumn, &aRect);
aRet.Union(tools::Rectangle(aRect.x, aRect.y, aRect.x + aRect.width, aRect.y + aRect.height));
}
return aRet;
}
struct GtkTreeRowReferenceDeleter
{
void operator()(GtkTreeRowReference* p) const
{
gtk_tree_row_reference_free(p);
}
};
bool separator_function(const GtkTreePath* path, const std::vector<std::unique_ptr<GtkTreeRowReference, GtkTreeRowReferenceDeleter>>& rSeparatorRows)
{
bool bFound = false;
for (auto& a : rSeparatorRows)
{
GtkTreePath* seppath = gtk_tree_row_reference_get_path(a.get());
if (seppath)
{
bFound = gtk_tree_path_compare(path, seppath) == 0;
gtk_tree_path_free(seppath);
}
if (bFound)
break;
}
return bFound;
}
void tree_store_set(GtkTreeModel* pTreeModel, GtkTreeIter *pIter, ...)
{
va_list args;
va_start(args, pIter);
gtk_tree_store_set_valist(GTK_TREE_STORE(pTreeModel), pIter, args);
va_end(args);
}
void list_store_set(GtkTreeModel* pTreeModel, GtkTreeIter *pIter, ...)
{
va_list args;
va_start(args, pIter);
gtk_list_store_set_valist(GTK_LIST_STORE(pTreeModel), pIter, args);
va_end(args);
}
void tree_store_insert_with_values(GtkTreeModel* pTreeModel, GtkTreeIter *pIter, GtkTreeIter *pParent, gint nPos,
gint nTextCol, const gchar* pText,
gint nIdCol, const gchar* pId)
{
gtk_tree_store_insert_with_values(GTK_TREE_STORE(pTreeModel), pIter, pParent, nPos,
nTextCol, pText, nIdCol, pId, -1);
}
void list_store_insert_with_values(GtkTreeModel* pTreeModel, GtkTreeIter *pIter, GtkTreeIter *pParent, gint nPos,
gint nTextCol, const gchar* pText,
gint nIdCol, const gchar* pId)
{
assert(!pParent); (void)pParent;
gtk_list_store_insert_with_values(GTK_LIST_STORE(pTreeModel), pIter, nPos,
nTextCol, pText, nIdCol, pId, -1);
}
void tree_store_prepend(GtkTreeModel* pTreeModel, GtkTreeIter *pIter, GtkTreeIter *pParent)
{
gtk_tree_store_prepend(GTK_TREE_STORE(pTreeModel), pIter, pParent);
}
void list_store_prepend(GtkTreeModel* pTreeModel, GtkTreeIter *pIter, GtkTreeIter *pParent)
{
assert(!pParent); (void)pParent;
gtk_list_store_prepend(GTK_LIST_STORE(pTreeModel), pIter);
}
void tree_store_insert(GtkTreeModel* pTreeModel, GtkTreeIter *pIter, GtkTreeIter *pParent, gint nPosition)
{
gtk_tree_store_insert(GTK_TREE_STORE(pTreeModel), pIter, pParent, nPosition);
}
void list_store_insert(GtkTreeModel* pTreeModel, GtkTreeIter *pIter, GtkTreeIter *pParent, gint nPosition)
{
assert(!pParent); (void)pParent;
gtk_list_store_insert(GTK_LIST_STORE(pTreeModel), pIter, nPosition);
}
void tree_store_clear(GtkTreeModel* pTreeModel)
{
gtk_tree_store_clear(GTK_TREE_STORE(pTreeModel));
}
void list_store_clear(GtkTreeModel* pTreeModel)
{
gtk_list_store_clear(GTK_LIST_STORE(pTreeModel));
}
bool tree_store_remove(GtkTreeModel* pTreeModel, GtkTreeIter *pIter)
{
return gtk_tree_store_remove(GTK_TREE_STORE(pTreeModel), pIter);
}
bool list_store_remove(GtkTreeModel* pTreeModel, GtkTreeIter *pIter)
{
return gtk_list_store_remove(GTK_LIST_STORE(pTreeModel), pIter);
}
void tree_store_swap(GtkTreeModel* pTreeModel, GtkTreeIter* pIter1, GtkTreeIter* pIter2)
{
gtk_tree_store_swap(GTK_TREE_STORE(pTreeModel), pIter1, pIter2);
}
void list_store_swap(GtkTreeModel* pTreeModel, GtkTreeIter* pIter1, GtkTreeIter* pIter2)
{
gtk_list_store_swap(GTK_LIST_STORE(pTreeModel), pIter1, pIter2);
}
void tree_store_set_value(GtkTreeModel* pTreeModel, GtkTreeIter* pIter, gint nColumn, GValue* pValue)
{
gtk_tree_store_set_value(GTK_TREE_STORE(pTreeModel), pIter, nColumn, pValue);
}
void list_store_set_value(GtkTreeModel* pTreeModel, GtkTreeIter* pIter, gint nColumn, GValue* pValue)
{
gtk_list_store_set_value(GTK_LIST_STORE(pTreeModel), pIter, nColumn, pValue);
}
int promote_arg(bool bArg)
{
return static_cast<int>(bArg);
}
class GtkInstanceTreeView : public GtkInstanceWidget, public virtual weld::TreeView
{
private:
GtkTreeView* m_pTreeView;
GtkTreeModel* m_pTreeModel;
typedef void(*setterFnc)(GtkTreeModel*, GtkTreeIter*, ...);
setterFnc m_Setter;
typedef void(*insertWithValuesFnc)(GtkTreeModel*, GtkTreeIter*, GtkTreeIter*, gint, gint, const gchar*, gint, const gchar*);
insertWithValuesFnc m_InsertWithValues;
typedef void(*insertFnc)(GtkTreeModel*, GtkTreeIter*, GtkTreeIter*, gint);
insertFnc m_Insert;
typedef void(*prependFnc)(GtkTreeModel*, GtkTreeIter*, GtkTreeIter*);
prependFnc m_Prepend;
typedef void(*clearFnc)(GtkTreeModel*);
clearFnc m_Clear;
typedef bool(*removeFnc)(GtkTreeModel*, GtkTreeIter*);
removeFnc m_Remove;
typedef void(*swapFnc)(GtkTreeModel*, GtkTreeIter*, GtkTreeIter*);
swapFnc m_Swap;
typedef void(*setValueFnc)(GtkTreeModel*, GtkTreeIter*, gint, GValue*);
setValueFnc m_SetValue;
std::unique_ptr<comphelper::string::NaturalStringSorter> m_xSorter;
GList *m_pColumns;
std::vector<gulong> m_aColumnSignalIds;
// map from toggle column to toggle visibility column
std::map<int, int> m_aToggleVisMap;
// map from toggle column to tristate column
std::map<int, int> m_aToggleTriStateMap;
// map from text column to text weight column
std::map<int, int> m_aWeightMap;
// map from text column to sensitive column
std::map<int, int> m_aSensitiveMap;
// map from text column to indent column
std::map<int, int> m_aIndentMap;
// map from text column to text align column
std::map<int, int> m_aAlignMap;
// currently expanding parent that logically, but not currently physically,
// contain placeholders
o3tl::sorted_vector<GtkTreePath*, CompareGtkTreePath> m_aExpandingPlaceHolderParents;
// which rows are separators (rare)
std::vector<std::unique_ptr<GtkTreeRowReference, GtkTreeRowReferenceDeleter>> m_aSeparatorRows;
std::vector<GtkSortType> m_aSavedSortTypes;
std::vector<int> m_aSavedSortColumns;
bool m_bWorkAroundBadDragRegion;
bool m_bInDrag;
bool m_bChangedByMouse;
gint m_nTextCol;
gint m_nTextView;
gint m_nImageCol;
gint m_nExpanderToggleCol;
gint m_nExpanderImageCol;
gint m_nIdCol;
int m_nPendingVAdjustment;
gulong m_nChangedSignalId;
gulong m_nRowActivatedSignalId;
gulong m_nTestExpandRowSignalId;
gulong m_nTestCollapseRowSignalId;
gulong m_nVAdjustmentChangedSignalId;
gulong m_nRowDeletedSignalId;
gulong m_nRowInsertedSignalId;
#if !GTK_CHECK_VERSION(4, 0, 0)
gulong m_nPopupMenuSignalId;
gulong m_nKeyPressSignalId;
gulong m_nCrossingSignalid;
#endif
gulong m_nQueryTooltipSignalId;
GtkAdjustment* m_pVAdjustment;
ImplSVEvent* m_pChangeEvent;
DECL_LINK(async_signal_changed, void*, void);
void launch_signal_changed()
{
//tdf#117991 selection change is sent before the focus change, and focus change
//is what will cause a spinbutton that currently has the focus to set its contents
//as the spin button value. So any LibreOffice callbacks on
//signal-change would happen before the spinbutton value-change occurs.
//To avoid this, send the signal-change to LibreOffice to occur after focus-change
//has been processed
if (m_pChangeEvent)
Application::RemoveUserEvent(m_pChangeEvent);
#if !GTK_CHECK_VERSION(4, 0, 0)
GdkEvent *pEvent = gtk_get_current_event();
m_bChangedByMouse = pEvent && categorizeEvent(pEvent) == VclInputFlags::MOUSE;
#else
//TODO maybe iterate over gtk_widget_observe_controllers looking for a motion controller
#endif
m_pChangeEvent = Application::PostUserEvent(LINK(this, GtkInstanceTreeView, async_signal_changed));
}
static void signalChanged(GtkTreeView*, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
pThis->launch_signal_changed();
}
void handle_row_activated()
{
if (signal_row_activated())
return;
GtkInstanceTreeIter aIter(nullptr);
if (!get_cursor(&aIter))
return;
if (gtk_tree_model_iter_has_child(m_pTreeModel, &aIter.iter))
get_row_expanded(aIter) ? collapse_row(aIter) : expand_row(aIter);
}
static void signalRowActivated(GtkTreeView*, GtkTreePath*, GtkTreeViewColumn*, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
SolarMutexGuard aGuard;
pThis->handle_row_activated();
}
virtual bool signal_popup_menu(const CommandEvent& rCEvt) override
{
return m_aPopupMenuHdl.Call(rCEvt);
}
void insert_row(GtkTreeIter& iter, const GtkTreeIter* parent, int pos, const OUString* pId, const OUString* pText,
const OUString* pIconName, const VirtualDevice* pDevice)
{
m_InsertWithValues(m_pTreeModel, &iter, const_cast<GtkTreeIter*>(parent), pos,
m_nTextCol, !pText ? nullptr : OUStringToOString(*pText, RTL_TEXTENCODING_UTF8).getStr(),
m_nIdCol, !pId ? nullptr : OUStringToOString(*pId, RTL_TEXTENCODING_UTF8).getStr());
if (pIconName)
{
GdkPixbuf* pixbuf = getPixbuf(*pIconName);
m_Setter(m_pTreeModel, &iter, m_nImageCol, pixbuf, -1);
if (pixbuf)
g_object_unref(pixbuf);
}
else if (pDevice)
{
cairo_surface_t* surface = get_underlying_cairo_surface(*pDevice);
Size aSize(pDevice->GetOutputSizePixel());
cairo_surface_t* target = cairo_surface_create_similar(surface,
cairo_surface_get_content(surface),
aSize.Width(),
aSize.Height());
cairo_t* cr = cairo_create(target);
cairo_set_source_surface(cr, surface, 0, 0);
cairo_paint(cr);
cairo_destroy(cr);
m_Setter(m_pTreeModel, &iter, m_nImageCol, target, -1);
cairo_surface_destroy(target);
}
}
bool separator_function(const GtkTreePath* path)
{
return ::separator_function(path, m_aSeparatorRows);
}
static gboolean separatorFunction(GtkTreeModel* pTreeModel, GtkTreeIter* pIter, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
GtkTreePath* path = gtk_tree_model_get_path(pTreeModel, pIter);
bool bRet = pThis->separator_function(path);
gtk_tree_path_free(path);
return bRet;
}
OUString get(const GtkTreeIter& iter, int col) const
{
gchar* pStr;
gtk_tree_model_get(m_pTreeModel, const_cast<GtkTreeIter*>(&iter), col, &pStr, -1);
OUString sRet(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
g_free(pStr);
return sRet;
}
OUString get(int pos, int col) const
{
OUString sRet;
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
sRet = get(iter, col);
return sRet;
}
gint get_int(const GtkTreeIter& iter, int col) const
{
gint nRet(-1);
gtk_tree_model_get(m_pTreeModel, const_cast<GtkTreeIter*>(&iter), col, &nRet, -1);
return nRet;
}
gint get_int(int pos, int col) const
{
gint nRet(-1);
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
nRet = get_int(iter, col);
gtk_tree_model_get(m_pTreeModel, &iter, col, &nRet, -1);
return nRet;
}
bool get_bool(const GtkTreeIter& iter, int col) const
{
gboolean bRet(false);
gtk_tree_model_get(m_pTreeModel, const_cast<GtkTreeIter*>(&iter), col, &bRet, -1);
return bRet;
}
bool get_bool(int pos, int col) const
{
bool bRet(false);
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
bRet = get_bool(iter, col);
return bRet;
}
void set_toggle(const GtkTreeIter& iter, TriState eState, int col)
{
if (col == -1)
col = m_nExpanderToggleCol;
else
col = to_internal_model(col);
if (eState == TRISTATE_INDET)
{
m_Setter(m_pTreeModel, const_cast<GtkTreeIter*>(&iter),
m_aToggleVisMap[col], promote_arg(true), // checkbuttons are invisible until toggled on or off
m_aToggleTriStateMap[col], promote_arg(true), // tristate on
-1);
}
else
{
m_Setter(m_pTreeModel, const_cast<GtkTreeIter*>(&iter),
m_aToggleVisMap[col], promote_arg(true), // checkbuttons are invisible until toggled on or off
m_aToggleTriStateMap[col], promote_arg(false), // tristate off
col, promote_arg(eState == TRISTATE_TRUE), // set toggle state
-1);
}
}
void set(const GtkTreeIter& iter, int col, std::u16string_view rText)
{
OString aStr(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
m_Setter(m_pTreeModel, const_cast<GtkTreeIter*>(&iter), col, aStr.getStr(), -1);
}
void set(int pos, int col, std::u16string_view rText)
{
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
set(iter, col, rText);
}
void set(const GtkTreeIter& iter, int col, bool bOn)
{
m_Setter(m_pTreeModel, const_cast<GtkTreeIter*>(&iter), col, promote_arg(bOn), -1);
}
void set(int pos, int col, bool bOn)
{
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
set(iter, col, bOn);
}
void set(const GtkTreeIter& iter, int col, gint bInt)
{
m_Setter(m_pTreeModel, const_cast<GtkTreeIter*>(&iter), col, bInt, -1);
}
void set(int pos, int col, gint bInt)
{
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
set(iter, col, bInt);
}
void set(const GtkTreeIter& iter, int col, double fValue)
{
m_Setter(m_pTreeModel, const_cast<GtkTreeIter*>(&iter), col, fValue, -1);
}
void set(int pos, int col, double fValue)
{
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
set(iter, col, fValue);
}
static gboolean signalTestExpandRow(GtkTreeView*, GtkTreeIter* iter, GtkTreePath*, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
return !pThis->signal_test_expand_row(*iter);
}
static gboolean signalTestCollapseRow(GtkTreeView*, GtkTreeIter* iter, GtkTreePath*, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
return !pThis->signal_test_collapse_row(*iter);
}
bool child_is_placeholder(GtkInstanceTreeIter& rGtkIter) const
{
GtkTreePath* pPath = gtk_tree_model_get_path(m_pTreeModel, &rGtkIter.iter);
bool bExpanding = m_aExpandingPlaceHolderParents.count(pPath);
gtk_tree_path_free(pPath);
if (bExpanding)
return true;
bool bPlaceHolder = false;
GtkTreeIter tmp;
if (gtk_tree_model_iter_children(m_pTreeModel, &tmp, &rGtkIter.iter))
{
rGtkIter.iter = tmp;
if (get_text(rGtkIter, -1) == "<dummy>")
{
bPlaceHolder = true;
}
}
return bPlaceHolder;
}
bool signal_test_expand_row(GtkTreeIter& iter)
{
disable_notify_events();
// if there's a preexisting placeholder child, required to make this
// potentially expandable in the first place, now we remove it
GtkInstanceTreeIter aIter(iter);
GtkTreePath* pPlaceHolderPath = nullptr;
bool bPlaceHolder = child_is_placeholder(aIter);
if (bPlaceHolder)
{
m_Remove(m_pTreeModel, &aIter.iter);
pPlaceHolderPath = gtk_tree_model_get_path(m_pTreeModel, &iter);
m_aExpandingPlaceHolderParents.insert(pPlaceHolderPath);
}
aIter.iter = iter;
bool bRet = signal_expanding(aIter);
if (bPlaceHolder)
{
//expand disallowed, restore placeholder
if (!bRet)
{
GtkTreeIter subiter;
OUString sDummy("<dummy>");
insert_row(subiter, &iter, -1, nullptr, &sDummy, nullptr, nullptr);
}
m_aExpandingPlaceHolderParents.erase(pPlaceHolderPath);
gtk_tree_path_free(pPlaceHolderPath);
}
enable_notify_events();
return bRet;
}
bool signal_test_collapse_row(const GtkTreeIter& iter)
{
disable_notify_events();
GtkInstanceTreeIter aIter(iter);
bool bRet = signal_collapsing(aIter);
enable_notify_events();
return bRet;
}
static void signalCellToggled(GtkCellRendererToggle* pCell, const gchar *path, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
void* pData = g_object_get_data(G_OBJECT(pCell), "g-lo-CellIndex");
pThis->signal_cell_toggled(path, reinterpret_cast<sal_IntPtr>(pData));
}
void signal_cell_toggled(const gchar *path, int nCol)
{
GtkTreePath *tree_path = gtk_tree_path_new_from_string(path);
// additionally set the cursor into the row the toggled element is in
gtk_tree_view_set_cursor(m_pTreeView, tree_path, nullptr, false);
GtkTreeIter iter;
gtk_tree_model_get_iter(m_pTreeModel, &iter, tree_path);
gboolean bRet(false);
gtk_tree_model_get(m_pTreeModel, &iter, nCol, &bRet, -1);
bRet = !bRet;
m_Setter(m_pTreeModel, &iter, nCol, bRet, -1);
set(iter, m_aToggleTriStateMap[nCol], false);
signal_toggled(iter_col(GtkInstanceTreeIter(iter), to_external_model(nCol)));
gtk_tree_path_free(tree_path);
}
DECL_LINK(async_stop_cell_editing, void*, void);
static void signalCellEditingStarted(GtkCellRenderer*, GtkCellEditable*, const gchar *path, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
if (!pThis->signal_cell_editing_started(path))
Application::PostUserEvent(LINK(pThis, GtkInstanceTreeView, async_stop_cell_editing));
}
bool signal_cell_editing_started(const gchar *path)
{
GtkTreePath *tree_path = gtk_tree_path_new_from_string(path);
GtkInstanceTreeIter aGtkIter(nullptr);
gtk_tree_model_get_iter(m_pTreeModel, &aGtkIter.iter, tree_path);
gtk_tree_path_free(tree_path);
return signal_editing_started(aGtkIter);
}
static void signalCellEdited(GtkCellRendererText* pCell, const gchar *path, const gchar *pNewText, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
pThis->signal_cell_edited(pCell, path, pNewText);
}
static void restoreNonEditable(GObject* pCell)
{
if (g_object_get_data(pCell, "g-lo-RestoreNonEditable"))
{
g_object_set(pCell, "editable", false, "editable-set", false, nullptr);
g_object_set_data(pCell, "g-lo-RestoreNonEditable", reinterpret_cast<gpointer>(false));
}
}
void signal_cell_edited(GtkCellRendererText* pCell, const gchar *path, const gchar* pNewText)
{
GtkTreePath *tree_path = gtk_tree_path_new_from_string(path);
GtkInstanceTreeIter aGtkIter(nullptr);
gtk_tree_model_get_iter(m_pTreeModel, &aGtkIter.iter, tree_path);
gtk_tree_path_free(tree_path);
OUString sText(pNewText, pNewText ? strlen(pNewText) : 0, RTL_TEXTENCODING_UTF8);
if (signal_editing_done(iter_string(aGtkIter, sText)))
{
void* pData = g_object_get_data(G_OBJECT(pCell), "g-lo-CellIndex");
set(aGtkIter.iter, reinterpret_cast<sal_IntPtr>(pData), sText);
}
restoreNonEditable(G_OBJECT(pCell));
}
static void signalCellEditingCanceled(GtkCellRenderer* pCell, gpointer /*widget*/)
{
restoreNonEditable(G_OBJECT(pCell));
}
void signal_column_clicked(GtkTreeViewColumn* pClickedColumn)
{
int nIndex(0);
for (GList* pEntry = g_list_first(m_pColumns); pEntry; pEntry = g_list_next(pEntry))
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pEntry->data);
if (pColumn == pClickedColumn)
{
TreeView::signal_column_clicked(nIndex);
break;
}
++nIndex;
}
}
static void signalColumnClicked(GtkTreeViewColumn* pColumn, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
pThis->signal_column_clicked(pColumn);
}
static void signalVAdjustmentChanged(GtkAdjustment*, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
pThis->signal_visible_range_changed();
}
// The outside concept of a column maps to a gtk CellRenderer, rather than
// a TreeViewColumn. If the first TreeViewColumn has a leading Toggle Renderer
// and/or a leading Image Renderer, those are considered special expander
// columns and precede index 0 and can be accessed via outside index -1
int to_external_model(int modelcol) const
{
if (m_nExpanderToggleCol != -1)
--modelcol;
if (m_nExpanderImageCol != -1)
--modelcol;
return modelcol;
}
int to_internal_model(int modelcol) const
{
if (m_nExpanderToggleCol != -1)
++modelcol;
if (m_nExpanderImageCol != -1)
++modelcol;
return modelcol;
}
void set_column_editable(int nCol, bool bEditable)
{
nCol = to_internal_model(nCol);
for (GList* pEntry = g_list_first(m_pColumns); pEntry; pEntry = g_list_next(pEntry))
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pEntry->data);
GList *pRenderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(pColumn));
for (GList* pRenderer = g_list_first(pRenderers); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
void* pData = g_object_get_data(G_OBJECT(pCellRenderer), "g-lo-CellIndex");
if (reinterpret_cast<sal_IntPtr>(pData) == nCol)
{
g_object_set(G_OBJECT(pCellRenderer), "editable", bEditable, "editable-set", true, nullptr);
break;
}
}
g_list_free(pRenderers);
}
}
static void signalRowDeleted(GtkTreeModel*, GtkTreePath*, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
pThis->signal_model_changed();
}
static void signalRowInserted(GtkTreeModel*, GtkTreePath*, GtkTreeIter*, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
pThis->signal_model_changed();
}
static gint sortFunc(GtkTreeModel* pModel, GtkTreeIter* a, GtkTreeIter* b, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
return pThis->sort_func(pModel, a, b);
}
gint sort_func(GtkTreeModel* pModel, GtkTreeIter* a, GtkTreeIter* b)
{
if (m_aCustomSort)
return m_aCustomSort(GtkInstanceTreeIter(*a), GtkInstanceTreeIter(*b));
return default_sort_func(pModel, a, b, m_xSorter.get());
}
#if !GTK_CHECK_VERSION(4, 0, 0)
bool signal_key_press(GdkEventKey* pEvent)
{
if (pEvent->keyval != GDK_KEY_Left && pEvent->keyval != GDK_KEY_Right)
return false;
GtkInstanceTreeIter aIter(nullptr);
if (!get_cursor(&aIter))
return false;
bool bHasChild = gtk_tree_model_iter_has_child(m_pTreeModel, &aIter.iter);
if (pEvent->keyval == GDK_KEY_Right)
{
if (bHasChild && !get_row_expanded(aIter))
{
expand_row(aIter);
return true;
}
return false;
}
if (bHasChild && get_row_expanded(aIter))
{
collapse_row(aIter);
return true;
}
if (iter_parent(aIter))
{
unselect_all();
set_cursor(aIter);
select(aIter);
return true;
}
return false;
}
static gboolean signalKeyPress(GtkWidget*, GdkEventKey* pEvent, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
return pThis->signal_key_press(pEvent);
}
#endif
static gboolean signalQueryTooltip(GtkWidget* /*pGtkWidget*/, gint x, gint y,
gboolean keyboard_tip, GtkTooltip *tooltip,
gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
GtkTreeIter iter;
GtkTreeView *pTreeView = pThis->m_pTreeView;
GtkTreeModel *pModel = gtk_tree_view_get_model(pTreeView);
GtkTreePath *pPath = nullptr;
#if GTK_CHECK_VERSION(4, 0, 0)
if (!gtk_tree_view_get_tooltip_context(pTreeView, x, y, keyboard_tip, &pModel, &pPath, &iter))
return false;
#else
if (!gtk_tree_view_get_tooltip_context(pTreeView, &x, &y, keyboard_tip, &pModel, &pPath, &iter))
return false;
#endif
OUString aTooltip = pThis->signal_query_tooltip(GtkInstanceTreeIter(iter));
if (!aTooltip.isEmpty())
{
gtk_tooltip_set_text(tooltip, OUStringToOString(aTooltip, RTL_TEXTENCODING_UTF8).getStr());
gtk_tree_view_set_tooltip_row(pTreeView, tooltip, pPath);
}
gtk_tree_path_free(pPath);
return !aTooltip.isEmpty();
}
void last_child(GtkTreeModel* pModel, GtkTreeIter* result, GtkTreeIter* pParent, int nChildren) const
{
gtk_tree_model_iter_nth_child(pModel, result, pParent, nChildren - 1);
nChildren = gtk_tree_model_iter_n_children(pModel, result);
if (nChildren)
{
GtkTreeIter newparent(*result);
last_child(pModel, result, &newparent, nChildren);
}
}
GtkTreePath* get_path_of_last_entry(GtkTreeModel *pModel)
{
GtkTreePath *lastpath;
// find the last entry in the model for comparison
int nChildren = gtk_tree_model_iter_n_children(pModel, nullptr);
if (!nChildren)
lastpath = gtk_tree_path_new_from_indices(0, -1);
else
{
GtkTreeIter iter;
last_child(pModel, &iter, nullptr, nChildren);
lastpath = gtk_tree_model_get_path(pModel, &iter);
}
return lastpath;
}
void set_font_color(const GtkTreeIter& iter, const Color& rColor)
{
if (rColor == COL_AUTO)
m_Setter(m_pTreeModel, const_cast<GtkTreeIter*>(&iter), m_nIdCol + 1, nullptr, -1);
else
{
GdkRGBA aColor{rColor.GetRed()/255.0f, rColor.GetGreen()/255.0f, rColor.GetBlue()/255.0f, 0};
m_Setter(m_pTreeModel, const_cast<GtkTreeIter*>(&iter), m_nIdCol + 1, &aColor, -1);
}
}
int get_expander_size() const
{
// gtk4: _TREE_VIEW_EXPANDER_SIZE define in gtk/gtktreeview.c
gint nExpanderSize = 16;
// gtk4: _TREE_VIEW_HORIZONTAL_SEPARATOR define in gtk/gtktreeview.c
gint nHorizontalSeparator = 4;
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_style_get(GTK_WIDGET(m_pTreeView),
"expander-size", &nExpanderSize,
"horizontal-separator", &nHorizontalSeparator,
nullptr);
#endif
return nExpanderSize + (nHorizontalSeparator/ 2);
}
void real_vadjustment_set_value(int value)
{
disable_notify_events();
gtk_adjustment_set_value(m_pVAdjustment, value);
enable_notify_events();
}
static gboolean setAdjustmentCallback(GtkWidget*, GdkFrameClock*, gpointer widget)
{
GtkInstanceTreeView* pThis = static_cast<GtkInstanceTreeView*>(widget);
if (pThis->m_nPendingVAdjustment != -1)
{
pThis->real_vadjustment_set_value(pThis->m_nPendingVAdjustment);
pThis->m_nPendingVAdjustment = -1;
}
return false;
}
bool iter_next(weld::TreeIter& rIter, bool bOnlyExpanded) const
{
GtkInstanceTreeIter& rGtkIter = static_cast<GtkInstanceTreeIter&>(rIter);
GtkTreeIter tmp;
GtkTreeIter iter = rGtkIter.iter;
bool ret = gtk_tree_model_iter_children(m_pTreeModel, &tmp, &iter);
if (ret && bOnlyExpanded && !get_row_expanded(rGtkIter))
ret = false;
rGtkIter.iter = tmp;
if (ret)
{
//on-demand dummy entry doesn't count
if (get_text(rGtkIter, -1) == "<dummy>")
return iter_next(rGtkIter, bOnlyExpanded);
return true;
}
tmp = iter;
if (gtk_tree_model_iter_next(m_pTreeModel, &tmp))
{
rGtkIter.iter = tmp;
//on-demand dummy entry doesn't count
if (get_text(rGtkIter, -1) == "<dummy>")
return iter_next(rGtkIter, bOnlyExpanded);
return true;
}
// Move up level(s) until we find the level where the next node exists.
while (gtk_tree_model_iter_parent(m_pTreeModel, &tmp, &iter))
{
iter = tmp;
if (gtk_tree_model_iter_next(m_pTreeModel, &tmp))
{
rGtkIter.iter = tmp;
//on-demand dummy entry doesn't count
if (get_text(rGtkIter, -1) == "<dummy>")
return iter_next(rGtkIter, bOnlyExpanded);
return true;
}
}
return false;
}
#if !GTK_CHECK_VERSION(4, 0, 0)
// tdf#154565 ignore the crossing event if it was triggered ultimately by a
// key stroke which is likely from exiting the search box. This way we can
// avoid the problem that with hover-selection that after return is used in
// the search box, selecting a matching row, that during teardown of the
// widget the box is hidden, and the crossing notification triggers
// selection of a different row under the mouse. If needs be this could be
// refined further to only happen for a specific key or other details of
// the triggering event
static gboolean signalCrossing(GtkWidget*, GdkEventCrossing*, gpointer)
{
if (GdkEvent *pEvent = gtk_get_current_event())
{
const bool bCrossingTriggeredByKeyStroke = gdk_event_get_event_type(pEvent) == GDK_KEY_PRESS;
gdk_event_free(pEvent);
return bCrossingTriggeredByKeyStroke;
}
return false;
}
#endif
public:
GtkInstanceTreeView(GtkTreeView* pTreeView, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pTreeView), pBuilder, bTakeOwnership)
, m_pTreeView(pTreeView)
, m_pTreeModel(gtk_tree_view_get_model(m_pTreeView))
, m_bWorkAroundBadDragRegion(false)
, m_bInDrag(false)
, m_bChangedByMouse(false)
, m_nTextCol(-1)
, m_nTextView(-1)
, m_nImageCol(-1)
, m_nExpanderToggleCol(-1)
, m_nExpanderImageCol(-1)
, m_nPendingVAdjustment(-1)
, m_nChangedSignalId(g_signal_connect(gtk_tree_view_get_selection(pTreeView), "changed",
G_CALLBACK(signalChanged), this))
, m_nRowActivatedSignalId(g_signal_connect(pTreeView, "row-activated", G_CALLBACK(signalRowActivated), this))
, m_nTestExpandRowSignalId(g_signal_connect(pTreeView, "test-expand-row", G_CALLBACK(signalTestExpandRow), this))
, m_nTestCollapseRowSignalId(g_signal_connect(pTreeView, "test-collapse-row", G_CALLBACK(signalTestCollapseRow), this))
, m_nVAdjustmentChangedSignalId(0)
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_nPopupMenuSignalId(g_signal_connect(pTreeView, "popup-menu", G_CALLBACK(signalPopupMenu), this))
, m_nKeyPressSignalId(g_signal_connect(pTreeView, "key-press-event", G_CALLBACK(signalKeyPress), this))
, m_nCrossingSignalid(g_signal_connect(pTreeView, "enter-notify-event", G_CALLBACK(signalCrossing), this))
#endif
, m_nQueryTooltipSignalId(0)
, m_pVAdjustment(gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(pTreeView)))
, m_pChangeEvent(nullptr)
{
if (GTK_IS_TREE_STORE(m_pTreeModel))
{
m_Setter = tree_store_set;
m_InsertWithValues = tree_store_insert_with_values;
m_Insert = tree_store_insert;
m_Prepend = tree_store_prepend;
m_Remove = tree_store_remove;
m_Swap = tree_store_swap;
m_SetValue = tree_store_set_value;
m_Clear = tree_store_clear;
}
else
{
/*
tdf#136559 see: https://gitlab.gnome.org/GNOME/gtk/-/issues/2693
If we only need a list and not a tree we can get a performance boost from using a ListStore
*/
assert(!gtk_tree_view_get_show_expanders(m_pTreeView) && "a liststore can only be used if no tree structure is needed");
m_Setter = list_store_set;
m_InsertWithValues = list_store_insert_with_values;
m_Insert = list_store_insert;
m_Prepend = list_store_prepend;
m_Remove = list_store_remove;
m_Swap = list_store_swap;
m_SetValue = list_store_set_value;
m_Clear = list_store_clear;
}
/* The outside concept of a column maps to a gtk CellRenderer, rather than
a TreeViewColumn. If the first TreeViewColumn has a leading Toggle Renderer
and/or a leading Image Renderer, those are considered special expander
columns and precede index 0 and can be accessed via outside index -1
*/
m_pColumns = gtk_tree_view_get_columns(m_pTreeView);
int nIndex(0);
int nViewColumn(0);
for (GList* pEntry = g_list_first(m_pColumns); pEntry; pEntry = g_list_next(pEntry))
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pEntry->data);
m_aColumnSignalIds.push_back(g_signal_connect(pColumn, "clicked", G_CALLBACK(signalColumnClicked), this));
GList *pRenderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(pColumn));
for (GList* pRenderer = g_list_first(pRenderers); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
if (GTK_IS_CELL_RENDERER_TEXT(pCellRenderer))
{
if (m_nTextCol == -1)
{
m_nTextCol = nIndex;
m_nTextView = nViewColumn;
}
m_aWeightMap[nIndex] = -1;
m_aSensitiveMap[nIndex] = -1;
m_aIndentMap[nIndex] = -1;
m_aAlignMap[nIndex] = -1;
g_signal_connect(G_OBJECT(pCellRenderer), "editing-started", G_CALLBACK(signalCellEditingStarted), this);
g_signal_connect(G_OBJECT(pCellRenderer), "editing-canceled", G_CALLBACK(signalCellEditingCanceled), this);
g_signal_connect(G_OBJECT(pCellRenderer), "edited", G_CALLBACK(signalCellEdited), this);
}
else if (GTK_IS_CELL_RENDERER_TOGGLE(pCellRenderer))
{
const bool bExpander = nIndex == 0 || (nIndex == 1 && m_nExpanderImageCol == 0);
if (bExpander)
m_nExpanderToggleCol = nIndex;
g_signal_connect(G_OBJECT(pCellRenderer), "toggled", G_CALLBACK(signalCellToggled), this);
m_aToggleVisMap[nIndex] = -1;
m_aToggleTriStateMap[nIndex] = -1;
}
else if (GTK_IS_CELL_RENDERER_PIXBUF(pCellRenderer))
{
const bool bExpander = g_list_next(pRenderer) != nullptr;
if (bExpander && m_nExpanderImageCol == -1)
m_nExpanderImageCol = nIndex;
else if (m_nImageCol == -1)
m_nImageCol = nIndex;
}
g_object_set_data(G_OBJECT(pCellRenderer), "g-lo-CellIndex", reinterpret_cast<gpointer>(nIndex));
++nIndex;
}
g_list_free(pRenderers);
++nViewColumn;
}
m_nIdCol = nIndex++;
for (auto& a : m_aToggleVisMap)
a.second = nIndex++;
for (auto& a : m_aToggleTriStateMap)
a.second = nIndex++;
for (auto& a : m_aWeightMap)
a.second = nIndex++;
for (auto& a : m_aSensitiveMap)
a.second = nIndex++;
for (auto& a : m_aIndentMap)
a.second = nIndex++;
for (auto& a : m_aAlignMap)
a.second = nIndex++;
ensure_drag_begin_end();
m_nRowDeletedSignalId = g_signal_connect(m_pTreeModel, "row-deleted", G_CALLBACK(signalRowDeleted), this);
m_nRowInsertedSignalId = g_signal_connect(m_pTreeModel, "row-inserted", G_CALLBACK(signalRowInserted), this);
}
virtual void connect_query_tooltip(const Link<const weld::TreeIter&, OUString>& rLink) override
{
weld::TreeView::connect_query_tooltip(rLink);
m_nQueryTooltipSignalId = g_signal_connect(m_pTreeView, "query-tooltip", G_CALLBACK(signalQueryTooltip), this);
}
virtual void columns_autosize() override
{
gtk_tree_view_columns_autosize(m_pTreeView);
}
virtual void set_column_fixed_widths(const std::vector<int>& rWidths) override
{
GList* pEntry = g_list_first(m_pColumns);
for (auto nWidth : rWidths)
{
assert(pEntry && "wrong count");
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pEntry->data);
gtk_tree_view_column_set_fixed_width(pColumn, nWidth);
pEntry = g_list_next(pEntry);
}
}
virtual void set_column_editables(const std::vector<bool>& rEditables) override
{
size_t nTabCount = rEditables.size();
for (size_t i = 0 ; i < nTabCount; ++i)
set_column_editable(i, rEditables[i]);
}
virtual void set_centered_column(int nCol) override
{
for (GList* pEntry = g_list_first(m_pColumns); pEntry; pEntry = g_list_next(pEntry))
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pEntry->data);
GList *pRenderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(pColumn));
for (GList* pRenderer = g_list_first(pRenderers); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
void* pData = g_object_get_data(G_OBJECT(pCellRenderer), "g-lo-CellIndex");
if (reinterpret_cast<sal_IntPtr>(pData) == nCol)
{
g_object_set(G_OBJECT(pCellRenderer), "xalign", 0.5, nullptr);
break;
}
}
g_list_free(pRenderers);
}
}
virtual int get_column_width(int nColumn) const override
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(g_list_nth_data(m_pColumns, nColumn));
assert(pColumn && "wrong count");
int nWidth = gtk_tree_view_column_get_width(pColumn);
// https://github.com/exaile/exaile/issues/580
// after setting fixed_width on a column and requesting width before
// gtk has a chance to do its layout of the column means that the width
// request hasn't come into effect
if (!nWidth)
nWidth = gtk_tree_view_column_get_fixed_width(pColumn);
return nWidth;
}
virtual OUString get_column_title(int nColumn) const override
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(g_list_nth_data(m_pColumns, nColumn));
assert(pColumn && "wrong count");
const gchar* pTitle = gtk_tree_view_column_get_title(pColumn);
OUString sRet(pTitle, pTitle ? strlen(pTitle) : 0, RTL_TEXTENCODING_UTF8);
return sRet;
}
virtual void set_column_title(int nColumn, const OUString& rTitle) override
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(g_list_nth_data(m_pColumns, nColumn));
assert(pColumn && "wrong count");
gtk_tree_view_column_set_title(pColumn, OUStringToOString(rTitle, RTL_TEXTENCODING_UTF8).getStr());
}
virtual void set_column_custom_renderer(int nColumn, bool bEnable) override
{
assert(n_children() == 0 && "tree must be empty");
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(g_list_nth_data(m_pColumns, nColumn));
assert(pColumn && "wrong count");
GtkCellRenderer* pExpander = nullptr;
GtkCellRenderer* pToggle = nullptr;
// migrate existing editable setting to the new renderer
gboolean is_editable(false);
void* pEditCellData(nullptr);
GList *pRenderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(pColumn));
for (GList* pRenderer = g_list_first(pRenderers); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
void* pData = g_object_get_data(G_OBJECT(pCellRenderer), "g-lo-CellIndex");
auto nCellIndex = reinterpret_cast<sal_IntPtr>(pData);
if (GTK_IS_CELL_RENDERER_TEXT(pCellRenderer))
{
g_object_get(pCellRenderer, "editable", &is_editable, nullptr);
pEditCellData = pData;
break;
}
else if (GTK_IS_CELL_RENDERER_TOGGLE(pCellRenderer))
{
if (nCellIndex == m_nExpanderToggleCol)
{
pToggle = pCellRenderer;
g_object_ref(pToggle);
}
}
else if (GTK_IS_CELL_RENDERER_PIXBUF(pCellRenderer))
{
if (nCellIndex == m_nExpanderImageCol)
{
pExpander = pCellRenderer;
g_object_ref(pExpander);
}
}
}
g_list_free(pRenderers);
GtkCellRenderer* pRenderer;
gtk_cell_layout_clear(GTK_CELL_LAYOUT(pColumn));
if (pExpander)
{
gtk_tree_view_column_pack_start(pColumn, pExpander, false);
gtk_tree_view_column_add_attribute(pColumn, pExpander, "pixbuf", m_nExpanderImageCol);
g_object_unref(pExpander);
}
if (pToggle)
{
gtk_tree_view_column_pack_start(pColumn, pToggle, false);
gtk_tree_view_column_add_attribute(pColumn, pToggle, "active", m_nExpanderToggleCol);
gtk_tree_view_column_add_attribute(pColumn, pToggle, "active", m_nExpanderToggleCol);
gtk_tree_view_column_add_attribute(pColumn, pToggle, "visible", m_aToggleTriStateMap[m_nExpanderToggleCol]);
g_object_unref(pToggle);
}
if (bEnable)
{
pRenderer = custom_cell_renderer_new();
GValue value = G_VALUE_INIT;
g_value_init(&value, G_TYPE_POINTER);
g_value_set_pointer(&value, static_cast<gpointer>(this));
g_object_set_property(G_OBJECT(pRenderer), "instance", &value);
gtk_tree_view_column_pack_start(pColumn, pRenderer, true);
gtk_tree_view_column_add_attribute(pColumn, pRenderer, "text", m_nTextCol);
gtk_tree_view_column_add_attribute(pColumn, pRenderer, "id", m_nIdCol);
}
else
{
pRenderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(pColumn, pRenderer, true);
gtk_tree_view_column_add_attribute(pColumn, pRenderer, "text", m_nTextCol);
}
if (is_editable)
{
g_object_set(pRenderer, "editable", true, "editable-set", true, nullptr);
g_object_set_data(G_OBJECT(pRenderer), "g-lo-CellIndex", pEditCellData);
g_signal_connect(pRenderer, "editing-started", G_CALLBACK(signalCellEditingStarted), this);
g_signal_connect(pRenderer, "editing-canceled", G_CALLBACK(signalCellEditingCanceled), this);
g_signal_connect(pRenderer, "edited", G_CALLBACK(signalCellEdited), this);
}
}
virtual void queue_draw() override
{
gtk_widget_queue_draw(GTK_WIDGET(m_pTreeView));
}
virtual void insert(const weld::TreeIter* pParent, int pos, const OUString* pText, const OUString* pId, const OUString* pIconName,
VirtualDevice* pImageSurface,
bool bChildrenOnDemand, weld::TreeIter* pRet) override
{
disable_notify_events();
GtkTreeIter iter;
const GtkInstanceTreeIter* pGtkIter = static_cast<const GtkInstanceTreeIter*>(pParent);
insert_row(iter, pGtkIter ? &pGtkIter->iter : nullptr, pos, pId, pText, pIconName, pImageSurface);
if (bChildrenOnDemand)
{
GtkTreeIter subiter;
OUString sDummy("<dummy>");
insert_row(subiter, &iter, -1, nullptr, &sDummy, nullptr, nullptr);
}
if (pRet)
{
GtkInstanceTreeIter* pGtkRetIter = static_cast<GtkInstanceTreeIter*>(pRet);
pGtkRetIter->iter = iter;
}
enable_notify_events();
}
virtual void insert_separator(int pos, const OUString& rId) override
{
disable_notify_events();
GtkTreeIter iter;
if (!gtk_tree_view_get_row_separator_func(m_pTreeView))
gtk_tree_view_set_row_separator_func(m_pTreeView, separatorFunction, this, nullptr);
insert_row(iter, nullptr, pos, &rId, nullptr, nullptr, nullptr);
GtkTreePath* pPath = gtk_tree_model_get_path(m_pTreeModel, &iter);
m_aSeparatorRows.emplace_back(gtk_tree_row_reference_new(m_pTreeModel, pPath));
gtk_tree_path_free(pPath);
enable_notify_events();
}
virtual void set_font_color(int pos, const Color& rColor) override
{
GtkTreeIter iter;
gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos);
set_font_color(iter, rColor);
}
virtual void set_font_color(const weld::TreeIter& rIter, const Color& rColor) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
set_font_color(rGtkIter.iter, rColor);
}
virtual void remove(int pos) override
{
disable_notify_events();
GtkTreeIter iter;
gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos);
m_Remove(m_pTreeModel, &iter);
enable_notify_events();
}
virtual int find_text(const OUString& rText) const override
{
Search aSearch(rText, m_nTextCol);
gtk_tree_model_foreach(m_pTreeModel, foreach_find, &aSearch);
return aSearch.index;
}
virtual int find_id(const OUString& rId) const override
{
Search aSearch(rId, m_nIdCol);
gtk_tree_model_foreach(m_pTreeModel, foreach_find, &aSearch);
return aSearch.index;
}
virtual void bulk_insert_for_each(int nSourceCount, const std::function<void(weld::TreeIter&, int nSourceIndex)>& func,
const weld::TreeIter* pParent,
const std::vector<int>* pFixedWidths) override
{
GtkInstanceTreeIter* pGtkIter = const_cast<GtkInstanceTreeIter*>(static_cast<const GtkInstanceTreeIter*>(pParent));
freeze();
if (!pGtkIter)
clear();
else
{
GtkTreeIter restore(pGtkIter->iter);
if (iter_children(*pGtkIter))
while (m_Remove(m_pTreeModel, &pGtkIter->iter));
pGtkIter->iter = restore;
}
GtkInstanceTreeIter aGtkIter(nullptr);
if (pFixedWidths)
set_column_fixed_widths(*pFixedWidths);
while (nSourceCount)
{
// tdf#125241 inserting backwards is massively faster
m_Prepend(m_pTreeModel, &aGtkIter.iter, pGtkIter ? &pGtkIter->iter : nullptr);
func(aGtkIter, --nSourceCount);
}
thaw();
}
virtual void swap(int pos1, int pos2) override
{
disable_notify_events();
GtkTreeIter iter1;
gtk_tree_model_iter_nth_child(m_pTreeModel, &iter1, nullptr, pos1);
GtkTreeIter iter2;
gtk_tree_model_iter_nth_child(m_pTreeModel, &iter2, nullptr, pos2);
m_Swap(m_pTreeModel, &iter1, &iter2);
enable_notify_events();
}
virtual void clear() override
{
disable_notify_events();
gtk_tree_view_set_row_separator_func(m_pTreeView, nullptr, nullptr, nullptr);
m_aSeparatorRows.clear();
m_Clear(m_pTreeModel);
enable_notify_events();
}
virtual void make_sorted() override
{
// thaw wants to restore sort state of freeze
assert(gtk_tree_view_get_model(m_pTreeView) && "don't select when frozen, select after thaw. Note selection doesn't survive a freeze");
m_xSorter.reset(new comphelper::string::NaturalStringSorter(
::comphelper::getProcessComponentContext(),
Application::GetSettings().GetUILanguageTag().getLocale()));
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_set_sort_func(pSortable, m_nTextCol, sortFunc, this, nullptr);
gtk_tree_sortable_set_sort_column_id(pSortable, m_nTextCol, GTK_SORT_ASCENDING);
}
virtual void make_unsorted() override
{
m_xSorter.reset();
int nSortColumn;
GtkSortType eSortType;
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_get_sort_column_id(pSortable, &nSortColumn, &eSortType);
gtk_tree_sortable_set_sort_column_id(pSortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, eSortType);
}
virtual void set_sort_order(bool bAscending) override
{
GtkSortType eSortType = bAscending ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING;
gint sort_column_id(0);
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_get_sort_column_id(pSortable, &sort_column_id, nullptr);
gtk_tree_sortable_set_sort_column_id(pSortable, sort_column_id, eSortType);
}
virtual bool get_sort_order() const override
{
int nSortColumn;
GtkSortType eSortType;
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_get_sort_column_id(pSortable, &nSortColumn, &eSortType);
return nSortColumn != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID && eSortType == GTK_SORT_ASCENDING;
}
virtual void set_sort_indicator(TriState eState, int col) override
{
assert(col >= 0 && "cannot sort on expander column");
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(g_list_nth_data(m_pColumns, col));
assert(pColumn && "wrong count");
if (eState == TRISTATE_INDET)
gtk_tree_view_column_set_sort_indicator(pColumn, false);
else
{
gtk_tree_view_column_set_sort_indicator(pColumn, true);
GtkSortType eSortType = eState == TRISTATE_TRUE ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING;
gtk_tree_view_column_set_sort_order(pColumn, eSortType);
}
}
virtual TriState get_sort_indicator(int col) const override
{
assert(col >= 0 && "cannot sort on expander column");
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(g_list_nth_data(m_pColumns, col));
if (!gtk_tree_view_column_get_sort_indicator(pColumn))
return TRISTATE_INDET;
return gtk_tree_view_column_get_sort_order(pColumn) == GTK_SORT_ASCENDING ? TRISTATE_TRUE : TRISTATE_FALSE;
}
virtual int get_sort_column() const override
{
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gint sort_column_id(0);
if (!gtk_tree_sortable_get_sort_column_id(pSortable, &sort_column_id, nullptr))
return -1;
return to_external_model(sort_column_id);
}
virtual void set_sort_column(int nColumn) override
{
if (nColumn == -1)
{
make_unsorted();
return;
}
GtkSortType eSortType;
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_get_sort_column_id(pSortable, nullptr, &eSortType);
int nSortCol = to_internal_model(nColumn);
gtk_tree_sortable_set_sort_func(pSortable, nSortCol, sortFunc, this, nullptr);
gtk_tree_sortable_set_sort_column_id(pSortable, nSortCol, eSortType);
}
virtual void set_sort_func(const std::function<int(const weld::TreeIter&, const weld::TreeIter&)>& func) override
{
weld::TreeView::set_sort_func(func);
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_sort_column_changed(pSortable);
}
virtual int n_children() const override
{
return gtk_tree_model_iter_n_children(m_pTreeModel, nullptr);
}
virtual int iter_n_children(const weld::TreeIter& rIter) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
return gtk_tree_model_iter_n_children(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
}
virtual void select(int pos) override
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't select when frozen, select after thaw. Note selection doesn't survive a freeze");
disable_notify_events();
if (pos == -1 || (pos == 0 && n_children() == 0))
{
gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(m_pTreeView));
}
else
{
GtkTreePath* path = gtk_tree_path_new_from_indices(pos, -1);
gtk_tree_selection_select_path(gtk_tree_view_get_selection(m_pTreeView), path);
gtk_tree_view_scroll_to_cell(m_pTreeView, path, nullptr, false, 0, 0);
gtk_tree_path_free(path);
}
enable_notify_events();
}
virtual void set_cursor(int pos) override
{
disable_notify_events();
GtkTreePath* path;
if (pos != -1)
{
path = gtk_tree_path_new_from_indices(pos, -1);
gtk_tree_view_scroll_to_cell(m_pTreeView, path, nullptr, false, 0, 0);
}
else
path = gtk_tree_path_new_from_indices(G_MAXINT, -1);
gtk_tree_view_set_cursor(m_pTreeView, path, nullptr, false);
gtk_tree_path_free(path);
enable_notify_events();
}
virtual void scroll_to_row(int pos) override
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't select when frozen, select after thaw. Note selection doesn't survive a freeze");
disable_notify_events();
GtkTreePath* path = gtk_tree_path_new_from_indices(pos, -1);
gtk_tree_view_expand_to_path(m_pTreeView, path);
gtk_tree_view_scroll_to_cell(m_pTreeView, path, nullptr, true, 0, 0);
gtk_tree_path_free(path);
enable_notify_events();
}
virtual bool is_selected(int pos) const override
{
GtkTreeIter iter;
gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos);
return gtk_tree_selection_iter_is_selected(gtk_tree_view_get_selection(m_pTreeView), &iter);
}
virtual void unselect(int pos) override
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't select when frozen, select after thaw. Note selection doesn't survive a freeze");
disable_notify_events();
if (pos == -1 || (pos == 0 && n_children() == 0))
{
gtk_tree_selection_select_all(gtk_tree_view_get_selection(m_pTreeView));
}
else
{
GtkTreePath* path = gtk_tree_path_new_from_indices(pos, -1);
gtk_tree_selection_unselect_path(gtk_tree_view_get_selection(m_pTreeView), path);
gtk_tree_path_free(path);
}
enable_notify_events();
}
virtual std::vector<int> get_selected_rows() const override
{
std::vector<int> aRows;
GList* pList = gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(m_pTreeView), nullptr);
for (GList* pItem = g_list_first(pList); pItem; pItem = g_list_next(pItem))
{
GtkTreePath* path = static_cast<GtkTreePath*>(pItem->data);
gint depth;
gint* indices = gtk_tree_path_get_indices_with_depth(path, &depth);
int nRow = indices[depth-1];
aRows.push_back(nRow);
}
g_list_free_full(pList, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
return aRows;
}
virtual void all_foreach(const std::function<bool(weld::TreeIter&)>& func) override
{
g_object_freeze_notify(G_OBJECT(m_pTreeModel));
GtkInstanceTreeIter aGtkIter(nullptr);
if (get_iter_first(aGtkIter))
{
do
{
if (func(aGtkIter))
break;
} while (iter_next(aGtkIter));
}
g_object_thaw_notify(G_OBJECT(m_pTreeModel));
}
virtual void selected_foreach(const std::function<bool(weld::TreeIter&)>& func) override
{
g_object_freeze_notify(G_OBJECT(m_pTreeModel));
GtkInstanceTreeIter aGtkIter(nullptr);
GtkTreeModel* pModel;
GList* pList = gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(m_pTreeView), &pModel);
for (GList* pItem = g_list_first(pList); pItem; pItem = g_list_next(pItem))
{
GtkTreePath* path = static_cast<GtkTreePath*>(pItem->data);
gtk_tree_model_get_iter(pModel, &aGtkIter.iter, path);
if (func(aGtkIter))
break;
}
g_list_free_full(pList, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
g_object_thaw_notify(G_OBJECT(m_pTreeModel));
}
virtual void visible_foreach(const std::function<bool(weld::TreeIter&)>& func) override
{
g_object_freeze_notify(G_OBJECT(m_pTreeModel));
GtkTreePath* start_path;
GtkTreePath* end_path;
if (!gtk_tree_view_get_visible_range(m_pTreeView, &start_path, &end_path))
return;
GtkInstanceTreeIter aGtkIter(nullptr);
gtk_tree_model_get_iter(m_pTreeModel, &aGtkIter.iter, start_path);
do
{
if (func(aGtkIter))
break;
GtkTreePath* path = gtk_tree_model_get_path(m_pTreeModel, &aGtkIter.iter);
bool bContinue = gtk_tree_path_compare(path, end_path) != 0;
gtk_tree_path_free(path);
if (!bContinue)
break;
if (!iter_next(aGtkIter))
break;
} while(true);
gtk_tree_path_free(start_path);
gtk_tree_path_free(end_path);
g_object_thaw_notify(G_OBJECT(m_pTreeModel));
}
virtual void connect_visible_range_changed(const Link<weld::TreeView&, void>& rLink) override
{
weld::TreeView::connect_visible_range_changed(rLink);
if (!m_nVAdjustmentChangedSignalId)
{
GtkAdjustment* pVAdjustment = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(m_pTreeView));
m_nVAdjustmentChangedSignalId = g_signal_connect(pVAdjustment, "value-changed", G_CALLBACK(signalVAdjustmentChanged), this);
}
}
virtual bool is_selected(const weld::TreeIter& rIter) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
return gtk_tree_selection_iter_is_selected(gtk_tree_view_get_selection(m_pTreeView), const_cast<GtkTreeIter*>(&rGtkIter.iter));
}
virtual OUString get_text(int pos, int col) const override
{
if (col == -1)
col = m_nTextCol;
else
col = to_internal_model(col);
return get(pos, col);
}
virtual void set_text(int pos, const OUString& rText, int col) override
{
if (col == -1)
col = m_nTextCol;
else
col = to_internal_model(col);
set(pos, col, rText);
}
virtual TriState get_toggle(int pos, int col) const override
{
if (col == -1)
col = m_nExpanderToggleCol;
else
col = to_internal_model(col);
if (get_bool(pos, m_aToggleTriStateMap.find(col)->second))
return TRISTATE_INDET;
return get_bool(pos, col) ? TRISTATE_TRUE : TRISTATE_FALSE;
}
virtual TriState get_toggle(const weld::TreeIter& rIter, int col) const override
{
if (col == -1)
col = m_nExpanderToggleCol;
else
col = to_internal_model(col);
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
if (get_bool(rGtkIter.iter, m_aToggleTriStateMap.find(col)->second))
return TRISTATE_INDET;
return get_bool(rGtkIter.iter, col) ? TRISTATE_TRUE : TRISTATE_FALSE;
}
virtual void set_toggle(const weld::TreeIter& rIter, TriState eState, int col) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
set_toggle(rGtkIter.iter, eState, col);
}
virtual void set_toggle(int pos, TriState eState, int col) override
{
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
set_toggle(iter, eState, col);
}
virtual void enable_toggle_buttons(weld::ColumnToggleType eType) override
{
for (GList* pEntry = g_list_first(m_pColumns); pEntry; pEntry = g_list_next(pEntry))
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pEntry->data);
GList *pRenderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(pColumn));
for (GList* pRenderer = g_list_first(pRenderers); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
if (!GTK_IS_CELL_RENDERER_TOGGLE(pCellRenderer))
continue;
GtkCellRendererToggle* pToggle = GTK_CELL_RENDERER_TOGGLE(pCellRenderer);
gtk_cell_renderer_toggle_set_radio(pToggle, eType == weld::ColumnToggleType::Radio);
}
g_list_free(pRenderers);
}
}
virtual void set_clicks_to_toggle(int /*nToggleBehavior*/) override
{
}
virtual void set_extra_row_indent(const weld::TreeIter& rIter, int nIndentLevel) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
set(rGtkIter.iter, m_aIndentMap[m_nTextCol], nIndentLevel * get_expander_size());
}
virtual void set_text_emphasis(const weld::TreeIter& rIter, bool bOn, int col) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
col = to_internal_model(col);
set(rGtkIter.iter, m_aWeightMap[col], bOn ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
}
virtual void set_text_emphasis(int pos, bool bOn, int col) override
{
col = to_internal_model(col);
set(pos, m_aWeightMap[col], bOn ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
}
virtual bool get_text_emphasis(const weld::TreeIter& rIter, int col) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
col = to_internal_model(col);
return get_int(rGtkIter.iter, m_aWeightMap.find(col)->second) == PANGO_WEIGHT_BOLD;
}
virtual bool get_text_emphasis(int pos, int col) const override
{
col = to_internal_model(col);
return get_int(pos, m_aWeightMap.find(col)->second) == PANGO_WEIGHT_BOLD;
}
virtual void set_text_align(const weld::TreeIter& rIter, double fAlign, int col) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
col = to_internal_model(col);
set(rGtkIter.iter, m_aAlignMap[col], fAlign);
}
virtual void set_text_align(int pos, double fAlign, int col) override
{
col = to_internal_model(col);
set(pos, m_aAlignMap[col], fAlign);
}
using GtkInstanceWidget::set_sensitive;
using GtkInstanceWidget::get_sensitive;
virtual void set_sensitive(int pos, bool bSensitive, int col) override
{
if (col == -1)
{
for (const auto& elem : m_aSensitiveMap)
set(pos, elem.second, bSensitive);
}
else
{
col = to_internal_model(col);
set(pos, m_aSensitiveMap[col], bSensitive);
}
}
virtual bool get_sensitive(int pos, int col) const override
{
col = to_internal_model(col);
return get_bool(pos, m_aSensitiveMap.find(col)->second);
}
virtual void set_sensitive(const weld::TreeIter& rIter, bool bSensitive, int col) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
if (col == -1)
{
for (const auto& elem : m_aSensitiveMap)
set(rGtkIter.iter, elem.second, bSensitive);
}
else
{
col = to_internal_model(col);
set(rGtkIter.iter, m_aSensitiveMap[col], bSensitive);
}
}
virtual bool get_sensitive(const weld::TreeIter& rIter, int col) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
col = to_internal_model(col);
return get_bool(rGtkIter.iter, m_aSensitiveMap.find(col)->second);
}
void set_image(const GtkTreeIter& iter, int col, GdkPixbuf* pixbuf)
{
if (col == -1)
col = m_nExpanderImageCol;
else
col = to_internal_model(col);
m_Setter(m_pTreeModel, const_cast<GtkTreeIter*>(&iter), col, pixbuf, -1);
if (pixbuf)
g_object_unref(pixbuf);
}
void set_image(int pos, GdkPixbuf* pixbuf, int col)
{
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
{
set_image(iter, col, pixbuf);
}
}
virtual void set_image(int pos, const css::uno::Reference<css::graphic::XGraphic>& rImage, int col) override
{
set_image(pos, getPixbuf(rImage), col);
}
virtual void set_image(int pos, const OUString& rImage, int col) override
{
set_image(pos, getPixbuf(rImage), col);
}
virtual void set_image(int pos, VirtualDevice& rImage, int col) override
{
set_image(pos, getPixbuf(rImage), col);
}
virtual void set_image(const weld::TreeIter& rIter, const css::uno::Reference<css::graphic::XGraphic>& rImage, int col) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
set_image(rGtkIter.iter, col, getPixbuf(rImage));
}
virtual void set_image(const weld::TreeIter& rIter, const OUString& rImage, int col) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
set_image(rGtkIter.iter, col, getPixbuf(rImage));
}
virtual void set_image(const weld::TreeIter& rIter, VirtualDevice& rImage, int col) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
set_image(rGtkIter.iter, col, getPixbuf(rImage));
}
virtual OUString get_id(int pos) const override
{
return get(pos, m_nIdCol);
}
virtual void set_id(int pos, const OUString& rId) override
{
return set(pos, m_nIdCol, rId);
}
virtual int get_iter_index_in_parent(const weld::TreeIter& rIter) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreePath* path = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
gint depth;
gint* indices = gtk_tree_path_get_indices_with_depth(path, &depth);
int nRet = indices[depth-1];
gtk_tree_path_free(path);
return nRet;
}
virtual int iter_compare(const weld::TreeIter& a, const weld::TreeIter& b) const override
{
const GtkInstanceTreeIter& rGtkIterA = static_cast<const GtkInstanceTreeIter&>(a);
const GtkInstanceTreeIter& rGtkIterB = static_cast<const GtkInstanceTreeIter&>(b);
GtkTreePath* pathA = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIterA.iter));
GtkTreePath* pathB = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIterB.iter));
int nRet = gtk_tree_path_compare(pathA, pathB);
gtk_tree_path_free(pathB);
gtk_tree_path_free(pathA);
return nRet;
}
// by copy and delete of old copy
void move_subtree(GtkTreeIter& rFromIter, GtkTreeIter* pGtkParentIter, int nIndexInNewParent)
{
int nCols = gtk_tree_model_get_n_columns(m_pTreeModel);
GValue value;
GtkTreeIter toiter;
m_Insert(m_pTreeModel, &toiter, pGtkParentIter, nIndexInNewParent);
for (int i = 0; i < nCols; ++i)
{
memset(&value, 0, sizeof(GValue));
gtk_tree_model_get_value(m_pTreeModel, &rFromIter, i, &value);
m_SetValue(m_pTreeModel, &toiter, i, &value);
g_value_unset(&value);
}
GtkTreeIter tmpfromiter;
if (gtk_tree_model_iter_children(m_pTreeModel, &tmpfromiter, &rFromIter))
{
int j = 0;
do
{
move_subtree(tmpfromiter, &toiter, j++);
} while (gtk_tree_model_iter_next(m_pTreeModel, &tmpfromiter));
}
m_Remove(m_pTreeModel, &rFromIter);
}
virtual void move_subtree(weld::TreeIter& rNode, const weld::TreeIter* pNewParent, int nIndexInNewParent) override
{
GtkInstanceTreeIter& rGtkIter = static_cast<GtkInstanceTreeIter&>(rNode);
const GtkInstanceTreeIter* pGtkParentIter = static_cast<const GtkInstanceTreeIter*>(pNewParent);
move_subtree(rGtkIter.iter, pGtkParentIter ? const_cast<GtkTreeIter*>(&pGtkParentIter->iter) : nullptr, nIndexInNewParent);
}
virtual int get_selected_index() const override
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't request selection when frozen");
int nRet = -1;
GtkTreeSelection *selection = gtk_tree_view_get_selection(m_pTreeView);
if (gtk_tree_selection_get_mode(selection) != GTK_SELECTION_MULTIPLE)
{
GtkTreeIter iter;
GtkTreeModel* pModel;
if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(m_pTreeView), &pModel, &iter))
{
GtkTreePath* path = gtk_tree_model_get_path(pModel, &iter);
gint depth;
gint* indices = gtk_tree_path_get_indices_with_depth(path, &depth);
nRet = indices[depth-1];
gtk_tree_path_free(path);
}
}
else
{
auto vec = get_selected_rows();
return vec.empty() ? -1 : vec[0];
}
return nRet;
}
bool get_selected_iterator(GtkTreeIter* pIter) const
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't request selection when frozen");
bool bRet = false;
GtkTreeSelection *selection = gtk_tree_view_get_selection(m_pTreeView);
if (gtk_tree_selection_get_mode(selection) != GTK_SELECTION_MULTIPLE)
bRet = gtk_tree_selection_get_selected(gtk_tree_view_get_selection(m_pTreeView), nullptr, pIter);
else
{
GtkTreeModel* pModel;
GList* pList = gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(m_pTreeView), &pModel);
for (GList* pItem = g_list_first(pList); pItem; pItem = g_list_next(pItem))
{
if (pIter)
{
GtkTreePath* path = static_cast<GtkTreePath*>(pItem->data);
gtk_tree_model_get_iter(pModel, pIter, path);
}
bRet = true;
break;
}
g_list_free_full(pList, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
}
return bRet;
}
virtual OUString get_selected_text() const override
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't request selection when frozen");
GtkTreeIter iter;
if (get_selected_iterator(&iter))
return get(iter, m_nTextCol);
return OUString();
}
virtual OUString get_selected_id() const override
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't request selection when frozen");
GtkTreeIter iter;
if (get_selected_iterator(&iter))
return get(iter, m_nIdCol);
return OUString();
}
virtual std::unique_ptr<weld::TreeIter> make_iterator(const weld::TreeIter* pOrig) const override
{
return std::unique_ptr<weld::TreeIter>(new GtkInstanceTreeIter(static_cast<const GtkInstanceTreeIter*>(pOrig)));
}
virtual void copy_iterator(const weld::TreeIter& rSource, weld::TreeIter& rDest) const override
{
const GtkInstanceTreeIter& rGtkSource(static_cast<const GtkInstanceTreeIter&>(rSource));
GtkInstanceTreeIter& rGtkDest(static_cast<GtkInstanceTreeIter&>(rDest));
rGtkDest.iter = rGtkSource.iter;
}
virtual bool get_selected(weld::TreeIter* pIter) const override
{
GtkInstanceTreeIter* pGtkIter = static_cast<GtkInstanceTreeIter*>(pIter);
return get_selected_iterator(pGtkIter ? &pGtkIter->iter : nullptr);
}
virtual bool get_cursor(weld::TreeIter* pIter) const override
{
GtkInstanceTreeIter* pGtkIter = static_cast<GtkInstanceTreeIter*>(pIter);
GtkTreePath* path;
gtk_tree_view_get_cursor(m_pTreeView, &path, nullptr);
if (pGtkIter && path)
{
gtk_tree_model_get_iter(m_pTreeModel, &pGtkIter->iter, path);
}
if (!path)
return false;
gtk_tree_path_free(path);
return true;
}
virtual int get_cursor_index() const override
{
int nRet = -1;
GtkTreePath* path;
gtk_tree_view_get_cursor(m_pTreeView, &path, nullptr);
if (path)
{
gint depth;
gint* indices = gtk_tree_path_get_indices_with_depth(path, &depth);
nRet = indices[depth-1];
gtk_tree_path_free(path);
}
return nRet;
}
virtual void set_cursor(const weld::TreeIter& rIter) override
{
disable_notify_events();
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreeIter Iter;
if (gtk_tree_model_iter_parent(m_pTreeModel, &Iter, const_cast<GtkTreeIter*>(&rGtkIter.iter)))
{
GtkTreePath* path = gtk_tree_model_get_path(m_pTreeModel, &Iter);
if (!gtk_tree_view_row_expanded(m_pTreeView, path))
gtk_tree_view_expand_to_path(m_pTreeView, path);
gtk_tree_path_free(path);
}
GtkTreePath* path = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
gtk_tree_view_scroll_to_cell(m_pTreeView, path, nullptr, false, 0, 0);
gtk_tree_view_set_cursor(m_pTreeView, path, nullptr, false);
gtk_tree_path_free(path);
enable_notify_events();
}
virtual bool get_iter_first(weld::TreeIter& rIter) const override
{
GtkInstanceTreeIter& rGtkIter = static_cast<GtkInstanceTreeIter&>(rIter);
return gtk_tree_model_get_iter_first(m_pTreeModel, &rGtkIter.iter);
}
virtual bool iter_next_sibling(weld::TreeIter& rIter) const override
{
GtkInstanceTreeIter& rGtkIter = static_cast<GtkInstanceTreeIter&>(rIter);
return gtk_tree_model_iter_next(m_pTreeModel, &rGtkIter.iter);
}
virtual bool iter_previous_sibling(weld::TreeIter& rIter) const override
{
GtkInstanceTreeIter& rGtkIter = static_cast<GtkInstanceTreeIter&>(rIter);
return gtk_tree_model_iter_previous(m_pTreeModel, &rGtkIter.iter);
}
virtual bool iter_next(weld::TreeIter& rIter) const override
{
return iter_next(rIter, false);
}
virtual bool iter_previous(weld::TreeIter& rIter) const override
{
bool ret = false;
GtkInstanceTreeIter& rGtkIter = static_cast<GtkInstanceTreeIter&>(rIter);
GtkTreeIter iter = rGtkIter.iter;
GtkTreeIter tmp = iter;
if (gtk_tree_model_iter_previous(m_pTreeModel, &tmp))
{
// Move down level(s) until we find the level where the last node exists.
int nChildren = gtk_tree_model_iter_n_children(m_pTreeModel, &tmp);
if (!nChildren)
rGtkIter.iter = tmp;
else
last_child(m_pTreeModel, &rGtkIter.iter, &tmp, nChildren);
ret = true;
}
else
{
// Move up level
if (gtk_tree_model_iter_parent(m_pTreeModel, &tmp, &iter))
{
rGtkIter.iter = tmp;
ret = true;
}
}
if (ret)
{
//on-demand dummy entry doesn't count
if (get_text(rGtkIter, -1) == "<dummy>")
return iter_previous(rGtkIter);
return true;
}
return false;
}
virtual bool iter_children(weld::TreeIter& rIter) const override
{
GtkInstanceTreeIter& rGtkIter = static_cast<GtkInstanceTreeIter&>(rIter);
GtkTreeIter tmp;
bool ret = gtk_tree_model_iter_children(m_pTreeModel, &tmp, &rGtkIter.iter);
rGtkIter.iter = tmp;
if (ret)
{
//on-demand dummy entry doesn't count
return get_text(rGtkIter, -1) != "<dummy>";
}
return ret;
}
virtual bool iter_parent(weld::TreeIter& rIter) const override
{
GtkInstanceTreeIter& rGtkIter = static_cast<GtkInstanceTreeIter&>(rIter);
GtkTreeIter tmp;
bool ret = gtk_tree_model_iter_parent(m_pTreeModel, &tmp, &rGtkIter.iter);
rGtkIter.iter = tmp;
return ret;
}
virtual void remove(const weld::TreeIter& rIter) override
{
disable_notify_events();
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
m_Remove(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
enable_notify_events();
}
virtual void remove_selection() override
{
disable_notify_events();
std::vector<GtkTreeIter> aIters;
GtkTreeModel* pModel;
GList* pList = gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(m_pTreeView), &pModel);
for (GList* pItem = g_list_first(pList); pItem; pItem = g_list_next(pItem))
{
GtkTreePath* path = static_cast<GtkTreePath*>(pItem->data);
aIters.emplace_back();
gtk_tree_model_get_iter(pModel, &aIters.back(), path);
}
g_list_free_full(pList, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
for (auto& iter : aIters)
m_Remove(m_pTreeModel, &iter);
enable_notify_events();
}
virtual void select(const weld::TreeIter& rIter) override
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't select when frozen, select after thaw. Note selection doesn't survive a freeze");
disable_notify_events();
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
gtk_tree_selection_select_iter(gtk_tree_view_get_selection(m_pTreeView), const_cast<GtkTreeIter*>(&rGtkIter.iter));
enable_notify_events();
}
virtual void scroll_to_row(const weld::TreeIter& rIter) override
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't select when frozen, select after thaw. Note selection doesn't survive a freeze");
disable_notify_events();
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreePath* path = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
gtk_tree_view_expand_to_path(m_pTreeView, path);
gtk_tree_view_scroll_to_cell(m_pTreeView, path, nullptr, true, 0, 0);
gtk_tree_path_free(path);
enable_notify_events();
}
virtual void unselect(const weld::TreeIter& rIter) override
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't select when frozen, select after thaw. Note selection doesn't survive a freeze");
disable_notify_events();
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
gtk_tree_selection_unselect_iter(gtk_tree_view_get_selection(m_pTreeView), const_cast<GtkTreeIter*>(&rGtkIter.iter));
enable_notify_events();
}
virtual int get_iter_depth(const weld::TreeIter& rIter) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreePath* path = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
int ret = gtk_tree_path_get_depth(path) - 1;
gtk_tree_path_free(path);
return ret;
}
virtual bool iter_has_child(const weld::TreeIter& rIter) const override
{
GtkInstanceTreeIter aTempCopy(static_cast<const GtkInstanceTreeIter*>(&rIter));
return iter_children(aTempCopy);
}
virtual bool get_row_expanded(const weld::TreeIter& rIter) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreePath* path = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
bool ret = gtk_tree_view_row_expanded(m_pTreeView, path);
gtk_tree_path_free(path);
return ret;
}
virtual bool get_children_on_demand(const weld::TreeIter& rIter) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkInstanceTreeIter aIter(&rGtkIter);
return child_is_placeholder(aIter);
}
virtual void set_children_on_demand(const weld::TreeIter& rIter, bool bChildrenOnDemand) override
{
disable_notify_events();
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkInstanceTreeIter aPlaceHolderIter(&rGtkIter);
bool bPlaceHolder = child_is_placeholder(aPlaceHolderIter);
if (bChildrenOnDemand && !bPlaceHolder)
{
GtkTreeIter subiter;
OUString sDummy("<dummy>");
insert_row(subiter, &rGtkIter.iter, -1, nullptr, &sDummy, nullptr, nullptr);
}
else if (!bChildrenOnDemand && bPlaceHolder)
remove(aPlaceHolderIter);
enable_notify_events();
}
virtual void expand_row(const weld::TreeIter& rIter) override
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't expand when frozen");
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreePath* path = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
if (!gtk_tree_view_row_expanded(m_pTreeView, path))
gtk_tree_view_expand_to_path(m_pTreeView, path);
gtk_tree_path_free(path);
}
virtual void collapse_row(const weld::TreeIter& rIter) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreePath* path = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
if (gtk_tree_view_row_expanded(m_pTreeView, path))
gtk_tree_view_collapse_row(m_pTreeView, path);
gtk_tree_path_free(path);
}
virtual OUString get_text(const weld::TreeIter& rIter, int col) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
if (col == -1)
col = m_nTextCol;
else
col = to_internal_model(col);
return get(rGtkIter.iter, col);
}
virtual void set_text(const weld::TreeIter& rIter, const OUString& rText, int col) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
if (col == -1)
col = m_nTextCol;
else
col = to_internal_model(col);
set(rGtkIter.iter, col, rText);
}
virtual OUString get_id(const weld::TreeIter& rIter) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
return get(rGtkIter.iter, m_nIdCol);
}
virtual void set_id(const weld::TreeIter& rIter, const OUString& rId) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
set(rGtkIter.iter, m_nIdCol, rId);
}
virtual void freeze() override
{
disable_notify_events();
bool bIsFirstFreeze = IsFirstFreeze();
GtkInstanceWidget::freeze();
if (bIsFirstFreeze)
{
g_object_ref(m_pTreeModel);
gtk_tree_view_set_model(m_pTreeView, nullptr);
g_object_freeze_notify(G_OBJECT(m_pTreeModel));
if (m_xSorter)
{
int nSortColumn;
GtkSortType eSortType;
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_get_sort_column_id(pSortable, &nSortColumn, &eSortType);
gtk_tree_sortable_set_sort_column_id(pSortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, eSortType);
m_aSavedSortColumns.push_back(nSortColumn);
m_aSavedSortTypes.push_back(eSortType);
}
}
enable_notify_events();
}
virtual void thaw() override
{
disable_notify_events();
if (IsLastThaw())
{
if (m_xSorter)
{
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_set_sort_column_id(pSortable, m_aSavedSortColumns.back(), m_aSavedSortTypes.back());
m_aSavedSortTypes.pop_back();
m_aSavedSortColumns.pop_back();
}
g_object_thaw_notify(G_OBJECT(m_pTreeModel));
gtk_tree_view_set_model(m_pTreeView, GTK_TREE_MODEL(m_pTreeModel));
g_object_unref(m_pTreeModel);
}
GtkInstanceWidget::thaw();
enable_notify_events();
}
virtual int get_height_rows(int nRows) const override
{
return ::get_height_rows(m_pTreeView, m_pColumns, nRows);
}
virtual Size get_size_request() const override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
{
return Size(gtk_scrolled_window_get_min_content_width(GTK_SCROLLED_WINDOW(pParent)),
gtk_scrolled_window_get_min_content_height(GTK_SCROLLED_WINDOW(pParent)));
}
int nWidth, nHeight;
gtk_widget_get_size_request(m_pWidget, &nWidth, &nHeight);
return Size(nWidth, nHeight);
}
virtual Size get_preferred_size() const override
{
Size aRet(-1, -1);
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
{
aRet = Size(gtk_scrolled_window_get_min_content_width(GTK_SCROLLED_WINDOW(pParent)),
gtk_scrolled_window_get_min_content_height(GTK_SCROLLED_WINDOW(pParent)));
}
GtkRequisition size;
#if !GTK_CHECK_VERSION(4, 0, 0)
// sometimes gtk gives a bad outcome for gtk_widget_get_preferred_size if GtkTreeView's
// do_validate_rows hasn't been run before querying the preferred size, if we call
// gtk_widget_get_preferred_width first, we can guarantee do_validate_rows gets called
gtk_widget_get_preferred_width(m_pWidget, nullptr, &size.width);
#endif
gtk_widget_get_preferred_size(m_pWidget, nullptr, &size);
if (aRet.Width() == -1)
aRet.setWidth(size.width);
if (aRet.Height() == -1)
aRet.setHeight(size.height);
return aRet;
}
virtual void show() override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
gtk_widget_show(pParent);
gtk_widget_show(m_pWidget);
}
virtual void hide() override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
gtk_widget_hide(pParent);
gtk_widget_hide(m_pWidget);
}
virtual void enable_drag_source(rtl::Reference<TransferDataContainer>& rHelper, sal_uInt8 eDNDConstants) override
{
do_enable_drag_source(rHelper, eDNDConstants);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
virtual void drag_source_set(const std::vector<GtkTargetEntry>& rGtkTargets, GdkDragAction eDragAction) override
{
if (rGtkTargets.empty() && !eDragAction)
gtk_tree_view_unset_rows_drag_source(m_pTreeView);
else
gtk_tree_view_enable_model_drag_source(m_pTreeView, GDK_BUTTON1_MASK, rGtkTargets.data(), rGtkTargets.size(), eDragAction);
}
#endif
virtual void set_selection_mode(SelectionMode eMode) override
{
disable_notify_events();
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(m_pTreeView), VclToGtk(eMode));
enable_notify_events();
}
virtual int count_selected_rows() const override
{
return gtk_tree_selection_count_selected_rows(gtk_tree_view_get_selection(m_pTreeView));
}
int starts_with(const OUString& rStr, int nStartRow, bool bCaseSensitive)
{
return ::starts_with(m_pTreeModel, rStr, m_nTextCol, nStartRow, bCaseSensitive);
}
virtual void disable_notify_events() override
{
g_signal_handler_block(gtk_tree_view_get_selection(m_pTreeView), m_nChangedSignalId);
g_signal_handler_block(m_pTreeView, m_nRowActivatedSignalId);
g_signal_handler_block(m_pTreeModel, m_nRowDeletedSignalId);
g_signal_handler_block(m_pTreeModel, m_nRowInsertedSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pTreeModel, m_nRowDeletedSignalId);
g_signal_handler_unblock(m_pTreeModel, m_nRowInsertedSignalId);
g_signal_handler_unblock(m_pTreeView, m_nRowActivatedSignalId);
g_signal_handler_unblock(gtk_tree_view_get_selection(m_pTreeView), m_nChangedSignalId);
}
virtual void connect_popup_menu(const Link<const CommandEvent&, bool>& rLink) override
{
ensureButtonPressSignal();
weld::TreeView::connect_popup_menu(rLink);
}
virtual bool get_dest_row_at_pos(const Point &rPos, weld::TreeIter* pResult, bool bDnDMode, bool bAutoScroll) override
{
if (rPos.X() < 0 || rPos.Y() < 0)
{
// short-circuit to avoid "gtk_tree_view_get_dest_row_at_pos: assertion 'drag_x >= 0'" g_assert
return false;
}
const bool bAsTree = gtk_tree_view_get_enable_tree_lines(m_pTreeView);
// to keep it simple we'll default to always drop before the current row
// except for the special edge cases
GtkTreeViewDropPosition pos = bAsTree ? GTK_TREE_VIEW_DROP_INTO_OR_BEFORE : GTK_TREE_VIEW_DROP_BEFORE;
// unhighlight current highlighted row
gtk_tree_view_set_drag_dest_row(m_pTreeView, nullptr, pos);
if (m_bWorkAroundBadDragRegion)
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_unset_state_flags(GTK_WIDGET(m_pTreeView), GTK_STATE_FLAG_DROP_ACTIVE);
#else
gtk_drag_unhighlight(GTK_WIDGET(m_pTreeView));
#endif
}
GtkTreePath *path = nullptr;
GtkTreeViewDropPosition gtkpos = bAsTree ? GTK_TREE_VIEW_DROP_INTO_OR_BEFORE : GTK_TREE_VIEW_DROP_BEFORE;
bool ret = gtk_tree_view_get_dest_row_at_pos(m_pTreeView, rPos.X(), rPos.Y(),
&path, >kpos);
// find the last entry in the model for comparison
GtkTreePath *lastpath = get_path_of_last_entry(m_pTreeModel);
if (!ret)
{
// empty space, draw an indicator at the last entry
assert(!path);
path = gtk_tree_path_copy(lastpath);
pos = GTK_TREE_VIEW_DROP_AFTER;
}
else if (bDnDMode && gtk_tree_path_compare(path, lastpath) == 0)
{
// if we're on the last entry, see if gtk thinks
// the drop should be before or after it, and if
// its after, treat it like a drop into empty
// space, i.e. append it
if (gtkpos == GTK_TREE_VIEW_DROP_AFTER ||
gtkpos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
{
ret = false;
pos = bAsTree ? gtkpos : GTK_TREE_VIEW_DROP_AFTER;
}
}
if (ret && pResult)
{
GtkInstanceTreeIter& rGtkIter = static_cast<GtkInstanceTreeIter&>(*pResult);
gtk_tree_model_get_iter(m_pTreeModel, &rGtkIter.iter, path);
}
if (m_bInDrag && bDnDMode)
{
// highlight the row
gtk_tree_view_set_drag_dest_row(m_pTreeView, path, pos);
}
assert(path);
gtk_tree_path_free(path);
gtk_tree_path_free(lastpath);
if (bAutoScroll)
{
// auto scroll if we're close to the edges
GtkAdjustment* pVAdjustment = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(m_pTreeView));
double fStep = gtk_adjustment_get_step_increment(pVAdjustment);
if (rPos.Y() < fStep)
{
double fValue = gtk_adjustment_get_value(pVAdjustment) - fStep;
if (fValue < 0)
fValue = 0.0;
gtk_adjustment_set_value(pVAdjustment, fValue);
}
else
{
GdkRectangle aRect;
gtk_tree_view_get_visible_rect(m_pTreeView, &aRect);
if (rPos.Y() > aRect.height - fStep)
{
double fValue = gtk_adjustment_get_value(pVAdjustment) + fStep;
double fMax = gtk_adjustment_get_upper(pVAdjustment);
if (fValue > fMax)
fValue = fMax;
gtk_adjustment_set_value(pVAdjustment, fValue);
}
}
}
return ret;
}
virtual void unset_drag_dest_row() override
{
gtk_tree_view_set_drag_dest_row(m_pTreeView, nullptr, GTK_TREE_VIEW_DROP_BEFORE);
}
virtual tools::Rectangle get_row_area(const weld::TreeIter& rIter) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreePath* pPath = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
tools::Rectangle aRet = ::get_row_area(m_pTreeView, m_pColumns, pPath);
gtk_tree_path_free(pPath);
return aRet;
}
virtual void start_editing(const weld::TreeIter& rIter) override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreePath* path = gtk_tree_model_get_path(m_pTreeModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
GtkTreeViewColumn* pColumn = nullptr;
for (GList* pEntry = g_list_first(m_pColumns); pEntry; pEntry = g_list_next(pEntry))
{
GtkTreeViewColumn* pTestColumn = GTK_TREE_VIEW_COLUMN(pEntry->data);
// see if this column is editable
gboolean is_editable(false);
GList *pRenderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(pTestColumn));
for (GList* pRenderer = g_list_first(pRenderers); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
if (GTK_IS_CELL_RENDERER_TEXT(pCellRenderer))
{
g_object_get(pCellRenderer, "editable", &is_editable, nullptr);
if (is_editable)
{
pColumn = pTestColumn;
break;
}
}
}
g_list_free(pRenderers);
if (is_editable)
break;
}
// if nothing explicit editable, allow editing of cells which are not
// usually editable, so we can have double click do its usual
// row-activate but if we explicitly want to edit (remote files dialog)
// we can still do that
if (!pColumn)
{
pColumn = GTK_TREE_VIEW_COLUMN(g_list_nth_data(m_pColumns, m_nTextView));
assert(pColumn && "wrong column");
GList *pRenderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(pColumn));
for (GList* pRenderer = g_list_first(pRenderers); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
if (GTK_IS_CELL_RENDERER_TEXT(pCellRenderer))
{
g_object_set(pCellRenderer, "editable", true, "editable-set", true, nullptr);
g_object_set_data(G_OBJECT(pCellRenderer), "g-lo-RestoreNonEditable", reinterpret_cast<gpointer>(true));
break;
}
}
g_list_free(pRenderers);
}
gtk_tree_view_scroll_to_cell(m_pTreeView, path, pColumn, false, 0, 0);
gtk_tree_view_set_cursor(m_pTreeView, path, pColumn, true);
gtk_tree_path_free(path);
}
virtual void end_editing() override
{
GtkTreeViewColumn *focus_column = nullptr;
gtk_tree_view_get_cursor(m_pTreeView, nullptr, &focus_column);
if (focus_column)
gtk_cell_area_stop_editing(gtk_cell_layout_get_area(GTK_CELL_LAYOUT(focus_column)), true);
}
virtual TreeView* get_drag_source() const override
{
return g_DragSource;
}
virtual bool do_signal_drag_begin(bool& rUnsetDragIcon) override
{
if (m_aDragBeginHdl.Call(rUnsetDragIcon))
return true;
g_DragSource = this;
return false;
}
#if GTK_CHECK_VERSION(4, 0, 0)
virtual void drag_set_icon(GtkDragSource*) override
{
}
#else
virtual void drag_set_icon(GdkDragContext* context) override
{
GtkTreeSelection *selection = gtk_tree_view_get_selection(m_pTreeView);
if (gtk_tree_selection_get_mode(selection) == GTK_SELECTION_MULTIPLE)
{
int nWidth = 0;
int nHeight = 0;
GList* pList = gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(m_pTreeView), nullptr);
std::vector<cairo_surface_t*> surfaces;
std::vector<int> heights;
for (GList* pItem = g_list_first(pList); pItem; pItem = g_list_next(pItem))
{
GtkTreePath* pPath = static_cast<GtkTreePath*>(pItem->data);
surfaces.push_back(gtk_tree_view_create_row_drag_icon(m_pTreeView, pPath));
double x1, x2, y1, y2;
cairo_t* cr = cairo_create(surfaces.back());
cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
cairo_destroy(cr);
heights.push_back(y2 - y1);
nWidth = std::max(nWidth, static_cast<int>(x2 - x1));
nHeight += heights.back();
}
g_list_free_full(pList, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
// if it's just one, then don't do anything and leave the default dnd icon as-is
if (surfaces.size() > 1)
{
cairo_surface_t* target = cairo_surface_create_similar(surfaces[0],
cairo_surface_get_content(surfaces[0]),
nWidth,
nHeight);
cairo_t* cr = cairo_create(target);
double y_pos = 0;
for (size_t i = 0; i < surfaces.size(); ++i)
{
cairo_set_source_surface(cr, surfaces[i], 2, y_pos + 2);
cairo_rectangle(cr, 0, y_pos, nWidth, heights[i]);
cairo_fill(cr);
y_pos += heights[i];
}
cairo_destroy(cr);
double fXScale, fYScale;
dl_cairo_surface_get_device_scale(target, &fXScale, &fYScale);
cairo_surface_set_device_offset(target,
- m_nPressStartX * fXScale,
0);
gtk_drag_set_icon_surface(context, target);
cairo_surface_destroy(target);
}
for (auto surface : surfaces)
cairo_surface_destroy(surface);
}
}
#endif
virtual void do_signal_drag_end() override
{
g_DragSource = nullptr;
}
// Under gtk 3.24.8 dragging into the TreeView is not highlighting
// entire TreeView widget, just the rectangle which has no entries
// in it, so as a workaround highlight the parent container
// on drag start, and undo it on drag end, and trigger removal
// of the treeview's highlight effort
virtual void drag_started() override
{
m_bInDrag = true;
GtkWidget* pWidget = GTK_WIDGET(m_pTreeView);
GtkWidget* pParent = gtk_widget_get_parent(pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_unset_state_flags(pWidget, GTK_STATE_FLAG_DROP_ACTIVE);
gtk_widget_set_state_flags(pParent, GTK_STATE_FLAG_DROP_ACTIVE, false);
#else
gtk_drag_unhighlight(pWidget);
gtk_drag_highlight(pParent);
#endif
m_bWorkAroundBadDragRegion = true;
}
}
virtual void drag_ended() override
{
m_bInDrag = false;
if (m_bWorkAroundBadDragRegion)
{
GtkWidget* pWidget = GTK_WIDGET(m_pTreeView);
GtkWidget* pParent = gtk_widget_get_parent(pWidget);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_unset_state_flags(pParent, GTK_STATE_FLAG_DROP_ACTIVE);
#else
gtk_drag_unhighlight(pParent);
#endif
m_bWorkAroundBadDragRegion = false;
}
// unhighlight the row
gtk_tree_view_set_drag_dest_row(m_pTreeView, nullptr, GTK_TREE_VIEW_DROP_BEFORE);
}
virtual int vadjustment_get_value() const override
{
if (m_nPendingVAdjustment != -1)
return m_nPendingVAdjustment;
return gtk_adjustment_get_value(m_pVAdjustment);
}
virtual void vadjustment_set_value(int value) override
{
disable_notify_events();
/* This rube goldberg device is to remove flicker from setting the
scroll position of a GtkTreeView directly after clearing it and
filling it. As a specific example the writer navigator with ~100
tables, scroll to the end, right click on an entry near the end
and rename it, the tree is cleared and refilled and an attempt
made to set the scroll position of the freshly refilled tree to
the same point as before the clear.
*/
// This forces the tree to recalculate now its preferred size
// after being cleared
GtkRequisition size;
gtk_widget_get_preferred_size(GTK_WIDGET(m_pTreeView), nullptr, &size);
m_nPendingVAdjustment = value;
// The value set here just has to be different to the final value
// set later so that isn't a no-op
gtk_adjustment_set_value(m_pVAdjustment, value - 0.0001);
// This will set the desired m_nPendingVAdjustment value right
// before the tree gets drawn
gtk_widget_add_tick_callback(GTK_WIDGET(m_pTreeView), setAdjustmentCallback, this, nullptr);
enable_notify_events();
}
void call_signal_custom_render(VirtualDevice& rOutput, const tools::Rectangle& rRect, bool bSelected, const OUString& rId)
{
signal_custom_render(rOutput, rRect, bSelected, rId);
}
Size call_signal_custom_get_size(VirtualDevice& rOutput, const OUString& rId)
{
return signal_custom_get_size(rOutput, rId);
}
virtual void set_show_expanders(bool bShow) override
{
gtk_tree_view_set_show_expanders(m_pTreeView, bShow);
}
virtual bool changed_by_hover() const override
{
return m_bChangedByMouse;
}
virtual ~GtkInstanceTreeView() override
{
if (m_pChangeEvent)
Application::RemoveUserEvent(m_pChangeEvent);
if (m_nQueryTooltipSignalId)
g_signal_handler_disconnect(m_pTreeView, m_nQueryTooltipSignalId);
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pTreeView, m_nCrossingSignalid);
g_signal_handler_disconnect(m_pTreeView, m_nKeyPressSignalId);
g_signal_handler_disconnect(m_pTreeView, m_nPopupMenuSignalId);
#endif
g_signal_handler_disconnect(m_pTreeModel, m_nRowDeletedSignalId);
g_signal_handler_disconnect(m_pTreeModel, m_nRowInsertedSignalId);
if (m_nVAdjustmentChangedSignalId)
{
GtkAdjustment* pVAdjustment = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(m_pTreeView));
g_signal_handler_disconnect(pVAdjustment, m_nVAdjustmentChangedSignalId);
}
g_signal_handler_disconnect(m_pTreeView, m_nTestCollapseRowSignalId);
g_signal_handler_disconnect(m_pTreeView, m_nTestExpandRowSignalId);
g_signal_handler_disconnect(m_pTreeView, m_nRowActivatedSignalId);
g_signal_handler_disconnect(gtk_tree_view_get_selection(m_pTreeView), m_nChangedSignalId);
GValue value = G_VALUE_INIT;
g_value_init(&value, G_TYPE_POINTER);
g_value_set_pointer(&value, static_cast<gpointer>(nullptr));
for (GList* pEntry = g_list_last(m_pColumns); pEntry; pEntry = g_list_previous(pEntry))
{
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pEntry->data);
g_signal_handler_disconnect(pColumn, m_aColumnSignalIds.back());
m_aColumnSignalIds.pop_back();
// unset "instance" to avoid dangling "instance" points in any CustomCellRenderers
GList *pRenderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(pColumn));
for (GList* pRenderer = g_list_first(pRenderers); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
if (!CUSTOM_IS_CELL_RENDERER(pCellRenderer))
continue;
g_object_set_property(G_OBJECT(pCellRenderer), "instance", &value);
}
g_list_free(pRenderers);
}
g_list_free(m_pColumns);
}
};
}
IMPL_LINK_NOARG(GtkInstanceTreeView, async_signal_changed, void*, void)
{
m_pChangeEvent = nullptr;
signal_changed();
m_bChangedByMouse = false;
}
IMPL_LINK_NOARG(GtkInstanceTreeView, async_stop_cell_editing, void*, void)
{
end_editing();
}
namespace {
class GtkInstanceIconView : public GtkInstanceWidget, public virtual weld::IconView
{
private:
GtkIconView* m_pIconView;
GtkTreeStore* m_pTreeStore;
gint m_nTextCol;
gint m_nImageCol;
gint m_nIdCol;
gulong m_nSelectionChangedSignalId;
gulong m_nItemActivatedSignalId;
#if !GTK_CHECK_VERSION(4, 0, 0)
gulong m_nPopupMenu;
#endif
gulong m_nQueryTooltipSignalId = 0;
ImplSVEvent* m_pSelectionChangeEvent;
DECL_LINK(async_signal_selection_changed, void*, void);
bool signal_command(const CommandEvent& rCEvt)
{
return m_aCommandHdl.Call(rCEvt);
}
virtual bool signal_popup_menu(const CommandEvent& rCEvt) override
{
return signal_command(rCEvt);
}
void launch_signal_selection_changed()
{
//tdf#117991 selection change is sent before the focus change, and focus change
//is what will cause a spinbutton that currently has the focus to set its contents
//as the spin button value. So any LibreOffice callbacks on
//signal-change would happen before the spinbutton value-change occurs.
//To avoid this, send the signal-change to LibreOffice to occur after focus-change
//has been processed
if (m_pSelectionChangeEvent)
Application::RemoveUserEvent(m_pSelectionChangeEvent);
m_pSelectionChangeEvent = Application::PostUserEvent(LINK(this, GtkInstanceIconView, async_signal_selection_changed));
}
static void signalSelectionChanged(GtkIconView*, gpointer widget)
{
GtkInstanceIconView* pThis = static_cast<GtkInstanceIconView*>(widget);
pThis->launch_signal_selection_changed();
}
void handle_item_activated()
{
if (signal_item_activated())
return;
}
static void signalItemActivated(GtkIconView*, GtkTreePath*, gpointer widget)
{
GtkInstanceIconView* pThis = static_cast<GtkInstanceIconView*>(widget);
SolarMutexGuard aGuard;
pThis->handle_item_activated();
}
static gboolean signalQueryTooltip(GtkWidget* /*pGtkWidget*/, gint x, gint y,
gboolean keyboard_tip, GtkTooltip* tooltip,
gpointer widget)
{
GtkInstanceIconView* pThis = static_cast<GtkInstanceIconView*>(widget);
GtkTreeIter iter;
GtkIconView* pIconView = pThis->m_pIconView;
GtkTreeModel* pModel = gtk_icon_view_get_model(pIconView);
GtkTreePath* pPath = nullptr;
#if GTK_CHECK_VERSION(4, 0, 0)
if (!gtk_icon_view_get_tooltip_context(pIconView, x, y, keyboard_tip, &pModel, &pPath, &iter))
return false;
#else
if (!gtk_icon_view_get_tooltip_context(pIconView, &x, &y, keyboard_tip, &pModel, &pPath, &iter))
return false;
#endif
OUString aTooltip = pThis->signal_query_tooltip(GtkInstanceTreeIter(iter));
if (!aTooltip.isEmpty())
{
gtk_tooltip_set_text(tooltip, OUStringToOString(aTooltip, RTL_TEXTENCODING_UTF8).getStr());
gtk_icon_view_set_tooltip_item(pIconView, tooltip, pPath);
}
gtk_tree_path_free(pPath);
return !aTooltip.isEmpty();
}
/* Set the item's tooltip text as its accessible description as well. */
void set_item_accessible_description_from_tooltip(GtkTreeIter& iter)
{
#if GTK_CHECK_VERSION(4, 0, 0)
(void)iter;
#else
AtkObject* pAtkObject = gtk_widget_get_accessible(GTK_WIDGET(m_pIconView));
assert(pAtkObject);
GtkTreePath* pPath = gtk_tree_model_get_path(GTK_TREE_MODEL(m_pTreeStore), &iter);
assert(gtk_tree_path_get_depth(pPath) == 1);
int* indices = gtk_tree_path_get_indices(pPath);
const int nIndex = indices[0];
assert(nIndex < atk_object_get_n_accessible_children(pAtkObject)
&& "item index too high for ItemView's accessible child count");
const OUString sTooltipText = signal_query_tooltip(GtkInstanceTreeIter(iter));
AtkObject* pChild = atk_object_ref_accessible_child(pAtkObject, nIndex);
atk_object_set_description(pChild,
OUStringToOString(sTooltipText, RTL_TEXTENCODING_UTF8).getStr());
g_object_unref(pChild);
#endif
}
void insert_item(GtkTreeIter& iter, int pos, const OUString* pId, const OUString* pText, const OUString* pIconName)
{
// m_nTextCol may be -1, so pass it last, to not terminate the sequence before the Id value
gtk_tree_store_insert_with_values(m_pTreeStore, &iter, nullptr, pos,
m_nIdCol, !pId ? nullptr : OUStringToOString(*pId, RTL_TEXTENCODING_UTF8).getStr(),
m_nTextCol, !pText ? nullptr : OUStringToOString(*pText, RTL_TEXTENCODING_UTF8).getStr(),
-1);
if (pIconName)
{
GdkPixbuf* pixbuf = getPixbuf(*pIconName);
gtk_tree_store_set(m_pTreeStore, &iter, m_nImageCol, pixbuf, -1);
if (pixbuf)
g_object_unref(pixbuf);
}
set_item_accessible_description_from_tooltip(iter);
}
void insert_item(GtkTreeIter& iter, int pos, const OUString* pId, const OUString* pText, const VirtualDevice* pIcon)
{
// m_nTextCol may be -1, so pass it last, to not terminate the sequence before the Id value
gtk_tree_store_insert_with_values(m_pTreeStore, &iter, nullptr, pos,
m_nIdCol, !pId ? nullptr : OUStringToOString(*pId, RTL_TEXTENCODING_UTF8).getStr(),
m_nTextCol, !pText ? nullptr : OUStringToOString(*pText, RTL_TEXTENCODING_UTF8).getStr(),
-1);
if (pIcon)
{
GdkPixbuf* pixbuf = getPixbuf(*pIcon);
gtk_tree_store_set(m_pTreeStore, &iter, m_nImageCol, pixbuf, -1);
if (pixbuf)
g_object_unref(pixbuf);
}
set_item_accessible_description_from_tooltip(iter);
}
OUString get(const GtkTreeIter& iter, int col) const
{
GtkTreeModel *pModel = GTK_TREE_MODEL(m_pTreeStore);
gchar* pStr;
gtk_tree_model_get(pModel, const_cast<GtkTreeIter*>(&iter), col, &pStr, -1);
OUString sRet(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
g_free(pStr);
return sRet;
}
bool get_selected_iterator(GtkTreeIter* pIter) const
{
assert(gtk_icon_view_get_model(m_pIconView) && "don't request selection when frozen");
bool bRet = false;
{
GtkTreeModel* pModel = GTK_TREE_MODEL(m_pTreeStore);
GList* pList = gtk_icon_view_get_selected_items(m_pIconView);
for (GList* pItem = g_list_first(pList); pItem; pItem = g_list_next(pItem))
{
if (pIter)
{
GtkTreePath* path = static_cast<GtkTreePath*>(pItem->data);
gtk_tree_model_get_iter(pModel, pIter, path);
}
bRet = true;
break;
}
g_list_free_full(pList, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
}
return bRet;
}
public:
GtkInstanceIconView(GtkIconView* pIconView, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pIconView), pBuilder, bTakeOwnership)
, m_pIconView(pIconView)
, m_pTreeStore(GTK_TREE_STORE(gtk_icon_view_get_model(m_pIconView)))
, m_nTextCol(gtk_icon_view_get_text_column(m_pIconView)) // May be -1
, m_nImageCol(gtk_icon_view_get_pixbuf_column(m_pIconView))
, m_nSelectionChangedSignalId(g_signal_connect(pIconView, "selection-changed",
G_CALLBACK(signalSelectionChanged), this))
, m_nItemActivatedSignalId(g_signal_connect(pIconView, "item-activated", G_CALLBACK(signalItemActivated), this))
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_nPopupMenu(g_signal_connect(pIconView, "popup-menu", G_CALLBACK(signalPopupMenu), this))
#endif
, m_pSelectionChangeEvent(nullptr)
{
m_nIdCol = std::max(m_nTextCol, m_nImageCol) + 1;
}
virtual int get_item_width() const override
{
return gtk_icon_view_get_item_width(m_pIconView);
}
virtual void set_item_width(int width) override
{
gtk_icon_view_set_item_width(m_pIconView, width);
}
virtual void insert(int pos, const OUString* pText, const OUString* pId, const OUString* pIconName, weld::TreeIter* pRet) override
{
disable_notify_events();
GtkTreeIter iter;
insert_item(iter, pos, pId, pText, pIconName);
if (pRet)
{
GtkInstanceTreeIter* pGtkRetIter = static_cast<GtkInstanceTreeIter*>(pRet);
pGtkRetIter->iter = iter;
}
enable_notify_events();
}
virtual void insert(int pos, const OUString* pText, const OUString* pId, const VirtualDevice* pIcon, weld::TreeIter* pRet) override
{
disable_notify_events();
GtkTreeIter iter;
insert_item(iter, pos, pId, pText, pIcon);
if (pRet)
{
GtkInstanceTreeIter* pGtkRetIter = static_cast<GtkInstanceTreeIter*>(pRet);
pGtkRetIter->iter = iter;
}
enable_notify_events();
}
virtual void insert_separator(int /* pos */, const OUString* /* pId */) override
{
// TODO: can't just copy from GtkInstanceTreeView, since there's
// no IconView analog for gtk_tree_view_get_row_separator_func
}
virtual void connect_query_tooltip(const Link<const weld::TreeIter&, OUString>& rLink) override
{
weld::IconView::connect_query_tooltip(rLink);
m_nQueryTooltipSignalId = g_signal_connect(m_pIconView, "query-tooltip", G_CALLBACK(signalQueryTooltip), this);
gtk_widget_set_has_tooltip(GTK_WIDGET(m_pIconView), true);
}
virtual OUString get_selected_id() const override
{
assert(gtk_icon_view_get_model(m_pIconView) && "don't request selection when frozen");
GtkTreeIter iter;
if (get_selected_iterator(&iter))
return get(iter, m_nIdCol);
return OUString();
}
virtual void clear() override
{
disable_notify_events();
gtk_tree_store_clear(m_pTreeStore);
enable_notify_events();
}
virtual void freeze() override
{
disable_notify_events();
bool bIsFirstFreeze = IsFirstFreeze();
GtkInstanceWidget::freeze();
if (bIsFirstFreeze)
g_object_freeze_notify(G_OBJECT(m_pTreeStore));
enable_notify_events();
}
virtual void thaw() override
{
disable_notify_events();
if (IsLastThaw())
g_object_thaw_notify(G_OBJECT(m_pTreeStore));
GtkInstanceWidget::thaw();
enable_notify_events();
}
virtual Size get_size_request() const override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
{
return Size(gtk_scrolled_window_get_min_content_width(GTK_SCROLLED_WINDOW(pParent)),
gtk_scrolled_window_get_min_content_height(GTK_SCROLLED_WINDOW(pParent)));
}
int nWidth, nHeight;
gtk_widget_get_size_request(m_pWidget, &nWidth, &nHeight);
return Size(nWidth, nHeight);
}
virtual Size get_preferred_size() const override
{
Size aRet(-1, -1);
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
{
aRet = Size(gtk_scrolled_window_get_min_content_width(GTK_SCROLLED_WINDOW(pParent)),
gtk_scrolled_window_get_min_content_height(GTK_SCROLLED_WINDOW(pParent)));
}
GtkRequisition size;
gtk_widget_get_preferred_size(m_pWidget, nullptr, &size);
if (aRet.Width() == -1)
aRet.setWidth(size.width);
if (aRet.Height() == -1)
aRet.setHeight(size.height);
return aRet;
}
virtual void show() override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
gtk_widget_show(pParent);
gtk_widget_show(m_pWidget);
}
virtual void hide() override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
gtk_widget_hide(pParent);
gtk_widget_hide(m_pWidget);
}
virtual OUString get_selected_text() const override
{
assert(gtk_icon_view_get_model(m_pIconView) && "don't request selection when frozen");
GtkTreeIter iter;
if (get_selected_iterator(&iter))
return get(iter, m_nTextCol);
return OUString();
}
virtual int count_selected_items() const override
{
GList* pList = gtk_icon_view_get_selected_items(m_pIconView);
int nRet = g_list_length(pList);
g_list_free_full(pList, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
return nRet;
}
virtual void select(int pos) override
{
assert(gtk_icon_view_get_model(m_pIconView) && "don't select when frozen, select after thaw. Note selection doesn't survive a freeze");
disable_notify_events();
if (pos == -1 || (pos == 0 && n_children() == 0))
{
gtk_icon_view_unselect_all(m_pIconView);
}
else
{
GtkTreePath* path = gtk_tree_path_new_from_indices(pos, -1);
gtk_icon_view_select_path(m_pIconView, path);
gtk_icon_view_scroll_to_path(m_pIconView, path, false, 0, 0);
gtk_tree_path_free(path);
}
enable_notify_events();
}
virtual void unselect(int pos) override
{
assert(gtk_icon_view_get_model(m_pIconView) && "don't select when frozen, select after thaw. Note selection doesn't survive a freeze");
disable_notify_events();
if (pos == -1 || (pos == 0 && n_children() == 0))
{
gtk_icon_view_select_all(m_pIconView);
}
else
{
GtkTreePath* path = gtk_tree_path_new_from_indices(pos, -1);
gtk_icon_view_select_path(m_pIconView, path);
gtk_tree_path_free(path);
}
enable_notify_events();
}
virtual bool get_selected(weld::TreeIter* pIter) const override
{
GtkInstanceTreeIter* pGtkIter = static_cast<GtkInstanceTreeIter*>(pIter);
return get_selected_iterator(pGtkIter ? &pGtkIter->iter : nullptr);
}
virtual bool get_cursor(weld::TreeIter* pIter) const override
{
GtkInstanceTreeIter* pGtkIter = static_cast<GtkInstanceTreeIter*>(pIter);
GtkTreePath* path;
gtk_icon_view_get_cursor(m_pIconView, &path, nullptr);
if (pGtkIter && path)
{
GtkTreeModel *pModel = GTK_TREE_MODEL(m_pTreeStore);
gtk_tree_model_get_iter(pModel, &pGtkIter->iter, path);
}
return path != nullptr;
}
virtual void set_cursor(const weld::TreeIter& rIter) override
{
disable_notify_events();
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreeModel *pModel = GTK_TREE_MODEL(m_pTreeStore);
GtkTreePath* path = gtk_tree_model_get_path(pModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
gtk_icon_view_set_cursor(m_pIconView, path, nullptr, false);
gtk_tree_path_free(path);
enable_notify_events();
}
virtual bool get_iter_first(weld::TreeIter& rIter) const override
{
GtkInstanceTreeIter& rGtkIter = static_cast<GtkInstanceTreeIter&>(rIter);
GtkTreeModel *pModel = GTK_TREE_MODEL(m_pTreeStore);
return gtk_tree_model_get_iter_first(pModel, &rGtkIter.iter);
}
virtual void scroll_to_item(const weld::TreeIter& rIter) override
{
assert(gtk_icon_view_get_model(m_pIconView) && "don't select when frozen, select after thaw. Note selection doesn't survive a freeze");
disable_notify_events();
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
GtkTreeModel *pModel = GTK_TREE_MODEL(m_pTreeStore);
GtkTreePath* path = gtk_tree_model_get_path(pModel, const_cast<GtkTreeIter*>(&rGtkIter.iter));
gtk_icon_view_scroll_to_path(m_pIconView, path, false, 0, 0);
gtk_tree_path_free(path);
enable_notify_events();
}
virtual std::unique_ptr<weld::TreeIter> make_iterator(const weld::TreeIter* pOrig) const override
{
return std::unique_ptr<weld::TreeIter>(new GtkInstanceTreeIter(static_cast<const GtkInstanceTreeIter*>(pOrig)));
}
virtual void selected_foreach(const std::function<bool(weld::TreeIter&)>& func) override
{
GtkInstanceTreeIter aGtkIter(nullptr);
GtkTreeModel *pModel = GTK_TREE_MODEL(m_pTreeStore);
GList* pList = gtk_icon_view_get_selected_items(m_pIconView);
for (GList* pItem = g_list_first(pList); pItem; pItem = g_list_next(pItem))
{
GtkTreePath* path = static_cast<GtkTreePath*>(pItem->data);
gtk_tree_model_get_iter(pModel, &aGtkIter.iter, path);
if (func(aGtkIter))
break;
}
g_list_free_full(pList, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
}
virtual int n_children() const override
{
return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_pTreeStore), nullptr);
}
virtual OUString get_id(const weld::TreeIter& rIter) const override
{
const GtkInstanceTreeIter& rGtkIter = static_cast<const GtkInstanceTreeIter&>(rIter);
return get(rGtkIter.iter, m_nIdCol);
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pIconView, m_nSelectionChangedSignalId);
g_signal_handler_block(m_pIconView, m_nItemActivatedSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pIconView, m_nItemActivatedSignalId);
g_signal_handler_unblock(m_pIconView, m_nSelectionChangedSignalId);
}
virtual ~GtkInstanceIconView() override
{
if (m_pSelectionChangeEvent)
Application::RemoveUserEvent(m_pSelectionChangeEvent);
if (m_nQueryTooltipSignalId)
g_signal_handler_disconnect(m_pIconView, m_nQueryTooltipSignalId);
g_signal_handler_disconnect(m_pIconView, m_nItemActivatedSignalId);
g_signal_handler_disconnect(m_pIconView, m_nSelectionChangedSignalId);
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pIconView, m_nPopupMenu);
#endif
}
};
}
IMPL_LINK_NOARG(GtkInstanceIconView, async_signal_selection_changed, void*, void)
{
m_pSelectionChangeEvent = nullptr;
signal_selection_changed();
}
namespace {
class GtkInstanceSpinButton : public GtkInstanceEditable, public virtual weld::SpinButton
{
private:
GtkSpinButton* m_pButton;
gulong m_nValueChangedSignalId;
gulong m_nOutputSignalId;
gulong m_nInputSignalId;
bool m_bFormatting;
bool m_bBlockOutput;
bool m_bBlank;
static void signalValueChanged(GtkSpinButton*, gpointer widget)
{
GtkInstanceSpinButton* pThis = static_cast<GtkInstanceSpinButton*>(widget);
SolarMutexGuard aGuard;
pThis->m_bBlank = false;
pThis->signal_value_changed();
}
bool guarded_signal_output()
{
if (m_bBlockOutput)
return true;
m_bFormatting = true;
bool bRet = signal_output();
m_bFormatting = false;
return bRet;
}
static gboolean signalOutput(GtkSpinButton*, gpointer widget)
{
GtkInstanceSpinButton* pThis = static_cast<GtkInstanceSpinButton*>(widget);
SolarMutexGuard aGuard;
return pThis->guarded_signal_output();
}
static gint signalInput(GtkSpinButton*, gdouble* new_value, gpointer widget)
{
GtkInstanceSpinButton* pThis = static_cast<GtkInstanceSpinButton*>(widget);
SolarMutexGuard aGuard;
int result;
TriState eHandled = pThis->signal_input(&result);
if (eHandled == TRISTATE_INDET)
return 0;
if (eHandled == TRISTATE_TRUE)
{
*new_value = pThis->toGtk(result);
return 1;
}
return GTK_INPUT_ERROR;
}
virtual void signal_activate() override
{
gtk_spin_button_update(m_pButton);
GtkInstanceEditable::signal_activate();
}
double toGtk(sal_Int64 nValue) const
{
return static_cast<double>(nValue) / Power10(get_digits());
}
sal_Int64 fromGtk(double fValue) const
{
return FRound(fValue * Power10(get_digits()));
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalScroll(GtkWidget* pWidget, GdkEventScroll* /*pEvent*/, gpointer /*widget*/)
{
// tdf#149823 follow WheelBehavior setting, so if we don't have focus
// we don't react to the scroll-event.
MouseWheelBehaviour nWheelBehavior(Application::GetSettings().GetMouseSettings().GetWheelBehavior());
switch (nWheelBehavior)
{
case MouseWheelBehaviour::ALWAYS:
break;
case MouseWheelBehaviour::Disable:
g_signal_stop_emission_by_name(pWidget, "scroll-event");
break;
case MouseWheelBehaviour::FocusOnly:
if (!gtk_widget_has_focus(pWidget))
g_signal_stop_emission_by_name(pWidget, "scroll-event");
break;
}
return false;
}
#endif
public:
GtkInstanceSpinButton(GtkSpinButton* pButton, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceEditable(GTK_WIDGET(pButton), pBuilder, bTakeOwnership)
, m_pButton(pButton)
, m_nValueChangedSignalId(g_signal_connect(pButton, "value-changed", G_CALLBACK(signalValueChanged), this))
, m_nOutputSignalId(g_signal_connect(pButton, "output", G_CALLBACK(signalOutput), this))
, m_nInputSignalId(g_signal_connect(pButton, "input", G_CALLBACK(signalInput), this))
, m_bFormatting(false)
, m_bBlockOutput(false)
, m_bBlank(false)
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_text_set_activates_default(GTK_TEXT(m_pDelegate), true);
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_connect(pButton, "scroll-event", G_CALLBACK(signalScroll), this);
#endif
}
virtual sal_Int64 get_value() const override
{
return fromGtk(gtk_spin_button_get_value(m_pButton));
}
virtual void set_value(sal_Int64 value) override
{
disable_notify_events();
m_bBlank = false;
gtk_spin_button_set_value(m_pButton, toGtk(value));
enable_notify_events();
}
virtual void set_text(const OUString& rText) override
{
disable_notify_events();
// tdf#122786 if we're just formatting a value, then we're done,
// however if set_text has been called directly we want to update our
// value from this new text, but don't want to reformat with that value
if (!m_bFormatting)
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_editable_set_text(m_pEditable, OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr());
#else
gtk_entry_set_text(GTK_ENTRY(m_pButton), OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr());
#endif
m_bBlockOutput = true;
gtk_spin_button_update(m_pButton);
m_bBlank = rText.isEmpty();
m_bBlockOutput = false;
}
else
{
bool bKeepBlank = m_bBlank && get_value() == 0;
if (!bKeepBlank)
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_editable_set_text(m_pEditable, OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr());
#else
gtk_entry_set_text(GTK_ENTRY(m_pButton), OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr());
#endif
m_bBlank = false;
}
}
enable_notify_events();
}
virtual void set_range(sal_Int64 min, sal_Int64 max) override
{
disable_notify_events();
gtk_spin_button_set_range(m_pButton, toGtk(min), toGtk(max));
enable_notify_events();
}
virtual void get_range(sal_Int64& min, sal_Int64& max) const override
{
double gtkmin, gtkmax;
gtk_spin_button_get_range(m_pButton, >kmin, >kmax);
min = fromGtk(gtkmin);
max = fromGtk(gtkmax);
}
virtual void set_increments(int step, int page) override
{
disable_notify_events();
gtk_spin_button_set_increments(m_pButton, toGtk(step), toGtk(page));
enable_notify_events();
}
virtual void get_increments(int& step, int& page) const override
{
double gtkstep, gtkpage;
gtk_spin_button_get_increments(m_pButton, >kstep, >kpage);
step = fromGtk(gtkstep);
page = fromGtk(gtkpage);
}
virtual void set_digits(unsigned int digits) override
{
disable_notify_events();
gtk_spin_button_set_digits(m_pButton, digits);
enable_notify_events();
}
virtual unsigned int get_digits() const override
{
return gtk_spin_button_get_digits(m_pButton);
}
virtual void set_font(const vcl::Font& rFont) override
{
m_aCustomFont.use_custom_font(&rFont, u"spinbutton");
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pButton, m_nValueChangedSignalId);
GtkInstanceEditable::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceEditable::enable_notify_events();
g_signal_handler_unblock(m_pButton, m_nValueChangedSignalId);
}
virtual ~GtkInstanceSpinButton() override
{
g_signal_handler_disconnect(m_pButton, m_nInputSignalId);
g_signal_handler_disconnect(m_pButton, m_nOutputSignalId);
g_signal_handler_disconnect(m_pButton, m_nValueChangedSignalId);
}
};
}
namespace {
class GtkInstanceFormattedSpinButton : public GtkInstanceEditable, public virtual weld::FormattedSpinButton
{
private:
GtkSpinButton* m_pButton;
std::unique_ptr<weld::EntryFormatter> m_xOwnFormatter;
weld::EntryFormatter* m_pFormatter;
gulong m_nValueChangedSignalId;
gulong m_nOutputSignalId;
gulong m_nInputSignalId;
bool m_bEmptyField;
bool m_bSyncingValue;
double m_dValueWhenEmpty;
bool signal_output()
{
double fValue = gtk_spin_button_get_value(m_pButton);
m_bEmptyField &= fValue == m_dValueWhenEmpty;
if (!m_bEmptyField)
GetFormatter().SetValue(fValue);
return true;
}
static gboolean signalOutput(GtkSpinButton*, gpointer widget)
{
GtkInstanceFormattedSpinButton* pThis = static_cast<GtkInstanceFormattedSpinButton*>(widget);
SolarMutexGuard aGuard;
return pThis->signal_output();
}
gint signal_input(double* value)
{
Formatter& rFormatter = GetFormatter();
rFormatter.Modify();
// if the blank-mode is enabled then if the input is empty don't parse
// the input but keep the value as it is. store what the value the
// blank is associated with and until the value is changed, or the text
// is updated from the outside, don't output that value
m_bEmptyField = rFormatter.IsEmptyFieldEnabled() && get_text().isEmpty();
if (m_bEmptyField)
{
m_dValueWhenEmpty = gtk_spin_button_get_value(m_pButton);
*value = m_dValueWhenEmpty;
}
else
*value = rFormatter.GetValue();
return 1;
}
static gint signalInput(GtkSpinButton*, gdouble* new_value, gpointer widget)
{
GtkInstanceFormattedSpinButton* pThis = static_cast<GtkInstanceFormattedSpinButton*>(widget);
SolarMutexGuard aGuard;
return pThis->signal_input(new_value);
}
static void signalValueChanged(GtkSpinButton*, gpointer widget)
{
GtkInstanceFormattedSpinButton* pThis = static_cast<GtkInstanceFormattedSpinButton*>(widget);
SolarMutexGuard aGuard;
pThis->signal_value_changed();
}
public:
GtkInstanceFormattedSpinButton(GtkSpinButton* pButton, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceEditable(GTK_WIDGET(pButton), pBuilder, bTakeOwnership)
, m_pButton(pButton)
, m_pFormatter(nullptr)
, m_nValueChangedSignalId(g_signal_connect(pButton, "value-changed", G_CALLBACK(signalValueChanged), this))
, m_nOutputSignalId(g_signal_connect(pButton, "output", G_CALLBACK(signalOutput), this))
, m_nInputSignalId(g_signal_connect(pButton, "input", G_CALLBACK(signalInput), this))
, m_bEmptyField(false)
, m_bSyncingValue(false)
, m_dValueWhenEmpty(0.0)
{
}
virtual void set_text(const OUString& rText) override
{
GtkInstanceEditable::set_text(rText);
Formatter& rFormatter = GetFormatter();
m_bEmptyField = rFormatter.IsEmptyFieldEnabled() && rText.isEmpty();
if (m_bEmptyField)
m_dValueWhenEmpty = gtk_spin_button_get_value(m_pButton);
}
virtual void connect_changed(const Link<weld::Entry&, void>& rLink) override
{
if (!m_pFormatter) // once a formatter is set, it takes over "changed"
{
GtkInstanceEditable::connect_changed(rLink);
return;
}
m_pFormatter->connect_changed(rLink);
}
virtual void connect_focus_out(const Link<weld::Widget&, void>& rLink) override
{
if (!m_pFormatter) // once a formatter is set, it takes over "focus-out"
{
GtkInstanceEditable::connect_focus_out(rLink);
return;
}
m_pFormatter->connect_focus_out(rLink);
}
virtual void SetFormatter(weld::EntryFormatter* pFormatter) override
{
m_xOwnFormatter.reset();
m_pFormatter = pFormatter;
sync_range_from_formatter();
sync_value_from_formatter();
sync_increments_from_formatter();
}
virtual weld::EntryFormatter& GetFormatter() override
{
if (!m_pFormatter)
{
auto aFocusOutHdl = m_aFocusOutHdl;
m_aFocusOutHdl = Link<weld::Widget&, void>();
auto aChangeHdl = m_aChangeHdl;
m_aChangeHdl = Link<weld::Entry&, void>();
double fValue = gtk_spin_button_get_value(m_pButton);
double fMin, fMax;
gtk_spin_button_get_range(m_pButton, &fMin, &fMax);
double fStep;
gtk_spin_button_get_increments(m_pButton, &fStep, nullptr);
m_xOwnFormatter.reset(new weld::EntryFormatter(*this));
m_xOwnFormatter->SetMinValue(fMin);
m_xOwnFormatter->SetMaxValue(fMax);
m_xOwnFormatter->SetSpinSize(fStep);
m_xOwnFormatter->SetValue(fValue);
m_xOwnFormatter->connect_focus_out(aFocusOutHdl);
m_xOwnFormatter->connect_changed(aChangeHdl);
m_pFormatter = m_xOwnFormatter.get();
}
return *m_pFormatter;
}
virtual void sync_value_from_formatter() override
{
if (!m_pFormatter)
return;
// tdf#135317 avoid reenterence
if (m_bSyncingValue)
return;
m_bSyncingValue = true;
disable_notify_events();
// tdf#138519 use gtk_adjustment_set_value instead of gtk_spin_button_set_value because the
// latter doesn't change the value if the new value is less than an EPSILON diff of 1e-10
// from the old value
gtk_adjustment_set_value(gtk_spin_button_get_adjustment(m_pButton), m_pFormatter->GetValue());
enable_notify_events();
m_bSyncingValue = false;
}
virtual void sync_range_from_formatter() override
{
if (!m_pFormatter)
return;
disable_notify_events();
double fMin = m_pFormatter->HasMinValue() ? m_pFormatter->GetMinValue() : std::numeric_limits<double>::lowest();
double fMax = m_pFormatter->HasMaxValue() ? m_pFormatter->GetMaxValue() : std::numeric_limits<double>::max();
gtk_spin_button_set_range(m_pButton, fMin, fMax);
enable_notify_events();
}
virtual void sync_increments_from_formatter() override
{
if (!m_pFormatter)
return;
disable_notify_events();
double fSpinSize = m_pFormatter->GetSpinSize();
gtk_spin_button_set_increments(m_pButton, fSpinSize, fSpinSize * 10);
enable_notify_events();
}
virtual void set_font(const vcl::Font& rFont) override
{
m_aCustomFont.use_custom_font(&rFont, u"spinbutton");
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pButton, m_nValueChangedSignalId);
GtkInstanceEditable::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceEditable::enable_notify_events();
g_signal_handler_unblock(m_pButton, m_nValueChangedSignalId);
}
virtual ~GtkInstanceFormattedSpinButton() override
{
g_signal_handler_disconnect(m_pButton, m_nInputSignalId);
g_signal_handler_disconnect(m_pButton, m_nOutputSignalId);
g_signal_handler_disconnect(m_pButton, m_nValueChangedSignalId);
m_pFormatter = nullptr;
m_xOwnFormatter.reset();
}
};
}
namespace {
class GtkInstanceLabel : public GtkInstanceWidget, public virtual weld::Label
{
private:
GtkLabel* m_pLabel;
void set_text_background_color(const Color& rColor)
{
guint16 nRed = rColor.GetRed() << 8;
guint16 nGreen = rColor.GetGreen() << 8;
guint16 nBlue = rColor.GetBlue() << 8;
PangoAttrType aFilterAttrs[] = {PANGO_ATTR_BACKGROUND, PANGO_ATTR_INVALID};
PangoAttrList* pOrigList = gtk_label_get_attributes(m_pLabel);
PangoAttrList* pAttrs = pOrigList ? pango_attr_list_copy(pOrigList) : pango_attr_list_new();
PangoAttrList* pRemovedAttrs = pOrigList ? pango_attr_list_filter(pAttrs, filter_pango_attrs, &aFilterAttrs) : nullptr;
pango_attr_list_insert(pAttrs, pango_attr_background_new(nRed, nGreen, nBlue));
gtk_label_set_attributes(m_pLabel, pAttrs);
pango_attr_list_unref(pAttrs);
pango_attr_list_unref(pRemovedAttrs);
}
void set_text_foreground_color(const Color& rColor, bool bSetBold)
{
guint16 nRed = rColor.GetRed() << 8;
guint16 nGreen = rColor.GetGreen() << 8;
guint16 nBlue = rColor.GetBlue() << 8;
PangoAttrType aFilterAttrs[] = {PANGO_ATTR_FOREGROUND, PANGO_ATTR_WEIGHT, PANGO_ATTR_INVALID};
if (!bSetBold)
aFilterAttrs[1] = PANGO_ATTR_INVALID;
PangoAttrList* pOrigList = gtk_label_get_attributes(m_pLabel);
PangoAttrList* pAttrs = pOrigList ? pango_attr_list_copy(pOrigList) : pango_attr_list_new();
PangoAttrList* pRemovedAttrs = pOrigList ? pango_attr_list_filter(pAttrs, filter_pango_attrs, &aFilterAttrs) : nullptr;
if (rColor != COL_AUTO)
pango_attr_list_insert(pAttrs, pango_attr_foreground_new(nRed, nGreen, nBlue));
if (bSetBold)
pango_attr_list_insert(pAttrs, pango_attr_weight_new(PANGO_WEIGHT_BOLD));
gtk_label_set_attributes(m_pLabel, pAttrs);
pango_attr_list_unref(pAttrs);
pango_attr_list_unref(pRemovedAttrs);
}
public:
GtkInstanceLabel(GtkLabel* pLabel, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pLabel), pBuilder, bTakeOwnership)
, m_pLabel(pLabel)
{
}
virtual void set_label(const OUString& rText) override
{
::set_label(m_pLabel, rText);
}
virtual OUString get_label() const override
{
return ::get_label(m_pLabel);
}
virtual void set_mnemonic_widget(Widget* pTarget) override
{
assert(!gtk_label_get_selectable(m_pLabel) && "don't use set_mnemonic_widget on selectable labels, for consistency with gen backend");
GtkInstanceWidget* pTargetWidget = dynamic_cast<GtkInstanceWidget*>(pTarget);
gtk_label_set_mnemonic_widget(m_pLabel, pTargetWidget ? pTargetWidget->getWidget() : nullptr);
}
virtual void set_label_type(weld::LabelType eType) override
{
switch (eType)
{
case weld::LabelType::Normal:
gtk_label_set_attributes(m_pLabel, nullptr);
break;
case weld::LabelType::Warning:
set_text_background_color(Application::GetSettings().GetStyleSettings().GetWarningColor());
break;
case weld::LabelType::Error:
set_text_background_color(Application::GetSettings().GetStyleSettings().GetHighlightColor());
break;
case weld::LabelType::Title:
set_text_foreground_color(Application::GetSettings().GetStyleSettings().GetLightColor(), true);
break;
}
}
virtual void set_font(const vcl::Font& rFont) override
{
::set_font(m_pLabel, rFont);
}
virtual void set_font_color(const Color& rColor) override
{
set_text_foreground_color(rColor, false);
}
};
}
std::unique_ptr<weld::Label> GtkInstanceFrame::weld_label_widget() const
{
GtkWidget* pLabel = gtk_frame_get_label_widget(m_pFrame);
if (!pLabel || !GTK_IS_LABEL(pLabel))
return nullptr;
return std::make_unique<GtkInstanceLabel>(GTK_LABEL(pLabel), m_pBuilder, false);
}
namespace {
GdkClipboard* widget_get_clipboard(GtkWidget* pWidget)
{
#if GTK_CHECK_VERSION(4, 0, 0)
return gtk_widget_get_clipboard(pWidget);
#else
return gtk_widget_get_clipboard(pWidget, GDK_SELECTION_CLIPBOARD);
#endif
}
class GtkInstanceTextView : public GtkInstanceWidget, public virtual weld::TextView
{
private:
GtkTextView* m_pTextView;
GtkTextBuffer* m_pTextBuffer;
GtkAdjustment* m_pVAdjustment;
GtkCssProvider* m_pFgCssProvider;
WidgetFont m_aCustomFont;
int m_nMaxTextLength;
gulong m_nChangedSignalId; // we don't disable/enable this one, it's to implement max-length
gulong m_nInsertTextSignalId;
gulong m_nCursorPosSignalId;
gulong m_nHasSelectionSignalId; // we don't disable/enable this one, it's to implement
// auto-scroll to cursor on losing selection
gulong m_nVAdjustChangedSignalId;
#if !GTK_CHECK_VERSION(4, 0, 0)
gulong m_nButtonPressEvent; // we don't disable/enable this one, it's to block mouse
// click down from getting to (potential) toplevel
// GtkSalFrame parent, which grabs focus away
static gboolean signalButtonPressEvent(GtkWidget*, GdkEventButton*, gpointer)
{
// e.g. on clicking on the help TextView in OTableDesignHelpBar the currently displayed text shouldn't disappear
return true;
}
#endif
static void signalChanged(GtkTextBuffer*, gpointer widget)
{
GtkInstanceTextView* pThis = static_cast<GtkInstanceTextView*>(widget);
SolarMutexGuard aGuard;
pThis->signal_changed();
}
static void signalInserText(GtkTextBuffer *pBuffer, GtkTextIter *pLocation, gchar* /*pText*/, gint /*nLen*/, gpointer widget)
{
GtkInstanceTextView* pThis = static_cast<GtkInstanceTextView*>(widget);
pThis->insert_text(pBuffer, pLocation);
}
void insert_text(GtkTextBuffer *pBuffer, GtkTextIter *pLocation)
{
if (m_nMaxTextLength)
{
gint nCount = gtk_text_buffer_get_char_count(pBuffer);
if (nCount > m_nMaxTextLength)
{
GtkTextIter nStart, nEnd;
gtk_text_buffer_get_iter_at_offset(m_pTextBuffer, &nStart, m_nMaxTextLength);
gtk_text_buffer_get_end_iter(m_pTextBuffer, &nEnd);
gtk_text_buffer_delete(m_pTextBuffer, &nStart, &nEnd);
gtk_text_iter_assign(pLocation, &nStart);
}
}
}
static void signalCursorPosition(GtkTextBuffer*, GParamSpec*, gpointer widget)
{
GtkInstanceTextView* pThis = static_cast<GtkInstanceTextView*>(widget);
pThis->signal_cursor_position();
}
static void signalHasSelection(GtkTextBuffer*, GParamSpec*, gpointer widget)
{
GtkInstanceTextView* pThis = static_cast<GtkInstanceTextView*>(widget);
pThis->signal_has_selection();
}
void signal_has_selection()
{
/*
in the data browser (Data Sources, shift+ctrl+f4), entering a
multiline cell selects all, on cursoring to the right, the selection
is lost and the cursor is at the end but gtk doesn't auto-scroll to
the cursor so if the text needs scrolling to see the cursor it is off
screen, another cursor makes gtk auto-scroll as wanted. So on losing
selection help gtk out and do the initial scroll ourselves here
*/
if (!gtk_text_buffer_get_has_selection(m_pTextBuffer))
{
GtkTextMark* pMark = gtk_text_buffer_get_insert(m_pTextBuffer);
gtk_text_view_scroll_mark_onscreen(m_pTextView, pMark);
}
}
static void signalVAdjustValueChanged(GtkAdjustment*, gpointer widget)
{
GtkInstanceTextView* pThis = static_cast<GtkInstanceTextView*>(widget);
SolarMutexGuard aGuard;
pThis->signal_vadjustment_changed();
}
public:
GtkInstanceTextView(GtkTextView* pTextView, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pTextView), pBuilder, bTakeOwnership)
, m_pTextView(pTextView)
, m_pTextBuffer(gtk_text_view_get_buffer(pTextView))
, m_pVAdjustment(gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(pTextView)))
, m_pFgCssProvider(nullptr)
, m_aCustomFont(m_pWidget)
, m_nMaxTextLength(0)
, m_nChangedSignalId(g_signal_connect(m_pTextBuffer, "changed", G_CALLBACK(signalChanged), this))
, m_nInsertTextSignalId(g_signal_connect_after(m_pTextBuffer, "insert-text", G_CALLBACK(signalInserText), this))
, m_nCursorPosSignalId(g_signal_connect(m_pTextBuffer, "notify::cursor-position", G_CALLBACK(signalCursorPosition), this))
, m_nHasSelectionSignalId(g_signal_connect(m_pTextBuffer, "notify::has-selection", G_CALLBACK(signalHasSelection), this))
, m_nVAdjustChangedSignalId(g_signal_connect(m_pVAdjustment, "value-changed", G_CALLBACK(signalVAdjustValueChanged), this))
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_nButtonPressEvent(g_signal_connect_after(m_pTextView, "button-press-event", G_CALLBACK(signalButtonPressEvent), this))
#endif
{
}
virtual void set_size_request(int nWidth, int nHeight) override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
{
gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(pParent), nWidth);
gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(pParent), nHeight);
return;
}
gtk_widget_set_size_request(m_pWidget, nWidth, nHeight);
}
virtual void set_text(const OUString& rText) override
{
disable_notify_events();
OString sText(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
gtk_text_buffer_set_text(m_pTextBuffer, sText.getStr(), sText.getLength());
enable_notify_events();
}
virtual OUString get_text() const override
{
GtkTextIter start, end;
gtk_text_buffer_get_bounds(m_pTextBuffer, &start, &end);
char* pStr = gtk_text_buffer_get_text(m_pTextBuffer, &start, &end, true);
OUString sRet(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
g_free(pStr);
return sRet;
}
virtual void replace_selection(const OUString& rText) override
{
disable_notify_events();
gtk_text_buffer_delete_selection(m_pTextBuffer, false, gtk_text_view_get_editable(m_pTextView));
OString sText(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
gtk_text_buffer_insert_at_cursor(m_pTextBuffer, sText.getStr(), sText.getLength());
enable_notify_events();
}
virtual bool get_selection_bounds(int& rStartPos, int& rEndPos) override
{
GtkTextIter start, end;
gtk_text_buffer_get_selection_bounds(m_pTextBuffer, &start, &end);
rStartPos = gtk_text_iter_get_offset(&start);
rEndPos = gtk_text_iter_get_offset(&end);
return rStartPos != rEndPos;
}
virtual void select_region(int nStartPos, int nEndPos) override
{
disable_notify_events();
GtkTextIter start, end;
gtk_text_buffer_get_iter_at_offset(m_pTextBuffer, &start, nStartPos);
gtk_text_buffer_get_iter_at_offset(m_pTextBuffer, &end, nEndPos);
gtk_text_buffer_select_range(m_pTextBuffer, &start, &end);
GtkTextMark* mark = gtk_text_buffer_create_mark(m_pTextBuffer, "scroll", &end, true);
gtk_text_view_scroll_mark_onscreen(m_pTextView, mark);
enable_notify_events();
}
virtual void set_editable(bool bEditable) override
{
gtk_text_view_set_editable(m_pTextView, bEditable);
}
virtual bool get_editable() const override
{
return gtk_text_view_get_editable(m_pTextView);
}
virtual void set_max_length(int nChars) override
{
m_nMaxTextLength = nChars;
}
virtual void set_monospace(bool bMonospace) override
{
gtk_text_view_set_monospace(m_pTextView, bMonospace);
}
virtual void set_font_color(const Color& rColor) override
{
const bool bRemoveColor = rColor == COL_AUTO;
if (bRemoveColor && !m_pFgCssProvider)
return;
GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(GTK_WIDGET(m_pTextView));
if (m_pFgCssProvider)
{
gtk_style_context_remove_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFgCssProvider));
m_pFgCssProvider = nullptr;
}
if (bRemoveColor)
return;
OUString sColor = rColor.AsRGBHexString();
m_pFgCssProvider = gtk_css_provider_new();
OUString aBuffer = "textview text { color: #" + sColor + "; }";
OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8);
css_provider_load_from_data(m_pFgCssProvider, aResult.getStr(), aResult.getLength());
gtk_style_context_add_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFgCssProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
virtual void set_font(const vcl::Font& rFont) override
{
m_aCustomFont.use_custom_font(&rFont, u"textview");
}
virtual vcl::Font get_font() override
{
if (const vcl::Font* pFont = m_aCustomFont.get_custom_font())
return *pFont;
return GtkInstanceWidget::get_font();
}
virtual void disable_notify_events() override
{
g_signal_handler_block(m_pVAdjustment, m_nVAdjustChangedSignalId);
g_signal_handler_block(m_pTextBuffer, m_nCursorPosSignalId);
g_signal_handler_block(m_pTextBuffer, m_nChangedSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pTextBuffer, m_nChangedSignalId);
g_signal_handler_unblock(m_pTextBuffer, m_nCursorPosSignalId);
g_signal_handler_unblock(m_pVAdjustment, m_nVAdjustChangedSignalId);
}
// in gtk, 'up' when on the first line, will jump to the start of the line
// if not there already
virtual bool can_move_cursor_with_up() const override
{
GtkTextIter start, end;
gtk_text_buffer_get_selection_bounds(m_pTextBuffer, &start, &end);
return !gtk_text_iter_equal(&start, &end) || !gtk_text_iter_is_start(&start);
}
// in gtk, 'down' when on the first line, will jump to the end of the line
// if not there already
virtual bool can_move_cursor_with_down() const override
{
GtkTextIter start, end;
gtk_text_buffer_get_selection_bounds(m_pTextBuffer, &start, &end);
return !gtk_text_iter_equal(&start, &end) || !gtk_text_iter_is_end(&end);
}
virtual void cut_clipboard() override
{
GdkClipboard *pClipboard = widget_get_clipboard(GTK_WIDGET(m_pTextView));
gtk_text_buffer_cut_clipboard(m_pTextBuffer, pClipboard, get_editable());
}
virtual void copy_clipboard() override
{
GdkClipboard *pClipboard = widget_get_clipboard(GTK_WIDGET(m_pTextView));
gtk_text_buffer_copy_clipboard(m_pTextBuffer, pClipboard);
}
virtual void paste_clipboard() override
{
GdkClipboard *pClipboard = widget_get_clipboard(GTK_WIDGET(m_pTextView));
gtk_text_buffer_paste_clipboard(m_pTextBuffer, pClipboard, nullptr, get_editable());
}
virtual void set_alignment(TxtAlign eXAlign) override
{
GtkJustification eJust = GTK_JUSTIFY_LEFT;
switch (eXAlign)
{
case TxtAlign::Left:
eJust = GTK_JUSTIFY_LEFT;
break;
case TxtAlign::Center:
eJust = GTK_JUSTIFY_CENTER;
break;
case TxtAlign::Right:
eJust = GTK_JUSTIFY_RIGHT;
break;
}
gtk_text_view_set_justification(m_pTextView, eJust);
}
virtual int vadjustment_get_value() const override
{
return gtk_adjustment_get_value(m_pVAdjustment);
}
virtual void vadjustment_set_value(int value) override
{
disable_notify_events();
gtk_adjustment_set_value(m_pVAdjustment, value);
enable_notify_events();
}
virtual int vadjustment_get_upper() const override
{
return gtk_adjustment_get_upper(m_pVAdjustment);
}
virtual int vadjustment_get_lower() const override
{
return gtk_adjustment_get_lower(m_pVAdjustment);
}
virtual int vadjustment_get_page_size() const override
{
return gtk_adjustment_get_page_size(m_pVAdjustment);
}
virtual void show() override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
gtk_widget_show(pParent);
gtk_widget_show(m_pWidget);
}
virtual void hide() override
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
if (GTK_IS_SCROLLED_WINDOW(pParent))
gtk_widget_hide(pParent);
gtk_widget_hide(m_pWidget);
}
virtual ~GtkInstanceTextView() override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pTextView, m_nButtonPressEvent);
#endif
g_signal_handler_disconnect(m_pVAdjustment, m_nVAdjustChangedSignalId);
g_signal_handler_disconnect(m_pTextBuffer, m_nInsertTextSignalId);
g_signal_handler_disconnect(m_pTextBuffer, m_nChangedSignalId);
g_signal_handler_disconnect(m_pTextBuffer, m_nCursorPosSignalId);
g_signal_handler_disconnect(m_pTextBuffer, m_nHasSelectionSignalId);
}
};
}
namespace {
class GtkInstanceDrawingArea;
// IMHandler
class IMHandler
{
private:
GtkInstanceDrawingArea* m_pArea;
#if GTK_CHECK_VERSION(4, 0, 0)
GtkEventController* m_pFocusController;
#endif
GtkIMContext* m_pIMContext;
OUString m_sPreeditText;
gulong m_nFocusInSignalId;
gulong m_nFocusOutSignalId;
bool m_bExtTextInput;
public:
IMHandler(GtkInstanceDrawingArea* pArea);
void signalFocus(bool bIn);
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalFocusIn(GtkEventControllerFocus*, gpointer im_handler);
#else
static gboolean signalFocusIn(GtkWidget*, GdkEvent*, gpointer im_handler);
#endif
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalFocusOut(GtkEventControllerFocus*, gpointer im_handler);
#else
static gboolean signalFocusOut(GtkWidget*, GdkEvent*, gpointer im_handler);
#endif
~IMHandler();
void updateIMSpotLocation();
void set_cursor_location(const tools::Rectangle& rRect);
static void signalIMCommit(GtkIMContext* /*pContext*/, gchar* pText, gpointer im_handler);
static void signalIMPreeditChanged(GtkIMContext* pIMContext, gpointer im_handler);
static gboolean signalIMRetrieveSurrounding(GtkIMContext* pContext, gpointer im_handler);
static gboolean signalIMDeleteSurrounding(GtkIMContext*, gint nOffset, gint nChars,
gpointer im_handler);
void StartExtTextInput();
static void signalIMPreeditStart(GtkIMContext*, gpointer im_handler);
void EndExtTextInput();
static void signalIMPreeditEnd(GtkIMContext*, gpointer im_handler);
#if !GTK_CHECK_VERSION(4, 0, 0)
bool im_context_filter_keypress(const GdkEventKey* pEvent);
#endif
};
#if !GTK_CHECK_VERSION(4, 0, 0)
AtkObject* (*default_drawing_area_get_accessible)(GtkWidget *widget);
#endif
class GtkInstanceDrawingArea : public GtkInstanceWidget, public virtual weld::DrawingArea
{
private:
GtkDrawingArea* m_pDrawingArea;
a11yref m_xAccessible;
#if !GTK_CHECK_VERSION(4, 0, 0)
AtkObject *m_pAccessible;
#endif
ScopedVclPtrInstance<VirtualDevice> m_xDevice;
std::unique_ptr<IMHandler> m_xIMHandler;
cairo_surface_t* m_pSurface;
#if !GTK_CHECK_VERSION(4, 0, 0)
gulong m_nDrawSignalId;
#endif
gulong m_nQueryTooltip;
#if !GTK_CHECK_VERSION(4, 0, 0)
gulong m_nPopupMenu;
gulong m_nScrollEvent;
#endif
GtkGesture *m_pZoomGesture;
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalDraw(GtkDrawingArea*, cairo_t *cr, int /*width*/, int /*height*/, gpointer widget)
#else
static gboolean signalDraw(GtkWidget*, cairo_t* cr, gpointer widget)
#endif
{
GtkInstanceDrawingArea* pThis = static_cast<GtkInstanceDrawingArea*>(widget);
SolarMutexGuard aGuard;
pThis->signal_draw(cr);
#if !GTK_CHECK_VERSION(4, 0, 0)
return false;
#endif
}
void signal_draw(cairo_t* cr)
{
if (!m_pSurface)
return;
GdkRectangle rect;
#if GTK_CHECK_VERSION(4, 0, 0)
double clip_x1, clip_x2, clip_y1, clip_y2;
cairo_clip_extents(cr, &clip_x1, &clip_y1, &clip_x2, &clip_y2);
rect.x = clip_x1;
rect.y = clip_y1;
rect.width = clip_x2 - clip_x1;
rect.height = clip_y2 - clip_y1;
if (rect.width <= 0 || rect.height <= 0)
return;
#else
if (!gdk_cairo_get_clip_rectangle(cr, &rect))
return;
#endif
tools::Rectangle aRect(Point(rect.x, rect.y), Size(rect.width, rect.height));
aRect = m_xDevice->PixelToLogic(aRect);
m_xDevice->Erase(aRect);
m_aDrawHdl.Call(std::pair<vcl::RenderContext&, const tools::Rectangle&>(*m_xDevice, aRect));
cairo_surface_mark_dirty(m_pSurface);
cairo_set_source_surface(cr, m_pSurface, 0, 0);
cairo_paint(cr);
tools::Rectangle aFocusRect(m_aGetFocusRectHdl.Call(*this));
if (!aFocusRect.IsEmpty())
{
gtk_render_focus(gtk_widget_get_style_context(GTK_WIDGET(m_pDrawingArea)), cr,
aFocusRect.Left(), aFocusRect.Top(), aFocusRect.GetWidth(), aFocusRect.GetHeight());
}
}
virtual void signal_size_allocate(guint nWidth, guint nHeight) override
{
Size aNewSize(nWidth, nHeight);
if (m_pSurface && aNewSize == m_xDevice->GetOutputSizePixel())
{
// unchanged
return;
}
m_xDevice->SetOutputSizePixel(Size(nWidth, nHeight));
m_pSurface = get_underlying_cairo_surface(*m_xDevice);
GtkInstanceWidget::signal_size_allocate(nWidth, nHeight);
}
static gboolean signalQueryTooltip(GtkWidget* pGtkWidget, gint x, gint y,
gboolean /*keyboard_mode*/, GtkTooltip *tooltip,
gpointer widget)
{
GtkInstanceDrawingArea* pThis = static_cast<GtkInstanceDrawingArea*>(widget);
tools::Rectangle aHelpArea(x, y);
OUString aTooltip = pThis->signal_query_tooltip(aHelpArea);
if (aTooltip.isEmpty())
return false;
gtk_tooltip_set_text(tooltip, OUStringToOString(aTooltip, RTL_TEXTENCODING_UTF8).getStr());
GdkRectangle aGdkHelpArea;
aGdkHelpArea.x = aHelpArea.Left();
aGdkHelpArea.y = aHelpArea.Top();
aGdkHelpArea.width = aHelpArea.GetWidth();
aGdkHelpArea.height = aHelpArea.GetHeight();
if (pThis->SwapForRTL())
aGdkHelpArea.x = gtk_widget_get_allocated_width(pGtkWidget) - aGdkHelpArea.width - 1 - aGdkHelpArea.x;
gtk_tooltip_set_tip_area(tooltip, &aGdkHelpArea);
return true;
}
virtual bool signal_popup_menu(const CommandEvent& rCEvt) override
{
return signal_command(rCEvt);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
bool signal_scroll(const GdkEventScroll* pEvent)
{
SalWheelMouseEvent aEvt(GtkSalFrame::GetWheelEvent(*pEvent));
if (SwapForRTL())
aEvt.mnX = gtk_widget_get_allocated_width(m_pWidget) - 1 - aEvt.mnX;
CommandWheelMode nMode;
sal_uInt16 nCode = aEvt.mnCode;
bool bHorz = aEvt.mbHorz;
if (nCode & KEY_MOD1)
nMode = CommandWheelMode::ZOOM;
else if (nCode & KEY_MOD2)
nMode = CommandWheelMode::DATAZOOM;
else
{
nMode = CommandWheelMode::SCROLL;
// #i85450# interpret shift-wheel as horizontal wheel action
if( (nCode & (KEY_SHIFT | KEY_MOD1 | KEY_MOD2 | KEY_MOD3)) == KEY_SHIFT )
bHorz = true;
}
CommandWheelData aWheelData(aEvt.mnDelta, aEvt.mnNotchDelta, aEvt.mnScrollLines,
nMode, nCode, bHorz, aEvt.mbDeltaIsPixel);
CommandEvent aCEvt(Point(aEvt.mnX, aEvt.mnY), CommandEventId::Wheel, true, &aWheelData);
return m_aCommandHdl.Call(aCEvt);
}
static gboolean signalScroll(GtkWidget*, GdkEventScroll* pEvent, gpointer widget)
{
GtkInstanceDrawingArea* pThis = static_cast<GtkInstanceDrawingArea*>(widget);
return pThis->signal_scroll(pEvent);
}
#endif
bool handleSignalZoom(GtkGesture* gesture, GdkEventSequence* sequence,
GestureEventZoomType eEventType)
{
gdouble x = 0;
gdouble y = 0;
gtk_gesture_get_point(gesture, sequence, &x, &y);
double fScaleDelta = gtk_gesture_zoom_get_scale_delta(GTK_GESTURE_ZOOM(gesture));
CommandGestureZoomData aGestureData(x, y, eEventType, fScaleDelta);
CommandEvent aCEvt(Point(x, y), CommandEventId::GestureZoom, true, &aGestureData);
return m_aCommandHdl.Call(aCEvt);
}
static bool signalZoomBegin(GtkGesture* gesture, GdkEventSequence* sequence, gpointer widget)
{
GtkInstanceDrawingArea* pThis = static_cast<GtkInstanceDrawingArea*>(widget);
return pThis->handleSignalZoom(gesture, sequence, GestureEventZoomType::Begin);
}
static bool signalZoomUpdate(GtkGesture* gesture, GdkEventSequence* sequence, gpointer widget)
{
GtkInstanceDrawingArea* pThis = static_cast<GtkInstanceDrawingArea*>(widget);
return pThis->handleSignalZoom(gesture, sequence, GestureEventZoomType::Update);
}
static bool signalZoomEnd(GtkGesture* gesture, GdkEventSequence* sequence, gpointer widget)
{
GtkInstanceDrawingArea* pThis = static_cast<GtkInstanceDrawingArea*>(widget);
return pThis->handleSignalZoom(gesture, sequence, GestureEventZoomType::End);
}
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalResize(GtkDrawingArea*, int nWidth, int nHeight, gpointer widget)
{
GtkInstanceWidget* pThis = static_cast<GtkInstanceWidget*>(widget);
SolarMutexGuard aGuard;
pThis->signal_size_allocate(nWidth, nHeight);
}
#endif
public:
GtkInstanceDrawingArea(GtkDrawingArea* pDrawingArea, GtkInstanceBuilder* pBuilder, a11yref xA11y, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pDrawingArea), pBuilder, bTakeOwnership)
, m_pDrawingArea(pDrawingArea)
, m_xAccessible(std::move(xA11y))
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_pAccessible(nullptr)
#endif
, m_xDevice(DeviceFormat::WITHOUT_ALPHA)
, m_pSurface(nullptr)
, m_nQueryTooltip(g_signal_connect(m_pDrawingArea, "query-tooltip", G_CALLBACK(signalQueryTooltip), this))
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_nPopupMenu(g_signal_connect(m_pDrawingArea, "popup-menu", G_CALLBACK(signalPopupMenu), this))
, m_nScrollEvent(g_signal_connect(m_pDrawingArea, "scroll-event", G_CALLBACK(signalScroll), this))
#endif
{
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_drawing_area_set_draw_func(m_pDrawingArea, signalDraw, this, nullptr);
#else
m_nDrawSignalId = g_signal_connect(m_pDrawingArea, "draw", G_CALLBACK(signalDraw), this);
gtk_widget_add_events(GTK_WIDGET(pDrawingArea), GDK_TOUCHPAD_GESTURE_MASK);
#endif
ensureMouseEventWidget();
#if GTK_CHECK_VERSION(4,0,0)
m_pZoomGesture = gtk_gesture_zoom_new();
gtk_widget_add_controller(m_pMouseEventBox, GTK_EVENT_CONTROLLER(m_pZoomGesture));
#else
m_pZoomGesture = gtk_gesture_zoom_new(m_pMouseEventBox);
#endif
gtk_event_controller_set_propagation_phase(GTK_EVENT_CONTROLLER(m_pZoomGesture),
GTK_PHASE_TARGET);
// Note that the default zoom gesture signal handler needs to run first to setup correct
// scale delta. Otherwise the first "begin" event will always contain scale delta of infinity.
g_signal_connect_after(m_pZoomGesture, "begin", G_CALLBACK(signalZoomBegin), this);
g_signal_connect_after(m_pZoomGesture, "update", G_CALLBACK(signalZoomUpdate), this);
g_signal_connect_after(m_pZoomGesture, "end", G_CALLBACK(signalZoomEnd), this);
gtk_widget_set_has_tooltip(m_pWidget, true);
g_object_set_data(G_OBJECT(m_pDrawingArea), "g-lo-GtkInstanceDrawingArea", this);
m_xDevice->EnableRTL(get_direction());
}
#if !GTK_CHECK_VERSION(4, 0, 0)
AtkObject* GetAtkObject(AtkObject* pDefaultAccessible)
{
if (!m_pAccessible && m_xAccessible.is())
{
GtkWidget* pParent = gtk_widget_get_parent(m_pWidget);
m_pAccessible = atk_object_wrapper_new(m_xAccessible, gtk_widget_get_accessible(pParent), pDefaultAccessible);
if (m_pAccessible)
g_object_ref(m_pAccessible);
}
return m_pAccessible;
}
#endif
#if GTK_CHECK_VERSION(4, 0, 0)
virtual void connect_size_allocate(const Link<const Size&, void>& rLink) override
{
m_nSizeAllocateSignalId = g_signal_connect(m_pWidget, "resize", G_CALLBACK(signalResize), this);
weld::Widget::connect_size_allocate(rLink);
}
#endif
virtual void connect_mouse_press(const Link<const MouseEvent&, bool>& rLink) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!(gtk_widget_get_events(m_pWidget) & GDK_BUTTON_PRESS_MASK))
gtk_widget_add_events(m_pWidget, GDK_BUTTON_PRESS_MASK);
#endif
GtkInstanceWidget::connect_mouse_press(rLink);
}
virtual void connect_mouse_release(const Link<const MouseEvent&, bool>& rLink) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
if (!(gtk_widget_get_events(m_pWidget) & GDK_BUTTON_RELEASE_MASK))
gtk_widget_add_events(m_pWidget, GDK_BUTTON_RELEASE_MASK);
#endif
GtkInstanceWidget::connect_mouse_release(rLink);
}
virtual void set_direction(bool bRTL) override
{
GtkInstanceWidget::set_direction(bRTL);
m_xDevice->EnableRTL(bRTL);
}
virtual void set_cursor(PointerStyle ePointerStyle) override
{
GdkCursor *pCursor = GtkSalFrame::getDisplay()->getCursor(ePointerStyle);
if (!gtk_widget_get_realized(GTK_WIDGET(m_pDrawingArea)))
gtk_widget_realize(GTK_WIDGET(m_pDrawingArea));
widget_set_cursor(GTK_WIDGET(m_pDrawingArea), pCursor);
}
virtual Point get_pointer_position() const override
{
GdkDisplay *pDisplay = gtk_widget_get_display(m_pWidget);
GdkSeat* pSeat = gdk_display_get_default_seat(pDisplay);
GdkDevice* pPointer = gdk_seat_get_pointer(pSeat);
double x(-1), y(-1);
GdkSurface* pWin = widget_get_surface(m_pWidget);
surface_get_device_position(pWin, pPointer, x, y, nullptr);
return Point(x, y);
}
virtual void set_input_context(const InputContext& rInputContext) override;
virtual void im_context_set_cursor_location(const tools::Rectangle& rCursorRect, int nExtTextInputWidth) override;
int im_context_get_surrounding(OUString& rSurroundingText)
{
return signal_im_context_get_surrounding(rSurroundingText);
}
bool im_context_delete_surrounding(const Selection& rRange)
{
return signal_im_context_delete_surrounding(rRange);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
virtual bool do_signal_key_press(const GdkEventKey* pEvent) override;
virtual bool do_signal_key_release(const GdkEventKey* pEvent) override;
#endif
virtual void queue_draw() override
{
gtk_widget_queue_draw(GTK_WIDGET(m_pDrawingArea));
}
virtual void queue_draw_area(int x, int y, int width, int height) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
tools::Rectangle aRect(Point(x, y), Size(width, height));
aRect = m_xDevice->LogicToPixel(aRect);
gtk_widget_queue_draw_area(GTK_WIDGET(m_pDrawingArea), aRect.Left(), aRect.Top(), aRect.GetWidth(), aRect.GetHeight());
#else
(void)x; (void)y; (void)width; (void)height;
queue_draw();
#endif
}
virtual a11yref get_accessible_parent() override
{
//get_accessible_parent should only be needed for the vcl implementation,
//in the gtk impl the native AtkObject parent set via
//atk_object_wrapper_new(m_xAccessible, gtk_widget_get_accessible(pParent));
//should negate the need.
assert(false && "get_accessible_parent should only be called on a vcl impl");
return uno::Reference<css::accessibility::XAccessible>();
}
virtual a11yrelationset get_accessible_relation_set() override
{
//get_accessible_relation_set should only be needed for the vcl implementation,
//in the gtk impl the native equivalent should negate the need.
assert(false && "get_accessible_relation_set should only be called on a vcl impl");
return uno::Reference<css::accessibility::XAccessibleRelationSet>();
}
virtual Point get_accessible_location_on_screen() override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
AtkObject* pAtkObject = default_drawing_area_get_accessible(m_pWidget);
#endif
gint x(0), y(0);
#if !GTK_CHECK_VERSION(4, 0, 0)
if (pAtkObject && ATK_IS_COMPONENT(pAtkObject))
atk_component_get_extents(ATK_COMPONENT(pAtkObject), &x, &y, nullptr, nullptr, ATK_XY_SCREEN);
#endif
return Point(x, y);
}
virtual void set_accessible_name(const OUString& rName) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
AtkObject* pAtkObject = default_drawing_area_get_accessible(m_pWidget);
if (!pAtkObject)
return;
atk_object_set_name(pAtkObject, OUStringToOString(rName, RTL_TEXTENCODING_UTF8).getStr());
#else
(void)rName;
#endif
}
virtual OUString get_accessible_name() const override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
AtkObject* pAtkObject = default_drawing_area_get_accessible(m_pWidget);
const char* pStr = pAtkObject ? atk_object_get_name(pAtkObject) : nullptr;
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
#else
return OUString();
#endif
}
virtual OUString get_accessible_description() const override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
AtkObject* pAtkObject = default_drawing_area_get_accessible(m_pWidget);
const char* pStr = pAtkObject ? atk_object_get_description(pAtkObject) : nullptr;
return OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
#else
return OUString();
#endif
}
virtual void enable_drag_source(rtl::Reference<TransferDataContainer>& rHelper, sal_uInt8 eDNDConstants) override
{
do_enable_drag_source(rHelper, eDNDConstants);
}
virtual bool do_signal_drag_begin(bool& rUnsetDragIcon) override
{
rUnsetDragIcon = false;
if (m_aDragBeginHdl.Call(*this))
return true;
return false;
}
virtual ~GtkInstanceDrawingArea() override
{
#if GTK_CHECK_VERSION(4,0,0)
gtk_widget_remove_controller(m_pMouseEventBox, GTK_EVENT_CONTROLLER(m_pZoomGesture));
#else
g_clear_object(&m_pZoomGesture);
#endif
g_object_steal_data(G_OBJECT(m_pDrawingArea), "g-lo-GtkInstanceDrawingArea");
#if !GTK_CHECK_VERSION(4, 0, 0)
if (m_pAccessible)
g_object_unref(m_pAccessible);
#endif
css::uno::Reference<css::lang::XComponent> xComp(m_xAccessible, css::uno::UNO_QUERY);
if (xComp.is())
xComp->dispose();
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pDrawingArea, m_nScrollEvent);
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pDrawingArea, m_nPopupMenu);
#endif
g_signal_handler_disconnect(m_pDrawingArea, m_nQueryTooltip);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_drawing_area_set_draw_func(m_pDrawingArea, nullptr, nullptr, nullptr);
#else
g_signal_handler_disconnect(m_pDrawingArea, m_nDrawSignalId);
#endif
}
virtual OutputDevice& get_ref_device() override
{
return *m_xDevice;
}
bool signal_command(const CommandEvent& rCEvt)
{
return m_aCommandHdl.Call(rCEvt);
}
virtual void click(const Point& rPos) override
{
MouseEvent aEvent(rPos);
m_aMousePressHdl.Call(aEvent);
m_aMouseReleaseHdl.Call(aEvent);
}
};
IMHandler::IMHandler(GtkInstanceDrawingArea* pArea)
: m_pArea(pArea)
, m_pIMContext(gtk_im_multicontext_new())
, m_bExtTextInput(false)
{
GtkWidget* pWidget = m_pArea->getWidget();
#if GTK_CHECK_VERSION(4, 0, 0)
m_pFocusController = gtk_event_controller_focus_new();
gtk_widget_add_controller(pWidget, m_pFocusController);
m_nFocusInSignalId = g_signal_connect(m_pFocusController, "enter", G_CALLBACK(signalFocusIn), this);
m_nFocusOutSignalId = g_signal_connect(m_pFocusController, "leave", G_CALLBACK(signalFocusOut), this);
#else
m_nFocusInSignalId = g_signal_connect(pWidget, "focus-in-event", G_CALLBACK(signalFocusIn), this);
m_nFocusOutSignalId = g_signal_connect(pWidget, "focus-out-event", G_CALLBACK(signalFocusOut), this);
#endif
g_signal_connect(m_pIMContext, "preedit-start", G_CALLBACK(signalIMPreeditStart), this);
g_signal_connect(m_pIMContext, "preedit-end", G_CALLBACK(signalIMPreeditEnd), this);
g_signal_connect(m_pIMContext, "commit", G_CALLBACK(signalIMCommit), this);
g_signal_connect(m_pIMContext, "preedit-changed", G_CALLBACK(signalIMPreeditChanged), this);
g_signal_connect(m_pIMContext, "retrieve-surrounding", G_CALLBACK(signalIMRetrieveSurrounding), this);
g_signal_connect(m_pIMContext, "delete-surrounding", G_CALLBACK(signalIMDeleteSurrounding), this);
if (!gtk_widget_get_realized(pWidget))
gtk_widget_realize(pWidget);
im_context_set_client_widget(m_pIMContext, pWidget);
if (gtk_widget_has_focus(m_pArea->getWidget()))
gtk_im_context_focus_in(m_pIMContext);
}
void IMHandler::signalFocus(bool bIn)
{
if (bIn)
gtk_im_context_focus_in(m_pIMContext);
else
gtk_im_context_focus_out(m_pIMContext);
}
#if GTK_CHECK_VERSION(4, 0, 0)
void IMHandler::signalFocusIn(GtkEventControllerFocus*, gpointer im_handler)
#else
gboolean IMHandler::signalFocusIn(GtkWidget*, GdkEvent*, gpointer im_handler)
#endif
{
IMHandler* pThis = static_cast<IMHandler*>(im_handler);
pThis->signalFocus(true);
#if !GTK_CHECK_VERSION(4, 0, 0)
return false;
#endif
}
#if GTK_CHECK_VERSION(4, 0, 0)
void IMHandler::signalFocusOut(GtkEventControllerFocus*, gpointer im_handler)
#else
gboolean IMHandler::signalFocusOut(GtkWidget*, GdkEvent*, gpointer im_handler)
#endif
{
IMHandler* pThis = static_cast<IMHandler*>(im_handler);
pThis->signalFocus(false);
#if !GTK_CHECK_VERSION(4, 0, 0)
return false;
#endif
}
IMHandler::~IMHandler()
{
EndExtTextInput();
#if GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pFocusController, m_nFocusOutSignalId);
g_signal_handler_disconnect(m_pFocusController, m_nFocusInSignalId);
#else
g_signal_handler_disconnect(m_pArea->getWidget(), m_nFocusOutSignalId);
g_signal_handler_disconnect(m_pArea->getWidget(), m_nFocusInSignalId);
#endif
if (gtk_widget_has_focus(m_pArea->getWidget()))
gtk_im_context_focus_out(m_pIMContext);
// first give IC a chance to deinitialize
im_context_set_client_widget(m_pIMContext, nullptr);
// destroy old IC
g_object_unref(m_pIMContext);
}
void IMHandler::updateIMSpotLocation()
{
CommandEvent aCEvt(Point(), CommandEventId::CursorPos);
// we expect set_cursor_location to get triggered by this
m_pArea->signal_command(aCEvt);
}
void IMHandler::set_cursor_location(const tools::Rectangle& rRect)
{
GdkRectangle aArea{static_cast<int>(rRect.Left()), static_cast<int>(rRect.Top()),
static_cast<int>(rRect.GetWidth()), static_cast<int>(rRect.GetHeight())};
gtk_im_context_set_cursor_location(m_pIMContext, &aArea);
}
void IMHandler::signalIMCommit(GtkIMContext* /*pContext*/, gchar* pText, gpointer im_handler)
{
IMHandler* pThis = static_cast<IMHandler*>(im_handler);
SolarMutexGuard aGuard;
// at least editeng expects to have seen a start before accepting a commit
pThis->StartExtTextInput();
OUString sText(pText, strlen(pText), RTL_TEXTENCODING_UTF8);
CommandExtTextInputData aData(sText, nullptr, sText.getLength(), 0, false);
CommandEvent aCEvt(Point(), CommandEventId::ExtTextInput, false, &aData);
pThis->m_pArea->signal_command(aCEvt);
pThis->updateIMSpotLocation();
pThis->EndExtTextInput();
pThis->m_sPreeditText.clear();
}
void IMHandler::signalIMPreeditChanged(GtkIMContext* pIMContext, gpointer im_handler)
{
IMHandler* pThis = static_cast<IMHandler*>(im_handler);
SolarMutexGuard aGuard;
sal_Int32 nCursorPos(0);
sal_uInt8 nCursorFlags(0);
std::vector<ExtTextInputAttr> aInputFlags;
OUString sText = GtkSalFrame::GetPreeditDetails(pIMContext, aInputFlags, nCursorPos, nCursorFlags);
// change from nothing to nothing -> do not start preedit e.g. this
// will activate input into a calc cell without user input
if (sText.isEmpty() && pThis->m_sPreeditText.isEmpty())
return;
pThis->m_sPreeditText = sText;
CommandExtTextInputData aData(sText, aInputFlags.data(), nCursorPos, nCursorFlags, false);
CommandEvent aCEvt(Point(), CommandEventId::ExtTextInput, false, &aData);
pThis->m_pArea->signal_command(aCEvt);
pThis->updateIMSpotLocation();
}
gboolean IMHandler::signalIMRetrieveSurrounding(GtkIMContext* pContext, gpointer im_handler)
{
IMHandler* pThis = static_cast<IMHandler*>(im_handler);
SolarMutexGuard aGuard;
OUString sSurroundingText;
int nCursorIndex = pThis->m_pArea->im_context_get_surrounding(sSurroundingText);
if (nCursorIndex != -1)
{
OString sUTF = OUStringToOString(sSurroundingText, RTL_TEXTENCODING_UTF8);
std::u16string_view sCursorText(sSurroundingText.subView(0, nCursorIndex));
gtk_im_context_set_surrounding(pContext, sUTF.getStr(), sUTF.getLength(),
OUStringToOString(sCursorText, RTL_TEXTENCODING_UTF8).getLength());
}
return true;
}
gboolean IMHandler::signalIMDeleteSurrounding(GtkIMContext*, gint nOffset, gint nChars,
gpointer im_handler)
{
bool bRet = false;
IMHandler* pThis = static_cast<IMHandler*>(im_handler);
SolarMutexGuard aGuard;
OUString sSurroundingText;
sal_Int32 nCursorIndex = pThis->m_pArea->im_context_get_surrounding(sSurroundingText);
Selection aSelection = SalFrame::CalcDeleteSurroundingSelection(sSurroundingText, nCursorIndex, nOffset, nChars);
if (aSelection != Selection(SAL_MAX_UINT32, SAL_MAX_UINT32))
bRet = pThis->m_pArea->im_context_delete_surrounding(aSelection);
return bRet;
}
void IMHandler::StartExtTextInput()
{
if (m_bExtTextInput)
return;
CommandEvent aCEvt(Point(), CommandEventId::StartExtTextInput);
m_pArea->signal_command(aCEvt);
m_bExtTextInput = true;
}
void IMHandler::signalIMPreeditStart(GtkIMContext*, gpointer im_handler)
{
IMHandler* pThis = static_cast<IMHandler*>(im_handler);
SolarMutexGuard aGuard;
pThis->StartExtTextInput();
pThis->updateIMSpotLocation();
}
void IMHandler::EndExtTextInput()
{
if (!m_bExtTextInput)
return;
CommandEvent aCEvt(Point(), CommandEventId::EndExtTextInput);
m_pArea->signal_command(aCEvt);
m_bExtTextInput = false;
}
void IMHandler::signalIMPreeditEnd(GtkIMContext*, gpointer im_handler)
{
IMHandler* pThis = static_cast<IMHandler*>(im_handler);
SolarMutexGuard aGuard;
pThis->updateIMSpotLocation();
pThis->EndExtTextInput();
}
#if !GTK_CHECK_VERSION(4, 0, 0)
bool IMHandler::im_context_filter_keypress(const GdkEventKey* pEvent)
{
return gtk_im_context_filter_keypress(m_pIMContext, const_cast<GdkEventKey*>(pEvent));
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
bool GtkInstanceDrawingArea::do_signal_key_press(const GdkEventKey* pEvent)
{
if (m_xIMHandler && m_xIMHandler->im_context_filter_keypress(pEvent))
return true;
return GtkInstanceWidget::do_signal_key_press(pEvent);
}
bool GtkInstanceDrawingArea::do_signal_key_release(const GdkEventKey* pEvent)
{
if (m_xIMHandler && m_xIMHandler->im_context_filter_keypress(pEvent))
return true;
return GtkInstanceWidget::do_signal_key_release(pEvent);
}
#endif
void GtkInstanceDrawingArea::set_input_context(const InputContext& rInputContext)
{
bool bUseIm(rInputContext.GetOptions() & InputContextFlags::Text);
if (!bUseIm)
{
m_xIMHandler.reset();
return;
}
// create a new im context
if (!m_xIMHandler)
m_xIMHandler.reset(new IMHandler(this));
}
void GtkInstanceDrawingArea::im_context_set_cursor_location(const tools::Rectangle& rCursorRect, int /*nExtTextInputWidth*/)
{
if (!m_xIMHandler)
return;
m_xIMHandler->set_cursor_location(rCursorRect);
}
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalEntryInsertSpecialCharKeyPress(GtkEntry* pEntry, GdkEventKey* pEvent, gpointer)
{
if ((pEvent->keyval == GDK_KEY_S || pEvent->keyval == GDK_KEY_s) &&
(pEvent->state & GDK_MODIFIER_MASK) == static_cast<GdkModifierType>(GDK_SHIFT_MASK|GDK_CONTROL_MASK))
{
if (auto pImplFncGetSpecialChars = vcl::GetGetSpecialCharsFunction())
{
weld::Window* pDialogParent = nullptr;
GtkWidget* pTopLevel = widget_get_toplevel(GTK_WIDGET(pEntry));
if (GtkSalFrame* pFrame = pTopLevel ? GtkSalFrame::getFromWindow(pTopLevel) : nullptr)
pDialogParent = pFrame->GetFrameWeld();
std::unique_ptr<GtkInstanceWindow> xFrameWeld;
if (!pDialogParent && pTopLevel)
{
xFrameWeld.reset(new GtkInstanceWindow(GTK_WINDOW(pTopLevel), nullptr, false));
pDialogParent = xFrameWeld.get();
}
OUString aChars = pImplFncGetSpecialChars(pDialogParent, ::get_font(GTK_WIDGET(pEntry)));
if (!aChars.isEmpty())
{
gtk_editable_delete_selection(GTK_EDITABLE(pEntry));
gint position = gtk_editable_get_position(GTK_EDITABLE(pEntry));
OString sText(OUStringToOString(aChars, RTL_TEXTENCODING_UTF8));
gtk_editable_insert_text(GTK_EDITABLE(pEntry), sText.getStr(), sText.getLength(),
&position);
gtk_editable_set_position(GTK_EDITABLE(pEntry), position);
}
}
return true;
}
return false;
}
#endif
namespace {
GtkBuilder* makeMenuToggleButtonBuilder()
{
#if !GTK_CHECK_VERSION(4, 0, 0)
OUString aUri(AllSettings::GetUIRootDir() + "vcl/ui/menutogglebutton3.ui");
#else
OUString aUri(AllSettings::GetUIRootDir() + "vcl/ui/menutogglebutton4.ui");
#endif
OUString aPath;
osl::FileBase::getSystemPathFromFileURL(aUri, aPath);
return gtk_builder_new_from_file(OUStringToOString(aPath, RTL_TEXTENCODING_UTF8).getStr());
}
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkBuilder* makeComboBoxBuilder()
{
OUString aUri(AllSettings::GetUIRootDir() + "vcl/ui/combobox.ui");
OUString aPath;
osl::FileBase::getSystemPathFromFileURL(aUri, aPath);
return gtk_builder_new_from_file(OUStringToOString(aPath, RTL_TEXTENCODING_UTF8).getStr());
}
// pop down the toplevel combobox menu when something is activated from a custom
// submenu, i.e. wysiwyg style menu
class CustomRenderMenuButtonHelper : public MenuHelper
{
private:
GtkToggleButton* m_pComboBox;
public:
CustomRenderMenuButtonHelper(GtkMenu* pMenu, GtkToggleButton* pComboBox)
: MenuHelper(pMenu, false)
, m_pComboBox(pComboBox)
{
}
virtual void signal_item_activate(const OUString& /*rIdent*/) override
{
gtk_toggle_button_set_active(m_pComboBox, false);
}
};
#endif
gboolean signalTooltipQuery(GtkWidget* pWidget, gint /*x*/, gint /*y*/,
gboolean /*keyboard_mode*/, GtkTooltip *tooltip)
{
const ImplSVHelpData& aHelpData = ImplGetSVHelpData();
if (aHelpData.mbBalloonHelp) // extended tips
{
#if !GTK_CHECK_VERSION(4, 0, 0)
// by default use accessible description
AtkObject* pAtkObject = gtk_widget_get_accessible(pWidget);
const char* pDesc = pAtkObject ? atk_object_get_description(pAtkObject) : nullptr;
if (pDesc && pDesc[0])
{
gtk_tooltip_set_text(tooltip, pDesc);
return true;
}
#endif
// fallback to the mechanism which needs help installed
OUString sHelpId = ::get_help_id(pWidget);
Help* pHelp = !sHelpId.isEmpty() ? Application::GetHelp() : nullptr;
if (pHelp)
{
OUString sHelpText = pHelp->GetHelpText(sHelpId, static_cast<weld::Widget*>(nullptr));
if (!sHelpText.isEmpty())
{
gtk_tooltip_set_text(tooltip, OUStringToOString(sHelpText, RTL_TEXTENCODING_UTF8).getStr());
return true;
}
}
}
const char* pDesc = gtk_widget_get_tooltip_text(pWidget);
if (pDesc && pDesc[0])
{
gtk_tooltip_set_text(tooltip, pDesc);
return true;
}
return false;
}
#if GTK_CHECK_VERSION(4, 0, 0)
class GtkInstanceComboBox : public GtkInstanceWidget, public vcl::ISearchableStringList, public virtual weld::ComboBox
{
private:
GtkComboBox* m_pComboBox;
// GtkOverlay* m_pOverlay;
// GtkTreeView* m_pTreeView;
// GtkMenuButton* m_pOverlayButton; // button that the StyleDropdown uses on an active row
GtkWidget* m_pMenuWindow;
GtkTreeModel* m_pTreeModel;
GtkCellRenderer* m_pButtonTextRenderer;
GtkWidget* m_pEntry;
GtkEditable* m_pEditable;
// GtkCellView* m_pCellView;
GtkEventController* m_pKeyController;
GtkEventController* m_pEntryKeyController;
GtkEventController* m_pMenuKeyController;
GtkEventController* m_pEntryFocusController;
// std::unique_ptr<CustomRenderMenuButtonHelper> m_xCustomMenuButtonHelper;
WidgetFont m_aCustomFont;
std::optional<vcl::Font> m_xEntryFont;
std::unique_ptr<comphelper::string::NaturalStringSorter> m_xSorter;
vcl::QuickSelectionEngine m_aQuickSelectionEngine;
std::vector<std::unique_ptr<GtkTreeRowReference, GtkTreeRowReferenceDeleter>> m_aSeparatorRows;
#if 0
OUString m_sMenuButtonRow;
#endif
// bool m_bHoverSelection;
// bool m_bMouseInOverlayButton;
bool m_bPopupActive;
bool m_bAutoComplete;
bool m_bAutoCompleteCaseSensitive;
bool m_bChangedByMenu;
bool m_bCustomRenderer;
bool m_bUserSelectEntry;
gint m_nTextCol;
gint m_nIdCol;
// gulong m_nToggleFocusInSignalId;
// gulong m_nToggleFocusOutSignalId;
// gulong m_nRowActivatedSignalId;
gulong m_nChangedSignalId;
gulong m_nPopupShownSignalId;
gulong m_nKeyPressEventSignalId;
gulong m_nEntryInsertTextSignalId;
gulong m_nEntryActivateSignalId;
gulong m_nEntryFocusInSignalId;
gulong m_nEntryFocusOutSignalId;
gulong m_nEntryKeyPressEventSignalId;
guint m_nAutoCompleteIdleId;
// gint m_nNonCustomLineHeight;
gint m_nPrePopupCursorPos;
int m_nMRUCount;
int m_nMaxMRUCount;
static gboolean idleAutoComplete(gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->auto_complete();
return false;
}
void auto_complete()
{
m_nAutoCompleteIdleId = 0;
OUString aStartText = get_active_text();
int nStartPos, nEndPos;
get_entry_selection_bounds(nStartPos, nEndPos);
int nMaxSelection = std::max(nStartPos, nEndPos);
if (nMaxSelection != aStartText.getLength())
return;
disable_notify_events();
int nActive = get_active();
int nStart = nActive;
if (nStart == -1)
nStart = 0;
int nPos = -1;
int nZeroRow = 0;
if (m_nMRUCount)
nZeroRow += (m_nMRUCount + 1);
if (!m_bAutoCompleteCaseSensitive)
{
// Try match case insensitive from current position
nPos = starts_with(m_pTreeModel, aStartText, 0, nStart, false);
if (nPos == -1 && nStart != 0)
{
// Try match case insensitive, but from start
nPos = starts_with(m_pTreeModel, aStartText, 0, nZeroRow, false);
}
}
if (nPos == -1)
{
// Try match case sensitive from current position
nPos = starts_with(m_pTreeModel, aStartText, 0, nStart, true);
if (nPos == -1 && nStart != 0)
{
// Try match case sensitive, but from start
nPos = starts_with(m_pTreeModel, aStartText, 0, nZeroRow, true);
}
}
if (nPos != -1)
{
OUString aText = get_text_including_mru(nPos);
if (aText != aStartText)
{
SolarMutexGuard aGuard;
set_active_including_mru(nPos, true);
}
select_entry_region(aText.getLength(), aStartText.getLength());
}
enable_notify_events();
}
static void signalEntryInsertText(GtkEntry* pEntry, const gchar* pNewText, gint nNewTextLength,
gint* position, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
SolarMutexGuard aGuard;
pThis->signal_entry_insert_text(pEntry, pNewText, nNewTextLength, position);
}
void signal_entry_insert_text(GtkEntry* pEntry, const gchar* pNewText, gint nNewTextLength, gint* position)
{
if (m_bPopupActive) // not entered by the user
return;
// first filter inserted text
if (m_aEntryInsertTextHdl.IsSet())
{
OUString sText(pNewText, nNewTextLength, RTL_TEXTENCODING_UTF8);
const bool bContinue = m_aEntryInsertTextHdl.Call(sText);
if (bContinue && !sText.isEmpty())
{
OString sFinalText(OUStringToOString(sText, RTL_TEXTENCODING_UTF8));
g_signal_handlers_block_by_func(pEntry, reinterpret_cast<gpointer>(signalEntryInsertText), this);
gtk_editable_insert_text(GTK_EDITABLE(pEntry), sFinalText.getStr(), sFinalText.getLength(), position);
g_signal_handlers_unblock_by_func(pEntry, reinterpret_cast<gpointer>(signalEntryInsertText), this);
}
g_signal_stop_emission_by_name(pEntry, "insert-text");
}
if (m_bAutoComplete)
{
// now check for autocompletes
if (m_nAutoCompleteIdleId)
g_source_remove(m_nAutoCompleteIdleId);
m_nAutoCompleteIdleId = g_idle_add(idleAutoComplete, this);
}
}
static void signalChanged(GtkComboBox*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
SolarMutexGuard aGuard;
pThis->fire_signal_changed();
}
void fire_signal_changed()
{
m_bUserSelectEntry = true;
m_bChangedByMenu = m_bPopupActive;
signal_changed();
m_bChangedByMenu = false;
}
static void signalPopupToggled(GObject*, GParamSpec*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
SolarMutexGuard aGuard;
pThis->signal_popup_toggled();
}
#if 0
int get_popup_height(gint& rPopupWidth)
{
const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
int nMaxRows = rSettings.GetListBoxMaximumLineCount();
bool bAddScrollWidth = false;
int nRows = get_count_including_mru();
if (nMaxRows < nRows)
{
nRows = nMaxRows;
bAddScrollWidth = true;
}
GList* pColumns = gtk_tree_view_get_columns(m_pTreeView);
gint nRowHeight = get_height_row(m_pTreeView, pColumns);
g_list_free(pColumns);
gint nSeparatorHeight = get_height_row_separator(m_pTreeView);
gint nHeight = get_height_rows(nRowHeight, nSeparatorHeight, nRows);
// if we're using a custom renderer, limit the height to the height nMaxRows would be
// for a normal renderer, and then round down to how many custom rows fit in that
// space
if (m_nNonCustomLineHeight != -1 && nRowHeight)
{
gint nNormalHeight = get_height_rows(m_nNonCustomLineHeight, nSeparatorHeight, nMaxRows);
if (nHeight > nNormalHeight)
{
gint nRowsOnly = nNormalHeight - get_height_rows(0, nSeparatorHeight, nMaxRows);
gint nCustomRows = (nRowsOnly + (nRowHeight - 1)) / nRowHeight;
nHeight = get_height_rows(nRowHeight, nSeparatorHeight, nCustomRows);
}
}
if (bAddScrollWidth)
rPopupWidth += rSettings.GetScrollBarSize();
return nHeight;
}
#endif
bool toggle_button_get_active()
{
GValue value = G_VALUE_INIT;
g_value_init(&value, G_TYPE_BOOLEAN);
g_object_get_property(G_OBJECT(m_pComboBox), "popup-shown", &value);
return g_value_get_boolean(&value);
}
void menu_toggled()
{
if (!m_bPopupActive)
{
#if 0
if (m_bHoverSelection)
{
// turn hover selection back off until mouse is moved again
// *after* menu is shown again
gtk_tree_view_set_hover_selection(m_pTreeView, false);
m_bHoverSelection = false;
}
#endif
if (!m_bUserSelectEntry)
set_active_including_mru(m_nPrePopupCursorPos, true);
#if 0
// undo show_menu tooltip blocking
GtkWidget* pParent = widget_get_toplevel(m_pToggleButton);
GtkSalFrame* pFrame = pParent ? GtkSalFrame::getFromWindow(pParent) : nullptr;
if (pFrame)
pFrame->UnblockTooltip();
#endif
}
else
{
m_nPrePopupCursorPos = get_active();
m_bUserSelectEntry = false;
// if we are in mru mode always start with the cursor at the top of the menu
if (m_nMaxMRUCount)
set_active_including_mru(0, true);
}
}
virtual void signal_popup_toggled() override
{
m_aQuickSelectionEngine.Reset();
bool bOldPopupActive = m_bPopupActive;
m_bPopupActive = toggle_button_get_active();
menu_toggled();
if (bOldPopupActive != m_bPopupActive)
{
ComboBox::signal_popup_toggled();
// restore focus to the GtkEntry when the popup is gone, which
// is what the vcl case does, to ease the transition a little,
// but don't do it if the focus was moved out of togglebutton
// by something else already (e.g. font combobox in toolbar
// on a "direct pick" from the menu which moves focus into
// the main document
if (!m_bPopupActive && m_pEntry && has_child_focus())
{
disable_notify_events();
gtk_widget_grab_focus(m_pEntry);
enable_notify_events();
}
}
}
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalEntryFocusIn(GtkEventControllerFocus*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
SolarMutexGuard aGuard;
pThis->signal_entry_focus_in();
}
#else
static gboolean signalEntryFocusIn(GtkWidget*, GdkEvent*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_entry_focus_in();
return false;
}
#endif
void signal_entry_focus_in()
{
signal_focus_in();
}
#if GTK_CHECK_VERSION(4, 0, 0)
static void signalEntryFocusOut(GtkEventControllerFocus*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
SolarMutexGuard aGuard;
pThis->signal_entry_focus_out();
}
#else
static gboolean signalEntryFocusOut(GtkWidget*, GdkEvent*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_entry_focus_out();
return false;
}
#endif
void signal_entry_focus_out()
{
// if we have an untidy selection on losing focus remove the selection
int nStartPos, nEndPos;
if (get_entry_selection_bounds(nStartPos, nEndPos))
{
int nMin = std::min(nStartPos, nEndPos);
int nMax = std::max(nStartPos, nEndPos);
if (nMin != 0 || nMax != get_active_text().getLength())
select_entry_region(0, 0);
}
signal_focus_out();
}
static void signalEntryActivate(GtkEntry*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_entry_activate();
}
void signal_entry_activate()
{
if (m_aEntryActivateHdl.IsSet())
{
SolarMutexGuard aGuard;
if (m_aEntryActivateHdl.Call(*this))
g_signal_stop_emission_by_name(m_pEntry, "activate");
}
update_mru();
}
OUString get(int pos, int col) const
{
OUString sRet;
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
{
gchar* pStr;
gtk_tree_model_get(m_pTreeModel, &iter, col, &pStr, -1);
sRet = OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
g_free(pStr);
}
return sRet;
}
void set(int pos, int col, std::u16string_view rText)
{
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
{
OString aStr(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
gtk_list_store_set(GTK_LIST_STORE(m_pTreeModel), &iter, col, aStr.getStr(), -1);
}
}
int find(std::u16string_view rStr, int col, bool bSearchMRUArea) const
{
GtkTreeIter iter;
if (!gtk_tree_model_get_iter_first(m_pTreeModel, &iter))
return -1;
int nRet = 0;
if (!bSearchMRUArea && m_nMRUCount)
{
if (!gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, m_nMRUCount + 1))
return -1;
nRet += (m_nMRUCount + 1);
}
OString aStr(OUStringToOString(rStr, RTL_TEXTENCODING_UTF8).getStr());
do
{
gchar* pStr;
gtk_tree_model_get(m_pTreeModel, &iter, col, &pStr, -1);
const bool bEqual = g_strcmp0(pStr, aStr.getStr()) == 0;
g_free(pStr);
if (bEqual)
return nRet;
++nRet;
} while (gtk_tree_model_iter_next(m_pTreeModel, &iter));
return -1;
}
bool separator_function(const GtkTreePath* path)
{
return ::separator_function(path, m_aSeparatorRows);
}
bool separator_function(int pos)
{
GtkTreePath* path = gtk_tree_path_new_from_indices(pos, -1);
bool bRet = separator_function(path);
gtk_tree_path_free(path);
return bRet;
}
static gboolean separatorFunction(GtkTreeModel* pTreeModel, GtkTreeIter* pIter, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
GtkTreePath* path = gtk_tree_model_get_path(pTreeModel, pIter);
bool bRet = pThis->separator_function(path);
gtk_tree_path_free(path);
return bRet;
}
// https://gitlab.gnome.org/GNOME/gtk/issues/310
//
// in the absence of a built-in solution
// a) support typeahead for the case where there is no entry widget, typing ahead
// into the button itself will select via the vcl selection engine, a matching
// entry
static gboolean signalKeyPress(GtkEventControllerKey*, guint keyval, guint keycode, GdkModifierType state, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
SolarMutexGuard aGuard;
return pThis->signal_key_press(CreateKeyEvent(keyval, keycode, state, 0));
}
// tdf#131076 we want return in a ComboBox to act like return in a
// GtkEntry and activate the default dialog/assistant button
bool combobox_activate()
{
GtkWidget *pComboBox = GTK_WIDGET(m_pComboBox);
GtkWidget *pToplevel = widget_get_toplevel(pComboBox);
GtkWindow *pWindow = GTK_WINDOW(pToplevel);
if (!pWindow)
return false;
if (!GTK_IS_DIALOG(pWindow) && !GTK_IS_ASSISTANT(pWindow))
return false;
bool bDone = false;
GtkWidget *pDefaultWidget = gtk_window_get_default_widget(pWindow);
if (pDefaultWidget && pDefaultWidget != pComboBox && gtk_widget_get_sensitive(pDefaultWidget))
bDone = gtk_widget_activate(pDefaultWidget);
return bDone;
}
static gboolean signalEntryKeyPress(GtkEventControllerKey*, guint keyval, guint keycode, GdkModifierType state, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
LocalizeDecimalSeparator(keyval);
return pThis->signal_entry_key_press(CreateKeyEvent(keyval, keycode, state, 0));
}
bool signal_entry_key_press(const KeyEvent& rKEvt)
{
vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
bool bDone = false;
auto nCode = aKeyCode.GetCode();
switch (nCode)
{
case KEY_DOWN:
{
sal_uInt16 nKeyMod = aKeyCode.GetModifier();
if (!nKeyMod)
{
int nCount = get_count_including_mru();
int nActive = get_active_including_mru() + 1;
while (nActive < nCount && separator_function(nActive))
++nActive;
if (nActive < nCount)
set_active_including_mru(nActive, true);
bDone = true;
}
else if (nKeyMod == KEY_MOD2 && !m_bPopupActive)
{
gtk_combo_box_popup(m_pComboBox);
bDone = true;
}
break;
}
case KEY_UP:
{
sal_uInt16 nKeyMod = aKeyCode.GetModifier();
if (!nKeyMod)
{
int nStartBound = m_bPopupActive ? 0 : (m_nMRUCount + 1);
int nActive = get_active_including_mru() - 1;
while (nActive >= nStartBound && separator_function(nActive))
--nActive;
if (nActive >= nStartBound)
set_active_including_mru(nActive, true);
bDone = true;
}
break;
}
case KEY_PAGEUP:
{
sal_uInt16 nKeyMod = aKeyCode.GetModifier();
if (!nKeyMod)
{
int nCount = get_count_including_mru();
int nStartBound = m_bPopupActive ? 0 : (m_nMRUCount + 1);
int nActive = nStartBound;
while (nActive < nCount && separator_function(nActive))
++nActive;
if (nActive < nCount)
set_active_including_mru(nActive, true);
bDone = true;
}
break;
}
case KEY_PAGEDOWN:
{
sal_uInt16 nKeyMod = aKeyCode.GetModifier();
if (!nKeyMod)
{
int nActive = get_count_including_mru() - 1;
int nStartBound = m_bPopupActive ? 0 : (m_nMRUCount + 1);
while (nActive >= nStartBound && separator_function(nActive))
--nActive;
if (nActive >= nStartBound)
set_active_including_mru(nActive, true);
bDone = true;
}
break;
}
default:
break;
}
return bDone;
}
bool signal_key_press(const KeyEvent& rKEvt)
{
#if 0
if (m_bHoverSelection)
{
// once a key is pressed, turn off hover selection until mouse is
// moved again otherwise when the treeview scrolls it jumps to the
// position under the mouse.
gtk_tree_view_set_hover_selection(m_pTreeView, false);
m_bHoverSelection = false;
}
#endif
vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
bool bDone = false;
auto nCode = aKeyCode.GetCode();
switch (nCode)
{
case KEY_DOWN:
case KEY_UP:
case KEY_PAGEUP:
case KEY_PAGEDOWN:
case KEY_HOME:
case KEY_END:
case KEY_LEFT:
case KEY_RIGHT:
case KEY_RETURN:
{
m_aQuickSelectionEngine.Reset();
sal_uInt16 nKeyMod = aKeyCode.GetModifier();
// tdf#131076 don't let bare return toggle menu popup active, but do allow deactivate
if (nCode == KEY_RETURN && !nKeyMod)
{
if (!m_bPopupActive)
bDone = combobox_activate();
else
{
// treat 'return' as if the active entry was clicked on
signalChanged(m_pComboBox, this);
gtk_combo_box_popdown(m_pComboBox);
bDone = true;
}
}
else if (nCode == KEY_UP && nKeyMod == KEY_MOD2 && m_bPopupActive)
{
gtk_combo_box_popdown(m_pComboBox);
bDone = true;
}
else if (nCode == KEY_DOWN && nKeyMod == KEY_MOD2 && !m_bPopupActive)
{
gtk_combo_box_popup(m_pComboBox);
bDone = true;
}
break;
}
case KEY_ESCAPE:
{
m_aQuickSelectionEngine.Reset();
if (m_bPopupActive)
{
gtk_combo_box_popdown(m_pComboBox);
bDone = true;
}
break;
}
default:
// tdf#131076 let base space toggle menu popup when it's not already visible
if (nCode == KEY_SPACE && !aKeyCode.GetModifier() && !m_bPopupActive)
bDone = false;
else
bDone = m_aQuickSelectionEngine.HandleKeyEvent(rKEvt);
break;
}
if (!bDone)
{
if (!m_pEntry)
bDone = signal_entry_key_press(rKEvt);
else
{
// with gtk4-4.2.1 the unconsumed keystrokes don't appear to get to
// the GtkEntry for up/down to move to the next entry without this extra help
// (which means this extra indirection is probably effectively
// the same as if calling signal_entry_key_press directly here)
bDone = gtk_event_controller_key_forward(GTK_EVENT_CONTROLLER_KEY(m_pMenuKeyController), m_pEntry);
}
}
return bDone;
}
vcl::StringEntryIdentifier typeahead_getEntry(int nPos, OUString& out_entryText) const
{
int nEntryCount(get_count_including_mru());
if (nPos >= nEntryCount)
nPos = 0;
out_entryText = get_text_including_mru(nPos);
// vcl::StringEntryIdentifier does not allow for 0 values, but our position is 0-based
// => normalize
return reinterpret_cast<vcl::StringEntryIdentifier>(nPos + 1);
}
static int typeahead_getEntryPos(vcl::StringEntryIdentifier entry)
{
// our pos is 0-based, but StringEntryIdentifier does not allow for a NULL
return reinterpret_cast<sal_Int64>(entry) - 1;
}
int tree_view_get_cursor() const
{
int nRet = -1;
#if 0
GtkTreePath* path;
gtk_tree_view_get_cursor(m_pTreeView, &path, nullptr);
if (path)
{
gint depth;
gint* indices = gtk_tree_path_get_indices_with_depth(path, &depth);
nRet = indices[depth-1];
gtk_tree_path_free(path);
}
#endif
return nRet;
}
int get_selected_entry() const
{
if (m_bPopupActive)
return tree_view_get_cursor();
else
return get_active_including_mru();
}
void set_typeahead_selected_entry(int nSelect)
{
set_active_including_mru(nSelect, true);
}
virtual vcl::StringEntryIdentifier CurrentEntry(OUString& out_entryText) const override
{
int nCurrentPos = get_selected_entry();
return typeahead_getEntry((nCurrentPos == -1) ? 0 : nCurrentPos, out_entryText);
}
virtual vcl::StringEntryIdentifier NextEntry(vcl::StringEntryIdentifier currentEntry, OUString& out_entryText) const override
{
int nNextPos = typeahead_getEntryPos(currentEntry) + 1;
return typeahead_getEntry(nNextPos, out_entryText);
}
virtual void SelectEntry(vcl::StringEntryIdentifier entry) override
{
int nSelect = typeahead_getEntryPos(entry);
if (nSelect == get_selected_entry())
{
// ignore that. This method is a callback from the QuickSelectionEngine, which means the user attempted
// to select the given entry by typing its starting letters. No need to act.
return;
}
// normalize
int nCount = get_count_including_mru();
if (nSelect >= nCount)
nSelect = nCount ? nCount-1 : -1;
set_typeahead_selected_entry(nSelect);
}
#if 0
static void signalGrabBroken(GtkWidget*, GdkEventGrabBroken *pEvent, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->grab_broken(pEvent);
}
void grab_broken(const GdkEventGrabBroken *event)
{
if (event->grab_window == nullptr)
{
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pToggleButton), false);
}
else if (!g_object_get_data(G_OBJECT(event->grab_window), "g-lo-InstancePopup")) // another LibreOffice popover took a grab
{
//try and regrab, so when we lose the grab to the menu of the color palette
//combobox we regain it so the color palette doesn't itself disappear on next
//click on the color palette combobox
do_grab(GTK_WIDGET(m_pMenuWindow));
}
}
static gboolean signalButtonPress(GtkWidget* pWidget, GdkEventButton* pEvent, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
return pThis->button_press(pWidget, pEvent);
}
bool button_press(GtkWidget* pWidget, GdkEventButton* pEvent)
{
//we want to pop down if the button was pressed outside our popup
gdouble x = pEvent->x_root;
gdouble y = pEvent->y_root;
gint xoffset, yoffset;
gdk_window_get_root_origin(widget_get_surface(pWidget), &xoffset, &yoffset);
GtkAllocation alloc;
gtk_widget_get_allocation(pWidget, &alloc);
xoffset += alloc.x;
yoffset += alloc.y;
gtk_widget_get_allocation(GTK_WIDGET(m_pMenuWindow), &alloc);
gint x1 = alloc.x + xoffset;
gint y1 = alloc.y + yoffset;
gint x2 = x1 + alloc.width;
gint y2 = y1 + alloc.height;
if (x > x1 && x < x2 && y > y1 && y < y2)
return false;
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pToggleButton), false);
return false;
}
static gboolean signalMotion(GtkWidget*, GdkEventMotion*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_motion();
return false;
}
void signal_motion()
{
// if hover-selection was disabled after pressing a key, then turn it back on again
if (!m_bHoverSelection && !m_bMouseInOverlayButton)
{
gtk_tree_view_set_hover_selection(m_pTreeView, true);
m_bHoverSelection = true;
}
}
#endif
static void signalRowActivated(GtkTreeView*, GtkTreePath*, GtkTreeViewColumn*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->handle_row_activated();
}
void handle_row_activated()
{
m_bUserSelectEntry = true;
m_bChangedByMenu = true;
disable_notify_events();
int nActive = get_active();
if (m_pEditable)
gtk_editable_set_text(m_pEditable, OUStringToOString(get_text(nActive), RTL_TEXTENCODING_UTF8).getStr());
#if 0
else
tree_view_set_cursor(nActive);
#endif
enable_notify_events();
// gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pToggleButton), false);
fire_signal_changed();
update_mru();
}
void do_clear()
{
disable_notify_events();
gtk_combo_box_set_row_separator_func(m_pComboBox, nullptr, nullptr, nullptr);
m_aSeparatorRows.clear();
gtk_list_store_clear(GTK_LIST_STORE(m_pTreeModel));
m_nMRUCount = 0;
enable_notify_events();
}
virtual int get_max_mru_count() const override
{
return m_nMaxMRUCount;
}
virtual void set_max_mru_count(int nMaxMRUCount) override
{
m_nMaxMRUCount = nMaxMRUCount;
update_mru();
}
void update_mru()
{
int nMRUCount = m_nMRUCount;
if (m_nMaxMRUCount)
{
OUString sActiveText = get_active_text();
OUString sActiveId = get_active_id();
insert_including_mru(0, sActiveText, &sActiveId, nullptr, nullptr);
++m_nMRUCount;
for (int i = 1; i < m_nMRUCount - 1; ++i)
{
if (get_text_including_mru(i) == sActiveText)
{
remove_including_mru(i);
--m_nMRUCount;
break;
}
}
}
while (m_nMRUCount > m_nMaxMRUCount)
{
remove_including_mru(m_nMRUCount - 1);
--m_nMRUCount;
}
if (m_nMRUCount && !nMRUCount)
insert_separator_including_mru(m_nMRUCount, "separator");
else if (!m_nMRUCount && nMRUCount)
remove_including_mru(m_nMRUCount); // remove separator
}
int get_count_including_mru() const
{
return gtk_tree_model_iter_n_children(m_pTreeModel, nullptr);
}
int get_active_including_mru() const
{
return gtk_combo_box_get_active(m_pComboBox);
}
void set_active_including_mru(int pos, bool bInteractive)
{
disable_notify_events();
gtk_combo_box_set_active(m_pComboBox, pos);
m_bChangedByMenu = false;
enable_notify_events();
if (bInteractive && !m_bPopupActive)
signal_changed();
}
int find_text_including_mru(std::u16string_view rStr, bool bSearchMRU) const
{
return find(rStr, m_nTextCol, bSearchMRU);
}
int find_id_including_mru(std::u16string_view rId, bool bSearchMRU) const
{
return find(rId, m_nIdCol, bSearchMRU);
}
OUString get_text_including_mru(int pos) const
{
return get(pos, m_nTextCol);
}
OUString get_id_including_mru(int pos) const
{
return get(pos, m_nIdCol);
}
void set_id_including_mru(int pos, std::u16string_view rId)
{
set(pos, m_nIdCol, rId);
}
void remove_including_mru(int pos)
{
disable_notify_events();
GtkTreeIter iter;
gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos);
if (!m_aSeparatorRows.empty())
{
bool bFound = false;
GtkTreePath* pPath = gtk_tree_path_new_from_indices(pos, -1);
for (auto aIter = m_aSeparatorRows.begin(); aIter != m_aSeparatorRows.end(); ++aIter)
{
GtkTreePath* seppath = gtk_tree_row_reference_get_path(aIter->get());
if (seppath)
{
if (gtk_tree_path_compare(pPath, seppath) == 0)
bFound = true;
gtk_tree_path_free(seppath);
}
if (bFound)
{
m_aSeparatorRows.erase(aIter);
break;
}
}
gtk_tree_path_free(pPath);
}
gtk_list_store_remove(GTK_LIST_STORE(m_pTreeModel), &iter);
enable_notify_events();
}
void insert_separator_including_mru(int pos, const OUString& rId)
{
disable_notify_events();
GtkTreeIter iter;
if (!gtk_combo_box_get_row_separator_func(m_pComboBox))
gtk_combo_box_set_row_separator_func(m_pComboBox, separatorFunction, this, nullptr);
insert_row(GTK_LIST_STORE(m_pTreeModel), iter, pos, &rId, u"", nullptr, nullptr);
GtkTreePath* pPath = gtk_tree_path_new_from_indices(pos, -1);
m_aSeparatorRows.emplace_back(gtk_tree_row_reference_new(m_pTreeModel, pPath));
gtk_tree_path_free(pPath);
enable_notify_events();
}
void insert_including_mru(int pos, std::u16string_view rText, const OUString* pId, const OUString* pIconName, const VirtualDevice* pImageSurface)
{
disable_notify_events();
GtkTreeIter iter;
insert_row(GTK_LIST_STORE(m_pTreeModel), iter, pos, pId, rText, pIconName, pImageSurface);
enable_notify_events();
}
#if 0
static gboolean signalGetChildPosition(GtkOverlay*, GtkWidget*, GdkRectangle* pAllocation, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
return pThis->signal_get_child_position(pAllocation);
}
bool signal_get_child_position(GdkRectangle* pAllocation)
{
if (!gtk_widget_get_visible(GTK_WIDGET(m_pOverlayButton)))
return false;
if (!gtk_widget_get_realized(GTK_WIDGET(m_pTreeView)))
return false;
int nRow = find_id_including_mru(m_sMenuButtonRow, true);
if (nRow == -1)
return false;
gtk_widget_get_preferred_width(GTK_WIDGET(m_pOverlayButton), &pAllocation->width, nullptr);
GtkTreePath* pPath = gtk_tree_path_new_from_indices(nRow, -1);
GList* pColumns = gtk_tree_view_get_columns(m_pTreeView);
tools::Rectangle aRect = get_row_area(m_pTreeView, pColumns, pPath);
gtk_tree_path_free(pPath);
g_list_free(pColumns);
pAllocation->x = aRect.Right() - pAllocation->width;
pAllocation->y = aRect.Top();
pAllocation->height = aRect.GetHeight();
return true;
}
static gboolean signalOverlayButtonCrossing(GtkWidget*, GdkEventCrossing* pEvent, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_overlay_button_crossing(pEvent->type == GDK_ENTER_NOTIFY);
return false;
}
void signal_overlay_button_crossing(bool bEnter)
{
m_bMouseInOverlayButton = bEnter;
if (!bEnter)
return;
if (m_bHoverSelection)
{
// once toggled button is pressed, turn off hover selection until
// mouse leaves the overlay button
gtk_tree_view_set_hover_selection(m_pTreeView, false);
m_bHoverSelection = false;
}
int nRow = find_id_including_mru(m_sMenuButtonRow, true);
assert(nRow != -1);
tree_view_set_cursor(nRow); // select the buttons row
}
#endif
int include_mru(int pos)
{
if (m_nMRUCount && pos != -1)
pos += (m_nMRUCount + 1);
return pos;
}
public:
GtkInstanceComboBox(GtkComboBox* pComboBox, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pComboBox), pBuilder, bTakeOwnership)
, m_pComboBox(pComboBox)
// , m_pOverlay(GTK_OVERLAY(gtk_builder_get_object(pComboBuilder, "overlay")))
// , m_pTreeView(GTK_TREE_VIEW(gtk_builder_get_object(pComboBuilder, "treeview")))
// , m_pOverlayButton(GTK_MENU_BUTTON(gtk_builder_get_object(pComboBuilder, "overlaybutton")))
, m_pMenuWindow(nullptr)
, m_pTreeModel(gtk_combo_box_get_model(pComboBox))
, m_pButtonTextRenderer(nullptr)
// , m_pToggleButton(GTK_WIDGET(gtk_builder_get_object(pComboBuilder, "button")))
, m_pEntry(GTK_IS_ENTRY(gtk_combo_box_get_child(pComboBox)) ? gtk_combo_box_get_child(pComboBox) : nullptr)
, m_pEditable(GTK_EDITABLE(m_pEntry))
, m_aCustomFont(m_pWidget)
// , m_pCellView(nullptr)
, m_aQuickSelectionEngine(*this)
// , m_bHoverSelection(false)
// , m_bMouseInOverlayButton(false)
, m_bPopupActive(false)
, m_bAutoComplete(false)
, m_bAutoCompleteCaseSensitive(false)
, m_bChangedByMenu(false)
, m_bCustomRenderer(false)
, m_bUserSelectEntry(false)
, m_nTextCol(gtk_combo_box_get_entry_text_column(pComboBox))
, m_nIdCol(gtk_combo_box_get_id_column(pComboBox))
// , m_nToggleFocusInSignalId(0)
// , m_nToggleFocusOutSignalId(0)
// , m_nRowActivatedSignalId(g_signal_connect(m_pTreeView, "row-activated", G_CALLBACK(signalRowActivated), this))
, m_nChangedSignalId(g_signal_connect(m_pComboBox, "changed", G_CALLBACK(signalChanged), this))
, m_nPopupShownSignalId(g_signal_connect(m_pComboBox, "notify::popup-shown", G_CALLBACK(signalPopupToggled), this))
, m_nAutoCompleteIdleId(0)
// , m_nNonCustomLineHeight(-1)
, m_nPrePopupCursorPos(-1)
, m_nMRUCount(0)
, m_nMaxMRUCount(0)
{
for (GtkWidget* pChild = gtk_widget_get_first_child(GTK_WIDGET(m_pComboBox));
pChild; pChild = gtk_widget_get_next_sibling(pChild))
{
if (GTK_IS_POPOVER(pChild))
{
m_pMenuWindow = pChild;
break;
}
}
SAL_WARN_IF(!m_pMenuWindow, "vcl.gtk", "GtkInstanceComboBox: couldn't find popup menu");
bool bHasEntry = gtk_combo_box_get_has_entry(m_pComboBox);
bool bPixbufUsedSurface = gtk_tree_model_get_n_columns(m_pTreeModel) == 4;
bool bFindButtonTextRenderer = !bHasEntry;
GtkCellLayout* pCellLayout = GTK_CELL_LAYOUT(m_pComboBox);
GList* cells = gtk_cell_layout_get_cells(pCellLayout);
guint i = g_list_length(cells) - 1;;
// reorder the cell renderers
for (GList* pRenderer = g_list_first(cells); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
gtk_cell_layout_reorder(pCellLayout, pCellRenderer, i--);
if (bFindButtonTextRenderer)
{
m_pButtonTextRenderer = pCellRenderer;
bFindButtonTextRenderer = false;
}
}
// Seeing as GtkCellRendererPixbuf no longer takes a surface, then insert our own replacement
// to render that instead here
if (bPixbufUsedSurface)
{
GtkCellRenderer* pSurfaceRenderer = surface_cell_renderer_new();
gtk_cell_layout_pack_start(pCellLayout, pSurfaceRenderer, false);
gtk_cell_layout_reorder(pCellLayout, pSurfaceRenderer, 0);
gtk_cell_layout_set_attributes(pCellLayout, pSurfaceRenderer, "surface", 3, nullptr);
}
if (bHasEntry)
{
m_bAutoComplete = true;
m_nEntryInsertTextSignalId = g_signal_connect(m_pEditable, "insert-text", G_CALLBACK(signalEntryInsertText), this);
m_nEntryActivateSignalId = g_signal_connect(m_pEntry, "activate", G_CALLBACK(signalEntryActivate), this);
m_pEntryFocusController = GTK_EVENT_CONTROLLER(gtk_event_controller_focus_new());
m_nEntryFocusInSignalId = g_signal_connect(m_pEntryFocusController, "enter", G_CALLBACK(signalEntryFocusIn), this);
m_nEntryFocusOutSignalId = g_signal_connect(m_pEntryFocusController, "leave", G_CALLBACK(signalEntryFocusOut), this);
gtk_widget_add_controller(m_pEntry, m_pEntryFocusController);
m_pEntryKeyController = GTK_EVENT_CONTROLLER(gtk_event_controller_key_new());
m_nEntryKeyPressEventSignalId = g_signal_connect(m_pEntryKeyController, "key-pressed", G_CALLBACK(signalEntryKeyPress), this);
gtk_widget_add_controller(m_pEntry, m_pEntryKeyController);
m_nKeyPressEventSignalId = 0;
m_pKeyController = nullptr;
}
else
{
m_nEntryInsertTextSignalId = 0;
m_nEntryActivateSignalId = 0;
m_pEntryFocusController = nullptr;
m_nEntryFocusInSignalId = 0;
m_nEntryFocusOutSignalId = 0;
m_pEntryKeyController = nullptr;
m_nEntryKeyPressEventSignalId = 0;
m_pKeyController = GTK_EVENT_CONTROLLER(gtk_event_controller_key_new());
m_nKeyPressEventSignalId = g_signal_connect(m_pKeyController, "key-pressed", G_CALLBACK(signalKeyPress), this);
gtk_widget_add_controller(GTK_WIDGET(m_pComboBox), m_pKeyController);
}
// g_signal_connect(m_pMenuWindow, "grab-broken-event", G_CALLBACK(signalGrabBroken), this);
// g_signal_connect(m_pMenuWindow, "button-press-event", G_CALLBACK(signalButtonPress), this);
// g_signal_connect(m_pMenuWindow, "motion-notify-event", G_CALLBACK(signalMotion), this);
// support typeahead for the menu itself, typing into the menu will
// select via the vcl selection engine, a matching entry.
if (m_pMenuWindow)
{
m_pMenuKeyController = GTK_EVENT_CONTROLLER(gtk_event_controller_key_new());
g_signal_connect(m_pMenuKeyController, "key-pressed", G_CALLBACK(signalKeyPress), this);
gtk_widget_add_controller(m_pMenuWindow, m_pMenuKeyController);
}
else
m_pMenuKeyController = nullptr;
#if 0
g_signal_connect(m_pOverlay, "get-child-position", G_CALLBACK(signalGetChildPosition), this);
gtk_overlay_add_overlay(m_pOverlay, GTK_WIDGET(m_pOverlayButton));
g_signal_connect(m_pOverlayButton, "leave-notify-event", G_CALLBACK(signalOverlayButtonCrossing), this);
g_signal_connect(m_pOverlayButton, "enter-notify-event", G_CALLBACK(signalOverlayButtonCrossing), this);
#endif
}
virtual int get_active() const override
{
int nActive = get_active_including_mru();
if (nActive == -1)
return -1;
if (m_nMRUCount)
{
if (nActive < m_nMRUCount)
nActive = find_text(get_text_including_mru(nActive));
else
nActive -= (m_nMRUCount + 1);
}
return nActive;
}
virtual OUString get_active_id() const override
{
int nActive = get_active();
return nActive != -1 ? get_id(nActive) : OUString();
}
virtual void set_active_id(const OUString& rStr) override
{
set_active(find_id(rStr));
m_bChangedByMenu = false;
}
virtual void set_size_request(int nWidth, int nHeight) override
{
if (m_pButtonTextRenderer)
{
// tweak the cell render to get a narrower size to stick
if (nWidth != -1)
{
// this bit isn't great, I really want to be able to ellipse the text in the comboboxtext itself and let
// the popup menu render them in full, in the interim ellipse both of them
g_object_set(G_OBJECT(m_pButtonTextRenderer), "ellipsize", PANGO_ELLIPSIZE_MIDDLE, nullptr);
// to find out how much of the width of the combobox belongs to the cell, set
// the cell and widget to the min cell width and see what the difference is
int min;
gtk_cell_renderer_get_preferred_width(m_pButtonTextRenderer, m_pWidget, &min, nullptr);
gtk_cell_renderer_set_fixed_size(m_pButtonTextRenderer, min, -1);
gtk_widget_set_size_request(m_pWidget, min, -1);
int nNonCellWidth = get_preferred_size().Width() - min;
int nCellWidth = nWidth - nNonCellWidth;
if (nCellWidth >= 0)
{
// now set the cell to the max width which it can be within the
// requested widget width
gtk_cell_renderer_set_fixed_size(m_pButtonTextRenderer, nWidth - nNonCellWidth, -1);
}
}
else
{
g_object_set(G_OBJECT(m_pButtonTextRenderer), "ellipsize", PANGO_ELLIPSIZE_NONE, nullptr);
gtk_cell_renderer_set_fixed_size(m_pButtonTextRenderer, -1, -1);
}
}
gtk_widget_set_size_request(m_pWidget, nWidth, nHeight);
}
virtual void set_active(int pos) override
{
set_active_including_mru(include_mru(pos), false);
}
virtual OUString get_active_text() const override
{
if (m_pEditable)
{
const gchar* pText = gtk_editable_get_text(m_pEditable);
return OUString(pText, pText ? strlen(pText) : 0, RTL_TEXTENCODING_UTF8);
}
int nActive = get_active();
if (nActive == -1)
return OUString();
return get_text(nActive);
}
virtual OUString get_text(int pos) const override
{
if (m_nMRUCount)
pos += (m_nMRUCount + 1);
return get_text_including_mru(pos);
}
virtual OUString get_id(int pos) const override
{
if (m_nMRUCount)
pos += (m_nMRUCount + 1);
return get_id_including_mru(pos);
}
virtual void set_id(int pos, const OUString& rId) override
{
if (m_nMRUCount)
pos += (m_nMRUCount + 1);
set_id_including_mru(pos, rId);
}
virtual void insert_vector(const std::vector<weld::ComboBoxEntry>& rItems, bool bKeepExisting) override
{
freeze();
int nInsertionPoint;
if (!bKeepExisting)
{
clear();
nInsertionPoint = 0;
}
else
nInsertionPoint = get_count();
GtkTreeIter iter;
// tdf#125241 inserting backwards is faster
for (auto aI = rItems.rbegin(); aI != rItems.rend(); ++aI)
{
const auto& rItem = *aI;
insert_row(GTK_LIST_STORE(m_pTreeModel), iter, nInsertionPoint, rItem.sId.isEmpty() ? nullptr : &rItem.sId,
rItem.sString, rItem.sImage.isEmpty() ? nullptr : &rItem.sImage, nullptr);
}
thaw();
}
virtual void remove(int pos) override
{
if (m_nMRUCount)
pos += (m_nMRUCount + 1);
remove_including_mru(pos);
}
virtual void insert(int pos, const OUString& rText, const OUString* pId, const OUString* pIconName, VirtualDevice* pImageSurface) override
{
insert_including_mru(include_mru(pos), rText, pId, pIconName, pImageSurface);
}
virtual void insert_separator(int pos, const OUString& rId) override
{
pos = pos == -1 ? get_count() : pos;
if (m_nMRUCount)
pos += (m_nMRUCount + 1);
insert_separator_including_mru(pos, rId);
}
virtual int get_count() const override
{
int nCount = get_count_including_mru();
if (m_nMRUCount)
nCount -= (m_nMRUCount + 1);
return nCount;
}
virtual int find_text(const OUString& rStr) const override
{
int nPos = find_text_including_mru(rStr, false);
if (nPos != -1 && m_nMRUCount)
nPos -= (m_nMRUCount + 1);
return nPos;
}
virtual int find_id(const OUString& rId) const override
{
int nPos = find_id_including_mru(rId, false);
if (nPos != -1 && m_nMRUCount)
nPos -= (m_nMRUCount + 1);
return nPos;
}
virtual void clear() override
{
do_clear();
}
virtual void make_sorted() override
{
m_xSorter.reset(new comphelper::string::NaturalStringSorter(
::comphelper::getProcessComponentContext(),
Application::GetSettings().GetUILanguageTag().getLocale()));
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_set_sort_column_id(pSortable, m_nTextCol, GTK_SORT_ASCENDING);
gtk_tree_sortable_set_sort_func(pSortable, m_nTextCol, default_sort_func, m_xSorter.get(), nullptr);
}
virtual bool has_entry() const override
{
return gtk_combo_box_get_has_entry(m_pComboBox);
}
virtual void set_entry_message_type(weld::EntryMessageType eType) override
{
assert(m_pEntry);
::set_entry_message_type(GTK_ENTRY(m_pEntry), eType);
}
virtual void set_entry_text(const OUString& rText) override
{
assert(m_pEditable);
disable_notify_events();
gtk_editable_set_text(m_pEditable, OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr());
enable_notify_events();
}
virtual void set_entry_width_chars(int nChars) override
{
assert(m_pEditable);
disable_notify_events();
gtk_editable_set_width_chars(m_pEditable, nChars);
gtk_editable_set_max_width_chars(m_pEditable, nChars);
enable_notify_events();
}
virtual void set_entry_max_length(int nChars) override
{
assert(m_pEntry);
disable_notify_events();
gtk_entry_set_max_length(GTK_ENTRY(m_pEntry), nChars);
enable_notify_events();
}
virtual void select_entry_region(int nStartPos, int nEndPos) override
{
assert(m_pEditable);
disable_notify_events();
gtk_editable_select_region(m_pEditable, nStartPos, nEndPos);
enable_notify_events();
}
virtual bool get_entry_selection_bounds(int& rStartPos, int &rEndPos) override
{
assert(m_pEditable);
return gtk_editable_get_selection_bounds(m_pEditable, &rStartPos, &rEndPos);
}
virtual void set_entry_completion(bool bEnable, bool bCaseSensitive) override
{
m_bAutoComplete = bEnable;
m_bAutoCompleteCaseSensitive = bCaseSensitive;
}
virtual void set_entry_placeholder_text(const OUString& rText) override
{
assert(m_pEntry);
gtk_entry_set_placeholder_text(GTK_ENTRY(m_pEntry), rText.toUtf8().getStr());
}
virtual void set_entry_editable(bool bEditable) override
{
assert(m_pEditable);
gtk_editable_set_editable(m_pEditable, bEditable);
}
virtual void cut_entry_clipboard() override
{
assert(m_pEntry);
gtk_widget_activate_action(m_pEntry, "cut.clipboard", nullptr);
}
virtual void copy_entry_clipboard() override
{
assert(m_pEntry);
gtk_widget_activate_action(m_pEntry, "copy.clipboard", nullptr);
}
virtual void paste_entry_clipboard() override
{
assert(m_pEntry);
gtk_widget_activate_action(m_pEntry, "paste.clipboard", nullptr);
}
virtual void set_font(const vcl::Font& rFont) override
{
m_aCustomFont.use_custom_font(&rFont, u"combobox");
}
virtual vcl::Font get_font() override
{
if (const vcl::Font* pFont = m_aCustomFont.get_custom_font())
return *pFont;
return GtkInstanceWidget::get_font();
}
virtual void set_entry_font(const vcl::Font& rFont) override
{
m_xEntryFont = rFont;
assert(m_pEntry);
PangoAttrList* pOrigList = gtk_entry_get_attributes(GTK_ENTRY(m_pEntry));
PangoAttrList* pAttrList = pOrigList ? pango_attr_list_copy(pOrigList) : pango_attr_list_new();
update_attr_list(pAttrList, rFont);
gtk_entry_set_attributes(GTK_ENTRY(m_pEntry), pAttrList);
pango_attr_list_unref(pAttrList);
}
virtual vcl::Font get_entry_font() override
{
if (m_xEntryFont)
return *m_xEntryFont;
assert(m_pEntry);
PangoContext* pContext = gtk_widget_get_pango_context(m_pEntry);
return pango_to_vcl(pango_context_get_font_description(pContext),
Application::GetSettings().GetUILanguageTag().getLocale());
}
virtual void disable_notify_events() override
{
if (m_pEditable)
{
g_signal_handler_block(m_pEditable, m_nEntryInsertTextSignalId);
g_signal_handler_block(m_pEntry, m_nEntryActivateSignalId);
g_signal_handler_block(m_pEntryFocusController, m_nEntryFocusInSignalId);
g_signal_handler_block(m_pEntryFocusController, m_nEntryFocusOutSignalId);
g_signal_handler_block(m_pEntryKeyController, m_nEntryKeyPressEventSignalId);
}
else
g_signal_handler_block(m_pKeyController, m_nKeyPressEventSignalId);
// if (m_nToggleFocusInSignalId)
// g_signal_handler_block(m_pToggleButton, m_nToggleFocusInSignalId);
// if (m_nToggleFocusOutSignalId)
// g_signal_handler_block(m_pToggleButton, m_nToggleFocusOutSignalId);
// g_signal_handler_block(m_pTreeView, m_nRowActivatedSignalId);
g_signal_handler_block(m_pComboBox, m_nPopupShownSignalId);
g_signal_handler_block(m_pComboBox, m_nChangedSignalId);
GtkInstanceWidget::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceWidget::enable_notify_events();
g_signal_handler_unblock(m_pComboBox, m_nChangedSignalId);
g_signal_handler_unblock(m_pComboBox, m_nPopupShownSignalId);
// g_signal_handler_unblock(m_pTreeView, m_nRowActivatedSignalId);
// if (m_nToggleFocusInSignalId)
// g_signal_handler_unblock(m_pToggleButton, m_nToggleFocusInSignalId);
// if (m_nToggleFocusOutSignalId)
// g_signal_handler_unblock(m_pToggleButton, m_nToggleFocusOutSignalId);
if (m_pEditable)
{
g_signal_handler_unblock(m_pEntry, m_nEntryActivateSignalId);
g_signal_handler_unblock(m_pEntryFocusController, m_nEntryFocusInSignalId);
g_signal_handler_unblock(m_pEntryFocusController, m_nEntryFocusOutSignalId);
g_signal_handler_unblock(m_pEntryKeyController, m_nEntryKeyPressEventSignalId);
g_signal_handler_unblock(m_pEditable, m_nEntryInsertTextSignalId);
}
else
g_signal_handler_unblock(m_pKeyController, m_nKeyPressEventSignalId);
}
virtual void freeze() override
{
disable_notify_events();
bool bIsFirstFreeze = IsFirstFreeze();
GtkInstanceWidget::freeze();
if (bIsFirstFreeze)
{
g_object_ref(m_pTreeModel);
// gtk_tree_view_set_model(m_pTreeView, nullptr);
g_object_freeze_notify(G_OBJECT(m_pTreeModel));
if (m_xSorter)
{
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_set_sort_column_id(pSortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
}
}
enable_notify_events();
}
virtual void thaw() override
{
disable_notify_events();
if (IsLastThaw())
{
if (m_xSorter)
{
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_set_sort_column_id(pSortable, m_nTextCol, GTK_SORT_ASCENDING);
}
g_object_thaw_notify(G_OBJECT(m_pTreeModel));
// gtk_tree_view_set_model(m_pTreeView, m_pTreeModel);
g_object_unref(m_pTreeModel);
}
GtkInstanceWidget::thaw();
enable_notify_events();
}
virtual bool get_popup_shown() const override
{
return m_bPopupActive;
}
virtual void connect_focus_in(const Link<Widget&, void>& rLink) override
{
// if (!m_nToggleFocusInSignalId)
// m_nToggleFocusInSignalId = g_signal_connect_after(m_pToggleButton, "focus-in-event", G_CALLBACK(signalFocusIn), this);
GtkInstanceWidget::connect_focus_in(rLink);
}
virtual void connect_focus_out(const Link<Widget&, void>& rLink) override
{
// if (!m_nToggleFocusOutSignalId)
// m_nToggleFocusOutSignalId = g_signal_connect_after(m_pToggleButton, "focus-out-event", G_CALLBACK(signalFocusOut), this);
GtkInstanceWidget::connect_focus_out(rLink);
}
virtual void grab_focus() override
{
if (has_focus())
return;
if (m_pEntry)
gtk_widget_grab_focus(m_pEntry);
else
{
// gtk_widget_grab_focus(m_pToggleButton);
gtk_widget_grab_focus(GTK_WIDGET(m_pComboBox));
}
}
virtual bool has_focus() const override
{
if (m_pEntry && gtk_widget_has_focus(m_pEntry))
return true;
// if (gtk_widget_has_focus(m_pToggleButton))
// return true;
#if 0
if (gtk_widget_get_visible(GTK_WIDGET(m_pMenuWindow)))
{
if (gtk_widget_has_focus(GTK_WIDGET(m_pOverlayButton)) || gtk_widget_has_focus(GTK_WIDGET(m_pTreeView)))
return true;
}
#endif
return GtkInstanceWidget::has_focus();
}
virtual bool changed_by_direct_pick() const override
{
return m_bChangedByMenu;
}
virtual void set_custom_renderer(bool bOn) override
{
if (bOn == m_bCustomRenderer)
return;
#if 0
GList* pColumns = gtk_tree_view_get_columns(m_pTreeView);
// keep the original height around for optimal popup height calculation
m_nNonCustomLineHeight = bOn ? ::get_height_row(m_pTreeView, pColumns) : -1;
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pColumns->data);
gtk_cell_layout_clear(GTK_CELL_LAYOUT(pColumn));
if (bOn)
{
GtkCellRenderer *pRenderer = custom_cell_renderer_new();
GValue value = G_VALUE_INIT;
g_value_init(&value, G_TYPE_POINTER);
g_value_set_pointer(&value, static_cast<gpointer>(this));
g_object_set_property(G_OBJECT(pRenderer), "instance", &value);
gtk_tree_view_column_pack_start(pColumn, pRenderer, true);
gtk_tree_view_column_add_attribute(pColumn, pRenderer, "text", m_nTextCol);
gtk_tree_view_column_add_attribute(pColumn, pRenderer, "id", m_nIdCol);
}
else
{
GtkCellRenderer *pRenderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(pColumn, pRenderer, true);
gtk_tree_view_column_add_attribute(pColumn, pRenderer, "text", m_nTextCol);
}
g_list_free(pColumns);
m_bCustomRenderer = bOn;
#endif
}
void call_signal_custom_render(VirtualDevice& rOutput, const tools::Rectangle& rRect, bool bSelected, const OUString& rId)
{
signal_custom_render(rOutput, rRect, bSelected, rId);
}
Size call_signal_custom_get_size(VirtualDevice& rOutput)
{
return signal_custom_get_size(rOutput);
}
VclPtr<VirtualDevice> create_render_virtual_device() const override
{
return create_virtual_device();
}
virtual void set_item_menu(const OUString& rIdent, weld::Menu* pMenu) override
{
#if 0
m_xCustomMenuButtonHelper.reset();
GtkInstanceMenu* pPopoverWidget = dynamic_cast<GtkInstanceMenu*>(pMenu);
GtkWidget* pMenuWidget = GTK_WIDGET(pPopoverWidget ? pPopoverWidget->getMenu() : nullptr);
gtk_menu_button_set_popup(m_pOverlayButton, pMenuWidget);
gtk_widget_set_visible(GTK_WIDGET(m_pOverlayButton), pMenuWidget != nullptr);
gtk_widget_queue_resize_no_redraw(GTK_WIDGET(m_pOverlayButton)); // force location recalc
if (pMenuWidget)
m_xCustomMenuButtonHelper.reset(new CustomRenderMenuButtonHelper(GTK_MENU(pMenuWidget), GTK_TOGGLE_BUTTON(m_pToggleButton)));
m_sMenuButtonRow = rIdent;
#else
(void)rIdent; (void)pMenu;
#endif
}
OUString get_mru_entries() const override
{
const sal_Unicode cSep = ';';
OUStringBuffer aEntries;
for (sal_Int32 n = 0; n < m_nMRUCount; n++)
{
aEntries.append(get_text_including_mru(n));
if (n < m_nMRUCount - 1)
aEntries.append(cSep);
}
return aEntries.makeStringAndClear();
}
virtual void set_mru_entries(const OUString& rEntries) override
{
const sal_Unicode cSep = ';';
// Remove old MRU entries
for (sal_Int32 n = m_nMRUCount; n;)
remove_including_mru(--n);
sal_Int32 nMRUCount = 0;
sal_Int32 nIndex = 0;
do
{
OUString aEntry = rEntries.getToken(0, cSep, nIndex);
// Accept only existing entries
int nPos = find_text(aEntry);
if (nPos != -1)
{
OUString sId = get_id(nPos);
insert_including_mru(0, aEntry, &sId, nullptr, nullptr);
++nMRUCount;
}
}
while (nIndex >= 0);
if (nMRUCount && !m_nMRUCount)
insert_separator_including_mru(nMRUCount, "separator");
else if (!nMRUCount && m_nMRUCount)
remove_including_mru(m_nMRUCount); // remove separator
m_nMRUCount = nMRUCount;
}
int get_menu_button_width() const override
{
#if 0
bool bVisible = gtk_widget_get_visible(GTK_WIDGET(m_pOverlayButton));
if (!bVisible)
gtk_widget_set_visible(GTK_WIDGET(m_pOverlayButton), true);
gint nWidth;
gtk_widget_get_preferred_width(GTK_WIDGET(m_pOverlayButton), &nWidth, nullptr);
if (!bVisible)
gtk_widget_set_visible(GTK_WIDGET(m_pOverlayButton), false);
return nWidth;
#else
return 0;
#endif
}
virtual ~GtkInstanceComboBox() override
{
// m_xCustomMenuButtonHelper.reset();
do_clear();
if (m_nAutoCompleteIdleId)
g_source_remove(m_nAutoCompleteIdleId);
if (m_pEditable)
{
g_signal_handler_disconnect(m_pEditable, m_nEntryInsertTextSignalId);
g_signal_handler_disconnect(m_pEntry, m_nEntryActivateSignalId);
g_signal_handler_disconnect(m_pEntryFocusController, m_nEntryFocusInSignalId);
g_signal_handler_disconnect(m_pEntryFocusController, m_nEntryFocusOutSignalId);
g_signal_handler_disconnect(m_pEntryKeyController, m_nEntryKeyPressEventSignalId);
}
else
g_signal_handler_disconnect(m_pKeyController, m_nKeyPressEventSignalId);
// if (m_nToggleFocusInSignalId)
// g_signal_handler_disconnect(m_pToggleButton, m_nToggleFocusInSignalId);
// if (m_nToggleFocusOutSignalId)
// g_signal_handler_disconnect(m_pToggleButton, m_nToggleFocusOutSignalId);
// g_signal_handler_disconnect(m_pTreeView, m_nRowActivatedSignalId);
g_signal_handler_disconnect(m_pComboBox, m_nPopupShownSignalId);
g_signal_handler_disconnect(m_pComboBox, m_nChangedSignalId);
// gtk_tree_view_set_model(m_pTreeView, nullptr);
}
};
#else
class GtkInstanceComboBox : public GtkInstanceContainer, public vcl::ISearchableStringList, public virtual weld::ComboBox
{
private:
GtkBuilder* m_pComboBuilder;
GtkComboBox* m_pComboBox;
GtkOverlay* m_pOverlay;
GtkTreeView* m_pTreeView;
GtkMenuButton* m_pOverlayButton; // button that the StyleDropdown uses on an active row
GtkWindow* m_pMenuWindow;
GtkTreeModel* m_pTreeModel;
GtkCellRenderer* m_pButtonTextRenderer;
GtkCellRenderer* m_pMenuTextRenderer;
GtkWidget* m_pToggleButton;
GtkWidget* m_pEntry;
GtkCellView* m_pCellView;
WidgetFont m_aCustomFont;
std::unique_ptr<CustomRenderMenuButtonHelper> m_xCustomMenuButtonHelper;
std::optional<vcl::Font> m_xEntryFont;
std::unique_ptr<comphelper::string::NaturalStringSorter> m_xSorter;
vcl::QuickSelectionEngine m_aQuickSelectionEngine;
std::vector<std::unique_ptr<GtkTreeRowReference, GtkTreeRowReferenceDeleter>> m_aSeparatorRows;
OUString m_sMenuButtonRow;
bool m_bHoverSelection;
bool m_bMouseInOverlayButton;
bool m_bPopupActive;
bool m_bAutoComplete;
bool m_bAutoCompleteCaseSensitive;
bool m_bChangedByMenu;
bool m_bCustomRenderer;
bool m_bActivateCalled;
gint m_nTextCol;
gint m_nIdCol;
gulong m_nToggleFocusInSignalId;
gulong m_nToggleFocusOutSignalId;
gulong m_nRowActivatedSignalId;
gulong m_nChangedSignalId;
gulong m_nPopupShownSignalId;
gulong m_nKeyPressEventSignalId;
gulong m_nEntryInsertTextSignalId;
gulong m_nEntryActivateSignalId;
gulong m_nEntryFocusInSignalId;
gulong m_nEntryFocusOutSignalId;
gulong m_nEntryKeyPressEventSignalId;
guint m_nAutoCompleteIdleId;
gint m_nNonCustomLineHeight;
gint m_nPrePopupCursorPos;
int m_nMRUCount;
int m_nMaxMRUCount;
static gboolean idleAutoComplete(gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->auto_complete();
return false;
}
void auto_complete()
{
m_nAutoCompleteIdleId = 0;
OUString aStartText = get_active_text();
int nStartPos, nEndPos;
get_entry_selection_bounds(nStartPos, nEndPos);
int nMaxSelection = std::max(nStartPos, nEndPos);
if (nMaxSelection != aStartText.getLength())
return;
disable_notify_events();
int nActive = get_active();
int nStart = nActive;
if (nStart == -1)
nStart = 0;
int nPos = -1;
int nZeroRow = 0;
if (m_nMRUCount)
nZeroRow += (m_nMRUCount + 1);
if (!m_bAutoCompleteCaseSensitive)
{
// Try match case insensitive from current position
nPos = starts_with(m_pTreeModel, aStartText, 0, nStart, false);
if (nPos == -1 && nStart != 0)
{
// Try match case insensitive, but from start
nPos = starts_with(m_pTreeModel, aStartText, 0, nZeroRow, false);
}
}
if (nPos == -1)
{
// Try match case sensitive from current position
nPos = starts_with(m_pTreeModel, aStartText, 0, nStart, true);
if (nPos == -1 && nStart != 0)
{
// Try match case sensitive, but from start
nPos = starts_with(m_pTreeModel, aStartText, 0, nZeroRow, true);
}
}
if (nPos != -1)
{
OUString aText = get_text_including_mru(nPos);
if (aText != aStartText)
{
SolarMutexGuard aGuard;
set_active_including_mru(nPos, true);
}
select_entry_region(aText.getLength(), aStartText.getLength());
}
enable_notify_events();
}
static void signalEntryInsertText(GtkEntry* pEntry, const gchar* pNewText, gint nNewTextLength,
gint* position, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
SolarMutexGuard aGuard;
pThis->signal_entry_insert_text(pEntry, pNewText, nNewTextLength, position);
}
void signal_entry_insert_text(GtkEntry* pEntry, const gchar* pNewText, gint nNewTextLength, gint* position)
{
// first filter inserted text
if (m_aEntryInsertTextHdl.IsSet())
{
OUString sText(pNewText, nNewTextLength, RTL_TEXTENCODING_UTF8);
const bool bContinue = m_aEntryInsertTextHdl.Call(sText);
if (bContinue && !sText.isEmpty())
{
OString sFinalText(OUStringToOString(sText, RTL_TEXTENCODING_UTF8));
g_signal_handlers_block_by_func(pEntry, reinterpret_cast<gpointer>(signalEntryInsertText), this);
gtk_editable_insert_text(GTK_EDITABLE(pEntry), sFinalText.getStr(), sFinalText.getLength(), position);
g_signal_handlers_unblock_by_func(pEntry, reinterpret_cast<gpointer>(signalEntryInsertText), this);
}
g_signal_stop_emission_by_name(pEntry, "insert-text");
}
if (m_bAutoComplete)
{
// now check for autocompletes
if (m_nAutoCompleteIdleId)
g_source_remove(m_nAutoCompleteIdleId);
m_nAutoCompleteIdleId = g_idle_add(idleAutoComplete, this);
}
}
static void signalChanged(GtkEntry*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
SolarMutexGuard aGuard;
pThis->fire_signal_changed();
}
void fire_signal_changed()
{
signal_changed();
m_bChangedByMenu = false;
}
static void signalPopupToggled(GtkToggleButton* /*pToggleButton*/, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_popup_toggled();
}
int get_popup_height(gint& rPopupWidth)
{
const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
int nMaxRows = rSettings.GetListBoxMaximumLineCount();
bool bAddScrollWidth = false;
int nRows = get_count_including_mru();
if (nMaxRows < nRows)
{
nRows = nMaxRows;
bAddScrollWidth = true;
}
GList* pColumns = gtk_tree_view_get_columns(m_pTreeView);
gint nRowHeight = get_height_row(m_pTreeView, pColumns);
g_list_free(pColumns);
gint nSeparatorHeight = get_height_row_separator(m_pTreeView);
gint nHeight = get_height_rows(nRowHeight, nSeparatorHeight, nRows);
// if we're using a custom renderer, limit the height to the height nMaxRows would be
// for a normal renderer, and then round down to how many custom rows fit in that
// space
if (m_nNonCustomLineHeight != -1 && nRowHeight)
{
gint nNormalHeight = get_height_rows(m_nNonCustomLineHeight, nSeparatorHeight, nMaxRows);
if (nHeight > nNormalHeight)
{
gint nRowsOnly = nNormalHeight - get_height_rows(0, nSeparatorHeight, nMaxRows);
gint nCustomRows = (nRowsOnly + (nRowHeight - 1)) / nRowHeight;
nHeight = get_height_rows(nRowHeight, nSeparatorHeight, nCustomRows);
}
}
if (bAddScrollWidth)
rPopupWidth += rSettings.GetScrollBarSize();
return nHeight;
}
void menu_toggled()
{
if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_pToggleButton)))
{
if (m_bHoverSelection)
{
// turn hover selection back off until mouse is moved again
// *after* menu is shown again
gtk_tree_view_set_hover_selection(m_pTreeView, false);
m_bHoverSelection = false;
}
bool bHadFocus = gtk_window_has_toplevel_focus(m_pMenuWindow);
do_ungrab(GTK_WIDGET(m_pMenuWindow));
gtk_widget_hide(GTK_WIDGET(m_pMenuWindow));
GdkSurface* pSurface = widget_get_surface(GTK_WIDGET(m_pMenuWindow));
g_object_set_data(G_OBJECT(pSurface), "g-lo-InstancePopup", GINT_TO_POINTER(false));
// so gdk_window_move_to_rect will work again the next time
gtk_widget_unrealize(GTK_WIDGET(m_pMenuWindow));
gtk_widget_set_size_request(GTK_WIDGET(m_pMenuWindow), -1, -1);
if (!m_bActivateCalled)
tree_view_set_cursor(m_nPrePopupCursorPos);
// undo show_menu tooltip blocking
GtkWidget* pParent = widget_get_toplevel(m_pToggleButton);
GtkSalFrame* pFrame = pParent ? GtkSalFrame::getFromWindow(pParent) : nullptr;
if (pFrame)
pFrame->UnblockTooltip();
if (bHadFocus)
{
GdkSurface* pParentSurface = pParent ? widget_get_surface(pParent) : nullptr;
void* pParentIsPopover = pParentSurface ? g_object_get_data(G_OBJECT(pParentSurface), "g-lo-InstancePopup") : nullptr;
if (pParentIsPopover)
do_grab(m_pToggleButton);
gtk_widget_grab_focus(m_pToggleButton);
}
}
else
{
GtkWidget* pComboBox = GTK_WIDGET(getContainer());
gint nComboWidth = gtk_widget_get_allocated_width(pComboBox);
GtkRequisition size;
gtk_widget_get_preferred_size(GTK_WIDGET(m_pMenuWindow), nullptr, &size);
gint nPopupWidth = size.width;
gint nPopupHeight = get_popup_height(nPopupWidth);
nPopupWidth = std::max(nPopupWidth, nComboWidth);
gtk_widget_set_size_request(GTK_WIDGET(m_pMenuWindow), nPopupWidth, nPopupHeight);
m_nPrePopupCursorPos = get_active();
m_bActivateCalled = false;
// if we are in mru mode always start with the cursor at the top of the menu
if (m_nMaxMRUCount)
tree_view_set_cursor(0);
GdkRectangle aAnchor {0, 0, gtk_widget_get_allocated_width(pComboBox), gtk_widget_get_allocated_height(pComboBox) };
show_menu(pComboBox, m_pMenuWindow, aAnchor, weld::Placement::Under, true);
GdkSurface* pSurface = widget_get_surface(GTK_WIDGET(m_pMenuWindow));
g_object_set_data(G_OBJECT(pSurface), "g-lo-InstancePopup", GINT_TO_POINTER(true));
}
}
virtual void signal_popup_toggled() override
{
m_aQuickSelectionEngine.Reset();
menu_toggled();
bool bIsShown = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_pToggleButton));
if (m_bPopupActive == bIsShown)
return;
m_bPopupActive = bIsShown;
ComboBox::signal_popup_toggled();
if (!m_bPopupActive && m_pEntry)
{
disable_notify_events();
//restore focus to the GtkEntry when the popup is gone, which
//is what the vcl case does, to ease the transition a little
gtk_widget_grab_focus(m_pEntry);
enable_notify_events();
}
}
static gboolean signalEntryFocusIn(GtkWidget*, GdkEvent*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_entry_focus_in();
return false;
}
void signal_entry_focus_in()
{
signal_focus_in();
}
static gboolean signalEntryFocusOut(GtkWidget*, GdkEvent*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_entry_focus_out();
return false;
}
void signal_entry_focus_out()
{
// if we have an untidy selection on losing focus remove the selection
int nStartPos, nEndPos;
if (get_entry_selection_bounds(nStartPos, nEndPos))
{
int nMin = std::min(nStartPos, nEndPos);
int nMax = std::max(nStartPos, nEndPos);
if (nMin != 0 || nMax != get_active_text().getLength())
select_entry_region(0, 0);
}
signal_focus_out();
}
static void signalEntryActivate(GtkEntry*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_entry_activate();
}
void signal_entry_activate()
{
if (m_aEntryActivateHdl.IsSet())
{
SolarMutexGuard aGuard;
if (m_aEntryActivateHdl.Call(*this))
g_signal_stop_emission_by_name(m_pEntry, "activate");
}
update_mru();
}
OUString get(int pos, int col) const
{
OUString sRet;
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
{
gchar* pStr;
gtk_tree_model_get(m_pTreeModel, &iter, col, &pStr, -1);
sRet = OUString(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
g_free(pStr);
}
return sRet;
}
void set(int pos, int col, std::u16string_view rText)
{
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos))
{
OString aStr(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
gtk_list_store_set(GTK_LIST_STORE(m_pTreeModel), &iter, col, aStr.getStr(), -1);
}
}
int find(std::u16string_view rStr, int col, bool bSearchMRUArea) const
{
GtkTreeIter iter;
if (!gtk_tree_model_get_iter_first(m_pTreeModel, &iter))
return -1;
int nRet = 0;
if (!bSearchMRUArea && m_nMRUCount)
{
if (!gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, m_nMRUCount + 1))
return -1;
nRet += (m_nMRUCount + 1);
}
OString aStr(OUStringToOString(rStr, RTL_TEXTENCODING_UTF8));
do
{
gchar* pStr;
gtk_tree_model_get(m_pTreeModel, &iter, col, &pStr, -1);
const bool bEqual = g_strcmp0(pStr, aStr.getStr()) == 0;
g_free(pStr);
if (bEqual)
return nRet;
++nRet;
} while (gtk_tree_model_iter_next(m_pTreeModel, &iter));
return -1;
}
bool separator_function(const GtkTreePath* path)
{
return ::separator_function(path, m_aSeparatorRows);
}
bool separator_function(int pos)
{
GtkTreePath* path = gtk_tree_path_new_from_indices(pos, -1);
bool bRet = separator_function(path);
gtk_tree_path_free(path);
return bRet;
}
static gboolean separatorFunction(GtkTreeModel* pTreeModel, GtkTreeIter* pIter, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
GtkTreePath* path = gtk_tree_model_get_path(pTreeModel, pIter);
bool bRet = pThis->separator_function(path);
gtk_tree_path_free(path);
return bRet;
}
// https://gitlab.gnome.org/GNOME/gtk/issues/310
//
// in the absence of a built-in solution
// a) support typeahead for the case where there is no entry widget, typing ahead
// into the button itself will select via the vcl selection engine, a matching
// entry
static gboolean signalKeyPress(GtkWidget*, GdkEventKey* pEvent, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
return pThis->signal_key_press(pEvent);
}
// tdf#131076 we want return in a ComboBox to act like return in a
// GtkEntry and activate the default dialog/assistant button
bool combobox_activate()
{
GtkWidget *pComboBox = GTK_WIDGET(m_pToggleButton);
GtkWidget *pToplevel = widget_get_toplevel(pComboBox);
GtkWindow *pWindow = GTK_WINDOW(pToplevel);
if (!pWindow)
return false;
if (!GTK_IS_DIALOG(pWindow) && !GTK_IS_ASSISTANT(pWindow))
return false;
bool bDone = false;
GtkWidget *pDefaultWidget = gtk_window_get_default_widget(pWindow);
if (pDefaultWidget && pDefaultWidget != m_pToggleButton && gtk_widget_get_sensitive(pDefaultWidget))
bDone = gtk_widget_activate(pDefaultWidget);
return bDone;
}
static gboolean signalEntryKeyPress(GtkEntry* pEntry, GdkEventKey* pEvent, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
LocalizeDecimalSeparator(pEvent->keyval);
if (signalEntryInsertSpecialCharKeyPress(pEntry, pEvent, nullptr))
return true;
return pThis->signal_entry_key_press(pEvent);
}
bool signal_entry_key_press(const GdkEventKey* pEvent)
{
KeyEvent aKEvt(GtkToVcl(*pEvent));
vcl::KeyCode aKeyCode = aKEvt.GetKeyCode();
bool bDone = false;
auto nCode = aKeyCode.GetCode();
switch (nCode)
{
case KEY_DOWN:
{
sal_uInt16 nKeyMod = aKeyCode.GetModifier();
if (!nKeyMod)
{
int nCount = get_count_including_mru();
int nActive = get_active_including_mru() + 1;
while (nActive < nCount && separator_function(nActive))
++nActive;
if (nActive < nCount)
set_active_including_mru(nActive, true);
bDone = true;
}
else if (nKeyMod == KEY_MOD2 && !m_bPopupActive)
{
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pToggleButton), true);
bDone = true;
}
break;
}
case KEY_UP:
{
sal_uInt16 nKeyMod = aKeyCode.GetModifier();
if (!nKeyMod)
{
int nStartBound = m_bPopupActive || !m_nMRUCount ? 0 : (m_nMRUCount + 1);
int nActive = get_active_including_mru() - 1;
while (nActive >= nStartBound && separator_function(nActive))
--nActive;
if (nActive >= nStartBound)
set_active_including_mru(nActive, true);
bDone = true;
}
break;
}
case KEY_PAGEUP:
{
sal_uInt16 nKeyMod = aKeyCode.GetModifier();
if (!nKeyMod)
{
int nCount = get_count_including_mru();
int nStartBound = m_bPopupActive || !m_nMaxMRUCount ? 0 : (m_nMRUCount + 1);
int nActive = nStartBound;
while (nActive < nCount && separator_function(nActive))
++nActive;
if (nActive < nCount)
set_active_including_mru(nActive, true);
bDone = true;
}
break;
}
case KEY_PAGEDOWN:
{
sal_uInt16 nKeyMod = aKeyCode.GetModifier();
if (!nKeyMod)
{
int nActive = get_count_including_mru() - 1;
int nStartBound = m_bPopupActive ? 0 : (m_nMRUCount + 1);
while (nActive >= nStartBound && separator_function(nActive))
--nActive;
if (nActive >= nStartBound)
set_active_including_mru(nActive, true);
bDone = true;
}
break;
}
default:
break;
}
return bDone;
}
bool signal_key_press(const GdkEventKey* pEvent)
{
if (m_bHoverSelection)
{
// once a key is pressed, turn off hover selection until mouse is
// moved again otherwise when the treeview scrolls it jumps to the
// position under the mouse.
gtk_tree_view_set_hover_selection(m_pTreeView, false);
m_bHoverSelection = false;
}
KeyEvent aKEvt(GtkToVcl(*pEvent));
vcl::KeyCode aKeyCode = aKEvt.GetKeyCode();
bool bDone = false;
auto nCode = aKeyCode.GetCode();
switch (nCode)
{
case KEY_DOWN:
case KEY_UP:
case KEY_PAGEUP:
case KEY_PAGEDOWN:
case KEY_HOME:
case KEY_END:
case KEY_LEFT:
case KEY_RIGHT:
case KEY_RETURN:
{
m_aQuickSelectionEngine.Reset();
sal_uInt16 nKeyMod = aKeyCode.GetModifier();
// tdf#131076 don't let bare return toggle menu popup active, but do allow deactivate
if (nCode == KEY_RETURN && !nKeyMod && !m_bPopupActive)
bDone = combobox_activate();
else if (nCode == KEY_UP && nKeyMod == KEY_MOD2 && m_bPopupActive)
{
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pToggleButton), false);
bDone = true;
}
else if (nCode == KEY_DOWN && nKeyMod == KEY_MOD2 && !m_bPopupActive)
{
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pToggleButton), true);
bDone = true;
}
break;
}
case KEY_ESCAPE:
{
m_aQuickSelectionEngine.Reset();
if (m_bPopupActive)
{
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pToggleButton), false);
bDone = true;
}
break;
}
default:
// tdf#131076 let base space toggle menu popup when it's not already visible
if (nCode == KEY_SPACE && !aKeyCode.GetModifier() && !m_bPopupActive)
bDone = false;
else
bDone = m_aQuickSelectionEngine.HandleKeyEvent(aKEvt);
break;
}
if (!bDone && !m_pEntry)
bDone = signal_entry_key_press(pEvent);
return bDone;
}
vcl::StringEntryIdentifier typeahead_getEntry(int nPos, OUString& out_entryText) const
{
int nEntryCount(get_count_including_mru());
if (nPos >= nEntryCount)
nPos = 0;
out_entryText = get_text_including_mru(nPos);
// vcl::StringEntryIdentifier does not allow for 0 values, but our position is 0-based
// => normalize
return reinterpret_cast<vcl::StringEntryIdentifier>(nPos + 1);
}
static int typeahead_getEntryPos(vcl::StringEntryIdentifier entry)
{
// our pos is 0-based, but StringEntryIdentifier does not allow for a NULL
return reinterpret_cast<sal_Int64>(entry) - 1;
}
void tree_view_set_cursor(int pos)
{
GtkTreePath* path;
if (pos == -1)
{
path = gtk_tree_path_new_from_indices(G_MAXINT, -1);
gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(m_pTreeView));
if (m_pCellView)
gtk_cell_view_set_displayed_row(m_pCellView, nullptr);
}
else
{
path = gtk_tree_path_new_from_indices(pos, -1);
if (gtk_tree_view_get_model(m_pTreeView))
gtk_tree_view_scroll_to_cell(m_pTreeView, path, nullptr, false, 0, 0);
if (m_pCellView)
gtk_cell_view_set_displayed_row(m_pCellView, path);
}
gtk_tree_view_set_cursor(m_pTreeView, path, nullptr, false);
gtk_tree_path_free(path);
}
int tree_view_get_cursor() const
{
int nRet = -1;
GtkTreePath* path;
gtk_tree_view_get_cursor(m_pTreeView, &path, nullptr);
if (path)
{
gint depth;
gint* indices = gtk_tree_path_get_indices_with_depth(path, &depth);
nRet = indices[depth-1];
gtk_tree_path_free(path);
}
return nRet;
}
int get_selected_entry() const
{
if (m_bPopupActive)
return tree_view_get_cursor();
else
return get_active_including_mru();
}
void set_typeahead_selected_entry(int nSelect)
{
if (m_bPopupActive)
tree_view_set_cursor(nSelect);
else
set_active_including_mru(nSelect, true);
}
virtual vcl::StringEntryIdentifier CurrentEntry(OUString& out_entryText) const override
{
int nCurrentPos = get_selected_entry();
return typeahead_getEntry((nCurrentPos == -1) ? 0 : nCurrentPos, out_entryText);
}
virtual vcl::StringEntryIdentifier NextEntry(vcl::StringEntryIdentifier currentEntry, OUString& out_entryText) const override
{
int nNextPos = typeahead_getEntryPos(currentEntry) + 1;
return typeahead_getEntry(nNextPos, out_entryText);
}
virtual void SelectEntry(vcl::StringEntryIdentifier entry) override
{
int nSelect = typeahead_getEntryPos(entry);
if (nSelect == get_selected_entry())
{
// ignore that. This method is a callback from the QuickSelectionEngine, which means the user attempted
// to select the given entry by typing its starting letters. No need to act.
return;
}
// normalize
int nCount = get_count_including_mru();
if (nSelect >= nCount)
nSelect = nCount ? nCount-1 : -1;
set_typeahead_selected_entry(nSelect);
}
static void signalGrabBroken(GtkWidget*, GdkEventGrabBroken *pEvent, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->grab_broken(pEvent);
}
void grab_broken(const GdkEventGrabBroken *event)
{
if (event->grab_window == nullptr)
{
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pToggleButton), false);
}
else if (!g_object_get_data(G_OBJECT(event->grab_window), "g-lo-InstancePopup")) // another LibreOffice popover took a grab
{
//try and regrab, so when we lose the grab to the menu of the color palette
//combobox we regain it so the color palette doesn't itself disappear on next
//click on the color palette combobox
do_grab(GTK_WIDGET(m_pMenuWindow));
}
}
static gboolean signalButtonPress(GtkWidget*, GdkEventButton* pEvent, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
return pThis->button_press(pEvent);
}
bool button_press(GdkEventButton* pEvent)
{
//we want to pop down if the button was pressed outside our popup
if (button_event_is_outside(GTK_WIDGET(m_pMenuWindow), pEvent))
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pToggleButton), false);
return false;
}
static gboolean signalMotion(GtkWidget*, GdkEventMotion*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_motion();
return false;
}
void signal_motion()
{
// if hover-selection was disabled after pressing a key, then turn it back on again
if (!m_bHoverSelection && !m_bMouseInOverlayButton)
{
gtk_tree_view_set_hover_selection(m_pTreeView, true);
m_bHoverSelection = true;
}
}
static void signalRowActivated(GtkTreeView*, GtkTreePath*, GtkTreeViewColumn*, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->handle_row_activated();
}
void handle_row_activated()
{
m_bActivateCalled = true;
m_bChangedByMenu = true;
disable_notify_events();
int nActive = get_active();
if (m_pEntry)
gtk_entry_set_text(GTK_ENTRY(m_pEntry), OUStringToOString(get_text(nActive), RTL_TEXTENCODING_UTF8).getStr());
else
tree_view_set_cursor(nActive);
enable_notify_events();
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_pToggleButton), false);
fire_signal_changed();
update_mru();
}
void do_clear()
{
disable_notify_events();
gtk_tree_view_set_row_separator_func(m_pTreeView, nullptr, nullptr, nullptr);
m_aSeparatorRows.clear();
gtk_list_store_clear(GTK_LIST_STORE(m_pTreeModel));
m_nMRUCount = 0;
enable_notify_events();
}
virtual int get_max_mru_count() const override
{
return m_nMaxMRUCount;
}
virtual void set_max_mru_count(int nMaxMRUCount) override
{
m_nMaxMRUCount = nMaxMRUCount;
update_mru();
}
void update_mru()
{
int nMRUCount = m_nMRUCount;
if (m_nMaxMRUCount)
{
OUString sActiveText = get_active_text();
OUString sActiveId = get_active_id();
insert_including_mru(0, sActiveText, &sActiveId, nullptr, nullptr);
++m_nMRUCount;
for (int i = 1; i < m_nMRUCount - 1; ++i)
{
if (get_text_including_mru(i) == sActiveText)
{
remove_including_mru(i);
--m_nMRUCount;
break;
}
}
}
while (m_nMRUCount > m_nMaxMRUCount)
{
remove_including_mru(m_nMRUCount - 1);
--m_nMRUCount;
}
if (m_nMRUCount && !nMRUCount)
insert_separator_including_mru(m_nMRUCount, "separator");
else if (!m_nMRUCount && nMRUCount)
remove_including_mru(m_nMRUCount); // remove separator
}
int get_count_including_mru() const
{
return gtk_tree_model_iter_n_children(m_pTreeModel, nullptr);
}
int get_active_including_mru() const
{
return tree_view_get_cursor();
}
void set_active_including_mru(int pos, bool bInteractive)
{
assert(gtk_tree_view_get_model(m_pTreeView) && "don't set_active when frozen, set_active after thaw. Note selection doesn't survive a freeze");
disable_notify_events();
tree_view_set_cursor(pos);
if (m_pEntry)
{
if (pos != -1)
gtk_entry_set_text(GTK_ENTRY(m_pEntry), OUStringToOString(get_text_including_mru(pos), RTL_TEXTENCODING_UTF8).getStr());
else
gtk_entry_set_text(GTK_ENTRY(m_pEntry), "");
}
m_bChangedByMenu = false;
enable_notify_events();
if (bInteractive && !m_bPopupActive)
signal_changed();
}
int find_text_including_mru(std::u16string_view rStr, bool bSearchMRU) const
{
return find(rStr, m_nTextCol, bSearchMRU);
}
int find_id_including_mru(std::u16string_view rId, bool bSearchMRU) const
{
return find(rId, m_nIdCol, bSearchMRU);
}
OUString get_text_including_mru(int pos) const
{
return get(pos, m_nTextCol);
}
OUString get_id_including_mru(int pos) const
{
return get(pos, m_nIdCol);
}
void set_id_including_mru(int pos, std::u16string_view rId)
{
set(pos, m_nIdCol, rId);
}
void remove_including_mru(int pos)
{
disable_notify_events();
GtkTreeIter iter;
gtk_tree_model_iter_nth_child(m_pTreeModel, &iter, nullptr, pos);
if (!m_aSeparatorRows.empty())
{
bool bFound = false;
GtkTreePath* pPath = gtk_tree_path_new_from_indices(pos, -1);
for (auto aIter = m_aSeparatorRows.begin(); aIter != m_aSeparatorRows.end(); ++aIter)
{
GtkTreePath* seppath = gtk_tree_row_reference_get_path(aIter->get());
if (seppath)
{
if (gtk_tree_path_compare(pPath, seppath) == 0)
bFound = true;
gtk_tree_path_free(seppath);
}
if (bFound)
{
m_aSeparatorRows.erase(aIter);
break;
}
}
gtk_tree_path_free(pPath);
}
gtk_list_store_remove(GTK_LIST_STORE(m_pTreeModel), &iter);
enable_notify_events();
}
void insert_separator_including_mru(int pos, const OUString& rId)
{
disable_notify_events();
GtkTreeIter iter;
if (!gtk_tree_view_get_row_separator_func(m_pTreeView))
gtk_tree_view_set_row_separator_func(m_pTreeView, separatorFunction, this, nullptr);
insert_row(GTK_LIST_STORE(m_pTreeModel), iter, pos, &rId, u"", nullptr, nullptr);
GtkTreePath* pPath = gtk_tree_path_new_from_indices(pos, -1);
m_aSeparatorRows.emplace_back(gtk_tree_row_reference_new(m_pTreeModel, pPath));
gtk_tree_path_free(pPath);
enable_notify_events();
}
void insert_including_mru(int pos, std::u16string_view rText, const OUString* pId, const OUString* pIconName, const VirtualDevice* pImageSurface)
{
disable_notify_events();
GtkTreeIter iter;
insert_row(GTK_LIST_STORE(m_pTreeModel), iter, pos, pId, rText, pIconName, pImageSurface);
enable_notify_events();
}
static gboolean signalGetChildPosition(GtkOverlay*, GtkWidget*, GdkRectangle* pAllocation, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
return pThis->signal_get_child_position(pAllocation);
}
bool signal_get_child_position(GdkRectangle* pAllocation)
{
if (!gtk_widget_get_visible(GTK_WIDGET(m_pOverlayButton)))
return false;
if (!gtk_widget_get_realized(GTK_WIDGET(m_pTreeView)))
return false;
int nRow = find_id_including_mru(m_sMenuButtonRow, true);
if (nRow == -1)
return false;
gtk_widget_get_preferred_width(GTK_WIDGET(m_pOverlayButton), &pAllocation->width, nullptr);
GtkTreePath* pPath = gtk_tree_path_new_from_indices(nRow, -1);
GList* pColumns = gtk_tree_view_get_columns(m_pTreeView);
tools::Rectangle aRect = get_row_area(m_pTreeView, pColumns, pPath);
gtk_tree_path_free(pPath);
g_list_free(pColumns);
pAllocation->x = aRect.Right() - pAllocation->width;
pAllocation->y = aRect.Top();
pAllocation->height = aRect.GetHeight();
return true;
}
static gboolean signalOverlayButtonCrossing(GtkWidget*, GdkEventCrossing* pEvent, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_overlay_button_crossing(pEvent->type == GDK_ENTER_NOTIFY);
return false;
}
void signal_overlay_button_crossing(bool bEnter)
{
m_bMouseInOverlayButton = bEnter;
if (!bEnter)
return;
if (m_bHoverSelection)
{
// once toggled button is pressed, turn off hover selection until
// mouse leaves the overlay button
gtk_tree_view_set_hover_selection(m_pTreeView, false);
m_bHoverSelection = false;
}
int nRow = find_id_including_mru(m_sMenuButtonRow, true);
assert(nRow != -1);
tree_view_set_cursor(nRow); // select the buttons row
}
void signal_combo_mnemonic_activate()
{
if (m_pEntry)
gtk_widget_grab_focus(m_pEntry);
else
gtk_widget_grab_focus(m_pToggleButton);
}
static gboolean signalComboMnemonicActivate(GtkWidget*, gboolean, gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
pThis->signal_combo_mnemonic_activate();
return true;
}
static gboolean signalComboTooltipQuery(GtkWidget* /*pWidget*/, gint x, gint y,
gboolean keyboard_mode, GtkTooltip *tooltip,
gpointer widget)
{
GtkInstanceComboBox* pThis = static_cast<GtkInstanceComboBox*>(widget);
return signalTooltipQuery(GTK_WIDGET(pThis->m_pComboBox), x, y, keyboard_mode, tooltip);
}
int include_mru(int pos)
{
if (m_nMRUCount && pos != -1)
pos += (m_nMRUCount + 1);
return pos;
}
public:
GtkInstanceComboBox(GtkBuilder* pComboBuilder, GtkComboBox* pComboBox, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceContainer(GTK_CONTAINER(gtk_builder_get_object(pComboBuilder, "box")), pBuilder, bTakeOwnership)
, m_pComboBuilder(pComboBuilder)
, m_pComboBox(pComboBox)
, m_pOverlay(GTK_OVERLAY(gtk_builder_get_object(pComboBuilder, "overlay")))
, m_pTreeView(GTK_TREE_VIEW(gtk_builder_get_object(pComboBuilder, "treeview")))
, m_pOverlayButton(GTK_MENU_BUTTON(gtk_builder_get_object(pComboBuilder, "overlaybutton")))
, m_pMenuWindow(GTK_WINDOW(gtk_builder_get_object(pComboBuilder, "popup")))
, m_pTreeModel(gtk_combo_box_get_model(pComboBox))
, m_pButtonTextRenderer(nullptr)
, m_pToggleButton(GTK_WIDGET(gtk_builder_get_object(pComboBuilder, "button")))
, m_pEntry(GTK_WIDGET(gtk_builder_get_object(pComboBuilder, "entry")))
, m_pCellView(nullptr)
, m_aCustomFont(m_pWidget)
, m_aQuickSelectionEngine(*this)
, m_bHoverSelection(false)
, m_bMouseInOverlayButton(false)
, m_bPopupActive(false)
, m_bAutoComplete(false)
, m_bAutoCompleteCaseSensitive(false)
, m_bChangedByMenu(false)
, m_bCustomRenderer(false)
, m_bActivateCalled(false)
, m_nTextCol(gtk_combo_box_get_entry_text_column(pComboBox))
, m_nIdCol(gtk_combo_box_get_id_column(pComboBox))
, m_nToggleFocusInSignalId(0)
, m_nToggleFocusOutSignalId(0)
, m_nRowActivatedSignalId(g_signal_connect(m_pTreeView, "row-activated", G_CALLBACK(signalRowActivated), this))
, m_nChangedSignalId(g_signal_connect(m_pEntry, "changed", G_CALLBACK(signalChanged), this))
, m_nPopupShownSignalId(g_signal_connect(m_pToggleButton, "toggled", G_CALLBACK(signalPopupToggled), this))
, m_nAutoCompleteIdleId(0)
, m_nNonCustomLineHeight(-1)
, m_nPrePopupCursorPos(-1)
, m_nMRUCount(0)
, m_nMaxMRUCount(0)
{
int nActive = gtk_combo_box_get_active(m_pComboBox);
if (gtk_style_context_has_class(gtk_widget_get_style_context(GTK_WIDGET(m_pComboBox)), "small-button"))
gtk_style_context_add_class(gtk_widget_get_style_context(GTK_WIDGET(getContainer())), "small-button");
if (gtk_widget_get_has_tooltip(GTK_WIDGET(m_pComboBox)))
{
gtk_widget_set_has_tooltip(GTK_WIDGET(getContainer()), true);
g_signal_connect(getContainer(), "query-tooltip", G_CALLBACK(signalComboTooltipQuery), this);
}
insertAsParent(GTK_WIDGET(m_pComboBox), GTK_WIDGET(getContainer()));
gtk_widget_set_visible(GTK_WIDGET(m_pComboBox), false);
gtk_widget_set_no_show_all(GTK_WIDGET(m_pComboBox), true);
gtk_tree_view_set_model(m_pTreeView, m_pTreeModel);
/* tdf#136455 gtk_combo_box_set_model with a null Model should be good
enough. But in practice, while the ComboBox model is unset, GTK
doesn't unset the ComboBox menus model, so that remains listening to
additions to the ListStore and slowing things down massively.
Using a new model does reset the menu to listen to that unused one instead */
gtk_combo_box_set_model(m_pComboBox, GTK_TREE_MODEL(gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING)));
GtkTreeViewColumn* pCol = gtk_tree_view_column_new();
gtk_tree_view_append_column(m_pTreeView, pCol);
bool bPixbufUsedSurface = gtk_tree_model_get_n_columns(m_pTreeModel) == 4;
GList* cells = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(m_pComboBox));
// move the cell renderers from the combobox to the replacement treeview
m_pMenuTextRenderer = static_cast<GtkCellRenderer*>(cells->data);
for (GList* pRenderer = g_list_first(cells); pRenderer; pRenderer = g_list_next(pRenderer))
{
GtkCellRenderer* pCellRenderer = GTK_CELL_RENDERER(pRenderer->data);
bool bTextRenderer = pCellRenderer == m_pMenuTextRenderer;
gtk_tree_view_column_pack_end(pCol, pCellRenderer, bTextRenderer);
if (!bTextRenderer)
{
if (bPixbufUsedSurface)
gtk_tree_view_column_set_attributes(pCol, pCellRenderer, "surface", 3, nullptr);
else
gtk_tree_view_column_set_attributes(pCol, pCellRenderer, "pixbuf", 2, nullptr);
}
}
gtk_tree_view_column_set_attributes(pCol, m_pMenuTextRenderer, "text", m_nTextCol, nullptr);
if (gtk_combo_box_get_has_entry(m_pComboBox))
{
m_bAutoComplete = true;
m_nEntryInsertTextSignalId = g_signal_connect(m_pEntry, "insert-text", G_CALLBACK(signalEntryInsertText), this);
m_nEntryActivateSignalId = g_signal_connect(m_pEntry, "activate", G_CALLBACK(signalEntryActivate), this);
m_nEntryFocusInSignalId = g_signal_connect(m_pEntry, "focus-in-event", G_CALLBACK(signalEntryFocusIn), this);
m_nEntryFocusOutSignalId = g_signal_connect(m_pEntry, "focus-out-event", G_CALLBACK(signalEntryFocusOut), this);
m_nEntryKeyPressEventSignalId = g_signal_connect(m_pEntry, "key-press-event", G_CALLBACK(signalEntryKeyPress), this);
m_nKeyPressEventSignalId = 0;
}
else
{
gtk_widget_set_visible(m_pEntry, false);
m_pEntry = nullptr;
GtkWidget* pArrow = GTK_WIDGET(gtk_builder_get_object(pComboBuilder, "arrow"));
gtk_container_child_set(getContainer(), m_pToggleButton, "expand", true, nullptr);
auto m_pCellArea = gtk_cell_area_box_new();
m_pCellView = GTK_CELL_VIEW(gtk_cell_view_new_with_context(m_pCellArea, nullptr));
gtk_widget_set_hexpand(GTK_WIDGET(m_pCellView), true);
GtkBox* pBox = GTK_BOX(gtk_widget_get_parent(pArrow));
gint nImageSpacing(2);
GtkStyleContext *pContext = gtk_widget_get_style_context(GTK_WIDGET(m_pToggleButton));
gtk_style_context_get_style(pContext, "image-spacing", &nImageSpacing, nullptr);
gtk_box_set_spacing(pBox, nImageSpacing);
gtk_box_pack_start(pBox, GTK_WIDGET(m_pCellView), false, true, 0);
gtk_cell_view_set_fit_model(m_pCellView, true);
gtk_cell_view_set_model(m_pCellView, m_pTreeModel);
m_pButtonTextRenderer = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_end(GTK_CELL_LAYOUT(m_pCellView), m_pButtonTextRenderer, true);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(m_pCellView), m_pButtonTextRenderer, "text", m_nTextCol, nullptr);
if (g_list_length(cells) > 1)
{
GtkCellRenderer* pCellRenderer = gtk_cell_renderer_pixbuf_new();
gtk_cell_layout_pack_end(GTK_CELL_LAYOUT(m_pCellView), pCellRenderer, false);
if (bPixbufUsedSurface)
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(m_pCellView), pCellRenderer, "surface", 3, nullptr);
else
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(m_pCellView), pCellRenderer, "pixbuf", 2, nullptr);
}
gtk_widget_show_all(GTK_WIDGET(m_pCellView));
m_nEntryInsertTextSignalId = 0;
m_nEntryActivateSignalId = 0;
m_nEntryFocusInSignalId = 0;
m_nEntryFocusOutSignalId = 0;
m_nEntryKeyPressEventSignalId = 0;
m_nKeyPressEventSignalId = g_signal_connect(m_pToggleButton, "key-press-event", G_CALLBACK(signalKeyPress), this);
}
g_list_free(cells);
if (nActive != -1)
tree_view_set_cursor(nActive);
g_signal_connect(getContainer(), "mnemonic-activate", G_CALLBACK(signalComboMnemonicActivate), this);
g_signal_connect(m_pMenuWindow, "grab-broken-event", G_CALLBACK(signalGrabBroken), this);
g_signal_connect(m_pMenuWindow, "button-press-event", G_CALLBACK(signalButtonPress), this);
g_signal_connect(m_pMenuWindow, "motion-notify-event", G_CALLBACK(signalMotion), this);
// support typeahead for the menu itself, typing into the menu will
// select via the vcl selection engine, a matching entry.
g_signal_connect(m_pMenuWindow, "key-press-event", G_CALLBACK(signalKeyPress), this);
g_signal_connect(m_pOverlay, "get-child-position", G_CALLBACK(signalGetChildPosition), this);
gtk_overlay_add_overlay(m_pOverlay, GTK_WIDGET(m_pOverlayButton));
g_signal_connect(m_pOverlayButton, "leave-notify-event", G_CALLBACK(signalOverlayButtonCrossing), this);
g_signal_connect(m_pOverlayButton, "enter-notify-event", G_CALLBACK(signalOverlayButtonCrossing), this);
}
virtual int get_active() const override
{
int nActive = get_active_including_mru();
if (nActive == -1)
return -1;
if (m_nMRUCount)
{
if (nActive < m_nMRUCount)
nActive = find_text(get_text_including_mru(nActive));
else
nActive -= (m_nMRUCount + 1);
}
return nActive;
}
virtual OUString get_active_id() const override
{
int nActive = get_active();
return nActive != -1 ? get_id(nActive) : OUString();
}
virtual void set_active_id(const OUString& rStr) override
{
set_active(find_id(rStr));
m_bChangedByMenu = false;
}
virtual void set_size_request(int nWidth, int nHeight) override
{
if (m_pButtonTextRenderer)
{
// tweak the cell render to get a narrower size to stick
if (nWidth != -1)
{
// this bit isn't great, I really want to be able to ellipse the text in the comboboxtext itself and let
// the popup menu render them in full, in the interim ellipse both of them
g_object_set(G_OBJECT(m_pButtonTextRenderer), "ellipsize", PANGO_ELLIPSIZE_MIDDLE, nullptr);
// to find out how much of the width of the combobox belongs to the cell, set
// the cell and widget to the min cell width and see what the difference is
int min;
gtk_cell_renderer_get_preferred_width(m_pButtonTextRenderer, m_pWidget, &min, nullptr);
gtk_cell_renderer_set_fixed_size(m_pButtonTextRenderer, min, -1);
gtk_widget_set_size_request(m_pWidget, min, -1);
int nNonCellWidth = get_preferred_size().Width() - min;
int nCellWidth = nWidth - nNonCellWidth;
if (nCellWidth >= 0)
{
// now set the cell to the max width which it can be within the
// requested widget width
gtk_cell_renderer_set_fixed_size(m_pButtonTextRenderer, nWidth - nNonCellWidth, -1);
}
}
else
{
g_object_set(G_OBJECT(m_pButtonTextRenderer), "ellipsize", PANGO_ELLIPSIZE_NONE, nullptr);
gtk_cell_renderer_set_fixed_size(m_pButtonTextRenderer, -1, -1);
}
}
gtk_widget_set_size_request(m_pWidget, nWidth, nHeight);
}
virtual void set_active(int pos) override
{
set_active_including_mru(include_mru(pos), false);
}
virtual OUString get_active_text() const override
{
if (m_pEntry)
{
const gchar* pText = gtk_entry_get_text(GTK_ENTRY(m_pEntry));
return OUString(pText, pText ? strlen(pText) : 0, RTL_TEXTENCODING_UTF8);
}
int nActive = get_active();
if (nActive == -1)
return OUString();
return get_text(nActive);
}
virtual OUString get_text(int pos) const override
{
if (m_nMRUCount)
pos += (m_nMRUCount + 1);
return get_text_including_mru(pos);
}
virtual OUString get_id(int pos) const override
{
if (m_nMRUCount)
pos += (m_nMRUCount + 1);
return get_id_including_mru(pos);
}
virtual void set_id(int pos, const OUString& rId) override
{
if (m_nMRUCount)
pos += (m_nMRUCount + 1);
set_id_including_mru(pos, rId);
}
virtual void insert_vector(const std::vector<weld::ComboBoxEntry>& rItems, bool bKeepExisting) override
{
freeze();
int nInsertionPoint;
if (!bKeepExisting)
{
clear();
nInsertionPoint = 0;
}
else
nInsertionPoint = get_count();
GtkTreeIter iter;
// tdf#125241 inserting backwards is faster
for (auto aI = rItems.rbegin(); aI != rItems.rend(); ++aI)
{
const auto& rItem = *aI;
insert_row(GTK_LIST_STORE(m_pTreeModel), iter, nInsertionPoint, rItem.sId.isEmpty() ? nullptr : &rItem.sId,
rItem.sString, rItem.sImage.isEmpty() ? nullptr : &rItem.sImage, nullptr);
}
thaw();
}
virtual void remove(int pos) override
{
if (m_nMRUCount)
pos += (m_nMRUCount + 1);
remove_including_mru(pos);
}
virtual void insert(int pos, const OUString& rText, const OUString* pId, const OUString* pIconName, VirtualDevice* pImageSurface) override
{
insert_including_mru(include_mru(pos), rText, pId, pIconName, pImageSurface);
}
virtual void insert_separator(int pos, const OUString& rId) override
{
pos = pos == -1 ? get_count() : pos;
if (m_nMRUCount)
pos += (m_nMRUCount + 1);
insert_separator_including_mru(pos, rId);
}
virtual int get_count() const override
{
int nCount = get_count_including_mru();
if (m_nMRUCount)
nCount -= (m_nMRUCount + 1);
return nCount;
}
virtual int find_text(const OUString& rStr) const override
{
int nPos = find_text_including_mru(rStr, false);
if (nPos != -1 && m_nMRUCount)
nPos -= (m_nMRUCount + 1);
return nPos;
}
virtual int find_id(const OUString& rId) const override
{
int nPos = find_id_including_mru(rId, false);
if (nPos != -1 && m_nMRUCount)
nPos -= (m_nMRUCount + 1);
return nPos;
}
virtual void clear() override
{
do_clear();
}
virtual void make_sorted() override
{
m_xSorter.reset(new comphelper::string::NaturalStringSorter(
::comphelper::getProcessComponentContext(),
Application::GetSettings().GetUILanguageTag().getLocale()));
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_set_sort_column_id(pSortable, m_nTextCol, GTK_SORT_ASCENDING);
gtk_tree_sortable_set_sort_func(pSortable, m_nTextCol, default_sort_func, m_xSorter.get(), nullptr);
}
virtual bool has_entry() const override
{
return gtk_combo_box_get_has_entry(m_pComboBox);
}
virtual void set_entry_message_type(weld::EntryMessageType eType) override
{
assert(m_pEntry);
::set_entry_message_type(GTK_ENTRY(m_pEntry), eType);
}
virtual void set_entry_text(const OUString& rText) override
{
assert(m_pEntry);
disable_notify_events();
gtk_entry_set_text(GTK_ENTRY(m_pEntry), OUStringToOString(rText, RTL_TEXTENCODING_UTF8).getStr());
enable_notify_events();
}
virtual void set_entry_width_chars(int nChars) override
{
assert(m_pEntry);
disable_notify_events();
gtk_entry_set_width_chars(GTK_ENTRY(m_pEntry), nChars);
gtk_entry_set_max_width_chars(GTK_ENTRY(m_pEntry), nChars);
enable_notify_events();
}
virtual void set_entry_max_length(int nChars) override
{
assert(m_pEntry);
disable_notify_events();
gtk_entry_set_max_length(GTK_ENTRY(m_pEntry), nChars);
enable_notify_events();
}
virtual void select_entry_region(int nStartPos, int nEndPos) override
{
assert(m_pEntry);
disable_notify_events();
gtk_editable_select_region(GTK_EDITABLE(m_pEntry), nStartPos, nEndPos);
enable_notify_events();
}
virtual bool get_entry_selection_bounds(int& rStartPos, int &rEndPos) override
{
assert(m_pEntry);
return gtk_editable_get_selection_bounds(GTK_EDITABLE(m_pEntry), &rStartPos, &rEndPos);
}
virtual void set_entry_completion(bool bEnable, bool bCaseSensitive) override
{
m_bAutoComplete = bEnable;
m_bAutoCompleteCaseSensitive = bCaseSensitive;
}
virtual void set_entry_placeholder_text(const OUString& rText) override
{
assert(m_pEntry);
gtk_entry_set_placeholder_text(GTK_ENTRY(m_pEntry), rText.toUtf8().getStr());
}
virtual void set_entry_editable(bool bEditable) override
{
assert(m_pEntry);
gtk_editable_set_editable(GTK_EDITABLE(m_pEntry), bEditable);
}
virtual void cut_entry_clipboard() override
{
assert(m_pEntry);
gtk_editable_cut_clipboard(GTK_EDITABLE(m_pEntry));
}
virtual void copy_entry_clipboard() override
{
assert(m_pEntry);
gtk_editable_copy_clipboard(GTK_EDITABLE(m_pEntry));
}
virtual void paste_entry_clipboard() override
{
assert(m_pEntry);
gtk_editable_paste_clipboard(GTK_EDITABLE(m_pEntry));
}
virtual void set_font(const vcl::Font& rFont) override
{
m_aCustomFont.use_custom_font(&rFont, u"box#combobox");
}
virtual vcl::Font get_font() override
{
if (const vcl::Font* pFont = m_aCustomFont.get_custom_font())
return *pFont;
return GtkInstanceWidget::get_font();
}
virtual void set_entry_font(const vcl::Font& rFont) override
{
m_xEntryFont = rFont;
assert(m_pEntry);
PangoAttrList* pOrigList = gtk_entry_get_attributes(GTK_ENTRY(m_pEntry));
PangoAttrList* pAttrList = pOrigList ? pango_attr_list_copy(pOrigList) : pango_attr_list_new();
update_attr_list(pAttrList, rFont);
gtk_entry_set_attributes(GTK_ENTRY(m_pEntry), pAttrList);
pango_attr_list_unref(pAttrList);
}
virtual vcl::Font get_entry_font() override
{
if (m_xEntryFont)
return *m_xEntryFont;
assert(m_pEntry);
PangoContext* pContext = gtk_widget_get_pango_context(m_pEntry);
return pango_to_vcl(pango_context_get_font_description(pContext),
Application::GetSettings().GetUILanguageTag().getLocale());
}
virtual void disable_notify_events() override
{
if (m_pEntry)
{
g_signal_handler_block(m_pEntry, m_nEntryInsertTextSignalId);
g_signal_handler_block(m_pEntry, m_nEntryActivateSignalId);
g_signal_handler_block(m_pEntry, m_nEntryFocusInSignalId);
g_signal_handler_block(m_pEntry, m_nEntryFocusOutSignalId);
g_signal_handler_block(m_pEntry, m_nEntryKeyPressEventSignalId);
g_signal_handler_block(m_pEntry, m_nChangedSignalId);
}
else
g_signal_handler_block(m_pToggleButton, m_nKeyPressEventSignalId);
if (m_nToggleFocusInSignalId)
g_signal_handler_block(m_pToggleButton, m_nToggleFocusInSignalId);
if (m_nToggleFocusOutSignalId)
g_signal_handler_block(m_pToggleButton, m_nToggleFocusOutSignalId);
g_signal_handler_block(m_pTreeView, m_nRowActivatedSignalId);
g_signal_handler_block(m_pToggleButton, m_nPopupShownSignalId);
GtkInstanceContainer::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkInstanceContainer::enable_notify_events();
g_signal_handler_unblock(m_pToggleButton, m_nPopupShownSignalId);
g_signal_handler_unblock(m_pTreeView, m_nRowActivatedSignalId);
if (m_nToggleFocusInSignalId)
g_signal_handler_unblock(m_pToggleButton, m_nToggleFocusInSignalId);
if (m_nToggleFocusOutSignalId)
g_signal_handler_unblock(m_pToggleButton, m_nToggleFocusOutSignalId);
if (m_pEntry)
{
g_signal_handler_unblock(m_pEntry, m_nChangedSignalId);
g_signal_handler_unblock(m_pEntry, m_nEntryActivateSignalId);
g_signal_handler_unblock(m_pEntry, m_nEntryFocusInSignalId);
g_signal_handler_unblock(m_pEntry, m_nEntryFocusOutSignalId);
g_signal_handler_unblock(m_pEntry, m_nEntryKeyPressEventSignalId);
g_signal_handler_unblock(m_pEntry, m_nEntryInsertTextSignalId);
}
else
g_signal_handler_unblock(m_pToggleButton, m_nKeyPressEventSignalId);
}
virtual void freeze() override
{
disable_notify_events();
bool bIsFirstFreeze = IsFirstFreeze();
GtkInstanceContainer::freeze();
if (bIsFirstFreeze)
{
g_object_ref(m_pTreeModel);
gtk_tree_view_set_model(m_pTreeView, nullptr);
g_object_freeze_notify(G_OBJECT(m_pTreeModel));
if (m_xSorter)
{
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_set_sort_column_id(pSortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
}
}
enable_notify_events();
}
virtual void thaw() override
{
disable_notify_events();
if (IsLastThaw())
{
if (m_xSorter)
{
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(m_pTreeModel);
gtk_tree_sortable_set_sort_column_id(pSortable, m_nTextCol, GTK_SORT_ASCENDING);
}
g_object_thaw_notify(G_OBJECT(m_pTreeModel));
gtk_tree_view_set_model(m_pTreeView, m_pTreeModel);
g_object_unref(m_pTreeModel);
}
GtkInstanceContainer::thaw();
enable_notify_events();
}
virtual bool get_popup_shown() const override
{
return m_bPopupActive;
}
virtual void connect_focus_in(const Link<Widget&, void>& rLink) override
{
if (!m_nToggleFocusInSignalId)
m_nToggleFocusInSignalId = g_signal_connect_after(m_pToggleButton, "focus-in-event", G_CALLBACK(signalFocusIn), this);
GtkInstanceContainer::connect_focus_in(rLink);
}
virtual void connect_focus_out(const Link<Widget&, void>& rLink) override
{
if (!m_nToggleFocusOutSignalId)
m_nToggleFocusOutSignalId = g_signal_connect_after(m_pToggleButton, "focus-out-event", G_CALLBACK(signalFocusOut), this);
GtkInstanceContainer::connect_focus_out(rLink);
}
virtual void grab_focus() override
{
if (has_focus())
return;
if (m_pEntry)
gtk_widget_grab_focus(m_pEntry);
else
gtk_widget_grab_focus(m_pToggleButton);
}
virtual bool has_focus() const override
{
if (m_pEntry && gtk_widget_has_focus(m_pEntry))
return true;
if (gtk_widget_has_focus(m_pToggleButton))
return true;
if (gtk_widget_get_visible(GTK_WIDGET(m_pMenuWindow)))
{
if (gtk_widget_has_focus(GTK_WIDGET(m_pOverlayButton)) || gtk_widget_has_focus(GTK_WIDGET(m_pTreeView)))
return true;
}
return GtkInstanceWidget::has_focus();
}
virtual bool changed_by_direct_pick() const override
{
return m_bChangedByMenu;
}
virtual void set_custom_renderer(bool bOn) override
{
if (bOn == m_bCustomRenderer)
return;
GList* pColumns = gtk_tree_view_get_columns(m_pTreeView);
// keep the original height around for optimal popup height calculation
m_nNonCustomLineHeight = bOn ? ::get_height_row(m_pTreeView, pColumns) : -1;
GtkTreeViewColumn* pColumn = GTK_TREE_VIEW_COLUMN(pColumns->data);
gtk_cell_layout_clear(GTK_CELL_LAYOUT(pColumn));
if (bOn)
{
GtkCellRenderer *pRenderer = custom_cell_renderer_new();
GValue value = G_VALUE_INIT;
g_value_init(&value, G_TYPE_POINTER);
g_value_set_pointer(&value, static_cast<gpointer>(this));
g_object_set_property(G_OBJECT(pRenderer), "instance", &value);
gtk_tree_view_column_pack_start(pColumn, pRenderer, true);
gtk_tree_view_column_add_attribute(pColumn, pRenderer, "text", m_nTextCol);
gtk_tree_view_column_add_attribute(pColumn, pRenderer, "id", m_nIdCol);
}
else
{
GtkCellRenderer *pRenderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(pColumn, pRenderer, true);
gtk_tree_view_column_add_attribute(pColumn, pRenderer, "text", m_nTextCol);
}
g_list_free(pColumns);
m_bCustomRenderer = bOn;
}
void call_signal_custom_render(VirtualDevice& rOutput, const tools::Rectangle& rRect, bool bSelected, const OUString& rId)
{
signal_custom_render(rOutput, rRect, bSelected, rId);
}
Size call_signal_custom_get_size(VirtualDevice& rOutput)
{
return signal_custom_get_size(rOutput);
}
VclPtr<VirtualDevice> create_render_virtual_device() const override
{
return create_virtual_device();
}
virtual void set_item_menu(const OUString& rIdent, weld::Menu* pMenu) override
{
m_xCustomMenuButtonHelper.reset();
GtkInstanceMenu* pPopoverWidget = dynamic_cast<GtkInstanceMenu*>(pMenu);
GtkWidget* pMenuWidget = GTK_WIDGET(pPopoverWidget ? pPopoverWidget->getMenu() : nullptr);
gtk_menu_button_set_popup(m_pOverlayButton, pMenuWidget);
gtk_widget_set_visible(GTK_WIDGET(m_pOverlayButton), pMenuWidget != nullptr);
gtk_widget_queue_resize_no_redraw(GTK_WIDGET(m_pOverlayButton)); // force location recalc
if (pMenuWidget)
m_xCustomMenuButtonHelper.reset(new CustomRenderMenuButtonHelper(GTK_MENU(pMenuWidget), GTK_TOGGLE_BUTTON(m_pToggleButton)));
m_sMenuButtonRow = rIdent;
}
OUString get_mru_entries() const override
{
const sal_Unicode cSep = ';';
OUStringBuffer aEntries;
for (sal_Int32 n = 0; n < m_nMRUCount; n++)
{
aEntries.append(get_text_including_mru(n));
if (n < m_nMRUCount - 1)
aEntries.append(cSep);
}
return aEntries.makeStringAndClear();
}
virtual void set_mru_entries(const OUString& rEntries) override
{
const sal_Unicode cSep = ';';
// Remove old MRU entries
for (sal_Int32 n = m_nMRUCount; n;)
remove_including_mru(--n);
sal_Int32 nMRUCount = 0;
sal_Int32 nIndex = 0;
do
{
OUString aEntry = rEntries.getToken(0, cSep, nIndex);
// Accept only existing entries
int nPos = find_text(aEntry);
if (nPos != -1)
{
OUString sId = get_id(nPos);
insert_including_mru(0, aEntry, &sId, nullptr, nullptr);
++nMRUCount;
}
}
while (nIndex >= 0);
if (nMRUCount && !m_nMRUCount)
insert_separator_including_mru(nMRUCount, "separator");
else if (!nMRUCount && m_nMRUCount)
remove_including_mru(m_nMRUCount); // remove separator
m_nMRUCount = nMRUCount;
}
int get_menu_button_width() const override
{
bool bVisible = gtk_widget_get_visible(GTK_WIDGET(m_pOverlayButton));
if (!bVisible)
gtk_widget_set_visible(GTK_WIDGET(m_pOverlayButton), true);
gint nWidth;
gtk_widget_get_preferred_width(GTK_WIDGET(m_pOverlayButton), &nWidth, nullptr);
if (!bVisible)
gtk_widget_set_visible(GTK_WIDGET(m_pOverlayButton), false);
return nWidth;
}
virtual ~GtkInstanceComboBox() override
{
m_xCustomMenuButtonHelper.reset();
do_clear();
if (m_nAutoCompleteIdleId)
g_source_remove(m_nAutoCompleteIdleId);
if (m_pEntry)
{
g_signal_handler_disconnect(m_pEntry, m_nChangedSignalId);
g_signal_handler_disconnect(m_pEntry, m_nEntryInsertTextSignalId);
g_signal_handler_disconnect(m_pEntry, m_nEntryActivateSignalId);
g_signal_handler_disconnect(m_pEntry, m_nEntryFocusInSignalId);
g_signal_handler_disconnect(m_pEntry, m_nEntryFocusOutSignalId);
g_signal_handler_disconnect(m_pEntry, m_nEntryKeyPressEventSignalId);
}
else
g_signal_handler_disconnect(m_pToggleButton, m_nKeyPressEventSignalId);
if (m_nToggleFocusInSignalId)
g_signal_handler_disconnect(m_pToggleButton, m_nToggleFocusInSignalId);
if (m_nToggleFocusOutSignalId)
g_signal_handler_disconnect(m_pToggleButton, m_nToggleFocusOutSignalId);
g_signal_handler_disconnect(m_pTreeView, m_nRowActivatedSignalId);
g_signal_handler_disconnect(m_pToggleButton, m_nPopupShownSignalId);
gtk_combo_box_set_model(m_pComboBox, m_pTreeModel);
gtk_tree_view_set_model(m_pTreeView, nullptr);
// restore original hierarchy in dtor so a new GtkInstanceComboBox will
// result in the same layout each time
{
DisconnectMouseEvents();
g_object_ref(m_pComboBox);
GtkContainer* pContainer = getContainer();
gtk_container_remove(pContainer, GTK_WIDGET(m_pComboBox));
replaceWidget(GTK_WIDGET(pContainer), GTK_WIDGET(m_pComboBox));
g_object_unref(m_pComboBox);
}
g_object_unref(m_pComboBuilder);
}
};
#endif
}
void custom_cell_renderer_ensure_device(CustomCellRenderer *cellsurface, gpointer user_data)
{
if (!cellsurface->device)
{
cellsurface->device = VclPtr<VirtualDevice>::Create();
cellsurface->device->SetBackground(COL_TRANSPARENT);
GtkInstanceWidget* pWidget = static_cast<GtkInstanceWidget*>(user_data);
// expand the point size of the desired font to the equivalent pixel size
weld::SetPointFont(*cellsurface->device, pWidget->get_font());
}
}
Size custom_cell_renderer_get_size(VirtualDevice& rDevice, const OUString& rCellId, gpointer user_data)
{
GtkInstanceWidget* pWidget = static_cast<GtkInstanceWidget*>(user_data);
if (GtkInstanceTreeView* pTreeView = dynamic_cast<GtkInstanceTreeView*>(pWidget))
return pTreeView->call_signal_custom_get_size(rDevice, rCellId);
else if (GtkInstanceComboBox* pComboBox = dynamic_cast<GtkInstanceComboBox*>(pWidget))
return pComboBox->call_signal_custom_get_size(rDevice);
return Size();
}
void custom_cell_renderer_render(VirtualDevice& rDevice, const tools::Rectangle& rRect, bool bSelected, const OUString& rCellId, gpointer user_data)
{
GtkInstanceWidget* pWidget = static_cast<GtkInstanceWidget*>(user_data);
if (GtkInstanceTreeView* pTreeView = dynamic_cast<GtkInstanceTreeView*>(pWidget))
pTreeView->call_signal_custom_render(rDevice, rRect, bSelected, rCellId);
else if (GtkInstanceComboBox* pComboBox = dynamic_cast<GtkInstanceComboBox*>(pWidget))
pComboBox->call_signal_custom_render(rDevice, rRect, bSelected, rCellId);
}
namespace {
class GtkInstanceEntryTreeView : public GtkInstanceContainer, public virtual weld::EntryTreeView
{
private:
GtkInstanceEntry* m_pEntry;
GtkInstanceTreeView* m_pTreeView;
#if !GTK_CHECK_VERSION(4, 0, 0)
gulong m_nKeyPressSignalId;
#endif
gulong m_nEntryInsertTextSignalId;
guint m_nAutoCompleteIdleId;
bool m_bAutoCompleteCaseSensitive;
bool m_bTreeChange;
#if !GTK_CHECK_VERSION(4, 0, 0)
bool signal_key_press(GdkEventKey* pEvent)
{
if (GtkSalFrame::GetMouseModCode(pEvent->state)) // only with no modifiers held
return false;
if (pEvent->keyval == GDK_KEY_KP_Up || pEvent->keyval == GDK_KEY_Up || pEvent->keyval == GDK_KEY_KP_Page_Up || pEvent->keyval == GDK_KEY_Page_Up ||
pEvent->keyval == GDK_KEY_KP_Down || pEvent->keyval == GDK_KEY_Down || pEvent->keyval == GDK_KEY_KP_Page_Down || pEvent->keyval == GDK_KEY_Page_Down)
{
gboolean ret;
disable_notify_events();
GtkWidget* pWidget = m_pTreeView->getWidget();
if (m_pTreeView->get_selected_index() == -1)
{
m_pTreeView->set_cursor(0);
m_pTreeView->select(0);
m_xEntry->set_text(m_xTreeView->get_selected_text());
}
else
{
gtk_widget_grab_focus(pWidget);
g_signal_emit_by_name(pWidget, "key-press-event", pEvent, &ret);
m_xEntry->set_text(m_xTreeView->get_selected_text());
gtk_widget_grab_focus(m_pEntry->getWidget());
}
m_xEntry->select_region(0, -1);
enable_notify_events();
m_bTreeChange = true;
m_pEntry->fire_signal_changed();
m_bTreeChange = false;
return true;
}
return false;
}
static gboolean signalKeyPress(GtkWidget*, GdkEventKey* pEvent, gpointer widget)
{
GtkInstanceEntryTreeView* pThis = static_cast<GtkInstanceEntryTreeView*>(widget);
return pThis->signal_key_press(pEvent);
}
#endif
static gboolean idleAutoComplete(gpointer widget)
{
GtkInstanceEntryTreeView* pThis = static_cast<GtkInstanceEntryTreeView*>(widget);
pThis->auto_complete();
return false;
}
void auto_complete()
{
m_nAutoCompleteIdleId = 0;
OUString aStartText = get_active_text();
int nStartPos, nEndPos;
get_entry_selection_bounds(nStartPos, nEndPos);
int nMaxSelection = std::max(nStartPos, nEndPos);
if (nMaxSelection != aStartText.getLength())
return;
disable_notify_events();
int nActive = get_active();
int nStart = nActive;
if (nStart == -1)
nStart = 0;
// Try match case sensitive from current position
int nPos = m_pTreeView->starts_with(aStartText, nStart, true);
if (nPos == -1 && nStart != 0)
{
// Try match case insensitive, but from start
nPos = m_pTreeView->starts_with(aStartText, 0, true);
}
if (!m_bAutoCompleteCaseSensitive)
{
// Try match case insensitive from current position
nPos = m_pTreeView->starts_with(aStartText, nStart, false);
if (nPos == -1 && nStart != 0)
{
// Try match case insensitive, but from start
nPos = m_pTreeView->starts_with(aStartText, 0, false);
}
}
if (nPos == -1)
{
// Try match case sensitive from current position
nPos = m_pTreeView->starts_with(aStartText, nStart, true);
if (nPos == -1 && nStart != 0)
{
// Try match case sensitive, but from start
nPos = m_pTreeView->starts_with(aStartText, 0, true);
}
}
if (nPos != -1)
{
OUString aText = get_text(nPos);
if (aText != aStartText)
set_active_text(aText);
select_entry_region(aText.getLength(), aStartText.getLength());
}
enable_notify_events();
}
void signal_entry_insert_text(GtkEntry*, const gchar*, gint, gint*)
{
// now check for autocompletes
if (m_nAutoCompleteIdleId)
g_source_remove(m_nAutoCompleteIdleId);
m_nAutoCompleteIdleId = g_idle_add(idleAutoComplete, this);
}
static void signalEntryInsertText(GtkEntry* pEntry, const gchar* pNewText, gint nNewTextLength,
gint* position, gpointer widget)
{
GtkInstanceEntryTreeView* pThis = static_cast<GtkInstanceEntryTreeView*>(widget);
pThis->signal_entry_insert_text(pEntry, pNewText, nNewTextLength, position);
}
public:
#if GTK_CHECK_VERSION(4, 0, 0)
GtkInstanceEntryTreeView(GtkWidget* pContainer, GtkInstanceBuilder* pBuilder, bool bTakeOwnership,
std::unique_ptr<weld::Entry> xEntry, std::unique_ptr<weld::TreeView> xTreeView)
#else
GtkInstanceEntryTreeView(GtkContainer* pContainer, GtkInstanceBuilder* pBuilder, bool bTakeOwnership,
std::unique_ptr<weld::Entry> xEntry, std::unique_ptr<weld::TreeView> xTreeView)
#endif
: EntryTreeView(std::move(xEntry), std::move(xTreeView))
, GtkInstanceContainer(pContainer, pBuilder, bTakeOwnership)
, m_pEntry(dynamic_cast<GtkInstanceEntry*>(m_xEntry.get()))
, m_pTreeView(dynamic_cast<GtkInstanceTreeView*>(m_xTreeView.get()))
, m_nAutoCompleteIdleId(0)
, m_bAutoCompleteCaseSensitive(false)
, m_bTreeChange(false)
{
assert(m_pEntry);
GtkWidget* pWidget = m_pEntry->getWidget();
#if !GTK_CHECK_VERSION(4, 0, 0)
m_nKeyPressSignalId = g_signal_connect(pWidget, "key-press-event", G_CALLBACK(signalKeyPress), this);
#endif
m_nEntryInsertTextSignalId = g_signal_connect(pWidget, "insert-text", G_CALLBACK(signalEntryInsertText), this);
}
virtual void insert_separator(int /*pos*/, const OUString& /*rId*/) override
{
assert(false);
}
virtual void make_sorted() override
{
GtkWidget* pTreeView = m_pTreeView->getWidget();
GtkTreeModel* pModel = gtk_tree_view_get_model(GTK_TREE_VIEW(pTreeView));
GtkTreeSortable* pSortable = GTK_TREE_SORTABLE(pModel);
gtk_tree_sortable_set_sort_column_id(pSortable, 1, GTK_SORT_ASCENDING);
}
virtual void set_entry_completion(bool bEnable, bool bCaseSensitive) override
{
assert(!bEnable && "not implemented yet"); (void)bEnable;
m_bAutoCompleteCaseSensitive = bCaseSensitive;
}
virtual void set_entry_placeholder_text(const OUString& rText) override
{
m_xEntry->set_placeholder_text(rText);
}
virtual void set_entry_editable(bool bEditable) override
{
m_xEntry->set_editable(bEditable);
}
virtual void cut_entry_clipboard() override
{
m_xEntry->cut_clipboard();
}
virtual void copy_entry_clipboard() override
{
m_xEntry->copy_clipboard();
}
virtual void paste_entry_clipboard() override
{
m_xEntry->paste_clipboard();
}
virtual void set_font(const vcl::Font&) override
{
assert(false && "not implemented");
}
virtual void set_entry_font(const vcl::Font& rFont) override
{
m_xEntry->set_font(rFont);
}
virtual vcl::Font get_entry_font() override
{
return m_xEntry->get_font();
}
virtual void grab_focus() override { m_xEntry->grab_focus(); }
virtual void connect_focus_in(const Link<Widget&, void>& rLink) override
{
m_xEntry->connect_focus_in(rLink);
}
virtual void connect_focus_out(const Link<Widget&, void>& rLink) override
{
m_xEntry->connect_focus_out(rLink);
}
virtual void disable_notify_events() override
{
GtkWidget* pWidget = m_pEntry->getWidget();
g_signal_handler_block(pWidget, m_nEntryInsertTextSignalId);
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_block(pWidget, m_nKeyPressSignalId);
#endif
m_pTreeView->disable_notify_events();
GtkInstanceContainer::disable_notify_events();
}
virtual void enable_notify_events() override
{
GtkWidget* pWidget = m_pEntry->getWidget();
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_unblock(pWidget, m_nKeyPressSignalId);
#endif
g_signal_handler_unblock(pWidget, m_nEntryInsertTextSignalId);
m_pTreeView->enable_notify_events();
GtkInstanceContainer::enable_notify_events();
}
virtual bool changed_by_direct_pick() const override
{
return m_bTreeChange;
}
virtual void set_custom_renderer(bool /*bOn*/) override
{
assert(false && "not implemented");
}
virtual int get_max_mru_count() const override
{
assert(false && "not implemented");
return 0;
}
virtual void set_max_mru_count(int) override
{
assert(false && "not implemented");
}
virtual OUString get_mru_entries() const override
{
assert(false && "not implemented");
return OUString();
}
virtual void set_mru_entries(const OUString&) override
{
assert(false && "not implemented");
}
virtual void set_item_menu(const OUString&, weld::Menu*) override
{
assert(false && "not implemented");
}
VclPtr<VirtualDevice> create_render_virtual_device() const override
{
return create_virtual_device();
}
int get_menu_button_width() const override
{
assert(false && "not implemented");
return 0;
}
virtual ~GtkInstanceEntryTreeView() override
{
if (m_nAutoCompleteIdleId)
g_source_remove(m_nAutoCompleteIdleId);
GtkWidget* pWidget = m_pEntry->getWidget();
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(pWidget, m_nKeyPressSignalId);
#endif
g_signal_handler_disconnect(pWidget, m_nEntryInsertTextSignalId);
}
};
}
namespace {
class GtkInstanceExpander : public GtkInstanceWidget, public virtual weld::Expander
{
private:
GtkExpander* m_pExpander;
gulong m_nSignalId;
#if !GTK_CHECK_VERSION(4, 0, 0)
gulong m_nButtonPressEventSignalId;
gulong m_nMappedSignalId;
#endif
static void signalExpanded(GtkExpander* /*pExpander*/, GParamSpec*, gpointer widget)
{
GtkInstanceExpander* pThis = static_cast<GtkInstanceExpander*>(widget);
SolarMutexGuard aGuard;
pThis->signal_expanded();
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean signalButton(GtkWidget*, GdkEventButton*, gpointer)
{
// don't let button press get to parent window, for the case of the
// an expander in a sidebar where otherwise single click to expand
// doesn't work
return true;
}
/* tdf#141186 if the expander is initially collapsed then when mapped all its
children are mapped too. If they are mapped then the mnemonics of the
children are taken into account on shortcuts and non-visible children in a
collapsed expander can be triggered which is confusing.
If the expander is expanded and collapsed the child is unmapped and the
problem doesn't occur.
So to avoid the problem of an initially collapsed expander, listen to
the map event and if the expander is mapped but collapsed then unmap the
child of the expander.
This problem was seen in gtk3-3.24.33 and not with gtk4-4.6.4 so a gtk3
fix only needed.
*/
static void signalMap(GtkWidget*, gpointer widget)
{
GtkInstanceExpander* pThis = static_cast<GtkInstanceExpander*>(widget);
if (!gtk_expander_get_expanded(pThis->m_pExpander))
{
if (GtkWidget* pChild = gtk_bin_get_child(GTK_BIN(pThis->m_pExpander)))
gtk_widget_unmap(pChild);
}
}
#endif
public:
GtkInstanceExpander(GtkExpander* pExpander, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
: GtkInstanceWidget(GTK_WIDGET(pExpander), pBuilder, bTakeOwnership)
, m_pExpander(pExpander)
, m_nSignalId(g_signal_connect(m_pExpander, "notify::expanded", G_CALLBACK(signalExpanded), this))
#if !GTK_CHECK_VERSION(4, 0, 0)
, m_nButtonPressEventSignalId(g_signal_connect_after(m_pExpander, "button-press-event", G_CALLBACK(signalButton), this))
, m_nMappedSignalId(g_signal_connect_after(m_pExpander, "map", G_CALLBACK(signalMap), this))
#endif
{
}
virtual void set_label(const OUString& rText) override
{
::set_label(GTK_LABEL(gtk_expander_get_label_widget(m_pExpander)), rText);
}
virtual OUString get_label() const override
{
return ::get_label(GTK_LABEL(gtk_expander_get_label_widget(m_pExpander)));
}
virtual bool get_expanded() const override
{
return gtk_expander_get_expanded(m_pExpander);
}
virtual void set_expanded(bool bExpand) override
{
gtk_expander_set_expanded(m_pExpander, bExpand);
}
virtual ~GtkInstanceExpander() override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
g_signal_handler_disconnect(m_pExpander, m_nMappedSignalId);
g_signal_handler_disconnect(m_pExpander, m_nButtonPressEventSignalId);
#endif
g_signal_handler_disconnect(m_pExpander, m_nSignalId);
}
};
}
namespace {
class GtkInstancePopover : public GtkInstanceContainer, public virtual weld::Popover
{
private:
#if !GTK_CHECK_VERSION(4, 0, 0)
//popover cannot escape dialog under X so we might need to stick up own window instead
GtkWindow* m_pMenuHack;
bool m_bMenuPoppedUp;
bool m_nButtonPressSeen;
#endif
GtkPopover* m_pPopover;
gulong m_nSignalId;
ImplSVEvent* m_pClosedEvent;
static void signalClosed(GtkPopover*, gpointer widget)
{
GtkInstancePopover* pThis = static_cast<GtkInstancePopover*>(widget);
// call signal-closed async so the closed callback isn't called
// while the GtkPopover handler is still in-execution
pThis->launch_signal_closed();
}
DECL_LINK(async_signal_closed, void*, void);
void launch_signal_closed()
{
if (m_pClosedEvent)
Application::RemoveUserEvent(m_pClosedEvent);
m_pClosedEvent = Application::PostUserEvent(LINK(this, GtkInstancePopover, async_signal_closed));
}
#if !GTK_CHECK_VERSION(4, 0, 0)
static gboolean keyPress(GtkWidget*, GdkEventKey* pEvent, gpointer widget)
{
GtkInstancePopover* pThis = static_cast<GtkInstancePopover*>(widget);
return pThis->key_press(pEvent);
}
bool key_press(const GdkEventKey* pEvent)
{
if (pEvent->keyval == GDK_KEY_Escape)
{
popdown();
return true;
}
return false;
}
static gboolean signalButtonPress(GtkWidget* /*pWidget*/, GdkEventButton* /*pEvent*/, gpointer widget)
{
GtkInstancePopover* pThis = static_cast<GtkInstancePopover*>(widget);
pThis->m_nButtonPressSeen = true;
return false;
}
static gboolean signalButtonRelease(GtkWidget* /*pWidget*/, GdkEventButton* pEvent, gpointer widget)
{
GtkInstancePopover* pThis = static_cast<GtkInstancePopover*>(widget);
if (pThis->m_nButtonPressSeen && button_event_is_outside(GTK_WIDGET(pThis->m_pMenuHack), pEvent))
pThis->popdown();
return false;
}
bool forward_event_if_popup_under_mouse(GdkEvent* pEvent)
{
GtkWidget* pEventWidget = gtk_get_event_widget(pEvent);
GtkWidget* pTopLevel = widget_get_toplevel(pEventWidget);
if (pTopLevel == GTK_WIDGET(m_pMenuHack))
return false;
GdkSurface* pSurface = widget_get_surface(pTopLevel);
void* pMouseEnteredAnotherPopup = g_object_get_data(G_OBJECT(pSurface), "g-lo-InstancePopup");
if (!pMouseEnteredAnotherPopup)
return false;
return gtk_widget_event(pEventWidget, pEvent);
}
static gboolean signalButtonCrossing(GtkWidget*, GdkEvent* pEvent, gpointer widget)
{
GtkInstancePopover* pThis = static_cast<GtkInstancePopover*>(widget);
return pThis->forward_event_if_popup_under_mouse(pEvent);
}
static gboolean signalMotion(GtkWidget*, GdkEvent* pEvent, gpointer widget)
{
GtkInstancePopover* pThis = static_cast<GtkInstancePopover*>(widget);
return pThis->forward_event_if_popup_under_mouse(pEvent);
}
static void signalGrabBroken(GtkWidget*, GdkEventGrabBroken *pEvent, gpointer widget)
{
GtkInstancePopover* pThis = static_cast<GtkInstancePopover*>(widget);
pThis->grab_broken(pEvent);
}
void grab_broken(const GdkEventGrabBroken *event)
{
if (event->grab_window == nullptr)
{
popdown();
}
else if (!g_object_get_data(G_OBJECT(event->grab_window), "g-lo-InstancePopup")) // another LibreOffice popover took a grab
{
//try and regrab, so when we lose the grab to the menu of the color palette
//combobox we regain it so the color palette doesn't itself disappear on next
//click on the color palette combobox
do_grab(GTK_WIDGET(m_pMenuHack));
}
}
#endif
public:
GtkInstancePopover(GtkPopover* pPopover, GtkInstanceBuilder* pBuilder, bool bTakeOwnership)
#if !GTK_CHECK_VERSION(4, 0, 0)
: GtkInstanceContainer(GTK_CONTAINER(pPopover), pBuilder, bTakeOwnership)
, m_pMenuHack(nullptr)
, m_bMenuPoppedUp(false)
, m_nButtonPressSeen(false)
#else
: GtkInstanceContainer(GTK_WIDGET(pPopover), pBuilder, bTakeOwnership)
#endif
, m_pPopover(pPopover)
, m_nSignalId(g_signal_connect(m_pPopover, "closed", G_CALLBACK(signalClosed), this))
, m_pClosedEvent(nullptr)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
//under wayland a Popover will work to "escape" the parent dialog, not
//so under X, so come up with this hack to use a raw GtkWindow
GdkDisplay *pDisplay = gtk_widget_get_display(GTK_WIDGET(m_pPopover));
if (DLSYM_GDK_IS_X11_DISPLAY(pDisplay))
{
m_pMenuHack = GTK_WINDOW(gtk_window_new(GTK_WINDOW_POPUP));
gtk_window_set_type_hint(m_pMenuHack, GDK_WINDOW_TYPE_HINT_COMBO);
gtk_window_set_resizable(m_pMenuHack, false);
g_signal_connect(m_pMenuHack, "key-press-event", G_CALLBACK(keyPress), this);
g_signal_connect(m_pMenuHack, "grab-broken-event", G_CALLBACK(signalGrabBroken), this);
g_signal_connect(m_pMenuHack, "button-press-event", G_CALLBACK(signalButtonPress), this);
g_signal_connect(m_pMenuHack, "button-release-event", G_CALLBACK(signalButtonRelease), this);
// to emulate a modeless popover we forward the leave/enter/motion events to the widgets
// they would have gone to a if we were really modeless
if (!gtk_popover_get_modal(m_pPopover))
{
g_signal_connect(m_pMenuHack, "leave-notify-event", G_CALLBACK(signalButtonCrossing), this);
g_signal_connect(m_pMenuHack, "enter-notify-event", G_CALLBACK(signalButtonCrossing), this);
g_signal_connect(m_pMenuHack, "motion-notify-event", G_CALLBACK(signalMotion), this);
}
}
#endif
}
virtual void popup_at_rect(weld::Widget* pParent, const tools::Rectangle& rRect, weld::Placement ePlace) override
{
GtkInstanceWidget* pGtkWidget = dynamic_cast<GtkInstanceWidget*>(pParent);
assert(pGtkWidget);
GtkWidget* pWidget = pGtkWidget->getWidget();
GdkRectangle aRect;
pWidget = getPopupRect(pWidget, rRect, aRect);
#if GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_set_parent(GTK_WIDGET(m_pPopover), pWidget);
#else
gtk_popover_set_relative_to(m_pPopover, pWidget);
#endif
gtk_popover_set_pointing_to(m_pPopover, &aRect);
if (ePlace == weld::Placement::Under)
gtk_popover_set_position(m_pPopover, GTK_POS_BOTTOM);
else
{
if (::SwapForRTL(pWidget))
gtk_popover_set_position(m_pPopover, GTK_POS_LEFT);
else
gtk_popover_set_position(m_pPopover, GTK_POS_RIGHT);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
//under wayland a Popover will work to "escape" the parent dialog, not
//so under X, so come up with this hack to use a raw GtkWindow
GdkDisplay *pDisplay = gtk_widget_get_display(GTK_WIDGET(m_pPopover));
if (DLSYM_GDK_IS_X11_DISPLAY(pDisplay))
{
if (!m_bMenuPoppedUp)
{
MovePopoverContentsToWindow(GTK_WIDGET(m_pPopover), m_pMenuHack, pWidget, aRect, ePlace);
m_bMenuPoppedUp = true;
}
return;
}
#endif
gtk_popover_popup(m_pPopover);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
virtual bool get_visible() const override
{
if (m_pMenuHack)
return gtk_widget_get_visible(GTK_WIDGET(m_pMenuHack));
return gtk_widget_get_visible(m_pWidget);
}
virtual void ensureMouseEventWidget() override
{
if (!m_pMouseEventBox && m_pMenuHack)
{
m_pMouseEventBox = GTK_WIDGET(m_pMenuHack);
return;
}
GtkInstanceContainer::ensureMouseEventWidget();
}
#endif
virtual void popdown() override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
//under wayland a Popover will work to "escape" the parent dialog, not
//so under X, so come up with this hack to use a raw GtkWindow
GdkDisplay *pDisplay = gtk_widget_get_display(GTK_WIDGET(m_pPopover));
if (DLSYM_GDK_IS_X11_DISPLAY(pDisplay))
{
if (m_bMenuPoppedUp)
{
m_nButtonPressSeen = false;
MoveWindowContentsToPopover(m_pMenuHack, GTK_WIDGET(m_pPopover), gtk_popover_get_relative_to(m_pPopover));
m_bMenuPoppedUp = false;
signal_closed();
}
return;
}
#endif
gtk_popover_popdown(m_pPopover);
}
void PopdownAndFlushClosedSignal()
{
if (get_visible())
popdown();
if (m_pClosedEvent)
{
Application::RemoveUserEvent(m_pClosedEvent);
async_signal_closed(nullptr);
}
}
virtual void resize_to_request() override
{
// resizing to request is what gtk does automatically
}
virtual ~GtkInstancePopover() override
{
PopdownAndFlushClosedSignal();
DisconnectMouseEvents();
#if !GTK_CHECK_VERSION(4, 0, 0)
if (m_pMenuHack)
gtk_widget_destroy(GTK_WIDGET(m_pMenuHack));
#endif
g_signal_handler_disconnect(m_pPopover, m_nSignalId);
}
};
IMPL_LINK_NOARG(GtkInstancePopover, async_signal_closed, void*, void)
{
m_pClosedEvent = nullptr;
signal_closed();
}
}
#if !GTK_CHECK_VERSION(4, 0, 0)
namespace
{
AtkObject* drawing_area_get_accessible(GtkWidget *pWidget)
{
AtkObject* pDefaultAccessible = default_drawing_area_get_accessible(pWidget);
void* pData = g_object_get_data(G_OBJECT(pWidget), "g-lo-GtkInstanceDrawingArea");
GtkInstanceDrawingArea* pDrawingArea = static_cast<GtkInstanceDrawingArea*>(pData);
AtkObject *pAtkObj = pDrawingArea ? pDrawingArea->GetAtkObject(pDefaultAccessible) : nullptr;
if (pAtkObj)
return pAtkObj;
return pDefaultAccessible;
}
void ensure_intercept_drawing_area_accessibility()
{
static bool bDone;
if (!bDone)
{
gpointer pClass = g_type_class_ref(GTK_TYPE_DRAWING_AREA);
GtkWidgetClass* pWidgetClass = GTK_WIDGET_CLASS(pClass);
default_drawing_area_get_accessible = pWidgetClass->get_accessible;
pWidgetClass->get_accessible = drawing_area_get_accessible;
g_type_class_unref(pClass);
bDone = true;
}
}
void ensure_disable_ctrl_page_up_down(GType eType)
{
gpointer pClass = g_type_class_ref(eType);
GtkWidgetClass* pWidgetClass = GTK_WIDGET_CLASS(pClass);
GtkBindingSet* pBindingSet = gtk_binding_set_by_class(pWidgetClass);
gtk_binding_entry_remove(pBindingSet, GDK_KEY_Page_Up, GDK_CONTROL_MASK);
gtk_binding_entry_remove(pBindingSet, GDK_KEY_Page_Up, static_cast<GdkModifierType>(GDK_SHIFT_MASK|GDK_CONTROL_MASK));
gtk_binding_entry_remove(pBindingSet, GDK_KEY_Page_Down, GDK_CONTROL_MASK);
gtk_binding_entry_remove(pBindingSet, GDK_KEY_Page_Down, static_cast<GdkModifierType>(GDK_SHIFT_MASK|GDK_CONTROL_MASK));
g_type_class_unref(pClass);
}
// tdf#130400 disable ctrl+page_up and ctrl+page_down bindings so the
// keystrokes are consumed by the surrounding notebook bindings instead
void ensure_disable_ctrl_page_up_down_bindings()
{
static bool bDone;
if (!bDone)
{
ensure_disable_ctrl_page_up_down(GTK_TYPE_TREE_VIEW);
ensure_disable_ctrl_page_up_down(GTK_TYPE_SPIN_BUTTON);
bDone = true;
}
}
}
#endif
namespace {
bool IsAllowedBuiltInIcon(std::u16string_view iconName)
{
// limit the named icons to those known by VclBuilder
return VclBuilder::mapStockToSymbol(iconName) != SymbolType::DONTKNOW;
}
}
namespace {
#if !GTK_CHECK_VERSION(4, 0, 0)
void silence_gwarning(const gchar* /*log_domain*/,
GLogLevelFlags /*log_level*/,
const gchar* /*message*/,
gpointer /*user_data*/)
{
}
#endif
void load_ui_file(GtkBuilder* pBuilder, const OUString& rUri)
{
#if GTK_CHECK_VERSION(4, 0, 0)
builder_add_from_gtk3_file(pBuilder, rUri);
#else
guint nLogHandlerId = 0;
GLogLevelFlags nFatalMask(static_cast<GLogLevelFlags>(G_LOG_FLAG_RECURSION|G_LOG_LEVEL_ERROR));
if (rUri.endsWith("sfx/ui/tabbarcontents.ui"))
{
// gtk unhelpfully has a bogus warning for the accelerator in this .ui because it assumes menus with accelerators
// if attached to something are attached to a MenuShell, but it's a MenuButton in this case. Turn off warnings, and
// in the case of fatal-warnings temp disable fatal warnings, for this case.
nLogHandlerId = g_log_set_handler("GLib-GObject",
static_cast<GLogLevelFlags>(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
silence_gwarning, nullptr);
nFatalMask = g_log_set_always_fatal(nFatalMask);
}
OUString aPath;
osl::FileBase::getSystemPathFromFileURL(rUri, aPath);
GError *err = nullptr;
auto rc = gtk_builder_add_from_file(pBuilder, OUStringToOString(aPath, RTL_TEXTENCODING_UTF8).getStr(), &err);
if (nLogHandlerId)
{
g_log_remove_handler("GLib-GObject", nLogHandlerId);
g_log_set_always_fatal(nFatalMask);
}
if (!rc)
{
SAL_WARN( "vcl.gtk", "GtkInstanceBuilder: error when calling gtk_builder_add_from_file: " << err->message);
g_error_free(err);
}
assert(rc && "could not load UI file");
#endif
}
#if !GTK_CHECK_VERSION(4, 0, 0)
void fix_expander(GtkExpander* pExpander, GParamSpec*, gpointer)
{
if (gtk_expander_get_resize_toplevel(pExpander))
{
GtkWidget *pToplevel = widget_get_toplevel(GTK_WIDGET(pExpander));
// https://gitlab.gnome.org/GNOME/gtk/issues/70
// I imagine at some point a release with a fix will be available in which
// case this can be avoided depending on version number
if (pToplevel && GTK_IS_WINDOW(pToplevel) && gtk_widget_get_realized(pToplevel))
{
int nToplevelWidth, nToplevelHeight;
int nChildHeight;
GtkWidget* child = gtk_bin_get_child(GTK_BIN(pExpander));
gtk_widget_get_preferred_height(child, &nChildHeight, nullptr);
gtk_window_get_size(GTK_WINDOW(pToplevel), &nToplevelWidth, &nToplevelHeight);
if (gtk_expander_get_expanded(pExpander))
nToplevelHeight += nChildHeight;
else
nToplevelHeight -= nChildHeight;
gtk_window_resize(GTK_WINDOW(pToplevel), nToplevelWidth, nToplevelHeight);
}
}
}
#endif
class GtkInstanceBuilder : public weld::Builder
{
private:
ResHookProc m_pStringReplace;
OUString m_aHelpRoot;
OUString m_aIconTheme;
OUString m_aUILang;
GtkBuilder* m_pBuilder;
GSList* m_pObjectList;
GtkWidget* m_pParentWidget;
gulong m_nNotifySignalId;
std::vector<GtkButton*> m_aMnemonicButtons;
#if GTK_CHECK_VERSION(4, 0, 0)
std::vector<GtkCheckButton*> m_aMnemonicCheckButtons;
#endif
std::vector<GtkLabel*> m_aMnemonicLabels;
VclPtr<SystemChildWindow> m_xInterimGlue;
bool m_bAllowCycleFocusOut;
void postprocess_widget(GtkWidget* pWidget)
{
const bool bHideHelp = comphelper::LibreOfficeKit::isActive() &&
officecfg::Office::Common::Help::HelpRootURL::get().isEmpty();
//fixup icons
//wanted: better way to do this, e.g. make gtk use gio for
//loading from a filename and provide gio protocol handler
//for our image in a zip urls
//
//unpack the images and keep them as dirs and just
//add the paths to the gtk icon theme dir
if (GTK_IS_IMAGE(pWidget))
{
GtkImage* pImage = GTK_IMAGE(pWidget);
if (const gchar* icon_name = image_get_icon_name(pImage))
{
OUString aIconName(icon_name, strlen(icon_name), RTL_TEXTENCODING_UTF8);
if (!IsAllowedBuiltInIcon(aIconName))
image_set_from_icon_name_theme_lang(pImage, aIconName, m_aIconTheme, m_aUILang);
}
}
#if GTK_CHECK_VERSION(4, 0, 0)
else if (GTK_IS_PICTURE(pWidget))
{
GtkPicture* pPicture = GTK_PICTURE(pWidget);
if (GFile* icon_file = gtk_picture_get_file(pPicture))
{
char* icon_name = g_file_get_uri(icon_file);
OUString aIconName(icon_name, strlen(icon_name), RTL_TEXTENCODING_UTF8);
g_free(icon_name);
assert(aIconName.startsWith("private:///graphicrepository/"));
aIconName.startsWith("private:///graphicrepository/", &aIconName);
picture_set_from_icon_name_theme_lang(GTK_PICTURE(pWidget), aIconName, m_aIconTheme, m_aUILang);
}
}
#endif
#if !GTK_CHECK_VERSION(4, 0, 0)
else if (GTK_IS_TOOL_BUTTON(pWidget))
{
GtkToolButton* pToolButton = GTK_TOOL_BUTTON(pWidget);
if (const gchar* icon_name = gtk_tool_button_get_icon_name(pToolButton))
{
OUString aIconName(icon_name, strlen(icon_name), RTL_TEXTENCODING_UTF8);
if (!IsAllowedBuiltInIcon(aIconName))
{
if (GtkWidget* pImage = image_new_from_icon_name_theme_lang(aIconName, m_aIconTheme, m_aUILang))
{
gtk_tool_button_set_icon_widget(pToolButton, pImage);
gtk_widget_show(pImage);
}
}
}
// if no tooltip reuse the label as default tooltip
if (!gtk_widget_get_tooltip_text(pWidget))
{
if (const gchar* label = gtk_tool_button_get_label(pToolButton))
gtk_widget_set_tooltip_text(pWidget, label);
}
}
else if (GTK_IS_EXPANDER(pWidget))
{
g_signal_connect(pWidget, "notify::expanded", G_CALLBACK(fix_expander), this);
}
#else
else if (GTK_IS_BUTTON(pWidget))
{
GtkButton* pButton = GTK_BUTTON(pWidget);
if (const gchar* icon_name = gtk_button_get_icon_name(pButton))
{
OUString aIconName(icon_name, strlen(icon_name), RTL_TEXTENCODING_UTF8);
if (!IsAllowedBuiltInIcon(aIconName))
{
if (GtkWidget* pImage = image_new_from_icon_name_theme_lang(aIconName, m_aIconTheme, m_aUILang))
{
gtk_widget_set_halign(pImage, GTK_ALIGN_CENTER);
gtk_widget_set_valign(pImage, GTK_ALIGN_CENTER);
gtk_button_set_child(pButton, pImage);
gtk_widget_show(pImage);
}
}
}
}
else if (GTK_IS_MENU_BUTTON(pWidget))
{
GtkMenuButton* pButton = GTK_MENU_BUTTON(pWidget);
if (const gchar* icon_name = gtk_menu_button_get_icon_name(pButton))
{
OUString aIconName(icon_name, strlen(icon_name), RTL_TEXTENCODING_UTF8);
if (!IsAllowedBuiltInIcon(aIconName))
{
if (GtkWidget* pImage = image_new_from_icon_name_theme_lang(aIconName, m_aIconTheme, m_aUILang))
{
gtk_widget_set_halign(pImage, GTK_ALIGN_CENTER);
gtk_widget_set_valign(pImage, GTK_ALIGN_CENTER);
// TODO after gtk 4.6 is released require that version and drop this
static auto menu_button_set_child = reinterpret_cast<void (*) (GtkMenuButton*, GtkWidget*)>(dlsym(nullptr, "gtk_menu_button_set_child"));
if (menu_button_set_child)
menu_button_set_child(pButton, pImage);
gtk_widget_show(pImage);
}
}
}
}
#endif
//set helpids
OUString sBuildableName = ::get_buildable_id(GTK_BUILDABLE(pWidget));
if (!sBuildableName.isEmpty())
{
OUString sHelpId = m_aHelpRoot + sBuildableName;
set_help_id(pWidget, sHelpId);
//hook up for extended help
const ImplSVHelpData& aHelpData = ImplGetSVHelpData();
if (aHelpData.mbBalloonHelp && !GTK_IS_DIALOG(pWidget) && !GTK_IS_ASSISTANT(pWidget))
{
gtk_widget_set_has_tooltip(pWidget, true);
g_signal_connect(pWidget, "query-tooltip", G_CALLBACK(signalTooltipQuery), nullptr);
}
if (bHideHelp && sBuildableName == "help")
gtk_widget_hide(pWidget);
}
if (m_pStringReplace)
{
// tdf#136498 %PRODUCTNAME shown in tool tips
const char* pTooltip = gtk_widget_get_tooltip_text(pWidget);
if (pTooltip && pTooltip[0])
{
OUString aTooltip(pTooltip, strlen(pTooltip), RTL_TEXTENCODING_UTF8);
aTooltip = (*m_pStringReplace)(aTooltip);
gtk_widget_set_tooltip_text(pWidget, OUStringToOString(aTooltip, RTL_TEXTENCODING_UTF8).getStr());
}
}
// expand placeholder and collect potentially missing mnemonics
if (GTK_IS_BUTTON(pWidget))
{
GtkButton* pButton = GTK_BUTTON(pWidget);
if (m_pStringReplace)
{
OUString aLabel(button_get_label(pButton));
if (!aLabel.isEmpty())
button_set_label(pButton, (*m_pStringReplace)(aLabel));
}
if (gtk_button_get_use_underline(pButton))
m_aMnemonicButtons.push_back(pButton);
}
#if GTK_CHECK_VERSION(4, 0, 0)
else if (GTK_IS_CHECK_BUTTON(pWidget))
{
GtkCheckButton* pButton = GTK_CHECK_BUTTON(pWidget);
if (m_pStringReplace)
{
OUString aLabel(get_label(pButton));
if (!aLabel.isEmpty())
set_label(pButton, (*m_pStringReplace)(aLabel));
}
if (gtk_check_button_get_use_underline(pButton))
m_aMnemonicCheckButtons.push_back(pButton);
}
#endif
else if (GTK_IS_LABEL(pWidget))
{
GtkLabel* pLabel = GTK_LABEL(pWidget);
if (m_pStringReplace)
{
OUString aLabel(get_label(pLabel));
if (!aLabel.isEmpty())
set_label(pLabel, (*m_pStringReplace)(aLabel));
}
if (gtk_label_get_use_underline(pLabel))
m_aMnemonicLabels.push_back(pLabel);
}
else if (GTK_IS_TEXT_VIEW(pWidget))
{
GtkTextView* pTextView = GTK_TEXT_VIEW(pWidget);
if (m_pStringReplace)
{
GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(pTextView);
GtkTextIter start, end;
gtk_text_buffer_get_bounds(pBuffer, &start, &end);
char* pTextStr = gtk_text_buffer_get_text(pBuffer, &start, &end, true);
int nTextLen = pTextStr ? strlen(pTextStr) : 0;
if (nTextLen)
{
OUString sOldText(pTextStr, nTextLen, RTL_TEXTENCODING_UTF8);
OString sText(OUStringToOString((*m_pStringReplace)(sOldText), RTL_TEXTENCODING_UTF8));
gtk_text_buffer_set_text(pBuffer, sText.getStr(), sText.getLength());
}
g_free(pTextStr);
}
}
#if !GTK_CHECK_VERSION(4, 0, 0)
else if (GTK_IS_ENTRY(pWidget))
{
g_signal_connect(pWidget, "key-press-event", G_CALLBACK(signalEntryInsertSpecialCharKeyPress), nullptr);
}
#endif
else if (GTK_IS_WINDOW(pWidget))
{
if (m_pStringReplace)
{
GtkWindow* pWindow = GTK_WINDOW(pWidget);
set_title(pWindow, (*m_pStringReplace)(get_title(pWindow)));
if (GTK_IS_MESSAGE_DIALOG(pWindow))
{
GtkMessageDialog* pMessageDialog = GTK_MESSAGE_DIALOG(pWindow);
set_primary_text(pMessageDialog, (*m_pStringReplace)(get_primary_text(pMessageDialog)));
set_secondary_text(pMessageDialog, (*m_pStringReplace)(get_secondary_text(pMessageDialog)));
}
}
}
}
//GtkBuilder sets translation domain during parse, and unsets it again afterwards.
//In order for GtkBuilder to find the translations bindtextdomain has to be called
//for the domain. So here on the first setting of "domain" we call Translate::Create
//to make sure that happens. Without this, if some other part of LibreOffice has
//used the translation machinery for this domain it will still work, but if it
//hasn't, e.g. tdf#119929, then the translation fails
void translation_domain_set()
{
Translate::Create(gtk_builder_get_translation_domain(m_pBuilder), LanguageTag(m_aUILang));
g_signal_handler_disconnect(m_pBuilder, m_nNotifySignalId);
}
static void signalNotify(GObject*, GParamSpec *pSpec, gpointer pData)
{
g_return_if_fail(pSpec != nullptr);
if (strcmp(pSpec->name, "translation-domain") == 0)
{
GtkInstanceBuilder* pBuilder = static_cast<GtkInstanceBuilder*>(pData);
pBuilder->translation_domain_set();
}
}
static void postprocess(gpointer data, gpointer user_data)
{
GObject* pObject = static_cast<GObject*>(data);
if (!GTK_IS_WIDGET(pObject))
return;
GtkInstanceBuilder* pThis = static_cast<GtkInstanceBuilder*>(user_data);
pThis->postprocess_widget(GTK_WIDGET(pObject));
}
void DisallowCycleFocusOut()
{
assert(!m_bAllowCycleFocusOut); // we only expect this to be called when this holds
GtkWidget* pTopLevel = widget_get_toplevel(m_pParentWidget);
assert(pTopLevel);
GtkSalFrame* pFrame = GtkSalFrame::getFromWindow(pTopLevel);
assert(pFrame);
// unhook handler and let gtk cycle its own way through this widget's
// children because it has no non-gtk siblings
pFrame->DisallowCycleFocusOut();
}
static void signalMap(GtkWidget*, gpointer user_data)
{
GtkInstanceBuilder* pThis = static_cast<GtkInstanceBuilder*>(user_data);
// tdf#138047 wait until map to do this because the final SalFrame may
// not be the same as at ctor time
pThis->DisallowCycleFocusOut();
}
void AllowCycleFocusOut()
{
assert(!m_bAllowCycleFocusOut); // we only expect this to be called when this holds
GtkWidget* pTopLevel = widget_get_toplevel(m_pParentWidget);
assert(pTopLevel);
GtkSalFrame* pFrame = GtkSalFrame::getFromWindow(pTopLevel);
assert(pFrame);
// rehook handler and let vcl cycle its own way through this widget's
// children
pFrame->AllowCycleFocusOut();
// tdf#145567 if the focus is in this hierarchy then, now that we are tearing down,
// move focus to the usual focus candidate for the frame
GtkWindow* pFocusWin = get_active_window();
GtkWidget* pFocus = pFocusWin ? gtk_window_get_focus(pFocusWin) : nullptr;
bool bHasFocus = pFocus && gtk_widget_is_ancestor(pFocus, pTopLevel);
if (bHasFocus)
pFrame->GrabFocus();
}
static void signalUnmap(GtkWidget*, gpointer user_data)
{
GtkInstanceBuilder* pThis = static_cast<GtkInstanceBuilder*>(user_data);
pThis->AllowCycleFocusOut();
}
public:
GtkInstanceBuilder(GtkWidget* pParent, std::u16string_view rUIRoot, const OUString& rUIFile,
SystemChildWindow* pInterimGlue, bool bAllowCycleFocusOut)
: weld::Builder()
, m_pStringReplace(Translate::GetReadStringHook())
, m_pParentWidget(pParent)
, m_nNotifySignalId(0)
, m_xInterimGlue(pInterimGlue)
, m_bAllowCycleFocusOut(bAllowCycleFocusOut)
{
OUString sHelpRoot(rUIFile);
#if !GTK_CHECK_VERSION(4, 0, 0)
ensure_intercept_drawing_area_accessibility();
ensure_disable_ctrl_page_up_down_bindings();
#endif
sal_Int32 nIdx = sHelpRoot.lastIndexOf('.');
if (nIdx != -1)
sHelpRoot = sHelpRoot.copy(0, nIdx);
sHelpRoot += "/";
m_aHelpRoot = sHelpRoot;
m_aIconTheme = Application::GetSettings().GetStyleSettings().DetermineIconTheme();
m_aUILang = Application::GetSettings().GetUILanguageTag().getBcp47();
OUString aUri(rUIRoot + rUIFile);
m_pBuilder = gtk_builder_new();
m_nNotifySignalId = g_signal_connect_data(G_OBJECT(m_pBuilder), "notify", G_CALLBACK(signalNotify), this, nullptr, G_CONNECT_AFTER);
load_ui_file(m_pBuilder, aUri);
m_pObjectList = gtk_builder_get_objects(m_pBuilder);
g_slist_foreach(m_pObjectList, postprocess, this);
GenerateMissingMnemonics();
if (m_xInterimGlue)
{
assert(m_pParentWidget);
g_object_set_data(G_OBJECT(m_pParentWidget), "InterimWindowGlue", m_xInterimGlue.get());
if (!m_bAllowCycleFocusOut)
{
g_signal_connect(G_OBJECT(m_pParentWidget), "map", G_CALLBACK(signalMap), this);
g_signal_connect(G_OBJECT(m_pParentWidget), "unmap", G_CALLBACK(signalUnmap), this);
}
}
}
void GenerateMissingMnemonics()
{
MnemonicGenerator aMnemonicGenerator('_');
for (const auto a : m_aMnemonicButtons)
aMnemonicGenerator.RegisterMnemonic(button_get_label(a));
#if GTK_CHECK_VERSION(4, 0, 0)
for (const auto a : m_aMnemonicCheckButtons)
aMnemonicGenerator.RegisterMnemonic(get_label(a));
#endif
for (const auto a : m_aMnemonicLabels)
aMnemonicGenerator.RegisterMnemonic(get_label(a));
for (const auto a : m_aMnemonicButtons)
{
OUString aLabel(button_get_label(a));
OUString aNewLabel = aMnemonicGenerator.CreateMnemonic(aLabel);
if (aLabel == aNewLabel)
continue;
button_set_label(a, aNewLabel);
}
#if GTK_CHECK_VERSION(4, 0, 0)
for (const auto a : m_aMnemonicCheckButtons)
{
OUString aLabel(get_label(a));
OUString aNewLabel = aMnemonicGenerator.CreateMnemonic(aLabel);
if (aLabel == aNewLabel)
continue;
set_label(a, aNewLabel);
}
#endif
for (const auto a : m_aMnemonicLabels)
{
OUString aLabel(get_label(a));
OUString aNewLabel = aMnemonicGenerator.CreateMnemonic(aLabel);
if (aLabel == aNewLabel)
continue;
set_label(a, aNewLabel);
}
m_aMnemonicLabels.clear();
#if GTK_CHECK_VERSION(4, 0, 0)
m_aMnemonicCheckButtons.clear();
#endif
m_aMnemonicButtons.clear();
}
OUString get_current_page_help_id()
{
OUString sPageHelpId;
// check to see if there is a notebook called tabcontrol and get the
// helpid for the current page of that
std::unique_ptr<weld::Notebook> xNotebook(weld_notebook("tabcontrol"));
if (xNotebook)
{
if (GtkInstanceContainer* pPage = dynamic_cast<GtkInstanceContainer*>(xNotebook->get_page(xNotebook->get_current_page_ident())))
{
GtkWidget* pContainer = pPage->getWidget();
if (GtkWidget* pPageWidget = widget_get_first_child(pContainer))
sPageHelpId = ::get_help_id(pPageWidget);
}
}
return sPageHelpId;
}
virtual ~GtkInstanceBuilder() override
{
g_slist_free(m_pObjectList);
g_object_unref(m_pBuilder);
if (m_xInterimGlue && !m_bAllowCycleFocusOut)
AllowCycleFocusOut();
m_xInterimGlue.disposeAndClear();
}
//ideally we would have/use weld::Container add and explicitly
//call add when we want to do this, but in the vcl impl the
//parent has to be set when the child is created, so for the
//gtk impl emulate this by doing this implicitly at weld time
void auto_add_parentless_widgets_to_container(GtkWidget* pWidget)
{
if (GTK_IS_POPOVER(pWidget))
return;
if (GTK_IS_WINDOW(pWidget))
return;
#if GTK_CHECK_VERSION(4, 0, 0)
if (!gtk_widget_get_parent(pWidget))
gtk_widget_set_parent(pWidget, m_pParentWidget);
#else
if (widget_get_toplevel(pWidget) == pWidget)
gtk_container_add(GTK_CONTAINER(m_pParentWidget), pWidget);
#endif
}
virtual std::unique_ptr<weld::MessageDialog> weld_message_dialog(const OUString &id) override
{
GtkMessageDialog* pMessageDialog = GTK_MESSAGE_DIALOG(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pMessageDialog)
return nullptr;
gtk_window_set_transient_for(GTK_WINDOW(pMessageDialog), GTK_WINDOW(widget_get_toplevel(m_pParentWidget)));
return std::make_unique<GtkInstanceMessageDialog>(pMessageDialog, this, true);
}
virtual std::unique_ptr<weld::Assistant> weld_assistant(const OUString &id) override
{
GtkAssistant* pAssistant = GTK_ASSISTANT(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pAssistant)
return nullptr;
if (m_pParentWidget)
gtk_window_set_transient_for(GTK_WINDOW(pAssistant), GTK_WINDOW(widget_get_toplevel(m_pParentWidget)));
return std::make_unique<GtkInstanceAssistant>(pAssistant, this, true);
}
virtual std::unique_ptr<weld::Dialog> weld_dialog(const OUString &id) override
{
GtkWindow* pDialog = GTK_WINDOW(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pDialog)
return nullptr;
if (m_pParentWidget)
gtk_window_set_transient_for(pDialog, GTK_WINDOW(widget_get_toplevel(m_pParentWidget)));
return std::make_unique<GtkInstanceDialog>(pDialog, this, true);
}
virtual std::unique_ptr<weld::Window> create_screenshot_window() override
{
GtkWidget* pTopLevel = nullptr;
for (GSList* l = m_pObjectList; l; l = g_slist_next(l))
{
GObject* pObj = static_cast<GObject*>(l->data);
if (!GTK_IS_WIDGET(pObj) || gtk_widget_get_parent(GTK_WIDGET(pObj)))
continue;
if (!pTopLevel)
pTopLevel = GTK_WIDGET(pObj);
else if (GTK_IS_WINDOW(pObj))
pTopLevel = GTK_WIDGET(pObj);
}
if (!pTopLevel)
return nullptr;
GtkWindow* pDialog;
if (GTK_IS_WINDOW(pTopLevel))
pDialog = GTK_WINDOW(pTopLevel);
else
{
pDialog = GTK_WINDOW(gtk_dialog_new());
::set_help_id(GTK_WIDGET(pDialog), ::get_help_id(pTopLevel));
GtkWidget* pContentArea = gtk_dialog_get_content_area(GTK_DIALOG(pDialog));
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_add(GTK_CONTAINER(pContentArea), pTopLevel);
gtk_widget_show_all(pTopLevel);
#else
gtk_box_append(GTK_BOX(pContentArea), pTopLevel);
gtk_widget_show(pTopLevel);
#endif
}
if (m_pParentWidget)
gtk_window_set_transient_for(pDialog, GTK_WINDOW(widget_get_toplevel(m_pParentWidget)));
return std::make_unique<GtkInstanceDialog>(pDialog, this, true);
}
virtual std::unique_ptr<weld::Widget> weld_widget(const OUString &id) override
{
GtkWidget* pWidget = GTK_WIDGET(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pWidget)
return nullptr;
auto_add_parentless_widgets_to_container(pWidget);
return std::make_unique<GtkInstanceWidget>(pWidget, this, false);
}
virtual std::unique_ptr<weld::Container> weld_container(const OUString &id) override
{
#if !GTK_CHECK_VERSION(4, 0, 0)
GtkContainer* pContainer = GTK_CONTAINER(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
#else
GtkWidget* pContainer = GTK_WIDGET(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
#endif
if (!pContainer)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pContainer));
return std::make_unique<GtkInstanceContainer>(pContainer, this, false);
}
virtual std::unique_ptr<weld::Box> weld_box(const OUString &id) override
{
GtkBox* pBox = GTK_BOX(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pBox)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pBox));
return std::make_unique<GtkInstanceBox>(pBox, this, false);
}
virtual std::unique_ptr<weld::Paned> weld_paned(const OUString &id) override
{
GtkPaned* pPaned = GTK_PANED(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pPaned)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pPaned));
return std::make_unique<GtkInstancePaned>(pPaned, this, false);
}
virtual std::unique_ptr<weld::Frame> weld_frame(const OUString &id) override
{
GtkFrame* pFrame = GTK_FRAME(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pFrame)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pFrame));
return std::make_unique<GtkInstanceFrame>(pFrame, this, false);
}
virtual std::unique_ptr<weld::ScrolledWindow> weld_scrolled_window(const OUString &id, bool bUserManagedScrolling = false) override
{
GtkScrolledWindow* pScrolledWindow = GTK_SCROLLED_WINDOW(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pScrolledWindow)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pScrolledWindow));
return std::make_unique<GtkInstanceScrolledWindow>(pScrolledWindow, this, false, bUserManagedScrolling);
}
virtual std::unique_ptr<weld::Notebook> weld_notebook(const OUString &id) override
{
GtkNotebook* pNotebook = GTK_NOTEBOOK(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pNotebook)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pNotebook));
return std::make_unique<GtkInstanceNotebook>(pNotebook, this, false);
}
virtual std::unique_ptr<weld::Button> weld_button(const OUString &id) override
{
GtkButton* pButton = GTK_BUTTON(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pButton)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pButton));
return std::make_unique<GtkInstanceButton>(pButton, this, false);
}
virtual std::unique_ptr<weld::MenuButton> weld_menu_button(const OUString &id) override
{
GtkMenuButton* pButton = GTK_MENU_BUTTON(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pButton)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pButton));
return std::make_unique<GtkInstanceMenuButton>(pButton, nullptr, this, false);
}
virtual std::unique_ptr<weld::MenuToggleButton> weld_menu_toggle_button(const OUString &id) override
{
GtkMenuButton* pButton = GTK_MENU_BUTTON(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pButton)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pButton));
// gtk doesn't come with exactly the same concept
GtkBuilder* pMenuToggleButton = makeMenuToggleButtonBuilder();
return std::make_unique<GtkInstanceMenuToggleButton>(pMenuToggleButton, pButton, this, false);
}
virtual std::unique_ptr<weld::LinkButton> weld_link_button(const OUString &id) override
{
GtkLinkButton* pButton = GTK_LINK_BUTTON(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pButton)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pButton));
return std::make_unique<GtkInstanceLinkButton>(pButton, this, false);
}
virtual std::unique_ptr<weld::ToggleButton> weld_toggle_button(const OUString &id) override
{
GtkToggleButton* pToggleButton = GTK_TOGGLE_BUTTON(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pToggleButton)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pToggleButton));
return std::make_unique<GtkInstanceToggleButton>(pToggleButton, this, false);
}
virtual std::unique_ptr<weld::RadioButton> weld_radio_button(const OUString &id) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
GtkCheckButton* pRadioButton = GTK_CHECK_BUTTON(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
#else
GtkRadioButton* pRadioButton = GTK_RADIO_BUTTON(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
#endif
if (!pRadioButton)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pRadioButton));
return std::make_unique<GtkInstanceRadioButton>(pRadioButton, this, false);
}
virtual std::unique_ptr<weld::CheckButton> weld_check_button(const OUString &id) override
{
GtkCheckButton* pCheckButton = GTK_CHECK_BUTTON(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pCheckButton)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pCheckButton));
return std::make_unique<GtkInstanceCheckButton>(pCheckButton, this, false);
}
virtual std::unique_ptr<weld::Scale> weld_scale(const OUString &id) override
{
GtkScale* pScale = GTK_SCALE(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pScale)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pScale));
return std::make_unique<GtkInstanceScale>(pScale, this, false);
}
virtual std::unique_ptr<weld::ProgressBar> weld_progress_bar(const OUString &id) override
{
GtkProgressBar* pProgressBar = GTK_PROGRESS_BAR(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pProgressBar)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pProgressBar));
return std::make_unique<GtkInstanceProgressBar>(pProgressBar, this, false);
}
virtual std::unique_ptr<weld::Spinner> weld_spinner(const OUString &id) override
{
GtkSpinner* pSpinner = GTK_SPINNER(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pSpinner)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pSpinner));
return std::make_unique<GtkInstanceSpinner>(pSpinner, this, false);
}
virtual std::unique_ptr<weld::Image> weld_image(const OUString &id) override
{
GtkWidget* pWidget = GTK_WIDGET(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pWidget)
return nullptr;
if (GTK_IS_IMAGE(pWidget))
{
auto_add_parentless_widgets_to_container(pWidget);
return std::make_unique<GtkInstanceImage>(GTK_IMAGE(pWidget), this, false);
}
#if GTK_CHECK_VERSION(4, 0, 0)
if (GTK_IS_PICTURE(pWidget))
{
auto_add_parentless_widgets_to_container(pWidget);
return std::make_unique<GtkInstancePicture>(GTK_PICTURE(pWidget), this, false);
}
#endif
return nullptr;
}
virtual std::unique_ptr<weld::Calendar> weld_calendar(const OUString &id) override
{
GtkCalendar* pCalendar = GTK_CALENDAR(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pCalendar)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pCalendar));
return std::make_unique<GtkInstanceCalendar>(pCalendar, this, false);
}
virtual std::unique_ptr<weld::Entry> weld_entry(const OUString &id) override
{
GtkEntry* pEntry = GTK_ENTRY(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pEntry)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pEntry));
return std::make_unique<GtkInstanceEntry>(pEntry, this, false);
}
virtual std::unique_ptr<weld::SpinButton> weld_spin_button(const OUString &id) override
{
GtkSpinButton* pSpinButton = GTK_SPIN_BUTTON(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pSpinButton)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pSpinButton));
return std::make_unique<GtkInstanceSpinButton>(pSpinButton, this, false);
}
virtual std::unique_ptr<weld::MetricSpinButton> weld_metric_spin_button(const OUString& id, FieldUnit eUnit) override
{
return std::make_unique<weld::MetricSpinButton>(weld_spin_button(id), eUnit);
}
virtual std::unique_ptr<weld::FormattedSpinButton> weld_formatted_spin_button(const OUString &id) override
{
GtkSpinButton* pSpinButton = GTK_SPIN_BUTTON(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pSpinButton)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pSpinButton));
return std::make_unique<GtkInstanceFormattedSpinButton>(pSpinButton, this, false);
}
virtual std::unique_ptr<weld::ComboBox> weld_combo_box(const OUString &id) override
{
GtkComboBox* pComboBox = GTK_COMBO_BOX(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pComboBox)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pComboBox));
#if GTK_CHECK_VERSION(4, 0, 0)
return std::make_unique<GtkInstanceComboBox>(pComboBox, this, false);
#else
/* we replace GtkComboBox because of difficulties with too tall menus
1) https://gitlab.gnome.org/GNOME/gtk/issues/1910
has_entry long menus take forever to appear (tdf#125388)
on measuring each row, the GtkComboBox GtkTreeMenu will call
its area_apply_attributes_cb function on the row, but that calls
gtk_tree_menu_get_path_item which then loops through each child of the
menu looking for the widget of the row, so performance drops to useless.
All area_apply_attributes_cb does it set menu item sensitivity, so block it from running
with fragile hackery which assumes that the unwanted callback is the only one with a
2) https://gitlab.gnome.org/GNOME/gtk/issues/94
when a super tall combobox menu is activated, and the selected
entry is sufficiently far down the list, then the menu doesn't
appear under wayland
3) https://gitlab.gnome.org/GNOME/gtk/issues/310
no typeahead support
4) we want to be able to control the width of the button, but have a drop down menu which
is not limited to the width of the button
5) https://bugs.documentfoundation.org/show_bug.cgi?id=131120
super tall menu doesn't appear under X sometimes
*/
GtkBuilder* pComboBuilder = makeComboBoxBuilder();
return std::make_unique<GtkInstanceComboBox>(pComboBuilder, pComboBox, this, false);
#endif
}
virtual std::unique_ptr<weld::TreeView> weld_tree_view(const OUString &id) override
{
GtkTreeView* pTreeView = GTK_TREE_VIEW(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pTreeView)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pTreeView));
return std::make_unique<GtkInstanceTreeView>(pTreeView, this, false);
}
virtual std::unique_ptr<weld::IconView> weld_icon_view(const OUString &id) override
{
GtkIconView* pIconView = GTK_ICON_VIEW(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pIconView)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pIconView));
return std::make_unique<GtkInstanceIconView>(pIconView, this, false);
}
virtual std::unique_ptr<weld::EntryTreeView> weld_entry_tree_view(const OUString& containerid, const OUString& entryid, const OUString& treeviewid) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
GtkWidget* pContainer = GTK_WIDGET(gtk_builder_get_object(m_pBuilder, OUStringToOString(containerid, RTL_TEXTENCODING_UTF8).getStr()));
#else
GtkContainer* pContainer = GTK_CONTAINER(gtk_builder_get_object(m_pBuilder, OUStringToOString(containerid, RTL_TEXTENCODING_UTF8).getStr()));
#endif
if (!pContainer)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pContainer));
return std::make_unique<GtkInstanceEntryTreeView>(pContainer, this, false,
weld_entry(entryid),
weld_tree_view(treeviewid));
}
virtual std::unique_ptr<weld::Label> weld_label(const OUString &id) override
{
GtkLabel* pLabel = GTK_LABEL(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pLabel)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pLabel));
return std::make_unique<GtkInstanceLabel>(pLabel, this, false);
}
virtual std::unique_ptr<weld::TextView> weld_text_view(const OUString &id) override
{
GtkTextView* pTextView = GTK_TEXT_VIEW(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pTextView)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pTextView));
return std::make_unique<GtkInstanceTextView>(pTextView, this, false);
}
virtual std::unique_ptr<weld::Expander> weld_expander(const OUString &id) override
{
GtkExpander* pExpander = GTK_EXPANDER(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pExpander)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pExpander));
return std::make_unique<GtkInstanceExpander>(pExpander, this, false);
}
virtual std::unique_ptr<weld::DrawingArea> weld_drawing_area(const OUString &id, const a11yref& rA11y,
FactoryFunction /*pUITestFactoryFunction*/, void* /*pUserData*/) override
{
GtkDrawingArea* pDrawingArea = GTK_DRAWING_AREA(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pDrawingArea)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pDrawingArea));
return std::make_unique<GtkInstanceDrawingArea>(pDrawingArea, this, rA11y, false);
}
virtual std::unique_ptr<weld::Menu> weld_menu(const OUString &id) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
GtkPopoverMenu* pMenu = GTK_POPOVER_MENU(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
#else
GtkMenu* pMenu = GTK_MENU(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
#endif
if (!pMenu)
return nullptr;
return std::make_unique<GtkInstanceMenu>(pMenu, true);
}
virtual std::unique_ptr<weld::Popover> weld_popover(const OUString &id) override
{
GtkPopover* pPopover = GTK_POPOVER(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pPopover)
return nullptr;
#if GTK_CHECK_VERSION(4, 0, 0)
return std::make_unique<GtkInstancePopover>(pPopover, this, false);
#else
return std::make_unique<GtkInstancePopover>(pPopover, this, true);
#endif
}
virtual std::unique_ptr<weld::Toolbar> weld_toolbar(const OUString &id) override
{
#if GTK_CHECK_VERSION(4, 0, 0)
GtkBox* pToolbar = GTK_BOX(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
#else
GtkToolbar* pToolbar = GTK_TOOLBAR(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
#endif
if (!pToolbar)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pToolbar));
return std::make_unique<GtkInstanceToolbar>(pToolbar, this, false);
}
virtual std::unique_ptr<weld::Scrollbar> weld_scrollbar(const OUString &id) override
{
GtkScrollbar* pScrollbar = GTK_SCROLLBAR(gtk_builder_get_object(m_pBuilder, OUStringToOString(id, RTL_TEXTENCODING_UTF8).getStr()));
if (!pScrollbar)
return nullptr;
auto_add_parentless_widgets_to_container(GTK_WIDGET(pScrollbar));
return std::make_unique<GtkInstanceScrollbar>(pScrollbar, this, false);
}
virtual std::unique_ptr<weld::SizeGroup> create_size_group() override
{
return std::make_unique<GtkInstanceSizeGroup>();
}
};
}
void GtkInstanceWindow::help()
{
//show help for widget with keyboard focus
GtkWidget* pWidget = gtk_window_get_focus(m_pWindow);
if (!pWidget)
pWidget = GTK_WIDGET(m_pWindow);
OUString sHelpId = ::get_help_id(pWidget);
while (sHelpId.isEmpty())
{
pWidget = gtk_widget_get_parent(pWidget);
if (!pWidget)
break;
sHelpId = ::get_help_id(pWidget);
}
std::unique_ptr<weld::Widget> xTemp(pWidget != m_pWidget ? new GtkInstanceWidget(pWidget, m_pBuilder, false) : nullptr);
weld::Widget* pSource = xTemp ? xTemp.get() : this;
bool bRunNormalHelpRequest = !m_aHelpRequestHdl.IsSet() || m_aHelpRequestHdl.Call(*pSource);
Help* pHelp = bRunNormalHelpRequest ? Application::GetHelp() : nullptr;
if (!pHelp)
return;
#if !GTK_CHECK_VERSION(4, 0, 0)
// tdf#126007, there's a nice fallback route for offline help where
// the current page of a notebook will get checked when the help
// button is pressed and there was no help for the dialog found.
//
// But for online help that route doesn't get taken, so bodge this here
// by using the page help id if available and if the help button itself
// was the original id
if (m_pBuilder && sHelpId.endsWith("/help"))
{
OUString sPageId = m_pBuilder->get_current_page_help_id();
if (!sPageId.isEmpty())
sHelpId = sPageId;
else
{
// tdf#129068 likewise the help for the wrapping dialog is less
// helpful than the help for the content area could be
GtkContainer* pContainer = nullptr;
if (GTK_IS_DIALOG(m_pWindow))
pContainer = GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(m_pWindow)));
else if (GTK_IS_ASSISTANT(m_pWindow))
{
GtkAssistant* pAssistant = GTK_ASSISTANT(m_pWindow);
pContainer = GTK_CONTAINER(gtk_assistant_get_nth_page(pAssistant, gtk_assistant_get_current_page(pAssistant)));
}
if (pContainer)
{
GtkWidget* pContentWidget = widget_get_first_child(GTK_WIDGET(pContainer));
if (pContentWidget)
sHelpId = ::get_help_id(pContentWidget);
}
}
}
#endif
pHelp->Start(sHelpId, pSource);
}
//iterate upwards through the hierarchy from this widgets through its parents
//calling func with their helpid until func returns true or we run out of parents
void GtkInstanceWidget::help_hierarchy_foreach(const std::function<bool(const OUString&)>& func)
{
GtkWidget* pParent = m_pWidget;
while ((pParent = gtk_widget_get_parent(pParent)))
{
if (func(::get_help_id(pParent)))
return;
}
}
std::unique_ptr<weld::Builder> GtkInstance::CreateBuilder(weld::Widget* pParent, const OUString& rUIRoot, const OUString& rUIFile)
{
GtkInstanceWidget* pParentWidget = dynamic_cast<GtkInstanceWidget*>(pParent);
GtkWidget* pBuilderParent = pParentWidget ? pParentWidget->getWidget() : nullptr;
return std::make_unique<GtkInstanceBuilder>(pBuilderParent, rUIRoot, rUIFile, nullptr, true);
}
#if !GTK_CHECK_VERSION(4, 0, 0)
// tdf#135965 for the case of native widgets inside a GtkSalFrame and F1 pressed, run help
// on gtk widget help ids until we hit a vcl parent and then use vcl window help ids
gboolean GtkSalFrame::NativeWidgetHelpPressed(GtkAccelGroup*, GObject*, guint, GdkModifierType, gpointer pFrame)
{
Help* pHelp = Application::GetHelp();
if (!pHelp)
return true;
GtkWindow* pWindow = static_cast<GtkWindow*>(pFrame);
vcl::Window* pChildWindow = nullptr;
//show help for widget with keyboard focus
GtkWidget* pWidget = gtk_window_get_focus(pWindow);
if (!pWidget)
pWidget = GTK_WIDGET(pWindow);
OUString sHelpId = ::get_help_id(pWidget);
while (sHelpId.isEmpty())
{
pWidget = gtk_widget_get_parent(pWidget);
if (!pWidget)
break;
pChildWindow = static_cast<vcl::Window*>(g_object_get_data(G_OBJECT(pWidget), "InterimWindowGlue"));
if (pChildWindow)
{
sHelpId = pChildWindow->GetHelpId();
break;
}
sHelpId = ::get_help_id(pWidget);
}
if (pChildWindow)
{
while (sHelpId.isEmpty())
{
pChildWindow = pChildWindow->GetParent();
if (!pChildWindow)
break;
sHelpId = pChildWindow->GetHelpId();
}
if (!pChildWindow)
return true;
pHelp->Start(sHelpId, pChildWindow);
return true;
}
if (!pWidget)
return true;
std::unique_ptr<weld::Widget> xTemp(new GtkInstanceWidget(pWidget, nullptr, false));
pHelp->Start(sHelpId, xTemp.get());
return true;
}
#endif
std::unique_ptr<weld::Builder> GtkInstance::CreateInterimBuilder(vcl::Window* pParent, const OUString& rUIRoot, const OUString& rUIFile,
bool bAllowCycleFocusOut, sal_uInt64)
{
// Create a foreign window which we know is a GtkGrid and make the native widgets a child of that, so we can
// support GtkWidgets within a vcl::Window
SystemWindowData winData = {};
winData.bClipUsingNativeWidget = true;
auto xEmbedWindow = VclPtr<SystemChildWindow>::Create(pParent, 0, &winData, false);
xEmbedWindow->Show(true, ShowFlags::NoActivate);
xEmbedWindow->set_expand(true);
const SystemEnvData* pEnvData = xEmbedWindow->GetSystemData();
if (!pEnvData)
return nullptr;
GtkWidget *pWindow = static_cast<GtkWidget*>(pEnvData->pWidget);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_show_all(pWindow);
#else
gtk_widget_show(pWindow);
#endif
// build the widget tree as a child of the GtkEventBox GtkGrid parent
return std::make_unique<GtkInstanceBuilder>(pWindow, rUIRoot, rUIFile, xEmbedWindow.get(), bAllowCycleFocusOut);
}
weld::MessageDialog* GtkInstance::CreateMessageDialog(weld::Widget* pParent, VclMessageType eMessageType, VclButtonsType eButtonsType, const OUString &rPrimaryMessage)
{
GtkInstanceWidget* pParentInstance = dynamic_cast<GtkInstanceWidget*>(pParent);
GtkWindow* pParentWindow = pParentInstance ? pParentInstance->getWindow() : nullptr;
GtkMessageDialog* pMessageDialog = GTK_MESSAGE_DIALOG(gtk_message_dialog_new(pParentWindow, GTK_DIALOG_MODAL,
VclToGtk(eMessageType), VclToGtk(eButtonsType), "%s",
OUStringToOString(rPrimaryMessage, RTL_TEXTENCODING_UTF8).getStr()));
return new GtkInstanceMessageDialog(pMessageDialog, nullptr, true);
}
weld::Window* GtkInstance::GetFrameWeld(const css::uno::Reference<css::awt::XWindow>& rWindow)
{
if (SalGtkXWindow* pGtkXWindow = dynamic_cast<SalGtkXWindow*>(rWindow.get()))
return pGtkXWindow->getFrameWeld();
return SalInstance::GetFrameWeld(rWindow);
}
weld::Window* GtkSalFrame::GetFrameWeld() const
{
if (!m_xFrameWeld)
m_xFrameWeld.reset(new GtkInstanceWindow(GTK_WINDOW(widget_get_toplevel(getWindow())), nullptr, false));
return m_xFrameWeld.get();
}
void* GtkInstance::CreateGStreamerSink(const SystemChildWindow *pWindow)
{
#if ENABLE_GSTREAMER_1_0
auto aSymbol = gstElementFactoryNameSymbol();
if (!aSymbol)
return nullptr;
const SystemEnvData* pEnvData = pWindow->GetSystemData();
if (!pEnvData)
return nullptr;
GstElement* pVideosink = aSymbol("gtksink", "gtksink");
if (!pVideosink)
return nullptr;
GtkWidget *pGstWidget;
g_object_get(pVideosink, "widget", &pGstWidget, nullptr);
gtk_widget_set_vexpand(pGstWidget, true);
gtk_widget_set_hexpand(pGstWidget, true);
GtkWidget *pParent = static_cast<GtkWidget*>(pEnvData->pWidget);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_container_add(GTK_CONTAINER(pParent), pGstWidget);
#endif
g_object_unref(pGstWidget);
#if !GTK_CHECK_VERSION(4, 0, 0)
gtk_widget_show_all(pParent);
#else
gtk_widget_show(pParent);
#endif
return pVideosink;
#else
(void)pWindow;
return nullptr;
#endif
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|