1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506
|
/*******************************************************************************
* *
* text.c - Text Editing Widget *
* *
* Copyright (c) 1995 Universities Research Association, Inc. *
* All rights reserved. *
* *
* This material resulted from work developed under a Government Contract and *
* is subject to the following license: The Government retains a paid-up, *
* nonexclusive, irrevocable worldwide license to reproduce, prepare derivative *
* works, perform publicly and display publicly by or for the Government, *
* including the right to distribute to other Government contractors. Neither *
* the United States nor the United States Department of Energy, nor any of *
* their employees, makes any warranty, express or implied, or assumes any *
* legal liability or responsibility for the accuracy, completeness, or *
* usefulness of any information, apparatus, product, or process disclosed, or *
* represents that its use would not infringe privately owned rights. *
* *
* Fermilab Nirvana GUI Library *
* June 15, 1995 *
* *
* Written by Mark Edel *
* *
* Modifications: *
* *
* 4/19/96 - Joachim Keltsch - Motif Input Method support *
* *
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#include <X11/Intrinsic.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/cursorfont.h>
#include <Xm/Xm.h>
#include <Xm/XmP.h>
#if XmVersion >= 1002
#include <Xm/PrimitiveP.h>
#endif
#include "textBuf.h"
#include "textDisp.h"
#include "textP.h"
#include "textSel.h"
#include "textDrag.h"
/* Number of pixels of motion from the initial (grab-focus) button press
required to begin recognizing a mouse drag for the purpose of making a
selection */
#define SELECT_THRESHOLD 5
/* Length of delay in milliseconds for vertical autoscrolling */
#define VERTICAL_SCROLL_DELAY 50
static void initialize(TextWidget request, TextWidget new);
static void redisplay(TextWidget w, XEvent *event, Region region);
static void destroy(TextWidget w);
static void resize(TextWidget w);
static Boolean setValues(TextWidget current, TextWidget request,
TextWidget new);
static void realize(Widget w, XtValueMask *valueMask,
XSetWindowAttributes *attributes);
static XtGeometryResult queryGeometry(Widget w, XtWidgetGeometry *proposed,
XtWidgetGeometry *answer);
static void grabFocusAP(Widget w, XEvent *event, String *args,
Cardinal *n_args);
static void moveDestinationAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void extendAdjustAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void extendStartAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void extendEndAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void processCancelAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void secondaryStartAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void secondaryOrDragStartAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void secondaryAdjustAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void secondaryOrDragAdjustAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void copyToAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void copyToOrEndDragAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void copyPrimaryAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void cutPrimaryAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void moveToAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void moveToOrEndDragAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void endDragAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void exchangeAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void mousePanAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void pasteClipboardAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void copyClipboardAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void cutClipboardAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void insertStringAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void selfInsertAP(Widget w, XEvent *event, String *args,
Cardinal *n_args);
static void newlineAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void newlineAndIndentAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void newlineNoIndentAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void processTabAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void endOfLineAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void beginningOfLineAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void deleteSelectionAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void deletePreviousCharacterAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void deleteNextCharacterAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void deletePreviousWordAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void deleteNextWordAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void deleteToStartOfLineAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void deleteToEndOfLineAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void forwardCharacterAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void backwardCharacterAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void forwardWordAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void backwardWordAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void forwardParagraphAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void backwardParagraphAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void keySelectAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void processUpAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void processShiftUpAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void processDownAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void processShiftDownAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void beginningOfFileAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void endOfFileAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void nextPageAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void previousPageAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void pageLeftAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void pageRightAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void toggleOverstrikeAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void scrollUpAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void scrollDownAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void scrollToLineAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void selectAllAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void deselectAllAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void focusInAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void focusOutAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs);
static void checkMoveSelectionChange(Widget w, XEvent *event, int startPos,
String *args, Cardinal *nArgs);
static void keyMoveExtendSelection(Widget w, XEvent *event, int startPos,
int rectangular);
static void checkAutoShowInsertPos(Widget w);
static int checkReadOnly(Widget w);
static void simpleInsertAtCursor(Widget w, char *chars, XEvent *event,
int allowPendingDelete);
static int pendingSelection(Widget w);
static int deletePendingSelection(Widget w, XEvent *event);
static int deleteEmulatedTab(Widget w, XEvent *event);
static void selectWord(Widget w, int pointerX);
static int spanForward(textBuffer *buf, int startPos, char *searchChars,
int ignoreSpace, int *foundPos);
static int spanBackward(textBuffer *buf, int startPos, char *searchChars, int
ignoreSpace, int *foundPos);
static void selectLine(Widget w);
static int whiteSpaceAtPos(textBuffer *buf, int pos);
static int startOfWord(TextWidget w, int pos);
static int endOfWord(TextWidget w, int pos);
static void checkAutoScroll(TextWidget w, int x, int y);
static void endDrag(Widget w);
static void cancelDrag(Widget w);
static void callCursorMovementCBs(Widget w, XEvent *event);
static void adjustSelection(TextWidget tw, int x, int y);
static void adjustSecondarySelection(TextWidget tw, int x, int y);
static void autoScrollTimerProc(XtPointer clientData, XtIntervalId *id);
static char *wrapText(TextWidget tw, char *startLine, char *text, int bufOffset,
int wrapMargin, int *breakBefore);
static int wrapLine(TextWidget tw, textBuffer *buf, int bufOffset,
int lineStartPos, int lineEndPos, int limitPos, int *breakAt,
int *charsAdded);
static char *createIndentString(TextWidget tw, textBuffer *buf, int bufOffset,
int lineStartPos, int lineEndPos, int *length, int *column);
static void cursorBlinkTimerProc(XtPointer clientData, XtIntervalId *id);
static int hasKey(char *key, String *args, Cardinal *nArgs);
static int max(int i1, int i2);
static int min(int i1, int i2);
static int strCaseCmp(char *str1, char *str2);
static char defaultTranslations[] =
"Ctrl<KeyPress>osfBackSpace: delete_previous_word()\n\
<KeyPress>osfBackSpace: delete_previous_character()\n\
Alt Shift Ctrl<KeyPress>osfDelete: cut_primary(\"rect\")\n\
Meta Shift Ctrl<KeyPress>osfDelete: cut_primary(\"rect\")\n\
Shift Ctrl<KeyPress>osfDelete: cut_primary()\n\
Ctrl<KeyPress>osfDelete: delete_to_end_of_line()\n\
Shift<KeyPress>osfDelete: cut_clipboard()\n\
<KeyPress>osfDelete: delete_next_character()\n\
Alt Shift Ctrl<KeyPress>osfInsert: copy_primary(\"rect\")\n\
Meta Shift Ctrl<KeyPress>osfInsert: copy_primary(\"rect\")\n\
Shift Ctrl<KeyPress>osfInsert: copy_primary()\n\
Shift<KeyPress>osfInsert: paste_clipboard()\n\
Ctrl<KeyPress>osfInsert: copy_clipboard()\n\
Shift Ctrl<KeyPress>osfCut: cut_primary()\n\
<KeyPress>osfCut: cut_clipboard()\n\
<KeyPress>osfCopy: copy_clipboard()\n\
<KeyPress>osfPaste: paste_clipboard()\n\
<KeyPress>osfPrimaryPaste: copy_primary()\n\
Alt Shift Ctrl<KeyPress>osfBeginLine: beginning_of_file(\"extend\", \"rect\")\n\
Meta Shift Ctrl<KeyPress>osfBeginLine: beginning_of_file(\"extend\" \"rect\")\n\
Alt Shift<KeyPress>osfBeginLine: beginning_of_line(\"extend\", \"rect\")\n\
Meta Shift<KeyPress>osfBeginLine: beginning_of_line(\"extend\", \"rect\")\n\
Shift Ctrl<KeyPress>osfBeginLine: beginning_of_file(\"extend\")\n\
Ctrl<KeyPress>osfBeginLine: beginning_of_file()\n\
Shift<KeyPress>osfBeginLine: beginning_of_line(\"extend\")\n\
<KeyPress>osfBeginLine: beginning_of_line()\n\
Alt Shift Ctrl<KeyPress>osfEndLine: end_of_file(\"extend\", \"rect\")\n\
Meta Shift Ctrl<KeyPress>osfEndLine: end_of_file(\"extend\", \"rect\")\n\
Alt Shift<KeyPress>osfEndLine: end_of_line(\"extend\", \"rect\")\n\
Meta Shift<KeyPress>osfEndLine: end_of_line(\"extend\", \"rect\")\n\
Shift Ctrl<KeyPress>osfEndLine: end_of_file(\"extend\")\n\
Ctrl<KeyPress>osfEndLine: end_of_file()\n\
Shift<KeyPress>osfEndLine: end_of_line(\"extend\")\n\
<KeyPress>osfEndLine: end_of_line()\n\
Alt Shift Ctrl<KeyPress>osfLeft: backward_word(\"extend\", \"rect\")\n\
Meta Shift Ctrl<KeyPress>osfLeft: backward_word(\"extend\", \"rect\")\n\
Alt Shift<KeyPress>osfLeft: key_select(\"left\", \"rect\")\n\
Meta Shift<KeyPress>osfLeft: key_select(\"left\", \"rect\")\n\
Shift Ctrl<KeyPress>osfLeft: backward_word(\"extend\")\n\
Ctrl<KeyPress>osfLeft: backward_word()\n\
Shift<KeyPress>osfLeft: key_select(\"left\")\n\
<KeyPress>osfLeft: backward_character()\n\
Alt Shift Ctrl<KeyPress>osfRight: forward_word(\"extend\", \"rect\")\n\
Meta Shift Ctrl<KeyPress>osfRight: forward_word(\"extend\", \"rect\")\n\
Alt Shift<KeyPress>osfRight: key_select(\"right\", \"rect\")\n\
Meta Shift<KeyPress>osfRight: key_select(\"right\", \"rect\")\n\
Shift Ctrl<KeyPress>osfRight: forward_word(\"extend\")\n\
Ctrl<KeyPress>osfRight: forward_word()\n\
Shift<KeyPress>osfRight: key_select(\"right\")\n\
<KeyPress>osfRight: forward_character()\n\
Alt Shift Ctrl<KeyPress>osfUp: backward_paragraph(\"extend\", \"rect\")\n\
Meta Shift Ctrl<KeyPress>osfUp: backward_paragraph(\"extend\", \"rect\")\n\
Alt Shift<KeyPress>osfUp: process_shift_up(\"rect\")\n\
Meta Shift<KeyPress>osfUp: process_shift_up(\"rect\")\n\
Shift Ctrl<KeyPress>osfUp: backward_paragraph(\"extend\")\n\
Ctrl<KeyPress>osfUp: backward_paragraph()\n\
Shift<KeyPress>osfUp: process_shift_up()\n\
<KeyPress>osfUp: process_up()\n\
Alt Shift Ctrl<KeyPress>osfDown: forward_paragraph(\"extend\", \"rect\")\n\
Meta Shift Ctrl<KeyPress>osfDown: forward_paragraph(\"extend\", \"rect\")\n\
Alt Shift<KeyPress>osfDown: process_shift_down(\"rect\")\n\
Meta Shift<KeyPress>osfDown: process_shift_down(\"rect\")\n\
Shift Ctrl<KeyPress>osfDown: forward_paragraph(\"extend\")\n\
Ctrl<KeyPress>osfDown: forward_paragraph()\n\
Shift<KeyPress>osfDown: process_shift_down()\n\
<KeyPress>osfDown: process_down()\n\
Alt Shift<KeyPress>osfPageLeft: page_left(\"extend\", \"rect\")\n\
Meta Shift<KeyPress>osfPageLeft: page_left(\"extend\", \"rect\")\n\
Shift<KeyPress>osfPageLeft: page_left(\"extend\")\n\
<KeyPress>osfPageLeft: page_left()\n\
Alt Shift<KeyPress>osfPageRight: page_right(\"extend\", \"rect\")\n\
Meta Shift<KeyPress>osfPageRight: page_right(\"extend\", \"rect\")\n\
Shift<KeyPress>osfPageRight: page_right(\"extend\")\n\
<KeyPress>osfPageRight: page_right()\n\
Alt Shift Ctrl<KeyPress>osfPageUp: page_left(\"extend\", \"rect\")\n\
Meta Shift Ctrl<KeyPress>osfPageUp: page_left(\"extend\", \"rect\")\n\
Alt Shift<KeyPress>osfPageUp: previous_page(\"extend\", \"rect\")\n\
Meta Shift<KeyPress>osfPageUp: previous_page(\"extend\", \"rect\")\n\
Shift Ctrl<KeyPress>osfPageUp: page_left(\"extend\")\n\
Ctrl<KeyPress>osfPageUp: page_left()\n\
Shift<KeyPress>osfPageUp: previous_page(\"extend\")\n\
<KeyPress>osfPageUp: previous_page()\n\
Alt Shift Ctrl<KeyPress>osfPageDown: page_right(\"extend\", \"rect\")\n\
Meta Shift Ctrl<KeyPress>osfPageDown: page_right(\"extend\", \"rect\")\n\
Alt Shift<KeyPress>osfPageDown: next_page(\"extend\", \"rect\")\n\
Meta Shift<KeyPress>osfPageDown: next_page(\"extend\", \"rect\")\n\
Shift Ctrl<KeyPress>osfPageDown: page_right(\"extend\")\n\
Ctrl<KeyPress>osfPageDown: page_right()\n\
Shift<KeyPress>osfPageDown: next_page(\"extend\")\n\
<KeyPress>osfPageDown: next_page()\n\
Shift<KeyPress>osfSelect: key_select()\n\
<KeyPress>osfCancel: process_cancel()\n\
Ctrl~Alt~Meta<KeyPress>v: paste_clipboard()\n\
Ctrl~Alt~Meta<KeyPress>c: copy_clipboard()\n\
Ctrl~Alt~Meta<KeyPress>x: cut_clipboard()\n\
Ctrl~Alt~Meta<KeyPress>u: delete_to_start_of_line()\n\
Ctrl<KeyPress>Return: newline_and_indent()\n\
Shift<KeyPress>Return: newline_no_indent()\n\
<KeyPress>Return: newline()\n\
Ctrl<KeyPress>Tab: self_insert()\n\
<KeyPress>Tab: process_tab()\n\
Alt Shift Ctrl<KeyPress>space: key_select(\"rect\")\n\
Meta Shift Ctrl<KeyPress>space: key_select(\"rect\")\n\
Shift Ctrl~Meta~Alt<KeyPress>space: key_select()\n\
Ctrl~Meta~Alt<KeyPress>slash: select_all()\n\
Ctrl~Meta~Alt<KeyPress>backslash: deselect_all()\n\
<KeyPress>: self_insert()\n\
Alt Ctrl<Btn1Down>: move_destination()\n\
Meta Ctrl<Btn1Down>: move_destination()\n\
Shift Ctrl<Btn1Down>: extend_start(\"rect\")\n\
Shift<Btn1Down>: extend_start()\n\
<Btn1Down>: grab_focus()\n\
Button1 Ctrl<MotionNotify>: extend_adjust(\"rect\")\n\
Button1~Ctrl<MotionNotify>: extend_adjust()\n\
<Btn1Up>: extend_end()\n\
<Btn2Down>: secondary_or_drag_start()\n\
Shift Ctrl Button2<MotionNotify>: secondary_or_drag_adjust(\"rect\", \
\"copy\", \"overlay\")\n\
Shift Button2<MotionNotify>: secondary_or_drag_adjust(\"copy\")\n\
Ctrl Button2<MotionNotify>: secondary_or_drag_adjust(\"rect\", \
\"overlay\")\n\
Button2<MotionNotify>: secondary_or_drag_adjust()\n\
Shift Ctrl<Btn2Up>: move_to_or_end_drag(\"copy\", \"overlay\")\n\
Shift <Btn2Up>: move_to_or_end_drag(\"copy\")\n\
Alt<Btn2Up>: exchange()\n\
Meta<Btn2Up>: exchange()\n\
Ctrl<Btn2Up>: copy_to_or_end_drag(\"overlay\")\n\
<Btn2Up>: copy_to_or_end_drag()\n\
Ctrl~Meta~Alt<Btn3Down>: mouse_pan()\n\
Ctrl~Meta~Alt Button3<MotionNotify>: mouse_pan()\n\
<Btn3Up>: end_drag()\n\
<FocusIn>: focusIn()\n\
<FocusOut>: focusOut()\n";
/* some of the translations from the Motif text widget were not picked up:
:<KeyPress>osfSelect: set-anchor()\n\
:<KeyPress>osfActivate: activate()\n\
~Shift Ctrl~Meta~Alt<KeyPress>Return: activate()\n\
~Shift Ctrl~Meta~Alt<KeyPress>space: set-anchor()\n\
:<KeyPress>osfClear: clear-selection()\n\
~Shift~Ctrl~Meta~Alt<KeyPress>Return: process-return()\n\
Shift~Meta~Alt<KeyPress>Tab: prev-tab-group()\n\
Ctrl~Meta~Alt<KeyPress>Tab: next-tab-group()\n\
<UnmapNotify>: unmap()\n\
<EnterNotify>: enter()\n\
<LeaveNotify>: leave()\n
*/
static XtActionsRec actionsList[] = {
{"self-insert", selfInsertAP},
{"self_insert", selfInsertAP},
{"grab-focus", grabFocusAP},
{"grab_focus", grabFocusAP},
{"extend-adjust", extendAdjustAP},
{"extend_adjust", extendAdjustAP},
{"extend-start", extendStartAP},
{"extend_start", extendStartAP},
{"extend-end", extendEndAP},
{"extend_end", extendEndAP},
{"secondary-adjust", secondaryAdjustAP},
{"secondary_adjust", secondaryAdjustAP},
{"secondary-or-drag-adjust", secondaryOrDragAdjustAP},
{"secondary_or_drag_adjust", secondaryOrDragAdjustAP},
{"secondary-start", secondaryStartAP},
{"secondary_start", secondaryStartAP},
{"secondary-or-drag-start", secondaryOrDragStartAP},
{"secondary_or_drag_start", secondaryOrDragStartAP},
{"process-bdrag", secondaryOrDragStartAP},
{"process_bdrag", secondaryOrDragStartAP},
{"move-destination", moveDestinationAP},
{"move_destination", moveDestinationAP},
{"move-to", moveToAP},
{"move_to", moveToAP},
{"move-to-or-end-drag", moveToOrEndDragAP},
{"move_to_or_end_drag", moveToOrEndDragAP},
{"end_drag", endDragAP},
{"copy-to", copyToAP},
{"copy_to", copyToAP},
{"copy-to-or-end-drag", copyToOrEndDragAP},
{"copy_to_or_end_drag", copyToOrEndDragAP},
{"exchange", exchangeAP},
{"process-cancel", processCancelAP},
{"process_cancel", processCancelAP},
{"paste-clipboard", pasteClipboardAP},
{"paste_clipboard", pasteClipboardAP},
{"copy-clipboard", copyClipboardAP},
{"copy_clipboard", copyClipboardAP},
{"cut-clipboard", cutClipboardAP},
{"cut_clipboard", cutClipboardAP},
{"copy-primary", copyPrimaryAP},
{"copy_primary", copyPrimaryAP},
{"cut-primary", cutPrimaryAP},
{"cut_primary", cutPrimaryAP},
{"newline", newlineAP},
{"newline-and-indent", newlineAndIndentAP},
{"newline_and_indent", newlineAndIndentAP},
{"newline-no-indent", newlineNoIndentAP},
{"newline_no_indent", newlineNoIndentAP},
{"delete-selection", deleteSelectionAP},
{"delete_selection", deleteSelectionAP},
{"delete-previous-character", deletePreviousCharacterAP},
{"delete_previous_character", deletePreviousCharacterAP},
{"delete-next-character", deleteNextCharacterAP},
{"delete_next_character", deleteNextCharacterAP},
{"delete-previous-word", deletePreviousWordAP},
{"delete_previous_word", deletePreviousWordAP},
{"delete-next-word", deleteNextWordAP},
{"delete_next_word", deleteNextWordAP},
{"delete-to-start-of-line", deleteToStartOfLineAP},
{"delete_to_start_of_line", deleteToStartOfLineAP},
{"delete-to-end-of-line", deleteToEndOfLineAP},
{"delete_to_end_of_line", deleteToEndOfLineAP},
{"forward-character", forwardCharacterAP},
{"forward_character", forwardCharacterAP},
{"backward-character", backwardCharacterAP},
{"backward_character", backwardCharacterAP},
{"key-select", keySelectAP},
{"key_select", keySelectAP},
{"process-up", processUpAP},
{"process_up", processUpAP},
{"process-down", processDownAP},
{"process_down", processDownAP},
{"process-shift-up", processShiftUpAP},
{"process_shift_up", processShiftUpAP},
{"process-shift-down", processShiftDownAP},
{"process_shift_down", processShiftDownAP},
{"process-home", beginningOfLineAP},
{"process_home", beginningOfLineAP},
{"forward-word", forwardWordAP},
{"forward_word", forwardWordAP},
{"backward-word", backwardWordAP},
{"backward_word", backwardWordAP},
{"forward-paragraph", forwardParagraphAP},
{"forward_paragraph", forwardParagraphAP},
{"backward-paragraph", backwardParagraphAP},
{"backward_paragraph", backwardParagraphAP},
{"beginning-of-line", beginningOfLineAP},
{"beginning_of_line", beginningOfLineAP},
{"end-of-line", endOfLineAP},
{"end_of_line", endOfLineAP},
{"beginning-of-file", beginningOfFileAP},
{"beginning_of_file", beginningOfFileAP},
{"end-of-file", endOfFileAP},
{"end_of_file", endOfFileAP},
{"next-page", nextPageAP},
{"next_page", nextPageAP},
{"previous-page", previousPageAP},
{"previous_page", previousPageAP},
{"page-left", pageLeftAP},
{"page_left", pageLeftAP},
{"page-right", pageRightAP},
{"page_right", pageRightAP},
{"toggle-overstrike", toggleOverstrikeAP},
{"toggle_overstrike", toggleOverstrikeAP},
{"scroll-up", scrollUpAP},
{"scroll_up", scrollUpAP},
{"scroll-down", scrollDownAP},
{"scroll_down", scrollDownAP},
{"scroll-to-line", scrollToLineAP},
{"scroll_to_line", scrollToLineAP},
{"select-all", selectAllAP},
{"select_all", selectAllAP},
{"deselect-all", deselectAllAP},
{"deselect_all", deselectAllAP},
{"focusIn", focusInAP},
{"focusOut", focusOutAP},
{"process-return", selfInsertAP},
{"process_return", selfInsertAP},
{"process-tab", processTabAP},
{"process_tab", processTabAP},
{"insert-string", insertStringAP},
{"insert_string", insertStringAP},
{"mouse_pan", mousePanAP},
};
/* The motif text widget defined a bunch of actions which the nedit text
widget as-of-yet does not support:
Actions which were not bound to keys (for emacs emulation, some of
them should probably be supported:
kill-next-character()
kill-next-word()
kill-previous-character()
kill-previous-word()
kill-selection()
kill-to-end-of-line()
kill-to-start-of-line()
unkill()
next-line()
newline-and-backup()
beep()
redraw-display()
scroll-one-line-down()
scroll-one-line-up()
set-insertion-point()
Actions which are not particularly useful:
set-anchor()
activate()
clear-selection() -> this is a wierd one
do-quick-action() -> don't think this ever worked
Help()
next-tab-group()
select-adjust()
select-start()
select-end()
*/
static XtResource resources[] = {
{XmNhighlightThickness, XmCHighlightThickness, XmRDimension,
sizeof(Dimension), XtOffset(TextWidget, primitive.highlight_thickness),
XmRInt, 0},
{XmNshadowThickness, XmCShadowThickness, XmRDimension, sizeof(Dimension),
XtOffset(TextWidget, primitive.shadow_thickness), XmRInt, 0},
{textNfont, textCFont, XmRFontStruct, sizeof(XFontStruct *),
XtOffset(TextWidget, text.fontStruct), XmRString, "fixed"},
{textNselectForeground, textCSelectForeground, XmRPixel, sizeof(Pixel),
XtOffset(TextWidget, text.selectFGPixel), XmRString, "black"},
{textNselectBackground, textCSelectBackground, XmRPixel, sizeof(Pixel),
XtOffset(TextWidget, text.selectBGPixel), XmRString, "#cccccc"},
{textNhighlightForeground, textCHighlightForeground, XmRPixel,sizeof(Pixel),
XtOffset(TextWidget, text.highlightFGPixel), XmRString, "white"},
{textNhighlightBackground, textCHighlightBackground, XmRPixel,sizeof(Pixel),
XtOffset(TextWidget, text.highlightBGPixel), XmRString, "red"},
{textNcursorForeground, textCCursorForeground, XmRPixel,sizeof(Pixel),
XtOffset(TextWidget, text.cursorFGPixel), XmRString, "black"},
{textNrows, textCRows, XmRInt,sizeof(int),
XtOffset(TextWidget, text.rows), XmRString, "24"},
{textNcolumns, textCColumns, XmRInt, sizeof(int),
XtOffset(TextWidget, text.columns), XmRString, "80"},
{textNmarginWidth, textCMarginWidth, XmRInt, sizeof(int),
XtOffset(TextWidget, text.marginWidth), XmRString, "5"},
{textNmarginHeight, textCMarginHeight, XmRInt, sizeof(int),
XtOffset(TextWidget, text.marginHeight), XmRString, "5"},
{textNpendingDelete, textCPendingDelete, XmRBoolean, sizeof(Boolean),
XtOffset(TextWidget, text.pendingDelete), XmRString, "True"},
{textNautoWrap, textCAutoWrap, XmRBoolean, sizeof(Boolean),
XtOffset(TextWidget, text.autoWrap), XmRString, "True"},
{textNcontinuousWrap, textCContinuousWrap, XmRBoolean, sizeof(Boolean),
XtOffset(TextWidget, text.continuousWrap), XmRString, "True"},
{textNautoIndent, textCAutoIndent, XmRBoolean, sizeof(Boolean),
XtOffset(TextWidget, text.autoIndent), XmRString, "True"},
{textNsmartIndent, textCSmartIndent, XmRBoolean, sizeof(Boolean),
XtOffset(TextWidget, text.smartIndent), XmRString, "False"},
{textNoverstrike, textCOverstrike, XmRBoolean, sizeof(Boolean),
XtOffset(TextWidget, text.overstrike), XmRString, "False"},
{textNheavyCursor, textCHeavyCursor, XmRBoolean, sizeof(Boolean),
XtOffset(TextWidget, text.heavyCursor), XmRString, "False"},
{textNreadOnly, textCReadOnly, XmRBoolean, sizeof(Boolean),
XtOffset(TextWidget, text.readOnly), XmRString, "False"},
{textNwrapMargin, textCWrapMargin, XmRInt, sizeof(int),
XtOffset(TextWidget, text.wrapMargin), XmRString, "0"},
{textNhScrollBar, textCHScrollBar, XmRWidget, sizeof(Widget),
XtOffset(TextWidget, text.hScrollBar), XmRString, ""},
{textNvScrollBar, textCVScrollBar, XmRWidget, sizeof(Widget),
XtOffset(TextWidget, text.vScrollBar), XmRString, ""},
{textNautoShowInsertPos, textCAutoShowInsertPos, XmRBoolean,
sizeof(Boolean), XtOffset(TextWidget, text.autoShowInsertPos),
XmRString, "True"},
{textNautoWrapPastedText, textCAutoWrapPastedText, XmRBoolean,
sizeof(Boolean), XtOffset(TextWidget, text.autoWrapPastedText),
XmRString, "False"},
{textNwordDelimiters, textCWordDelimiters, XmRString, sizeof(char *),
XtOffset(TextWidget, text.delimiters), XmRString,
".,/\\`'!@#%^&*()-=+{}[]\":;<>?"},
{textNblinkRate, textCBlinkRate, XmRInt, sizeof(int),
XtOffset(TextWidget, text.cursorBlinkRate), XmRString, "500"},
{textNemulateTabs, textCEmulateTabs, XmRInt, sizeof(int),
XtOffset(TextWidget, text.emulateTabs), XmRString, "0"},
{textNfocusCallback, textCFocusCallback, XmRCallback, sizeof(caddr_t),
XtOffset(TextWidget, text.focusInCB), XtRCallback, NULL},
{textNlosingFocusCallback, textCLosingFocusCallback, XmRCallback,
sizeof(caddr_t), XtOffset(TextWidget, text.focusOutCB), XtRCallback,NULL},
{textNcursorMovementCallback, textCCursorMovementCallback, XmRCallback,
sizeof(caddr_t), XtOffset(TextWidget, text.cursorCB), XtRCallback, NULL},
{textNdragStartCallback, textCDragStartCallback, XmRCallback,
sizeof(caddr_t), XtOffset(TextWidget, text.dragStartCB), XtRCallback,
NULL},
{textNdragEndCallback, textCDragEndCallback, XmRCallback,
sizeof(caddr_t), XtOffset(TextWidget, text.dragEndCB), XtRCallback, NULL},
{textNsmartIndentCallback, textCSmartIndentCallback, XmRCallback,
sizeof(caddr_t), XtOffset(TextWidget, text.smartIndentCB), XtRCallback,
NULL},
};
TextClassRec textClassRec = {
/* CoreClassPart */
{
(WidgetClass) &xmPrimitiveClassRec, /* superclass */
"Text", /* class_name */
sizeof(TextRec), /* widget_size */
NULL, /* class_initialize */
NULL, /* class_part_initialize */
FALSE, /* class_inited */
(XtInitProc)initialize, /* initialize */
NULL, /* initialize_hook */
realize, /* realize */
actionsList, /* actions */
XtNumber(actionsList), /* num_actions */
resources, /* resources */
XtNumber(resources), /* num_resources */
NULLQUARK, /* xrm_class */
TRUE, /* compress_motion */
TRUE, /* compress_exposure */
TRUE, /* compress_enterleave */
FALSE, /* visible_interest */
(XtWidgetProc)destroy, /* destroy */
(XtWidgetProc)resize, /* resize */
(XtExposeProc)redisplay, /* expose */
(XtSetValuesFunc)setValues, /* set_values */
NULL, /* set_values_hook */
XtInheritSetValuesAlmost, /* set_values_almost */
NULL, /* get_values_hook */
NULL, /* accept_focus */
XtVersion, /* version */
NULL, /* callback private */
defaultTranslations, /* tm_table */
queryGeometry, /* query_geometry */
NULL, /* display_accelerator */
NULL, /* extension */
},
/* Motif primitive class fields */
{
(XtWidgetProc)_XtInherit, /* Primitive border_highlight */
(XtWidgetProc)_XtInherit, /* Primitive border_unhighlight */
NULL, /*XtInheritTranslations,*/ /* translations */
NULL, /* arm_and_activate */
NULL, /* get resources */
0, /* num get_resources */
NULL, /* extension */
},
/* Text class part */
{
0, /* ignored */
}
};
WidgetClass textWidgetClass = (WidgetClass)&textClassRec;
/*
** Widget initialize method
*/
static void initialize(TextWidget request, TextWidget new)
{
XFontStruct *fs = new->text.fontStruct;
char *delimiters;
textBuffer *buf;
Pixel white, black;
/* Set the initial window size based on the rows and columns resources */
if (request->core.width == 0)
new->core.width = fs->max_bounds.width * new->text.columns +
new->text.marginWidth * 2;
if (request->core.height == 0)
new->core.height = (fs->ascent + fs->descent) * new->text.rows +
new->text.marginHeight * 2;
/* The default colors work for B&W as well as color, except for
selectFGPixel and selectBGPixel, where color highlighting looks
much better without reverse video, so if we get here, and the
selection is totally unreadable because of the bad default colors,
swap the colors and make the selection reverse video */
white = WhitePixelOfScreen(XtScreen((Widget)new));
black = BlackPixelOfScreen(XtScreen((Widget)new));
if ( new->text.selectBGPixel == white &&
new->core.background_pixel == white &&
new->text.selectFGPixel == black &&
new->primitive.foreground == black) {
new->text.selectBGPixel = black;
new->text.selectFGPixel = white;
}
/* Create the initial text buffer for the widget to display (which can
be replaced later with TextSetBuffer) */
buf = BufCreate();
/* Create and initialize the text-display part of the widget */
new->text.textD = TextDCreate((Widget)new, new->text.hScrollBar,
new->text.vScrollBar, new->text.marginWidth, new->text.marginHeight,
new->core.width - new->text.marginWidth * 2,
new->core.height - new->text.marginHeight * 2,
buf, new->text.fontStruct, new->core.background_pixel,
new->primitive.foreground, new->text.selectFGPixel,
new->text.selectBGPixel, new->text.highlightFGPixel,
new->text.highlightBGPixel, new->text.cursorFGPixel,
new->text.continuousWrap, new->text.wrapMargin);
/* Add mandatory delimiters blank, tab, and newline to the list of
delimiters. The memory use scheme here is that new values are
always copied, and can therefore be safely freed on subsequent
set-values calls or destroy */
delimiters = XtMalloc(strlen(new->text.delimiters) + 4);
sprintf(delimiters, "%s%s", " \t\n", new->text.delimiters);
new->text.delimiters = delimiters;
/* Start with the cursor blanked (widgets don't have focus on creation,
the initial FocusIn event will unblank it and get blinking started) */
new->text.textD->cursorOn = False;
/* Initialize the widget variables */
new->text.autoScrollProcID = 0;
new->text.cursorBlinkProcID = 0;
new->text.dragState = NOT_CLICKED;
new->text.multiClickState = NO_CLICKS;
new->text.lastBtnDown = 0;
new->text.selectionOwner = False;
new->text.motifDestOwner = False;
new->text.emTabsBeforeCursor = 0;
#ifdef USE_XMIM
/* Register the widget to the input manager */
XmImRegister((Widget)new, 0);
/* In case some Resources for the IC need to be set, add them below */
XmImVaSetValues((Widget)new, NULL);
#endif
}
/*
** Widget destroy method
*/
static void destroy(TextWidget w)
{
textBuffer *buf;
/* Free the text display and possibly the attached buffer. The buffer
is freed only if after removing all of the modify procs (by calling
StopHandlingXSelections and TextDFree) there are no modify procs
left */
StopHandlingXSelections((Widget)w);
buf = w->text.textD->buffer;
TextDFree(w->text.textD);
if (buf->nModifyProcs == 0)
BufFree(buf);
if (w->text.cursorBlinkProcID != 0)
XtRemoveTimeOut(w->text.cursorBlinkProcID);
XtFree(w->text.delimiters);
XtRemoveAllCallbacks((Widget)w, textNfocusCallback);
XtRemoveAllCallbacks((Widget)w, textNlosingFocusCallback);
XtRemoveAllCallbacks((Widget)w, textNcursorMovementCallback);
XtRemoveAllCallbacks((Widget)w, textNdragStartCallback);
XtRemoveAllCallbacks((Widget)w, textNdragEndCallback);
#ifdef USE_XMIM
/* Unregister the widget from the input manager */
XmImUnregister((Widget)w);
#endif
}
/*
** Widget resize method. Called when the size of the widget changes
*/
static void resize(TextWidget w)
{
XFontStruct *fs = w->text.fontStruct;
int height = w->core.height, width = w->core.width;
int marginWidth = w->text.marginWidth, marginHeight = w->text.marginHeight;
w->text.columns = (width - marginWidth*2) / fs->max_bounds.width;
w->text.rows = (height - marginHeight*2) / (fs->ascent + fs->descent);
/* Reject widths and heights less than a character, which the text
display can't tolerate. This is not strictly legal, but I've seen
it done in other widgets and it seems to do no serious harm. NEdit
prevents panes from getting smaller than one line, but sometimes
splitting windows on Linux 2.0 systems (same Motif, why the change in
behavior?), causes one or two resize calls with < 1 line of height.
Fixing it here is 100x easier than re-designing textDisp.c */
if (w->text.columns < 1) {
w->text.columns = 1;
w->core.width = width = fs->max_bounds.width + marginWidth*2;
}
if (w->text.rows < 1) {
w->text.rows = 1;
w->core.height = height = fs->ascent + fs->descent + marginHeight*2;
}
/* Resize the text display that the widget uses to render text */
TextDResize(w->text.textD, width - marginWidth*2, height - marginHeight*2);
/* if the window became shorter or narrower, there may be text left
in the bottom or right margin area, which must be cleaned up */
if (XtIsRealized((Widget)w)) {
XClearArea(XtDisplay(w), XtWindow(w), 0, height-marginHeight,
width, marginHeight, False);
XClearArea(XtDisplay(w), XtWindow(w), width-marginWidth, 0,
marginWidth, height, False);
}
}
/*
** Widget redisplay method
*/
static void redisplay(TextWidget w, XEvent *event, Region region)
{
XExposeEvent *e = (XExposeEvent *)event;
TextDRedisplayRect(w->text.textD, e->x, e->y, e->width, e->height);
}
/*
** Widget setValues method
*/
static Boolean setValues(TextWidget current, TextWidget request,
TextWidget new)
{
Boolean redraw = False;
if (new->text.overstrike != current->text.overstrike) {
if (current->text.textD->cursorStyle == BLOCK_CURSOR)
TextDSetCursorStyle(current->text.textD,
current->text.heavyCursor ? HEAVY_CURSOR : NORMAL_CURSOR);
else if (current->text.textD->cursorStyle == NORMAL_CURSOR ||
current->text.textD->cursorStyle == HEAVY_CURSOR)
TextDSetCursorStyle(current->text.textD, BLOCK_CURSOR);
}
if (new->text.fontStruct != current->text.fontStruct)
TextDSetFont(current->text.textD, new->text.fontStruct);
if (new->text.wrapMargin != current->text.wrapMargin ||
new->text.continuousWrap != current->text.continuousWrap)
TextDSetWrapMode(current->text.textD, new->text.continuousWrap,
new->text.wrapMargin);
/* When delimiters are changed, copy the memory, so that the caller
doesn't have to manage it, and add mandatory delimiters blank,
tab, and newline to the list */
if (new->text.delimiters != current->text.delimiters) {
char *delimiters = XtMalloc(strlen(new->text.delimiters) + 4);
XtFree(current->text.delimiters);
sprintf(delimiters, "%s%s", " \t\n", new->text.delimiters);
new->text.delimiters = delimiters;
}
return redraw;
}
/*
** Widget realize method
*/
static void realize(Widget w, XtValueMask *valueMask,
XSetWindowAttributes *attributes)
{
/* Set bit gravity window attribute. This saves a full blank and redraw
on window resizing */
*valueMask |= CWBitGravity;
attributes->bit_gravity = NorthWestGravity;
/* Continue with realize method from superclass */
(xmPrimitiveClassRec.core_class.realize)(w, valueMask, attributes);
}
/*
** Widget query geometry method ... experimental, not fully tested, does
** nothing in NEdit since paned window ignores children's suggested geometry
*/
static XtGeometryResult queryGeometry(Widget w, XtWidgetGeometry *proposed,
XtWidgetGeometry *answer)
{
int curHeight = ((TextWidget)w)->core.height;
int curWidth = ((TextWidget)w)->core.width;
XFontStruct *fs = ((TextWidget)w)->text.textD->fontStruct;
int fontWidth = fs->max_bounds.width;
int fontHeight = fs->ascent + fs->descent;
int marginHeight = ((TextWidget)w)->text.marginHeight;
int propWidth = (proposed->request_mode & CWWidth) ? proposed->width : 0;
int propHeight = (proposed->request_mode & CWHeight) ? proposed->height : 0;
/* suggest a height that is an exact multiple of the line height
and at least one line high and 10 chars wide */
answer->request_mode = CWHeight | CWWidth;
answer->width = max(fontWidth * 10, propWidth);
answer->height = max(1, ((propHeight - 2*marginHeight) / fontHeight)) *
fontHeight + 2*marginHeight;
/* printf("propWidth %d, propHeight %d, ansWidth %d, ansHeight %d\n",
propWidth, propHeight, answer->width, answer->height); */
if (propWidth == answer->width && propHeight == answer->height)
return XtGeometryYes;
else if (answer->width == curWidth && answer->height == curHeight)
return XtGeometryNo;
else
return XtGeometryAlmost;
}
/*
** Set the text buffer which this widget will display and interact with.
** The currently attached buffer is automatically freed, ONLY if it has
** no additional modify procs attached (as it would if it were being
** displayed by another text widget).
*/
void TextSetBuffer(Widget w, textBuffer *buffer)
{
textBuffer *oldBuf = ((TextWidget)w)->text.textD->buffer;
StopHandlingXSelections(w);
TextDSetBuffer(((TextWidget)w)->text.textD, buffer);
if (oldBuf->nModifyProcs == 0)
BufFree(oldBuf);
}
/*
** Get the buffer associated with this text widget. Note that attaching
** additional modify callbacks to the buffer will prevent it from being
** automatically freed when the widget is destroyed.
*/
textBuffer *TextGetBuffer(Widget w)
{
return ((TextWidget)w)->text.textD->buffer;
}
/*
** Translate a position into a line number (if the position is visible,
** if it's not, return False
*/
int TextPosToLineAndCol(Widget w, int pos, int *lineNum, int *column)
{
return TextDPosToLineAndCol(((TextWidget)w)->text.textD, pos, lineNum,
column);
}
/*
** Translate a buffer text position to the XY location where the center
** of the cursor would be positioned to point to that character. Returns
** False if the position is not displayed because it is VERTICALLY out
** of view. If the position is horizontally out of view, returns the
** x coordinate where the position would be if it were visible.
*/
int TextPosToXY(Widget w, int pos, int *x, int *y)
{
return TextDPositionToXY(((TextWidget)w)->text.textD, pos, x, y);
}
/*
** Return the cursor position
*/
int TextGetCursorPos(Widget w)
{
return TextDGetInsertPosition(((TextWidget)w)->text.textD);
}
/*
** Set the cursor position
*/
void TextSetCursorPos(Widget w, int pos)
{
TextDSetInsertPosition(((TextWidget)w)->text.textD, pos);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, NULL);
}
/*
** Return the horizontal and vertical scroll positions of the widget
*/
void TextGetScroll(Widget w, int *topLineNum, int *horizOffset)
{
TextDGetScroll(((TextWidget)w)->text.textD, topLineNum, horizOffset);
}
/*
** Set the horizontal and vertical scroll positions of the widget
*/
void TextSetScroll(Widget w, int topLineNum, int horizOffset)
{
TextDSetScroll(((TextWidget)w)->text.textD, topLineNum, horizOffset);
}
/*
** Set this widget to be the owner of selections made in it's attached
** buffer (text buffers may be shared among several text widgets).
*/
void TextHandleXSelections(Widget w)
{
HandleXSelections(w);
}
void TextStopHandlingSelections(Widget w)
{
StopHandlingXSelections(w);
}
void TextPasteClipboard(Widget w, Time time)
{
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, time);
InsertClipboard(w, time, False);
callCursorMovementCBs(w, NULL);
}
void TextColPasteClipboard(Widget w, Time time)
{
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, time);
InsertClipboard(w, time, True);
callCursorMovementCBs(w, NULL);
}
void TextCopyClipboard(Widget w, Time time)
{
cancelDrag(w);
if (!((TextWidget)w)->text.textD->buffer->primary.selected) {
XBell(XtDisplay(w), 0);
return;
}
CopyToClipboard(w, time);
}
void TextCutClipboard(Widget w, Time time)
{
textDisp *textD = ((TextWidget)w)->text.textD;
cancelDrag(w);
if (checkReadOnly(w))
return;
if (!textD->buffer->primary.selected) {
XBell(XtDisplay(w), 0);
return;
}
TakeMotifDestination(w, time);
CopyToClipboard (w, time);
BufRemoveSelected(textD->buffer);
TextDSetInsertPosition(textD, textD->buffer->cursorPosHint);
checkAutoShowInsertPos(w);
}
int TextFirstVisiblePos(Widget w)
{
return ((TextWidget)w)->text.textD->firstChar;
}
int TextLastVisiblePos(Widget w)
{
return ((TextWidget)w)->text.textD->lastChar;
}
/*
** Insert text "chars" at the cursor position, respecting pending delete
** selections, overstrike, and handling cursor repositioning as if the text
** had been typed. If autoWrap is on wraps the text to fit within the wrap
** margin, auto-indenting where the line was wrapped (but nowhere else).
** "allowPendingDelete" controls whether primary selections in the widget are
** treated as pending delete selections (True), or ignored (False). "event"
** is optional and is just passed on to the cursor movement callbacks.
*/
void TextInsertAtCursor(Widget w, char *chars, XEvent *event,
int allowPendingDelete, int allowWrap)
{
int wrapMargin, colNum, lineStartPos, cursorPos;
char *c, *lineStartText, *wrappedText;
TextWidget tw = (TextWidget)w;
textDisp *textD = tw->text.textD;
textBuffer *buf = textD->buffer;
int fontWidth = textD->fontStruct->max_bounds.width;
int replaceSel, singleLine, breakAt = 0;
/* Don't wrap if auto-wrap is off or suppressed, or it's just a newline */
if (!allowWrap || !tw->text.autoWrap ||
(chars[0] == '\n' && chars[1] == '\0')) {
simpleInsertAtCursor(w, chars, event, allowPendingDelete);
return;
}
/* If this is going to be a pending delete operation, the real insert
position is the start of the selection. This will make rectangular
selections wrap strangely, but this routine should rarely be used for
them, and even more rarely when they need to be wrapped. */
replaceSel = allowPendingDelete && pendingSelection(w);
cursorPos = replaceSel ? buf->primary.start : TextDGetInsertPosition(textD);
/* If the text is only one line and doesn't need to be wrapped, just insert
it and be done (for efficiency only, this routine is called for each
character typed). (Of course, it may not be significantly more efficient
than the more general code below it, so it may be a waste of time!) */
wrapMargin = tw->text.wrapMargin != 0 ? tw->text.wrapMargin :
(tw->core.width - tw->text.marginWidth) / fontWidth;
lineStartPos = BufStartOfLine(buf, cursorPos);
colNum = BufCountDispChars(buf, lineStartPos, cursorPos);
for (c=chars; *c!='\0' && *c!='\n'; c++)
colNum += BufCharWidth(*c, colNum, buf->tabDist, buf->nullSubsChar);
singleLine = *c == '\0';
if (colNum < wrapMargin && singleLine) {
simpleInsertAtCursor(w, chars, event, True);
return;
}
/* Wrap the text */
lineStartText = BufGetRange(buf, lineStartPos, cursorPos);
wrappedText = wrapText(tw, lineStartText, chars, lineStartPos, wrapMargin,
replaceSel ? NULL : &breakAt);
XtFree(lineStartText);
/* Insert the text. Where possible, use TextDInsert which is optimized
for less redraw. */
if (replaceSel) {
BufReplaceSelected(buf, wrappedText);
TextDSetInsertPosition(textD, buf->cursorPosHint);
} else if (tw->text.overstrike) {
if (breakAt == 0 && singleLine)
TextDOverstrike(textD, wrappedText);
else {
BufReplace(buf, cursorPos-breakAt, cursorPos, wrappedText);
TextDSetInsertPosition(textD, buf->cursorPosHint);
}
} else {
if (breakAt == 0) {
TextDInsert(textD, wrappedText);
} else {
BufReplace(buf, cursorPos-breakAt, cursorPos, wrappedText);
TextDSetInsertPosition(textD, buf->cursorPosHint);
}
}
XtFree(wrappedText);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
/*
** Fetch text from the widget's buffer, adding wrapping newlines to emulate
** effect acheived by wrapping in the text display in continuous wrap mode.
*/
char *TextGetWrapped(Widget w, int startPos, int endPos, int *outLen)
{
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
textBuffer *outBuf;
int fromPos, toPos, outPos;
char c, *outString;
if (!((TextWidget)w)->text.continuousWrap || startPos == endPos) {
*outLen = endPos - startPos;
return BufGetRange(buf, startPos, endPos);
}
/* Create a text buffer with a good estimate of the size that adding
newlines will expand it to. Since it's a text buffer, if we guess
wrong, it will fail softly, and simply expand the size */
outBuf = BufCreatePreallocated((endPos-startPos) + (endPos-startPos)/5);
outPos = 0;
/* Go (displayed) line by line through the buffer, adding newlines where
the text is wrapped at some character other than an existing newline */
fromPos = startPos;
toPos = TextDCountForwardNLines(textD, startPos, 1, False);
while (toPos < endPos) {
BufCopyFromBuf(buf, outBuf, fromPos, toPos, outPos);
outPos += toPos - fromPos;
c = BufGetCharacter(outBuf, outPos-1);
if (c == ' ' || c == '\t')
BufReplace(outBuf, outPos-1, outPos, "\n");
else if (c != '\n') {
BufInsert(outBuf, outPos, "\n");
outPos++;
}
fromPos = toPos;
toPos = TextDCountForwardNLines(textD, fromPos, 1, True);
}
BufCopyFromBuf(buf, outBuf, fromPos, endPos, outPos);
/* return the contents of the output buffer as a string */
outString = BufGetAll(outBuf);
*outLen = outBuf->length;
BufFree(outBuf);
return outString;
}
/*
** Return the (statically allocated) action table for menu item actions.
**
** Warning: This routine can only be used before the first text widget is
** created! After that, apparently, Xt takes over the table and overwrites
** it with its own version. XtGetActionList is preferable, but is not
** available before X11R5.
*/
XtActionsRec *TextGetActions(int *nActions)
{
*nActions = XtNumber(actionsList);
return actionsList;
}
static void grabFocusAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
XButtonEvent *e = (XButtonEvent *)event;
TextWidget tw = (TextWidget)w;
textDisp *textD = tw->text.textD;
Time lastBtnDown = tw->text.lastBtnDown;
int row, column;
/* Indicate state for future events, PRIMARY_CLICKED indicates that
the proper initialization has been done for primary dragging and/or
multi-clicking. Also record the timestamp for multi-click processing */
tw->text.dragState = PRIMARY_CLICKED;
tw->text.lastBtnDown = e->time;
/* Become owner of the MOTIF_DESTINATION selection, making this widget
the designated recipient of secondary quick actions in Motif XmText
widgets and in other NEdit text widgets */
TakeMotifDestination(w, e->time);
/* Check for possible multi-click sequence in progress */
if (tw->text.multiClickState != NO_CLICKS) {
if (e->time < lastBtnDown + XtGetMultiClickTime(XtDisplay(w))) {
if (tw->text.multiClickState == ONE_CLICK) {
selectWord(w, e->x);
callCursorMovementCBs(w, event);
return;
} else if (tw->text.multiClickState == TWO_CLICKS) {
selectLine(w);
callCursorMovementCBs(w, event);
return;
} else if (tw->text.multiClickState == THREE_CLICKS) {
BufSelect(textD->buffer, 0, textD->buffer->length);
return;
} else if (tw->text.multiClickState > THREE_CLICKS)
tw->text.multiClickState = NO_CLICKS;
} else
tw->text.multiClickState = NO_CLICKS;
}
/* Clear any existing selections */
BufUnselect(textD->buffer);
/* Move the cursor to the pointer location */
moveDestinationAP(w, event, args, nArgs);
/* Record the site of the initial button press and the initial character
position so subsequent motion events and clicking can decide when and
where to begin a primary selection */
tw->text.btnDownX = e->x;
tw->text.btnDownY = e->y;
tw->text.anchor = TextDGetInsertPosition(textD);
TextDXYToUnconstrainedPosition(textD, e->x, e->y, &row, &column);
column = TextDOffsetWrappedColumn(textD, row, column);
tw->text.rectAnchor = column;
}
static void moveDestinationAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XButtonEvent *e = (XButtonEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
/* Get input focus */
XmProcessTraversal(w, XmTRAVERSE_CURRENT);
/* Move the cursor */
TextDSetInsertPosition(textD, TextDXYToPosition(textD, e->x, e->y));
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void extendAdjustAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
TextWidget tw = (TextWidget)w;
XMotionEvent *e = (XMotionEvent *)event;
int dragState = tw->text.dragState;
int rectDrag = hasKey("rect", args, nArgs);
/* Make sure the proper initialization was done on mouse down */
if (dragState != PRIMARY_DRAG && dragState != PRIMARY_CLICKED &&
dragState != PRIMARY_RECT_DRAG)
return;
/* If the selection hasn't begun, decide whether the mouse has moved
far enough from the initial mouse down to be considered a drag */
if (tw->text.dragState == PRIMARY_CLICKED) {
if (abs(e->x - tw->text.btnDownX) > SELECT_THRESHOLD ||
abs(e->y - tw->text.btnDownY) > SELECT_THRESHOLD)
tw->text.dragState = rectDrag ? PRIMARY_RECT_DRAG : PRIMARY_DRAG;
else
return;
}
/* If "rect" argument has appeared or disappeared, keep dragState up
to date about which type of drag this is */
tw->text.dragState = rectDrag ? PRIMARY_RECT_DRAG : PRIMARY_DRAG;
/* Record the new position for the autoscrolling timer routine, and
engage or disengage the timer if the mouse is in/out of the window */
checkAutoScroll(tw, e->x, e->y);
/* Adjust the selection and move the cursor */
adjustSelection(tw, e->x, e->y);
}
static void extendStartAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XMotionEvent *e = (XMotionEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
selection *sel = &buf->primary;
int anchor, rectAnchor, anchorLineStart, newPos, row, column;
/* Find the new anchor point for the rest of this drag operation */
newPos = TextDXYToPosition(textD, e->x, e->y);
TextDXYToUnconstrainedPosition(textD, e->x, e->y, &row, &column);
column = TextDOffsetWrappedColumn(textD, row, column);
if (sel->selected) {
if (sel->rectangular) {
rectAnchor = column < (sel->rectEnd + sel->rectStart) / 2 ?
sel->rectEnd : sel->rectStart;
anchorLineStart = BufStartOfLine(buf, newPos <
(sel->end + sel->start) / 2 ? sel->end : sel->start);
anchor = BufCountForwardDispChars(buf, anchorLineStart, rectAnchor);
} else {
if (abs(newPos - sel->start) < abs(newPos - sel->end))
anchor = sel->end;
else
anchor = sel->start;
anchorLineStart = BufStartOfLine(buf, anchor);
rectAnchor = BufCountDispChars(buf, anchorLineStart, anchor);
}
} else {
anchor = TextDGetInsertPosition(textD);
anchorLineStart = BufStartOfLine(buf, anchor);
rectAnchor = BufCountDispChars(buf, anchorLineStart, anchor);
}
((TextWidget)w)->text.anchor = anchor;
((TextWidget)w)->text.rectAnchor = rectAnchor;
/* Make the new selection */
if (hasKey("rect", args, nArgs))
BufRectSelect(buf, BufStartOfLine(buf, min(anchor, newPos)),
BufEndOfLine(buf, max(anchor, newPos)),
min(rectAnchor, column), max(rectAnchor, column));
else
BufSelect(buf, min(anchor, newPos), max(anchor, newPos));
/* Never mind the motion threshold, go right to dragging since
extend-start is unambiguously the start of a selection */
((TextWidget)w)->text.dragState = PRIMARY_DRAG;
/* Don't do by-word or by-line adjustment, just by character */
((TextWidget)w)->text.multiClickState = NO_CLICKS;
/* Move the cursor */
TextDSetInsertPosition(textD, newPos);
callCursorMovementCBs(w, event);
}
static void extendEndAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XButtonEvent *e = (XButtonEvent *)event;
TextWidget tw = (TextWidget)w;
if (tw->text.dragState == PRIMARY_CLICKED &&
tw->text.lastBtnDown <= e->time + XtGetMultiClickTime(XtDisplay(w)))
tw->text.multiClickState++;
endDrag(w);
}
static void processCancelAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
int dragState = ((TextWidget)w)->text.dragState;
textBuffer *buf = ((TextWidget)w)->text.textD->buffer;
if (dragState == PRIMARY_DRAG || dragState == PRIMARY_RECT_DRAG)
BufUnselect(buf);
cancelDrag(w);
}
static void secondaryStartAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XMotionEvent *e = (XMotionEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
selection *sel = &buf->secondary;
int anchor, pos, row, column;
/* Find the new anchor point and make the new selection */
pos = TextDXYToPosition(textD, e->x, e->y);
if (sel->selected) {
if (abs(pos - sel->start) < abs(pos - sel->end))
anchor = sel->end;
else
anchor = sel->start;
BufSecondarySelect(buf, anchor, pos);
} else
anchor = pos;
/* Record the site of the initial button press and the initial character
position so subsequent motion events can decide when to begin a
selection, (and where the selection began) */
((TextWidget)w)->text.btnDownX = e->x;
((TextWidget)w)->text.btnDownY = e->y;
((TextWidget)w)->text.anchor = pos;
TextDXYToUnconstrainedPosition(textD, e->x, e->y, &row, &column);
column = TextDOffsetWrappedColumn(textD, row, column);
((TextWidget)w)->text.rectAnchor = column;
((TextWidget)w)->text.dragState = SECONDARY_CLICKED;
}
static void secondaryOrDragStartAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XMotionEvent *e = (XMotionEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
/* If the click was outside of the primary selection, this is not
a drag, start a secondary selection */
if (!buf->primary.selected || !TextDInSelection(textD, e->x, e->y)) {
secondaryStartAP(w, event, args, nArgs);
return;
}
if (checkReadOnly(w))
return;
/* Record the site of the initial button press and the initial character
position so subsequent motion events can decide when to begin a
drag, and where to drag to */
((TextWidget)w)->text.btnDownX = e->x;
((TextWidget)w)->text.btnDownY = e->y;
((TextWidget)w)->text.dragState = CLICKED_IN_SELECTION;
}
static void secondaryAdjustAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
TextWidget tw = (TextWidget)w;
XMotionEvent *e = (XMotionEvent *)event;
int dragState = tw->text.dragState;
int rectDrag = hasKey("rect", args, nArgs);
/* Make sure the proper initialization was done on mouse down */
if (dragState != SECONDARY_DRAG && dragState != SECONDARY_RECT_DRAG &&
dragState != SECONDARY_CLICKED)
return;
/* If the selection hasn't begun, decide whether the mouse has moved
far enough from the initial mouse down to be considered a drag */
if (tw->text.dragState == SECONDARY_CLICKED) {
if (abs(e->x - tw->text.btnDownX) > SELECT_THRESHOLD ||
abs(e->y - tw->text.btnDownY) > SELECT_THRESHOLD)
tw->text.dragState = rectDrag ? SECONDARY_RECT_DRAG: SECONDARY_DRAG;
else
return;
}
/* If "rect" argument has appeared or disappeared, keep dragState up
to date about which type of drag this is */
tw->text.dragState = rectDrag ? SECONDARY_RECT_DRAG : SECONDARY_DRAG;
/* Record the new position for the autoscrolling timer routine, and
engage or disengage the timer if the mouse is in/out of the window */
checkAutoScroll(tw, e->x, e->y);
/* Adjust the selection */
adjustSecondarySelection(tw, e->x, e->y);
}
static void secondaryOrDragAdjustAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
TextWidget tw = (TextWidget)w;
XMotionEvent *e = (XMotionEvent *)event;
int dragState = tw->text.dragState;
/* Only dragging of blocks of text is handled in this action proc.
Otherwise, defer to secondaryAdjust to handle the rest */
if (dragState != CLICKED_IN_SELECTION && dragState != PRIMARY_BLOCK_DRAG) {
secondaryAdjustAP(w, event, args, nArgs);
return;
}
/* Decide whether the mouse has moved far enough from the
initial mouse down to be considered a drag */
if (tw->text.dragState == CLICKED_IN_SELECTION) {
if (abs(e->x - tw->text.btnDownX) > SELECT_THRESHOLD ||
abs(e->y - tw->text.btnDownY) > SELECT_THRESHOLD)
BeginBlockDrag(tw);
else
return;
}
/* Record the new position for the autoscrolling timer routine, and
engage or disengage the timer if the mouse is in/out of the window */
checkAutoScroll(tw, e->x, e->y);
/* Adjust the selection */
BlockDragSelection(tw, e->x, e->y, hasKey("overlay", args, nArgs) ?
(hasKey("copy", args, nArgs) ? DRAG_OVERLAY_COPY : DRAG_OVERLAY_MOVE) :
(hasKey("copy", args, nArgs) ? DRAG_COPY : DRAG_MOVE));
}
static void copyToAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
XButtonEvent *e = (XButtonEvent *)event;
TextWidget tw = (TextWidget)w;
textDisp *textD = tw->text.textD;
int dragState = tw->text.dragState;
textBuffer *buf = textD->buffer;
selection *secondary = &buf->secondary, *primary = &buf->primary;
int rectangular = secondary->rectangular;
char *textToCopy;
int insertPos, lineStart, column;
endDrag(w);
if (!((dragState == SECONDARY_DRAG && secondary->selected) ||
(dragState == SECONDARY_RECT_DRAG && secondary->selected) ||
dragState == SECONDARY_CLICKED || dragState == NOT_CLICKED))
return;
if (!(secondary->selected && !((TextWidget)w)->text.motifDestOwner)) {
if (checkReadOnly(w)) {
BufSecondaryUnselect(buf);
return;
}
}
if (secondary->selected) {
if (tw->text.motifDestOwner) {
TextDBlankCursor(textD);
textToCopy = BufGetSecSelectText(buf);
if (primary->selected && rectangular) {
insertPos = TextDGetInsertPosition(textD);
BufReplaceSelected(buf, textToCopy);
TextDSetInsertPosition(textD, buf->cursorPosHint);
} else if (rectangular) {
insertPos = TextDGetInsertPosition(textD);
lineStart = BufStartOfLine(buf, insertPos);
column = BufCountDispChars(buf, lineStart, insertPos);
BufInsertCol(buf, column, lineStart, textToCopy, NULL, NULL);
TextDSetInsertPosition(textD, buf->cursorPosHint);
} else
TextInsertAtCursor(w, textToCopy, event, True,
tw->text.autoWrapPastedText);
XtFree(textToCopy);
BufSecondaryUnselect(buf);
TextDUnblankCursor(textD);
} else
SendSecondarySelection(w, e->time, False);
} else if (primary->selected) {
textToCopy = BufGetSelectionText(buf);
TextDSetInsertPosition(textD, TextDXYToPosition(textD, e->x, e->y));
TextInsertAtCursor(w, textToCopy, event, False,
tw->text.autoWrapPastedText);
XtFree(textToCopy);
} else {
TextDSetInsertPosition(textD, TextDXYToPosition(textD, e->x, e->y));
InsertPrimarySelection(w, e->time, False);
}
}
static void copyToOrEndDragAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
int dragState = ((TextWidget)w)->text.dragState;
if (dragState != PRIMARY_BLOCK_DRAG) {
copyToAP(w, event, args, nArgs);
return;
}
FinishBlockDrag((TextWidget)w);
}
static void moveToAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
XButtonEvent *e = (XButtonEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
int dragState = ((TextWidget)w)->text.dragState;
textBuffer *buf = textD->buffer;
selection *secondary = &buf->secondary, *primary = &buf->primary;
int insertPos, rectangular = secondary->rectangular;
int column, lineStart;
char *textToCopy;
endDrag(w);
if (!((dragState == SECONDARY_DRAG && secondary->selected) ||
(dragState == SECONDARY_RECT_DRAG && secondary->selected) ||
dragState == SECONDARY_CLICKED || dragState == NOT_CLICKED))
return;
if (checkReadOnly(w)) {
BufSecondaryUnselect(buf);
return;
}
if (secondary->selected) {
if (((TextWidget)w)->text.motifDestOwner) {
textToCopy = BufGetSecSelectText(buf);
if (primary->selected && rectangular) {
insertPos = TextDGetInsertPosition(textD);
BufReplaceSelected(buf, textToCopy);
TextDSetInsertPosition(textD, buf->cursorPosHint);
} else if (rectangular) {
insertPos = TextDGetInsertPosition(textD);
lineStart = BufStartOfLine(buf, insertPos);
column = BufCountDispChars(buf, lineStart, insertPos);
BufInsertCol(buf, column, lineStart, textToCopy, NULL, NULL);
TextDSetInsertPosition(textD, buf->cursorPosHint);
} else
TextInsertAtCursor(w, textToCopy, event, True,
((TextWidget)w)->text.autoWrapPastedText);
XtFree(textToCopy);
BufRemoveSecSelect(buf);
BufSecondaryUnselect(buf);
} else
SendSecondarySelection(w, e->time, True);
} else if (primary->selected) {
textToCopy = BufGetRange(buf, primary->start, primary->end);
TextDSetInsertPosition(textD, TextDXYToPosition(textD, e->x, e->y));
TextInsertAtCursor(w, textToCopy, event, False,
((TextWidget)w)->text.autoWrapPastedText);
XtFree(textToCopy);
BufRemoveSelected(buf);
BufUnselect(buf);
} else {
TextDSetInsertPosition(textD, TextDXYToPosition(textD, e->x, e->y));
MovePrimarySelection(w, e->time, False);
}
}
static void moveToOrEndDragAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
int dragState = ((TextWidget)w)->text.dragState;
if (dragState != PRIMARY_BLOCK_DRAG) {
moveToAP(w, event, args, nArgs);
return;
}
FinishBlockDrag((TextWidget)w);
}
static void endDragAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
if (((TextWidget)w)->text.dragState == PRIMARY_BLOCK_DRAG)
FinishBlockDrag((TextWidget)w);
else
endDrag(w);
}
static void exchangeAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
XButtonEvent *e = (XButtonEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
selection *sec = &buf->secondary, *primary = &buf->primary;
char *primaryText, *secText;
int newPrimaryStart, newPrimaryEnd, secWasRect;
endDrag(w);
if (checkReadOnly(w))
return;
/* If there's no secondary selection here, or the primary and secondary
selection overlap, just beep and return */
if (!sec->selected || (primary->selected &&
((primary->start <= sec->start && primary->end > sec->start) ||
(sec->start <= primary->start && sec->end > primary->start)))) {
BufSecondaryUnselect(buf);
XBell(XtDisplay(w), 0);
return;
}
/* if the primary selection is in another widget, use selection routines */
if (!primary->selected) {
ExchangeSelections(w, e->time);
return;
}
/* Both primary and secondary are in this widget, do the exchange here */
primaryText = BufGetSelectionText(buf);
secText = BufGetSecSelectText(buf);
secWasRect = sec->rectangular;
BufReplaceSecSelect(buf, primaryText);
newPrimaryStart = primary->start;
BufReplaceSelected(buf, secText);
newPrimaryEnd = newPrimaryStart + strlen(secText);
XtFree(primaryText);
XtFree(secText);
BufSecondaryUnselect(buf);
if (secWasRect) {
TextDSetInsertPosition(textD, buf->cursorPosHint);
} else {
BufSelect(buf, newPrimaryStart, newPrimaryEnd);
TextDSetInsertPosition(textD, newPrimaryEnd);
}
checkAutoShowInsertPos(w);
}
static void copyPrimaryAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
TextWidget tw = (TextWidget)w;
textDisp *textD = tw->text.textD;
textBuffer *buf = textD->buffer;
selection *primary = &buf->primary;
int rectangular = hasKey("rect", args, nArgs);
char *textToCopy;
int insertPos, col;
cancelDrag(w);
if (checkReadOnly(w))
return;
if (primary->selected && rectangular) {
textToCopy = BufGetSelectionText(buf);
insertPos = TextDGetInsertPosition(textD);
col = BufCountDispChars(buf, BufStartOfLine(buf, insertPos), insertPos);
BufInsertCol(buf, col, insertPos, textToCopy, NULL, NULL);
TextDSetInsertPosition(textD, buf->cursorPosHint);
XtFree(textToCopy);
checkAutoShowInsertPos(w);
} else if (primary->selected) {
textToCopy = BufGetSelectionText(buf);
insertPos = TextDGetInsertPosition(textD);
BufInsert(buf, insertPos, textToCopy);
TextDSetInsertPosition(textD, insertPos + strlen(textToCopy));
XtFree(textToCopy);
checkAutoShowInsertPos(w);
} else if (rectangular) {
if (!TextDPositionToXY(textD, TextDGetInsertPosition(textD),
&tw->text.btnDownX, &tw->text.btnDownY))
return; /* shouldn't happen */
InsertPrimarySelection(w, e->time, True);
} else
InsertPrimarySelection(w, e->time, False);
}
static void cutPrimaryAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
selection *primary = &buf->primary;
char *textToCopy;
int rectangular = hasKey("rect", args, nArgs);
int insertPos, col;
cancelDrag(w);
if (checkReadOnly(w))
return;
if (primary->selected && rectangular) {
textToCopy = BufGetSelectionText(buf);
insertPos = TextDGetInsertPosition(textD);
col = BufCountDispChars(buf, BufStartOfLine(buf, insertPos), insertPos);
BufInsertCol(buf, col, insertPos, textToCopy, NULL, NULL);
TextDSetInsertPosition(textD, buf->cursorPosHint);
XtFree(textToCopy);
BufRemoveSelected(buf);
checkAutoShowInsertPos(w);
} else if (primary->selected) {
textToCopy = BufGetSelectionText(buf);
insertPos = TextDGetInsertPosition(textD);
BufInsert(buf, insertPos, textToCopy);
TextDSetInsertPosition(textD, insertPos + strlen(textToCopy));
XtFree(textToCopy);
BufRemoveSelected(buf);
checkAutoShowInsertPos(w);
} else if (rectangular) {
if (!TextDPositionToXY(textD, TextDGetInsertPosition(textD),
&((TextWidget)w)->text.btnDownX,
&((TextWidget)w)->text.btnDownY))
return; /* shouldn't happen */
MovePrimarySelection(w, e->time, True);
} else {
MovePrimarySelection(w, e->time, False);
}
}
static void mousePanAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
XButtonEvent *e = (XButtonEvent *)event;
TextWidget tw = (TextWidget)w;
textDisp *textD = tw->text.textD;
int lineHeight = textD->ascent + textD->descent;
int topLineNum, horizOffset;
static Cursor panCursor = 0;
if (tw->text.dragState == MOUSE_PAN) {
TextDSetScroll(textD,
(tw->text.btnDownY - e->y + lineHeight/2) / lineHeight,
tw->text.btnDownX - e->x);
} else if (tw->text.dragState == NOT_CLICKED) {
TextDGetScroll(textD, &topLineNum, &horizOffset);
tw->text.btnDownX = e->x + horizOffset;
tw->text.btnDownY = e->y + topLineNum * lineHeight;
tw->text.dragState = MOUSE_PAN;
if (!panCursor)
panCursor = XCreateFontCursor(XtDisplay(w), XC_fleur);
XGrabPointer(XtDisplay(w), XtWindow(w), False,
ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
GrabModeAsync, None, panCursor, CurrentTime);
} else
cancelDrag(w);
}
static void pasteClipboardAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
if (hasKey("rect", args, nArgs))
TextColPasteClipboard(w, ((XKeyEvent *)event)->time);
else
TextPasteClipboard(w, ((XKeyEvent *)event)->time);
}
static void copyClipboardAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
TextCopyClipboard(w, ((XKeyEvent *)event)->time);
}
static void cutClipboardAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
TextCutClipboard(w, ((XKeyEvent *)event)->time);
}
static void insertStringAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
smartIndentCBStruct smartIndent;
textDisp *textD = ((TextWidget)w)->text.textD;
if (*nArgs == 0)
return;
cancelDrag(w);
if (checkReadOnly(w))
return;
if (((TextWidget)w)->text.smartIndent) {
smartIndent.reason = CHAR_TYPED;
smartIndent.pos = TextDGetInsertPosition(textD);
smartIndent.indentRequest = 0;
smartIndent.charsTyped = args[0];
XtCallCallbacks(w, textNsmartIndentCallback, (XtPointer)&smartIndent);
}
TextInsertAtCursor(w, args[0], event, True, True);
BufUnselect((((TextWidget)w)->text.textD)->buffer);
}
static void selfInsertAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
#ifdef USE_XMIM
Status status;
#endif
XKeyEvent *e = (XKeyEvent *)event;
char chars[20];
KeySym keysym;
int nChars;
static XComposeStatus compose = {NULL, 0};
smartIndentCBStruct smartIndent;
textDisp *textD = ((TextWidget)w)->text.textD;
#ifdef USE_XMIM
nChars = XmImMbLookupString(w, (XKeyEvent *)event, chars, 19, &keysym,
&status);
if (nChars == 0 || status == XLookupNone ||
status == XLookupKeySym || status == XBufferOverflow)
return;
#else
nChars = XLookupString((XKeyEvent *)event, chars, 19, &keysym, &compose);
if (nChars == 0)
return;
#endif
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, e->time);
chars[nChars] = '\0';
/* If smart indent is on, call the smart indent callback to check the
inserted character */
if (((TextWidget)w)->text.smartIndent) {
smartIndent.reason = CHAR_TYPED;
smartIndent.pos = TextDGetInsertPosition(textD);
smartIndent.indentRequest = 0;
smartIndent.charsTyped = chars;
XtCallCallbacks(w, textNsmartIndentCallback, (XtPointer)&smartIndent);
}
TextInsertAtCursor(w, chars, event, True, True);
BufUnselect(textD->buffer);
}
static void newlineAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
if (((TextWidget)w)->text.autoIndent || ((TextWidget)w)->text.smartIndent)
newlineAndIndentAP(w, event, args, nArgs);
else
newlineNoIndentAP(w, event, args, nArgs);
}
static void newlineNoIndentAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, e->time);
simpleInsertAtCursor(w, "\n", event, True);
BufUnselect((((TextWidget)w)->text.textD)->buffer);
}
static void newlineAndIndentAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
TextWidget tw = (TextWidget)w;
textDisp *textD = tw->text.textD;
textBuffer *buf = textD->buffer;
char *indentStr;
int cursorPos, lineStartPos, column;
if (checkReadOnly(w))
return;
cancelDrag(w);
TakeMotifDestination(w, e->time);
/* Create a string containing a newline followed by auto or smart
indent string */
cursorPos = TextDGetInsertPosition(textD);
lineStartPos = BufStartOfLine(buf, cursorPos);
indentStr = createIndentString(tw, buf, 0, lineStartPos,
cursorPos, NULL, &column);
/* Insert it at the cursor */
simpleInsertAtCursor(w, indentStr, event, True);
XtFree(indentStr);
/* If emulated tabs are on, make the inserted indent deletable by tab */
if (tw->text.emulateTabs)
tw->text.emTabsBeforeCursor = column / tw->text.emulateTabs;
BufUnselect(buf);
}
static void processTabAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
selection *sel = &buf->primary;
int emTabDist = ((TextWidget)w)->text.emulateTabs;
int emTabsBeforeCursor = ((TextWidget)w)->text.emTabsBeforeCursor;
int insertPos, indent, startIndent, toIndent, lineStart, tabWidth;
char *outStr, *outPtr;
if (checkReadOnly(w))
return;
cancelDrag(w);
TakeMotifDestination(w, ((XKeyEvent *)event)->time);
/* If emulated tabs are off, just insert a tab */
if (emTabDist <= 0) {
TextInsertAtCursor(w, "\t", event, True, True);
return;
}
/* Find the starting and ending indentation. If the tab is to
replace an existing selection, use the start of the selection
instead of the cursor position as the indent. When replacing
rectangular selections, tabs are automatically recalculated as
if the inserted text began at the start of the line */
insertPos = pendingSelection(w) ?
sel->start : TextDGetInsertPosition(textD);
lineStart = BufStartOfLine(buf, insertPos);
if (pendingSelection(w) && sel->rectangular)
insertPos = BufCountForwardDispChars(buf, lineStart, sel->rectStart);
startIndent = BufCountDispChars(buf, lineStart, insertPos);
toIndent = startIndent + emTabDist - (startIndent % emTabDist);
if (pendingSelection(w) && sel->rectangular) {
toIndent -= startIndent;
startIndent = 0;
}
/* Allocate a buffer assuming all the inserted characters will be spaces */
outStr = XtMalloc(toIndent - startIndent + 1);
/* Add spaces and tabs to outStr until it reaches toIndent */
outPtr = outStr;
indent = startIndent;
while (indent < toIndent) {
tabWidth = BufCharWidth('\t', indent, buf->tabDist, buf->nullSubsChar);
if (buf->useTabs && tabWidth > 1 && indent + tabWidth <= toIndent) {
*outPtr++ = '\t';
indent += tabWidth;
} else {
*outPtr++ = ' ';
indent++;
}
}
*outPtr = '\0';
/* Insert the emulated tab */
TextInsertAtCursor(w, outStr, event, True, True);
XtFree(outStr);
/* Restore and ++ emTabsBeforeCursor cleared by TextInsertAtCursor */
((TextWidget)w)->text.emTabsBeforeCursor = emTabsBeforeCursor + 1;
BufUnselect(buf);
}
static void deleteSelectionAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, e->time);
deletePendingSelection(w, event);
}
static void deletePreviousCharacterAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
char c;
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, e->time);
if (deletePendingSelection(w, event))
return;
if (insertPos == 0) {
XBell(XtDisplay(w), 0);
return;
}
if (deleteEmulatedTab(w, event))
return;
if (((TextWidget)w)->text.overstrike) {
c = BufGetCharacter(textD->buffer, insertPos - 1);
if (c == '\n')
BufRemove(textD->buffer, insertPos - 1, insertPos);
else if (c != '\t')
BufReplace(textD->buffer, insertPos - 1, insertPos, " ");
} else
BufRemove(textD->buffer, insertPos - 1, insertPos);
TextDSetInsertPosition(textD, insertPos - 1);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void deleteNextCharacterAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, e->time);
if (deletePendingSelection(w, event))
return;
if (insertPos == textD->buffer->length) {
XBell(XtDisplay(w), 0);
return;
}
BufRemove(textD->buffer, insertPos , insertPos + 1);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void deletePreviousWordAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
int pos, lineStart = BufStartOfLine(textD->buffer, insertPos);
char *delimiters = ((TextWidget)w)->text.delimiters;
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, e->time);
if (deletePendingSelection(w, event))
return;
if (insertPos == lineStart) {
XBell(XtDisplay(w), 0);
return;
}
pos = max(insertPos - 1, 0);
while (strchr(delimiters, BufGetCharacter(textD->buffer, pos)) != NULL &&
pos != lineStart)
pos--;
pos = startOfWord((TextWidget)w, pos);
BufRemove(textD->buffer, pos, insertPos);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void deleteNextWordAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
int pos, lineEnd = BufEndOfLine(textD->buffer, insertPos);
char *delimiters = ((TextWidget)w)->text.delimiters;
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, e->time);
if (deletePendingSelection(w, event))
return;
if (insertPos == lineEnd) {
XBell(XtDisplay(w), 0);
return;
}
pos = insertPos;
while (strchr(delimiters, BufGetCharacter(textD->buffer, pos)) != NULL &&
pos != lineEnd)
pos++;
pos = endOfWord((TextWidget)w, pos);
BufRemove(textD->buffer, insertPos, pos);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void deleteToEndOfLineAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
int endOfLine = TextDEndOfLine(textD, insertPos, False);
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, e->time);
if (deletePendingSelection(w, event))
return;
if (insertPos == endOfLine) {
XBell(XtDisplay(w), 0);
return;
}
BufRemove(textD->buffer, insertPos, endOfLine);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void deleteToStartOfLineAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
XKeyEvent *e = (XKeyEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
int startOfLine = BufStartOfLine(textD->buffer, insertPos);
cancelDrag(w);
if (checkReadOnly(w))
return;
TakeMotifDestination(w, e->time);
if (deletePendingSelection(w, event))
return;
if (insertPos == startOfLine) {
XBell(XtDisplay(w), 0);
return;
}
BufRemove(textD->buffer, startOfLine, insertPos);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void forwardCharacterAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
cancelDrag(w);
if (!TextDMoveRight(((TextWidget)w)->text.textD))
XBell(XtDisplay(w), 0);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void backwardCharacterAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
cancelDrag(w);
if (!TextDMoveLeft(((TextWidget)w)->text.textD))
XBell(XtDisplay(w), 0);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void forwardWordAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
int pos, insertPos = TextDGetInsertPosition(textD);
char *delimiters = ((TextWidget)w)->text.delimiters;
cancelDrag(w);
if (insertPos == buf->length) {
XBell(XtDisplay(w), 0);
return;
}
pos = insertPos;
if (strchr(delimiters, BufGetCharacter(buf, pos)) == NULL)
pos = endOfWord((TextWidget)w, pos);
for (; pos<buf->length; pos++) {
if (strchr(delimiters, BufGetCharacter(buf, pos)) == NULL)
break;
}
TextDSetInsertPosition(textD, pos);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void backwardWordAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
int pos, insertPos = TextDGetInsertPosition(textD);
char *delimiters = ((TextWidget)w)->text.delimiters;
cancelDrag(w);
if (insertPos == 0) {
XBell(XtDisplay(w), 0);
return;
}
pos = max(insertPos - 1, 0);
while (strchr(delimiters, BufGetCharacter(buf, pos)) != NULL && pos > 0)
pos--;
pos = startOfWord((TextWidget)w, pos);
TextDSetInsertPosition(textD, pos);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void forwardParagraphAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int pos, insertPos = TextDGetInsertPosition(textD);
textBuffer *buf = textD->buffer;
char c;
static char whiteChars[] = " \t";
cancelDrag(w);
if (insertPos == buf->length) {
XBell(XtDisplay(w), 0);
return;
}
pos = min(BufEndOfLine(buf, insertPos)+1, buf->length);
while (pos < buf->length) {
c = BufGetCharacter(buf, pos);
if (c == '\n')
break;
if (strchr(whiteChars, c) != NULL)
pos++;
else
pos = min(BufEndOfLine(buf, pos)+1, buf->length);
}
TextDSetInsertPosition(textD, min(pos+1, buf->length));
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void backwardParagraphAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int parStart, pos, insertPos = TextDGetInsertPosition(textD);
textBuffer *buf = textD->buffer;
char c;
static char whiteChars[] = " \t";
cancelDrag(w);
if (insertPos == 0) {
XBell(XtDisplay(w), 0);
return;
}
parStart = BufStartOfLine(buf, max(insertPos-1, 0));
pos = max(parStart - 2, 0);
while (pos > 0) {
c = BufGetCharacter(buf, pos);
if (c == '\n')
break;
if (strchr(whiteChars, c) != NULL)
pos--;
else {
parStart = BufStartOfLine(buf, pos);
pos = max(parStart - 2, 0);
}
}
TextDSetInsertPosition(textD, parStart);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void keySelectAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int stat, insertPos = TextDGetInsertPosition(textD);
cancelDrag(w);
if (hasKey("left", args, nArgs)) stat = TextDMoveLeft(textD);
else if (hasKey("right", args, nArgs)) stat = TextDMoveRight(textD);
else if (hasKey("up", args, nArgs)) stat = TextDMoveUp(textD);
else if (hasKey("down", args, nArgs)) stat = TextDMoveDown(textD);
else {
keyMoveExtendSelection(w, event, insertPos, hasKey("rect", args,nArgs));
return;
}
if (!stat)
XBell(XtDisplay(w), 0);
else {
keyMoveExtendSelection(w, event, insertPos, hasKey("rect", args,nArgs));
checkAutoShowInsertPos(w);
}
}
static void processUpAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
cancelDrag(w);
if (!TextDMoveUp(((TextWidget)w)->text.textD))
XBell(XtDisplay(w), 0);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void processShiftUpAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
cancelDrag(w);
if (!TextDMoveUp(((TextWidget)w)->text.textD))
XBell(XtDisplay(w), 0);
keyMoveExtendSelection(w, event, insertPos, hasKey("rect", args, nArgs));
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void processDownAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
cancelDrag(w);
if (!TextDMoveDown(((TextWidget)w)->text.textD))
XBell(XtDisplay(w), 0);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void processShiftDownAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
cancelDrag(w);
if (!TextDMoveDown(((TextWidget)w)->text.textD))
XBell(XtDisplay(w), 0);
keyMoveExtendSelection(w, event, insertPos, hasKey("rect", args, nArgs));
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void beginningOfLineAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
cancelDrag(w);
TextDSetInsertPosition(textD, TextDStartOfLine(textD, insertPos));
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void endOfLineAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
cancelDrag(w);
TextDSetInsertPosition(textD, TextDEndOfLine(textD, insertPos, False));
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void beginningOfFileAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
cancelDrag(w);
TextDSetInsertPosition(((TextWidget)w)->text.textD, 0);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void endOfFileAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
cancelDrag(w);
TextDSetInsertPosition(textD, textD->buffer->length);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void nextPageAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
int lastTopLine = textD->nBufferLines - (textD->nVisibleLines - 2);
int insertPos = TextDGetInsertPosition(textD);
int pos, targetLine;
cancelDrag(w);
if (insertPos >= buf->length && textD->topLineNum == lastTopLine) {
XBell(XtDisplay(w), 0);
return;
}
targetLine = textD->topLineNum + textD->nVisibleLines - 1;
if (targetLine < 1) targetLine = 1;
if (targetLine > lastTopLine) targetLine = lastTopLine;
pos = TextDCountForwardNLines(textD, insertPos, textD->nVisibleLines-1,
False);
TextDSetInsertPosition(textD, pos);
TextDSetScroll(textD, targetLine, textD->horizOffset);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void previousPageAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
int pos, targetLine;
cancelDrag(w);
if (insertPos <= 0 && textD->topLineNum == 1) {
XBell(XtDisplay(w), 0);
return;
}
targetLine = textD->topLineNum - (textD->nVisibleLines - 1);
if (targetLine < 1)
targetLine = 1;
pos = TextDCountBackwardNLines(textD, insertPos, textD->nVisibleLines-1);
TextDSetInsertPosition(textD, pos);
TextDSetScroll(textD, targetLine, textD->horizOffset);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void pageLeftAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
int insertPos = TextDGetInsertPosition(textD);
int maxCharWidth = textD->fontStruct->max_bounds.width;
int lineStartPos, indent, pos;
cancelDrag(w);
lineStartPos = BufStartOfLine(buf, insertPos);
if (insertPos == lineStartPos && textD->horizOffset == 0) {
XBell(XtDisplay(w), 0);
return;
}
indent = BufCountDispChars(buf, lineStartPos, insertPos);
pos = BufCountForwardDispChars(buf, lineStartPos,
max(0, indent - textD->width / maxCharWidth));
TextDSetInsertPosition(textD, pos);
TextDSetScroll(textD, textD->topLineNum,
max(0, textD->horizOffset - textD->width));
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void pageRightAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
int insertPos = TextDGetInsertPosition(textD);
int maxCharWidth = textD->fontStruct->max_bounds.width;
int oldHorizOffset = textD->horizOffset;
int lineStartPos, indent, pos;
cancelDrag(w);
lineStartPos = BufStartOfLine(buf, insertPos);
indent = BufCountDispChars(buf, lineStartPos, insertPos);
pos = BufCountForwardDispChars(buf, lineStartPos,
indent + textD->width / maxCharWidth);
TextDSetInsertPosition(textD, pos);
TextDSetScroll(textD, textD->topLineNum, textD->horizOffset + textD->width);
if (textD->horizOffset == oldHorizOffset && insertPos == pos)
XBell(XtDisplay(w), 0);
checkMoveSelectionChange(w, event, insertPos, args, nArgs);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
static void toggleOverstrikeAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
TextWidget tw = (TextWidget)w;
if (tw->text.overstrike) {
tw->text.overstrike = False;
TextDSetCursorStyle(tw->text.textD,
tw->text.heavyCursor ? HEAVY_CURSOR : NORMAL_CURSOR);
} else {
tw->text.overstrike = True;
if ( tw->text.textD->cursorStyle == NORMAL_CURSOR ||
tw->text.textD->cursorStyle == HEAVY_CURSOR)
TextDSetCursorStyle(tw->text.textD, BLOCK_CURSOR);
}
}
static void scrollUpAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int topLineNum, horizOffset, nLines;
if (*nArgs == 0 || sscanf(args[0], "%d", &nLines) != 1)
return;
TextDGetScroll(textD, &topLineNum, &horizOffset);
TextDSetScroll(textD, topLineNum-nLines, horizOffset);
}
static void scrollDownAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int topLineNum, horizOffset, nLines;
if (*nArgs == 0 || sscanf(args[0], "%d", &nLines) != 1)
return;
TextDGetScroll(textD, &topLineNum, &horizOffset);
TextDSetScroll(textD, topLineNum+nLines, horizOffset);
}
static void scrollToLineAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int topLineNum, horizOffset, lineNum;
if (*nArgs == 0 || sscanf(args[0], "%d", &lineNum) != 1)
return;
TextDGetScroll(textD, &topLineNum, &horizOffset);
TextDSetScroll(textD, lineNum, horizOffset);
}
static void selectAllAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
textBuffer *buf = ((TextWidget)w)->text.textD->buffer;
cancelDrag(w);
BufSelect(buf, 0, buf->length);
}
static void deselectAllAP(Widget w, XEvent *event, String *args,
Cardinal *nArgs)
{
cancelDrag(w);
BufUnselect(((TextWidget)w)->text.textD->buffer);
}
static void focusInAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
TextWidget tw = (TextWidget)w;
textDisp *textD = tw->text.textD;
/* I don't entirely understand the traversal mechanism in Motif widgets,
particularly, what leads to this widget getting a focus-in event when
it does not actually have the input focus. The temporary solution is
to do the comparison below, and not show the cursor when Motif says
we don't have focus, but keep looking for the real answer */
#if XmVersion >= 1002
if (w != XmGetFocusWidget(w))
return;
#endif
/* If the timer is not already started, start it */
if (tw->text.cursorBlinkRate != 0 && tw->text.cursorBlinkProcID == 0) {
tw->text.cursorBlinkProcID = XtAppAddTimeOut(
XtWidgetToApplicationContext((Widget)w),
tw->text.cursorBlinkRate, cursorBlinkTimerProc, w);
}
/* Change the cursor to active style */
if (((TextWidget)w)->text.overstrike)
TextDSetCursorStyle(textD, BLOCK_CURSOR);
else
TextDSetCursorStyle(textD, ((TextWidget)w)->text.heavyCursor ?
HEAVY_CURSOR : NORMAL_CURSOR);
TextDUnblankCursor(textD);
#ifdef USE_XMIM
/* Notify Motif input manager that widget has focus */
XmImVaSetFocusValues(w,NULL);
#endif
/* Call any registered focus-in callbacks */
XtCallCallbacks((Widget)w, textNfocusCallback, (XtPointer)event);
}
static void focusOutAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
{
textDisp *textD = ((TextWidget)w)->text.textD;
/* Remove the cursor blinking timer procedure */
if (((TextWidget)w)->text.cursorBlinkProcID != 0)
XtRemoveTimeOut(((TextWidget)w)->text.cursorBlinkProcID);
((TextWidget)w)->text.cursorBlinkProcID = 0;
/* Leave a dim or destination cursor */
TextDSetCursorStyle(textD, ((TextWidget)w)->text.motifDestOwner ?
CARET_CURSOR : DIM_CURSOR);
TextDUnblankCursor(textD);
/* Call any registered focus-out callbacks */
XtCallCallbacks((Widget)w, textNlosingFocusCallback, (XtPointer)event);
}
/*
** For actions involving cursor movement, "extend" keyword means incorporate
** the new cursor position in the selection, and lack of an "extend" keyword
** means cancel the existing selection
*/
static void checkMoveSelectionChange(Widget w, XEvent *event, int startPos,
String *args, Cardinal *nArgs)
{
if (hasKey("extend", args, nArgs))
keyMoveExtendSelection(w, event, startPos, hasKey("rect", args, nArgs));
else
BufUnselect((((TextWidget)w)->text.textD)->buffer);
}
/*
** If a selection change was requested via a keyboard command for moving
** the insertion cursor (usually with the "extend" keyword), adjust the
** selection to include the new cursor position, or begin a new selection
** between startPos and the new cursor position with anchor at startPos.
*/
static void keyMoveExtendSelection(Widget w, XEvent *event, int origPos,
int rectangular)
{
XKeyEvent *e = (XKeyEvent *)event;
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
selection *sel = &buf->primary;
int newPos = TextDGetInsertPosition(textD);
int startPos, endPos, col, startCol, endCol, newCol, origCol;
int anchor, rectAnchor, anchorLineStart;
/* Moving the cursor does not take the Motif destination, but as soon as
the user selects something, grab it (I'm not sure if this distinction
actually makes sense, but it's what Motif was doing, back when their
secondary selections actually worked correctly) */
TakeMotifDestination(w, e->time);
/* Extend the selection based on original and new selection type */
if (sel->selected && sel->rectangular && rectangular) { /* rect -> rect */
col = BufCountDispChars(buf, BufStartOfLine(buf, newPos), newPos);
rectAnchor = col < (sel->rectEnd + sel->rectStart) / 2 ?
sel->rectEnd : sel->rectStart;
anchorLineStart = BufStartOfLine(buf, newPos <
(sel->end + sel->start) / 2 ? sel->end : sel->start);
anchor = BufCountForwardDispChars(buf, anchorLineStart, rectAnchor);
startCol = min(rectAnchor, col);
endCol = max(rectAnchor, col);
startPos = BufStartOfLine(buf, min(anchor, newPos));
endPos = BufEndOfLine(buf, max(anchor, newPos));
BufRectSelect(buf, startPos, endPos, startCol, endCol);
} else if (sel->selected && sel->rectangular) { /* rect -> plain */
startPos = BufCountForwardDispChars(buf,
BufStartOfLine(buf, sel->start), sel->rectStart);
endPos = BufCountForwardDispChars(buf,
BufStartOfLine(buf, sel->end), sel->rectEnd);
if (abs(origPos - startPos) < abs(origPos - endPos))
anchor = endPos;
else
anchor = startPos;
BufSelect(buf, anchor, newPos);
} else if (sel->selected && rectangular) { /* plain -> rect */
startCol = BufCountDispChars(buf,
BufStartOfLine(buf, sel->start), sel->start);
endCol = BufCountDispChars(buf,
BufStartOfLine(buf, sel->end), sel->end);
newCol = BufCountDispChars(buf, BufStartOfLine(buf, newPos), newPos);
startPos = BufStartOfLine(buf, min(sel->start, newPos));
endPos = BufEndOfLine(buf, max(sel->end, newPos));
BufRectSelect(buf, startPos, endPos, startCol, endCol);
} else if (sel->selected) { /* plain -> plain */
if (abs(origPos - sel->start) < abs(origPos - sel->end))
anchor = sel->end;
else
anchor = sel->start;
BufSelect(buf, anchor, newPos);
} else if (rectangular) { /* no sel -> rect */
origCol = BufCountDispChars(buf, BufStartOfLine(buf, origPos), origPos);
newCol = BufCountDispChars(buf, BufStartOfLine(buf, newPos), newPos);
startCol = min(newCol, origCol);
endCol = max(newCol, origCol);
startPos = BufStartOfLine(buf, min(origPos, newPos));
endPos = BufEndOfLine(buf, max(origPos, newPos));
BufRectSelect(buf, startPos, endPos, startCol, endCol);
} else { /* no sel -> plain */
anchor = origPos;
BufSelect(buf, anchor, newPos);
}
}
static void checkAutoShowInsertPos(Widget w)
{
if (((TextWidget)w)->text.autoShowInsertPos)
TextDMakeInsertPosVisible(((TextWidget)w)->text.textD);
}
static int checkReadOnly(Widget w)
{
if (((TextWidget)w)->text.readOnly) {
XBell(XtDisplay(w), 0);
return True;
}
return False;
}
/*
** Insert text "chars" at the cursor position, as if the text had been
** typed. Same as TextInsertAtCursor, but without the complicated auto-wrap
** scanning and re-formatting.
*/
static void simpleInsertAtCursor(Widget w, char *chars, XEvent *event,
int allowPendingDelete)
{
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
char *c;
if (allowPendingDelete && pendingSelection(w)) {
BufReplaceSelected(buf, chars);
TextDSetInsertPosition(textD, buf->cursorPosHint);
} else if (((TextWidget)w)->text.overstrike) {
for (c=chars; *c!='\0' && *c!='\n'; c++);
if (*c == '\n')
TextDInsert(textD, chars);
else
TextDOverstrike(textD, chars);
} else
TextDInsert(textD, chars);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
}
/*
** If there's a selection, delete it and position the cursor where the
** selection was deleted. (Called by routines which do deletion to check
** first for and do possible selection delete)
*/
static int deletePendingSelection(Widget w, XEvent *event)
{
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = textD->buffer;
if (((TextWidget)w)->text.textD->buffer->primary.selected) {
BufRemoveSelected(buf);
TextDSetInsertPosition(textD, buf->cursorPosHint);
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
return True;
} else
return False;
}
/*
** Return true if pending delete is on and there's a selection contiguous
** with the cursor ready to be deleted. These criteria are used to decide
** if typing a character or inserting something should delete the selection
** first.
*/
static int pendingSelection(Widget w)
{
selection *sel = &((TextWidget)w)->text.textD->buffer->primary;
int pos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
return ((TextWidget)w)->text.pendingDelete && sel->selected &&
pos >= sel->start && pos <= sel->end;
}
/*
** Check if tab emulation is on and if there are emulated tabs before the
** cursor, and if so, delete an emulated tab as a unit. Also finishes up
** by calling checkAutoShowInsertPos and callCursorMovementCBs, so the
** calling action proc can just return (this is necessary to preserve
** emTabsBeforeCursor which is otherwise cleared by callCursorMovementCBs).
*/
static int deleteEmulatedTab(Widget w, XEvent *event)
{
textDisp *textD = ((TextWidget)w)->text.textD;
textBuffer *buf = ((TextWidget)w)->text.textD->buffer;
int emTabDist = ((TextWidget)w)->text.emulateTabs;
int emTabsBeforeCursor = ((TextWidget)w)->text.emTabsBeforeCursor;
int startIndent, toIndent, insertPos, startPos, lineStart;
int pos, indent, startPosIndent;
char c, *spaceString;
if (emTabDist <= 0 || emTabsBeforeCursor <= 0)
return False;
/* Find the position of the previous tab stop */
insertPos = TextDGetInsertPosition(textD);
lineStart = BufStartOfLine(buf, insertPos);
startIndent = BufCountDispChars(buf, lineStart, insertPos);
toIndent = (startIndent-1) - ((startIndent-1) % emTabDist);
/* Find the position at which to begin deleting (stop at non-whitespace
characters) */
startPosIndent = indent = 0;
startPos = lineStart;
for (pos=lineStart; pos < insertPos; pos++) {
c = BufGetCharacter(buf, pos);
indent += BufCharWidth(c, indent, buf->tabDist, buf->nullSubsChar);
if (indent > toIndent)
break;
startPosIndent = indent;
startPos = pos + 1;
}
/* Just to make sure, check that we're not deleting any non-white chars */
for (pos=insertPos-1; pos>=startPos; pos--) {
c = BufGetCharacter(buf, pos);
if (c != ' ' && c != '\t') {
startPos = pos + 1;
break;
}
}
/* Do the text replacement and reposition the cursor. If any spaces need
to be inserted to make up for a deleted tab, do a BufReplace, otherwise,
do a BufRemove. */
if (startPosIndent < toIndent) {
spaceString = XtMalloc(toIndent - startPosIndent + 1);
memset(spaceString, ' ', toIndent-startPosIndent);
spaceString[toIndent - startPosIndent] = '\0';
BufReplace(buf, startPos, insertPos, spaceString);
TextDSetInsertPosition(textD, startPos + toIndent - startPosIndent);
XtFree(spaceString);
} else {
BufRemove(buf, startPos, insertPos);
TextDSetInsertPosition(textD, startPos);
}
/* The normal cursor movement stuff would usually be called by the action
routine, but this wraps around it to restore emTabsBeforeCursor */
checkAutoShowInsertPos(w);
callCursorMovementCBs(w, event);
/* Decrement and restore the marker for consecutive emulated tabs, which
would otherwise have been zeroed by callCursorMovementCBs */
((TextWidget)w)->text.emTabsBeforeCursor = emTabsBeforeCursor - 1;
return True;
}
/*
** Select the word or whitespace adjacent to the cursor, and move the cursor
** to its end. pointerX is used as a tie-breaker, when the cursor is at the
** boundary between a word and some white-space. If the cursor is on the
** left, the word or space on the left is used. If it's on the right, that
** is used instead.
*/
static void selectWord(Widget w, int pointerX)
{
TextWidget tw = (TextWidget)w;
textBuffer *buf = tw->text.textD->buffer;
int x, y, insertPos = TextDGetInsertPosition(tw->text.textD);
TextPosToXY(w, insertPos, &x, &y);
if (pointerX < x && insertPos > 0 && BufGetCharacter(buf, insertPos-1) != '\n')
insertPos--;
BufSelect(buf, startOfWord(tw, insertPos), endOfWord(tw, insertPos));
}
static int startOfWord(TextWidget w, int pos)
{
int startPos;
textBuffer *buf = w->text.textD->buffer;
char *delimiters=w->text.delimiters;
char c = BufGetCharacter(buf, pos);
if (c == ' ' || c== '\t') {
if (!spanBackward(buf, pos, " \t", False, &startPos))
return 0;
} else if (strchr(delimiters, c)) {
if (!spanBackward(buf, pos, delimiters, True, &startPos))
return 0;
} else {
if (!BufSearchBackward(buf, pos, delimiters, &startPos))
return 0;
}
return min(pos, startPos+1);
}
static int endOfWord(TextWidget w, int pos)
{
int endPos;
textBuffer *buf = w->text.textD->buffer;
char *delimiters=w->text.delimiters;
char c = BufGetCharacter(buf, pos);
if (c == ' ' || c== '\t') {
if (!spanForward(buf, pos, " \t", False, &endPos))
return buf->length;
} else if (strchr(delimiters, c)) {
if (!spanForward(buf, pos, delimiters, True, &endPos))
return buf->length;
} else {
if (!BufSearchForward(buf, pos, delimiters, &endPos))
return buf->length;
}
return endPos;
}
/*
** Search forwards in buffer "buf" for the first character NOT in
** "searchChars", starting with the character "startPos", and returning the
** result in "foundPos" returns True if found, False if not. If ignoreSpace
** is set, then Space, Tab, and Newlines are ignored in searchChars.
*/
static int spanForward(textBuffer *buf, int startPos, char *searchChars,
int ignoreSpace, int *foundPos)
{
int pos;
char *c;
pos = startPos;
while (pos < buf->length) {
for (c=searchChars; *c!='\0'; c++)
if(!(ignoreSpace && (*c==' ' || *c=='\t' || *c=='\n')))
if (BufGetCharacter(buf, pos) == *c)
break;
if(*c == 0) {
*foundPos = pos;
return True;
}
pos++;
}
*foundPos = buf->length;
return False;
}
/*
** Search backwards in buffer "buf" for the first character NOT in
** "searchChars", starting with the character BEFORE "startPos", returning the
** result in "foundPos" returns True if found, False if not. If ignoreSpace is
** set, then Space, Tab, and Newlines are ignored in searchChars.
*/
static int spanBackward(textBuffer *buf, int startPos, char *searchChars, int
ignoreSpace, int *foundPos)
{
int pos;
char *c;
if (startPos == 0) {
*foundPos = 0;
return False;
}
pos = startPos == 0 ? 0 : startPos - 1;
while (pos >= 0) {
for (c=searchChars; *c!='\0'; c++)
if(!(ignoreSpace && (*c==' ' || *c=='\t' || *c=='\n')))
if (BufGetCharacter(buf, pos) == *c)
break;
if(*c == 0) {
*foundPos = pos;
return True;
}
pos--;
}
*foundPos = 0;
return False;
}
/*
** Select the line containing the cursor, including the terminating newline,
** and move the cursor to its end.
*/
static void selectLine(Widget w)
{
textDisp *textD = ((TextWidget)w)->text.textD;
int insertPos = TextDGetInsertPosition(textD);
int endPos, startPos;
endPos = TextDEndOfLine(textD, insertPos, False);
startPos = TextDStartOfLine(textD, insertPos);
BufSelect(textD->buffer, startPos, min(endPos+1, textD->buffer->length));
TextDSetInsertPosition(textD, endPos);
}
/*
** Given a new mouse pointer location, pass the position on to the
** autoscroll timer routine, and make sure the timer is on when it's
** needed and off when it's not.
*/
static void checkAutoScroll(TextWidget w, int x, int y)
{
int inWindow;
/* Is the pointer in or out of the window? */
inWindow = x >= w->text.marginWidth &&
x < w->core.width - w->text.marginWidth &&
y >= w->text.marginHeight &&
y < w->core.height - w->text.marginHeight;
/* If it's in the window, cancel the timer procedure */
if (inWindow) {
if (w->text.autoScrollProcID != 0)
XtRemoveTimeOut(w->text.autoScrollProcID);;
w->text.autoScrollProcID = 0;
return;
}
/* If the timer is not already started, start it */
if (w->text.autoScrollProcID == 0) {
w->text.autoScrollProcID = XtAppAddTimeOut(
XtWidgetToApplicationContext((Widget)w),
0, autoScrollTimerProc, w);
}
/* Pass on the newest mouse location to the autoscroll routine */
w->text.mouseX = x;
w->text.mouseY = y;
}
/*
** Reset drag state and cancel the auto-scroll timer
*/
static void endDrag(Widget w)
{
if (((TextWidget)w)->text.autoScrollProcID != 0)
XtRemoveTimeOut(((TextWidget)w)->text.autoScrollProcID);
((TextWidget)w)->text.autoScrollProcID = 0;
if (((TextWidget)w)->text.dragState == MOUSE_PAN)
XUngrabPointer(XtDisplay(w), CurrentTime);
((TextWidget)w)->text.dragState = NOT_CLICKED;
}
/*
** Cancel any drag operation that might be in progress. Should be included
** in nearly every key event to cleanly end any dragging before edits are made
** which might change the insert position or the content of the buffer during
** a drag operation)
*/
static void cancelDrag(Widget w)
{
int dragState = ((TextWidget)w)->text.dragState;
if (((TextWidget)w)->text.autoScrollProcID != 0)
XtRemoveTimeOut(((TextWidget)w)->text.autoScrollProcID);
if (dragState == SECONDARY_DRAG || dragState == SECONDARY_RECT_DRAG)
BufSecondaryUnselect(((TextWidget)w)->text.textD->buffer);
if (dragState == PRIMARY_BLOCK_DRAG)
CancelBlockDrag((TextWidget)w);
if (dragState == MOUSE_PAN)
XUngrabPointer(XtDisplay(w), CurrentTime);
if (dragState != NOT_CLICKED)
((TextWidget)w)->text.dragState = DRAG_CANCELED;
}
/*
** Do operations triggered by cursor movement: Call cursor movement callback
** procedure(s), and cancel marker indicating that the cursor is after one or
** more just-entered emulated tabs (spaces to be deleted as a unit).
*/
static void callCursorMovementCBs(Widget w, XEvent *event)
{
((TextWidget)w)->text.emTabsBeforeCursor = 0;
XtCallCallbacks((Widget)w, textNcursorMovementCallback, (XtPointer)event);
}
/*
** Adjust the selection as the mouse is dragged to position: (x, y).
*/
static void adjustSelection(TextWidget tw, int x, int y)
{
textDisp *textD = tw->text.textD;
textBuffer *buf = textD->buffer;
int row, col, startCol, endCol, startPos, endPos;
int newPos = TextDXYToPosition(textD, x, y);
/* Adjust the selection */
if (tw->text.dragState == PRIMARY_RECT_DRAG) {
TextDXYToUnconstrainedPosition(textD, x, y, &row, &col);
col = TextDOffsetWrappedColumn(textD, row, col);
startCol = min(tw->text.rectAnchor, col);
endCol = max(tw->text.rectAnchor, col);
startPos = BufStartOfLine(buf, min(tw->text.anchor, newPos));
endPos = BufEndOfLine(buf, max(tw->text.anchor, newPos));
BufRectSelect(buf, startPos, endPos, startCol, endCol);
} else if (tw->text.multiClickState == ONE_CLICK) {
startPos = startOfWord(tw, min(tw->text.anchor, newPos));
endPos = endOfWord(tw, max(tw->text.anchor, newPos));
BufSelect(buf, startPos, endPos);
newPos = newPos < tw->text.anchor ? startPos : endPos;
} else if (tw->text.multiClickState == TWO_CLICKS) {
startPos = BufStartOfLine(buf, min(tw->text.anchor, newPos));
endPos = BufEndOfLine(buf, max(tw->text.anchor, newPos));
BufSelect(buf, startPos, min(endPos+1, buf->length));
newPos = newPos < tw->text.anchor ? startPos : endPos;
} else
BufSelect(buf, tw->text.anchor, newPos);
/* Move the cursor */
TextDSetInsertPosition(textD, newPos);
callCursorMovementCBs((Widget)tw, NULL);
}
static void adjustSecondarySelection(TextWidget tw, int x, int y)
{
textDisp *textD = tw->text.textD;
textBuffer *buf = textD->buffer;
int row, col, startCol, endCol, startPos, endPos;
int newPos = TextDXYToPosition(textD, x, y);
if (tw->text.dragState == SECONDARY_RECT_DRAG) {
TextDXYToUnconstrainedPosition(textD, x, y, &row, &col);
col = TextDOffsetWrappedColumn(textD, row, col);
startCol = min(tw->text.rectAnchor, col);
endCol = max(tw->text.rectAnchor, col);
startPos = BufStartOfLine(buf, min(tw->text.anchor, newPos));
endPos = BufEndOfLine(buf, max(tw->text.anchor, newPos));
BufSecRectSelect(buf, startPos, endPos, startCol, endCol);
} else
BufSecondarySelect(textD->buffer, tw->text.anchor, newPos);
}
/*
** Wrap multi-line text in argument "text" to be inserted at the end of the
** text on line "startLine" and return the result. If "breakBefore" is
** non-NULL, allow wrapping to extend back into "startLine", in which case
** the returned text will include the wrapped part of "startLine", and
** "breakBefore" will return the number of characters at the end of
** "startLine" that were absorbed into the returned string. "breakBefore"
** will return zero if no characters were absorbed into the returned string.
** The buffer offset of text in the widget's text buffer is needed so that
** smart indent (which can be triggered by wrapping) can search back farther
** in the buffer than just the text in startLine.
*/
static char *wrapText(TextWidget tw, char *startLine, char *text, int bufOffset,
int wrapMargin, int *breakBefore)
{
textBuffer *wrapBuf, *buf = tw->text.textD->buffer;
int startLineLen = strlen(startLine);
int colNum, pos, lineStartPos, limitPos, breakAt, charsAdded;
int firstBreak = -1, tabDist = buf->tabDist;
char c, *wrappedText;
/* Create a temporary text buffer and load it with the strings */
wrapBuf = BufCreate();
BufInsert(wrapBuf, 0, startLine);
BufInsert(wrapBuf, wrapBuf->length, text);
/* Scan the buffer for long lines and apply wrapLine when wrapMargin is
exceeded. limitPos enforces no breaks in the "startLine" part of the
string (if requested), and prevents re-scanning of long unbreakable
lines for each character beyond the margin */
colNum = 0;
pos = 0;
lineStartPos = 0;
limitPos = breakBefore == NULL ? startLineLen : 0;
while (pos < wrapBuf->length) {
c = BufGetCharacter(wrapBuf, pos);
if (c == '\n') {
lineStartPos = limitPos = pos + 1;
colNum = 0;
} else {
colNum += BufCharWidth(c, colNum, tabDist, buf->nullSubsChar);
if (colNum > wrapMargin) {
if (!wrapLine(tw, wrapBuf, bufOffset, lineStartPos, pos,
limitPos, &breakAt, &charsAdded)) {
limitPos = pos;
} else {
lineStartPos = limitPos = breakAt+1;
pos += charsAdded;
colNum = BufCountDispChars(wrapBuf, lineStartPos, pos+1);
if (firstBreak == -1)
firstBreak = breakAt;
}
}
}
pos++;
}
/* Return the wrapped text, possibly including part of startLine */
if (breakBefore == NULL)
wrappedText = BufGetRange(wrapBuf, startLineLen, wrapBuf->length);
else {
*breakBefore = firstBreak != -1 && firstBreak < startLineLen ?
startLineLen - firstBreak : 0;
wrappedText = BufGetRange(wrapBuf, startLineLen - *breakBefore,
wrapBuf->length);
}
BufFree(wrapBuf);
return wrappedText;
}
/*
** Wraps the end of a line beginning at lineStartPos and ending at lineEndPos
** in "buf", at the last white-space on the line >= limitPos. (The implicit
** assumption is that just the last character of the line exceeds the wrap
** margin, and anywhere on the line we can wrap is correct). Returns False if
** unable to wrap the line. "breakAt", returns the character position at
** which the line was broken,
**
** Auto-wrapping can also trigger auto-indent. The additional parameter
** bufOffset is needed when auto-indent is set to smart indent and the smart
** indent routines need to scan far back in the buffer. "charsAdded" returns
** the number of characters added to acheive the auto-indent. wrapMargin is
** used to decide whether auto-indent should be skipped because the indent
** string itself would exceed the wrap margin.
*/
static int wrapLine(TextWidget tw, textBuffer *buf, int bufOffset,
int lineStartPos, int lineEndPos, int limitPos, int *breakAt,
int *charsAdded)
{
int p, length, column;
char c, *indentStr;
/* Scan backward for whitespace or BOL. If BOL, return False, no
whitespace in line at which to wrap */
for (p=lineEndPos; ; p--) {
if (p < lineStartPos || p < limitPos)
return False;
c = BufGetCharacter(buf, p);
if (c == '\t' || c == ' ')
break;
}
/* Create an auto-indent string to insert to do wrap. If the auto
indent string reaches the wrap position, slice the auto-indent
back off and return to the left margin */
if (tw->text.autoIndent || tw->text.smartIndent) {
indentStr = createIndentString(tw, buf, bufOffset, lineStartPos,
lineEndPos, &length, &column);
if (column >= p-lineStartPos)
indentStr[1] = '\0';
} else {
indentStr = "\n";
length = 1;
}
/* Replace the whitespace character with the auto-indent string
and return the stats */
BufReplace(buf, p, p+1, indentStr);
if (tw->text.autoIndent || tw->text.smartIndent)
XtFree(indentStr);
*breakAt = p;
*charsAdded = length-1;
return True;
}
/*
** Create and return an auto-indent string to add a newline at lineEndPos to a
** line starting at lineStartPos in buf. "buf" may or may not be the real
** text buffer for the widget. If it is not the widget's text buffer it's
** offset position from the real buffer must be specified in "bufOffset" to
** allow the smart-indent routines to scan back as far as necessary. The
** string length is returned in "length" (or "length" can be passed as NULL,
** and the indent column is returned in "column" (if non NULL).
*/
static char *createIndentString(TextWidget tw, textBuffer *buf, int bufOffset,
int lineStartPos, int lineEndPos, int *length, int *column)
{
textDisp *textD = tw->text.textD;
int pos, indent = -1, tabDist = textD->buffer->tabDist;
int i, useTabs = textD->buffer->useTabs;
char *indentPtr, *indentStr, c;
smartIndentCBStruct smartIndent;
/* If smart indent is on, call the smart indent callback. It is not
called when multi-line changes are being made (lineStartPos != 0),
because smart indent needs to search back an indeterminate distance
through the buffer, and reconciling that with wrapping changes made,
but not yet committed in the buffer, would make programming smart
indent more difficult for users and make everything more complicated */
if (tw->text.smartIndent && (lineStartPos == 0 || buf == textD->buffer)) {
smartIndent.reason = NEWLINE_INDENT_NEEDED;
smartIndent.pos = lineEndPos + bufOffset;
smartIndent.indentRequest = 0;
smartIndent.charsTyped = NULL;
XtCallCallbacks((Widget)tw, textNsmartIndentCallback,
(XtPointer)&smartIndent);
indent = smartIndent.indentRequest;
}
/* If smart indent wasn't used, measure the indent distance of the line */
if (indent == -1) {
indent = 0;
for (pos=lineStartPos; pos<lineEndPos; pos++) {
c = BufGetCharacter(buf, pos);
if (c != ' ' && c != '\t')
break;
if (c == '\t')
indent += tabDist - (indent % tabDist);
else
indent++;
}
}
/* Allocate and create a string of tabs and spaces to achieve the indent */
indentPtr = indentStr = XtMalloc(indent + 2);
*indentPtr++ = '\n';
if (useTabs) {
for (i=0; i < indent / tabDist; i++)
*indentPtr++ = '\t';
for (i=0; i < indent % tabDist; i++)
*indentPtr++ = ' ';
} else {
for (i=0; i < indent; i++)
*indentPtr++ = ' ';
}
*indentPtr = '\0';
/* Return any requested stats */
if (length != NULL)
*length = indentPtr - indentStr;
if (column != NULL)
*column = indent;
return indentStr;
}
/*
** Xt timer procedure for autoscrolling
*/
static void autoScrollTimerProc(XtPointer clientData, XtIntervalId *id)
{
TextWidget w = (TextWidget)clientData;
textDisp *textD = w->text.textD;
int topLineNum, horizOffset, newPos, cursorX, y;
int fontWidth = textD->fontStruct->max_bounds.width;
int fontHeight = textD->fontStruct->ascent + textD->fontStruct->descent;
/* For vertical autoscrolling just dragging the mouse outside of the top
or bottom of the window is sufficient, for horizontal (non-rectangular)
scrolling, see if the position where the CURSOR would go is outside */
newPos = TextDXYToPosition(textD, w->text.mouseX, w->text.mouseY);
if (w->text.dragState == PRIMARY_RECT_DRAG)
cursorX = w->text.mouseX;
else if (!TextDPositionToXY(textD, newPos, &cursorX, &y))
cursorX = w->text.mouseX;
/* Scroll away from the pointer, 1 character (horizontal), or 1 character
for each fontHeight distance from the mouse to the text (vertical) */
TextDGetScroll(textD, &topLineNum, &horizOffset);
if (cursorX >= (int)w->core.width - w->text.marginWidth)
horizOffset += fontWidth;
else if (w->text.mouseX < w->text.marginWidth)
horizOffset -= fontWidth;
if (w->text.mouseY >= (int)w->core.height - w->text.marginHeight)
topLineNum += 1 + ((w->text.mouseY - (int)w->core.height -
w->text.marginHeight) / fontHeight) + 1;
else if (w->text.mouseY < w->text.marginHeight)
topLineNum -= 1 + ((w->text.marginHeight-w->text.mouseY) / fontHeight);
TextDSetScroll(textD, topLineNum, horizOffset);
/* Continue the drag operation in progress. If none is in progress
(safety check) don't continue to re-establish the timer proc */
if (w->text.dragState == PRIMARY_DRAG) {
adjustSelection(w, w->text.mouseX, w->text.mouseY);
} else if (w->text.dragState == PRIMARY_RECT_DRAG) {
adjustSelection(w, w->text.mouseX, w->text.mouseY);
} else if (w->text.dragState == SECONDARY_DRAG) {
adjustSecondarySelection(w, w->text.mouseX, w->text.mouseY);
} else if (w->text.dragState == SECONDARY_RECT_DRAG) {
adjustSecondarySelection(w, w->text.mouseX, w->text.mouseY);
} else if (w->text.dragState == PRIMARY_BLOCK_DRAG) {
BlockDragSelection(w, w->text.mouseX, w->text.mouseY, USE_LAST);
} else {
w->text.autoScrollProcID = 0;
return;
}
/* re-establish the timer proc (this routine) to continue processing */
w->text.autoScrollProcID = XtAppAddTimeOut(
XtWidgetToApplicationContext((Widget)w),
w->text.mouseY >= w->text.marginHeight &&
w->text.mouseY < w->core.height - w->text.marginHeight ?
(VERTICAL_SCROLL_DELAY*fontWidth) / fontHeight :
VERTICAL_SCROLL_DELAY, autoScrollTimerProc, w);
}
/*
** Xt timer procedure for cursor blinking
*/
static void cursorBlinkTimerProc(XtPointer clientData, XtIntervalId *id)
{
TextWidget w = (TextWidget)clientData;
textDisp *textD = w->text.textD;
/* Blink the cursor */
if (textD->cursorOn)
TextDBlankCursor(textD);
else
TextDUnblankCursor(textD);
/* re-establish the timer proc (this routine) to continue processing */
w->text.cursorBlinkProcID = XtAppAddTimeOut(
XtWidgetToApplicationContext((Widget)w),
w->text.cursorBlinkRate, cursorBlinkTimerProc, w);
}
/*
** look at an action procedure's arguments to see if argument "key" has been
** specified in the argument list
*/
static int hasKey(char *key, String *args, Cardinal *nArgs)
{
int i;
for (i=0; i<*nArgs; i++)
if (!strCaseCmp(args[i], key))
return True;
return False;
}
static int max(int i1, int i2)
{
return i1 >= i2 ? i1 : i2;
}
static int min(int i1, int i2)
{
return i1 <= i2 ? i1 : i2;
}
/*
** strCaseCmp compares its arguments and returns 0 if the two strings
** are equal IGNORING case differences. Otherwise returns 1.
*/
static int strCaseCmp(char *str1, char *str2)
{
char *c1, *c2;
for (c1=str1, c2=str2; *c1!='\0' && *c2!='\0'; c1++, c2++)
if (toupper((unsigned char)*c1) != toupper((unsigned char)*c2))
return 1;
return 0;
}
|