gtk3_interface.c 98.1 KB
Newer Older
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
/*
 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
#include <dlfcn.h>
#include <setjmp.h>
#include <X11/Xlib.h>
#include <limits.h>
#include <string.h>
#include "gtk3_interface.h"
#include "java_awt_Transparency.h"
#include "sizecalc.h"
#include <jni_util.h>
#include <stdio.h>
#include "awt.h"

static void *gtk3_libhandle = NULL;
static void *gthread_libhandle = NULL;

static jmp_buf j;

/* Widgets */
static GtkWidget *gtk3_widget = NULL;
static GtkWidget *gtk3_window = NULL;
static GtkFixed  *gtk3_fixed  = NULL;
static GtkStyleProvider *gtk3_css = NULL;

/* Paint system */
static cairo_surface_t *surface = NULL;
static cairo_t *cr = NULL;

static const char ENV_PREFIX[] = "GTK_MODULES=";

static GtkWidget *gtk3_widgets[_GTK_WIDGET_TYPE_SIZE];

static void throw_exception(JNIEnv *env, const char* name, const char* message)
{
    jclass class = (*env)->FindClass(env, name);

    if (class != NULL)
        (*env)->ThrowNew(env, class, message);

    (*env)->DeleteLocalRef(env, class);
}

static void gtk3_add_state(GtkWidget *widget, GtkStateType state) {
    GtkStateType old_state = fp_gtk_widget_get_state(widget);
    fp_gtk_widget_set_state(widget, old_state | state);
}

static void gtk3_remove_state(GtkWidget *widget, GtkStateType state) {
    GtkStateType old_state = fp_gtk_widget_get_state(widget);
    fp_gtk_widget_set_state(widget, old_state & ~state);
}

/* This is a workaround for the bug:
 * http://sourceware.org/bugzilla/show_bug.cgi?id=1814
 * (dlsym/dlopen clears dlerror state)
 * This bug is specific to Linux, but there is no harm in
 * applying this workaround on Solaris as well.
 */
static void* dl_symbol(const char* name)
{
    void* result = dlsym(gtk3_libhandle, name);
    if (!result)
        longjmp(j, NO_SYMBOL_EXCEPTION);

    return result;
}

static void* dl_symbol_gthread(const char* name)
{
    void* result = dlsym(gthread_libhandle, name);
    if (!result)
        longjmp(j, NO_SYMBOL_EXCEPTION);

    return result;
}

gboolean gtk3_check(const char* lib_name, gboolean load)
{
    if (gtk3_libhandle != NULL) {
        /* We've already successfully opened the GTK libs, so return true. */
        return TRUE;
    } else {
#ifdef RTLD_NOLOAD
        void *lib = dlopen(lib_name, RTLD_LAZY | RTLD_NOLOAD);
        if (!load || lib != NULL) {
            return lib != NULL;
        }
#else
#ifdef _AIX
        /* On AIX we could implement this with the help of loadquery(L_GETINFO, ..)  */
        /* (see reload_table() in hotspot/src/os/aix/vm/loadlib_aix.cpp) but it is   */
        /* probably not worth it because most AIX servers don't have GTK libs anyway */
#endif
#endif
        return dlopen(lib_name, RTLD_LAZY | RTLD_LOCAL) != NULL;
    }
}

#define ADD_SUPPORTED_ACTION(actionStr)                                        \
do {                                                                           \
    jfieldID fld_action = (*env)->GetStaticFieldID(env, cls_action, actionStr, \
                                                 "Ljava/awt/Desktop$Action;"); \
    if (!(*env)->ExceptionCheck(env)) {                                        \
        jobject action = (*env)->GetStaticObjectField(env, cls_action,         \
                                                                  fld_action); \
        (*env)->CallBooleanMethod(env, supportedActions, mid_arrayListAdd,     \
                                                                      action); \
    } else {                                                                   \
        (*env)->ExceptionClear(env);                                           \
    }                                                                          \
} while(0);


static void update_supported_actions(JNIEnv *env) {
    GVfs * (*fp_g_vfs_get_default) (void);
    const gchar * const * (*fp_g_vfs_get_supported_uri_schemes) (GVfs * vfs);
    const gchar * const * schemes = NULL;

    jclass cls_action = (*env)->FindClass(env, "java/awt/Desktop$Action");
    CHECK_NULL(cls_action);
    jclass cls_xDesktopPeer = (*env)->
                                     FindClass(env, "sun/awt/X11/XDesktopPeer");
    CHECK_NULL(cls_xDesktopPeer);
    jfieldID fld_supportedActions = (*env)->GetStaticFieldID(env,
                      cls_xDesktopPeer, "supportedActions", "Ljava/util/List;");
    CHECK_NULL(fld_supportedActions);
    jobject supportedActions = (*env)->GetStaticObjectField(env,
                                        cls_xDesktopPeer, fld_supportedActions);

    jclass cls_arrayList = (*env)->FindClass(env, "java/util/ArrayList");
    CHECK_NULL(cls_arrayList);
    jmethodID mid_arrayListAdd = (*env)->GetMethodID(env, cls_arrayList, "add",
                                                       "(Ljava/lang/Object;)Z");
    CHECK_NULL(mid_arrayListAdd);
    jmethodID mid_arrayListClear = (*env)->GetMethodID(env, cls_arrayList,
                                                                "clear", "()V");
    CHECK_NULL(mid_arrayListClear);

    (*env)->CallVoidMethod(env, supportedActions, mid_arrayListClear);

    ADD_SUPPORTED_ACTION("OPEN");

    /**
     * gtk_show_uri() documentation says:
     *
     * > you need to install gvfs to get support for uri schemes such as http://
     * > or ftp://, as only local files are handled by GIO itself.
     *
     * So OPEN action was safely added here.
     * However, it looks like Solaris 11 have gvfs support only for 32-bit
     * applications only by default.
     */

    fp_g_vfs_get_default = dl_symbol("g_vfs_get_default");
    fp_g_vfs_get_supported_uri_schemes =
                           dl_symbol("g_vfs_get_supported_uri_schemes");
    dlerror();

    if (fp_g_vfs_get_default && fp_g_vfs_get_supported_uri_schemes) {
        GVfs * vfs = fp_g_vfs_get_default();
        schemes = vfs ? fp_g_vfs_get_supported_uri_schemes(vfs) : NULL;
        if (schemes) {
            int i = 0;
            while (schemes[i]) {
                if (strcmp(schemes[i], "http") == 0) {
                    ADD_SUPPORTED_ACTION("BROWSE");
                    ADD_SUPPORTED_ACTION("MAIL");
                    break;
                }
                i++;
            }
        }
    } else {
#ifdef DEBUG
        fprintf(stderr, "Cannot load g_vfs_get_supported_uri_schemes\n");
#endif /* DEBUG */
    }

}
/**
 * Functions for awt_Desktop.c
 */
static gboolean gtk3_show_uri_load(JNIEnv *env) {
    gboolean success = FALSE;
    dlerror();
    fp_gtk_show_uri = dl_symbol("gtk_show_uri");
    const char *dlsym_error = dlerror();
    if (dlsym_error) {
#ifdef DEBUG
        fprintf (stderr, "Cannot load symbol: %s \n", dlsym_error);
#endif /* DEBUG */
    } else if (fp_gtk_show_uri == NULL) {
#ifdef DEBUG
        fprintf(stderr, "dlsym(gtk_show_uri) returned NULL\n");
#endif /* DEBUG */
    } else {
        gtk->gtk_show_uri = fp_gtk_show_uri;
        update_supported_actions(env);
        success = TRUE;
    }
    return success;
}

/**
 * Functions for sun_awt_X11_GtkFileDialogPeer.c
 */
static void gtk3_file_chooser_load()
{
    fp_gtk_file_chooser_get_filename = dl_symbol(
            "gtk_file_chooser_get_filename");
    fp_gtk_file_chooser_dialog_new = dl_symbol("gtk_file_chooser_dialog_new");
    fp_gtk_file_chooser_set_current_folder = dl_symbol(
            "gtk_file_chooser_set_current_folder");
    fp_gtk_file_chooser_set_filename = dl_symbol(
            "gtk_file_chooser_set_filename");
    fp_gtk_file_chooser_set_current_name = dl_symbol(
            "gtk_file_chooser_set_current_name");
    fp_gtk_file_filter_add_custom = dl_symbol("gtk_file_filter_add_custom");
    fp_gtk_file_chooser_set_filter = dl_symbol("gtk_file_chooser_set_filter");
    fp_gtk_file_chooser_get_type = dl_symbol("gtk_file_chooser_get_type");
    fp_gtk_file_filter_new = dl_symbol("gtk_file_filter_new");
    fp_gtk_file_chooser_set_do_overwrite_confirmation = dl_symbol(
                "gtk_file_chooser_set_do_overwrite_confirmation");
    fp_gtk_file_chooser_set_select_multiple = dl_symbol(
            "gtk_file_chooser_set_select_multiple");
    fp_gtk_file_chooser_get_current_folder = dl_symbol(
            "gtk_file_chooser_get_current_folder");
    fp_gtk_file_chooser_get_filenames = dl_symbol(
            "gtk_file_chooser_get_filenames");
    fp_gtk_g_slist_length = dl_symbol("g_slist_length");
    fp_gdk_x11_drawable_get_xid = dl_symbol("gdk_x11_window_get_xid");
}

static void empty() {}

static gboolean gtk3_version_3_10 = TRUE;
static gboolean gtk3_version_3_14 = FALSE;

