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
|
/* GTK - The GIMP Toolkit
* Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <cairo-gobject.h>
#include "gtkanimationdescription.h"
#include "gtk9slice.h"
#include "gtkgradient.h"
#include "gtkthemingengine.h"
#include "gtkstyleprovider.h"
#include "gtkcssprovider.h"
#include "gtkstylecontextprivate.h"
#include "gtkprivate.h"
/**
* SECTION:gtkcssprovider
* @Short_description: CSS-like styling for widgets
* @Title: GtkCssProvider
* @See_also: #GtkStyleContext, #GtkStyleProvider
*
* GtkCssProvider is an object implementing the #GtkStyleProvider interface.
* It is able to parse <ulink url="http://www.w3.org/TR/CSS2">CSS</ulink>-like
* input in order to style widgets.
*
* <refsect2 id="gtkcssprovider-files">
* <title>Default files</title>
* <para>
* An application can cause GTK+ to parse a specific CSS style sheet by
* calling gtk_css_provider_load_from_file() and adding the provider with
* gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen().
* In addition, certain files will be read when GTK+ is initialized. First,
* the file <filename><envar>$XDG_CONFIG_HOME</envar>/gtk-3.0/gtk.css</filename>
* is loaded if it exists. Then, GTK+ tries to load
* <filename><envar>$HOME</envar>/.themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
* falling back to
* <filename><replaceable>datadir</replaceable>/share/themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
* where <replaceable>theme-name</replaceable> is the name of the current theme
* (see the #GtkSettings:gtk-theme-name setting) and <replaceable>datadir</replaceable>
* is the prefix configured when GTK+ was compiled, unless overridden by the
* <envar>GTK_DATA_PREFIX</envar> environment variable.
* </para>
* </refsect2>
* <refsect2 id="gtkcssprovider-stylesheets">
* <title>Style sheets</title>
* <para>
* The basic structure of the style sheets understood by this provider is
* a series of statements, which are either rule sets or '@-rules', separated
* by whitespace.
* </para>
* <para>
* A rule set consists of a selector and a declaration block, which is
* a series of declarations enclosed in curly braces ({ and }). The
* declarations are separated by semicolons (;). Multiple selectors can
* share the same declaration block, by putting all the separators in
* front of the block, separated by commas.
* </para>
* <example><title>A rule set with two selectors</title>
* <programlisting language="text">
* GtkButton, GtkEntry {
* color: #ff00ea;
* font: Comic Sans 12
* }
* </programlisting>
* </example>
* </refsect2>
* <refsect2 id="gtkcssprovider-selectors">
* <title>Selectors</title>
* <para>
* Selectors work very similar to the way they do in CSS, with widget class
* names taking the role of element names, and widget names taking the role
* of IDs. When used in a selector, widget names must be prefixed with a
* '#' character. The '*' character represents the so-called universal
* selector, which matches any widget.
* </para>
* <para>
* To express more complicated situations, selectors can be combined in
* various ways:
* <itemizedlist>
* <listitem><para>To require that a widget satisfies several conditions,
* combine several selectors into one by concatenating them. E.g.
* <literal>GtkButton#button1</literal> matches a GtkButton widget
* with the name button1.</para></listitem>
* <listitem><para>To only match a widget when it occurs inside some other
* widget, write the two selectors after each other, separated by whitespace.
* E.g. <literal>GtkToolBar GtkButton</literal> matches GtkButton widgets
* that occur inside a GtkToolBar.</para></listitem>
* <listitem><para>In the previous example, the GtkButton is matched even
* if it occurs deeply nested inside the toolbar. To restrict the match
* to direct children of the parent widget, insert a '>' character between
* the two selectors. E.g. <literal>GtkNotebook > GtkLabel</literal> matches
* GtkLabel widgets that are direct children of a GtkNotebook.</para></listitem>
* </itemizedlist>
* </para>
* <example>
* <title>Widget classes and names in selectors</title>
* <programlisting language="text">
* /* Theme labels that are descendants of a window */
* GtkWindow GtkLabel {
* background-color: #898989
* }
*
* /* Theme notebooks, and anything that's within these */
* GtkNotebook {
* background-color: #a939f0
* }
*
* /* Theme combo boxes, and entries that
* are direct children of a notebook */
* GtkComboBox,
* GtkNotebook > GtkEntry {
* color: @fg_color;
* background-color: #1209a2
* }
*
* /* Theme any widget within a GtkBin */
* GtkBin * {
* font-name: Sans 20
* }
*
* /* Theme a label named title-label */
* GtkLabel#title-label {
* font-name: Sans 15
* }
*
* /* Theme any widget named main-entry */
* #main-entry {
* background-color: #f0a810
* }
* </programlisting>
* </example>
* <para>
* Widgets may also define style classes, which can be used for matching.
* When used in a selector, style classes must be prefixed with a '.'
* character.
* </para>
* <para>
* Refer to the documentation of individual widgets to learn which
* style classes they define and see <xref linkend="gtkstylecontext-classes"/>
* for a list of all style classes used by GTK+ widgets.
* </para>
* <para>
* Note that there is some ambiguity in the selector syntax when it comes
* to differentiation widget class names from regions. GTK+ currently treats
* a string as a widget class name if it contains any uppercase characters
* (which should work for more widgets with names like GtkLabel).
* </para>
* <example>
* <title>Style classes in selectors</title>
* <programlisting language="text">
* /* Theme all widgets defining the class entry */
* .entry {
* color: #39f1f9;
* }
*
* /* Theme spinbuttons' entry */
* GtkSpinButton.entry {
* color: #900185
* }
* </programlisting>
* </example>
* <para>
* In complicated widgets like e.g. a GtkNotebook, it may be desirable
* to style different parts of the widget differently. To make this
* possible, container widgets may define regions, whose names
* may be used for matching in selectors.
* </para>
* <para>
* Some containers allow to further differentiate between regions by
* applying so-called pseudo-classes to the region. For example, the
* tab region in GtkNotebook allows to single out the first or last
* tab by using the :first-child or :last-child pseudo-class.
* When used in selectors, pseudo-classes must be prefixed with a
* ':' character.
* </para>
* <para>
* Refer to the documentation of individual widgets to learn which
* regions and pseudo-classes they define and see
* <xref linkend="gtkstylecontext-classes"/> for a list of all regions
* used by GTK+ widgets.
* </para>
* <example>
* <title>Regions in selectors</title>
* <programlisting language="text">
* /* Theme any label within a notebook */
* GtkNotebook GtkLabel {
* color: #f90192;
* }
*
* /* Theme labels within notebook tabs */
* GtkNotebook tab GtkLabel {
* color: #703910;
* }
*
* /* Theme labels in the any first notebook
* tab, both selectors are equivalent */
* GtkNotebook tab:nth-child(first) GtkLabel,
* GtkNotebook tab:first-child GtkLabel {
* color: #89d012;
* }
* </programlisting>
* </example>
* <para>
* Another use of pseudo-classes is to match widgets depending on their
* state. This is conceptually similar to the :hover, :active or :focus
* pseudo-classes in CSS. The available pseudo-classes for widget states
* are :active, :prelight (or :hover), :insensitive, :selected, :focused
* and :inconsistent.
* </para>
* <example>
* <title>Styling specific widget states</title>
* <programlisting language="text">
* /* Theme active (pressed) buttons */
* GtkButton:active {
* background-color: #0274d9;
* }
*
* /* Theme buttons with the mouse pointer on it,
* both are equivalent */
* GtkButton:hover,
* GtkButton:prelight {
* background-color: #3085a9;
* }
*
* /* Theme insensitive widgets, both are equivalent */
* :insensitive,
* *:insensitive {
* background-color: #320a91;
* }
*
* /* Theme selection colors in entries */
* GtkEntry:selected {
* background-color: #56f9a0;
* }
*
* /* Theme focused labels */
* GtkLabel:focused {
* background-color: #b4940f;
* }
*
* /* Theme inconsistent checkbuttons */
* GtkCheckButton:inconsistent {
* background-color: #20395a;
* }
* </programlisting>
* </example>
* <para>
* Widget state pseudoclasses may only apply to the last element
* in a selector.
* </para>
* <para>
* To determine the effective style for a widget, all the matching rule
* sets are merged. As in CSS, rules apply by specificity, so the rules
* whose selectors more closely match a widget path will take precedence
* over the others.
* </para>
* </refsect2>
* <refsect2 id="gtkcssprovider-rules">
* <title>@ Rules</title>
* <para>
* GTK+'s CSS supports the @import rule, in order to load another
* CSS style sheet in addition to the currently parsed one.
* </para>
* <example>
* <title>Using the @import rule</title>
* <programlisting language="text">
* @import url ("path/to/common.css");
* </programlisting>
* </example>
* <para>
* GTK+ also supports an additional @define-color rule, in order
* to define a color name which may be used instead of color numeric
* representations. Also see the #GtkSettings:gtk-color-scheme setting
* for a way to override the values of these named colors.
* </para>
* <example>
* <title>Defining colors</title>
* <programlisting language="text">
* @define-color bg_color #f9a039;
*
* * {
* background-color: @bg_color;
* }
* </programlisting>
* </example>
* </refsect2>
* <refsect2 id="gtkcssprovider-symbolic-colors">
* <title>Symbolic colors</title>
* <para>
* Besides being able to define color names, the CSS parser is also able
* to read different color expressions, which can also be nested, providing
* a rich language to define colors which are derived from a set of base
* colors.
* </para>
* <example>
* <title>Using symbolic colors</title>
* <programlisting language="text">
* @define-color entry-color shade (@bg_color, 0.7);
*
* GtkEntry {
* background-color: @entry-color;
* }
*
* GtkEntry:focused {
* background-color: mix (@entry-color,
* shade (#fff, 0.5),
* 0.8);
* }
* </programlisting>
* </example>
* <para>
* The various ways to express colors in GTK+ CSS are:
* </para>
* <informaltable>
* <tgroup cols="3">
* <thead>
* <row>
* <entry>Syntax</entry>
* <entry>Explanation</entry>
* <entry>Examples</entry>
* </row>
* </thead>
* <tbody>
* <row>
* <entry>rgb(@r, @g, @b)</entry>
* <entry>An opaque color; @r, @g, @b can be either integers between
* 0 and 255 or percentages</entry>
* <entry><literallayout>rgb(128, 10, 54)
* rgb(20%, 30%, 0%)</literallayout></entry>
* </row>
* <row>
* <entry>rgba(@r, @g, @b, @a)</entry>
* <entry>A translucent color; @r, @g, @b are as in the previous row,
* @a is a floating point number between 0 and 1</entry>
* <entry><literallayout>rgba(255, 255, 0, 0.5)</literallayout></entry>
* </row>
* <row>
* <entry>#@xxyyzz</entry>
* <entry>An opaque color; @xx, @yy, @zz are hexadecimal numbers
* specifying @r, @g, @b variants with between 1 and 4
* hexadecimal digits per component are allowed</entry>
* <entry><literallayout>#ff12ab
* #f0c</literallayout></entry>
* </row>
* <row>
* <entry>@name</entry>
* <entry>Reference to a color that has been defined with
* @define-color
* </entry>
* <entry>@bg_color</entry>
* </row>
* <row>
* <entry>mix(@color1, @color2, @f)</entry>
* <entry>A linear combination of @color1 and @color2. @f is a
* floating point number between 0 and 1.</entry>
* <entry><literallayout>mix(#ff1e0a, @bg_color, 0.8)</literallayout></entry>
* </row>
* <row>
* <entry>shade(@color, @f)</entry>
* <entry>A lighter or darker variant of @color. @f is a
* floating point number.
* </entry>
* <entry>shade(@fg_color, 0.5)</entry>
* </row>
* <row>
* <entry>lighter(@color)</entry>
* <entry>A lighter variant of @color</entry>
* </row>
* <row>
* <entry>darker(@color)</entry>
* <entry>A darker variant of @color</entry>
* </row>
* </tbody>
* </tgroup>
* </informaltable>
* </refsect2>
* <refsect2 id="gtkcssprovider-gradients">
* <title>Gradients</title>
* <para>
* Linear or radial Gradients can be used as background images.
* </para>
* <para>
* A linear gradient along the line from (@start_x, @start_y) to
* (@end_x, @end_y) is specified using the syntax
* <literallayout>-gtk-gradient (linear,
* @start_x @start_y, @end_x @end_y,
* color-stop (@position, @color),
* ...)</literallayout>
* where @start_x and @end_x can be either a floating point number between
* 0 and 1 or one of the special values 'left', 'right' or 'center', @start_y
* and @end_y can be either a floating point number between 0 and 1 or one
* of the special values 'top', 'bottom' or 'center', @position is a floating
* point number between 0 and 1 and @color is a color expression (see above).
* The color-stop can be repeated multiple times to add more than one color
* stop. 'from (@color)' and 'to (@color)' can be used as abbreviations for
* color stops with position 0 and 1, respectively.
* </para>
* <example>
* <title>A linear gradient</title>
* <inlinegraphic fileref="gradient1.png" format="PNG"/>
* <para>This gradient was specified with
* <literallayout>-gtk-gradient (linear,
* left top, right bottom,
* from(@yellow), to(@blue))</literallayout></para>
* </example>
* <example>
* <title>Another linear gradient</title>
* <inlinegraphic fileref="gradient2.png" format="PNG"/>
* <para>This gradient was specified with
* <literallayout>-gtk-gradient (linear,
* 0 0, 0 1,
* color-stop(0, @yellow),
* color-stop(0.2, @blue),
* color-stop(1, #0f0))</literallayout></para>
* </example>
* <para>
* A radial gradient along the two circles defined by (@start_x, @start_y,
* @start_radius) and (@end_x, @end_y, @end_radius) is specified using the
* syntax
* <literallayout>-gtk-gradient (radial,
* @start_x @start_y, @start_radius,
* @end_x @end_y, @end_radius,
* color-stop (@position, @color),
* ...)</literallayout>
* where @start_radius and @end_radius are floating point numbers and
* the other parameters are as before.
* </para>
* <example>
* <title>A radial gradient</title>
* <inlinegraphic fileref="gradient3.png" format="PNG"/>
* <para>This gradient was specified with
* <literallayout>-gtk-gradient (radial,
* center center, 0,
* center center, 1,
* from(@yellow), to(@green))</literallayout></para>
* </example>
* <example>
* <title>Another radial gradient</title>
* <inlinegraphic fileref="gradient4.png" format="PNG"/>
* <para>This gradient was specified with
* <literallayout>-gtk-gradient (radial,
* 0.4 0.4, 0.1,
* 0.6 0.6, 0.7,
* color-stop (0, #f00),
* color-stop (0.1, #a0f),
* color-stop (0.2, @yellow),
* color-stop (1, @green))</literallayout></para>
* </example>
* </refsect2>
* <refsect2 id="gtkcssprovider-slices">
* <title>Border images</title>
* <para>
* Images can be used in 'slices' for the purpose of creating scalable
* borders.
* </para>
* <inlinegraphic fileref="slices.png" format="PNG"/>
* <para>
* The syntax for specifying border images of this kind is:
* <literallayout>url(@path) @top @right @bottom @left [repeat|stretch]? [repeat|stretch]?</literallayout>
* The sizes of the 'cut off' portions are specified
* with the @top, @right, @bottom and @left parameters.
* The 'middle' sections can be repeated or stretched to create
* the desired effect, by adding the 'repeat' or 'stretch' options after
* the dimensions. If two options are specified, the first one affects
* the horizontal behaviour and the second one the vertical behaviour.
* If only one option is specified, it affects both.
* </para>
* <example>
* <title>A border image</title>
* <inlinegraphic fileref="border1.png" format="PNG"/>
* <para>This border image was specified with
* <literallayout>url("gradient1.png") 10 10 10 10</literallayout>
* </para>
* </example>
* <example>
* <title>A repeating border image</title>
* <inlinegraphic fileref="border2.png" format="PNG"/>
* <para>This border image was specified with
* <literallayout>url("gradient1.png") 10 10 10 10 repeat</literallayout>
* </para>
* </example>
* <example>
* <title>A stretched border image</title>
* <inlinegraphic fileref="border3.png" format="PNG"/>
* <para>This border image was specified with
* <literallayout>url("gradient1.png") 10 10 10 10 stretch</literallayout>
* </para>
* </example>
* </refsect2>
* <refsect2 id="gtkcssprovider-transitions">
* <para>Styles can specify transitions that will be used to create a gradual
* change in the appearance when a widget state changes. The following
* syntax is used to specify transitions:
* <literallayout>@duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?</literallayout>
* The @duration is the amount of time that the animation will take for
* a complete cycle from start to end. If the loop option is given, the
* animation will be repated until the state changes again.
* The option after the duration determines the transition function from a
* small set of predefined functions.
* <figure><title>Linear transition</title>
* <graphic fileref="linear.png" format="PNG"/>
* </figure>
* <figure><title>Ease transition</title>
* <graphic fileref="ease.png" format="PNG"/>
* </figure>
* <figure><title>Ease-in-out transition</title>
* <graphic fileref="ease-in-out.png" format="PNG"/>
* </figure>
* <figure><title>Ease-in transition</title>
* <graphic fileref="ease-in.png" format="PNG"/>
* </figure>
* <figure><title>Ease-out transition</title>
* <graphic fileref="ease-out.png" format="PNG"/>
* </figure>
* </para>
* </refsect2>
* <refsect2 id="gtkcssprovider-properties">
* <title>Supported properties</title>
* <para>
* Properties are the part that differ the most to common CSS,
* not all properties are supported (some are planned to be
* supported eventually, some others are meaningless or don't
* map intuitively in a widget based environment).
* </para>
* <para>
* There is also a difference in shorthand properties, for
* example in common CSS it is fine to define a font through
* the different @font-family, @font-style, @font-size
* properties, meanwhile in GTK+'s CSS only the canonical
* @font property is supported.
* </para>
* <para>
* The currently supported properties are:
* </para>
* <informaltable>
* <tgroup cols="4">
* <thead>
* <row>
* <entry>Property name</entry>
* <entry>Syntax</entry>
* <entry>Maps to</entry>
* <entry>Examples</entry>
* </row>
* </thead>
* <tbody>
* <row>
* <entry>engine</entry>
* <entry>engine-name</entry>
* <entry>#GtkThemingEngine</entry>
* <entry>engine: clearlooks;
* engine: none; /* use the default (i.e. builtin) engine) */ </entry>
* </row>
* <row>
* <entry>background-color</entry>
* <entry morerows="2">color (see above)</entry>
* <entry morerows="2">#GdkRGBA</entry>
* <entry morerows="2"><literallayout>background-color: #fff;
* color: &color1;
* background-color: shade (&color1, 0.5);
* color: mix (&color1, #f0f, 0.8);</literallayout>
* </entry>
* </row>
* <row>
* <entry>color</entry>
* </row>
* <row>
* <entry>border-color</entry>
* </row>
* <row>
* <entry>font</entry>
* <entry>@family [@style] [@size]</entry>
* <entry>#PangoFontDescription</entry>
* <entry>font: Sans 15;</entry>
* </row>
* <row>
* <entry>margin</entry>
* <entry morerows="1"><literallayout>@width
* @vertical_width @horizontal_width
* @top_width @horizontal_width @bottom_width
* @top_width @right_width @bottom_width @left_width</literallayout>
* </entry>
* <entry morerows="1">#GtkBorder</entry>
* <entry morerows="1"><literallayout>margin: 5;
* margin: 5 10;
* margin: 5 10 3;
* margin: 5 10 3 5;</literallayout>
* </entry>
* </row>
* <row>
* <entry>padding</entry>
* </row>
* <row>
* <entry>background-image</entry>
* <entry><literallayout>gradient (see above) or
* url(@path)</literallayout></entry>
* <entry>#cairo_pattern_t</entry>
* <entry><literallayout>-gtk-gradient (linear,
* left top, right top,
* from (#fff), to (#000));
* -gtk-gradient (linear, 0.0 0.5, 0.5 1.0,
* from (#fff),
* color-stop (0.5, #f00),
* to (#000));
* -gtk-gradient (radial,
* center center, 0.2,
* center center, 0.8,
* color-stop (0.0, #fff),
* color-stop (1.0, #000));
* url ('background.png');</literallayout>
* </entry>
* </row>
* <row>
* <entry>border-width</entry>
* <entry>integer</entry>
* <entry>#gint</entry>
* <entry>border-width: 5;</entry>
* </row>
* <row>
* <entry>border-radius</entry>
* <entry>integer</entry>
* <entry>#gint</entry>
* <entry>border-radius: 5;</entry>
* </row>
* <row>
* <entry>border-style</entry>
* <entry>[none|solid|inset|outset]</entry>
* <entry>#GtkBorderStyle</entry>
* <entry>border-style: solid;</entry>
* </row>
* <row>
* <entry>border-image</entry>
* <entry><literallayout>border image (see above)</literallayout></entry>
* <entry>internal use only</entry>
* <entry><literallayout>border-image: url("/path/to/image.png") 3 4 3 4 stretch;
* border-image: url("/path/to/image.png") 3 4 4 3 repeat stretch;</literallayout>
* </entry>
* </row>
* <row>
* <entry>transition</entry>
* <entry>transition (see above)</entry>
* <entry>internal use only</entry>
* <entry><literallayout>transition: 150ms ease-in-out;
* transition: 1s linear loop;</literallayout>
* </entry>
* </row>
* </tbody>
* </tgroup>
* </informaltable>
* <para>
* GtkThemingEngines can register their own, engine-specific style properties
* with the function gtk_theming_engine_register_property(). These properties
* can be set in CSS like other properties, using a name of the form
* <literallayout>-<replaceable>namespace</replaceable>-<replaceable>name</replaceable></literallayout>, where <replaceable>namespace</replaceable> is typically
* the name of the theming engine, and <replaceable>name</replaceable> is the
* name of the property. Style properties that have been registered by widgets
* using gtk_widget_class_install_style_property() can also be set in this
* way, using the widget class name for <replaceable>namespace</replaceable>.
* </para>
* <example>
* <title>Using engine-specific style properties</title>
* <programlisting>
* * {
* engine: clearlooks;
* border-radius: 4;
* -GtkPaned-handle-size: 6;
* -clearlooks-colorize-scrollbar: false;
* }
* </programlisting>
* </example>
* </refsect2>
*/
typedef struct GtkCssProviderPrivate GtkCssProviderPrivate;
typedef struct SelectorElement SelectorElement;
typedef struct SelectorPath SelectorPath;
typedef struct SelectorStyleInfo SelectorStyleInfo;
typedef enum SelectorElementType SelectorElementType;
typedef enum CombinatorType CombinatorType;
typedef enum ParserScope ParserScope;
typedef enum ParserSymbol ParserSymbol;
enum SelectorElementType {
SELECTOR_TYPE_NAME,
SELECTOR_NAME,
SELECTOR_GTYPE,
SELECTOR_REGION,
SELECTOR_CLASS,
SELECTOR_GLOB
};
enum CombinatorType {
COMBINATOR_DESCENDANT, /* No direct relation needed */
COMBINATOR_CHILD /* Direct child */
};
struct SelectorElement
{
SelectorElementType elem_type;
CombinatorType combinator;
union
{
GQuark name;
GType type;
struct
{
GQuark name;
GtkRegionFlags flags;
} region;
};
};
struct SelectorPath
{
GSList *elements;
GtkStateFlags state;
guint ref_count;
};
struct SelectorStyleInfo
{
SelectorPath *path;
GHashTable *style;
};
struct GtkCssProviderPrivate
{
GScanner *scanner;
gchar *filename;
const gchar *buffer;
const gchar *value_pos;
GHashTable *symbolic_colors;
GPtrArray *selectors_info;
/* Current parser state */
GSList *state;
GSList *cur_selectors;
GHashTable *cur_properties;
};
enum ParserScope {
SCOPE_SELECTOR,
SCOPE_PSEUDO_CLASS,
SCOPE_NTH_CHILD,
SCOPE_DECLARATION,
SCOPE_VALUE
};
/* Extend GtkStateType, since these
* values are also used as symbols
*/
enum ParserSymbol {
/* Scope: pseudo-class */
SYMBOL_NTH_CHILD = GTK_STATE_FOCUSED + 1,
SYMBOL_FIRST_CHILD,
SYMBOL_LAST_CHILD,
SYMBOL_SORTED_CHILD,
/* Scope: nth-child */
SYMBOL_NTH_CHILD_EVEN,
SYMBOL_NTH_CHILD_ODD,
SYMBOL_NTH_CHILD_FIRST,
SYMBOL_NTH_CHILD_LAST
};
static void gtk_css_provider_finalize (GObject *object);
static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
static void scanner_apply_scope (GScanner *scanner,
ParserScope scope);
static gboolean css_provider_parse_value (GtkCssProvider *css_provider,
const gchar *value_str,
GValue *value,
GError **error);
static gboolean gtk_css_provider_load_from_path_internal (GtkCssProvider *css_provider,
const gchar *path,
gboolean reset,
GError **error);
GQuark
gtk_css_provider_error_quark (void)
{
return g_quark_from_static_string ("gtk-css-provider-error-quark");
}
G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
gtk_css_style_provider_iface_init));
static void
gtk_css_provider_class_init (GtkCssProviderClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gtk_css_provider_finalize;
g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
}
static SelectorPath *
selector_path_new (void)
{
SelectorPath *path;
path = g_slice_new0 (SelectorPath);
path->ref_count = 1;
return path;
}
static SelectorPath *
selector_path_ref (SelectorPath *path)
{
path->ref_count++;
return path;
}
static void
selector_path_unref (SelectorPath *path)
{
path->ref_count--;
if (path->ref_count > 0)
return;
while (path->elements)
{
g_slice_free (SelectorElement, path->elements->data);
path->elements = g_slist_delete_link (path->elements, path->elements);
}
g_slice_free (SelectorPath, path);
}
static void
selector_path_prepend_type (SelectorPath *path,
const gchar *type_name)
{
SelectorElement *elem;
GType type;
elem = g_slice_new (SelectorElement);
elem->combinator = COMBINATOR_DESCENDANT;
type = g_type_from_name (type_name);
if (type == G_TYPE_INVALID)
{
elem->elem_type = SELECTOR_TYPE_NAME;
elem->name = g_quark_from_string (type_name);
}
else
{
elem->elem_type = SELECTOR_GTYPE;
elem->type = type;
}
path->elements = g_slist_prepend (path->elements, elem);
}
static void
selector_path_prepend_glob (SelectorPath *path)
{
SelectorElement *elem;
elem = g_slice_new (SelectorElement);
elem->elem_type = SELECTOR_GLOB;
elem->combinator = COMBINATOR_DESCENDANT;
path->elements = g_slist_prepend (path->elements, elem);
}
static void
selector_path_prepend_region (SelectorPath *path,
const gchar *name,
GtkRegionFlags flags)
{
SelectorElement *elem;
elem = g_slice_new (SelectorElement);
elem->combinator = COMBINATOR_DESCENDANT;
elem->elem_type = SELECTOR_REGION;
elem->region.name = g_quark_from_string (name);
elem->region.flags = flags;
path->elements = g_slist_prepend (path->elements, elem);
}
static void
selector_path_prepend_name (SelectorPath *path,
const gchar *name)
{
SelectorElement *elem;
elem = g_slice_new (SelectorElement);
elem->combinator = COMBINATOR_DESCENDANT;
elem->elem_type = SELECTOR_NAME;
elem->name = g_quark_from_string (name);
path->elements = g_slist_prepend (path->elements, elem);
}
static void
selector_path_prepend_class (SelectorPath *path,
const gchar *name)
{
SelectorElement *elem;
elem = g_slice_new (SelectorElement);
elem->combinator = COMBINATOR_DESCENDANT;
elem->elem_type = SELECTOR_CLASS;
elem->name = g_quark_from_string (name);
path->elements = g_slist_prepend (path->elements, elem);
}
static void
selector_path_prepend_combinator (SelectorPath *path,
CombinatorType combinator)
{
SelectorElement *elem;
g_assert (path->elements != NULL);
/* It is actually stored in the last element */
elem = path->elements->data;
elem->combinator = combinator;
}
static gint
selector_path_depth (SelectorPath *path)
{
return g_slist_length (path->elements);
}
static SelectorStyleInfo *
selector_style_info_new (SelectorPath *path)
{
SelectorStyleInfo *info;
info = g_slice_new0 (SelectorStyleInfo);
info->path = selector_path_ref (path);
return info;
}
static void
selector_style_info_free (SelectorStyleInfo *info)
{
if (info->style)
g_hash_table_unref (info->style);
if (info->path)
selector_path_unref (info->path);
g_slice_free (SelectorStyleInfo, info);
}
static void
selector_style_info_set_style (SelectorStyleInfo *info,
GHashTable *style)
{
if (info->style)
g_hash_table_unref (info->style);
if (style)
info->style = g_hash_table_ref (style);
else
info->style = NULL;
}
static GScanner *
create_scanner (void)
{
GScanner *scanner;
scanner = g_scanner_new (NULL);
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "active", GUINT_TO_POINTER (GTK_STATE_ACTIVE));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "prelight", GUINT_TO_POINTER (GTK_STATE_PRELIGHT));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "hover", GUINT_TO_POINTER (GTK_STATE_PRELIGHT));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "selected", GUINT_TO_POINTER (GTK_STATE_SELECTED));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "insensitive", GUINT_TO_POINTER (GTK_STATE_INSENSITIVE));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "inconsistent", GUINT_TO_POINTER (GTK_STATE_INCONSISTENT));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "focused", GUINT_TO_POINTER (GTK_STATE_FOCUSED));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "focus", GUINT_TO_POINTER (GTK_STATE_FOCUSED));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "nth-child", GUINT_TO_POINTER (SYMBOL_NTH_CHILD));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "first-child", GUINT_TO_POINTER (SYMBOL_FIRST_CHILD));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "last-child", GUINT_TO_POINTER (SYMBOL_LAST_CHILD));
g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "sorted", GUINT_TO_POINTER (SYMBOL_SORTED_CHILD));
g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "even", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_EVEN));
g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "odd", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_ODD));
g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "first", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_FIRST));
g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "last", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_LAST));
scanner_apply_scope (scanner, SCOPE_SELECTOR);
return scanner;
}
static void
gtk_css_provider_init (GtkCssProvider *css_provider)
{
GtkCssProviderPrivate *priv;
priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
GTK_TYPE_CSS_PROVIDER,
GtkCssProviderPrivate);
priv->selectors_info = g_ptr_array_new_with_free_func ((GDestroyNotify) selector_style_info_free);
priv->scanner = create_scanner ();
priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) gtk_symbolic_color_unref);
}
typedef struct ComparePathData ComparePathData;
struct ComparePathData
{
guint64 score;
SelectorPath *path;
GSList *iter;
};
static gboolean
compare_selector_element (GtkWidgetPath *path,
guint index,
SelectorElement *elem,
guint8 *score)
{
*score = 0;
if (elem->elem_type == SELECTOR_TYPE_NAME)
{
const gchar *type_name;
GType resolved_type;
/* Resolve the type name */
type_name = g_quark_to_string (elem->name);
resolved_type = g_type_from_name (type_name);
if (resolved_type == G_TYPE_INVALID)
{
/* Type couldn't be resolved, so the selector
* clearly doesn't affect the given widget path
*/
return FALSE;
}
elem->elem_type = SELECTOR_GTYPE;
elem->type = resolved_type;
}
if (elem->elem_type == SELECTOR_GTYPE)
{
GType type;
type = gtk_widget_path_iter_get_object_type (path, index);
if (!g_type_is_a (type, elem->type))
return FALSE;
if (type == elem->type)
*score |= 0xF;
else
{
GType parent = type;
*score = 0xE;
while ((parent = g_type_parent (parent)) != G_TYPE_INVALID)
{
if (parent == elem->type)
break;
*score -= 1;
if (*score == 1)
{
g_warning ("Hierarchy is higher than expected.");
break;
}
}
}
return TRUE;
}
else if (elem->elem_type == SELECTOR_REGION)
{
GtkRegionFlags flags;
if (!gtk_widget_path_iter_has_qregion (path, index,
elem->region.name,
&flags))
return FALSE;
if (elem->region.flags != 0 &&
(flags & elem->region.flags) == 0)
return FALSE;
*score = 0xF;
return TRUE;
}
else if (elem->elem_type == SELECTOR_GLOB)
{
/* Treat as lowest matching type */
*score = 1;
return TRUE;
}
else if (elem->elem_type == SELECTOR_NAME)
{
if (!gtk_widget_path_iter_has_qname (path, index, elem->name))
return FALSE;
*score = 0xF;
return TRUE;
}
else if (elem->elem_type == SELECTOR_CLASS)
{
if (!gtk_widget_path_iter_has_qclass (path, index, elem->name))
return FALSE;
*score = 0xF;
return TRUE;
}
return FALSE;
}
static guint64
compare_selector (GtkWidgetPath *path,
SelectorPath *selector)
{
GSList *elements = selector->elements;
gboolean match = TRUE, first = TRUE, first_match = FALSE;
guint64 score = 0;
gint i;
i = gtk_widget_path_length (path) - 1;
while (elements && match && i >= 0)
{
SelectorElement *elem;
guint8 elem_score;
elem = elements->data;
match = compare_selector_element (path, i, elem, &elem_score);
if (match && first)
first_match = TRUE;
/* Only move on to the next index if there is no match
* with the current element (whether to continue or not
* handled right after in the combinator check), or a
* GType or glob has just been matched.
*
* Region and widget names do not trigger this because
* the next element in the selector path could also be
* related to the same index.
*/
if (!match ||
(elem->elem_type == SELECTOR_GTYPE ||
elem->elem_type == SELECTOR_GLOB))
i--;
if (!match &&
elem->elem_type != SELECTOR_TYPE_NAME &&
elem->combinator == COMBINATOR_DESCENDANT)
{
/* With descendant combinators there may
* be intermediate chidren in the hierarchy
*/
match = TRUE;
}
else if (match)
elements = elements->next;
if (match)
{
/* Only 4 bits are actually used */
score <<= 4;
score |= elem_score;
}
first = FALSE;
}
/* If there are pending selector
* elements to compare, it's not
* a match.
*/
if (elements)
match = FALSE;
if (!match)
score = 0;
else if (first_match)
{
/* Assign more weight to these selectors
* that matched right from the first element.
*/
score <<= 4;
}
return score;
}
typedef struct StylePriorityInfo StylePriorityInfo;
struct StylePriorityInfo
{
guint64 score;
GHashTable *style;
GtkStateFlags state;
};
static GArray *
css_provider_get_selectors (GtkCssProvider *css_provider,
GtkWidgetPath *path)
{
GtkCssProviderPrivate *priv;
GArray *priority_info;
guint i, j;
priv = css_provider->priv;
priority_info = g_array_new (FALSE, FALSE, sizeof (StylePriorityInfo));
for (i = 0; i < priv->selectors_info->len; i++)
{
SelectorStyleInfo *info;
StylePriorityInfo new;
gboolean added = FALSE;
guint64 score;
info = g_ptr_array_index (priv->selectors_info, i);
score = compare_selector (path, info->path);
if (score <= 0)
continue;
new.score = score;
new.style = info->style;
new.state = info->path->state;
for (j = 0; j < priority_info->len; j++)
{
StylePriorityInfo *cur;
cur = &g_array_index (priority_info, StylePriorityInfo, j);
if (cur->score > new.score)
{
g_array_insert_val (priority_info, j, new);
added = TRUE;
break;
}
}
if (!added)
g_array_append_val (priority_info, new);
}
return priority_info;
}
static void
css_provider_dump_symbolic_colors (GtkCssProvider *css_provider,
GtkStyleProperties *props)
{
GtkCssProviderPrivate *priv;
GHashTableIter iter;
gpointer key, value;
priv = css_provider->priv;
g_hash_table_iter_init (&iter, priv->symbolic_colors);
while (g_hash_table_iter_next (&iter, &key, &value))
{
const gchar *name;
GtkSymbolicColor *color;
name = key;
color = value;
gtk_style_properties_map_color (props, name, color);
}
}
static GtkStyleProperties *
gtk_css_provider_get_style (GtkStyleProvider *provider,
GtkWidgetPath *path)
{
GtkCssProvider *css_provider;
GtkCssProviderPrivate *priv;
GtkStyleProperties *props;
GArray *priority_info;
guint i;
css_provider = GTK_CSS_PROVIDER (provider);
props = gtk_style_properties_new ();
priv = css_provider->priv;
css_provider_dump_symbolic_colors (css_provider, props);
priority_info = css_provider_get_selectors (css_provider, path);
for (i = 0; i < priority_info->len; i++)
{
StylePriorityInfo *info;
GHashTableIter iter;
gpointer key, value;
info = &g_array_index (priority_info, StylePriorityInfo, i);
g_hash_table_iter_init (&iter, info->style);
while (g_hash_table_iter_next (&iter, &key, &value))
{
gchar *prop = key;
/* Properties starting with '-' may be both widget style properties
* or custom properties from the theming engine, so check whether
* the type is registered or not.
*/
if (prop[0] == '-' &&
!gtk_style_properties_lookup_property (prop, NULL, NULL))
continue;
gtk_style_properties_set_property (props, key, info->state, value);
}
}
g_array_free (priority_info, TRUE);
return props;
}
static gboolean
gtk_css_provider_get_style_property (GtkStyleProvider *provider,
GtkWidgetPath *path,
GtkStateFlags state,
GParamSpec *pspec,
GValue *value)
{
GArray *priority_info;
gboolean found = FALSE;
gchar *prop_name;
gint i;
prop_name = g_strdup_printf ("-%s-%s",
g_type_name (pspec->owner_type),
pspec->name);
priority_info = css_provider_get_selectors (GTK_CSS_PROVIDER (provider), path);
for (i = priority_info->len - 1; i >= 0; i--)
{
StylePriorityInfo *info;
GValue *val;
info = &g_array_index (priority_info, StylePriorityInfo, i);
val = g_hash_table_lookup (info->style, prop_name);
if (val &&
(info->state == 0 ||
info->state == state ||
((info->state & state) != 0 &&
(info->state & ~(state)) == 0)))
{
const gchar *val_str;
val_str = g_value_get_string (val);
found = TRUE;
css_provider_parse_value (GTK_CSS_PROVIDER (provider), val_str, value, NULL);
break;
}
}
g_array_free (priority_info, TRUE);
g_free (prop_name);
return found;
}
static void
gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
{
iface->get_style = gtk_css_provider_get_style;
iface->get_style_property = gtk_css_provider_get_style_property;
}
static void
gtk_css_provider_finalize (GObject *object)
{
GtkCssProvider *css_provider;
GtkCssProviderPrivate *priv;
css_provider = GTK_CSS_PROVIDER (object);
priv = css_provider->priv;
g_scanner_destroy (priv->scanner);
g_free (priv->filename);
g_ptr_array_free (priv->selectors_info, TRUE);
g_slist_foreach (priv->cur_selectors, (GFunc) selector_path_unref, NULL);
g_slist_free (priv->cur_selectors);
if (priv->cur_properties)
g_hash_table_unref (priv->cur_properties);
if (priv->symbolic_colors)
g_hash_table_destroy (priv->symbolic_colors);
G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
}
/**
* gtk_css_provider_new:
*
* Returns a newly created #GtkCssProvider.
*
* Returns: A new #GtkCssProvider
**/
GtkCssProvider *
gtk_css_provider_new (void)
{
return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
}
static void
property_value_free (GValue *value)
{
if (G_IS_VALUE (value))
g_value_unset (value);
g_slice_free (GValue, value);
}
static void
scanner_apply_scope (GScanner *scanner,
ParserScope scope)
{
g_scanner_set_scope (scanner, scope);
if (scope == SCOPE_VALUE)
{
scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_";
scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_ +(),.%\t\n'/\"";
scanner->config->scan_identifier_1char = TRUE;
}
else if (scope == SCOPE_SELECTOR)
{
scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "*@";
scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_#.";
scanner->config->scan_identifier_1char = TRUE;
}
else if (scope == SCOPE_PSEUDO_CLASS ||
scope == SCOPE_NTH_CHILD ||
scope == SCOPE_DECLARATION)
{
scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "-_";
scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_";
scanner->config->scan_identifier_1char = FALSE;
}
else
g_assert_not_reached ();
scanner->config->scan_float = FALSE;
scanner->config->cpair_comment_single = NULL;
}
static void
css_provider_push_scope (GtkCssProvider *css_provider,
ParserScope scope)
{
GtkCssProviderPrivate *priv;
priv = css_provider->priv;
priv->state = g_slist_prepend (priv->state, GUINT_TO_POINTER (scope));
scanner_apply_scope (priv->scanner, scope);
}
static ParserScope
css_provider_pop_scope (GtkCssProvider *css_provider)
{
GtkCssProviderPrivate *priv;
ParserScope scope = SCOPE_SELECTOR;
priv = css_provider->priv;
if (!priv->state)
{
g_warning ("Push/pop calls to parser scope aren't paired");
scanner_apply_scope (priv->scanner, SCOPE_SELECTOR);
return SCOPE_SELECTOR;
}
priv->state = g_slist_delete_link (priv->state, priv->state);
/* Fetch new scope */
if (priv->state)
scope = GPOINTER_TO_INT (priv->state->data);
scanner_apply_scope (priv->scanner, scope);
return scope;
}
static void
css_provider_reset_parser (GtkCssProvider *css_provider)
{
GtkCssProviderPrivate *priv;
priv = css_provider->priv;
g_slist_free (priv->state);
priv->state = NULL;
scanner_apply_scope (priv->scanner, SCOPE_SELECTOR);
priv->scanner->user_data = NULL;
priv->value_pos = NULL;
g_slist_foreach (priv->cur_selectors, (GFunc) selector_path_unref, NULL);
g_slist_free (priv->cur_selectors);
priv->cur_selectors = NULL;
if (priv->cur_properties)
g_hash_table_unref (priv->cur_properties);
priv->cur_properties = g_hash_table_new_full (g_str_hash,
g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) property_value_free);
}
static void
css_provider_commit (GtkCssProvider *css_provider)
{
GtkCssProviderPrivate *priv;
GSList *l;
priv = css_provider->priv;
l = priv->cur_selectors;
while (l)
{
SelectorPath *path = l->data;
SelectorStyleInfo *info;
info = selector_style_info_new (path);
selector_style_info_set_style (info, priv->cur_properties);
g_ptr_array_add (priv->selectors_info, info);
l = l->next;
}
}
static GTokenType
parse_nth_child (GtkCssProvider *css_provider,
GScanner *scanner,
GtkRegionFlags *flags)
{
ParserSymbol symbol;
g_scanner_get_next_token (scanner);
if (scanner->token != G_TOKEN_SYMBOL)
return G_TOKEN_SYMBOL;
symbol = GPOINTER_TO_INT (scanner->value.v_symbol);
if (symbol == SYMBOL_NTH_CHILD)
{
g_scanner_get_next_token (scanner);
if (scanner->token != G_TOKEN_LEFT_PAREN)
return G_TOKEN_LEFT_PAREN;
css_provider_push_scope (css_provider, SCOPE_NTH_CHILD);
g_scanner_get_next_token (scanner);
if (scanner->token != G_TOKEN_SYMBOL)
return G_TOKEN_SYMBOL;
symbol = GPOINTER_TO_INT (scanner->value.v_symbol);
switch (symbol)
{
case SYMBOL_NTH_CHILD_EVEN:
*flags = GTK_REGION_EVEN;
break;
case SYMBOL_NTH_CHILD_ODD:
*flags = GTK_REGION_ODD;
break;
case SYMBOL_NTH_CHILD_FIRST:
*flags = GTK_REGION_FIRST;
break;
case SYMBOL_NTH_CHILD_LAST:
*flags = GTK_REGION_LAST;
break;
default:
break;
}
g_scanner_get_next_token (scanner);
if (scanner->token != G_TOKEN_RIGHT_PAREN)
return G_TOKEN_RIGHT_PAREN;
css_provider_pop_scope (css_provider);
}
else if (symbol == SYMBOL_FIRST_CHILD)
*flags = GTK_REGION_FIRST;
else if (symbol == SYMBOL_LAST_CHILD)
*flags = GTK_REGION_LAST;
else if (symbol == SYMBOL_SORTED_CHILD)
*flags = GTK_REGION_SORTED;
else
{
*flags = 0;
return G_TOKEN_SYMBOL;
}
return G_TOKEN_NONE;
}
static GTokenType
parse_pseudo_class (GtkCssProvider *css_provider,
GScanner *scanner,
SelectorPath *selector)
{
GtkStateType state;
g_scanner_get_next_token (scanner);
if (scanner->token != G_TOKEN_SYMBOL)
return G_TOKEN_SYMBOL;
state = GPOINTER_TO_INT (scanner->value.v_symbol);
switch (state)
{
case GTK_STATE_ACTIVE:
selector->state |= GTK_STATE_FLAG_ACTIVE;
break;
case GTK_STATE_PRELIGHT:
selector->state |= GTK_STATE_FLAG_PRELIGHT;
break;
case GTK_STATE_SELECTED:
selector->state |= GTK_STATE_FLAG_SELECTED;
break;
case GTK_STATE_INSENSITIVE:
selector->state |= GTK_STATE_FLAG_INSENSITIVE;
break;
case GTK_STATE_INCONSISTENT:
selector->state |= GTK_STATE_FLAG_INCONSISTENT;
break;
case GTK_STATE_FOCUSED:
selector->state |= GTK_STATE_FLAG_FOCUSED;
break;
default:
return G_TOKEN_SYMBOL;
}
return G_TOKEN_NONE;
}
/* Parses a number of concatenated classes */
static void
parse_classes (SelectorPath *path,
const gchar *str)
{
gchar *pos;
if ((pos = strchr (str, '.')) != NULL)
{
/* Leave the last class to the call after the loop */
while (pos)
{
*pos = '\0';
selector_path_prepend_class (path, str);
str = pos + 1;
pos = strchr (str, '.');
}
}
selector_path_prepend_class (path, str);
}
static gboolean
is_widget_class_name (const gchar *str)
{
/* Do a pretty lax check here, not all
* widget class names contain only CamelCase
* (gtkmm widgets don't), but at least part of
* the name will be CamelCase, so check for
* the first uppercase char
*/
while (*str)
{
if (g_ascii_isupper (*str))
return TRUE;
str++;
}
return FALSE;
}
static GTokenType
parse_selector (GtkCssProvider *css_provider,
GScanner *scanner,
SelectorPath **selector_out)
{
SelectorPath *path;
path = selector_path_new ();
*selector_out = path;
if (scanner->token != ':' &&
scanner->token != '#' &&
scanner->token != '.' &&
scanner->token != G_TOKEN_IDENTIFIER)
return G_TOKEN_IDENTIFIER;
while (scanner->token == '#' ||
scanner->token == '.' ||
scanner->token == G_TOKEN_IDENTIFIER)
{
if (scanner->token == '#' ||
scanner->token == '.')
{
gboolean is_class;
gchar *pos;
is_class = (scanner->token == '.');
g_scanner_get_next_token (scanner);
if (scanner->token != G_TOKEN_IDENTIFIER)
return G_TOKEN_IDENTIFIER;
selector_path_prepend_glob (path);
selector_path_prepend_combinator (path, COMBINATOR_CHILD);
if (is_class)
parse_classes (path, scanner->value.v_identifier);
else
{
if ((pos = strchr (scanner->value.v_identifier, '.')) != NULL)
*pos = '\0';
selector_path_prepend_name (path, scanner->value.v_identifier);
/* Parse any remaining classes */
if (pos)
parse_classes (path, pos + 1);
}
}
else if (is_widget_class_name (scanner->value.v_identifier))
{
gchar *pos;
if ((pos = strchr (scanner->value.v_identifier, '#')) != NULL ||
(pos = strchr (scanner->value.v_identifier, '.')) != NULL)
{
gchar *type_name, *name;
gboolean is_class;
is_class = (*pos == '.');
/* Widget type and name/class put together */
name = pos + 1;
*pos = '\0';
type_name = scanner->value.v_identifier;
selector_path_prepend_type (path, type_name);
/* This is only so there is a direct relationship
* between widget type and its name.
*/
selector_path_prepend_combinator (path, COMBINATOR_CHILD);
if (is_class)
parse_classes (path, name);
else
{
if ((pos = strchr (name, '.')) != NULL)
*pos = '\0';
selector_path_prepend_name (path, name);
/* Parse any remaining classes */
if (pos)
parse_classes (path, pos + 1);
}
}
else
selector_path_prepend_type (path, scanner->value.v_identifier);
}
else if (_gtk_style_context_check_region_name (scanner->value.v_identifier))
{
GtkRegionFlags flags = 0;
gchar *region_name;
region_name = g_strdup (scanner->value.v_identifier);
if (g_scanner_peek_next_token (scanner) == ':')
{
ParserSymbol symbol;
g_scanner_get_next_token (scanner);
css_provider_push_scope (css_provider, SCOPE_PSEUDO_CLASS);
/* Check for the next token being nth-child, parse in that
* case, and fallback into common state parsing if not.
*/
if (g_scanner_peek_next_token (scanner) != G_TOKEN_SYMBOL)
return G_TOKEN_SYMBOL;
symbol = GPOINTER_TO_INT (scanner->next_value.v_symbol);
if (symbol == SYMBOL_FIRST_CHILD ||
symbol == SYMBOL_LAST_CHILD ||
symbol == SYMBOL_NTH_CHILD ||
symbol == SYMBOL_SORTED_CHILD)
{
GTokenType token;
if ((token = parse_nth_child (css_provider, scanner, &flags)) != G_TOKEN_NONE)
return token;
css_provider_pop_scope (css_provider);
}
else
{
css_provider_pop_scope (css_provider);
selector_path_prepend_region (path, region_name, 0);
g_free (region_name);
break;
}
}
selector_path_prepend_region (path, region_name, flags);
g_free (region_name);
}
else if (scanner->value.v_identifier[0] == '*')
selector_path_prepend_glob (path);
else
return G_TOKEN_IDENTIFIER;
g_scanner_get_next_token (scanner);
if (scanner->token == '>')
{
selector_path_prepend_combinator (path, COMBINATOR_CHILD);
g_scanner_get_next_token (scanner);
}
}
if (scanner->token == ':')
{
/* Add glob selector if path is empty */
if (selector_path_depth (path) == 0)
selector_path_prepend_glob (path);
css_provider_push_scope (css_provider, SCOPE_PSEUDO_CLASS);
while (scanner->token == ':')
{
GTokenType token;
if ((token = parse_pseudo_class (css_provider, scanner, path)) != G_TOKEN_NONE)
return token;
g_scanner_get_next_token (scanner);
}
css_provider_pop_scope (css_provider);
}
return G_TOKEN_NONE;
}
#define SKIP_SPACES(s) while (s[0] == ' ' || s[0] == '\t' || s[0] == '\n') s++;
#define SKIP_SPACES_BACK(s) while (s[0] == ' ' || s[0] == '\t' || s[0] == '\n') s--;
static GtkSymbolicColor *
symbolic_color_parse_str (const gchar *string,
gchar **end_ptr)
{
GtkSymbolicColor *symbolic_color = NULL;
gchar *str;
str = (gchar *) string;
*end_ptr = str;
if (str[0] == '@')
{
const gchar *end;
gchar *name;
str++;
end = str;
while (*end == '-' || *end == '_' || g_ascii_isalpha (*end))
end++;
name = g_strndup (str, end - str);
symbolic_color = gtk_symbolic_color_new_name (name);
g_free (name);
*end_ptr = (gchar *) end;
}
else if (g_str_has_prefix (str, "lighter") ||
g_str_has_prefix (str, "darker"))
{
GtkSymbolicColor *param_color;
gboolean is_lighter = FALSE;
is_lighter = g_str_has_prefix (str, "lighter");
if (is_lighter)
str += strlen ("lighter");
else
str += strlen ("darker");
SKIP_SPACES (str);
if (*str != '(')
{
*end_ptr = (gchar *) str;
return NULL;
}
str++;
SKIP_SPACES (str);
param_color = symbolic_color_parse_str (str, end_ptr);
if (!param_color)
return NULL;
str = *end_ptr;
SKIP_SPACES (str);
*end_ptr = (gchar *) str;
if (*str != ')')
{
gtk_symbolic_color_unref (param_color);
return NULL;
}
if (is_lighter)
symbolic_color = gtk_symbolic_color_new_shade (param_color, 1.3);
else
symbolic_color = gtk_symbolic_color_new_shade (param_color, 0.7);
gtk_symbolic_color_unref (param_color);
(*end_ptr)++;
}
else if (g_str_has_prefix (str, "shade") ||
g_str_has_prefix (str, "alpha"))
{
GtkSymbolicColor *param_color;
gboolean is_shade = FALSE;
gdouble factor;
is_shade = g_str_has_prefix (str, "shade");
if (is_shade)
str += strlen ("shade");
else
str += strlen ("alpha");
SKIP_SPACES (str);
if (*str != '(')
{
*end_ptr = (gchar *) str;
return NULL;
}
str++;
SKIP_SPACES (str);
param_color = symbolic_color_parse_str (str, end_ptr);
if (!param_color)
return NULL;
str = *end_ptr;
SKIP_SPACES (str);
if (str[0] != ',')
{
gtk_symbolic_color_unref (param_color);
*end_ptr = (gchar *) str;
return NULL;
}
str++;
SKIP_SPACES (str);
factor = g_ascii_strtod (str, end_ptr);
str = *end_ptr;
SKIP_SPACES (str);
*end_ptr = (gchar *) str;
if (str[0] != ')')
{
gtk_symbolic_color_unref (param_color);
return NULL;
}
if (is_shade)
symbolic_color = gtk_symbolic_color_new_shade (param_color, factor);
else
symbolic_color = gtk_symbolic_color_new_alpha (param_color, factor);
gtk_symbolic_color_unref (param_color);
(*end_ptr)++;
}
else if (g_str_has_prefix (str, "mix"))
{
GtkSymbolicColor *color1, *color2;
gdouble factor;
str += strlen ("mix");
SKIP_SPACES (str);
if (*str != '(')
{
*end_ptr = (gchar *) str;
return NULL;
}
str++;
SKIP_SPACES (str);
color1 = symbolic_color_parse_str (str, end_ptr);
if (!color1)
return NULL;
str = *end_ptr;
SKIP_SPACES (str);
if (str[0] != ',')
{
gtk_symbolic_color_unref (color1);
*end_ptr = (gchar *) str;
return NULL;
}
str++;
SKIP_SPACES (str);
color2 = symbolic_color_parse_str (str, end_ptr);
if (!color2 || *end_ptr[0] != ',')
{
gtk_symbolic_color_unref (color1);
return NULL;
}
str = *end_ptr;
SKIP_SPACES (str);
if (str[0] != ',')
{
gtk_symbolic_color_unref (color1);
gtk_symbolic_color_unref (color2);
*end_ptr = (gchar *) str;
return NULL;
}
str++;
SKIP_SPACES (str);
factor = g_ascii_strtod (str, end_ptr);
str = *end_ptr;
SKIP_SPACES (str);
*end_ptr = (gchar *) str;
if (str[0] != ')')
{
gtk_symbolic_color_unref (color1);
gtk_symbolic_color_unref (color2);
return NULL;
}
symbolic_color = gtk_symbolic_color_new_mix (color1, color2, factor);
gtk_symbolic_color_unref (color1);
gtk_symbolic_color_unref (color2);
(*end_ptr)++;
}
else
{
GdkRGBA color;
gchar *color_str;
const gchar *end;
end = str + 1;
if (str[0] == '#')
{
/* Color in hex format */
while (g_ascii_isxdigit (*end))
end++;
}
else if (g_str_has_prefix (str, "rgb"))
{
/* color in rgb/rgba format */
while (*end != ')' && *end != '\0')
end++;
if (*end == ')')
end++;
}
else
{
/* Color name */
while (*end != '\0' &&
(g_ascii_isalnum (*end) || *end == ' '))
end++;
}
color_str = g_strndup (str, end - str);
*end_ptr = (gchar *) end;
if (!gdk_rgba_parse (&color, color_str))
{
g_free (color_str);
return NULL;
}
symbolic_color = gtk_symbolic_color_new_literal (&color);
g_free (color_str);
}
return symbolic_color;
}
static GtkSymbolicColor *
symbolic_color_parse (const gchar *str,
GError **error)
{
GtkSymbolicColor *color;
gchar *end;
color = symbolic_color_parse_str (str, &end);
if (*end != '\0')
{
g_set_error_literal (error,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_FAILED,
"Could not parse symbolic color");
if (color)
{
gtk_symbolic_color_unref (color);
color = NULL;
}
}
return color;
}
static GtkGradient *
gradient_parse_str (const gchar *str,
gchar **end_ptr)
{
GtkGradient *gradient = NULL;
gdouble coords[6];
gchar *end;
guint i;
if (g_str_has_prefix (str, "-gtk-gradient"))
{
cairo_pattern_type_t type;
str += strlen ("-gtk-gradient");
SKIP_SPACES (str);
if (*str != '(')
{
*end_ptr = (gchar *) str;
return NULL;
}
str++;
SKIP_SPACES (str);
/* Parse gradient type */
if (g_str_has_prefix (str, "linear"))
{
type = CAIRO_PATTERN_TYPE_LINEAR;
str += strlen ("linear");
}
else if (g_str_has_prefix (str, "radial"))
{
type = CAIRO_PATTERN_TYPE_RADIAL;
str += strlen ("radial");
}
else
{
*end_ptr = (gchar *) str;
return NULL;
}
SKIP_SPACES (str);
/* Parse start/stop position parameters */
for (i = 0; i < 2; i++)
{
if (*str != ',')
{
*end_ptr = (gchar *) str;
return NULL;
}
str++;
SKIP_SPACES (str);
if (strncmp (str, "left", 4) == 0)
{
coords[i * 3] = 0;
str += strlen ("left");
}
else if (strncmp (str, "right", 5) == 0)
{
coords[i * 3] = 1;
str += strlen ("right");
}
else if (strncmp (str, "center", 6) == 0)
{
coords[i * 3] = 0.5;
str += strlen ("center");
}
else
{
coords[i * 3] = g_ascii_strtod (str, &end);
if (str == end)
{
*end_ptr = (gchar *) str;
return NULL;
}
str = end;
}
SKIP_SPACES (str);
if (strncmp (str, "top", 3) == 0)
{
coords[(i * 3) + 1] = 0;
str += strlen ("top");
}
else if (strncmp (str, "bottom", 6) == 0)
{
coords[(i * 3) + 1] = 1;
str += strlen ("bottom");
}
else if (strncmp (str, "center", 6) == 0)
{
coords[(i * 3) + 1] = 0.5;
str += strlen ("center");
}
else
{
coords[(i * 3) + 1] = g_ascii_strtod (str, &end);
if (str == end)
{
*end_ptr = (gchar *) str;
return NULL;
}
str = end;
}
SKIP_SPACES (str);
if (type == CAIRO_PATTERN_TYPE_RADIAL)
{
/* Parse radius */
if (*str != ',')
{
*end_ptr = (gchar *) str;
return NULL;
}
str++;
SKIP_SPACES (str);
coords[(i * 3) + 2] = g_ascii_strtod (str, &end);
str = end;
SKIP_SPACES (str);
}
}
if (type == CAIRO_PATTERN_TYPE_LINEAR)
gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
else
gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
coords[3], coords[4], coords[5]);
while (*str == ',')
{
GtkSymbolicColor *color;
gdouble position;
if (*str != ',')
{
*end_ptr = (gchar *) str;
return gradient;
}
str++;
SKIP_SPACES (str);
if (g_str_has_prefix (str, "from"))
{
position = 0;
str += strlen ("from");
SKIP_SPACES (str);
if (*str != '(')
{
*end_ptr = (gchar *) str;
return gradient;
}
}
else if (g_str_has_prefix (str, "to"))
{
position = 1;
str += strlen ("to");
SKIP_SPACES (str);
if (*str != '(')
{
*end_ptr = (gchar *) str;
return gradient;
}
}
else if (g_str_has_prefix (str, "color-stop"))
{
str += strlen ("color-stop");
SKIP_SPACES (str);
if (*str != '(')
{
*end_ptr = (gchar *) str;
return gradient;
}
str++;
SKIP_SPACES (str);
position = g_ascii_strtod (str, &end);
str = end;
SKIP_SPACES (str);
if (*str != ',')
{
*end_ptr = (gchar *) str;
return gradient;
}
}
else
{
*end_ptr = (gchar *) str;
return gradient;
}
str++;
SKIP_SPACES (str);
color = symbolic_color_parse_str (str, &end);
str = end;
SKIP_SPACES (str);
if (*str != ')')
{
*end_ptr = (gchar *) str;
return gradient;
}
str++;
SKIP_SPACES (str);
if (color)
{
gtk_gradient_add_color_stop (gradient, position, color);
gtk_symbolic_color_unref (color);
}
}
if (*str != ')')
{
*end_ptr = (gchar *) str;
return gradient;
}
str++;
}
*end_ptr = (gchar *) str;
return gradient;
}
static gchar *
path_parse_str (GtkCssProvider *css_provider,
const gchar *str,
gchar **end_ptr,
GError **error)
{
gchar *path, *chr;
const gchar *start, *end;
start = str;
if (g_str_has_prefix (str, "url"))
{
str += strlen ("url");
SKIP_SPACES (str);
if (*str != '(')
{
*end_ptr = (gchar *) str;
return NULL;
}
chr = strchr (str, ')');
if (!chr)
{
*end_ptr = (gchar *) str;
return NULL;
}
end = chr + 1;
str++;
SKIP_SPACES (str);
if (*str == '"' || *str == '\'')
{
const gchar *p;
p = str;
str++;
chr--;
SKIP_SPACES_BACK (chr);
if (*chr != *p || chr == p)
{
*end_ptr = (gchar *)str;
return NULL;
}
}
else
{
*end_ptr = (gchar *)str;
return NULL;
}
path = g_strndup (str, chr - str);
g_strstrip (path);
*end_ptr = (gchar *)end;
}
else
{
path = g_strdup (str);
*end_ptr = (gchar *)str + strlen (str);
}
/* Always return an absolute path */
if (!g_path_is_absolute (path))
{
GtkCssProviderPrivate *priv;
gchar *dirname, *full_path;
priv = css_provider->priv;
/* Use relative path to the current CSS file path, if any */
if (priv->filename)
dirname = g_path_get_dirname (priv->filename);
else
dirname = g_get_current_dir ();
full_path = g_build_filename (dirname, path, NULL);
g_free (path);
g_free (dirname);
path = full_path;
}
if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
{
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_EXIST,
"File doesn't exist: %s", path);
g_free (path);
path = NULL;
*end_ptr = (gchar *)start;
}
return path;
}
static gchar *
path_parse (GtkCssProvider *css_provider,
const gchar *str,
GError **error)
{
gchar *path;
gchar *end;
path = path_parse_str (css_provider, str, &end, error);
if (!path)
return NULL;
if (*end != '\0')
{
g_set_error_literal (error,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_FAILED,
"Error parsing path");
g_free (path);
path = NULL;
}
return path;
}
static Gtk9Slice *
slice_parse_str (GtkCssProvider *css_provider,
const gchar *str,
gchar **end_ptr,
GError **error)
{
gdouble distance_top, distance_bottom;
gdouble distance_left, distance_right;
GtkSliceSideModifier mods[2];
GdkPixbuf *pixbuf;
Gtk9Slice *slice;
gchar *path;
gint i = 0;
SKIP_SPACES (str);
/* Parse image url */
path = path_parse_str (css_provider, str, end_ptr, error);
if (!path)
return NULL;
str = *end_ptr;
SKIP_SPACES (str);
/* Parse top/left/bottom/right distances */
distance_top = g_ascii_strtod (str, end_ptr);
str = *end_ptr;
SKIP_SPACES (str);
distance_right = g_ascii_strtod (str, end_ptr);
str = *end_ptr;
SKIP_SPACES (str);
distance_bottom = g_ascii_strtod (str, end_ptr);
str = *end_ptr;
SKIP_SPACES (str);
distance_left = g_ascii_strtod (str, end_ptr);
str = *end_ptr;
SKIP_SPACES (str);
while (*str && i < 2)
{
if (g_str_has_prefix (str, "stretch"))
{
str += strlen ("stretch");
mods[i] = GTK_SLICE_STRETCH;
}
else if (g_str_has_prefix (str, "repeat"))
{
str += strlen ("repeat");
mods[i] = GTK_SLICE_REPEAT;
}
else
{
g_free (path);
*end_ptr = (gchar *) str;
return NULL;
}
SKIP_SPACES (str);
i++;
}
*end_ptr = (gchar *) str;
if (*str != '\0')
{
g_free (path);
return NULL;
}
if (i != 2)
{
/* Fill in second modifier, same as the first */
mods[1] = mods[0];
}
pixbuf = gdk_pixbuf_new_from_file (path, error);
g_free (path);
if (!pixbuf)
{
*end_ptr = (gchar *) str;
return NULL;
}
slice = _gtk_9slice_new (pixbuf,
distance_top, distance_bottom,
distance_left, distance_right,
mods[0], mods[1]);
g_object_unref (pixbuf);
return slice;
}
static gdouble
unit_parse_str (const gchar *str,
gchar **end_str)
{
gdouble unit;
SKIP_SPACES (str);
unit = g_ascii_strtod (str, end_str);
str = *end_str;
/* Now parse the unit type, if any. We
* don't admit spaces between these.
*/
if (*str != ' ' && *str != '\0')
{
while (**end_str != ' ' && **end_str != '\0')
(*end_str)++;
/* Only handle pixels at the moment */
if (strncmp (str, "px", 2) != 0)
{
gchar *type;
type = g_strndup (str, *end_str - str);
g_warning ("Unknown unit '%s', only pixel units are "
"currently supported in CSS style", type);
g_free (type);
}
}
return unit;
}
static GtkBorder *
border_parse_str (const gchar *str,
gchar **end_str)
{
gdouble first, second, third, fourth;
GtkBorder *border;
border = gtk_border_new ();
SKIP_SPACES (str);
if (!g_ascii_isdigit (*str) && *str != '-')
return border;
first = unit_parse_str (str, end_str);
str = *end_str;
SKIP_SPACES (str);
if (!g_ascii_isdigit (*str) && *str != '-')
{
border->left = border->right = border->top = border->bottom = (gint) first;
*end_str = (gchar *) str;
return border;
}
second = unit_parse_str (str, end_str);
str = *end_str;
SKIP_SPACES (str);
if (!g_ascii_isdigit (*str) && *str != '-')
{
border->top = border->bottom = (gint) first;
border->left = border->right = (gint) second;
*end_str = (gchar *) str;
return border;
}
third = unit_parse_str (str, end_str);
str = *end_str;
SKIP_SPACES (str);
if (!g_ascii_isdigit (*str) && *str != '-')
{
border->top = (gint) first;
border->left = border->right = (gint) second;
border->bottom = (gint) third;
*end_str = (gchar *) str;
return border;
}
fourth = unit_parse_str (str, end_str);
border->top = (gint) first;
border->right = (gint) second;
border->bottom = (gint) third;
border->left = (gint) fourth;
return border;
}
static gboolean
css_provider_parse_value (GtkCssProvider *css_provider,
const gchar *value_str,
GValue *value,
GError **error)
{
GtkCssProviderPrivate *priv;
GType type;
gboolean parsed = TRUE;
gchar *end = NULL;
priv = css_provider->priv;
type = G_VALUE_TYPE (value);
if (type == GDK_TYPE_RGBA ||
type == GDK_TYPE_COLOR)
{
GdkRGBA rgba;
GdkColor color;
if (type == GDK_TYPE_RGBA &&
gdk_rgba_parse (&rgba, value_str))
g_value_set_boxed (value, &rgba);
else if (type == GDK_TYPE_COLOR &&
gdk_color_parse (value_str, &color))
g_value_set_boxed (value, &color);
else
{
GtkSymbolicColor *symbolic_color;
symbolic_color = symbolic_color_parse_str (value_str, &end);
if (symbolic_color)
{
g_value_unset (value);
g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
g_value_take_boxed (value, symbolic_color);
}
else
parsed = FALSE;
}
}
else if (type == PANGO_TYPE_FONT_DESCRIPTION)
{
PangoFontDescription *font_desc;
font_desc = pango_font_description_from_string (value_str);
g_value_take_boxed (value, font_desc);
}
else if (type == G_TYPE_BOOLEAN)
{
if (value_str[0] == '1' ||
g_ascii_strcasecmp (value_str, "true") == 0)
g_value_set_boolean (value, TRUE);
else
g_value_set_boolean (value, FALSE);
}
else if (type == G_TYPE_INT)
g_value_set_int (value, atoi (value_str));
else if (type == G_TYPE_UINT)
g_value_set_uint (value, (guint) atoi (value_str));
else if (type == G_TYPE_DOUBLE)
g_value_set_double (value, g_ascii_strtod (value_str, NULL));
else if (type == G_TYPE_FLOAT)
g_value_set_float (value, (gfloat) g_ascii_strtod (value_str, NULL));
else if (type == GTK_TYPE_THEMING_ENGINE)
{
GtkThemingEngine *engine;
engine = gtk_theming_engine_load (value_str);
if (engine)
g_value_set_object (value, engine);
else
parsed = FALSE;
}
else if (type == GTK_TYPE_ANIMATION_DESCRIPTION)
{
GtkAnimationDescription *desc;
desc = _gtk_animation_description_from_string (value_str);
if (desc)
g_value_take_boxed (value, desc);
else
parsed = FALSE;
}
else if (type == GTK_TYPE_BORDER)
{
GtkBorder *border;
border = border_parse_str (value_str, &end);
g_value_take_boxed (value, border);
}
else if (type == CAIRO_GOBJECT_TYPE_PATTERN)
{
GtkGradient *gradient;
gradient = gradient_parse_str (value_str, &end);
if (gradient)
{
g_value_unset (value);
g_value_init (value, GTK_TYPE_GRADIENT);
g_value_take_boxed (value, gradient);
}
else
{
gchar *path;
GdkPixbuf *pixbuf;
g_clear_error (error);
path = path_parse_str (css_provider, value_str, &end, error);
if (path)
{
pixbuf = gdk_pixbuf_new_from_file (path, NULL);
g_free (path);
if (pixbuf)
{
cairo_surface_t *surface;
cairo_pattern_t *pattern;
cairo_t *cr;
cairo_matrix_t matrix;
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf));
cr = cairo_create (surface);
gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
cairo_paint (cr);
pattern = cairo_pattern_create_for_surface (surface);
cairo_matrix_init_scale (&matrix,
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf));
cairo_pattern_set_matrix (pattern, &matrix);
cairo_surface_destroy (surface);
cairo_destroy (cr);
g_object_unref (pixbuf);
g_value_take_boxed (value, pattern);
}
else
parsed = FALSE;
}
else
parsed = FALSE;
}
}
else if (G_TYPE_IS_ENUM (type))
{
GEnumClass *enum_class;
GEnumValue *enum_value;
enum_class = g_type_class_ref (type);
enum_value = g_enum_get_value_by_nick (enum_class, value_str);
if (!enum_value)
{
g_set_error (error,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_FAILED,
"Unknown value '%s' for enum type '%s'",
value_str, g_type_name (type));
parsed = FALSE;
}
else
g_value_set_enum (value, enum_value->value);
g_type_class_unref (enum_class);
}
else if (G_TYPE_IS_FLAGS (type))
{
GFlagsClass *flags_class;
GFlagsValue *flag_value;
guint flags = 0;
gchar *ptr;
flags_class = g_type_class_ref (type);
/* Parse comma separated values */
ptr = strchr (value_str, ',');
while (ptr && parsed)
{
gchar *flag_str;
*ptr = '\0';
ptr++;
flag_str = (gchar *) value_str;
flag_value = g_flags_get_value_by_nick (flags_class,
g_strstrip (flag_str));
if (!flag_value)
{
g_set_error (error,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_FAILED,
"Unknown flag '%s' for type '%s'",
value_str, g_type_name (type));
parsed = FALSE;
}
else
flags |= flag_value->value;
value_str = ptr;
ptr = strchr (value_str, ',');
}
/* Store last/only value */
flag_value = g_flags_get_value_by_nick (flags_class, value_str);
if (!flag_value)
{
g_set_error (error,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_FAILED,
"Unknown flag '%s' for type '%s'",
value_str, g_type_name (type));
parsed = FALSE;
}
else
flags |= flag_value->value;
if (parsed)
g_value_set_enum (value, flags);
g_type_class_unref (flags_class);
}
else if (type == GTK_TYPE_9SLICE)
{
Gtk9Slice *slice;
slice = slice_parse_str (css_provider, value_str, &end, error);
if (slice)
g_value_take_boxed (value, slice);
else
parsed = FALSE;
}
else
{
g_set_error (error,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_FAILED,
"Cannot parse string '%s' for type %s",
value_str, g_type_name (type));
parsed = FALSE;
}
if (end && *end)
{
/* Set error position in the scanner
* according to what we've parsed so far
*/
priv->value_pos += (end - value_str);
if (error && !*error)
g_set_error_literal (error,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_FAILED,
"Failed to parse value");
}
return parsed;
}
static void
scanner_report_warning (GtkCssProvider *css_provider,
GTokenType expected_token,
GError *error)
{
GtkCssProviderPrivate *priv;
const gchar *line_end, *line_start;
const gchar *expected_str;
gchar buf[2], *line, *str;
guint pos;
priv = css_provider->priv;
if (error)
str = g_strdup (error->message);
else
{
if (priv->scanner->user_data)
expected_str = priv->scanner->user_data;
else
{
switch (expected_token)
{
case G_TOKEN_SYMBOL:
expected_str = "Symbol";
case G_TOKEN_IDENTIFIER:
expected_str = "Identifier";
default:
buf[0] = expected_token;
buf[1] = '\0';
expected_str = buf;
}
}
str = g_strdup_printf ("Parse error, expecting a %s '%s'",
(expected_str != buf) ? "valid" : "",
expected_str);
}
if (priv->value_pos)
line_start = priv->value_pos - 1;
else
line_start = priv->scanner->text - 1;
while (*line_start != '\n' &&
line_start != priv->buffer)
line_start--;
if (*line_start == '\n')
line_start++;
if (priv->value_pos)
pos = priv->value_pos - line_start + 1;
else
pos = priv->scanner->text - line_start - 1;
line_end = strchr (line_start, '\n');
if (line_end)
line = g_strndup (line_start, (line_end - line_start));
else
line = g_strdup (line_start);
g_message ("CSS: %s\n"
"%s, line %d, char %d:\n"
"%*c %s\n"
"%*c ^",
str, priv->scanner->input_name,
priv->scanner->line, priv->scanner->position,
3, ' ', line,
3 + pos, ' ');
g_free (line);
g_free (str);
}
static GTokenType
parse_rule (GtkCssProvider *css_provider,
GScanner *scanner,
GError **error)
{
GtkCssProviderPrivate *priv;
GTokenType expected_token;
SelectorPath *selector;
priv = css_provider->priv;
css_provider_push_scope (css_provider, SCOPE_SELECTOR);
/* Handle directives */
if (scanner->token == G_TOKEN_IDENTIFIER &&
scanner->value.v_identifier[0] == '@')
{
gchar *directive;
directive = &scanner->value.v_identifier[1];
if (strcmp (directive, "define-color") == 0)
{
GtkSymbolicColor *color;
gchar *color_name, *color_str;
/* Directive is a color mapping */
g_scanner_get_next_token (scanner);
if (scanner->token != G_TOKEN_IDENTIFIER)
{
scanner->user_data = "Color name";
return G_TOKEN_IDENTIFIER;
}
color_name = g_strdup (scanner->value.v_identifier);
css_provider_push_scope (css_provider, SCOPE_VALUE);
g_scanner_get_next_token (scanner);
if (scanner->token != G_TOKEN_IDENTIFIER)
{
scanner->user_data = "Color definition";
return G_TOKEN_IDENTIFIER;
}
color_str = g_strstrip (scanner->value.v_identifier);
color = symbolic_color_parse (color_str, error);
if (!color)
{
scanner->user_data = "Color definition";
return G_TOKEN_IDENTIFIER;
}
g_hash_table_insert (priv->symbolic_colors, color_name, color);
css_provider_pop_scope (css_provider);
g_scanner_get_next_token (scanner);
if (scanner->token != ';')
return ';';
return G_TOKEN_NONE;
}
else if (strcmp (directive, "import") == 0)
{
GScanner *scanner_backup;
GSList *state_backup;
gboolean loaded;
gchar *path = NULL;
css_provider_push_scope (css_provider, SCOPE_VALUE);
g_scanner_get_next_token (scanner);
if (scanner->token == G_TOKEN_IDENTIFIER &&
g_str_has_prefix (scanner->value.v_identifier, "url"))
path = path_parse (css_provider,
g_strstrip (scanner->value.v_identifier),
error);
else if (scanner->token == G_TOKEN_STRING)
path = path_parse (css_provider,
g_strstrip (scanner->value.v_string),
error);
if (path == NULL)
{
scanner->user_data = "File URL";
return G_TOKEN_IDENTIFIER;
}
css_provider_pop_scope (css_provider);
g_scanner_get_next_token (scanner);
if (scanner->token != ';')
{
g_free (path);
return ';';
}
/* Snapshot current parser state and scanner in order to restore after importing */
state_backup = priv->state;
scanner_backup = priv->scanner;
priv->state = NULL;
priv->scanner = create_scanner ();
/* FIXME: Avoid recursive importing */
loaded = gtk_css_provider_load_from_path_internal (css_provider, path,
FALSE, error);
/* Restore previous state */
css_provider_reset_parser (css_provider);
priv->state = state_backup;
g_scanner_destroy (priv->scanner);
priv->scanner = scanner_backup;
g_free (path);
if (!loaded)
{
scanner->user_data = "File URL";
return G_TOKEN_IDENTIFIER;
}
else
return G_TOKEN_NONE;
}
else
{
scanner->user_data = "Directive";
return G_TOKEN_IDENTIFIER;
}
}
expected_token = parse_selector (css_provider, scanner, &selector);
if (expected_token != G_TOKEN_NONE)
{
selector_path_unref (selector);
scanner->user_data = "Selector";
return expected_token;
}
priv->cur_selectors = g_slist_prepend (priv->cur_selectors, selector);
while (scanner->token == ',')
{
g_scanner_get_next_token (scanner);
expected_token = parse_selector (css_provider, scanner, &selector);
if (expected_token != G_TOKEN_NONE)
{
selector_path_unref (selector);
scanner->user_data = "Selector";
return expected_token;
}
priv->cur_selectors = g_slist_prepend (priv->cur_selectors, selector);
}
css_provider_pop_scope (css_provider);
if (scanner->token != G_TOKEN_LEFT_CURLY)
return G_TOKEN_LEFT_CURLY;
/* Declarations parsing */
css_provider_push_scope (css_provider, SCOPE_DECLARATION);
g_scanner_get_next_token (scanner);
while (scanner->token == G_TOKEN_IDENTIFIER)
{
gchar *value_str = NULL;
GtkStylePropertyParser parse_func = NULL;
GParamSpec *pspec;
gchar *prop;
prop = g_strdup (scanner->value.v_identifier);
g_scanner_get_next_token (scanner);
if (scanner->token != ':')
{
g_free (prop);
return ':';
}
priv->value_pos = priv->scanner->text;
css_provider_push_scope (css_provider, SCOPE_VALUE);
g_scanner_get_next_token (scanner);
if (scanner->token != G_TOKEN_IDENTIFIER)
{
g_free (prop);
scanner->user_data = "Property value";
return G_TOKEN_IDENTIFIER;
}
value_str = scanner->value.v_identifier;
SKIP_SPACES (value_str);
g_strchomp (value_str);
if (gtk_style_properties_lookup_property (prop, &parse_func, &pspec))
{
GValue *val;
val = g_slice_new0 (GValue);
g_value_init (val, pspec->value_type);
if (strcmp (value_str, "none") == 0)
{
/* Insert the default value, so it has an opportunity
* to override other style providers when merged
*/
g_param_value_set_default (pspec, val);
g_hash_table_insert (priv->cur_properties, prop, val);
}
else if (pspec->value_type == G_TYPE_STRING)
{
g_value_set_string (val, value_str);
g_hash_table_insert (priv->cur_properties, prop, val);
}
else if ((parse_func && (parse_func) (value_str, val, error)) ||
(!parse_func && css_provider_parse_value (css_provider, value_str, val, error)))
g_hash_table_insert (priv->cur_properties, prop, val);
else
{
g_value_unset (val);
g_slice_free (GValue, val);
g_free (prop);
scanner->user_data = "Property value";
return G_TOKEN_IDENTIFIER;
}
}
else if (prop[0] == '-')
{
GValue *val;
val = g_slice_new0 (GValue);
g_value_init (val, G_TYPE_STRING);
g_value_set_string (val, value_str);
g_hash_table_insert (priv->cur_properties, prop, val);
}
else
g_free (prop);
css_provider_pop_scope (css_provider);
g_scanner_get_next_token (scanner);
if (scanner->token != ';')
break;
g_scanner_get_next_token (scanner);
}
if (scanner->token != G_TOKEN_RIGHT_CURLY)
return G_TOKEN_RIGHT_CURLY;
css_provider_pop_scope (css_provider);
return G_TOKEN_NONE;
}
static gboolean
parse_stylesheet (GtkCssProvider *css_provider,
GError **error)
{
GtkCssProviderPrivate *priv;
gboolean result;
result = TRUE;
priv = css_provider->priv;
g_scanner_get_next_token (priv->scanner);
while (!g_scanner_eof (priv->scanner))
{
GTokenType expected_token;
GError *err = NULL;
css_provider_reset_parser (css_provider);
expected_token = parse_rule (css_provider, priv->scanner, &err);
if (expected_token != G_TOKEN_NONE)
{
/* If a GError was passed in, propagate the error and bail out,
* else report a warning and keep going
*/
if (error != NULL)
{
result = FALSE;
if (err)
g_propagate_error (error, err);
else
g_set_error_literal (error,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_FAILED,
"Error parsing stylesheet");
break;
}
else
{
scanner_report_warning (css_provider, expected_token, err);
g_clear_error (&err);
}
while (!g_scanner_eof (priv->scanner) &&
priv->scanner->token != G_TOKEN_RIGHT_CURLY)
g_scanner_get_next_token (priv->scanner);
}
else
css_provider_commit (css_provider);
g_scanner_get_next_token (priv->scanner);
}
return result;
}
/**
* gtk_css_provider_load_from_data:
* @css_provider: a #GtkCssProvider
* @data: CSS data loaded in memory
* @length: the length of @data in bytes, or -1 for NUL terminated strings
* @error: (out) (allow-none): return location for a #GError, or %NULL
*
* Loads @data into @css_provider, making it clear any previously loaded
* information.
*
* Returns: %TRUE if the data could be loaded.
**/
gboolean
gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
const gchar *data,
gssize length,
GError **error)
{
GtkCssProviderPrivate *priv;
g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
g_return_val_if_fail (data != NULL, FALSE);
priv = css_provider->priv;
if (length < 0)
length = strlen (data);
if (priv->selectors_info->len > 0)
g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
priv->scanner->input_name = "-";
priv->buffer = data;
g_scanner_input_text (priv->scanner, data, (guint) length);
g_free (priv->filename);
priv->filename = NULL;
priv->buffer = NULL;
return parse_stylesheet (css_provider, error);
}
/**
* gtk_css_provider_load_from_file:
* @css_provider: a #GtkCssProvider
* @file: #GFile pointing to a file to load
* @error: (out) (allow-none): return location for a #GError, or %NULL
*
* Loads the data contained in @file into @css_provider, making it
* clear any previously loaded information.
*
* Returns: %TRUE if the data could be loaded.
**/
gboolean
gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
GFile *file,
GError **error)
{
GtkCssProviderPrivate *priv;
GError *internal_error = NULL;
gchar *data;
gsize length;
gboolean ret;
g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
g_return_val_if_fail (G_IS_FILE (file), FALSE);
priv = css_provider->priv;
if (!g_file_load_contents (file, NULL,
&data, &length,
NULL, &internal_error))
{
g_propagate_error (error, internal_error);
return FALSE;
}
if (priv->selectors_info->len > 0)
g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
g_free (priv->filename);
priv->filename = g_file_get_path (file);
priv->scanner->input_name = priv->filename;
priv->buffer = data;
g_scanner_input_text (priv->scanner, data, (guint) length);
ret = parse_stylesheet (css_provider, error);
priv->buffer = NULL;
g_free (data);
return ret;
}
static gboolean
gtk_css_provider_load_from_path_internal (GtkCssProvider *css_provider,
const gchar *path,
gboolean reset,
GError **error)
{
GtkCssProviderPrivate *priv;
GError *internal_error = NULL;
GMappedFile *mapped_file;
const gchar *data;
gsize length;
gboolean ret;
priv = css_provider->priv;
mapped_file = g_mapped_file_new (path, FALSE, &internal_error);
if (internal_error)
{
g_propagate_error (error, internal_error);
return FALSE;
}
length = g_mapped_file_get_length (mapped_file);
data = g_mapped_file_get_contents (mapped_file);
if (!data)
data = "";
if (reset)
{
if (priv->selectors_info->len > 0)
g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
g_free (priv->filename);
priv->filename = g_strdup (path);
}
priv->scanner->input_name = priv->filename;
priv->buffer = data;
g_scanner_input_text (priv->scanner, data, (guint) length);
ret = parse_stylesheet (css_provider, error);
priv->buffer = NULL;
g_mapped_file_unref (mapped_file);
return ret;
}
/**
* gtk_css_provider_load_from_path:
* @css_provider: a #GtkCssProvider
* @path: the path of a filename to load, in the GLib filename encoding
* @error: (out) (allow-none): return location for a #GError, or %NULL
*
* Loads the data contained in @path into @css_provider, making it clear
* any previously loaded information.
*
* Returns: %TRUE if the data could be loaded.
**/
gboolean
gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
const gchar *path,
GError **error)
{
g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
g_return_val_if_fail (path != NULL, FALSE);
return gtk_css_provider_load_from_path_internal (css_provider, path,
TRUE, error);
}
/**
* gtk_css_provider_get_default:
*
* Returns the provider containing the style settings used as a
* fallback for all widgets.
*
* Returns: (transfer none): The provider used for fallback styling.
* This memory is owned by GTK+, and you must not free it.
**/
GtkCssProvider *
gtk_css_provider_get_default (void)
{
static GtkCssProvider *provider;
if (G_UNLIKELY (!provider))
{
const gchar *str =
"@define-color fg_color #000; \n"
"@define-color bg_color #dcdad5; \n"
"@define-color text_color #000; \n"
"@define-color base_color #fff; \n"
"@define-color selected_bg_color #4b6983; \n"
"@define-color selected_fg_color #fff; \n"
"@define-color tooltip_bg_color #eee1b3; \n"
"@define-color tooltip_fg_color #000; \n"
"\n"
"@define-color info_fg_color rgb (181, 171, 156);\n"
"@define-color info_bg_color rgb (252, 252, 189);\n"
"@define-color warning_fg_color rgb (173, 120, 41);\n"
"@define-color warning_bg_color rgb (250, 173, 61);\n"
"@define-color question_fg_color rgb (97, 122, 214);\n"
"@define-color question_bg_color rgb (138, 173, 212);\n"
"@define-color error_fg_color rgb (166, 38, 38);\n"
"@define-color error_bg_color rgb (237, 54, 54);\n"
"\n"
"*,\n"
"GtkTreeView > GtkButton {\n"
" background-color: @bg_color;\n"
" color: @fg_color;\n"
" border-color: shade (@bg_color, 0.6);\n"
" padding: 2;\n"
" border-width: 0;\n"
"}\n"
"\n"
"*:prelight {\n"
" background-color: shade (@bg_color, 1.05);\n"
" color: shade (@fg_color, 1.3);\n"
"}\n"
"\n"
"*:selected {\n"
" background-color: @selected_bg_color;\n"
" color: @selected_fg_color;\n"
"}\n"
"\n"
".expander, .view.expander {\n"
" color: #fff;\n"
"}\n"
"\n"
".expander:prelight {\n"
" color: @text_color;\n"
"}\n"
"\n"
".expander:active {\n"
" transition: 300ms linear;\n"
"}\n"
"\n"
"*:insensitive {\n"
" border-color: shade (@bg_color, 0.7);\n"
" background-color: shade (@bg_color, 0.9);\n"
" color: shade (@bg_color, 0.7);\n"
"}\n"
"\n"
"GtkTreeView, GtkIconView {\n"
" background-color: @base_color;\n"
" color: @text_color;\n"
"}\n"
"\n"
".view {\n"
" background-color: @base_color;\n"
" color: @text_color;\n"
"}\n"
".view:selected {\n"
" background-color: shade (@bg_color, 0.9);\n"
" color: @fg_color;\n"
"}\n"
"\n"
".view:selected:focused {\n"
" background-color: @selected_bg_color;\n"
" color: @selected_fg_color;\n"
"}\n"
"\n"
"GtkTreeView > row {\n"
" background-color: @base_color;\n"
" color: @text_color;\n"
"}\n"
"\n"
"GtkTreeView > row:nth-child(odd) { \n"
" background-color: shade (@base_color, 0.93); \n"
"}\n"
"\n"
".tooltip {\n"
" background-color: @tooltip_bg_color; \n"
" color: @tooltip_fg_color; \n"
" border-color: @tooltip_fg_color; \n"
" border-width: 1;\n"
" border-style: solid;\n"
"}\n"
"\n"
".button,\n"
".slider {\n"
" border-style: outset; \n"
" border-width: 2; \n"
"}\n"
"\n"
".button:active {\n"
" background-color: shade (@bg_color, 0.7);\n"
" border-style: inset; \n"
"}\n"
"\n"
".button:prelight,\n"
".slider:prelight {\n"
" background-color: @selected_bg_color;\n"
" color: @selected_fg_color;\n"
" border-color: shade (@selected_bg_color, 0.7);\n"
"}\n"
"\n"
".trough {\n"
" border-style: inset;\n"
" border-width: 1;\n"
" padding: 0;\n"
"}\n"
"\n"
".entry {\n"
" border-style: inset;\n"
" border-width: 2;\n"
" background-color: @base_color;\n"
" color: @text_color;\n"
"}\n"
"\n"
".entry:insensitive {\n"
" background-color: shade (@base_color, 0.9);\n"
" color: shade (@base_color, 0.7);\n"
"}\n"
".entry:active {\n"
" background-color: #c4c2bd;\n"
" color: #000;\n"
"}\n"
"\n"
".progressbar,\n"
".entry.progressbar {\n"
" background-color: @selected_bg_color;\n"
" border-color: shade (@selected_bg_color, 0.7);\n"
" color: @selected_fg_color;\n"
" border-style: outset;\n"
" border-width: 1;\n"
"}\n"
"\n"
"GtkCheckButton:hover,\n"
"GtkCheckButton:selected,\n"
"GtkRadioButton:hover,\n"
"GtkRadioButton:selected {\n"
" background-color: shade (@bg_color, 1.05);\n"
"}\n"
"\n"
".check, .radio {\n"
" border-style: solid;\n"
" border-width: 1;\n"
" background-color: @base_color;\n"
" border-color: @fg_color;\n"
"}\n"
"\n"
".check:active, .radio:active,\n"
".check:hover, .radio:hover {\n"
" background-color: @base_color;\n"
" border-color: @fg_color;\n"
" color: @text_color;\n"
"}\n"
"\n"
".check:selected, .radio:selected {\n"
" background-color: @selected_bg_color;\n"
" color: @selected_fg_color;\n"
"}\n"
"\n"
".menu.check, .menu.radio {\n"
" color: @fg_color;\n"
" border-style: none;\n"
" border-width: 0;\n"
"}\n"
"\n"
".popup {\n"
" border-style: outset;\n"
" border-width: 1;\n"
"}\n"
"\n"
".viewport {\n"
" border-style: inset;\n"
" border-width: 2;\n"
"}\n"
"\n"
".notebook {\n"
" border-style: outset;\n"
" border-width: 1;\n"
"}\n"
"\n"
".frame {\n"
" border-style: inset;\n"
" border-width: 1;\n"
"}\n"
"\n"
"GtkScrolledWindow.frame {\n"
" padding: 0;\n"
"}\n"
"\n"
".menu,\n"
".menubar,\n"
".toolbar {\n"
" border-style: outset;\n"
" border-width: 1;\n"
"}\n"
"\n"
".menu:hover,\n"
".menubar:hover,\n"
".menu.check:hover,\n"
".menu.radio:hover {\n"
" background-color: @selected_bg_color;\n"
" color: @selected_fg_color;\n"
"}\n"
"\n"
"GtkSpinButton.button {\n"
" border-width: 1;\n"
"}\n"
"\n"
".scale.slider:hover,\n"
"GtkSpinButton.button:hover {\n"
" background-color: shade (@bg_color, 1.05);\n"
" border-color: shade (@bg_color, 0.8);\n"
"}\n"
"\n"
"GtkToggleButton.button:inconsistent {\n"
" border-style: outset;\n"
" border-width: 1px;\n"
" background-color: shade (@bg_color, 0.9);\n"
" border-color: shade (@bg_color, 0.7);\n"
"}\n"
"\n"
"GtkLabel:selected {\n"
" background-color: shade (@bg_color, 0.9);\n"
" color: @fg_color;\n"
"}\n"
"\n"
"GtkLabel:selected:focused {\n"
" background-color: @selected_bg_color;\n"
" color: @selected_fg_color;\n"
"}\n"
"\n"
".spinner:active {\n"
" transition: 750ms linear loop;\n"
"}\n"
"\n"
".info {\n"
" background-color: @info_bg_color;\n"
" color: @info_fg_color;\n"
"}\n"
"\n"
".warning {\n"
" background-color: @warning_bg_color;\n"
" color: @warning_fg_color;\n"
"}\n"
"\n"
".question {\n"
" background-color: @question_bg_color;\n"
" color: @question_fg_color;\n"
"}\n"
"\n"
".error {\n"
" background-color: @error_bg_color;\n"
" color: @error_fg_color;\n"
"}\n"
"\n"
".highlight {\n"
" background-color: @selected_bg_color;\n"
" color: @selected_fg_color;\n"
"}\n"
"\n"
".light-area-focus {\n"
" color: #000;\n"
"}\n"
"\n"
".dark-area-focus {\n"
" color: #fff;\n"
"}\n"
"GtkCalendar.view {\n"
" border-width: 1;\n"
" border-style: inset;\n"
" padding: 1;\n"
"}\n"
"\n"
"GtkCalendar.view:inconsistent {\n"
" color: darker (@bg_color);\n"
"}\n"
"\n"
"GtkCalendar.header {\n"
" background-color: @bg_color;\n"
" border-style: outset;\n"
" border-width: 2;\n"
"}\n"
"\n"
"GtkCalendar.highlight {\n"
" border-width: 0;\n"
"}\n"
"\n"
"GtkCalendar.button {\n"
" background-color: @bg_color;\n"
"}\n"
"\n"
"GtkCalendar.button:hover {\n"
" background-color: lighter (@bg_color);\n"
" color: @fg_color;\n"
"}\n"
"\n"
".menu {\n"
" border-width: 1;\n"
" padding: 0;\n"
"}\n"
"\n"
".menu * {\n"
" border-width: 0;\n"
" padding: 2;\n"
"}\n"
"\n";
provider = gtk_css_provider_new ();
if (!gtk_css_provider_load_from_data (provider, str, -1, NULL))
{
g_error ("Failed to load the internal default CSS.");
}
}
return provider;
}
static gchar *
css_provider_get_theme_dir (void)
{
const gchar *var;
gchar *path;
var = g_getenv ("GTK_DATA_PREFIX");
if (var)
path = g_build_filename (var, "share", "themes", NULL);
else
path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
return path;
}
/**
* gtk_css_provider_get_named:
* @name: A theme name
* @variant: variant to load, for example, "dark", or %NULL for the default
*
* Loads a theme from the usual theme paths
*
* Returns: (transfer none): a #GtkCssProvider with the theme loaded.
* This memory is owned by GTK+, and you must not free it.
**/
GtkCssProvider *
gtk_css_provider_get_named (const gchar *name,
const gchar *variant)
{
static GHashTable *themes = NULL;
GtkCssProvider *provider;
if (G_UNLIKELY (!themes))
themes = g_hash_table_new (g_str_hash, g_str_equal);
provider = g_hash_table_lookup (themes, name);
if (!provider)
{
const gchar *home_dir;
gchar *subpath, *path = NULL;
if (variant)
subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
else
subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
/* First look in the users home directory
*/
home_dir = g_get_home_dir ();
if (home_dir)
{
path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
if (!g_file_test (path, G_FILE_TEST_EXISTS))
{
g_free (path);
path = NULL;
}
}
if (!path)
{
gchar *theme_dir = css_provider_get_theme_dir ();
path = g_build_filename (theme_dir, name, subpath, NULL);
g_free (theme_dir);
if (!g_file_test (path, G_FILE_TEST_EXISTS))
{
g_free (path);
path = NULL;
}
}
g_free (subpath);
if (path)
{
GError *error = NULL;
provider = gtk_css_provider_new ();
gtk_css_provider_load_from_path (provider, path, &error);
if (error)
{
g_warning ("Could not load named theme \"%s\": %s", name, error->message);
g_error_free (error);
g_object_unref (provider);
provider = NULL;
}
else
g_hash_table_insert (themes, g_strdup (name), provider);
}
}
return provider;
}
|