1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
/*
* This file is part of GtkSourceView
*
* Copyright (C) 2007 -2009 Jesús Barbero RodrÃguez <chuchiperriman@gmail.com>
* Copyright (C) 2009 - Jesse van den Kieboom <jessevdk@gnome.org>
* Copyright (C) 2013 - Sébastien Wilmet <swilmet@gnome.org>
*
* GtkSourceView is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* GtkSourceView is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:completion
* @title: GtkSourceCompletion
* @short_description: Main Completion Object
*
* The completion system helps the user when he/she writes some text,
* such as words, command names, functions, and suchlike. Proposals can
* be shown, to complete the text the user is writing. Each proposal can
* contain an additional piece of information (for example
* documentation), that is displayed when the "Details" button is
* clicked.
*
* Proposals are created via a #GtkSourceCompletionProvider. There can
* be for example a provider to complete words (see
* #GtkSourceCompletionWords), another provider for the completion of
* function names, etc. To add a provider, call
* gtk_source_completion_add_provider().
*
* When the completion is activated, a #GtkSourceCompletionContext object is
* created. The providers are asked whether they match the context, with
* gtk_source_completion_provider_match(). If a provider doesn't match the
* context, it will not be visible in the completion window. On the
* other hand, if the provider matches the context, its proposals will
* be displayed.
*
* When several providers match, they are all shown in the completion
* window, but one can switch between providers: see the
* #GtkSourceCompletion::move-page signal. It is also possible to
* activate the first proposals with key bindings, see the
* #GtkSourceCompletion:accelerators property.
*
* The #GtkSourceCompletionProposal interface represents a proposal.
* The #GtkSourceCompletionItem class is a simple implementation of this
* interface.
*
* If a proposal contains extra information (see
* gtk_source_completion_provider_get_info_widget()), it will be
* displayed in a #GtkSourceCompletionInfo window, which appears when
* the "Details" button is clicked.
*
* A #GtkSourceCompletionInfo window can also be used to display
* calltips. When no proposals are available, it can be useful to
* display extra information like a function prototype (number of
* parameters, types of parameters, etc).
*
* Each #GtkSourceView object is associated with a #GtkSourceCompletion
* instance. This instance can be obtained with
* gtk_source_view_get_completion(). The #GtkSourceView class contains also the
* #GtkSourceView::show-completion signal.
*
* A same #GtkSourceCompletionProvider object can be used for several
* #GtkSourceCompletion's.
*
* # GtkSourceCompletion as GtkBuildable
*
* The GtkSourceCompletion implementation of the #GtkBuildable interface exposes
* the info window object (see gtk_source_completion_get_info_window()) with the
* internal-child "info_window".
*
* An example of a UI definition fragment with GtkSourceCompletion:
* |[
* <object class="GtkSourceCompletion">
* <property name="select_on_show">False</property>
* <child internal-child="info_window">
* <object class="GtkSourceCompletionInfo">
* <property name="border_width">6</property>
* </object>
* </child>
* </object>
* ]|
*/
/* Idea to improve the code: use a composite widget template. This class is not
* a GtkWidget, so some refactoring needs to be done, to have a subclass of
* GtkSourceCompletionInfo for the main completion window.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gtksourcecompletion.h"
#include "gtksourcecompletion-private.h"
#include <glib/gi18n-lib.h>
#include "gtksourcecompletionmodel.h"
#include "gtksourcecompletioncontext.h"
#include "gtksourcecompletioninfo.h"
#include "gtksourcecompletionproposal.h"
#include "gtksourcecompletionprovider.h"
#include "gtksourcecompletioncontainer.h"
#include "gtksourcebuffer.h"
#include "gtksourceview.h"
enum
{
SHOW,
HIDE,
POPULATE_CONTEXT,
/* Actions */
ACTIVATE_PROPOSAL,
MOVE_CURSOR,
MOVE_PAGE,
N_SIGNALS
};
enum
{
PROP_0,
PROP_VIEW,
PROP_REMEMBER_INFO_VISIBILITY,
PROP_SELECT_ON_SHOW,
PROP_SHOW_HEADERS,
PROP_SHOW_ICONS,
PROP_ACCELERATORS,
PROP_AUTO_COMPLETE_DELAY,
PROP_PROVIDER_PAGE_SIZE,
PROP_PROPOSAL_PAGE_SIZE
};
struct _GtkSourceCompletionPrivate
{
GtkSourceCompletionInfo *main_window;
GtkSourceCompletionInfo *info_window;
/* Bottom bar, containing the "Details" button and the selection image
* and label. */
GtkWidget *bottom_bar;
/* Image and label in the bottom bar, on the right, for showing which
* provider(s) are selected. */
GtkImage *selection_image;
GtkLabel *selection_label;
/* The default widget for the info window */
GtkLabel *default_info;
/* The "Details" button, for showing the info window */
GtkToggleButton *info_button;
/* List of proposals */
GtkTreeView *tree_view_proposals;
GtkCellRenderer *cell_renderer_proposal;
/* Completion management */
GtkSourceCompletionModel *model_proposals;
GList *providers;
GtkSourceCompletionContext *context;
GList *active_providers;
GList *running_providers;
guint show_timed_out_id;
GtkTextBuffer *buffer;
GList *auto_completion_selection;
GtkSourceCompletionContext *auto_completion_context;
/* Number of times the interactive completion is blocked */
guint block_interactive_num;
/* Properties */
/* Weak reference to the view. You must check if view != NULL before
* using it.
*/
GtkSourceView *view;
guint num_accelerators;
guint auto_complete_delay;
guint proposal_page_size;
guint provider_page_size;
guint remember_info_visibility : 1;
guint select_on_show : 1;
guint show_headers : 1;
guint show_icons : 1;
};
static guint signals[N_SIGNALS];
static void gtk_source_completion_buildable_interface_init (GtkBuildableIface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkSourceCompletion, gtk_source_completion, G_TYPE_OBJECT,
G_ADD_PRIVATE (GtkSourceCompletion)
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
gtk_source_completion_buildable_interface_init))
static void
scroll_to_iter (GtkSourceCompletion *completion,
GtkTreeIter *iter)
{
GtkTreePath *path;
GtkTreeIter prev_iter = *iter;
path = gtk_tree_model_get_path (GTK_TREE_MODEL (completion->priv->model_proposals),
iter);
gtk_tree_view_scroll_to_cell (completion->priv->tree_view_proposals,
path, NULL,
FALSE, 0, 0);
gtk_tree_path_free (path);
if (gtk_source_completion_model_iter_previous (completion->priv->model_proposals, &prev_iter) &&
gtk_source_completion_model_iter_is_header (completion->priv->model_proposals, &prev_iter))
{
/* If we want to scroll to the first proposal of a provider,
* it's better to show the header too, if there is a header.
* We first scroll to the proposal, and then to the
* header, so we are sure that the proposal is visible.
*/
path = gtk_tree_model_get_path (GTK_TREE_MODEL (completion->priv->model_proposals),
&prev_iter);
gtk_tree_view_scroll_to_cell (completion->priv->tree_view_proposals,
path, NULL,
FALSE, 0, 0);
gtk_tree_path_free (path);
}
}
/* Returns %TRUE if a proposal is selected.
* Call g_object_unref() on @provider and @proposal when no longer needed.
*/
static gboolean
get_selected_proposal (GtkSourceCompletion *completion,
GtkSourceCompletionProvider **provider,
GtkSourceCompletionProposal **proposal)
{
GtkTreeIter iter;
GtkTreeSelection *selection;
selection = gtk_tree_view_get_selection (completion->priv->tree_view_proposals);
if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
{
return FALSE;
}
if (gtk_source_completion_model_iter_is_header (completion->priv->model_proposals, &iter))
{
return FALSE;
}
if (provider != NULL)
{
gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->model_proposals), &iter,
GTK_SOURCE_COMPLETION_MODEL_COLUMN_PROVIDER, provider,
-1);
}
if (proposal != NULL)
{
gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->model_proposals), &iter,
GTK_SOURCE_COMPLETION_MODEL_COLUMN_PROPOSAL, proposal,
-1);
}
return TRUE;
}
/* Returns %TRUE if the first proposal is selected. */
static gboolean
check_first_selected (GtkSourceCompletion *completion)
{
GtkTreeSelection *selection;
GtkTreeIter iter;
if (get_selected_proposal (completion, NULL, NULL) ||
!completion->priv->select_on_show)
{
return FALSE;
}
if (!gtk_source_completion_model_first_proposal (completion->priv->model_proposals, &iter))
{
return FALSE;
}
selection = gtk_tree_view_get_selection (completion->priv->tree_view_proposals);
gtk_tree_selection_select_iter (selection, &iter);
scroll_to_iter (completion, &iter);
return TRUE;
}
static void
get_iter_at_insert (GtkSourceCompletion *completion,
GtkTextIter *iter)
{
gtk_text_buffer_get_iter_at_mark (completion->priv->buffer,
iter,
gtk_text_buffer_get_insert (completion->priv->buffer));
}
static GList *
select_providers (GList *providers,
GtkSourceCompletionContext *context)
{
GtkTextIter context_iter;
GList *selection = NULL;
GList *l;
if (!gtk_source_completion_context_get_iter (context, &context_iter))
{
return NULL;
}
for (l = providers; l != NULL; l = l->next)
{
GtkSourceCompletionProvider *provider = l->data;
gboolean good_activation = (gtk_source_completion_provider_get_activation (provider) &
gtk_source_completion_context_get_activation (context)) != 0;
if (good_activation &&
gtk_source_completion_provider_match (provider, context))
{
selection = g_list_prepend (selection, provider);
}
}
return g_list_reverse (selection);
}
static gint
minimum_auto_complete_delay (GtkSourceCompletion *completion,
GList *providers)
{
gint min_delay = completion->priv->auto_complete_delay;
while (providers != NULL)
{
GtkSourceCompletionProvider *provider = providers->data;
gint delay = gtk_source_completion_provider_get_interactive_delay (provider);
if (0 <= delay && delay < min_delay)
{
min_delay = delay;
}
providers = g_list_next (providers);
}
return min_delay;
}
static void
reset_completion (GtkSourceCompletion *completion)
{
if (completion->priv->show_timed_out_id != 0)
{
g_source_remove (completion->priv->show_timed_out_id);
completion->priv->show_timed_out_id = 0;
}
if (completion->priv->context != NULL)
{
/* Inform providers of cancellation through the context */
_gtk_source_completion_context_cancel (completion->priv->context);
g_clear_object (&completion->priv->context);
}
g_list_free (completion->priv->running_providers);
g_list_free (completion->priv->active_providers);
completion->priv->running_providers = NULL;
completion->priv->active_providers = NULL;
}
/* A separator is a character like (, a space etc. An _ is not a separator. */
static gboolean
is_separator (const gunichar ch)
{
if (g_unichar_isprint (ch) &&
(g_unichar_isalnum (ch) || ch == g_utf8_get_char ("_")))
{
return FALSE;
}
return TRUE;
}
/* Assigns @start_word to the start position of the word, and @end_word to the
* end position.
*/
static void
get_word_iter (GtkTextBuffer *buffer,
GtkTextIter *start_word,
GtkTextIter *end_word)
{
gtk_text_buffer_get_iter_at_mark (buffer,
end_word,
gtk_text_buffer_get_insert (buffer));
*start_word = *end_word;
while (gtk_text_iter_backward_char (start_word))
{
gunichar ch = gtk_text_iter_get_char (start_word);
if (is_separator (ch))
{
gtk_text_iter_forward_char (start_word);
return;
}
}
}
static void
replace_current_word (GtkTextBuffer *buffer,
const gchar *new_text)
{
GtkTextIter word_start;
GtkTextIter word_end;
get_word_iter (buffer, &word_start, &word_end);
gtk_text_buffer_begin_user_action (buffer);
gtk_text_buffer_delete (buffer, &word_start, &word_end);
if (new_text != NULL)
{
gtk_text_buffer_insert (buffer, &word_start, new_text, -1);
}
gtk_text_buffer_end_user_action (buffer);
}
static void
update_window_position (GtkSourceCompletion *completion)
{
GtkSourceCompletionProvider *provider;
GtkSourceCompletionProposal *proposal;
GtkTextIter iter;
gboolean iter_set = FALSE;
if (completion->priv->view == NULL)
{
return;
}
/* The model can be modified while there is no completion context, for
* example when the headers are shown or hidden. This triggers a signal
* to update the window position, but if there is no completion context,
* no need to update the window position (the window is normally hidden
* in this case). When a new population is done, this function will be
* called again, so no problem.
*/
if (completion->priv->context == NULL)
{
return;
}
if (get_selected_proposal (completion, &provider, &proposal))
{
GtkTextIter context_iter;
gboolean valid_context;
valid_context = gtk_source_completion_context_get_iter (completion->priv->context,
&context_iter);
if (valid_context &&
gtk_source_completion_provider_get_start_iter (provider,
completion->priv->context,
proposal,
&iter))
{
iter_set = TRUE;
}
g_object_unref (provider);
g_object_unref (proposal);
}
if (!iter_set)
{
GtkTextIter end_word;
get_word_iter (completion->priv->buffer, &iter, &end_word);
}
gtk_source_completion_info_move_to_iter (completion->priv->main_window,
GTK_TEXT_VIEW (completion->priv->view),
&iter);
}
static void
set_info_widget (GtkSourceCompletion *completion,
GtkWidget *new_widget)
{
GtkWidget *cur_widget = gtk_bin_get_child (GTK_BIN (completion->priv->info_window));
if (cur_widget == new_widget)
{
return;
}
if (cur_widget != NULL)
{
gtk_container_remove (GTK_CONTAINER (completion->priv->info_window), cur_widget);
}
gtk_container_add (GTK_CONTAINER (completion->priv->info_window), new_widget);
}
static void
update_proposal_info_state (GtkSourceCompletion *completion)
{
GtkSourceCompletionProvider *provider = NULL;
GtkSourceCompletionProposal *proposal = NULL;
GtkWidget *info_widget;
if (!get_selected_proposal (completion, &provider, &proposal))
{
gtk_widget_set_sensitive (GTK_WIDGET (completion->priv->info_button), FALSE);
return;
}
info_widget = gtk_source_completion_provider_get_info_widget (provider, proposal);
if (info_widget != NULL)
{
set_info_widget (completion, info_widget);
gtk_widget_set_sensitive (GTK_WIDGET (completion->priv->info_button), TRUE);
gtk_source_completion_provider_update_info (provider,
proposal,
completion->priv->info_window);
}
else
{
gchar *text = gtk_source_completion_proposal_get_info (proposal);
if (text != NULL)
{
set_info_widget (completion, GTK_WIDGET (completion->priv->default_info));
gtk_widget_set_sensitive (GTK_WIDGET (completion->priv->info_button), TRUE);
gtk_label_set_markup (completion->priv->default_info, text);
g_free (text);
}
else
{
gtk_widget_set_sensitive (GTK_WIDGET (completion->priv->info_button), FALSE);
}
}
g_object_unref (provider);
g_object_unref (proposal);
}
static void
update_info_window_visibility (GtkSourceCompletion *completion)
{
if (gtk_widget_get_sensitive (GTK_WIDGET (completion->priv->info_button)) &&
gtk_toggle_button_get_active (completion->priv->info_button))
{
gtk_widget_show (GTK_WIDGET (completion->priv->info_window));
}
else
{
gtk_widget_hide (GTK_WIDGET (completion->priv->info_window));
}
}
static void
update_proposal_info (GtkSourceCompletion *completion)
{
update_proposal_info_state (completion);
update_info_window_visibility (completion);
}
static void
gtk_source_completion_show_default (GtkSourceCompletion *completion)
{
if (completion->priv->view == NULL)
{
return;
}
gtk_widget_show (GTK_WIDGET (completion->priv->main_window));
/* Do the autosize when the widget is visible. It doesn't work if it is
* done before.
*/
gtk_tree_view_columns_autosize (completion->priv->tree_view_proposals);
if (!completion->priv->remember_info_visibility)
{
gtk_toggle_button_set_active (completion->priv->info_button, FALSE);
}
update_proposal_info (completion);
gtk_widget_grab_focus (GTK_WIDGET (completion->priv->view));
}
static void
gtk_source_completion_hide_default (GtkSourceCompletion *completion)
{
gtk_widget_hide (GTK_WIDGET (completion->priv->info_window));
gtk_widget_hide (GTK_WIDGET (completion->priv->main_window));
}
static void
gtk_source_completion_proposals_size_allocate (GtkSourceCompletion *completion,
GtkAllocation *allocation,
GtkWidget *widget)
{
GtkTreeViewColumn *column;
gint cell_offset = 0;
gint column_offset;
gint focus_padding;
gint horizontal_separator;
gint x_offset = 0;
if (!gtk_widget_get_realized (GTK_WIDGET (completion->priv->tree_view_proposals)))
{
return;
}
gtk_widget_style_get (GTK_WIDGET (completion->priv->tree_view_proposals),
"focus-padding", &focus_padding,
"horizontal-separator", &horizontal_separator,
NULL);
column = gtk_tree_view_get_column (completion->priv->tree_view_proposals, 1);
column_offset = gtk_tree_view_column_get_x_offset (column);
gtk_tree_view_column_cell_get_position (column,
completion->priv->cell_renderer_proposal,
&cell_offset,
NULL);
x_offset = column_offset + cell_offset + horizontal_separator + focus_padding;
gtk_tree_view_convert_bin_window_to_widget_coords (completion->priv->tree_view_proposals,
x_offset,
0,
&x_offset,
NULL);
gtk_widget_translate_coordinates (GTK_WIDGET (completion->priv->tree_view_proposals),
GTK_WIDGET (completion->priv->main_window),
x_offset,
0,
&x_offset,
NULL);
_gtk_source_completion_info_set_xoffset (completion->priv->main_window, -x_offset);
}
static void
gtk_source_completion_activate_proposal (GtkSourceCompletion *completion)
{
GtkSourceCompletionProvider *provider = NULL;
GtkSourceCompletionProposal *proposal = NULL;
GtkTextIter insert_iter;
GtkTextIter context_iter;
gboolean valid_context;
gboolean activated;
if (completion->priv->view == NULL)
{
return;
}
if (!get_selected_proposal (completion, &provider, &proposal))
{
return;
}
get_iter_at_insert (completion, &insert_iter);
gtk_source_completion_block_interactive (completion);
activated = gtk_source_completion_provider_activate_proposal (provider, proposal, &insert_iter);
valid_context = (completion->priv->context != NULL &&
gtk_source_completion_context_get_iter (completion->priv->context,
&context_iter));
if (!activated && valid_context)
{
GtkTextIter start_iter;
gchar *text = gtk_source_completion_proposal_get_text (proposal);
gboolean has_start = gtk_source_completion_provider_get_start_iter (provider,
completion->priv->context,
proposal,
&start_iter);
if (has_start)
{
gtk_text_buffer_begin_user_action (completion->priv->buffer);
gtk_text_buffer_delete (completion->priv->buffer, &start_iter, &insert_iter);
gtk_text_buffer_insert (completion->priv->buffer, &start_iter, text, -1);
gtk_text_buffer_end_user_action (completion->priv->buffer);
}
else
{
replace_current_word (completion->priv->buffer, text);
}
g_free (text);
}
gtk_source_completion_unblock_interactive (completion);
gtk_source_completion_hide (completion);
g_object_unref (provider);
g_object_unref (proposal);
}
static void
update_info_position (GtkSourceCompletion *completion)
{
GdkScreen *screen;
gint x, y;
gint width, height;
gint screen_width;
gint info_width;
gtk_window_get_position (GTK_WINDOW (completion->priv->main_window), &x, &y);
gtk_window_get_size (GTK_WINDOW (completion->priv->main_window), &width, &height);
gtk_window_get_size (GTK_WINDOW (completion->priv->info_window), &info_width, NULL);
screen = gtk_window_get_screen (GTK_WINDOW (completion->priv->main_window));
screen_width = gdk_screen_get_width (screen);
/* Determine on which side to place it */
if (x + width + info_width >= screen_width)
{
x -= info_width;
}
else
{
x += width;
}
gtk_window_move (GTK_WINDOW (completion->priv->info_window), x, y);
}
static GtkSourceCompletionProvider *
get_visible_provider (GtkSourceCompletion *completion)
{
GList *visible = gtk_source_completion_model_get_visible_providers (completion->priv->model_proposals);
if (visible != NULL)
{
return GTK_SOURCE_COMPLETION_PROVIDER (visible->data);
}
else
{
return NULL;
}
}
static void
get_num_visible_providers (GtkSourceCompletion *completion,
guint *num,
guint *current)
{
GList *providers = gtk_source_completion_model_get_providers (completion->priv->model_proposals);
GtkSourceCompletionProvider *visible = get_visible_provider (completion);
*num = g_list_length (providers);
*current = 0;
if (visible != NULL)
{
gint idx = g_list_index (providers, visible);
g_return_if_fail (idx != -1);
*current = idx + 1;
}
g_list_free (providers);
}
static void
update_selection_label (GtkSourceCompletion *completion)
{
guint pos;
guint num;
gchar *name;
gchar *selection_text;
GtkSourceCompletionProvider *visible;
get_num_visible_providers (completion, &num, &pos);
if (num <= 1)
{
/* At most one provider. All the proposals are shown. */
gtk_image_clear (completion->priv->selection_image);
gtk_widget_hide (GTK_WIDGET (completion->priv->selection_label));
return;
}
visible = get_visible_provider (completion);
if (visible == NULL)
{
/* Translators: "All" is used as a label in the status bar of the
popup, telling that all completion pages are shown. */
name = g_strdup_printf("<b>%s</b>", _("All"));
gtk_image_clear (completion->priv->selection_image);
}
else
{
gchar *temp_name = gtk_source_completion_provider_get_name (visible);
name = g_markup_escape_text (temp_name, -1);
g_free (temp_name);
gtk_image_set_from_pixbuf (completion->priv->selection_image,
gtk_source_completion_provider_get_icon (visible));
}
selection_text = g_strdup_printf ("<small>%s (%d/%d)</small>", name, pos + 1, num + 1);
gtk_label_set_markup (completion->priv->selection_label, selection_text);
gtk_widget_show (GTK_WIDGET (completion->priv->selection_label));
g_free (selection_text);
g_free (name);
}
static gboolean
get_next_iter (GtkSourceCompletion *completion,
gint num,
GtkTreeIter *iter)
{
GtkTreeSelection *selection;
gboolean has_selection;
selection = gtk_tree_view_get_selection (completion->priv->tree_view_proposals);
has_selection = gtk_tree_selection_get_selected (selection, NULL, iter);
if (!has_selection)
{
return gtk_source_completion_model_first_proposal (completion->priv->model_proposals,
iter);
}
while (num > 0)
{
if (!gtk_source_completion_model_next_proposal (completion->priv->model_proposals, iter))
{
return gtk_source_completion_model_last_proposal (completion->priv->model_proposals,
iter);
}
num--;
}
return TRUE;
}
static gboolean
get_previous_iter (GtkSourceCompletion *completion,
gint num,
GtkTreeIter *iter)
{
GtkTreeSelection *selection;
gboolean has_selection;
selection = gtk_tree_view_get_selection (completion->priv->tree_view_proposals);
has_selection = gtk_tree_selection_get_selected (selection, NULL, iter);
if (!has_selection)
{
return gtk_source_completion_model_last_proposal (completion->priv->model_proposals,
iter);
}
while (num > 0)
{
if (!gtk_source_completion_model_previous_proposal (completion->priv->model_proposals,
iter))
{
return gtk_source_completion_model_first_proposal (completion->priv->model_proposals,
iter);
}
num--;
}
return TRUE;
}
static void
gtk_source_completion_move_cursor (GtkSourceCompletion *completion,
GtkScrollStep step,
gint num)
{
GtkTreeIter iter;
gboolean ok;
if (step == GTK_SCROLL_ENDS)
{
if (num > 0)
{
ok = gtk_source_completion_model_last_proposal (completion->priv->model_proposals,
&iter);
}
else
{
ok = gtk_source_completion_model_first_proposal (completion->priv->model_proposals,
&iter);
}
}
else
{
if (step == GTK_SCROLL_PAGES)
{
num *= completion->priv->proposal_page_size;
}
if (num > 0)
{
ok = get_next_iter (completion, num, &iter);
}
else
{
ok = get_previous_iter (completion, -1 * num, &iter);
}
}
if (ok)
{
GtkTreeSelection *selection;
selection = gtk_tree_view_get_selection (completion->priv->tree_view_proposals);
gtk_tree_selection_select_iter (selection, &iter);
scroll_to_iter (completion, &iter);
}
}
static GList *
get_last_provider (GtkSourceCompletion *completion)
{
GList *providers = gtk_source_completion_model_get_providers (completion->priv->model_proposals);
GList *ret;
g_return_val_if_fail (providers != NULL, NULL);
if (providers->next == NULL)
{
ret = NULL;
}
else
{
ret = g_list_copy (g_list_last (providers));
}
g_list_free (providers);
return ret;
}
static GList *
providers_cycle_forward (GList *all_providers,
GList *position,
gint num)
{
GList *l = position;
gint i;
if (all_providers == NULL || all_providers->next == NULL)
{
return NULL;
}
for (i = 0; i < num; i++)
{
l = l == NULL ? all_providers : l->next;
}
return l;
}
static GList *
providers_cycle_backward (GList *all_providers,
GList *position,
gint num)
{
gint i;
GList *l = position;
GList *end = g_list_last (all_providers);
if (all_providers == NULL || all_providers->next == NULL)
{
return NULL;
}
for (i = 0; i < num; i++)
{
l = l == NULL ? end : l->prev;
}
return l;
}
static GList *
get_next_provider (GtkSourceCompletion *completion,
gint num)
{
GList *providers;
GList *visible_providers;
GList *position;
GList *ret;
providers = gtk_source_completion_model_get_providers (completion->priv->model_proposals);
visible_providers = gtk_source_completion_model_get_visible_providers (completion->priv->model_proposals);
if (visible_providers == NULL)
{
position = NULL;
}
else
{
position = g_list_find (providers, visible_providers->data);
}
position = providers_cycle_forward (providers, position, num);
if (position == NULL)
{
ret = NULL;
}
else
{
ret = g_list_append (NULL, position->data);
}
g_list_free (providers);
return ret;
}
static GList *
get_previous_provider (GtkSourceCompletion *completion,
gint num)
{
GList *providers;
GList *visible_providers;
GList *position;
GList *ret;
providers = gtk_source_completion_model_get_providers (completion->priv->model_proposals);
visible_providers = gtk_source_completion_model_get_visible_providers (completion->priv->model_proposals);
if (visible_providers == NULL)
{
position = NULL;
}
else
{
position = g_list_find (providers, visible_providers->data);
}
position = providers_cycle_backward (providers, position, num);
if (position == NULL)
{
ret = NULL;
}
else
{
ret = g_list_append (NULL, position->data);
}
g_list_free (providers);
return ret;
}
static void
gtk_source_completion_move_page (GtkSourceCompletion *completion,
GtkScrollStep step,
gint num)
{
GList *visible_providers = NULL;
if (step == GTK_SCROLL_ENDS)
{
if (num > 0)
{
visible_providers = get_last_provider (completion);
}
else
{
visible_providers = NULL;
}
}
else
{
if (step == GTK_SCROLL_PAGES)
{
num *= completion->priv->provider_page_size;
}
if (num > 0)
{
visible_providers = get_next_provider (completion, num);
}
else
{
visible_providers = get_previous_provider (completion, -1 * num);
}
}
gtk_tree_view_set_model (completion->priv->tree_view_proposals, NULL);
gtk_tree_view_columns_autosize (completion->priv->tree_view_proposals);
gtk_source_completion_model_set_visible_providers (completion->priv->model_proposals,
visible_providers);
gtk_tree_view_set_model (completion->priv->tree_view_proposals,
GTK_TREE_MODEL (completion->priv->model_proposals));
update_selection_label (completion);
check_first_selected (completion);
g_list_free (visible_providers);
}
/* Begins at 0. Returns -1 if no accelerators available for @iter. */
static gint
get_accel_at_iter (GtkSourceCompletion *completion,
GtkTreeIter *iter)
{
GtkTreeIter it;
guint accel;
if (gtk_source_completion_model_iter_is_header (completion->priv->model_proposals, iter))
{
return -1;
}
if (!gtk_source_completion_model_first_proposal (completion->priv->model_proposals, &it))
{
g_return_val_if_reached (-1);
}
for (accel = 0; accel < completion->priv->num_accelerators; accel++)
{
if (gtk_source_completion_model_iter_equal (completion->priv->model_proposals,
iter,
&it))
{
return accel;
}
if (!gtk_source_completion_model_next_proposal (completion->priv->model_proposals, &it))
{
return -1;
}
}
return -1;
}
static void
render_proposal_accelerator_func (GtkTreeViewColumn *column,
GtkCellRenderer *cell,
GtkTreeModel *model,
GtkTreeIter *iter,
GtkSourceCompletion *completion)
{
gint accel = get_accel_at_iter (completion, iter);
gchar *text = NULL;
if (accel != -1)
{
text = g_strdup_printf ("<small><b>%d</b></small>", (accel + 1) % 10);
}
g_object_set (cell, "markup", text, NULL);
g_free (text);
}
static gboolean
activate_by_accelerator (GtkSourceCompletion *completion,
gint num)
{
GtkTreeSelection *selection;
GtkTreeIter iter;
gint i;
if (completion->priv->num_accelerators == 0)
{
return FALSE;
}
num = num == 0 ? 9 : num - 1;
if (num < 0 || completion->priv->num_accelerators <= (guint)num)
{
return FALSE;
}
if (!gtk_source_completion_model_first_proposal (completion->priv->model_proposals, &iter))
{
return FALSE;
}
for (i = 0; i < num; i++)
{
if (!gtk_source_completion_model_next_proposal (completion->priv->model_proposals, &iter))
{
return FALSE;
}
}
selection = gtk_tree_view_get_selection (completion->priv->tree_view_proposals);
gtk_tree_selection_select_iter (selection, &iter);
gtk_source_completion_activate_proposal (completion);
return TRUE;
}
static void
selection_changed_cb (GtkTreeSelection *selection,
GtkSourceCompletion *completion)
{
update_proposal_info (completion);
if (get_selected_proposal (completion, NULL, NULL))
{
update_window_position (completion);
}
}
static gboolean
gtk_source_completion_configure_event (GtkWidget *widget,
GdkEventConfigure *event,
GtkSourceCompletion *completion)
{
update_info_position (completion);
return FALSE;
}
static gboolean
hide_completion_cb (GtkSourceCompletion *completion)
{
gtk_source_completion_hide (completion);
return FALSE;
}
static gboolean
view_key_press_event_cb (GtkSourceView *view,
GdkEventKey *event,
GtkSourceCompletion *completion)
{
static gboolean mnemonic_keyval_set = FALSE;
static guint mnemonic_keyval = GDK_KEY_VoidSymbol;
GdkModifierType mod;
GtkBindingSet *binding_set;
if (!gtk_widget_get_visible (GTK_WIDGET (completion->priv->main_window)))
{
return FALSE;
}
if (G_UNLIKELY (!mnemonic_keyval_set))
{
const gchar *label_text = gtk_button_get_label (GTK_BUTTON (completion->priv->info_button));
GtkWidget *label = gtk_label_new_with_mnemonic (label_text);
g_object_ref_sink (label);
mnemonic_keyval = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
mnemonic_keyval_set = TRUE;
g_object_unref (label);
}
mod = gtk_accelerator_get_default_mod_mask () & event->state;
/* Handle info button mnemonic */
if ((mod & GDK_MOD1_MASK) != 0 &&
event->keyval == mnemonic_keyval &&
gtk_widget_get_sensitive (GTK_WIDGET (completion->priv->info_button)))
{
gtk_toggle_button_set_active (completion->priv->info_button,
!gtk_toggle_button_get_active (completion->priv->info_button));
return TRUE;
}
if ((mod & GDK_MOD1_MASK) != 0 &&
GDK_KEY_0 <= event->keyval && event->keyval <= GDK_KEY_9)
{
if (activate_by_accelerator (completion, event->keyval - GDK_KEY_0))
{
return TRUE;
}
}
binding_set = gtk_binding_set_by_class (GTK_SOURCE_COMPLETION_GET_CLASS (completion));
if (gtk_binding_set_activate (binding_set,
event->keyval,
event->state,
G_OBJECT (completion)))
{
return TRUE;
}
return FALSE;
}
static void
buffer_mark_set_cb (GtkTextBuffer *buffer,
GtkTextIter *iter,
GtkTextMark *mark,
GtkSourceCompletion *completion)
{
if (mark == gtk_text_buffer_get_insert (buffer))
{
gtk_source_completion_hide (completion);
}
}
static void
update_transient_for_info (GObject *window,
GParamSpec *spec,
GtkSourceCompletion *completion)
{
gtk_window_set_transient_for (GTK_WINDOW (completion->priv->info_window),
gtk_window_get_transient_for (GTK_WINDOW (completion->priv->main_window)));
}
static void
replace_model (GtkSourceCompletion *completion)
{
if (completion->priv->model_proposals != NULL)
{
g_object_unref (completion->priv->model_proposals);
}
completion->priv->model_proposals = gtk_source_completion_model_new ();
gtk_source_completion_model_set_show_headers (completion->priv->model_proposals,
completion->priv->show_headers);
}
/* Takes ownership of @providers and @context. */
static void
update_completion (GtkSourceCompletion *completion,
GList *providers,
GtkSourceCompletionContext *context)
{
GList *item;
GtkTextIter context_iter;
gboolean valid_context;
/* Copy the parameters, because they can be freed by reset_completion(). */
GList *providers_copy = g_list_copy (providers);
GtkSourceCompletionContext *context_copy = g_object_ref_sink (context);
/* Make sure to first cancel any running completion */
reset_completion (completion);
completion->priv->context = context_copy;
completion->priv->running_providers = g_list_copy (providers_copy);
completion->priv->active_providers = g_list_copy (providers_copy);
/* Create a new CompletionModel */
gtk_tree_view_set_model (completion->priv->tree_view_proposals, NULL);
gtk_tree_view_columns_autosize (completion->priv->tree_view_proposals);
replace_model (completion);
valid_context = gtk_source_completion_context_get_iter (context_copy, &context_iter);
if (valid_context)
{
for (item = providers_copy; item != NULL; item = g_list_next (item))
{
GtkSourceCompletionProvider *provider = item->data;
gtk_source_completion_provider_populate (provider, context_copy);
}
}
g_list_free (providers_copy);
}
static gboolean
auto_completion_final (GtkSourceCompletion *completion)
{
/* Store and set to NULL because update_completion will cancel the last
completion, which will also remove the timeout source which in turn
would free these guys */
GtkSourceCompletionContext *context = completion->priv->auto_completion_context;
GList *selection = completion->priv->auto_completion_selection;
completion->priv->auto_completion_context = NULL;
completion->priv->auto_completion_selection = NULL;
update_completion (completion, selection, context);
g_list_free (selection);
g_object_unref (context);
return G_SOURCE_REMOVE;
}
static void
auto_completion_destroy (GtkSourceCompletion *completion)
{
if (completion->priv->auto_completion_context != NULL)
{
g_object_unref (completion->priv->auto_completion_context);
completion->priv->auto_completion_context = NULL;
}
g_list_free (completion->priv->auto_completion_selection);
completion->priv->auto_completion_selection = NULL;
}
static void
start_interactive_completion (GtkSourceCompletion *completion,
GtkTextIter *iter)
{
GtkSourceCompletionContext *context;
GList *providers;
gint delay;
reset_completion (completion);
/* Create the context */
context = gtk_source_completion_create_context (completion, iter);
g_object_ref_sink (context);
g_object_set (context,
"activation",
GTK_SOURCE_COMPLETION_ACTIVATION_INTERACTIVE,
NULL);
g_signal_emit (completion, signals[POPULATE_CONTEXT], 0, context);
/* Select providers */
providers = select_providers (completion->priv->providers, context);
if (providers == NULL)
{
g_object_unref (context);
return;
}
/* Create the timeout */
delay = minimum_auto_complete_delay (completion, providers);
completion->priv->auto_completion_context = context;
completion->priv->auto_completion_selection = providers;
completion->priv->show_timed_out_id =
g_timeout_add_full (G_PRIORITY_DEFAULT,
delay,
(GSourceFunc)auto_completion_final,
completion,
(GDestroyNotify)auto_completion_destroy);
}
static void
update_active_completion (GtkSourceCompletion *completion,
GtkTextIter *new_iter)
{
GList *selected_providers;
g_assert (completion->priv->context != NULL);
g_object_set (completion->priv->context,
"iter", new_iter,
NULL);
selected_providers = select_providers (completion->priv->providers,
completion->priv->context);
if (selected_providers != NULL)
{
update_completion (completion,
selected_providers,
completion->priv->context);
g_list_free (selected_providers);
}
else
{
gtk_source_completion_hide (completion);
}
}
static void
buffer_delete_range_cb (GtkTextBuffer *buffer,
GtkTextIter *start,
GtkTextIter *end,
GtkSourceCompletion *completion)
{
if (completion->priv->context != NULL)
{
update_active_completion (completion, start);
}
}
static void
buffer_insert_text_cb (GtkTextBuffer *buffer,
GtkTextIter *location,
gchar *text,
gint len,
GtkSourceCompletion *completion)
{
if (completion->priv->context != NULL)
{
update_active_completion (completion, location);
}
else
{
start_interactive_completion (completion, location);
}
}
static void
update_bottom_bar_visibility (GtkSourceCompletion *completion)
{
GList *providers;
guint nb_providers;
providers = gtk_source_completion_model_get_providers (completion->priv->model_proposals);
nb_providers = g_list_length (providers);
g_list_free (providers);
if (nb_providers > 1)
{
gtk_widget_show (completion->priv->bottom_bar);
return;
}
if (gtk_source_completion_model_has_info (completion->priv->model_proposals))
{
gtk_widget_show (completion->priv->bottom_bar);
}
else
{
gtk_widget_hide (completion->priv->bottom_bar);
}
}
static void
style_context_changed (GtkStyleContext *style_context,
GtkSourceCompletion *completion)
{
PangoFontDescription *font_desc = NULL;
gtk_style_context_save (style_context);
gtk_style_context_set_state (style_context, GTK_STATE_FLAG_NORMAL);
gtk_style_context_get (style_context,
gtk_style_context_get_state (style_context),
GTK_STYLE_PROPERTY_FONT, &font_desc,
NULL);
gtk_style_context_restore (style_context);
/*
* Work around issue where when a proposal provides "<b>markup</b>" and
* the weight is set in the font description, the <b> markup will not
* have it's weight respected. This seems to be happening because the
* weight mask is getting set in pango_font_description_from_string()
* even if the the value is set to normal. That matter is complicated
* because PangoAttrFontDesc and PangoAttrWeight will both have the
* same starting offset in the PangoLayout.
* https://bugzilla.gnome.org/show_bug.cgi?id=755968
*/
if (PANGO_WEIGHT_NORMAL == pango_font_description_get_weight (font_desc))
{
pango_font_description_unset_fields (font_desc, PANGO_FONT_MASK_WEIGHT);
}
g_object_set (completion->priv->cell_renderer_proposal,
"font-desc", font_desc,
NULL);
pango_font_description_free (font_desc);
}
static void
populating_done (GtkSourceCompletion *completion,
GtkSourceCompletionContext *context)
{
if (gtk_source_completion_model_is_empty (completion->priv->model_proposals, TRUE))
{
gtk_source_completion_hide (completion);
return;
}
gtk_tree_view_set_model (completion->priv->tree_view_proposals,
GTK_TREE_MODEL (completion->priv->model_proposals));
update_selection_label (completion);
update_bottom_bar_visibility (completion);
if (!check_first_selected (completion))
{
/* Update the window position only if the first proposal is not
* selected, because if it is selected, the window position will
* already be updated.
*/
update_window_position (completion);
}
if (!gtk_widget_get_visible (GTK_WIDGET (completion->priv->main_window)))
{
g_signal_emit (completion, signals[SHOW], 0);
}
}
static void
gtk_source_completion_dispose (GObject *object)
{
GtkSourceCompletion *completion = GTK_SOURCE_COMPLETION (object);
reset_completion (completion);
if (completion->priv->view != NULL)
{
g_object_remove_weak_pointer (G_OBJECT (completion->priv->view),
(gpointer *)&completion->priv->view);
completion->priv->view = NULL;
}
g_clear_object (&completion->priv->buffer);
g_clear_object (&completion->priv->default_info);
g_clear_object (&completion->priv->model_proposals);
if (completion->priv->info_window != NULL)
{
gtk_widget_destroy (GTK_WIDGET (completion->priv->info_window));
completion->priv->info_window = NULL;
}
if (completion->priv->main_window != NULL)
{
gtk_widget_destroy (GTK_WIDGET (completion->priv->main_window));
completion->priv->main_window = NULL;
}
g_list_free_full (completion->priv->providers, g_object_unref);
completion->priv->providers = NULL;
G_OBJECT_CLASS (gtk_source_completion_parent_class)->dispose (object);
}
/* Unconditionnally block interactive completion, without taking into account
* priv->block_interactive_num.
* g_signal_handlers_block_by_func() has a counter too, so you may think that
* block_interactive_num is useless. But it is useful when the buffer changes,
* to keep the signal handler blocked on the new buffer.
*/
static void
block_interactive (GtkSourceCompletion *completion)
{
g_signal_handlers_block_by_func (completion->priv->buffer,
buffer_insert_text_cb,
completion);
g_signal_handlers_block_by_func (completion->priv->buffer,
buffer_delete_range_cb,
completion);
}
static void
connect_buffer (GtkSourceCompletion *completion)
{
GtkTextBuffer *new_buffer = NULL;
if (completion->priv->view != NULL)
{
new_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (completion->priv->view));
}
if (completion->priv->buffer == new_buffer)
{
return;
}
if (completion->priv->buffer != NULL)
{
g_signal_handlers_disconnect_by_func (completion->priv->buffer,
buffer_mark_set_cb,
completion);
g_signal_handlers_disconnect_by_func (completion->priv->buffer,
gtk_source_completion_block_interactive,
completion);
g_signal_handlers_disconnect_by_func (completion->priv->buffer,
gtk_source_completion_unblock_interactive,
completion);
g_signal_handlers_disconnect_by_func (completion->priv->buffer,
buffer_delete_range_cb,
completion);
g_signal_handlers_disconnect_by_func (completion->priv->buffer,
buffer_insert_text_cb,
completion);
reset_completion (completion);
g_object_unref (completion->priv->buffer);
}
completion->priv->buffer = new_buffer;
if (new_buffer == NULL)
{
return;
}
g_object_ref (completion->priv->buffer);
g_signal_connect_object (new_buffer,
"mark-set",
G_CALLBACK (buffer_mark_set_cb),
completion,
G_CONNECT_AFTER);
g_signal_connect_object (new_buffer,
"undo",
G_CALLBACK (gtk_source_completion_block_interactive),
completion,
G_CONNECT_SWAPPED);
g_signal_connect_object (new_buffer,
"undo",
G_CALLBACK (gtk_source_completion_unblock_interactive),
completion,
G_CONNECT_SWAPPED | G_CONNECT_AFTER);
g_signal_connect_object (new_buffer,
"redo",
G_CALLBACK (gtk_source_completion_block_interactive),
completion,
G_CONNECT_SWAPPED);
g_signal_connect_object (new_buffer,
"redo",
G_CALLBACK (gtk_source_completion_unblock_interactive),
completion,
G_CONNECT_SWAPPED | G_CONNECT_AFTER);
g_signal_connect_object (new_buffer,
"delete-range",
G_CALLBACK (buffer_delete_range_cb),
completion,
G_CONNECT_AFTER);
g_signal_connect_object (new_buffer,
"insert-text",
G_CALLBACK (buffer_insert_text_cb),
completion,
G_CONNECT_AFTER);
if (completion->priv->block_interactive_num > 0)
{
block_interactive (completion);
}
}
static void
connect_view (GtkSourceCompletion *completion,
GtkSourceView *view)
{
g_assert (completion->priv->view == NULL);
completion->priv->view = view;
g_object_add_weak_pointer (G_OBJECT (view),
(gpointer *)&completion->priv->view);
g_signal_connect_object (completion->priv->view,
"focus-out-event",
G_CALLBACK (hide_completion_cb),
completion,
G_CONNECT_SWAPPED);
g_signal_connect_object (completion->priv->view,
"button-press-event",
G_CALLBACK (hide_completion_cb),
completion,
G_CONNECT_SWAPPED);
g_signal_connect_object (completion->priv->view,
"key-press-event",
G_CALLBACK (view_key_press_event_cb),
completion,
0);
g_signal_connect_object (completion->priv->view,
"paste-clipboard",
G_CALLBACK (gtk_source_completion_block_interactive),
completion,
G_CONNECT_SWAPPED);
g_signal_connect_object (completion->priv->view,
"paste-clipboard",
G_CALLBACK (gtk_source_completion_unblock_interactive),
completion,
G_CONNECT_SWAPPED | G_CONNECT_AFTER);
connect_buffer (completion);
g_signal_connect_object (completion->priv->view,
"notify::buffer",
G_CALLBACK (connect_buffer),
completion,
G_CONNECT_SWAPPED);
}
static void
gtk_source_completion_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkSourceCompletion *completion;
g_return_if_fail (GTK_SOURCE_IS_COMPLETION (object));
completion = GTK_SOURCE_COMPLETION (object);
switch (prop_id)
{
case PROP_VIEW:
g_value_set_object (value, completion->priv->view);
break;
case PROP_REMEMBER_INFO_VISIBILITY:
g_value_set_boolean (value, completion->priv->remember_info_visibility);
break;
case PROP_SELECT_ON_SHOW:
g_value_set_boolean (value, completion->priv->select_on_show);
break;
case PROP_SHOW_HEADERS:
g_value_set_boolean (value, completion->priv->show_headers);
break;
case PROP_SHOW_ICONS:
g_value_set_boolean (value, completion->priv->show_icons);
break;
case PROP_ACCELERATORS:
g_value_set_uint (value, completion->priv->num_accelerators);
break;
case PROP_AUTO_COMPLETE_DELAY:
g_value_set_uint (value, completion->priv->auto_complete_delay);
break;
case PROP_PROPOSAL_PAGE_SIZE:
g_value_set_uint (value, completion->priv->proposal_page_size);
break;
case PROP_PROVIDER_PAGE_SIZE:
g_value_set_uint (value, completion->priv->provider_page_size);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_source_completion_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkSourceCompletion *completion;
g_return_if_fail (GTK_SOURCE_IS_COMPLETION (object));
completion = GTK_SOURCE_COMPLETION (object);
switch (prop_id)
{
case PROP_VIEW:
connect_view (completion, g_value_get_object (value));
break;
case PROP_REMEMBER_INFO_VISIBILITY:
completion->priv->remember_info_visibility = g_value_get_boolean (value);
break;
case PROP_SELECT_ON_SHOW:
completion->priv->select_on_show = g_value_get_boolean (value);
break;
case PROP_SHOW_HEADERS:
completion->priv->show_headers = g_value_get_boolean (value);
if (completion->priv->model_proposals != NULL)
{
gtk_source_completion_model_set_show_headers (completion->priv->model_proposals,
completion->priv->show_headers);
}
break;
case PROP_SHOW_ICONS:
completion->priv->show_icons = g_value_get_boolean (value);
break;
case PROP_ACCELERATORS:
completion->priv->num_accelerators = g_value_get_uint (value);
break;
case PROP_AUTO_COMPLETE_DELAY:
completion->priv->auto_complete_delay = g_value_get_uint (value);
break;
case PROP_PROPOSAL_PAGE_SIZE:
completion->priv->proposal_page_size = g_value_get_uint (value);
break;
case PROP_PROVIDER_PAGE_SIZE:
completion->priv->provider_page_size = g_value_get_uint (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
selection_func (GtkTreeSelection *selection,
GtkTreeModel *model,
GtkTreePath *path,
gboolean path_currently_selected,
GtkSourceCompletion *completion)
{
GtkTreeIter iter;
gtk_tree_model_get_iter (model, &iter, path);
if (gtk_source_completion_model_iter_is_header (completion->priv->model_proposals,
&iter))
{
/* A header must never be selected */
g_return_val_if_fail (!path_currently_selected, TRUE);
return FALSE;
}
else
{
return TRUE;
}
}
static void
accelerators_notify_cb (GtkSourceCompletion *completion,
GParamSpec *pspec,
GtkTreeViewColumn *column)
{
gtk_tree_view_column_set_visible (column, completion->priv->num_accelerators > 0);
}
static void
cell_icon_func (GtkTreeViewColumn *column,
GtkCellRenderer *cell,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{
GdkPixbuf *pixbuf;
gchar *icon_name;
GIcon *gicon;
gboolean set = FALSE;
gtk_tree_model_get (model, iter,
GTK_SOURCE_COMPLETION_MODEL_COLUMN_ICON, &pixbuf,
GTK_SOURCE_COMPLETION_MODEL_COLUMN_ICON_NAME, &icon_name,
GTK_SOURCE_COMPLETION_MODEL_COLUMN_GICON, &gicon,
-1);
if (pixbuf != NULL)
{
g_object_set (cell, "pixbuf", pixbuf, NULL);
g_object_unref (pixbuf);
set = TRUE;
}
if (icon_name != NULL)
{
g_object_set (cell, "icon-name", icon_name, NULL);
g_free (icon_name);
set = TRUE;
}
if (gicon != NULL)
{
g_object_set (cell, "gicon", gicon, NULL);
g_object_unref (gicon);
set = TRUE;
}
if (!set)
{
g_object_set (cell, "icon-name", NULL, NULL);
}
}
static void
init_tree_view (GtkSourceCompletion *completion,
GtkBuilder *builder)
{
GtkTreeSelection *selection;
GtkTreeViewColumn *column;
GtkCellRenderer *cell_renderer;
GtkStyleContext *style_context;
GdkRGBA *background_color = NULL;
GdkRGBA foreground_color;
completion->priv->tree_view_proposals = GTK_TREE_VIEW (gtk_builder_get_object (builder, "tree_view_proposals"));
g_signal_connect_swapped (completion->priv->tree_view_proposals,
"row-activated",
G_CALLBACK (gtk_source_completion_activate_proposal),
completion);
g_signal_connect_swapped (completion->priv->tree_view_proposals,
"size-allocate",
G_CALLBACK (gtk_source_completion_proposals_size_allocate),
completion);
/* Selection */
selection = gtk_tree_view_get_selection (completion->priv->tree_view_proposals);
gtk_tree_selection_set_select_function (selection,
(GtkTreeSelectionFunc)selection_func,
completion,
NULL);
g_signal_connect (selection,
"changed",
G_CALLBACK (selection_changed_cb),
completion);
/* Icon cell renderer */
cell_renderer = GTK_CELL_RENDERER (gtk_builder_get_object (builder, "cell_renderer_icon"));
column = GTK_TREE_VIEW_COLUMN (gtk_builder_get_object (builder, "tree_view_column_icon"));
/* We use a cell function instead of plain attributes for the icon since
* the pixbuf renderer will not renderer any icon if pixbuf is set to NULL.
* See https://bugzilla.gnome.org/show_bug.cgi?id=753510
*/
gtk_tree_view_column_set_cell_data_func (column,
cell_renderer,
cell_icon_func,
NULL,
NULL);
gtk_tree_view_column_set_attributes (column, cell_renderer,
"cell-background-set", GTK_SOURCE_COMPLETION_MODEL_COLUMN_IS_HEADER,
NULL);
style_context = gtk_widget_get_style_context (GTK_WIDGET (completion->priv->tree_view_proposals));
gtk_style_context_save (style_context);
gtk_style_context_set_state (style_context, GTK_STATE_FLAG_INSENSITIVE);
gtk_style_context_get (style_context,
gtk_style_context_get_state (style_context),
"background-color", &background_color,
NULL);
gtk_style_context_get_color (style_context,
gtk_style_context_get_state (style_context),
&foreground_color);
gtk_style_context_restore (style_context);
g_object_set (cell_renderer,
"cell-background-rgba", background_color,
NULL);
g_object_bind_property (completion, "show-icons",
cell_renderer, "visible",
G_BINDING_SYNC_CREATE);
/* Proposal text cell renderer */
cell_renderer = GTK_CELL_RENDERER (gtk_builder_get_object (builder, "cell_renderer_proposal"));
completion->priv->cell_renderer_proposal = cell_renderer;
column = GTK_TREE_VIEW_COLUMN (gtk_builder_get_object (builder, "tree_view_column_proposal"));
gtk_tree_view_column_set_attributes (column, cell_renderer,
"markup", GTK_SOURCE_COMPLETION_MODEL_COLUMN_MARKUP,
"cell-background-set", GTK_SOURCE_COMPLETION_MODEL_COLUMN_IS_HEADER,
"foreground-set", GTK_SOURCE_COMPLETION_MODEL_COLUMN_IS_HEADER,
NULL);
g_object_set (cell_renderer,
"foreground-rgba", &foreground_color,
"cell-background-rgba", background_color,
NULL);
/* Accelerators cell renderer */
column = GTK_TREE_VIEW_COLUMN (gtk_builder_get_object (builder, "tree_view_column_accelerator"));
cell_renderer = GTK_CELL_RENDERER (gtk_builder_get_object (builder, "cell_renderer_accelerator"));
gtk_tree_view_column_set_attributes (column,
cell_renderer,
"cell-background-set", GTK_SOURCE_COMPLETION_MODEL_COLUMN_IS_HEADER,
NULL);
g_object_set (cell_renderer,
"foreground-rgba", &foreground_color,
"cell-background-rgba", background_color,
NULL);
gtk_tree_view_column_set_cell_data_func (column,
cell_renderer,
(GtkTreeCellDataFunc)render_proposal_accelerator_func,
completion,
NULL);
g_signal_connect_object (completion,
"notify::accelerators",
G_CALLBACK (accelerators_notify_cb),
column,
0);
if (background_color != NULL)
{
gdk_rgba_free (background_color);
}
}
static void
init_main_window (GtkSourceCompletion *completion,
GtkBuilder *builder)
{
if (completion->priv->view == NULL)
{
return;
}
completion->priv->main_window = GTK_SOURCE_COMPLETION_INFO (gtk_builder_get_object (builder, "main_window"));
completion->priv->info_button = GTK_TOGGLE_BUTTON (gtk_builder_get_object (builder, "info_button"));
completion->priv->selection_image = GTK_IMAGE (gtk_builder_get_object (builder, "selection_image"));
completion->priv->selection_label = GTK_LABEL (gtk_builder_get_object (builder, "selection_label"));
completion->priv->bottom_bar = GTK_WIDGET (gtk_builder_get_object (builder, "bottom_bar"));
gtk_container_set_border_width (GTK_CONTAINER (completion->priv->main_window), 0);
gtk_window_set_attached_to (GTK_WINDOW (completion->priv->main_window),
GTK_WIDGET (completion->priv->view));
g_signal_connect (completion->priv->main_window,
"configure-event",
G_CALLBACK (gtk_source_completion_configure_event),
completion);
g_signal_connect_swapped (completion->priv->main_window,
"size-allocate",
G_CALLBACK (update_window_position),
completion);
g_signal_connect (completion->priv->main_window,
"delete-event",
G_CALLBACK (gtk_widget_hide_on_delete),
NULL);
g_signal_connect (completion->priv->main_window,
"notify::transient-for",
G_CALLBACK (update_transient_for_info),
completion);
g_signal_connect_swapped (completion->priv->info_button,
"toggled",
G_CALLBACK (update_info_window_visibility),
completion);
}
static void
init_info_window (GtkSourceCompletion *completion)
{
completion->priv->info_window = gtk_source_completion_info_new ();
g_object_ref_sink (completion->priv->info_window);
gtk_window_set_attached_to (GTK_WINDOW (completion->priv->info_window),
GTK_WIDGET (completion->priv->main_window));
g_signal_connect_swapped (completion->priv->info_window,
"size-allocate",
G_CALLBACK (update_info_position),
completion);
/* Default info widget */
completion->priv->default_info = GTK_LABEL (gtk_label_new (NULL));
g_object_ref_sink (completion->priv->default_info);
gtk_widget_show (GTK_WIDGET (completion->priv->default_info));
}
static void
connect_style_context (GtkSourceCompletion *completion)
{
GtkStyleContext *style_context;
if (completion->priv->view == NULL)
{
return;
}
style_context = gtk_widget_get_style_context (GTK_WIDGET (completion->priv->view));
g_signal_connect_object (style_context,
"changed",
G_CALLBACK (style_context_changed),
completion,
G_CONNECT_AFTER);
style_context_changed (style_context, completion);
}
static void
gtk_source_completion_constructed (GObject *object)
{
GtkSourceCompletion *completion = GTK_SOURCE_COMPLETION (object);
GError *error = NULL;
GtkBuilder *builder = gtk_builder_new ();
GtkSourceCompletionContainer *container = _gtk_source_completion_container_new ();
g_object_ref_sink (container);
gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
/* GtkSourceCompletionContainer is a private type. */
gtk_builder_expose_object (builder, "completion_container", G_OBJECT (container));
gtk_builder_add_from_resource (builder,
"/org/gnome/gtksourceview/ui/gtksourcecompletion.ui",
&error);
if (error != NULL)
{
g_error ("Error while loading the completion UI: %s", error->message);
}
init_tree_view (completion, builder);
init_main_window (completion, builder);
init_info_window (completion);
connect_style_context (completion);
g_object_unref (builder);
g_object_unref (container);
G_OBJECT_CLASS (gtk_source_completion_parent_class)->constructed (object);
}
static void
gtk_source_completion_class_init (GtkSourceCompletionClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkBindingSet *binding_set;
object_class->get_property = gtk_source_completion_get_property;
object_class->set_property = gtk_source_completion_set_property;
object_class->dispose = gtk_source_completion_dispose;
object_class->constructed = gtk_source_completion_constructed;
klass->show = gtk_source_completion_show_default;
klass->hide = gtk_source_completion_hide_default;
klass->move_cursor = gtk_source_completion_move_cursor;
klass->move_page = gtk_source_completion_move_page;
klass->activate_proposal = gtk_source_completion_activate_proposal;
/**
* GtkSourceCompletion:view:
*
* The #GtkSourceView bound to the completion object.
*/
g_object_class_install_property (object_class,
PROP_VIEW,
g_param_spec_object ("view",
"View",
"The GtkSourceView bound to the completion",
GTK_SOURCE_TYPE_VIEW,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/**
* GtkSourceCompletion:remember-info-visibility:
*
* Determines whether the visibility of the info window should be
* saved when the completion is hidden, and restored when the completion
* is shown again.
*/
g_object_class_install_property (object_class,
PROP_REMEMBER_INFO_VISIBILITY,
g_param_spec_boolean ("remember-info-visibility",
"Remember Info Visibility",
"Remember the last info window visibility state",
FALSE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
/**
* GtkSourceCompletion:select-on-show:
*
* Determines whether the first proposal should be selected when the
* completion is first shown.
*/
g_object_class_install_property (object_class,
PROP_SELECT_ON_SHOW,
g_param_spec_boolean ("select-on-show",
"Select on Show",
"Select first proposal when completion is shown",
TRUE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
/**
* GtkSourceCompletion:show-headers:
*
* Determines whether provider headers should be shown in the proposal
* list. It can be useful to disable when there is only one provider.
*/
g_object_class_install_property (object_class,
PROP_SHOW_HEADERS,
g_param_spec_boolean ("show-headers",
"Show Headers",
"Show provider headers when proposals from multiple providers are available",
TRUE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
/**
* GtkSourceCompletion:show-icons:
*
* Determines whether provider and proposal icons should be shown in
* the completion popup.
*/
g_object_class_install_property (object_class,
PROP_SHOW_ICONS,
g_param_spec_boolean ("show-icons",
"Show Icons",
"Show provider and proposal icons in the completion popup",
TRUE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
/**
* GtkSourceCompletion:accelerators:
*
* Number of keyboard accelerators to show for the first proposals. For
* example, to activate the first proposal, the user can press
* <keycombo><keycap>Alt</keycap><keycap>1</keycap></keycombo>.
*/
g_object_class_install_property (object_class,
PROP_ACCELERATORS,
g_param_spec_uint ("accelerators",
"Accelerators",
"Number of proposal accelerators to show",
0,
10,
5,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
/**
* GtkSourceCompletion:auto-complete-delay:
*
* Determines the popup delay (in milliseconds) at which the completion
* will be shown for interactive completion.
*/
g_object_class_install_property (object_class,
PROP_AUTO_COMPLETE_DELAY,
g_param_spec_uint ("auto-complete-delay",
"Auto Complete Delay",
"Completion popup delay for interactive completion",
0,
G_MAXUINT,
250,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
/**
* GtkSourceCompletion:provider-page-size:
*
* The scroll page size of the provider pages in the completion window.
*
* See the #GtkSourceCompletion::move-page signal.
*/
g_object_class_install_property (object_class,
PROP_PROVIDER_PAGE_SIZE,
g_param_spec_uint ("provider-page-size",
"Provider Page Size",
"Provider scrolling page size",
1,
G_MAXUINT,
5,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
/**
* GtkSourceCompletion:proposal-page-size:
*
* The scroll page size of the proposals in the completion window. In
* other words, when <keycap>PageDown</keycap> or
* <keycap>PageUp</keycap> is pressed, the selected
* proposal becomes the one which is located one page size backward or
* forward.
*
* See also the #GtkSourceCompletion::move-cursor signal.
*/
g_object_class_install_property (object_class,
PROP_PROPOSAL_PAGE_SIZE,
g_param_spec_uint ("proposal-page-size",
"Proposal Page Size",
"Proposal scrolling page size",
1,
G_MAXUINT,
5,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
/**
* GtkSourceCompletion::show:
* @completion: The #GtkSourceCompletion who emits the signal
*
* Emitted when the completion window is shown. The default handler
* will actually show the window.
*/
signals[SHOW] =
g_signal_new ("show",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkSourceCompletionClass, show),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
/**
* GtkSourceCompletion::hide:
* @completion: The #GtkSourceCompletion who emits the signal
*
* Emitted when the completion window is hidden. The default handler
* will actually hide the window.
*/
signals[HIDE] =
g_signal_new ("hide",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkSourceCompletionClass, hide),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
/**
* GtkSourceCompletion::populate-context:
* @completion: The #GtkSourceCompletion who emits the signal
* @context: The #GtkSourceCompletionContext for the current completion
*
* Emitted just before starting to populate the completion with providers.
* You can use this signal to add additional attributes in the context.
*/
signals[POPULATE_CONTEXT] =
g_signal_new ("populate-context",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkSourceCompletionClass, populate_context),
NULL, NULL, NULL,
G_TYPE_NONE,
1,
GTK_SOURCE_TYPE_COMPLETION_CONTEXT);
/* Actions */
/**
* GtkSourceCompletion::move-cursor:
* @completion: The #GtkSourceCompletion who emits the signal
* @step: The #GtkScrollStep by which to move the cursor
* @num: The amount of steps to move the cursor
*
* The #GtkSourceCompletion::move-cursor signal is a keybinding
* signal which gets emitted when the user initiates a cursor
* movement.
*
* The <keycap>Up</keycap>, <keycap>Down</keycap>,
* <keycap>PageUp</keycap>, <keycap>PageDown</keycap>,
* <keycap>Home</keycap> and <keycap>End</keycap> keys are bound to the
* normal behavior expected by those keys.
*
* When @step is equal to %GTK_SCROLL_PAGES, the page size is defined by
* the #GtkSourceCompletion:proposal-page-size property. It is used for
* the <keycap>PageDown</keycap> and <keycap>PageUp</keycap> keys.
*
* Applications should not connect to it, but may emit it with
* g_signal_emit_by_name() if they need to control the cursor
* programmatically.
*/
signals [MOVE_CURSOR] =
g_signal_new ("move-cursor",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkSourceCompletionClass, move_cursor),
NULL, NULL, NULL,
G_TYPE_NONE,
2,
GTK_TYPE_SCROLL_STEP,
G_TYPE_INT);
/**
* GtkSourceCompletion::move-page:
* @completion: The #GtkSourceCompletion who emits the signal
* @step: The #GtkScrollStep by which to move the page
* @num: The amount of steps to move the page
*
* The #GtkSourceCompletion::move-page signal is a keybinding
* signal which gets emitted when the user initiates a page
* movement (i.e. switches between provider pages).
*
* <keycombo><keycap>Control</keycap><keycap>Left</keycap></keycombo>
* is for going to the previous provider.
* <keycombo><keycap>Control</keycap><keycap>Right</keycap></keycombo>
* is for going to the next provider.
* <keycombo><keycap>Control</keycap><keycap>Home</keycap></keycombo>
* is for displaying all the providers.
* <keycombo><keycap>Control</keycap><keycap>End</keycap></keycombo>
* is for going to the last provider.
*
* When @step is equal to #GTK_SCROLL_PAGES, the page size is defined by
* the #GtkSourceCompletion:provider-page-size property.
*
* Applications should not connect to it, but may emit it with
* g_signal_emit_by_name() if they need to control the page selection
* programmatically.
*/
signals [MOVE_PAGE] =
g_signal_new ("move-page",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkSourceCompletionClass, move_page),
NULL, NULL, NULL,
G_TYPE_NONE,
2,
GTK_TYPE_SCROLL_STEP,
G_TYPE_INT);
/**
* GtkSourceCompletion::activate-proposal:
* @completion: The #GtkSourceCompletion who emits the signal
*
* The #GtkSourceCompletion::activate-proposal signal is a
* keybinding signal which gets emitted when the user initiates
* a proposal activation.
*
* Applications should not connect to it, but may emit it with
* g_signal_emit_by_name() if they need to control the proposal
* activation programmatically.
*/
signals [ACTIVATE_PROPOSAL] =
g_signal_new ("activate-proposal",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkSourceCompletionClass, activate_proposal),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
/* Key bindings */
binding_set = gtk_binding_set_by_class (klass);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Down,
0,
"move-cursor",
2,
GTK_TYPE_SCROLL_STEP, GTK_SCROLL_STEPS,
G_TYPE_INT, 1);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Page_Down,
0,
"move-cursor",
2,
GTK_TYPE_SCROLL_STEP, GTK_SCROLL_PAGES,
G_TYPE_INT, 1);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Up,
0,
"move-cursor",
2,
GTK_TYPE_SCROLL_STEP, GTK_SCROLL_STEPS,
G_TYPE_INT, -1);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Page_Up,
0,
"move-cursor",
2,
GTK_TYPE_SCROLL_STEP, GTK_SCROLL_PAGES,
G_TYPE_INT, -1);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Home,
0,
"move-cursor",
2,
GTK_TYPE_SCROLL_STEP, GTK_SCROLL_ENDS,
G_TYPE_INT, -1);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_End,
0,
"move-cursor",
2,
GTK_TYPE_SCROLL_STEP, GTK_SCROLL_ENDS,
G_TYPE_INT, 1);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Escape,
0,
"hide",
0);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Return,
0,
"activate-proposal",
0);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Tab,
0,
"activate-proposal",
0);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Left,
GDK_CONTROL_MASK,
"move-page",
2,
GTK_TYPE_SCROLL_STEP, GTK_SCROLL_STEPS,
G_TYPE_INT, -1);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Right,
GDK_CONTROL_MASK,
"move-page",
2,
GTK_TYPE_SCROLL_STEP, GTK_SCROLL_STEPS,
G_TYPE_INT, 1);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Home,
GDK_CONTROL_MASK,
"move-page",
2,
GTK_TYPE_SCROLL_STEP, GTK_SCROLL_ENDS,
G_TYPE_INT, -1);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_End,
GDK_CONTROL_MASK,
"move-page",
2,
GTK_TYPE_SCROLL_STEP, GTK_SCROLL_ENDS,
G_TYPE_INT, 1);
}
static void
gtk_source_completion_init (GtkSourceCompletion *completion)
{
completion->priv = gtk_source_completion_get_instance_private (completion);
}
static GObject *
gtk_source_completion_buildable_get_internal_child (GtkBuildable *buildable,
GtkBuilder *builder,
const gchar *childname)
{
GtkSourceCompletion *completion = GTK_SOURCE_COMPLETION (buildable);
if (g_strcmp0 (childname, "info_window") == 0)
{
return G_OBJECT (gtk_source_completion_get_info_window (completion));
}
return NULL;
}
static void
gtk_source_completion_buildable_interface_init (GtkBuildableIface *iface)
{
iface->get_internal_child = gtk_source_completion_buildable_get_internal_child;
}
void
_gtk_source_completion_add_proposals (GtkSourceCompletion *completion,
GtkSourceCompletionContext *context,
GtkSourceCompletionProvider *provider,
GList *proposals,
gboolean finished)
{
GList *item;
g_return_if_fail (GTK_SOURCE_IS_COMPLETION (completion));
g_return_if_fail (GTK_SOURCE_IS_COMPLETION_CONTEXT (context));
g_return_if_fail (GTK_SOURCE_IS_COMPLETION_PROVIDER (provider));
g_return_if_fail (completion->priv->context == context);
item = g_list_find (completion->priv->running_providers, provider);
g_return_if_fail (item != NULL);
gtk_source_completion_model_add_proposals (completion->priv->model_proposals,
provider,
proposals);
if (finished)
{
/* Remove provider from list of running providers */
completion->priv->running_providers =
g_list_delete_link (completion->priv->running_providers,
item);
if (completion->priv->running_providers == NULL)
{
populating_done (completion, context);
}
}
}
/**
* gtk_source_completion_start:
* @completion: a #GtkSourceCompletion.
* @providers: (element-type GtkSource.CompletionProvider) (nullable):
* a list of #GtkSourceCompletionProvider, or %NULL.
* @context: (transfer floating): The #GtkSourceCompletionContext
* with which to start the completion.
*
* Starts a new completion with the specified #GtkSourceCompletionContext and
* a list of potential candidate providers for completion.
*
* It can be convenient for showing a completion on-the-fly, without the need to
* add or remove providers to the #GtkSourceCompletion.
*
* Another solution is to add providers with
* gtk_source_completion_add_provider(), and implement
* gtk_source_completion_provider_match() for each provider.
*
* Returns: %TRUE if it was possible to the show completion window.
*/
gboolean
gtk_source_completion_start (GtkSourceCompletion *completion,
GList *providers,
GtkSourceCompletionContext *context)
{
GList *selected_providers;
g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION (completion), FALSE);
g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_CONTEXT (context), FALSE);
if (completion->priv->view == NULL)
{
return FALSE;
}
/* Make sure to clear any active completion */
reset_completion (completion);
/* We need to take owenership of the context right before doing
anything so we don't leak it or get a crash emitting the signal */
g_object_ref_sink (context);
if (providers == NULL)
{
g_object_unref (context);
return FALSE;
}
/* Populate the context */
g_signal_emit (completion, signals[POPULATE_CONTEXT], 0, context);
/* From the providers, select the ones that match the context */
selected_providers = select_providers (providers, context);
if (selected_providers == NULL)
{
g_object_unref (context);
gtk_source_completion_hide (completion);
return FALSE;
}
update_completion (completion, selected_providers, context);
g_list_free (selected_providers);
g_object_unref (context);
return TRUE;
}
/**
* gtk_source_completion_get_providers:
* @completion: a #GtkSourceCompletion.
*
* Get list of providers registered on @completion. The returned list is owned
* by the completion and should not be freed.
*
* Returns: (element-type GtkSource.CompletionProvider) (transfer none):
* list of #GtkSourceCompletionProvider.
*/
GList *
gtk_source_completion_get_providers (GtkSourceCompletion *completion)
{
g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION (completion), NULL);
return completion->priv->providers;
}
GQuark
gtk_source_completion_error_quark (void)
{
static GQuark quark = 0;
if (G_UNLIKELY (quark == 0))
{
quark = g_quark_from_static_string ("gtk-source-completion-error-quark");
}
return quark;
}
/**
* gtk_source_completion_new:
* @view: a #GtkSourceView.
*
* Creates a new #GtkSourceCompletion associated with @view.
*
* Returns: a new #GtkSourceCompletion.
*/
GtkSourceCompletion *
gtk_source_completion_new (GtkSourceView *view)
{
g_return_val_if_fail (GTK_SOURCE_IS_VIEW (view), NULL);
return g_object_new (GTK_SOURCE_TYPE_COMPLETION,
"view", view,
NULL);
}
/**
* gtk_source_completion_add_provider:
* @completion: a #GtkSourceCompletion.
* @provider: a #GtkSourceCompletionProvider.
* @error: a #GError.
*
* Add a new #GtkSourceCompletionProvider to the completion object. This will
* add a reference @provider, so make sure to unref your own copy when you
* no longer need it.
*
* Returns: %TRUE if @provider was successfully added, otherwise if @error
* is provided, it will be set with the error and %FALSE is returned.
*/
gboolean
gtk_source_completion_add_provider (GtkSourceCompletion *completion,
GtkSourceCompletionProvider *provider,
GError **error)
{
g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION (completion), FALSE);
g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROVIDER (provider), FALSE);
if (g_list_find (completion->priv->providers, provider) != NULL)
{
if (error != NULL)
{
g_set_error (error,
GTK_SOURCE_COMPLETION_ERROR,
GTK_SOURCE_COMPLETION_ERROR_ALREADY_BOUND,
"Provider is already bound to this completion object");
}
return FALSE;
}
completion->priv->providers = g_list_append (completion->priv->providers,
g_object_ref (provider));
if (error != NULL)
{
*error = NULL;
}
return TRUE;
}
/**
* gtk_source_completion_remove_provider:
* @completion: a #GtkSourceCompletion.
* @provider: a #GtkSourceCompletionProvider.
* @error: a #GError.
*
* Remove @provider from the completion.
*
* Returns: %TRUE if @provider was successfully removed, otherwise if @error
* is provided, it will be set with the error and %FALSE is returned.
*/
gboolean
gtk_source_completion_remove_provider (GtkSourceCompletion *completion,
GtkSourceCompletionProvider *provider,
GError **error)
{
GList *item;
g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION (completion), FALSE);
g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROVIDER (provider), FALSE);
item = g_list_find (completion->priv->providers, provider);
if (item == NULL)
{
if (error != NULL)
{
g_set_error (error,
GTK_SOURCE_COMPLETION_ERROR,
GTK_SOURCE_COMPLETION_ERROR_NOT_BOUND,
"Provider is not bound to this completion object");
}
return FALSE;
}
completion->priv->providers = g_list_remove_link (completion->priv->providers, item);
g_object_unref (provider);
if (error != NULL)
{
*error = NULL;
}
return TRUE;
}
/**
* gtk_source_completion_hide:
* @completion: a #GtkSourceCompletion.
*
* Hides the completion if it is active (visible).
*/
void
gtk_source_completion_hide (GtkSourceCompletion *completion)
{
g_return_if_fail (GTK_SOURCE_IS_COMPLETION (completion));
reset_completion (completion);
if (gtk_widget_get_visible (GTK_WIDGET (completion->priv->main_window)))
{
g_signal_emit (completion, signals[HIDE], 0);
}
}
/**
* gtk_source_completion_get_info_window:
* @completion: a #GtkSourceCompletion.
*
* The info widget is the window where the completion displays optional extra
* information of the proposal.
*
* Returns: (transfer none): The #GtkSourceCompletionInfo window
* associated with @completion.
*/
GtkSourceCompletionInfo *
gtk_source_completion_get_info_window (GtkSourceCompletion *completion)
{
g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION (completion), NULL);
return completion->priv->info_window;
}
/**
* gtk_source_completion_get_view:
* @completion: a #GtkSourceCompletion.
*
* The #GtkSourceView associated with @completion, or %NULL if the view has been
* destroyed.
*
* Returns: (nullable) (transfer none): The #GtkSourceView associated with
* @completion, or %NULL.
*/
GtkSourceView *
gtk_source_completion_get_view (GtkSourceCompletion *completion)
{
g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION (completion), NULL);
return completion->priv->view;
}
/**
* gtk_source_completion_create_context:
* @completion: a #GtkSourceCompletion.
* @position: (nullable): a #GtkTextIter, or %NULL.
*
* Create a new #GtkSourceCompletionContext for @completion. The position where
* the completion occurs can be specified by @position. If @position is %NULL,
* the current cursor position will be used.
*
* Returns: (transfer floating): a new #GtkSourceCompletionContext.
* The reference being returned is a 'floating' reference,
* so if you invoke gtk_source_completion_start() with this context
* you don't need to unref it.
*/
GtkSourceCompletionContext *
gtk_source_completion_create_context (GtkSourceCompletion *completion,
GtkTextIter *position)
{
GtkTextIter iter;
g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION (completion), NULL);
if (completion->priv->view == NULL)
{
return NULL;
}
if (position == NULL)
{
get_iter_at_insert (completion, &iter);
}
else
{
iter = *position;
}
return _gtk_source_completion_context_new (completion, &iter);
}
/**
* gtk_source_completion_block_interactive:
* @completion: a #GtkSourceCompletion.
*
* Block interactive completion. This can be used to disable interactive
* completion when inserting or deleting text from the buffer associated with
* the completion. Use gtk_source_completion_unblock_interactive() to enable
* interactive completion again.
*
* This function may be called multiple times. It will continue to block
* interactive completion until gtk_source_completion_unblock_interactive()
* has been called the same number of times.
*/
void
gtk_source_completion_block_interactive (GtkSourceCompletion *completion)
{
g_return_if_fail (GTK_SOURCE_IS_COMPLETION (completion));
if (completion->priv->view == NULL)
{
return;
}
if (completion->priv->block_interactive_num == 0)
{
block_interactive (completion);
}
completion->priv->block_interactive_num++;
}
/**
* gtk_source_completion_unblock_interactive:
* @completion: a #GtkSourceCompletion.
*
* Unblock interactive completion. This can be used after using
* gtk_source_completion_block_interactive() to enable interactive completion
* again.
*/
void
gtk_source_completion_unblock_interactive (GtkSourceCompletion *completion)
{
g_return_if_fail (GTK_SOURCE_IS_COMPLETION (completion));
if (completion->priv->view == NULL)
{
return;
}
if (completion->priv->block_interactive_num == 1)
{
g_signal_handlers_unblock_by_func (completion->priv->buffer,
buffer_insert_text_cb,
completion);
g_signal_handlers_unblock_by_func (completion->priv->buffer,
buffer_delete_range_cb,
completion);
}
if (completion->priv->block_interactive_num > 0)
{
completion->priv->block_interactive_num--;
}
}
|