GtkApi* gtk3_load(JNIEnv *env, const char* lib_name)
{
    gboolean result;
    int i;
    int (*handler)();
    int (*io_handler)();
    char *gtk_modules_env;
    gtk3_libhandle = dlopen(lib_name, RTLD_LAZY | RTLD_LOCAL);
    if (gtk3_libhandle == NULL) {
        return FALSE;
    }

    gthread_libhandle = dlopen(GTHREAD_LIB_VERSIONED, RTLD_LAZY | RTLD_LOCAL);
    if (gthread_libhandle == NULL) {
        gthread_libhandle = dlopen(GTHREAD_LIB, RTLD_LAZY | RTLD_LOCAL);
        if (gthread_libhandle == NULL)
            return FALSE;
    }

    if (setjmp(j) == 0)
    {
        fp_gtk_check_version = dl_symbol("gtk_check_version");

        /* GLib */
        fp_glib_check_version = dlsym(gtk3_libhandle, "glib_check_version");
        if (!fp_glib_check_version) {
            dlerror();
        }
        fp_g_free = dl_symbol("g_free");
        fp_g_object_unref = dl_symbol("g_object_unref");

        fp_g_main_context_iteration =
            dl_symbol("g_main_context_iteration");

        fp_g_value_init = dl_symbol("g_value_init");
        fp_g_type_is_a = dl_symbol("g_type_is_a");
        fp_g_value_get_boolean = dl_symbol("g_value_get_boolean");
        fp_g_value_get_char = dl_symbol("g_value_get_char");
        fp_g_value_get_uchar = dl_symbol("g_value_get_uchar");
        fp_g_value_get_int = dl_symbol("g_value_get_int");
        fp_g_value_get_uint = dl_symbol("g_value_get_uint");
        fp_g_value_get_long = dl_symbol("g_value_get_long");
        fp_g_value_get_ulong = dl_symbol("g_value_get_ulong");
        fp_g_value_get_int64 = dl_symbol("g_value_get_int64");
        fp_g_value_get_uint64 = dl_symbol("g_value_get_uint64");
        fp_g_value_get_float = dl_symbol("g_value_get_float");
        fp_g_value_get_double = dl_symbol("g_value_get_double");
        fp_g_value_get_string = dl_symbol("g_value_get_string");
        fp_g_value_get_enum = dl_symbol("g_value_get_enum");
        fp_g_value_get_flags = dl_symbol("g_value_get_flags");
        fp_g_value_get_param = dl_symbol("g_value_get_param");
        fp_g_value_get_boxed = dl_symbol("g_value_get_boxed");
        fp_g_value_get_pointer = dl_symbol("g_value_get_pointer");

        fp_g_object_get = dl_symbol("g_object_get");
        fp_g_object_set = dl_symbol("g_object_set");

        fp_g_str_has_prefix = dl_symbol("g_str_has_prefix");
        fp_g_strsplit = dl_symbol("g_strsplit");
        fp_g_strfreev = dl_symbol("g_strfreev");

        /* GDK */
        fp_gdk_get_default_root_window =
            dl_symbol("gdk_get_default_root_window");

        /* Pixbuf */
        fp_gdk_pixbuf_new = dl_symbol("gdk_pixbuf_new");
        fp_gdk_pixbuf_new_from_file =
                dl_symbol("gdk_pixbuf_new_from_file");
        fp_gdk_pixbuf_get_from_drawable =
                    dl_symbol("gdk_pixbuf_get_from_window");
        fp_gdk_pixbuf_get_width = dl_symbol("gdk_pixbuf_get_width");
        fp_gdk_pixbuf_get_height = dl_symbol("gdk_pixbuf_get_height");
        fp_gdk_pixbuf_get_pixels = dl_symbol("gdk_pixbuf_get_pixels");
        fp_gdk_pixbuf_get_rowstride =
                dl_symbol("gdk_pixbuf_get_rowstride");
        fp_gdk_pixbuf_get_has_alpha =
                dl_symbol("gdk_pixbuf_get_has_alpha");
        fp_gdk_pixbuf_get_bits_per_sample =
                dl_symbol("gdk_pixbuf_get_bits_per_sample");
        fp_gdk_pixbuf_get_n_channels =
                dl_symbol("gdk_pixbuf_get_n_channels");
        fp_gdk_pixbuf_get_colorspace =
                dl_symbol("gdk_pixbuf_get_colorspace");

        fp_cairo_image_surface_create = dl_symbol("cairo_image_surface_create");
        fp_cairo_surface_destroy = dl_symbol("cairo_surface_destroy");
        fp_cairo_create = dl_symbol("cairo_create");
        fp_cairo_destroy = dl_symbol("cairo_destroy");
        fp_cairo_fill = dl_symbol("cairo_fill");
        fp_cairo_rectangle = dl_symbol("cairo_rectangle");
        fp_cairo_set_source_rgb = dl_symbol("cairo_set_source_rgb");
        fp_cairo_set_source_rgba = dl_symbol("cairo_set_source_rgba");
        fp_cairo_surface_flush = dl_symbol("cairo_surface_flush");
        fp_cairo_paint = dl_symbol("cairo_paint");
        fp_cairo_clip = dl_symbol("cairo_clip");
        fp_cairo_image_surface_get_data =
                       dl_symbol("cairo_image_surface_get_data");
        fp_cairo_image_surface_get_stride =
                       dl_symbol("cairo_image_surface_get_stride");

        fp_gdk_pixbuf_get_from_surface =
                       dl_symbol("gdk_pixbuf_get_from_surface");

        fp_gtk_widget_get_state = dl_symbol("gtk_widget_get_state");
        fp_gtk_widget_set_state = dl_symbol("gtk_widget_set_state");

        fp_gtk_widget_is_focus = dl_symbol("gtk_widget_is_focus");
        fp_gtk_widget_set_allocation = dl_symbol("gtk_widget_set_allocation");
        fp_gtk_widget_get_parent = dl_symbol("gtk_widget_get_parent");
        fp_gtk_widget_get_window = dl_symbol("gtk_widget_get_window");

        fp_gtk_widget_get_style_context =
                       dl_symbol("gtk_widget_get_style_context");
        fp_gtk_style_context_get_color =
                       dl_symbol("gtk_style_context_get_color");
        fp_gtk_style_context_get_background_color =
                       dl_symbol("gtk_style_context_get_background_color");
        fp_gtk_widget_get_state_flags = dl_symbol("gtk_widget_get_state_flags");
        fp_gtk_style_context_set_state =
                       dl_symbol("gtk_style_context_set_state");
        fp_gtk_style_context_add_class =
                       dl_symbol("gtk_style_context_add_class");
        fp_gtk_style_context_save = dl_symbol("gtk_style_context_save");
        fp_gtk_style_context_restore = dl_symbol("gtk_style_context_restore");
        fp_gtk_render_check = dl_symbol("gtk_render_check");
        fp_gtk_render_option = dl_symbol("gtk_render_option");
        fp_gtk_render_extension = dl_symbol("gtk_render_extension");
        fp_gtk_render_expander = dl_symbol("gtk_render_expander");
        fp_gtk_render_frame_gap = dl_symbol("gtk_render_frame_gap");
        fp_gtk_render_line = dl_symbol("gtk_render_line");
        fp_gtk_widget_render_icon_pixbuf =
                      dl_symbol("gtk_widget_render_icon_pixbuf");
        if (fp_gtk_check_version(3, 10, 0)) {
            gtk3_version_3_10 = FALSE;
        } else {
            fp_gdk_window_create_similar_image_surface =
                       dl_symbol("gdk_window_create_similar_image_surface");
        }
        gtk3_version_3_14 = !fp_gtk_check_version(3, 14, 0);

        fp_gdk_window_create_similar_surface =
                      dl_symbol("gdk_window_create_similar_surface");
        fp_gtk_settings_get_for_screen =
                      dl_symbol("gtk_settings_get_for_screen");
        fp_gtk_widget_get_screen = dl_symbol("gtk_widget_get_screen");
        fp_gtk_css_provider_get_named = dl_symbol("gtk_css_provider_get_named");
        fp_gtk_style_context_add_provider =
                      dl_symbol("gtk_style_context_add_provider");
        fp_gtk_render_frame = dl_symbol("gtk_render_frame");
        fp_gtk_render_focus = dl_symbol("gtk_render_focus");
        fp_gtk_render_handle = dl_symbol("gtk_render_handle");
        fp_gtk_render_arrow = dl_symbol("gtk_render_arrow");

        fp_gtk_style_context_get_property =
                      dl_symbol("gtk_style_context_get_property");
        fp_gtk_scrolled_window_set_shadow_type =
                      dl_symbol("gtk_scrolled_window_set_shadow_type");
        fp_gtk_render_slider = dl_symbol("gtk_render_slider");
        fp_gtk_style_context_get_padding =
                      dl_symbol("gtk_style_context_get_padding");
        fp_gtk_range_set_inverted = dl_symbol("gtk_range_set_inverted");
        fp_gtk_style_context_get_font = dl_symbol("gtk_style_context_get_font");
        fp_gtk_widget_get_allocated_width =
                      dl_symbol("gtk_widget_get_allocated_width");
        fp_gtk_widget_get_allocated_height =
                      dl_symbol("gtk_widget_get_allocated_height");
        fp_gtk_icon_theme_get_default = dl_symbol("gtk_icon_theme_get_default");
        fp_gtk_icon_theme_load_icon = dl_symbol("gtk_icon_theme_load_icon");

        fp_gtk_adjustment_set_lower = dl_symbol("gtk_adjustment_set_lower");
        fp_gtk_adjustment_set_page_increment =
                      dl_symbol("gtk_adjustment_set_page_increment");
        fp_gtk_adjustment_set_page_size =
                      dl_symbol("gtk_adjustment_set_page_size");
        fp_gtk_adjustment_set_step_increment =
                      dl_symbol("gtk_adjustment_set_step_increment");
        fp_gtk_adjustment_set_upper = dl_symbol("gtk_adjustment_set_upper");
        fp_gtk_adjustment_set_value = dl_symbol("gtk_adjustment_set_value");

        fp_gtk_render_activity = dl_symbol("gtk_render_activity");
        fp_gtk_render_background = dl_symbol("gtk_render_background");
        fp_gtk_style_context_has_class =
                      dl_symbol("gtk_style_context_has_class");

        fp_gtk_style_context_set_junction_sides =
                      dl_symbol("gtk_style_context_set_junction_sides");
        fp_gtk_style_context_add_region =
                      dl_symbol("gtk_style_context_add_region");

        fp_gtk_init_check = dl_symbol("gtk_init_check");

        /* GTK widgets */
        fp_gtk_arrow_new = dl_symbol("gtk_arrow_new");
        fp_gtk_button_new = dl_symbol("gtk_button_new");
        fp_gtk_spin_button_new = dl_symbol("gtk_spin_button_new");
        fp_gtk_check_button_new = dl_symbol("gtk_check_button_new");
        fp_gtk_check_menu_item_new =
                dl_symbol("gtk_check_menu_item_new");
        fp_gtk_color_selection_dialog_new =
                dl_symbol("gtk_color_selection_dialog_new");
        fp_gtk_entry_new = dl_symbol("gtk_entry_new");
        fp_gtk_fixed_new = dl_symbol("gtk_fixed_new");
        fp_gtk_handle_box_new = dl_symbol("gtk_handle_box_new");
        fp_gtk_image_new = dl_symbol("gtk_image_new");
        fp_gtk_hpaned_new = dl_symbol("gtk_hpaned_new");
        fp_gtk_vpaned_new = dl_symbol("gtk_vpaned_new");
        fp_gtk_scale_new = dl_symbol("gtk_scale_new");
        fp_gtk_hscrollbar_new = dl_symbol("gtk_hscrollbar_new");
        fp_gtk_vscrollbar_new = dl_symbol("gtk_vscrollbar_new");
        fp_gtk_hseparator_new = dl_symbol("gtk_hseparator_new");
        fp_gtk_vseparator_new = dl_symbol("gtk_vseparator_new");
        fp_gtk_label_new = dl_symbol("gtk_label_new");
        fp_gtk_menu_new = dl_symbol("gtk_menu_new");
        fp_gtk_menu_bar_new = dl_symbol("gtk_menu_bar_new");
        fp_gtk_menu_item_new = dl_symbol("gtk_menu_item_new");
        fp_gtk_menu_item_set_submenu =
                dl_symbol("gtk_menu_item_set_submenu");
        fp_gtk_notebook_new = dl_symbol("gtk_notebook_new");
        fp_gtk_progress_bar_new =
            dl_symbol("gtk_progress_bar_new");
        fp_gtk_progress_bar_set_orientation =
            dl_symbol("gtk_orientable_set_orientation");
        fp_gtk_radio_button_new =
            dl_symbol("gtk_radio_button_new");
        fp_gtk_radio_menu_item_new =
            dl_symbol("gtk_radio_menu_item_new");
        fp_gtk_scrolled_window_new =
            dl_symbol("gtk_scrolled_window_new");
        fp_gtk_separator_menu_item_new =
            dl_symbol("gtk_separator_menu_item_new");
        fp_gtk_text_view_new = dl_symbol("gtk_text_view_new");
        fp_gtk_toggle_button_new =
            dl_symbol("gtk_toggle_button_new");
        fp_gtk_toolbar_new = dl_symbol("gtk_toolbar_new");
        fp_gtk_tree_view_new = dl_symbol("gtk_tree_view_new");
        fp_gtk_viewport_new = dl_symbol("gtk_viewport_new");
        fp_gtk_window_new = dl_symbol("gtk_window_new");
        fp_gtk_window_present = dl_symbol("gtk_window_present");
        fp_gtk_window_move = dl_symbol("gtk_window_move");
        fp_gtk_window_resize = dl_symbol("gtk_window_resize");

          fp_gtk_dialog_new = dl_symbol("gtk_dialog_new");
        fp_gtk_frame_new = dl_symbol("gtk_frame_new");

        fp_gtk_adjustment_new = dl_symbol("gtk_adjustment_new");
        fp_gtk_container_add = dl_symbol("gtk_container_add");
        fp_gtk_menu_shell_append =
            dl_symbol("gtk_menu_shell_append");
        fp_gtk_widget_realize = dl_symbol("gtk_widget_realize");
        fp_gtk_widget_destroy = dl_symbol("gtk_widget_destroy");
        fp_gtk_widget_render_icon =
            dl_symbol("gtk_widget_render_icon");
        fp_gtk_widget_set_name =
            dl_symbol("gtk_widget_set_name");
        fp_gtk_widget_set_parent =
            dl_symbol("gtk_widget_set_parent");
        fp_gtk_widget_set_direction =
            dl_symbol("gtk_widget_set_direction");
        fp_gtk_widget_style_get =
            dl_symbol("gtk_widget_style_get");
        fp_gtk_widget_class_install_style_property =
            dl_symbol("gtk_widget_class_install_style_property");
        fp_gtk_widget_class_find_style_property =
            dl_symbol("gtk_widget_class_find_style_property");
        fp_gtk_widget_style_get_property =
            dl_symbol("gtk_widget_style_get_property");
        fp_pango_font_description_to_string =
            dl_symbol("pango_font_description_to_string");
        fp_gtk_settings_get_default =
            dl_symbol("gtk_settings_get_default");
        fp_gtk_widget_get_settings =
            dl_symbol("gtk_widget_get_settings");
        fp_gtk_border_get_type =  dl_symbol("gtk_border_get_type");
        fp_gtk_arrow_set = dl_symbol("gtk_arrow_set");
        fp_gtk_widget_size_request =
            dl_symbol("gtk_widget_size_request");
        fp_gtk_range_get_adjustment =
            dl_symbol("gtk_range_get_adjustment");

        fp_gtk_widget_hide = dl_symbol("gtk_widget_hide");
        fp_gtk_main_quit = dl_symbol("gtk_main_quit");
        fp_g_signal_connect_data = dl_symbol("g_signal_connect_data");
        fp_gtk_widget_show = dl_symbol("gtk_widget_show");
        fp_gtk_main = dl_symbol("gtk_main");

        fp_g_path_get_dirname = dl_symbol("g_path_get_dirname");

        fp_gdk_threads_init = dl_symbol("gdk_threads_init");
        fp_gdk_threads_enter = dl_symbol("gdk_threads_enter");
        fp_gdk_threads_leave = dl_symbol("gdk_threads_leave");

        /**
         * Functions for sun_awt_X11_GtkFileDialogPeer.c
         */
        gtk3_file_chooser_load();

        fp_gtk_combo_box_new = dlsym(gtk3_libhandle, "gtk_combo_box_new");
        fp_gtk_combo_box_entry_new = dlsym(gtk3_libhandle,
                                                "gtk_combo_box_new_with_entry");
        fp_gtk_separator_tool_item_new = dlsym(gtk3_libhandle,
                                                 "gtk_separator_tool_item_new");

        fp_g_list_append = dl_symbol("g_list_append");
        fp_g_list_free = dl_symbol("g_list_free");
        fp_g_list_free_full = dl_symbol("g_list_free_full");
    }
    /* Now we have only one kind of exceptions: NO_SYMBOL_EXCEPTION
     * Otherwise we can check the return value of setjmp method.
     */
    else
    {
        dlclose(gtk3_libhandle);
        gtk3_libhandle = NULL;

        dlclose(gthread_libhandle);
        gthread_libhandle = NULL;

        return NULL;
    }

    /*
     * Strip the AT-SPI GTK_MODULES if present
     */
    gtk_modules_env = getenv ("GTK_MODULES");
    if ((gtk_modules_env && strstr(gtk_modules_env, "atk-bridge")) ||
        (gtk_modules_env && strstr(gtk_modules_env, "gail"))) {
        /* careful, strtok modifies its args */
        gchar *tmp_env = strdup(gtk_modules_env);
        if (tmp_env) {
            /* the new env will be smaller than the old one */
            gchar *s, *new_env = SAFE_SIZE_STRUCT_ALLOC(malloc,
                    sizeof(ENV_PREFIX), 1, strlen (gtk_modules_env));

            if (new_env) {
                strcpy(new_env, ENV_PREFIX);

                /* strip out 'atk-bridge' and 'gail' */
                size_t PREFIX_LENGTH = strlen(ENV_PREFIX);
                gchar *tmp_ptr = NULL;
                for (s = strtok_r(tmp_env, ":", &tmp_ptr); s;
                     s = strtok_r(NULL, ":", &tmp_ptr)) {
                    if ((!strstr(s, "atk-bridge")) && (!strstr(s, "gail"))) {
                        if (strlen(new_env) > PREFIX_LENGTH) {
                            new_env = strcat(new_env, ":");
                        }
                        new_env = strcat(new_env, s);
                     }
                 }
                 if (putenv(new_env) != 0) {
                    /* no free() on success, putenv() doesn't copy string */
                    free(new_env);
                 }
             }
            free (tmp_env);
        }
    }
    /*
     * GTK should be initialized with gtk_init_check() before use.
     *
     * gtk_init_check installs its own error handlers. It is critical that
     * we preserve error handler set from AWT. Otherwise we'll crash on
     * BadMatch errors which we would normally ignore. The IO error handler
     * is preserved here, too, just for consistency.
    */
    AWT_LOCK();
    handler = XSetErrorHandler(NULL);
    io_handler = XSetIOErrorHandler(NULL);

    //According the GTK documentation, gdk_threads_init() should be
    //called before gtk_init() or gtk_init_check()
    fp_gdk_threads_init();
    result = (*fp_gtk_init_check)(NULL, NULL);

    XSetErrorHandler(handler);
    XSetIOErrorHandler(io_handler);
    AWT_UNLOCK();

    /* Initialize widget array. */
    for (i = 0; i < _GTK_WIDGET_TYPE_SIZE; i++)
    {
        gtk3_widgets[i] = NULL;
    }
    if (result) {
        GtkApi* gtk = (GtkApi*)malloc(sizeof(GtkApi));
        gtk3_init(gtk);
        return gtk;
    }
    return NULL;
}

static int gtk3_unload()
{
    int i;
    char *gtk3_error;

    if (!gtk3_libhandle)
        return TRUE;

    /* Release painting objects */
    if (surface != NULL) {
        fp_cairo_destroy(cr);
        fp_cairo_surface_destroy(surface);
        surface = NULL;
    }

    if (gtk3_window != NULL) {
        /* Destroying toplevel widget will destroy all contained widgets */
        (*fp_gtk_widget_destroy)(gtk3_window);

        /* Unset some static data so they get reinitialized on next load */
        gtk3_window = NULL;
    }

    dlerror();
    dlclose(gtk3_libhandle);
    dlclose(gthread_libhandle);
    if ((gtk3_error = dlerror()) != NULL)
    {
        return FALSE;
    }
    return TRUE;
}

/* Dispatch all pending events from the GTK event loop.
 * This is needed to catch theme change and update widgets' style.
 */
static void flush_gtk_event_loop()
{
    while((*fp_g_main_context_iteration)(NULL));
}

/*
 * Initialize components of containment hierarchy. This creates a GtkFixed
 * inside a GtkWindow. All widgets get realized.
 */
static void init_containers()
{
    if (gtk3_window == NULL)
    {
        gtk3_window = (*fp_gtk_window_new)(GTK_WINDOW_TOPLEVEL);
        gtk3_fixed = (GtkFixed *)(*fp_gtk_fixed_new)();
        (*fp_gtk_container_add)((GtkContainer*)gtk3_window,
                                (GtkWidget *)gtk3_fixed);
        (*fp_gtk_widget_realize)(gtk3_window);
        (*fp_gtk_widget_realize)((GtkWidget *)gtk3_fixed);

        GtkSettings* settings = fp_gtk_settings_get_for_screen(
                                         fp_gtk_widget_get_screen(gtk3_window));
        gchar*  strval = NULL;
        fp_g_object_get(settings, "gtk-theme-name", &strval, NULL);
        gtk3_css = fp_gtk_css_provider_get_named(strval, NULL);
    }
}

/*
 * Ensure everything is ready for drawing an element of the specified width
 * and height.
 *
 * We should somehow handle translucent images. GTK can draw to X Drawables
 * only, which don't support alpha. When we retrieve the image back from
 * the server, translucency information is lost. There're several ways to
 * work around this:
 * 1) Subclass GdkPixmap and cache translucent objects on client side. This
 * requires us to implement parts of X server drawing logic on client side.
 * Many X requests can potentially be "translucent"; e.g. XDrawLine with
 * fill=tile and a translucent tile is a "translucent" operation, whereas
 * XDrawLine with fill=solid is an "opaque" one. Moreover themes can (and some
 * do) intermix transparent and opaque operations which makes caching even
 * more problematic.
 * 2) Use Xorg 32bit ARGB visual when available. GDK has no native support
 * for it (as of version 2.6). Also even in JDS 3 Xorg does not support
 * these visuals by default, which makes optimizing for them pointless.
 * We can consider doing this at a later point when ARGB visuals become more
 * popular.
 * 3') GTK has plans to use Cairo as its graphical backend (presumably in
 * 2.8), and Cairo supports alpha. With it we could also get rid of the
 * unnecessary round trip to server and do all the drawing on client side.
 * 4) For now we draw to two different pixmaps and restore alpha channel by
 * comparing results. This can be optimized by using subclassed pixmap and
*/
static void gtk3_init_painting(JNIEnv *env, gint width, gint height)
{
    init_containers();

    if (cr) {
        fp_cairo_destroy(cr);
    }

    if (surface != NULL) {
        /* free old stuff */
        fp_cairo_surface_destroy(surface);

    }

    if (gtk3_version_3_10) {
        surface = fp_gdk_window_create_similar_image_surface(
                           fp_gtk_widget_get_window(gtk3_window),
                                         CAIRO_FORMAT_ARGB32, width, height, 1);
    } else {
        surface = fp_cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
                                                                 width, height);
    }

    cr = fp_cairo_create(surface);
}

/*
 * Restore image from white and black pixmaps and copy it into destination
 * buffer. This method compares two pixbufs taken from white and black
 * pixmaps and decodes color and alpha components. Pixbufs are RGB without
 * alpha, destination buffer is ABGR.
 *
 * The return value is the transparency type of the resulting image, either
 * one of java_awt_Transparency_OPAQUE, java_awt_Transparency_BITMASK, and
 * java_awt_Transparency_TRANSLUCENT.
 */
static gint gtk3_copy_image(gint *dst, gint width, gint height)
{
    gint i, j, r, g, b;
    guchar *data;
    gint stride, padding;

    fp_cairo_surface_flush(surface);
    data = (*fp_cairo_image_surface_get_data)(surface);
    stride = (*fp_cairo_image_surface_get_stride)(surface);
    padding = stride - width * 4;

    for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
            int r = *data++;
            int g = *data++;
            int b = *data++;
            int a = *data++;
            *dst++ = (a << 24 | b << 16 | g << 8 | r);
        }
        data += padding;
    }
    return java_awt_Transparency_TRANSLUCENT;
}

static void gtk3_set_direction(GtkWidget *widget, GtkTextDirection dir)
{
    /*
     * Some engines (inexplicably) look at the direction of the widget's
     * parent, so we need to set the direction of both the widget and its
     * parent.
     */
    (*fp_gtk_widget_set_direction)(widget, dir);
    GtkWidget* parent = fp_gtk_widget_get_parent(widget);
    if (parent != NULL) {
        fp_gtk_widget_set_direction(parent, dir);
    }
}

/* GTK state_type filter */
static GtkStateType get_gtk_state_type(WidgetType widget_type, gint synth_state)
{
    GtkStateType result = GTK_STATE_NORMAL;

    if ((synth_state & DISABLED) != 0) {
        result = GTK_STATE_INSENSITIVE;
    } else if ((synth_state & PRESSED) != 0) {
        result = GTK_STATE_ACTIVE;
    } else if ((synth_state & MOUSE_OVER) != 0) {
        result = GTK_STATE_PRELIGHT;
    }
    return result;
}

static GtkStateFlags get_gtk_state_flags(gint synth_state)
{
    GtkStateFlags flags = 0;

    if ((synth_state & DISABLED) != 0) {
        flags |= GTK_STATE_FLAG_INSENSITIVE;
    }
    if (((synth_state & PRESSED) != 0 || (synth_state & SELECTED) != 0)) {
        flags |= GTK_STATE_FLAG_ACTIVE;
    }
    if ((synth_state & MOUSE_OVER) != 0) {
        flags |= GTK_STATE_FLAG_PRELIGHT;
    }
    if ((synth_state & FOCUSED) != 0) {
        flags |= GTK_STATE_FLAG_FOCUSED;
    }
    return flags;
}

static GtkStateFlags get_gtk_flags(GtkStateType state_type) {
    GtkStateFlags flags = 0;
    switch (state_type)
    {
        case GTK_STATE_PRELIGHT:
          flags |= GTK_STATE_FLAG_PRELIGHT;
          break;
        case GTK_STATE_SELECTED:
          flags |= GTK_STATE_FLAG_SELECTED;
          break;
        case GTK_STATE_INSENSITIVE:
          flags |= GTK_STATE_FLAG_INSENSITIVE;
          break;
        case GTK_STATE_ACTIVE:
          flags |= GTK_STATE_FLAG_ACTIVE;
          break;
        case GTK_STATE_FOCUSED:
          flags |= GTK_STATE_FLAG_FOCUSED;
          break;
        default:
          break;
    }
    return flags;
}

/* GTK shadow_type filter */
static GtkShadowType get_gtk_shadow_type(WidgetType widget_type,
                                                               gint synth_state)
{
    GtkShadowType result = GTK_SHADOW_OUT;

    if ((synth_state & SELECTED) != 0) {
        result = GTK_SHADOW_IN;
    }
    return result;
}


static GtkWidget* gtk3_get_arrow(GtkArrowType arrow_type,
                                                      GtkShadowType shadow_type)
{
    GtkWidget *arrow = NULL;
    if (NULL == gtk3_widgets[_GTK_ARROW_TYPE])
    {
        gtk3_widgets[_GTK_ARROW_TYPE] = (*fp_gtk_arrow_new)(arrow_type,
                                                                   shadow_type);
        (*fp_gtk_container_add)((GtkContainer *)gtk3_fixed,
                                                 gtk3_widgets[_GTK_ARROW_TYPE]);
        (*fp_gtk_widget_realize)(gtk3_widgets[_GTK_ARROW_TYPE]);
    }
    arrow = gtk3_widgets[_GTK_ARROW_TYPE];

    (*fp_gtk_arrow_set)(arrow, arrow_type, shadow_type);
    return arrow;
}

static GtkAdjustment* create_adjustment()
{
    return (GtkAdjustment *)
            (*fp_gtk_adjustment_new)(50.0, 0.0, 100.0, 10.0, 20.0, 20.0);
}

/**
 * Returns a pointer to the cached native widget for the specified widget
 * type.
 */
static GtkWidget *gtk3_get_widget(WidgetType widget_type)
{
    gboolean init_result = FALSE;
    GtkWidget *result = NULL;
    switch (widget_type)
    {
        case BUTTON:
        case TABLE_HEADER:
            if (init_result = (NULL == gtk3_widgets[_GTK_BUTTON_TYPE]))
            {
                gtk3_widgets[_GTK_BUTTON_TYPE] = (*fp_gtk_button_new)();
            }
            result = gtk3_widgets[_GTK_BUTTON_TYPE];
            break;
        case CHECK_BOX:
            if (init_result = (NULL == gtk3_widgets[_GTK_CHECK_BUTTON_TYPE]))
            {
                gtk3_widgets[_GTK_CHECK_BUTTON_TYPE] =
                    (*fp_gtk_check_button_new)();
            }
            result = gtk3_widgets[_GTK_CHECK_BUTTON_TYPE];
            break;
        case CHECK_BOX_MENU_ITEM:
            if (init_result = (NULL == gtk3_widgets[_GTK_CHECK_MENU_ITEM_TYPE]))
            {
                gtk3_widgets[_GTK_CHECK_MENU_ITEM_TYPE] =
                    (*fp_gtk_check_menu_item_new)();
            }
            result = gtk3_widgets[_GTK_CHECK_MENU_ITEM_TYPE];
            break;
        /************************************************************
         *    Creation a dedicated color chooser is dangerous because
         * it deadlocks the EDT
         ************************************************************/
/*        case COLOR_CHOOSER:
            if (init_result =
                    (NULL == gtk3_widgets[_GTK_COLOR_SELECTION_DIALOG_TYPE]))
            {
                gtk3_widgets[_GTK_COLOR_SELECTION_DIALOG_TYPE] =
                    (*fp_gtk_color_selection_dialog_new)(NULL);
            }
            result = gtk3_widgets[_GTK_COLOR_SELECTION_DIALOG_TYPE];
            break;*/
        case COMBO_BOX:
            if (init_result = (NULL == gtk3_widgets[_GTK_COMBO_BOX_TYPE]))
            {
                gtk3_widgets[_GTK_COMBO_BOX_TYPE] =
                    (*fp_gtk_combo_box_new)();
            }
            result = gtk3_widgets[_GTK_COMBO_BOX_TYPE];
            break;
        case COMBO_BOX_ARROW_BUTTON:
            if (init_result =
                    (NULL == gtk3_widgets[_GTK_COMBO_BOX_ARROW_BUTTON_TYPE]))
            {
                gtk3_widgets[_GTK_COMBO_BOX_ARROW_BUTTON_TYPE] =
                     (*fp_gtk_toggle_button_new)();
            }
            result = gtk3_widgets[_GTK_COMBO_BOX_ARROW_BUTTON_TYPE];
            break;
        case COMBO_BOX_TEXT_FIELD:
            if (init_result =
                    (NULL == gtk3_widgets[_GTK_COMBO_BOX_TEXT_FIELD_TYPE]))
            {
                result = gtk3_widgets[_GTK_COMBO_BOX_TEXT_FIELD_TYPE] =
                     (*fp_gtk_entry_new)();
            }
            result = gtk3_widgets[_GTK_COMBO_BOX_TEXT_FIELD_TYPE];
            break;
        case DESKTOP_ICON:
        case INTERNAL_FRAME_TITLE_PANE:
        case LABEL:
            if (init_result = (NULL == gtk3_widgets[_GTK_LABEL_TYPE]))
            {
                gtk3_widgets[_GTK_LABEL_TYPE] =
                    (*fp_gtk_label_new)(NULL);
            }
            result = gtk3_widgets[_GTK_LABEL_TYPE];
            break;
        case DESKTOP_PANE:
        case PANEL:
        case ROOT_PANE:
            if (init_result = (NULL == gtk3_widgets[_GTK_CONTAINER_TYPE]))
            {
                /* There is no constructor for a container type.  I've
                 * chosen GtkFixed container since it has a default
                 * constructor.
                 */
                gtk3_widgets[_GTK_CONTAINER_TYPE] =
                    (*fp_gtk_fixed_new)();
            }
            result = gtk3_widgets[_GTK_CONTAINER_TYPE];
            break;
        case EDITOR_PANE:
        case TEXT_AREA:
        case TEXT_PANE:
            if (init_result = (NULL == gtk3_widgets[_GTK_TEXT_VIEW_TYPE]))
            {
                gtk3_widgets[_GTK_TEXT_VIEW_TYPE] =
                    (*fp_gtk_text_view_new)();
            }
            result = gtk3_widgets[_GTK_TEXT_VIEW_TYPE];
            break;
        case FORMATTED_TEXT_FIELD:
        case PASSWORD_FIELD:
        case TEXT_FIELD:
            if (init_result = (NULL == gtk3_widgets[_GTK_ENTRY_TYPE]))
            {
                gtk3_widgets[_GTK_ENTRY_TYPE] =
                    (*fp_gtk_entry_new)();
            }
            result = gtk3_widgets[_GTK_ENTRY_TYPE];
            break;
        case HANDLE_BOX:
            if (init_result = (NULL == gtk3_widgets[_GTK_HANDLE_BOX_TYPE]))
            {
                gtk3_widgets[_GTK_HANDLE_BOX_TYPE] =
                    (*fp_gtk_handle_box_new)();
            }
            result = gtk3_widgets[_GTK_HANDLE_BOX_TYPE];
            break;
        case HSCROLL_BAR:
        case HSCROLL_BAR_BUTTON_LEFT:
        case HSCROLL_BAR_BUTTON_RIGHT:
        case HSCROLL_BAR_TRACK:
        case HSCROLL_BAR_THUMB:
            if (init_result = (NULL == gtk3_widgets[_GTK_HSCROLLBAR_TYPE]))
            {
                gtk3_widgets[_GTK_HSCROLLBAR_TYPE] =
                    (*fp_gtk_hscrollbar_new)(create_adjustment());
            }
            result = gtk3_widgets[_GTK_HSCROLLBAR_TYPE];
            break;
        case HSEPARATOR:
            if (init_result = (NULL == gtk3_widgets[_GTK_HSEPARATOR_TYPE]))
            {
                gtk3_widgets[_GTK_HSEPARATOR_TYPE] =
                    (*fp_gtk_hseparator_new)();
            }
            result = gtk3_widgets[_GTK_HSEPARATOR_TYPE];
            break;
        case HSLIDER:
        case HSLIDER_THUMB:
        case HSLIDER_TRACK:
            if (init_result = (NULL == gtk3_widgets[_GTK_HSCALE_TYPE]))
            {
                gtk3_widgets[_GTK_HSCALE_TYPE] =
                    (*fp_gtk_scale_new)(GTK_ORIENTATION_HORIZONTAL, NULL);
            }
            result = gtk3_widgets[_GTK_HSCALE_TYPE];
            break;
        case HSPLIT_PANE_DIVIDER:
        case SPLIT_PANE:
            if (init_result = (NULL == gtk3_widgets[_GTK_HPANED_TYPE]))
            {
                gtk3_widgets[_GTK_HPANED_TYPE] = (*fp_gtk_hpaned_new)();
            }
            result = gtk3_widgets[_GTK_HPANED_TYPE];
            break;
        case IMAGE:
            if (init_result = (NULL == gtk3_widgets[_GTK_IMAGE_TYPE]))
            {
                gtk3_widgets[_GTK_IMAGE_TYPE] = (*fp_gtk_image_new)();
            }
            result = gtk3_widgets[_GTK_IMAGE_TYPE];
            break;
        case INTERNAL_FRAME:
            if (init_result = (NULL == gtk3_widgets[_GTK_WINDOW_TYPE]))
            {
                gtk3_widgets[_GTK_WINDOW_TYPE] =
                    (*fp_gtk_window_new)(GTK_WINDOW_TOPLEVEL);
            }
            result = gtk3_widgets[_GTK_WINDOW_TYPE];
            break;
        case TOOL_TIP:
            if (init_result = (NULL == gtk3_widgets[_GTK_TOOLTIP_TYPE]))
            {
                result = (*fp_gtk_window_new)(GTK_WINDOW_TOPLEVEL);
                gtk3_widgets[_GTK_TOOLTIP_TYPE] = result;
            }
            result = gtk3_widgets[_GTK_TOOLTIP_TYPE];
            break;
        case LIST:
        case TABLE:
        case TREE:
        case TREE_CELL:
            if (init_result = (NULL == gtk3_widgets[_GTK_TREE_VIEW_TYPE]))
            {
                gtk3_widgets[_GTK_TREE_VIEW_TYPE] =
                    (*fp_gtk_tree_view_new)();
            }
            result = gtk3_widgets[_GTK_TREE_VIEW_TYPE];
            break;
        case TITLED_BORDER:
            if (init_result = (NULL == gtk3_widgets[_GTK_FRAME_TYPE]))
            {
                gtk3_widgets[_GTK_FRAME_TYPE] = fp_gtk_frame_new(NULL);
            }
            result = gtk3_widgets[_GTK_FRAME_TYPE];
            break;
        case POPUP_MENU:
            if (init_result = (NULL == gtk3_widgets[_GTK_MENU_TYPE]))
            {
                gtk3_widgets[_GTK_MENU_TYPE] =
                    (*fp_gtk_menu_new)();
            }
            result = gtk3_widgets[_GTK_MENU_TYPE];
            break;
        case MENU:
        case MENU_ITEM:
        case MENU_ITEM_ACCELERATOR:
            if (init_result = (NULL == gtk3_widgets[_GTK_MENU_ITEM_TYPE]))
            {
                gtk3_widgets[_GTK_MENU_ITEM_TYPE] =
                    (*fp_gtk_menu_item_new)();
            }
            result = gtk3_widgets[_GTK_MENU_ITEM_TYPE];
            break;
        case MENU_BAR:
            if (init_result = (NULL == gtk3_widgets[_GTK_MENU_BAR_TYPE]))
            {
                gtk3_widgets[_GTK_MENU_BAR_TYPE] =
                    (*fp_gtk_menu_bar_new)();
            }
            result = gtk3_widgets[_GTK_MENU_BAR_TYPE];
            break;
        case COLOR_CHOOSER:
        case OPTION_PANE:
            if (init_result = (NULL == gtk3_widgets[_GTK_DIALOG_TYPE]))
            {
                gtk3_widgets[_GTK_DIALOG_TYPE] =
                    (*fp_gtk_dialog_new)();
            }
            result = gtk3_widgets[_GTK_DIALOG_TYPE];
            break;
        case POPUP_MENU_SEPARATOR:
            if (init_result =
                    (NULL == gtk3_widgets[_GTK_SEPARATOR_MENU_ITEM_TYPE]))
            {
                gtk3_widgets[_GTK_SEPARATOR_MENU_ITEM_TYPE] =
                    (*fp_gtk_separator_menu_item_new)();
            }
            result = gtk3_widgets[_GTK_SEPARATOR_MENU_ITEM_TYPE];
            break;
        case HPROGRESS_BAR:
            if (init_result = (NULL == gtk3_widgets[_GTK_HPROGRESS_BAR_TYPE]))
            {
                gtk3_widgets[_GTK_HPROGRESS_BAR_TYPE] =
                    (*fp_gtk_progress_bar_new)();
            }
            result = gtk3_widgets[_GTK_HPROGRESS_BAR_TYPE];
            break;
        case VPROGRESS_BAR:
            if (init_result = (NULL == gtk3_widgets[_GTK_VPROGRESS_BAR_TYPE]))
            {
                gtk3_widgets[_GTK_VPROGRESS_BAR_TYPE] =
                    (*fp_gtk_progress_bar_new)();
                /*
                 * Vertical JProgressBars always go bottom-to-top,
                 * regardless of the ComponentOrientation.
                 */
                (*fp_gtk_progress_bar_set_orientation)(
                    (GtkProgressBar *)gtk3_widgets[_GTK_VPROGRESS_BAR_TYPE],
                    GTK_PROGRESS_BOTTOM_TO_TOP);
            }
            result = gtk3_widgets[_GTK_VPROGRESS_BAR_TYPE];
            break;
        case RADIO_BUTTON:
            if (init_result = (NULL == gtk3_widgets[_GTK_RADIO_BUTTON_TYPE]))
            {
                gtk3_widgets[_GTK_RADIO_BUTTON_TYPE] =
                    (*fp_gtk_radio_button_new)(NULL);
            }
            result = gtk3_widgets[_GTK_RADIO_BUTTON_TYPE];
            break;
        case RADIO_BUTTON_MENU_ITEM:
            if (init_result =
                    (NULL == gtk3_widgets[_GTK_RADIO_MENU_ITEM_TYPE]))
            {
                gtk3_widgets[_GTK_RADIO_MENU_ITEM_TYPE] =
                    (*fp_gtk_radio_menu_item_new)(NULL);
            }
            result = gtk3_widgets[_GTK_RADIO_MENU_ITEM_TYPE];
            break;
        case SCROLL_PANE:
            if (init_result =
                    (NULL == gtk3_widgets[_GTK_SCROLLED_WINDOW_TYPE]))
            {
                gtk3_widgets[_GTK_SCROLLED_WINDOW_TYPE] =
                    (*fp_gtk_scrolled_window_new)(NULL, NULL);
            }
            result = gtk3_widgets[_GTK_SCROLLED_WINDOW_TYPE];
            break;
        case SPINNER:
        case SPINNER_ARROW_BUTTON:
        case SPINNER_TEXT_FIELD:
            if (init_result = (NULL == gtk3_widgets[_GTK_SPIN_BUTTON_TYPE]))
            {
                result = gtk3_widgets[_GTK_SPIN_BUTTON_TYPE] =
                    (*fp_gtk_spin_button_new)(NULL, 0, 0);
            }
            result = gtk3_widgets[_GTK_SPIN_BUTTON_TYPE];
            break;
        case TABBED_PANE:
        case TABBED_PANE_TAB_AREA:
        case TABBED_PANE_CONTENT:
        case TABBED_PANE_TAB:
            if (init_result = (NULL == gtk3_widgets[_GTK_NOTEBOOK_TYPE]))
            {
                gtk3_widgets[_GTK_NOTEBOOK_TYPE] =
                    (*fp_gtk_notebook_new)(NULL);
            }
            result = gtk3_widgets[_GTK_NOTEBOOK_TYPE];
            break;
        case TOGGLE_BUTTON:
            if (init_result = (NULL == gtk3_widgets[_GTK_TOGGLE_BUTTON_TYPE]))
            {
                gtk3_widgets[_GTK_TOGGLE_BUTTON_TYPE] =
                    (*fp_gtk_toggle_button_new)(NULL);
            }
            result = gtk3_widgets[_GTK_TOGGLE_BUTTON_TYPE];
            break;
        case TOOL_BAR:
        case TOOL_BAR_DRAG_WINDOW:
            if (init_result = (NULL == gtk3_widgets[_GTK_TOOLBAR_TYPE]))
            {
                gtk3_widgets[_GTK_TOOLBAR_TYPE] =
                    (*fp_gtk_toolbar_new)(NULL);
            }
            result = gtk3_widgets[_GTK_TOOLBAR_TYPE];
            break;
        case TOOL_BAR_SEPARATOR:
            if (init_result =
                    (NULL == gtk3_widgets[_GTK_SEPARATOR_TOOL_ITEM_TYPE]))
            {
                gtk3_widgets[_GTK_SEPARATOR_TOOL_ITEM_TYPE] =
                    (*fp_gtk_separator_tool_item_new)();
            }
            result = gtk3_widgets[_GTK_SEPARATOR_TOOL_ITEM_TYPE];
            break;
        case VIEWPORT:
            if (init_result = (NULL == gtk3_widgets[_GTK_VIEWPORT_TYPE]))
            {
                GtkAdjustment *adjustment = create_adjustment();
                gtk3_widgets[_GTK_VIEWPORT_TYPE] =
                    (*fp_gtk_viewport_new)(adjustment, adjustment);
            }
            result = gtk3_widgets[_GTK_VIEWPORT_TYPE];
            break;
        case VSCROLL_BAR:
        case VSCROLL_BAR_BUTTON_UP:
        case VSCROLL_BAR_BUTTON_DOWN:
        case VSCROLL_BAR_TRACK:
        case VSCROLL_BAR_THUMB:
            if (init_result = (NULL == gtk3_widgets[_GTK_VSCROLLBAR_TYPE]))
            {
                gtk3_widgets[_GTK_VSCROLLBAR_TYPE] =
                    (*fp_gtk_vscrollbar_new)(create_adjustment());
            }
            result = gtk3_widgets[_GTK_VSCROLLBAR_TYPE];
            break;
        case VSEPARATOR:
            if (init_result = (NULL == gtk3_widgets[_GTK_VSEPARATOR_TYPE]))
            {
                gtk3_widgets[_GTK_VSEPARATOR_TYPE] =
                    (*fp_gtk_vseparator_new)();
            }
            result = gtk3_widgets[_GTK_VSEPARATOR_TYPE];
            break;
        case VSLIDER:
        case VSLIDER_THUMB:
        case VSLIDER_TRACK:
            if (init_result = (NULL == gtk3_widgets[_GTK_VSCALE_TYPE]))
            {
                gtk3_widgets[_GTK_VSCALE_TYPE] =
                    (*fp_gtk_scale_new)(GTK_ORIENTATION_VERTICAL, NULL);
            }
            result = gtk3_widgets[_GTK_VSCALE_TYPE];
            /*
             * Vertical JSliders start at the bottom, while vertical
             * GtkVScale widgets start at the top (by default), so to fix
             * this we set the "inverted" flag to get the Swing behavior.
             */
             fp_gtk_range_set_inverted((GtkRange*)result, TRUE);
            break;
        case VSPLIT_PANE_DIVIDER:
            if (init_result = (NULL == gtk3_widgets[_GTK_VPANED_TYPE]))
            {
                gtk3_widgets[_GTK_VPANED_TYPE] = (*fp_gtk_vpaned_new)();
            }
            result = gtk3_widgets[_GTK_VPANED_TYPE];
            break;
        default:
            result = NULL;
            break;
    }

    if (result != NULL && init_result)
    {
        if (widget_type == RADIO_BUTTON_MENU_ITEM ||
                widget_type == CHECK_BOX_MENU_ITEM ||
                widget_type == MENU_ITEM ||
                widget_type == MENU ||
                widget_type == POPUP_MENU_SEPARATOR)
        {
            GtkWidget *menu = gtk3_get_widget(POPUP_MENU);
            (*fp_gtk_menu_shell_append)((GtkMenuShell *)menu, result);
        }
        else if (widget_type == POPUP_MENU)
        {
            GtkWidget *menu_bar = gtk3_get_widget(MENU_BAR);
            GtkWidget *root_menu = (*fp_gtk_menu_item_new)();
            (*fp_gtk_menu_item_set_submenu)((GtkMenuItem*)root_menu, result);
            (*fp_gtk_menu_shell_append)((GtkMenuShell *)menu_bar, root_menu);
        }
        else if (widget_type == COMBO_BOX_TEXT_FIELD )
        {
            GtkWidget* combo = gtk3_get_widget(COMBO_BOX);

            /*
            * We add a regular GtkButton/GtkEntry to a GtkComboBoxEntry
            * in order to trick engines into thinking it's a real combobox
            * arrow button/text field.
            */

            fp_gtk_container_add ((GtkContainer*)(combo), result);
            GtkStyleContext* context = fp_gtk_widget_get_style_context (combo);
            fp_gtk_style_context_add_class (context, "combobox-entry");
            context = fp_gtk_widget_get_style_context (result);
            fp_gtk_style_context_add_class (context, "combobox");
            fp_gtk_style_context_add_class (context, "entry");
        }
        else if (widget_type == COMBO_BOX_ARROW_BUTTON )
        {
            GtkWidget* combo = gtk3_get_widget(COMBO_BOX);
            fp_gtk_widget_set_parent(result, combo);
        }
        else if (widget_type != TOOL_TIP &&
                 widget_type != INTERNAL_FRAME &&
                 widget_type != OPTION_PANE)
        {
            (*fp_gtk_container_add)((GtkContainer *)gtk3_fixed, result);
        }
        (*fp_gtk_widget_realize)(result);
    }
    return result;
}

static void gtk3_paint_arrow(WidgetType widget_type, GtkStateType state_type,
        GtkShadowType shadow_type, const gchar *detail,
        gint x, gint y, gint width, gint height,
        GtkArrowType arrow_type, gboolean fill)
{
    gdouble xx, yy, a = G_PI;
    int s = width;
    gtk3_widget = gtk3_get_arrow(arrow_type, shadow_type);

    switch (widget_type)
    {
        case SPINNER_ARROW_BUTTON:
            s = (int)(0.4 * width + 0.5) + 1;
            if (arrow_type == GTK_ARROW_UP) {
                a = 0;
            } else if (arrow_type == GTK_ARROW_DOWN) {
                a = G_PI;
            }
            break;

        case HSCROLL_BAR_BUTTON_LEFT:
            s = (int)(0.5 * MIN(height, width * 2) + 0.5) + 1;
            a = 3 * G_PI / 2;
            break;

        case HSCROLL_BAR_BUTTON_RIGHT:
            s = (int)(0.5 * MIN(height, width * 2) + 0.5) + 1;
            a = G_PI / 2;
            break;

        case VSCROLL_BAR_BUTTON_UP:
            s = (int)(0.5 * MIN(height * 2, width) + 0.5) + 1;
            a = 0;
            break;

        case VSCROLL_BAR_BUTTON_DOWN:
            s = (int)(0.5 * MIN(height * 2, width) + 0.5) + 1;
            a = G_PI;
            break;

        case COMBO_BOX_ARROW_BUTTON:
            s = (int)(0.3 * height + 0.5) + 1;
            a = G_PI;
            break;

        case TABLE:
            s = (int)(0.8 * height + 0.5) + 1;
            if (arrow_type == GTK_ARROW_UP) {
                a = G_PI;
            } else if (arrow_type == GTK_ARROW_DOWN) {
                a = 0;
            }
            break;

        case MENU_ITEM:
            if (arrow_type == GTK_ARROW_UP) {
                a = G_PI;
            } else if (arrow_type == GTK_ARROW_DOWN) {
                a = 0;
            } else if (arrow_type == GTK_ARROW_RIGHT) {
                a = G_PI / 2;
            } else if (arrow_type == GTK_ARROW_LEFT) {
                a = 3 * G_PI / 2;
            }
            break;

        default:
            if (arrow_type == GTK_ARROW_UP) {
                a = G_PI;
            } else if (arrow_type == GTK_ARROW_DOWN) {
                a = 0;
            } else if (arrow_type == GTK_ARROW_RIGHT) {
                a = G_PI / 2;
            } else if (arrow_type == GTK_ARROW_LEFT) {
                a = 3 * G_PI / 2;
            }
            break;
    }

    if (s < width && s < height) {
        xx = x + (0.5 * (width - s) + 0.5);
        yy = y + (0.5 * (height - s) + 0.5);
    } else {
        xx = x;
        yy = y;
    }

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);
    fp_gtk_style_context_save (context);


    if (detail != NULL) {
        transform_detail_string(detail, context);
    }

    GtkStateFlags flags = get_gtk_flags(state_type);

    fp_gtk_style_context_set_state (context, flags);

    (*fp_gtk_render_arrow)(context, cr, a, xx, yy, s);

    fp_gtk_style_context_restore (context);
}

static void gtk3_paint_box(WidgetType widget_type, GtkStateType state_type,
                    GtkShadowType shadow_type, const gchar *detail,
                    gint x, gint y, gint width, gint height,
                    gint synth_state, GtkTextDirection dir)
{
    gtk3_widget = gtk3_get_widget(widget_type);

    if (widget_type == HSLIDER_TRACK) {
        /*
         * For horizontal JSliders with right-to-left orientation, we need
         * to set the "inverted" flag to match the native GTK behavior where
         * the foreground highlight is on the right side of the slider thumb.
         * This is needed especially for the ubuntulooks engine, which looks
         * exclusively at the "inverted" flag to determine on which side of
         * the thumb to paint the highlight...
         */
        fp_gtk_range_set_inverted((GtkRange*)gtk3_widget, dir ==
                                                              GTK_TEXT_DIR_RTL);

        /*
         * Note however that other engines like clearlooks will look at both
         * the "inverted" field and the text direction to determine how
         * the foreground highlight is painted:
         *     !inverted && ltr --> paint highlight on left side
         *     !inverted && rtl --> paint highlight on right side
         *      inverted && ltr --> paint highlight on right side
         *      inverted && rtl --> paint highlight on left side
         * So the only way to reliably get the desired results for horizontal
         * JSlider (i.e., highlight on left side for LTR ComponentOrientation
         * and highlight on right side for RTL ComponentOrientation) is to
         * always override text direction as LTR, and then set the "inverted"
         * flag accordingly (as we have done above).
         */
        dir = GTK_TEXT_DIR_LTR;
    }

    /*
     * Some engines (e.g. clearlooks) will paint the shadow of certain
     * widgets (e.g. COMBO_BOX_ARROW_BUTTON) differently depending on the
     * the text direction.
     */
    gtk3_set_direction(gtk3_widget, dir);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);
    fp_gtk_style_context_save (context);

    transform_detail_string(detail, context);

    GtkStateFlags flags = get_gtk_flags(state_type);

    if (shadow_type == GTK_SHADOW_IN && widget_type != COMBO_BOX_ARROW_BUTTON) {
        flags |= GTK_STATE_FLAG_ACTIVE;
    }

    if (synth_state & MOUSE_OVER) {
        flags |= GTK_STATE_FLAG_PRELIGHT;
    }

    if (synth_state & FOCUSED) {
        flags |= GTK_STATE_FLAG_FOCUSED;
    }

    if (synth_state & DEFAULT) {
        fp_gtk_style_context_add_class (context, "default");
    }

    fp_gtk_style_context_set_state (context, flags);

    if (fp_gtk_style_context_has_class(context, "progressbar")) {
        fp_gtk_render_activity (context, cr, x, y, width, height);
    } else {
        fp_gtk_render_background (context, cr, x, y, width, height);
        if (shadow_type != GTK_SHADOW_NONE) {
            fp_gtk_render_frame(context, cr, x, y, width, height);
        }
    }

    fp_gtk_style_context_restore (context);
    /*
     * Reset the text direction to the default value so that we don't
     * accidentally affect other operations and widgets.
     */
    gtk3_set_direction(gtk3_widget, GTK_TEXT_DIR_LTR);
}

static void gtk3_paint_box_gap(WidgetType widget_type, GtkStateType state_type,
        GtkShadowType shadow_type, const gchar *detail,
        gint x, gint y, gint width, gint height,
        GtkPositionType gap_side, gint gap_x, gint gap_width)
{
    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);

    fp_gtk_style_context_save (context);

    GtkStateFlags flags = get_gtk_flags(state_type);
    fp_gtk_style_context_set_state(context, flags);

    if (detail != 0) {
        transform_detail_string(detail, context);
    }
    fp_gtk_render_background(context, cr, x, y, width, height);

    if (shadow_type != GTK_SHADOW_NONE) {
        fp_gtk_render_frame_gap(context, cr, x, y, width, height, gap_side,
                                    (gdouble)gap_x, (gdouble)gap_x + gap_width);
    }
    fp_gtk_style_context_restore (context);
}

static void gtk3_paint_check(WidgetType widget_type, gint synth_state,
        const gchar *detail, gint x, gint y, gint width, gint height)
{
    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);

    fp_gtk_style_context_save (context);

    GtkStateFlags flags = get_gtk_state_flags(synth_state);
    if (gtk3_version_3_14 && (synth_state & SELECTED)) {
        flags = GTK_STATE_FLAG_CHECKED;
    }
    fp_gtk_style_context_set_state(context, flags);

    fp_gtk_style_context_add_class (context, "check");

    fp_gtk_render_check (context, cr, x, y, width, height);

    fp_gtk_style_context_restore (context);
}


static void gtk3_paint_expander(WidgetType widget_type, GtkStateType state_type,
        const gchar *detail, gint x, gint y, gint width, gint height,
        GtkExpanderStyle expander_style)
{
    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);

    fp_gtk_style_context_save (context);

    GtkStateFlags flags = get_gtk_flags(state_type);
    if (expander_style == GTK_EXPANDER_EXPANDED) {
        flags |= GTK_STATE_FLAG_ACTIVE;
    }

    fp_gtk_style_context_set_state(context, flags);

    if (detail != 0) {
        transform_detail_string(detail, context);
    }

    fp_gtk_render_expander (context, cr, x + 2, y + 2, width - 4, height - 4);

    fp_gtk_style_context_restore (context);
}

static void gtk3_paint_extension(WidgetType widget_type, GtkStateType state_type,
        GtkShadowType shadow_type, const gchar *detail,
        gint x, gint y, gint width, gint height, GtkPositionType gap_side)
{
    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);

    fp_gtk_style_context_save (context);

    GtkStateFlags flags = GTK_STATE_FLAG_NORMAL;

    if (state_type == 0) {
        flags = GTK_STATE_FLAG_ACTIVE;
    }

    fp_gtk_style_context_set_state(context, flags);

    if (detail != 0) {
        transform_detail_string(detail, context);
    }
    switch(gap_side) {
      case GTK_POS_LEFT:
        fp_gtk_style_context_add_class(context, "right");
        break;
      case GTK_POS_RIGHT:
        fp_gtk_style_context_add_class(context, "left");
        break;
      case GTK_POS_TOP:
        fp_gtk_style_context_add_class(context, "bottom");
        break;
      case GTK_POS_BOTTOM:
        fp_gtk_style_context_add_class(context, "top");
        break;
      default:
        break;
    }

    fp_gtk_render_extension(context, cr, x, y, width, height, gap_side);

    fp_gtk_style_context_restore (context);
}

static void gtk3_paint_flat_box(WidgetType widget_type, GtkStateType state_type,
        GtkShadowType shadow_type, const gchar *detail,
        gint x, gint y, gint width, gint height, gboolean has_focus)
{
    if (state_type == GTK_STATE_PRELIGHT &&
        (widget_type == CHECK_BOX || widget_type == RADIO_BUTTON)) {
        return;
    }
    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);

    fp_gtk_style_context_save (context);

    if (detail != 0) {
        transform_detail_string(detail, context);
    }

    GtkStateFlags flags = get_gtk_flags(state_type);

    if (has_focus) {
        flags |= GTK_STATE_FLAG_FOCUSED;
    }

    fp_gtk_style_context_set_state (context, flags);

    if (widget_type == COMBO_BOX_TEXT_FIELD) {
        width += height /2;
    }

    fp_gtk_render_background (context, cr, x, y, width, height);

    fp_gtk_style_context_restore (context);
}

static void gtk3_paint_focus(WidgetType widget_type, GtkStateType state_type,
        const char *detail, gint x, gint y, gint width, gint height)
{
    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);
    fp_gtk_style_context_save (context);

    transform_detail_string(detail, context);
    fp_gtk_render_focus (context, cr, x, y, width, height);

    fp_gtk_style_context_restore (context);

}

static void gtk3_paint_handle(WidgetType widget_type, GtkStateType state_type,
        GtkShadowType shadow_type, const gchar *detail,
        gint x, gint y, gint width, gint height, GtkOrientation orientation)
{
    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);

    fp_gtk_style_context_save (context);

    GtkStateFlags flags = get_gtk_flags(state_type);
    fp_gtk_style_context_set_state(context, GTK_STATE_FLAG_PRELIGHT);

    if (detail != 0) {
        transform_detail_string(detail, context);
        fp_gtk_style_context_add_class (context, "handlebox_bin");
    }

    fp_gtk_render_handle(context, cr, x, y, width, height);
    fp_gtk_render_background(context, cr, x, y, width, height);

    fp_gtk_style_context_restore (context);
}

static void gtk3_paint_hline(WidgetType widget_type, GtkStateType state_type,
        const gchar *detail, gint x, gint y, gint width, gint height)
{
    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);

    fp_gtk_style_context_save (context);

    if (detail != 0) {
        transform_detail_string(detail, context);
    }

    fp_gtk_render_line(context, cr, x, y, x + width, y);

    fp_gtk_style_context_restore (context);
}

static void gtk3_paint_vline(WidgetType widget_type, GtkStateType state_type,
        const gchar *detail, gint x, gint y, gint width, gint height)
{
    gtk3_widget = gtk3_get_widget(widget_type);


    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);

    fp_gtk_style_context_save (context);

    if (detail != 0) {
        transform_detail_string(detail, context);
    }

    fp_gtk_render_line(context, cr, x, y, x, y + height);

    fp_gtk_style_context_restore (context);
}

static void gtk3_paint_option(WidgetType widget_type, gint synth_state,
        const gchar *detail, gint x, gint y, gint width, gint height)
{
     gtk3_widget = gtk3_get_widget(widget_type);

     GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);

     fp_gtk_style_context_save (context);

     GtkStateFlags flags = get_gtk_state_flags(synth_state);
     if (gtk3_version_3_14 && (synth_state & SELECTED)) {
         flags = GTK_STATE_FLAG_CHECKED;
     }
     fp_gtk_style_context_set_state(context, flags);

     if (detail != 0) {
         transform_detail_string(detail, context);
     }

     fp_gtk_render_option(context, cr, x, y, width, height);

     fp_gtk_style_context_restore (context);
}

static void gtk3_paint_shadow(WidgetType widget_type, GtkStateType state_type,
                       GtkShadowType shadow_type, const gchar *detail,
                       gint x, gint y, gint width, gint height,
                       gint synth_state, GtkTextDirection dir)
{
    if (shadow_type == GTK_SHADOW_NONE) {
        return;
    }
    gtk3_widget = gtk3_get_widget(widget_type);

    /*
     * Some engines (e.g. clearlooks) will paint the shadow of certain
     * widgets (e.g. COMBO_BOX_TEXT_FIELD) differently depending on the
     * the text direction.
     */
    gtk3_set_direction(gtk3_widget, dir);


    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);
    fp_gtk_style_context_save (context);

    if (detail) {
        transform_detail_string(detail, context);
    }

    GtkStateFlags flags = get_gtk_flags(state_type);

    if (synth_state & MOUSE_OVER) {
        flags |= GTK_STATE_FLAG_PRELIGHT;
    }

    if (synth_state & FOCUSED) {
        flags |= GTK_STATE_FLAG_FOCUSED;
    }

    fp_gtk_style_context_set_state (context, flags);

    if (widget_type == COMBO_BOX_TEXT_FIELD) {
        width += height / 2;
    }
    fp_gtk_render_frame(context, cr, x, y, width, height);

    fp_gtk_style_context_restore (context);

    /*
     * Reset the text direction to the default value so that we don't
     * accidentally affect other operations and widgets.
     */
    gtk3_set_direction(gtk3_widget, GTK_TEXT_DIR_LTR);
}

static void gtk3_paint_slider(WidgetType widget_type, GtkStateType state_type,
        GtkShadowType shadow_type, const gchar *detail,
        gint x, gint y, gint width, gint height, GtkOrientation orientation,
        gboolean has_focus)
{
    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);

    fp_gtk_style_context_save (context);

    if (detail) {
       transform_detail_string(detail, context);
    }

    GtkStateFlags flags = get_gtk_flags(state_type);

    if (state_type == GTK_STATE_ACTIVE) {
        flags |= GTK_STATE_FLAG_PRELIGHT;
    }

    if (has_focus) {
        flags |= GTK_STATE_FLAG_FOCUSED;
    }

    fp_gtk_style_context_set_state (context, flags);

    (*fp_gtk_render_slider)(context, cr, x, y, width, height, orientation);

    fp_gtk_style_context_restore (context);
}

static void gtk3_paint_background(WidgetType widget_type,
             GtkStateType state_type, gint x, gint y, gint width, gint height) {
    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);
    fp_gtk_style_context_save (context);

    GtkStateFlags flags = get_gtk_flags(state_type);

    fp_gtk_style_context_set_state (context, flags);

    fp_gtk_render_background (context, cr, x, y, width, height);

    fp_gtk_style_context_restore (context);
}

static GdkPixbuf *gtk3_get_stock_icon(gint widget_type, const gchar *stock_id,
        GtkIconSize size, GtkTextDirection direction, const char *detail)
{
    int sz;

    switch(size) {
      case GTK_ICON_SIZE_MENU:
        sz = 16;
        break;
      case GTK_ICON_SIZE_SMALL_TOOLBAR:
        sz = 18;
        break;
      case GTK_ICON_SIZE_LARGE_TOOLBAR:
        sz = 24;
        break;
      case GTK_ICON_SIZE_BUTTON:
        sz = 20;
        break;
      case GTK_ICON_SIZE_DND:
        sz = 32;
        break;
      case GTK_ICON_SIZE_DIALOG:
        sz = 48;
        break;
      default:
        sz = 0;
        break;
    }

    init_containers();
    gtk3_widget = gtk3_get_widget((widget_type < 0) ? IMAGE : widget_type);
    (*fp_gtk_widget_set_direction)(gtk3_widget, direction);
    GtkIconTheme *icon_theme = fp_gtk_icon_theme_get_default();
    GdkPixbuf *result = fp_gtk_icon_theme_load_icon(icon_theme, stock_id, sz,
                                             GTK_ICON_LOOKUP_USE_BUILTIN, NULL);
    return result;
}

static jboolean gtk3_get_pixbuf_data(JNIEnv *env, GdkPixbuf* pixbuf,
                              jmethodID icon_upcall_method, jobject this) {
    if (!pixbuf) {
        return JNI_FALSE;
    }
    guchar *pixbuf_data = (*fp_gdk_pixbuf_get_pixels)(pixbuf);
    if (pixbuf_data) {
        int row_stride = (*fp_gdk_pixbuf_get_rowstride)(pixbuf);
        int width = (*fp_gdk_pixbuf_get_width)(pixbuf);
        int height = (*fp_gdk_pixbuf_get_height)(pixbuf);
        int bps = (*fp_gdk_pixbuf_get_bits_per_sample)(pixbuf);
        int channels = (*fp_gdk_pixbuf_get_n_channels)(pixbuf);
        gboolean alpha = (*fp_gdk_pixbuf_get_has_alpha)(pixbuf);

        jbyteArray data = (*env)->NewByteArray(env, (row_stride * height));
        JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);

        (*env)->SetByteArrayRegion(env, data, 0, (row_stride * height),
                                   (jbyte *)pixbuf_data);
        (*fp_g_object_unref)(pixbuf);

        /* Call the callback method to create the image on the Java side. */
        (*env)->CallVoidMethod(env, this, icon_upcall_method, data,
                width, height, row_stride, bps, channels, alpha);
        return JNI_TRUE;
    }
    return JNI_FALSE;
}

static jboolean gtk3_get_file_icon_data(JNIEnv *env, const char *filename,
                 GError **error, jmethodID icon_upcall_method, jobject this) {
    GdkPixbuf* pixbuf = fp_gdk_pixbuf_new_from_file(filename, error);
    return gtk3_get_pixbuf_data(env, pixbuf, icon_upcall_method, this);
}

static jboolean gtk3_get_icon_data(JNIEnv *env, gint widget_type,
                              const gchar *stock_id, GtkIconSize size,
                              GtkTextDirection direction, const char *detail,
                              jmethodID icon_upcall_method, jobject this) {
    GdkPixbuf* pixbuf = gtk3_get_stock_icon(widget_type, stock_id, size,
                                       direction, detail);
    return gtk3_get_pixbuf_data(env, pixbuf, icon_upcall_method, this);
}

/*************************************************/
static gint gtk3_get_xthickness(JNIEnv *env, WidgetType widget_type)
{
    init_containers();

    gtk3_widget = gtk3_get_widget(widget_type);
    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);
    if (context) {
        GtkBorder padding;
        fp_gtk_style_context_get_padding(context, 0, &padding);
        return padding.left + 1;
    }
    return 0;
}

static gint gtk3_get_ythickness(JNIEnv *env, WidgetType widget_type)
{
    init_containers();

    gtk3_widget = gtk3_get_widget(widget_type);
    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);
    if (context) {
        GtkBorder padding;
        fp_gtk_style_context_get_padding(context, 0, &padding);
        return padding.top + 1;
    }
    return 0;
}

/*************************************************/
static guint8 recode_color(gdouble channel)
{
    guint16 result = (guint16)(channel * 65535);
    if (result > 65535) {
        result = 65535;
    }
    return (guint8)( result >> 8);
}

static GtkStateFlags gtk3_get_state_flags(GtkStateType state_type) {
    switch (state_type)
    {
        case GTK_STATE_NORMAL:
            return GTK_STATE_FLAG_NORMAL;
        case GTK_STATE_ACTIVE:
            return GTK_STATE_FLAG_ACTIVE;
        case GTK_STATE_PRELIGHT:
            return GTK_STATE_FLAG_PRELIGHT;
        case GTK_STATE_SELECTED:
            return GTK_STATE_FLAG_SELECTED;
        case GTK_STATE_INSENSITIVE:
            return GTK_STATE_FLAG_INSENSITIVE;
        case GTK_STATE_INCONSISTENT:
            return GTK_STATE_FLAG_INCONSISTENT;
        case GTK_STATE_FOCUSED:
            return GTK_STATE_FLAG_FOCUSED;
    }
    return 0;
}


static void rgb_to_hls (gdouble *r, gdouble *g, gdouble *b) {
  gdouble min;
  gdouble max;
  gdouble red;
  gdouble green;
  gdouble blue;
  gdouble h, l, s;
  gdouble delta;

  red = *r;
  green = *g;
  blue = *b;

  if (red > green)
    {
      if (red > blue)
        max = red;
      else
        max = blue;

      if (green < blue)
        min = green;
      else
        min = blue;
    }
  else
    {
      if (green > blue)
        max = green;
      else
        max = blue;

      if (red < blue)
        min = red;
      else
        min = blue;
    }

  l = (max + min) / 2;
  s = 0;
  h = 0;

  if (max != min)
    {
      if (l <= 0.5)
        s = (max - min) / (max + min);
      else
        s = (max - min) / (2 - max - min);

      delta = max -min;
      if (red == max)
        h = (green - blue) / delta;
      else if (green == max)
        h = 2 + (blue - red) / delta;
      else if (blue == max)
        h = 4 + (red - green) / delta;

      h *= 60;
      if (h < 0.0)
        h += 360;
    }

  *r = h;
  *g = l;
  *b = s;
}

static void hls_to_rgb (gdouble *h, gdouble *l, gdouble *s)
{
  gdouble hue;
  gdouble lightness;
  gdouble saturation;
  gdouble m1, m2;
  gdouble r, g, b;

  lightness = *l;
  saturation = *s;

  if (lightness <= 0.5)
    m2 = lightness * (1 + saturation);
  else
    m2 = lightness + saturation - lightness * saturation;
  m1 = 2 * lightness - m2;

  if (saturation == 0)
    {
      *h = lightness;
      *l = lightness;
      *s = lightness;
    }
  else
    {
      hue = *h + 120;
      while (hue > 360)
        hue -= 360;
      while (hue < 0)
        hue += 360;

      if (hue < 60)
        r = m1 + (m2 - m1) * hue / 60;
      else if (hue < 180)
        r = m2;
      else if (hue < 240)
        r = m1 + (m2 - m1) * (240 - hue) / 60;
      else
        r = m1;

      hue = *h;
      while (hue > 360)
        hue -= 360;
      while (hue < 0)
        hue += 360;

      if (hue < 60)
        g = m1 + (m2 - m1) * hue / 60;
      else if (hue < 180)
        g = m2;
      else if (hue < 240)
        g = m1 + (m2 - m1) * (240 - hue) / 60;
      else
        g = m1;

      hue = *h - 120;
      while (hue > 360)
        hue -= 360;
      while (hue < 0)
        hue += 360;

      if (hue < 60)
        b = m1 + (m2 - m1) * hue / 60;
      else if (hue < 180)
        b = m2;
      else if (hue < 240)
        b = m1 + (m2 - m1) * (240 - hue) / 60;
      else
        b = m1;

      *h = r;
      *l = g;
      *s = b;
    }
}



static void gtk3_style_shade (const GdkRGBA *a, GdkRGBA *b, gdouble k) {
  gdouble red = a->red;
  gdouble green = a->green;
  gdouble blue = a->blue;

  rgb_to_hls (&red, &green, &blue);

  green *= k;
  if (green > 1.0)
    green = 1.0;
  else if (green < 0.0)
    green = 0.0;

  blue *= k;
  if (blue > 1.0)
    blue = 1.0;
  else if (blue < 0.0)
    blue = 0.0;

  hls_to_rgb (&red, &green, &blue);

  b->red = red;
  b->green = green;
  b->blue = blue;
}

static GdkRGBA gtk3_get_color_for_flags(GtkStyleContext* context,
                                  GtkStateFlags flags, ColorType color_type) {
    GdkRGBA c, color;
    color.alpha = 1;

    switch (color_type)
    {
        case FOREGROUND:
        case TEXT_FOREGROUND:
            fp_gtk_style_context_get_color(context, flags, &color);
            break;
        case BACKGROUND:
        case TEXT_BACKGROUND:
            fp_gtk_style_context_get_background_color(context, flags, &color);
            break;
        case LIGHT:
            c = gtk3_get_color_for_flags(context, flags, BACKGROUND);
            gtk3_style_shade(&c, &color, LIGHTNESS_MULT);
            break;
        case DARK:
            c = gtk3_get_color_for_flags(context, flags, BACKGROUND);
            gtk3_style_shade (&c, &color, DARKNESS_MULT);
            break;
        case MID:
            {
                GdkRGBA c1 = gtk3_get_color_for_flags(context, flags, LIGHT);
                GdkRGBA c2 = gtk3_get_color_for_flags(context, flags, DARK);
                color.red = (c1.red + c2.red) / 2;
                color.green = (c1.green + c2.green) / 2;
                color.blue = (c1.blue + c2.blue) / 2;
            }
            break;
        case FOCUS:
        case BLACK:
            color.red = 0;
            color.green = 0;
            color.blue = 0;
            break;
        case WHITE:
            color.red = 1;
            color.green = 1;
            color.blue = 1;
            break;
    }
    return color;
}

static gint gtk3_get_color_for_state(JNIEnv *env, WidgetType widget_type,
                              GtkStateType state_type, ColorType color_type)
{

    gint result = 0;

    GtkStateFlags flags = gtk3_get_state_flags(state_type);

    init_containers();

    gtk3_widget = gtk3_get_widget(widget_type);

    GtkStyleContext* context = fp_gtk_widget_get_style_context(gtk3_widget);

    if (widget_type == TOOL_TIP) {
        fp_gtk_style_context_add_class(context, "tooltip");
    }
    if (widget_type == CHECK_BOX_MENU_ITEM
     || widget_type == RADIO_BUTTON_MENU_ITEM) {
        flags &= GTK_STATE_FLAG_NORMAL | GTK_STATE_FLAG_SELECTED
                  | GTK_STATE_FLAG_INSENSITIVE | GTK_STATE_FLAG_FOCUSED;
    }

    GdkRGBA color = gtk3_get_color_for_flags(context, flags, color_type);

    if (recode_color(color.alpha) == 0) {
        color = gtk3_get_color_for_flags(
        fp_gtk_widget_get_style_context(gtk3_get_widget(INTERNAL_FRAME)),
        0, BACKGROUND);
    }

    result = recode_color(color.alpha) << 24 | recode_color(color.red) << 16 |
             recode_color(color.green) << 8 | recode_color(color.blue);

    return result;
}

/*************************************************/
static jobject create_Boolean(JNIEnv *env, jboolean boolean_value);
static jobject create_Integer(JNIEnv *env, jint int_value);
static jobject create_Long(JNIEnv *env, jlong long_value);
static jobject create_Float(JNIEnv *env, jfloat float_value);
static jobject create_Double(JNIEnv *env, jdouble double_value);
static jobject create_Character(JNIEnv *env, jchar char_value);
static jobject create_Insets(JNIEnv *env, GtkBorder *border);

static jobject gtk3_get_class_value(JNIEnv *env, WidgetType widget_type,
                                                     const char* key)
{
    init_containers();

    gtk3_widget = gtk3_get_widget(widget_type);

    GValue value = { 0, { { 0 } } };

    GParamSpec* param = (*fp_gtk_widget_class_find_style_property)(
                                    ((GTypeInstance*)gtk3_widget)->g_class, key);
    if ( param )
    {
        (*fp_g_value_init)( &value, param->value_type );
        (*fp_gtk_widget_style_get_property)(gtk3_widget, key, &value);

        if ((*fp_g_type_is_a)( param->value_type, G_TYPE_BOOLEAN ))
        {
            gboolean val = (*fp_g_value_get_boolean)(&value);
            return create_Boolean(env, (jboolean)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_CHAR ))
        {
            gchar val = (*fp_g_value_get_char)(&value);
            return create_Character(env, (jchar)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_UCHAR ))
        {
            guchar val = (*fp_g_value_get_uchar)(&value);
            return create_Character(env, (jchar)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_INT ))
        {
            gint val = (*fp_g_value_get_int)(&value);
            return create_Integer(env, (jint)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_UINT ))
        {
            guint val = (*fp_g_value_get_uint)(&value);
                    return create_Integer(env, (jint)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_LONG ))
        {
            glong val = (*fp_g_value_get_long)(&value);
            return create_Long(env, (jlong)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_ULONG ))
        {
            gulong val = (*fp_g_value_get_ulong)(&value);
            return create_Long(env, (jlong)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_INT64 ))
        {
            gint64 val = (*fp_g_value_get_int64)(&value);
            return create_Long(env, (jlong)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_UINT64 ))
        {
            guint64 val = (*fp_g_value_get_uint64)(&value);
            return create_Long(env, (jlong)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_FLOAT ))
        {
            gfloat val = (*fp_g_value_get_float)(&value);
            return create_Float(env, (jfloat)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_DOUBLE ))
        {
            gdouble val = (*fp_g_value_get_double)(&value);
            return create_Double(env, (jdouble)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_ENUM ))
        {
            gint val = (*fp_g_value_get_enum)(&value);
            return create_Integer(env, (jint)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_FLAGS ))
        {
            guint val = (*fp_g_value_get_flags)(&value);
            return create_Integer(env, (jint)val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, G_TYPE_STRING ))
        {
            const gchar* val = (*fp_g_value_get_string)(&value);

            /* We suppose that all values come in C locale and
             * utf-8 representation of a string is the same as
             * the string itself. If this isn't so we should
             * use g_convert.
             */
            return (*env)->NewStringUTF(env, val);
        }
        else if ((*fp_g_type_is_a)( param->value_type, GTK_TYPE_BORDER ))
        {
            GtkBorder *border = (GtkBorder*)(*fp_g_value_get_boxed)(&value);
            return border ? create_Insets(env, border) : NULL;
        }

        /*      TODO: Other types are not supported yet.*/
/*        else if((*fp_g_type_is_a)( param->value_type, G_TYPE_PARAM ))
        {
            GParamSpec* val = (*fp_g_value_get_param)(&value);
            printf( "Param: %p\n", val );
        }
        else if((*fp_g_type_is_a)( param->value_type, G_TYPE_BOXED ))
        {
            gpointer* val = (*fp_g_value_get_boxed)(&value);
            printf( "Boxed: %p\n", val );
        }
        else if((*fp_g_type_is_a)( param->value_type, G_TYPE_POINTER ))
        {
            gpointer* val = (*fp_g_value_get_pointer)(&value);
            printf( "Pointer: %p\n", val );
        }
        else if((*fp_g_type_is_a)( param->value_type, G_TYPE_OBJECT ))
        {
            GObject* val = (GObject*)(*fp_g_value_get_object)(&value);
            printf( "Object: %p\n", val );
        }*/
    }

    return NULL;
}

static void gtk3_set_range_value(WidgetType widget_type, jdouble value,
                          jdouble min, jdouble max, jdouble visible)
{
    GtkAdjustment *adj;

    gtk3_widget = gtk3_get_widget(widget_type);

    adj = (*fp_gtk_range_get_adjustment)((GtkRange *)gtk3_widget);

    fp_gtk_adjustment_set_value(adj, value);
    fp_gtk_adjustment_set_lower(adj, min);
    fp_gtk_adjustment_set_upper(adj, max);
    fp_gtk_adjustment_set_page_size(adj, visible);
}

/*************************************************/
static jobject create_Object(JNIEnv *env, jmethodID *cid,
                             const char* class_name,
                             const char* signature,
                             jvalue* value)
{
    jclass  class;
    jobject result;

    class = (*env)->FindClass(env, class_name);
    if (class == NULL)
        return NULL; /* can't find/load the class, exception thrown */

    if (*cid == NULL)
    {
        *cid = (*env)->GetMethodID(env, class, "<init>", signature);
        if (*cid == NULL)
        {
            (*env)->DeleteLocalRef(env, class);
            return NULL; /* can't find/get the method, exception thrown */
        }
    }

    result = (*env)->NewObjectA(env, class, *cid, value);

    (*env)->DeleteLocalRef(env, class);
    return result;
}

jobject create_Boolean(JNIEnv *env, jboolean boolean_value)
{
    static jmethodID cid = NULL;
    jvalue value;

    value.z = boolean_value;

    return create_Object(env, &cid, "java/lang/Boolean", "(Z)V", &value);
}

jobject create_Integer(JNIEnv *env, jint int_value)
{
    static jmethodID cid = NULL;
    jvalue value;

    value.i = int_value;

    return create_Object(env, &cid, "java/lang/Integer", "(I)V", &value);
}

jobject create_Long(JNIEnv *env, jlong long_value)
{
    static jmethodID cid = NULL;
    jvalue value;

    value.j = long_value;

    return create_Object(env, &cid, "java/lang/Long", "(J)V", &value);
}

jobject create_Float(JNIEnv *env, jfloat float_value)
{
    static jmethodID cid = NULL;
    jvalue value;

    value.f = float_value;

    return create_Object(env, &cid, "java/lang/Float", "(F)V", &value);
}

jobject create_Double(JNIEnv *env, jdouble double_value)
{
    static jmethodID cid = NULL;
    jvalue value;

    value.d = double_value;

    return create_Object(env, &cid, "java/lang/Double", "(D)V", &value);
}

jobject create_Character(JNIEnv *env, jchar char_value)
{
    static jmethodID cid = NULL;
    jvalue value;

    value.c = char_value;

    return create_Object(env, &cid, "java/lang/Character", "(C)V", &value);
}


jobject create_Insets(JNIEnv *env, GtkBorder *border)
{
    static jmethodID cid = NULL;
    jvalue values[4];

    values[0].i = border->top;
    values[1].i = border->left;
    values[2].i = border->bottom;
    values[3].i = border->right;

    return create_Object(env, &cid, "java/awt/Insets", "(IIII)V", values);
}

/*********************************************/
static jstring gtk3_get_pango_font_name(JNIEnv *env, WidgetType widget_type)
{
    init_containers();

    gtk3_widget = gtk3_get_widget(widget_type);
    jstring  result = NULL;
    GtkStyleContext* context = fp_gtk_widget_get_style_context (gtk3_widget);
    if (context)
    {
        PangoFontDescription* fd = fp_gtk_style_context_get_font(context, 0);
        gchar* val = (*fp_pango_font_description_to_string)(fd);
        result = (*env)->NewStringUTF(env, val);
        (*fp_g_free)( val );
    }

    return result;
}

/***********************************************/
static jobject get_string_property(JNIEnv *env, GtkSettings* settings,
                                                             const gchar* key) {
    jobject result = NULL;
    gchar*  strval = NULL;

    (*fp_g_object_get)(settings, key, &strval, NULL);
    result = (*env)->NewStringUTF(env, strval);
    (*fp_g_free)(strval);

    return result;
}

static jobject get_integer_property(JNIEnv *env, GtkSettings* settings,
                                                             const gchar* key) {
    gint intval = 0;
    (*fp_g_object_get)(settings, key, &intval, NULL);
    return create_Integer(env, intval);
}

static jobject get_boolean_property(JNIEnv *env, GtkSettings* settings,
                                                             const gchar* key) {
    gint intval = 0;
    (*fp_g_object_get)(settings, key, &intval, NULL);
    return create_Boolean(env, intval);
}

static jobject gtk3_get_setting(JNIEnv *env, Setting property)
{
    GtkSettings* settings = (*fp_gtk_settings_get_default)();

    switch (property)
    {
        case GTK_FONT_NAME:
            return get_string_property(env, settings, "gtk-font-name");
        case GTK_ICON_SIZES:
            return get_string_property(env, settings, "gtk-icon-sizes");
        case GTK_CURSOR_BLINK:
            return get_boolean_property(env, settings, "gtk-cursor-blink");
        case GTK_CURSOR_BLINK_TIME:
            return get_integer_property(env, settings, "gtk-cursor-blink-time");
    }

    return NULL;
}

static void transform_detail_string (const gchar *detail,
                                                     GtkStyleContext *context) {
  if (!detail)
    return;

  if (strcmp (detail, "arrow") == 0)
    fp_gtk_style_context_add_class (context, "arrow");
  else if (strcmp (detail, "button") == 0)
    fp_gtk_style_context_add_class (context, "button");
  else if (strcmp (detail, "buttondefault") == 0)
    {
      fp_gtk_style_context_add_class (context, "button");
      fp_gtk_style_context_add_class (context, "default");
    }
  else if (strcmp (detail, "calendar") == 0)
    fp_gtk_style_context_add_class (context, "calendar");
  else if (strcmp (detail, "cellcheck") == 0)
    {
      fp_gtk_style_context_add_class (context, "cell");
      fp_gtk_style_context_add_class (context, "check");
    }
  else if (strcmp (detail, "cellradio") == 0)
    {
      fp_gtk_style_context_add_class (context, "cell");
      fp_gtk_style_context_add_class (context, "radio");
    }
  else if (strcmp (detail, "checkbutton") == 0)
    fp_gtk_style_context_add_class (context, "check");
  else if (strcmp (detail, "check") == 0)
    {
      fp_gtk_style_context_add_class (context, "check");
      fp_gtk_style_context_add_class (context, "menu");
    }
  else if (strcmp (detail, "radiobutton") == 0)
    {
      fp_gtk_style_context_add_class (context, "radio");
    }
  else if (strcmp (detail, "option") == 0)
    {
      fp_gtk_style_context_add_class (context, "radio");
      fp_gtk_style_context_add_class (context, "menu");
    }
  else if (strcmp (detail, "entry") == 0 ||
           strcmp (detail, "entry_bg") == 0)
    fp_gtk_style_context_add_class (context, "entry");
  else if (strcmp (detail, "expander") == 0)
    fp_gtk_style_context_add_class (context, "expander");
  else if (strcmp (detail, "tooltip") == 0)
    fp_gtk_style_context_add_class (context, "tooltip");
  else if (strcmp (detail, "frame") == 0)
    fp_gtk_style_context_add_class (context, "frame");
  else if (strcmp (detail, "scrolled_window") == 0)
    fp_gtk_style_context_add_class (context, "scrolled-window");
  else if (strcmp (detail, "viewport") == 0 ||
           strcmp (detail, "viewportbin") == 0)
    fp_gtk_style_context_add_class (context, "viewport");
  else if (strncmp (detail, "trough", 6) == 0)
    fp_gtk_style_context_add_class (context, "trough");
  else if (strcmp (detail, "spinbutton") == 0)
    fp_gtk_style_context_add_class (context, "spinbutton");
  else if (strcmp (detail, "spinbutton_up") == 0)
    {
      fp_gtk_style_context_add_class (context, "spinbutton");
      fp_gtk_style_context_add_class (context, "button");
      fp_gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
    }
  else if (strcmp (detail, "spinbutton_down") == 0)
    {
      fp_gtk_style_context_add_class (context, "spinbutton");
      fp_gtk_style_context_add_class (context, "button");
      fp_gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
    }
  else if ((detail[0] == 'h' || detail[0] == 'v') &&
           strncmp (&detail[1], "scrollbar_", 9) == 0)
    {
      fp_gtk_style_context_add_class (context, "button");
      fp_gtk_style_context_add_class (context, "scrollbar");
    }
  else if (strcmp (detail, "slider") == 0)
    {
      fp_gtk_style_context_add_class (context, "slider");
      fp_gtk_style_context_add_class (context, "scrollbar");
    }
  else if (strcmp (detail, "vscale") == 0 ||
           strcmp (detail, "hscale") == 0)
    {
      fp_gtk_style_context_add_class (context, "slider");
      fp_gtk_style_context_add_class (context, "scale");
    }
  else if (strcmp (detail, "menuitem") == 0)
    {
      fp_gtk_style_context_add_class (context, "menuitem");
      fp_gtk_style_context_add_class (context, "menu");
    }
  else if (strcmp (detail, "menu") == 0)
    {
      fp_gtk_style_context_add_class (context, "popup");
      fp_gtk_style_context_add_class (context, "menu");
    }
  else if (strcmp (detail, "accellabel") == 0)
    fp_gtk_style_context_add_class (context, "accelerator");
  else if (strcmp (detail, "menubar") == 0)
    fp_gtk_style_context_add_class (context, "menubar");
  else if (strcmp (detail, "base") == 0)
    fp_gtk_style_context_add_class (context, "background");
  else if (strcmp (detail, "bar") == 0 ||
           strcmp (detail, "progressbar") == 0)
    fp_gtk_style_context_add_class (context, "progressbar");
  else if (strcmp (detail, "toolbar") == 0)
    fp_gtk_style_context_add_class (context, "toolbar");
  else if (strcmp (detail, "handlebox_bin") == 0)
    fp_gtk_style_context_add_class (context, "dock");
  else if (strcmp (detail, "notebook") == 0)
    fp_gtk_style_context_add_class (context, "notebook");
  else if (strcmp (detail, "tab") == 0)
  {
      fp_gtk_style_context_add_class (context, "notebook");
      fp_gtk_style_context_add_region (context, "tab", 0);
  } else if (strcmp (detail, "paned") == 0) {
      fp_gtk_style_context_add_class (context, "pane-separator");
  }
  else if (fp_g_str_has_prefix (detail, "cell"))
    {
      GtkRegionFlags row, col;
      gboolean ruled = FALSE;
      gchar** tokens;
      guint i;

      tokens = fp_g_strsplit (detail, "_", -1);
      row = col = 0;
      i = 0;

      while (tokens[i])
        {
          if (strcmp (tokens[i], "even") == 0)
            row |= GTK_REGION_EVEN;
          else if (strcmp (tokens[i], "odd") == 0)
            row |= GTK_REGION_ODD;
          else if (strcmp (tokens[i], "start") == 0)
            col |= GTK_REGION_FIRST;
          else if (strcmp (tokens[i], "end") == 0)
            col |= GTK_REGION_LAST;
          else if (strcmp (tokens[i], "ruled") == 0)
            ruled = TRUE;
          else if (strcmp (tokens[i], "sorted") == 0)
            col |= GTK_REGION_SORTED;

          i++;
        }

      if (!ruled)
        row &= ~(GTK_REGION_EVEN | GTK_REGION_ODD);

      fp_gtk_style_context_add_class (context, "cell");
      fp_gtk_style_context_add_region (context, "row", row);
      fp_gtk_style_context_add_region (context, "column", col);

      fp_g_strfreev (tokens);
    }
}

static GdkWindow* gtk3_get_window(void *widget) {
    return fp_gtk_widget_get_window((GtkWidget*)widget);
}

static void gtk3_init(GtkApi* gtk) {
    gtk->version = GTK_3;

    gtk->show_uri_load = &gtk3_show_uri_load;
    gtk->unload = &gtk3_unload;
    gtk->flush_event_loop = &flush_gtk_event_loop;
    gtk->gtk_check_version = fp_gtk_check_version;
    gtk->get_setting = &gtk3_get_setting;

    gtk->paint_arrow = &gtk3_paint_arrow;
    gtk->paint_box = &gtk3_paint_box;
    gtk->paint_box_gap = &gtk3_paint_box_gap;
    gtk->paint_expander = &gtk3_paint_expander;
    gtk->paint_extension = &gtk3_paint_extension;
    gtk->paint_flat_box = &gtk3_paint_flat_box;
    gtk->paint_focus = &gtk3_paint_focus;
    gtk->paint_handle = &gtk3_paint_handle;
    gtk->paint_hline = &gtk3_paint_hline;
    gtk->paint_vline = &gtk3_paint_vline;
    gtk->paint_option = &gtk3_paint_option;
    gtk->paint_shadow = &gtk3_paint_shadow;
    gtk->paint_slider = &gtk3_paint_slider;
    gtk->paint_background = &gtk3_paint_background;
    gtk->paint_check = &gtk3_paint_check;
    gtk->set_range_value = &gtk3_set_range_value;

    gtk->init_painting = &gtk3_init_painting;
    gtk->copy_image = &gtk3_copy_image;

    gtk->get_xthickness = &gtk3_get_xthickness;
    gtk->get_ythickness = &gtk3_get_ythickness;
    gtk->get_color_for_state = &gtk3_get_color_for_state;
    gtk->get_class_value = &gtk3_get_class_value;

    gtk->get_pango_font_name = &gtk3_get_pango_font_name;
    gtk->get_icon_data = &gtk3_get_icon_data;
    gtk->get_file_icon_data = &gtk3_get_file_icon_data;
    gtk->gdk_threads_enter = fp_gdk_threads_enter;
    gtk->gdk_threads_leave = fp_gdk_threads_leave;
    gtk->gtk_show_uri = fp_gtk_show_uri;
    gtk->g_free = fp_g_free;

    gtk->gtk_file_chooser_get_filename = fp_gtk_file_chooser_get_filename;
    gtk->gtk_widget_hide = fp_gtk_widget_hide;
    gtk->gtk_main_quit = fp_gtk_main_quit;
    gtk->gtk_file_chooser_dialog_new = fp_gtk_file_chooser_dialog_new;
    gtk->gtk_file_chooser_set_current_folder =
                          fp_gtk_file_chooser_set_current_folder;
    gtk->gtk_file_chooser_set_filename = fp_gtk_file_chooser_set_filename;
    gtk->gtk_file_chooser_set_current_name =
                          fp_gtk_file_chooser_set_current_name;
    gtk->gtk_file_filter_add_custom = fp_gtk_file_filter_add_custom;
    gtk->gtk_file_chooser_set_filter = fp_gtk_file_chooser_set_filter;
    gtk->gtk_file_chooser_get_type = fp_gtk_file_chooser_get_type;
    gtk->gtk_file_filter_new = fp_gtk_file_filter_new;
    gtk->gtk_file_chooser_set_do_overwrite_confirmation =
                          fp_gtk_file_chooser_set_do_overwrite_confirmation;
    gtk->gtk_file_chooser_set_select_multiple =
                          fp_gtk_file_chooser_set_select_multiple;
    gtk->gtk_file_chooser_get_current_folder =
                          fp_gtk_file_chooser_get_current_folder;
    gtk->gtk_file_chooser_get_filenames = fp_gtk_file_chooser_get_filenames;
    gtk->gtk_g_slist_length = fp_gtk_g_slist_length;
    gtk->g_signal_connect_data = fp_g_signal_connect_data;
    gtk->gtk_widget_show = fp_gtk_widget_show;
    gtk->gtk_main = fp_gtk_main;
    gtk->gtk_main_level = fp_gtk_main_level;
    gtk->g_path_get_dirname = fp_g_path_get_dirname;
    gtk->gtk_widget_destroy = fp_gtk_widget_destroy;
    gtk->gtk_window_present = fp_gtk_window_present;
    gtk->gtk_window_move = fp_gtk_window_move;
    gtk->gtk_window_resize = fp_gtk_window_resize;
    gtk->get_window = &gtk3_get_window;

    gtk->g_object_unref = fp_g_object_unref;
    gtk->g_list_append = fp_g_list_append;
    gtk->g_list_free = fp_g_list_free;
    gtk->g_list_free_full = fp_g_list_free_full;
}