1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124
|
/** <title>NSTextView</title>
Copyright (C) 1996, 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
Much code of this class was originally derived from code which was
in NSText.m.
Author: Scott Christley <scottc@net-community.com>
Date: 1996
Author: Felipe A. Rodriguez <far@ix.netcom.com>
Date: July 1998
Author: Daniel Bhringer <boehring@biomed.ruhr-uni-bochum.de>
Date: August 1998
Author: Fred Kiefer <FredKiefer@gmx.de>
Date: March 2000, September 2000
Author: Nicola Pero <n.pero@mi.flashnet.it>
Date: 2000, 2001, 2002
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include <gnustep/gui/config.h>
#include <Foundation/NSCoder.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSException.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/NSString.h>
#include <Foundation/NSNotification.h>
#include <AppKit/NSApplication.h>
#include <AppKit/NSWindow.h>
#include <AppKit/NSEvent.h>
#include <AppKit/NSFont.h>
#include <AppKit/NSColor.h>
#include <AppKit/NSTextView.h>
#include <AppKit/NSParagraphStyle.h>
#include <AppKit/NSRulerView.h>
#include <AppKit/NSScrollView.h>
#include <AppKit/NSPasteboard.h>
#include <AppKit/NSSpellChecker.h>
#include <AppKit/NSControl.h>
#include <AppKit/NSLayoutManager.h>
#include <AppKit/NSTextStorage.h>
#include <AppKit/NSColorPanel.h>
#define HUGE 1e7
/* not the same as NSMakeRange! */
static inline
NSRange MakeRangeFromAbs (unsigned a1, unsigned a2)
{
if (a1 < a2)
return NSMakeRange (a1, a2 - a1);
else
return NSMakeRange (a2, a1 - a2);
}
#define SET_DELEGATE_NOTIFICATION(notif_name) \
if ([_delegate respondsToSelector: @selector(text##notif_name: )]) \
[nc addObserver: _delegate \
selector: @selector(text##notif_name: ) \
name: NSText##notif_name##Notification \
object: _notifObject]
/*
* Synchronizing flags. Used to manage synchronizing shared
* attributes between textviews coupled with the same layout manager.
* These synchronizing flags are only accessed when
* _tvf.multiple_textviews == YES and this can only happen if we have
* a non-nil NSLayoutManager - so we don't check. */
/* YES when in the process of synchronizing text view attributes.
Used to avoid recursive synchronizations. */
#define IS_SYNCHRONIZING_FLAGS _layoutManager->_isSynchronizingFlags
/* YES when in the process of synchronizing delegates.
Used to avoid recursive synchronizations. */
#define IS_SYNCHRONIZING_DELEGATES _layoutManager->_isSynchronizingDelegates
/*
* Began editing flag. There are quite some different ways in which
* editing can be started. Each time editing is started, we need to check
* with the delegate if it is OK to start editing - but we need to check
* only once. So, we use a flag. */
static BOOL noLayoutManagerException ()
{
[NSException raise: NSGenericException
format: @"Can't edit a NSTextView without a layout manager!"];
return YES;
}
/* YES when editing has already began. If NO, then we need to ask to
the delegate for permission to begin editing before allowing any
change to be made. We explicitly check for a layout manager, and
raise an exception if not found. */
#define BEGAN_EDITING \
(_layoutManager ? _layoutManager->_beganEditing : noLayoutManagerException ())
#define SET_BEGAN_EDITING(X) \
if (_layoutManager != nil) _layoutManager->_beganEditing = X
/* The shared notification center */
static NSNotificationCenter *nc;
@interface NSTextView (GNUstepPrivate)
/*
* Used to implement the blinking insertion point
*/
- (void) _blink: (NSTimer *)t;
/*
* these NSLayoutManager- like method is here only informally
*/
- (NSRect) rectForCharacterRange: (NSRange)aRange;
/*
* various GNU extensions
*/
+ (NSDictionary*) defaultTypingAttributes;
//
// GNU utility methods
//
- (void) setAttributes: (NSDictionary *) attributes range: (NSRange) aRange;
- (void) _illegalMovement: (int) notNumber;
- (void) copySelection;
- (void) pasteSelection;
@end
@implementation NSTextView
/* Class methods */
+ (void) initialize
{
if ([self class] == [NSTextView class])
{
[self setVersion: 1];
nc = [NSNotificationCenter defaultCenter];
[self registerForServices];
}
}
+ (void) registerForServices
{
NSArray *types;
types = [NSArray arrayWithObjects: NSStringPboardType,
NSRTFPboardType, NSRTFDPboardType, nil];
[[NSApplication sharedApplication] registerServicesMenuSendTypes: types
returnTypes: types];
}
/* Initializing Methods */
- (NSTextContainer*) buildUpTextNetwork: (NSSize)aSize
{
NSTextContainer *textContainer;
NSLayoutManager *layoutManager;
NSTextStorage *textStorage;
textStorage = [[NSTextStorage alloc] init];
layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager: layoutManager];
RELEASE (layoutManager);
textContainer = [[NSTextContainer alloc] initWithContainerSize: aSize];
[layoutManager addTextContainer: textContainer];
RELEASE (textContainer);
/* The situation at this point is as follows:
textView (us) --RETAINs--> textStorage
textStorage --RETAINs--> layoutManager
layoutManager --RETAINs--> textContainer */
/* We keep a flag to remember that we are directly responsible for
managing the text objects. */
_tvf.owns_text_network = YES;
return textContainer;
}
/* Designated initializer */
- (id) initWithFrame: (NSRect)frameRect
textContainer: (NSTextContainer*)aTextContainer
{
[super initWithFrame: frameRect];
[self setMinSize: NSMakeSize (0, 0)];
[self setMaxSize: NSMakeSize (HUGE,HUGE)];
_tf.is_field_editor = NO;
_tf.is_editable = YES;
_tf.is_selectable = YES;
_tf.is_rich_text = NO;
_tf.imports_graphics = NO;
_tf.draws_background = YES;
_tf.is_horizontally_resizable = NO;
_tf.is_vertically_resizable = NO;
_tf.uses_font_panel = YES;
_tf.uses_ruler = YES;
_tf.is_ruler_visible = NO;
_original_selected_range.location = NSNotFound;
ASSIGN (_caret_color, [NSColor blackColor]);
[self setTypingAttributes: [isa defaultTypingAttributes]];
[self setBackgroundColor: [NSColor textBackgroundColor]];
[aTextContainer setTextView: self];
[self setEditable: YES];
[self setUsesFontPanel: YES];
[self setUsesRuler: YES];
[self setSelectedRange: NSMakeRange (0, 0)];
return self;
}
- (id) initWithFrame: (NSRect)frameRect
{
NSTextContainer *aTextContainer;
aTextContainer = [self buildUpTextNetwork: frameRect.size];
self = [self initWithFrame: frameRect textContainer: aTextContainer];
/* At this point the situation is as follows:
textView (us) --RETAINs--> textStorage
textStorage --RETAINs--> layoutManager
layoutManager --RETAINs--> textContainer
textContainter --RETAINs --> textView (us) */
/* The text system should be destroyed when the textView (us) is
released. To get this result, we send a RELEASE message to us
breaking the RETAIN cycle. */
RELEASE (self);
return self;
}
- (void) encodeWithCoder: (NSCoder *)aCoder
{
BOOL flag;
[super encodeWithCoder: aCoder];
flag = _tvf.smart_insert_delete;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _tvf.allows_undo;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
}
- (id) initWithCoder: (NSCoder *)aDecoder
{
NSTextContainer *aTextContainer;
BOOL flag;
self = [super initWithCoder: aDecoder];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_tvf.smart_insert_delete = flag;
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_tvf.allows_undo = flag;
/* build up the rest of the text system, which doesn't get stored
<doesn't even implement the Coding protocol>. */
aTextContainer = [self buildUpTextNetwork: _frame.size];
[aTextContainer setTextView: (NSTextView*)self];
/* See initWithFrame: for comments on this RELEASE */
RELEASE (self);
[self setSelectedRange: NSMakeRange (0, 0)];
return self;
}
- (void)dealloc
{
if (_tvf.owns_text_network == YES)
{
/* Prevent recursive dealloc */
if (_tvf.is_in_dealloc == YES)
{
return;
}
_tvf.is_in_dealloc = YES;
/* Balance the RELEASE we sent to us to break the retain cycle
in initWithFrame: or initWithCoder: (otherwise releasing the
_textStorage will make our retain count go below zero ;-) */
RETAIN (self);
/* This releases all the text objects (us included) in fall */
RELEASE (_textStorage);
}
if (_insertionPointTimer != nil)
{
[_insertionPointTimer invalidate];
DESTROY (_insertionPointTimer);
}
RELEASE (_selectedTextAttributes);
RELEASE (_markedTextAttributes);
RELEASE (_caret_color);
RELEASE (_typingAttributes);
[super dealloc];
}
/*
* Implementation of methods declared in superclass but depending
* on the internals of the NSTextView
*/
- (void) replaceCharactersInRange: (NSRange)aRange
withString: (NSString *)aString
{
if (aRange.location == NSNotFound)
return;
if ([self shouldChangeTextInRange: aRange
replacementString: aString] == NO)
return;
[_textStorage beginEditing];
if (_tf.is_rich_text && [_textStorage length] == 0)
{
/* In this case, there is nothing in the _textStorage ... so the
_textStorage can't choose appropriate formatting attributes
for the string ... we want the typingAttributes to be used in
this case. So we set the attributes manually. */
NSAttributedString *as;
as = AUTORELEASE ([[NSAttributedString alloc]
initWithString: aString
attributes: _typingAttributes]);
[self replaceRange: aRange withAttributedString: as];
}
else
{
[_textStorage replaceCharactersInRange: aRange withString: aString];
}
[_textStorage endEditing];
[self didChangeText];
}
- (NSData*) RTFDFromRange: (NSRange)aRange
{
return [_textStorage RTFDFromRange: aRange documentAttributes: nil];
}
- (NSData*) RTFFromRange: (NSRange)aRange
{
return [_textStorage RTFFromRange: aRange documentAttributes: nil];
}
- (NSString *) string
{
return [_textStorage string];
}
/*
* [NSText] Managing the Ruler
*/
- (void) toggleRuler: (id)sender
{
[self setRulerVisible: !_tf.is_ruler_visible];
}
/*
* [NSText] Managing the Selected Range
*/
- (NSRange) selectedRange
{
return _selected_range;
}
- (void) setSelectedRange: (NSRange)range
{
[self setSelectedRange: range affinity: [self selectionAffinity]
stillSelecting: NO];
}
/*
* [NSText] Copy and Paste
*/
- (void) copy: (id)sender
{
NSMutableArray *types = [NSMutableArray array];
if (_tf.imports_graphics)
[types addObject: NSRTFDPboardType];
if (_tf.is_rich_text)
[types addObject: NSRTFPboardType];
[types addObject: NSStringPboardType];
[self writeSelectionToPasteboard: [NSPasteboard generalPasteboard]
types: types];
}
/* Copy the current font to the font pasteboard */
- (void) copyFont: (id)sender
{
NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSFontPboard];
[self writeSelectionToPasteboard: pb type: NSFontPboardType];
}
/* Copy the current ruler settings to the ruler pasteboard */
- (void) copyRuler: (id)sender
{
NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSRulerPboard];
[self writeSelectionToPasteboard: pb type: NSRulerPboardType];
}
- (void) delete: (id)sender
{
[self deleteForward: sender];
}
- (void) paste: (id)sender
{
[self readSelectionFromPasteboard: [NSPasteboard generalPasteboard]];
}
- (void) pasteFont: (id)sender
{
NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSFontPboard];
[self readSelectionFromPasteboard: pb type: NSFontPboardType];
}
- (void) pasteRuler: (id)sender
{
NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSRulerPboard];
[self readSelectionFromPasteboard: pb type: NSRulerPboardType];
}
/*
* [NSText] Managing Fonts
*/
- (NSFont*) font
{
if ([_textStorage length] > 0)
{
NSFont *font;
font = [_textStorage attribute: NSFontAttributeName
atIndex: 0
effectiveRange: NULL];
if (font != nil)
{
return font;
}
}
return [_typingAttributes objectForKey: NSFontAttributeName];
}
- (void) changeFont: (id)sender
{
NSRange foundRange;
int maxSelRange;
NSRange aRange= [self rangeForUserCharacterAttributeChange];
NSRange searchRange = aRange;
NSFont *font;
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange replacementString: nil])
return;
[_textStorage beginEditing];
for (maxSelRange = NSMaxRange (aRange);
searchRange.location < maxSelRange;
searchRange = NSMakeRange (NSMaxRange (foundRange),
maxSelRange - NSMaxRange (foundRange)))
{
font = [_textStorage attribute: NSFontAttributeName
atIndex: searchRange.location
longestEffectiveRange: &foundRange
inRange: searchRange];
if (font != nil)
{
[self setFont: [sender convertFont: font] ofRange: foundRange];
}
}
[_textStorage endEditing];
[self didChangeText];
/* Set typing attributes */
font = [_typingAttributes objectForKey: NSFontAttributeName];
if (font != nil)
{
[_typingAttributes setObject: [sender convertFont: font]
forKey: NSFontAttributeName];
}
}
- (void) setFont: (NSFont*)font
{
if (font == nil)
{
return;
}
[self setFont: font ofRange: NSMakeRange (0, [_textStorage length])];
[_typingAttributes setObject: font forKey: NSFontAttributeName];
}
- (void) setFont: (NSFont*)font ofRange: (NSRange)aRange
{
if (font != nil)
{
if (![self shouldChangeTextInRange: aRange replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage addAttribute: NSFontAttributeName value: font
range: aRange];
[_textStorage endEditing];
[self didChangeText];
}
}
/*
* [NSText] Managing Alignment
*/
- (NSTextAlignment) alignment
{
unsigned location = 0;
NSParagraphStyle *aStyle;
if (_tf.is_rich_text)
{
location = [self rangeForUserParagraphAttributeChange].location;
}
if (location != NSNotFound)
{
aStyle = [_textStorage attribute: NSParagraphStyleAttributeName
atIndex: location
effectiveRange: NULL];
if (aStyle != nil)
{
return [aStyle alignment];
}
}
/* Get alignment from typing attributes */
return [[_typingAttributes objectForKey: NSParagraphStyleAttributeName]
alignment];
}
- (void) setAlignment: (NSTextAlignment)mode
{
[self setAlignment: mode range: NSMakeRange (0, [_textStorage length])];
}
- (void) alignCenter: (id)sender
{
[self setAlignment: NSCenterTextAlignment
range: [self rangeForUserParagraphAttributeChange]];
}
- (void) alignLeft: (id)sender
{
[self setAlignment: NSLeftTextAlignment
range: [self rangeForUserParagraphAttributeChange]];
}
- (void) alignRight: (id)sender
{
[self setAlignment: NSRightTextAlignment
range: [self rangeForUserParagraphAttributeChange]];
}
/*
* [NSText] Managing Text Color
*/
- (NSColor*) textColor
{
if ([_textStorage length] > 0)
{
NSColor *color;
color = [_textStorage attribute: NSForegroundColorAttributeName
atIndex: 0
effectiveRange: NULL];
if (color != nil)
{
return color;
}
}
return [_typingAttributes objectForKey: NSForegroundColorAttributeName];
}
- (void) setTextColor: (NSColor*)color
{
/* FIXME: This should work also for non rich text objects */
NSRange fullRange = NSMakeRange (0, [_textStorage length]);
[self setTextColor: color range: fullRange];
}
- (void) setTextColor: (NSColor*)color range: (NSRange)aRange
{
/* FIXME: This should only work for rich text object */
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange replacementString: nil])
{
return;
}
[_textStorage beginEditing];
if (color != nil)
{
[_textStorage addAttribute: NSForegroundColorAttributeName
value: color
range: aRange];
[_typingAttributes setObject: color
forKey: NSForegroundColorAttributeName];
}
else
{
[_textStorage removeAttribute: NSForegroundColorAttributeName
range: aRange];
}
[_textStorage endEditing];
[self didChangeText];
}
/*
* [NSText] Text Attributes
*/
- (void) subscript: (id)sender
{
NSNumber *value = [_typingAttributes
objectForKey: NSSuperscriptAttributeName];
int sValue;
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (aRange.length)
{
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage subscriptRange: aRange];
[_textStorage endEditing];
[self didChangeText];
}
// Set the typing attributes
if (value != nil)
sValue = [value intValue] - 1;
else
sValue = -1;
[_typingAttributes setObject: [NSNumber numberWithInt: sValue]
forKey: NSSuperscriptAttributeName];
}
- (void) superscript: (id)sender
{
NSNumber *value = [_typingAttributes
objectForKey: NSSuperscriptAttributeName];
int sValue;
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (aRange.length)
{
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage superscriptRange: aRange];
[_textStorage endEditing];
[self didChangeText];
}
// Set the typing attributes
if (value != nil)
sValue = [value intValue] + 1;
else
sValue = 1;
[_typingAttributes setObject: [NSNumber numberWithInt: sValue]
forKey: NSSuperscriptAttributeName];
}
- (void) unscript: (id)sender
{
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (aRange.length)
{
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage unscriptRange: aRange];
[_textStorage endEditing];
[self didChangeText];
}
// Set the typing attributes
[_typingAttributes removeObjectForKey: NSSuperscriptAttributeName];
}
- (void) underline: (id)sender
{
BOOL doUnderline = YES;
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if ([[_textStorage attribute: NSUnderlineStyleAttributeName
atIndex: aRange.location
effectiveRange: NULL] intValue])
doUnderline = NO;
if (aRange.length)
{
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage addAttribute: NSUnderlineStyleAttributeName
value: [NSNumber numberWithInt: doUnderline]
range: aRange];
[_textStorage endEditing];
[self didChangeText];
}
[_typingAttributes
setObject: [NSNumber numberWithInt: doUnderline]
forKey: NSUnderlineStyleAttributeName];
}
/*
* [NSText] Reading and Writing RTFD files
*/
- (BOOL) readRTFDFromFile: (NSString*)path
{
NSAttributedString *peek;
peek = [[NSAttributedString alloc] initWithPath: path
documentAttributes: NULL];
if (peek != nil)
{
if (!_tf.is_rich_text)
{
[self setRichText: YES];
}
[self replaceRange: NSMakeRange (0, [_textStorage length])
withAttributedString: peek];
RELEASE(peek);
return YES;
}
return NO;
}
- (BOOL) writeRTFDToFile: (NSString*)path atomically: (BOOL)flag
{
NSFileWrapper *wrapper;
NSRange range = NSMakeRange (0, [_textStorage length]);
wrapper = [_textStorage RTFDFileWrapperFromRange: range
documentAttributes: nil];
return [wrapper writeToFile: path atomically: flag updateFilenames: YES];
}
/*
* [NSText] Managing size
*/
- (void) setHorizontallyResizable: (BOOL)flag
{
/* Safety call */
[_textContainer setWidthTracksTextView: !flag];
[super setHorizontallyResizable: flag];
}
- (void) setVerticallyResizable: (BOOL)flag
{
/* Safety call */
[_textContainer setHeightTracksTextView: !flag];
[super setVerticallyResizable: flag];
}
- (void) sizeToFit
{
if (_tf.is_horizontally_resizable || _tf.is_vertically_resizable)
{
NSSize size;
size = [_layoutManager usedRectForTextContainer: _textContainer].size;
size.width += 2 * _textContainerInset.width;
size.height += 2 * _textContainerInset.height;
[self setConstrainedFrameSize: size];
}
}
/*
* [NSText] Spelling
*/
- (void) checkSpelling: (id)sender
{
NSSpellChecker *sp = [NSSpellChecker sharedSpellChecker];
NSString *misspelledWord = nil;
NSRange errorRange;
int count = 0;
errorRange = [sp checkSpellingOfString: [self string]
startingAt: NSMaxRange (_selected_range)
language: [sp language]
wrap: YES
inSpellDocumentWithTag: [self spellCheckerDocumentTag]
wordCount: &count];
if (errorRange.length)
{
[self setSelectedRange: errorRange];
misspelledWord = [[self string] substringFromRange: errorRange];
[sp updateSpellingPanelWithMisspelledWord: misspelledWord];
}
else
{
[sp updateSpellingPanelWithMisspelledWord: @""];
}
}
- (void) changeSpelling: (id)sender
{
[self insertText: [[(NSControl*)sender selectedCell] stringValue]];
}
- (void) ignoreSpelling: (id)sender
{
NSSpellChecker *sp = [NSSpellChecker sharedSpellChecker];
[sp ignoreWord: [[(NSControl*)sender selectedCell] stringValue]
inSpellDocumentWithTag: [self spellCheckerDocumentTag]];
}
/*
* [NSText] Scrolling
*/
- (void) scrollRangeToVisible: (NSRange)aRange
{
// Don't try scrolling an ancestor clipview if we are field editor.
// This makes things so much simpler and stabler for now.
if (_tf.is_field_editor == NO)
{
[self scrollRectToVisible: [self rectForCharacterRange: aRange]];
}
}
/*
* [NSResponder] Handle enabling/disabling of services menu items.
*/
- (id) validRequestorForSendType: (NSString*)sendType
returnType: (NSString*)returnType
{
BOOL sendOK = NO;
BOOL returnOK = NO;
if (sendType == nil)
{
sendOK = YES;
}
else if (_selected_range.length && [sendType isEqual: NSStringPboardType])
{
sendOK = YES;
}
if (returnType == nil)
{
returnOK = YES;
}
else if (_tf.is_editable && [returnType isEqual: NSStringPboardType])
{
returnOK = YES;
}
if (sendOK && returnOK)
{
return self;
}
return [super validRequestorForSendType: sendType returnType: returnType];
}
/*
* NSTextView's specific methods
*/
- (void) _updateMultipleTextViews
{
id oldNotifObject = _notifObject;
if ([[_layoutManager textContainers] count] > 1)
{
_tvf.multiple_textviews = YES;
_notifObject = [_layoutManager firstTextView];
}
else
{
_tvf.multiple_textviews = NO;
_notifObject = self;
}
if ((_delegate != nil) && (oldNotifObject != _notifObject))
{
[nc removeObserver: _delegate name: nil object: oldNotifObject];
if ([_delegate respondsToSelector:
@selector(shouldChangeTextInRange:replacementString:)])
{
_tvf.delegate_responds_to_should_change = YES;
}
else
{
_tvf.delegate_responds_to_should_change = NO;
}
/* SET_DELEGATE_NOTIFICATION defined at the beginning of file */
/* NSText notifications */
SET_DELEGATE_NOTIFICATION (DidBeginEditing);
SET_DELEGATE_NOTIFICATION (DidChange);
SET_DELEGATE_NOTIFICATION (DidEndEditing);
/* NSTextView notifications */
SET_DELEGATE_NOTIFICATION (ViewDidChangeSelection);
SET_DELEGATE_NOTIFICATION (ViewWillChangeNotifyingTextView);
}
}
/* This should only be called by [NSTextContainer -setTextView:] */
- (void) setTextContainer: (NSTextContainer*)aTextContainer
{
_textContainer = aTextContainer;
_layoutManager = [aTextContainer layoutManager];
_textStorage = [_layoutManager textStorage];
[self _updateMultipleTextViews];
}
- (void) replaceTextContainer: (NSTextContainer*)aTextContainer
{
/* FIXME/TODO: Tell the layout manager the text container is changed
keeping all the rest intact */
/* Do not retain: text container is owning us. */
_textContainer = aTextContainer;
[self _updateMultipleTextViews];
}
- (NSTextContainer *) textContainer
{
return _textContainer;
}
- (void) setTextContainerInset: (NSSize)inset
{
_textContainerInset = inset;
[self invalidateTextContainerOrigin];
}
- (NSSize) textContainerInset
{
return _textContainerInset;
}
- (NSPoint) textContainerOrigin
{
return _textContainerOrigin;
}
- (void) invalidateTextContainerOrigin
{
NSRect usedRect;
NSSize textContainerSize;
usedRect = [_layoutManager usedRectForTextContainer: _textContainer];
textContainerSize = [_textContainer containerSize];
/* The `text container origin' is - I think - the origin of the used
rect, but relative to our own coordinate system (used rect as
returned by [NSLayoutManager -usedRectForTextContainer:] is
instead relative to the text container coordinate system). This
information is used when we ask the layout manager to draw - the
base point is precisely this `text container origin', which is
the origin of the used rect in our own coordinate system. */
/* First get the pure text container origin */
_textContainerOrigin.x = NSMinX (_bounds);
_textContainerOrigin.x += _textContainerInset.width;
/* Then move to the used rect origin */
_textContainerOrigin.x += usedRect.origin.x;
/* First get the pure text container origin */
_textContainerOrigin.y = NSMaxY (_bounds);
_textContainerOrigin.y -= _textContainerInset.height;
_textContainerOrigin.y -= textContainerSize.height;
/* Then move to the used rect origin */
_textContainerOrigin.y += usedRect.origin.y;
}
- (NSLayoutManager*) layoutManager
{
return _layoutManager;
}
- (NSTextStorage*) textStorage
{
return _textStorage;
}
- (void) setAllowsUndo: (BOOL)flag
{
_tvf.allows_undo = flag;
}
- (BOOL) allowsUndo
{
return _tvf.allows_undo;
}
- (void) setNeedsDisplayInRect: (NSRect)aRect
avoidAdditionalLayout: (BOOL)flag
{
/* FIXME: This is here until the layout manager is working */
/* This is very important */
[super setNeedsDisplayInRect: aRect];
}
/* We override NSView's setNeedsDisplayInRect: */
- (void) setNeedsDisplayInRect: (NSRect)aRect
{
[self setNeedsDisplayInRect: aRect avoidAdditionalLayout: NO];
}
- (BOOL) shouldDrawInsertionPoint
{
return (_selected_range.length == 0) && _tf.is_editable
&& [_window isKeyWindow] && ([_window firstResponder] == self);
}
/*
* It only makes real sense to call this method with `flag == YES'.
* If you want to delete the insertion point, what you want is rather
* to redraw what was under the insertion point - which can't be done
* here - you need to set the rect as needing redisplay (without
* additional layout) instead. NB: You need to flush the window after
* calling this method if you want the insertion point to appear on
* the screen immediately. This could only be needed to implement
* blinking insertion point - but even there, it could probably be
* done without. */
- (void) drawInsertionPointInRect: (NSRect)rect
color: (NSColor*)color
turnedOn: (BOOL)flag
{
if (_window == nil)
{
return;
}
if (flag)
{
if (color == nil)
{
color = _caret_color;
}
[color set];
NSRectFill (rect);
}
else
{
[_background_color set];
NSRectFill (rect);
}
}
- (void) setConstrainedFrameSize: (NSSize)desiredSize
{
NSSize newSize;
if (_tf.is_horizontally_resizable)
{
newSize.width = desiredSize.width;
newSize.width = MAX (newSize.width, _minSize.width);
newSize.width = MIN (newSize.width, _maxSize.width);
}
else
{
newSize.width = _frame.size.width;
}
if (_tf.is_vertically_resizable)
{
newSize.height = desiredSize.height;
newSize.height = MAX (newSize.height, _minSize.height);
newSize.height = MIN (newSize.height, _maxSize.height);
}
else
{
newSize.height = _frame.size.height;
}
if (NSEqualSizes (_frame.size, newSize) == NO)
{
[self setFrameSize: newSize];
}
}
/*
* Code to share settings between multiple textviews
*
*/
/*
_syncTextViewsCalling:withFlag: calls a set method on all text
views sharing the same layout manager as this one. It sets the
IS_SYNCHRONIZING_FLAGS flag to YES to prevent recursive calls;
calls the specified action on all the textviews (this one included)
with the specified flag; sets back the IS_SYNCHRONIZING_FLAGS flag
to NO; then returns.
We need to explicitly call the methods - we can't copy the flags
directly from one textview to another, to allow subclasses to
override eg setEditable: to take some particular action when
editing is turned on or off. */
- (void) _syncTextViewsByCalling: (SEL)action withFlag: (BOOL)flag
{
NSArray *array;
int i, count;
void (*msg)(id, SEL, BOOL);
if (IS_SYNCHRONIZING_FLAGS == YES)
{
[NSException raise: NSGenericException
format: @"_syncTextViewsCalling:withFlag: "
@"called recursively"];
}
array = [_layoutManager textContainers];
count = [array count];
msg = (void (*)(id, SEL, BOOL))[self methodForSelector: action];
if (!msg)
{
[NSException raise: NSGenericException
format: @"invalid selector in "
@"_syncTextViewsCalling:withFlag:"];
}
IS_SYNCHRONIZING_FLAGS = YES;
for (i = 0; i < count; i++)
{
NSTextView *tv;
tv = [(NSTextContainer *)[array objectAtIndex: i] textView];
(*msg) (tv, action, flag);
}
IS_SYNCHRONIZING_FLAGS = NO;
}
#define NSTEXTVIEW_SYNC(X) \
if (_tvf.multiple_textviews && (IS_SYNCHRONIZING_FLAGS == NO)) \
{ [self _syncTextViewsByCalling: X withFlag: flag]; \
return; }
/*
* NB: You might override these methods in subclasses, as in the
* following example:
* - (void) setEditable: (BOOL)flag
* {
* [super setEditable: flag];
* XXX your custom code here XXX
* }
*
* If you override them in this way, they are automatically
* synchronized between multiple textviews - ie, when it is called on
* one, it will be automatically called on all related textviews.
* */
- (void) setEditable: (BOOL)flag
{
NSTEXTVIEW_SYNC (@selector(setEditable:));
[super setEditable: flag];
if ([self shouldDrawInsertionPoint])
{
[self updateInsertionPointStateAndRestartTimer: YES];
}
else
{
if (_insertionPointTimer != nil)
{
[_insertionPointTimer invalidate];
DESTROY (_insertionPointTimer);
_drawInsertionPointNow = NO;
}
}
}
- (void) setFieldEditor: (BOOL)flag
{
NSTEXTVIEW_SYNC (@selector(setFieldEditor:));
[self setHorizontallyResizable: NO];
[self setVerticallyResizable: NO];
[super setFieldEditor: flag];
}
- (void) setSelectable: (BOOL)flag
{
NSTEXTVIEW_SYNC (@selector(setSelectable:));
[super setSelectable: flag];
}
- (void) setRichText: (BOOL)flag
{
NSTEXTVIEW_SYNC (@selector(setRichText:));
[super setRichText: flag];
[self updateDragTypeRegistration];
/* FIXME/TODO: Also convert text to plain text or to rich text */
}
- (void) setImportsGraphics: (BOOL)flag
{
NSTEXTVIEW_SYNC (@selector(setImportsGraphics:));
[super setImportsGraphics: flag];
[self updateDragTypeRegistration];
}
- (void) setUsesRuler: (BOOL)flag
{
NSTEXTVIEW_SYNC (@selector(setUsesRuler:));
_tf.uses_ruler = flag;
}
- (BOOL) usesRuler
{
return _tf.uses_ruler;
}
- (void) setUsesFontPanel: (BOOL)flag
{
NSTEXTVIEW_SYNC (@selector(setUsesFontPanel:));
[super setUsesFontPanel: flag];
}
- (void) setRulerVisible: (BOOL)flag
{
NSScrollView *sv;
NSTEXTVIEW_SYNC (@selector(setRulerVisible:));
sv = [self enclosingScrollView];
_tf.is_ruler_visible = flag;
if (sv != nil)
{
[sv setRulersVisible: _tf.is_ruler_visible];
}
}
#undef NSTEXTVIEW_SYNC
/* NB: Only NSSelectionAffinityDownstream works */
- (void) setSelectedRange: (NSRange)range
affinity: (NSSelectionAffinity)affinity
stillSelecting: (BOOL)flag
{
/* The `official' (the last one the delegate approved of) selected
range before this one. */
NSRange oldRange;
/* If the user was interactively changing the selection, the last
displayed selection could have been a temporary selection,
different from the last official one: */
NSRange oldDisplayedRange = _selected_range;
if (flag == YES)
{
/* Store the original range before the interactive selection
process begin. That's because we will need to ask the delegate
if it's all right for him to do the change, and then notify
him we did. In both cases, we need to post the original selection
together with the new one. */
if (_original_selected_range.location == NSNotFound)
{
_original_selected_range = _selected_range;
}
}
else
{
/* Retrieve the original range */
if (_original_selected_range.location != NSNotFound)
{
oldRange = _original_selected_range;
_original_selected_range.location = NSNotFound;
}
else
{
oldRange = _selected_range;
}
/* Ask delegate to modify the range */
if (_tvf.delegate_responds_to_will_change_sel)
{
range = [_delegate textView: _notifObject
willChangeSelectionFromCharacterRange: oldRange
toCharacterRange: range];
}
}
/* Set the new selected range */
_selected_range = range;
/* FIXME: when and if to restart timer <and where to stop it before> */
[self updateInsertionPointStateAndRestartTimer: !flag];
if (flag == NO)
{
[self updateFontPanel];
/* Insertion Point */
if (range.length)
{
// Store the selected text in the selection pasteboard
[self copySelection];
if (_insertionPointTimer != nil)
{
[_insertionPointTimer invalidate];
DESTROY (_insertionPointTimer);
_drawInsertionPointNow = YES;
}
}
else /* no selection, only insertion point */
{
if (_tf.is_rich_text && [_textStorage length])
{
NSDictionary *dict;
if (range.location > 0)
{
/* If the insertion point is after a bold word, for
example, we need to use bold for further
insertions - this is why we take the attributes
from range.location - 1. */
dict = [_textStorage attributesAtIndex: (range.location - 1)
effectiveRange: NULL];
}
else
{
/* Unless we are at the beginning of text - we use the
first valid attributes then */
dict = [_textStorage attributesAtIndex: range.location
effectiveRange: NULL];
}
[self setTypingAttributes: dict];
}
}
}
if (_window != nil)
{
NSRange overlap;
if (flag == NO)
{
/* Make the selected range visible */
[self scrollRangeToVisible: range];
}
/* Try to optimize for overlapping ranges */
overlap = NSIntersectionRange (oldRange, range);
if (overlap.length)
{
if (range.location != oldDisplayedRange.location)
{
NSRange r;
r = MakeRangeFromAbs (MIN (range.location,
oldDisplayedRange.location),
MAX (range.location,
oldDisplayedRange.location));
[self setNeedsDisplayInRect: [self rectForCharacterRange: r]
avoidAdditionalLayout: YES];
}
if (NSMaxRange (range) != NSMaxRange (oldDisplayedRange))
{
NSRange r;
r = MakeRangeFromAbs (MIN (NSMaxRange (range),
NSMaxRange (oldDisplayedRange)),
MAX (NSMaxRange (range),
NSMaxRange (oldDisplayedRange)));
[self setNeedsDisplayInRect: [self rectForCharacterRange: r]
avoidAdditionalLayout: YES];
}
}
else
{
[self setNeedsDisplayInRect: [self rectForCharacterRange: range]
avoidAdditionalLayout: YES];
[self setNeedsDisplayInRect: [self rectForCharacterRange:
oldDisplayedRange]
avoidAdditionalLayout: YES];
}
}
[self setSelectionGranularity: NSSelectByCharacter];
/* TODO: Remove the marking from marked text if the new selection is
greater than the marked region. */
if (flag == NO)
{
NSDictionary *userInfo;
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithBytes: &oldRange
objCType: @encode(NSRange)],
NSOldSelectedCharacterRange, nil];
[nc postNotificationName: NSTextViewDidChangeSelectionNotification
object: _notifObject userInfo: userInfo];
}
}
- (NSSelectionAffinity) selectionAffinity
{
return NSSelectionAffinityDownstream;
}
- (void) setSelectionGranularity: (NSSelectionGranularity)granularity
{
_selectionGranularity = granularity;
}
- (NSSelectionGranularity) selectionGranularity
{
return _selectionGranularity;
}
- (void) setInsertionPointColor: (NSColor*)aColor
{
ASSIGN (_caret_color, aColor);
}
- (NSColor*) insertionPointColor
{
return _caret_color;
}
- (void) updateInsertionPointStateAndRestartTimer: (BOOL)flag
{
/* Update insertion point rect */
NSRange charRange;
NSRange glyphRange;
unsigned glyphIndex;
NSRect rect;
/* Simple case - no insertion point */
if ((_selected_range.length > 0) || _selected_range.location == NSNotFound)
{
if (_insertionPointTimer != nil)
{
[_insertionPointTimer invalidate];
DESTROY (_insertionPointTimer);
_drawInsertionPointNow = YES;
}
/* FIXME: horizontal position of insertion point */
_originalInsertPoint = 0;
return;
}
charRange = NSMakeRange (_selected_range.location, 0);
glyphRange = [_layoutManager glyphRangeForCharacterRange: charRange
actualCharacterRange: NULL];
glyphIndex = glyphRange.location;
rect = [_layoutManager lineFragmentRectForGlyphAtIndex: glyphIndex
effectiveRange: NULL];
if ([self selectionAffinity] != NSSelectionAffinityUpstream)
{
/* Standard case - draw the insertion point just before the
associated glyph index */
NSPoint loc = [_layoutManager locationForGlyphAtIndex: glyphIndex];
rect.origin.x += loc.x;
}
else /* _affinity == NSSelectionAffinityUpstream - non standard */
{
/* FIXME - THIS DOES NOT WORK - as a consequence,
NSSelectionAffinityUpstream DOES NOT WORK */
/* Check - if the previous glyph is on another line */
/* FIXME: Don't know how to do this check, this is a hack and
DOES NOT WORK - clearly this code should be inside the layout
manager anyway */
NSRect rect2;
rect2 = [_layoutManager lineFragmentRectForGlyphAtIndex: glyphIndex - 1
effectiveRange: NULL];
if (NSMinY (rect2) < NSMinY (rect))
{
/* Then we need to draw the insertion point just after the
previous glyph - DOES NOT WORK */
glyphRange = NSMakeRange (glyphIndex - 1, 1);
rect = [_layoutManager boundingRectForGlyphRange: glyphRange
inTextContainer:_textContainer];
rect.origin.x = NSMaxX (rect) - 1;
}
else /* Else, standard case again */
{
NSPoint loc = [_layoutManager locationForGlyphAtIndex: glyphIndex];
rect.origin.x += loc.x;
}
}
rect.size.width = 1;
if (rect.size.height == 0)
{
rect.size.height = 12;
}
_insertionPointRect = rect;
/* Remember horizontal position of insertion point */
_originalInsertPoint = _insertionPointRect.origin.x;
if (flag)
{
/* Start blinking timer if not yet started */
if (_insertionPointTimer == nil && [self shouldDrawInsertionPoint])
{
_insertionPointTimer = [NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: @selector(_blink:)
userInfo: nil
repeats: YES];
RETAIN (_insertionPointTimer);
}
}
else
{
if (_insertionPointTimer != nil)
{
[_insertionPointTimer invalidate];
DESTROY (_insertionPointTimer);
}
}
}
- (void) setSelectedTextAttributes: (NSDictionary *)attributes
{
ASSIGN (_selectedTextAttributes, attributes);
}
- (NSDictionary *) selectedTextAttributes
{
return _selectedTextAttributes;
}
- (NSRange) markedRange
{
// calculate
return NSMakeRange (NSNotFound, 0);
}
- (void) setMarkedTextAttributes: (NSDictionary*)attributes
{
ASSIGN (_markedTextAttributes, attributes);
}
- (NSDictionary*) markedTextAttributes
{
return _markedTextAttributes;
}
- (void) alignJustified: (id)sender
{
[self setAlignment: NSJustifiedTextAlignment
range: [self rangeForUserParagraphAttributeChange]];
}
- (void) changeColor: (id)sender
{
NSColor *aColor = (NSColor*)[sender color];
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
// sets the color for the selected range.
[self setTextColor: aColor range: aRange];
}
- (void) setAlignment: (NSTextAlignment)alignment range: (NSRange)aRange
{
NSParagraphStyle *style;
NSMutableParagraphStyle *mstyle;
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage setAlignment: alignment
range: aRange];
[_textStorage endEditing];
[self didChangeText];
// Set the typing attributes
style = [_typingAttributes objectForKey: NSParagraphStyleAttributeName];
if (style == nil)
style = [NSParagraphStyle defaultParagraphStyle];
mstyle = [style mutableCopy];
[mstyle setAlignment: alignment];
// FIXME: Should use setTypingAttributes
[_typingAttributes setObject: mstyle forKey: NSParagraphStyleAttributeName];
RELEASE (mstyle);
}
- (void) insertText: (NSString*) insertString
{
NSRange insertRange = [self rangeForUserTextChange];
if (insertRange.location == NSNotFound)
return;
if (_tf.is_rich_text)
{
[self replaceRange: insertRange
withAttributedString: AUTORELEASE([[NSAttributedString alloc]
initWithString: insertString
attributes: _typingAttributes])];
}
else
{
[self replaceCharactersInRange: insertRange
withString: insertString];
}
// move cursor <!> [self selectionRangeForProposedRange: ]
[self setSelectedRange:
NSMakeRange (insertRange.location + [insertString length], 0)];
}
- (void) setTypingAttributes: (NSDictionary*)dict
{
if (dict == nil)
{
dict = [isa defaultTypingAttributes];
}
if ([dict isKindOfClass: [NSMutableDictionary class]] == NO)
{
RELEASE (_typingAttributes);
_typingAttributes = [[NSMutableDictionary alloc]
initWithDictionary: dict];
}
else
{
ASSIGN (_typingAttributes, (NSMutableDictionary*)dict);
}
[self updateFontPanel];
[self updateRuler];
}
- (NSDictionary*) typingAttributes
{
return [NSDictionary dictionaryWithDictionary: _typingAttributes];
}
- (void) useStandardKerning: (id)sender
{
// rekern for selected range if rich text, else rekern entire document.
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage removeAttribute: NSKernAttributeName
range: aRange];
[_textStorage endEditing];
[self didChangeText];
}
- (void) lowerBaseline: (id)sender
{
id value;
float sValue;
NSRange effRange;
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
// We take the value form the first character and use it for the whole range
value = [_textStorage attribute: NSBaselineOffsetAttributeName
atIndex: aRange.location
effectiveRange: &effRange];
if (value != nil)
sValue = [value floatValue] + 1.0;
else
sValue = 1.0;
[_textStorage addAttribute: NSBaselineOffsetAttributeName
value: [NSNumber numberWithFloat: sValue]
range: aRange];
}
- (void) raiseBaseline: (id)sender
{
id value;
float sValue;
NSRange effRange;
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
// We take the value form the first character and use it for the whole range
value = [_textStorage attribute: NSBaselineOffsetAttributeName
atIndex: aRange.location
effectiveRange: &effRange];
if (value != nil)
sValue = [value floatValue] - 1.0;
else
sValue = -1.0;
[_textStorage addAttribute: NSBaselineOffsetAttributeName
value: [NSNumber numberWithFloat: sValue]
range: aRange];
[_textStorage endEditing];
[self didChangeText];
}
- (void) turnOffKerning: (id)sender
{
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage addAttribute: NSKernAttributeName
value: [NSNumber numberWithFloat: 0.0]
range: aRange];
[_textStorage endEditing];
[self didChangeText];
}
- (void) loosenKerning: (id)sender
{
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
// FIXME: Should use the current kerning and work relative to point size
[_textStorage addAttribute: NSKernAttributeName
value: [NSNumber numberWithFloat: 1.0]
range: aRange];
[_textStorage endEditing];
[self didChangeText];
}
- (void) tightenKerning: (id)sender
{
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
// FIXME: Should use the current kerning and work relative to point size
[_textStorage addAttribute: NSKernAttributeName
value: [NSNumber numberWithFloat: -1.0]
range: aRange];
[_textStorage endEditing];
[self didChangeText];
}
- (void) useStandardLigatures: (id)sender
{
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage addAttribute: NSLigatureAttributeName
value: [NSNumber numberWithInt: 1]
range: aRange];
[_textStorage endEditing];
[self didChangeText];
}
- (void) turnOffLigatures: (id)sender
{
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage addAttribute: NSLigatureAttributeName
value: [NSNumber numberWithInt: 0]
range: aRange];
[_textStorage endEditing];
[self didChangeText];
}
- (void) useAllLigatures: (id)sender
{
NSRange aRange = [self rangeForUserCharacterAttributeChange];
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
[_textStorage addAttribute: NSLigatureAttributeName
value: [NSNumber numberWithInt: 2]
range: aRange];
[_textStorage endEditing];
[self didChangeText];
}
- (void) toggleTraditionalCharacterShape: (id)sender
{
// TODO
NSLog(@"Method %s is not implemented for class %s",
"toggleTraditionalCharacterShape:", "NSTextView");
}
- (void) clickedOnLink: (id)link
atIndex: (unsigned int)charIndex
{
if (_delegate != nil)
{
SEL selector = @selector(textView:clickedOnLink:atIndex:);
if ([_delegate respondsToSelector: selector])
{
[_delegate textView: self clickedOnLink: link atIndex: charIndex];
}
}
}
/*
The text is inserted at the insertion point if there is one, otherwise
replacing the selection.
*/
- (void) pasteAsPlainText: (id)sender
{
[self readSelectionFromPasteboard: [NSPasteboard generalPasteboard]
type: NSStringPboardType];
}
- (void) pasteAsRichText: (id)sender
{
[self readSelectionFromPasteboard: [NSPasteboard generalPasteboard]
type: NSRTFPboardType];
}
- (void) updateFontPanel
{
/* Update fontPanel only if told so */
if (_tf.uses_font_panel)
{
NSRange longestRange;
NSFontManager *fm = [NSFontManager sharedFontManager];
NSFont *currentFont;
if (_selected_range.length > 0) /* Multiple chars selection */
{
currentFont = [_textStorage attribute: NSFontAttributeName
atIndex: _selected_range.location
longestEffectiveRange: &longestRange
inRange: _selected_range];
[fm setSelectedFont: currentFont
isMultiple: !NSEqualRanges (longestRange, _selected_range)];
}
else /* Just Insertion Point. */
{
currentFont = [_typingAttributes objectForKey: NSFontAttributeName];
[fm setSelectedFont: currentFont isMultiple: NO];
}
}
}
- (void) updateRuler
{
/* Update ruler view only if told so */
if (_tf.uses_ruler && _tf.is_ruler_visible)
{
NSParagraphStyle *paraStyle;
NSScrollView *sv = [self enclosingScrollView];
NSRulerView *rv = [sv horizontalRulerView];
NSArray *makers;
if (_selected_range.length > 0) /* Multiple chars selection */
{
paraStyle = [_textStorage attribute: NSParagraphStyleAttributeName
atIndex: _selected_range.location
effectiveRange: NULL];
}
else
{
paraStyle = [_typingAttributes objectForKey:
NSParagraphStyleAttributeName];
}
makers = [_layoutManager rulerMarkersForTextView: self
paragraphStyle: paraStyle
ruler: rv];
[rv setMarkers: makers];
}
}
- (NSArray*) acceptableDragTypes
{
return [self readablePasteboardTypes];
}
- (void) updateDragTypeRegistration
{
// FIXME: Should change registration for all our text views
if (_tf.is_editable && _tf.is_rich_text)
[self registerForDraggedTypes: [self acceptableDragTypes]];
else
[self unregisterDraggedTypes];
}
- (BOOL) shouldChangeTextInRange: (NSRange)affectedCharRange
replacementString: (NSString*)replacementString
{
if (BEGAN_EDITING == NO)
{
if (([_delegate respondsToSelector: @selector(textShouldBeginEditing:)])
&& ([_delegate textShouldBeginEditing: _notifObject] == NO))
return NO;
SET_BEGAN_EDITING (YES);
[nc postNotificationName: NSTextDidBeginEditingNotification
object: _notifObject];
}
if (_tvf.delegate_responds_to_should_change)
{
return [_delegate shouldChangeTextInRange: affectedCharRange
replacementString: replacementString];
}
return YES;
}
- (void) didChangeText
{
[nc postNotificationName: NSTextDidChangeNotification
object: _notifObject];
}
- (void) setSmartInsertDeleteEnabled: (BOOL)flag
{
_tvf.smart_insert_delete = flag;
}
- (BOOL) smartInsertDeleteEnabled
{
return _tvf.smart_insert_delete;
}
- (NSRange) smartDeleteRangeForProposedRange: (NSRange)proposedCharRange
{
// FIXME.
return proposedCharRange;
}
- (NSString *)smartInsertAfterStringForString: (NSString *)aString
replacingRange: (NSRange)charRange
{
// FIXME.
return nil;
}
- (NSString *)smartInsertBeforeStringForString: (NSString *)aString
replacingRange: (NSRange)charRange
{
// FIXME.
return nil;
}
- (void) smartInsertForString: (NSString*)aString
replacingRange: (NSRange)charRange
beforeString: (NSString**)beforeString
afterString: (NSString**)afterString
{
/* Determines whether whitespace needs to be added around aString to
preserve proper spacing and punctuation when it's inserted into the
receiver's text over charRange. Returns by reference in beforeString and
afterString any whitespace that should be added, unless either or both is
nil. Both are returned as nil if aString is nil or if smart insertion and
deletion is disabled.
As part of its implementation, this method calls
smartInsertAfterStringForString: replacingRange: and
smartInsertBeforeStringForString: replacingRange: .To change this method's
behavior, override those two methods instead of this one.
NSTextView uses this method as necessary. You can also use it in
implementing your own methods that insert text. To do so, invoke this
method with the proper arguments, then insert beforeString, aString, and
afterString in order over charRange. */
if (beforeString)
*beforeString = [self smartInsertBeforeStringForString: aString
replacingRange: charRange];
if (afterString)
*afterString = [self smartInsertAfterStringForString: aString
replacingRange: charRange];
}
/*
* Handling Events
*/
- (void) mouseDown: (NSEvent*)theEvent
{
NSSelectionAffinity affinity = [self selectionAffinity];
NSSelectionGranularity granularity = NSSelectByCharacter;
NSRange chosenRange, proposedRange;
NSPoint point, startPoint;
NSEvent *currentEvent;
unsigned startIndex;
unsigned mask;
/* If non selectable then ignore the mouse down. */
if (_tf.is_selectable == NO)
{
return;
}
/* Otherwise, NSWindow has already made us first responder (if
possible) */
startPoint = [self convertPoint: [theEvent locationInWindow] fromView: nil];
startIndex = [self characterIndexForPoint: startPoint];
if ([theEvent modifierFlags] & NSShiftKeyMask)
{
/* Shift-click is for extending an existing selection using
the existing granularity */
granularity = _selectionGranularity;
/* Compute the new selection */
proposedRange = NSMakeRange (startIndex, 0);
proposedRange = NSUnionRange (_selected_range, proposedRange);
proposedRange = [self selectionRangeForProposedRange: proposedRange
granularity: granularity];
/* Merge it with the old one */
proposedRange = NSUnionRange (_selected_range, proposedRange);
/* Now decide what happens if the user shift-drags. The range
will be based in startIndex, so we need to adjust it. */
if (startIndex <= _selected_range.location)
{
startIndex = NSMaxRange (proposedRange);
}
else
{
startIndex = proposedRange.location;
}
}
else /* No shift */
{
switch ([theEvent clickCount])
{
case 1: granularity = NSSelectByCharacter;
break;
case 2: granularity = NSSelectByWord;
break;
case 3: granularity = NSSelectByParagraph;
break;
}
proposedRange = NSMakeRange (startIndex, 0);
/* We manage clicks on attachments and links only on the first
click, so that if you double-click on them, only the first
click gets sent to them; the other clicks select by
word/paragraph as usual. */
if (granularity == NSSelectByCharacter)
{
if ([_textStorage containsAttachments])
{
NSTextAttachment *attachment;
/* Check if the click was on an attachment cell. */
attachment = [_textStorage attribute: NSAttachmentAttributeName
atIndex: startIndex
effectiveRange: NULL];
if (attachment != nil)
{
id <NSTextAttachmentCell> cell = [attachment attachmentCell];
if (cell != nil)
{
/* FIXME: Where to get the cellFrame? */
NSRect cellFrame = NSMakeRect(0, 0, 0, 0);
/* FIXME: What about the insertion point ? */
if ([cell wantsToTrackMouseForEvent: theEvent
inRect: cellFrame
ofView: self
atCharacterIndex: startIndex]
&& [cell trackMouse: theEvent
inRect: cellFrame
ofView: self
atCharacterIndex: startIndex
untilMouseUp: NO])
{
return;
}
}
}
}
/* This is the code for handling click event on a link (a link
is some chars with the NSLinkAttributeName set to something
which is not-null, a NSURL object usually). */
{
/* What exactly is this link object, it's up to the
programmer who is using the NSTextView and who
originally created the link object and saved it under
the NSLinkAttributeName in the text. Normally, a NSURL
object is used.*/
id link = [_textStorage attribute: NSLinkAttributeName
atIndex: startIndex
effectiveRange: NULL];
if (link != nil && _delegate != nil)
{
SEL selector = @selector(textView:clickedOnLink:atIndex:);
if ([_delegate respondsToSelector: selector])
{
/* Move the insertion point over the link. */
chosenRange = [self selectionRangeForProposedRange:
proposedRange
granularity: granularity];
[self setSelectedRange: chosenRange affinity: affinity
stillSelecting: NO];
[self displayIfNeeded];
/* Now 'activate' the link. The _delegate returns
YES if it handles the click, NO if it doesn't
-- and if it doesn't, we need to pass the click
to the next responder. */
if ([_delegate textView: self clickedOnLink: link
atIndex: startIndex])
{
return;
}
else
{
[super mouseDown: theEvent];
return;
}
}
}
}
}
}
chosenRange = [self selectionRangeForProposedRange: proposedRange
granularity: granularity];
[self setSelectedRange: chosenRange affinity: affinity
stillSelecting: YES];
/* Do an immediate redisplay for visual feedback */
[self displayIfNeeded];
/* Enter modal loop tracking the mouse */
mask = NSLeftMouseDraggedMask | NSLeftMouseUpMask;
for (currentEvent = [_window nextEventMatchingMask: mask];
[currentEvent type] != NSLeftMouseUp;
currentEvent = [_window nextEventMatchingMask: mask])
{
BOOL didScroll = [self autoscroll: currentEvent];
point = [self convertPoint: [currentEvent locationInWindow]
fromView: nil];
proposedRange = MakeRangeFromAbs ([self characterIndexForPoint: point],
startIndex);
chosenRange = [self selectionRangeForProposedRange: proposedRange
granularity: granularity];
[self setSelectedRange: chosenRange affinity: affinity
stillSelecting: YES];
if (didScroll)
{
/* FIXME: Only redisplay where needed, and avoid relayout */
[self setNeedsDisplay: YES];
}
/* Do an immediate redisplay for visual feedback */
[self displayIfNeeded];
}
NSDebugLog(@"chosenRange. location = %d, length = %d\n",
(int)chosenRange.location, (int)chosenRange.length);
[self setSelectedRange: chosenRange affinity: affinity
stillSelecting: NO];
/* Ahm - this shouldn't really be needed but... */
[self displayIfNeeded];
/* Remember granularity till a new selection destroys the memory */
[self setSelectionGranularity: granularity];
}
- (void) keyDown: (NSEvent*)theEvent
{
// If not editable, don't recognize the key down
if (_tf.is_editable == NO)
{
[super keyDown: theEvent];
}
else
{
[self interpretKeyEvents: [NSArray arrayWithObject: theEvent]];
}
}
- (void) insertNewline: (id)sender
{
if (_tf.is_field_editor)
{
[self _illegalMovement: NSReturnTextMovement];
return;
}
[self insertText: @"\n"];
}
- (void) insertTab: (id)sender
{
if (_tf.is_field_editor)
{
[self _illegalMovement: NSTabTextMovement];
return;
}
[self insertText: @"\t"];
}
- (void) insertBacktab: (id)sender
{
if (_tf.is_field_editor)
{
[self _illegalMovement: NSBacktabTextMovement];
return;
}
//[self insertText: @"\t"];
}
- (void) deleteForward: (id)sender
{
NSRange range = [self rangeForUserTextChange];
if (range.location == NSNotFound)
{
return;
}
/* Manage case of insertion point - implicitly means to delete following
character */
if (range.length == 0)
{
if (range.location != [_textStorage length])
{
/* Not at the end of text -- delete following character */
range.length = 1;
}
else
{
/* At the end of text - TODO: Make beeping or not beeping
configurable vie User Defaults */
NSBeep ();
return;
}
}
if (![self shouldChangeTextInRange: range replacementString: @""])
{
return;
}
[_textStorage beginEditing];
[_textStorage deleteCharactersInRange: range];
[_textStorage endEditing];
[self didChangeText];
/* The new selected range is just the insertion point at the beginning
of deleted range */
[self setSelectedRange: NSMakeRange (range.location, 0)];
}
- (void) deleteBackward: (id)sender
{
NSRange range = [self rangeForUserTextChange];
if (range.location == NSNotFound)
{
return;
}
/* Manage case of insertion point - implicitly means to delete
previous character */
if (range.length == 0)
{
if (range.location != 0)
{
/* Not at the beginning of text -- delete previous character */
range.location -= 1;
range.length = 1;
}
else
{
/* At the beginning of text - TODO: Make beeping or not
beeping configurable via User Defaults */
NSBeep ();
return;
}
}
if (![self shouldChangeTextInRange: range replacementString: @""])
{
return;
}
[_textStorage beginEditing];
[_textStorage deleteCharactersInRange: range];
[_textStorage endEditing];
[self didChangeText];
/* The new selected range is just the insertion point at the beginning
of deleted range */
[self setSelectedRange: NSMakeRange (range.location, 0)];
}
- (void) moveUp: (id)sender
{
float originalInsertionPoint;
float startingY;
unsigned newLocation;
if (_tf.is_field_editor)
return;
/* Do nothing if we are at beginning of text */
if (_selected_range.location == 0)
{
return;
}
/* Read from memory the horizontal position we aim to move the cursor
at on the next line */
originalInsertionPoint = _originalInsertPoint;
/* Ask the layout manager to compute where to go */
startingY = NSMidY (_insertionPointRect);
newLocation = [_layoutManager
_charIndexForInsertionPointMovingFromY: startingY
bestX: originalInsertionPoint
up: YES
textContainer: _textContainer];
/* Move the insertion point */
[self setSelectedRange: NSMakeRange (newLocation, 0)];
/* Restore the _originalInsertPoint (which was changed
by setSelectedRange:) because we don't want it to change between
moveUp:/moveDown: operations. */
_originalInsertPoint = originalInsertionPoint;
}
- (void) moveDown: (id)sender
{
float originalInsertionPoint;
float startingY;
unsigned newLocation;
if (_tf.is_field_editor)
return;
/* Do nothing if we are at end of text */
if (_selected_range.location == [_textStorage length])
{
return;
}
/* Read from memory the horizontal position we aim to move the cursor
at on the next line */
originalInsertionPoint = _originalInsertPoint;
/* Ask the layout manager to compute where to go */
startingY = NSMidY (_insertionPointRect);
newLocation = [_layoutManager
_charIndexForInsertionPointMovingFromY: startingY
bestX: originalInsertionPoint
up: NO
textContainer: _textContainer];
/* Move the insertion point */
[self setSelectedRange: NSMakeRange (newLocation, 0)];
/* Restore the _originalInsertPoint (which was changed
by setSelectedRange:) because we don't want it to change between
moveUp:/moveDown: operations. */
_originalInsertPoint = originalInsertionPoint;
}
- (void) moveLeft: (id)sender
{
unsigned newLocation;
/* Do nothing if we are at beginning of text with no selection */
if (_selected_range.location == 0 && _selected_range.length == 0)
return;
if (_selected_range.location == 0)
{
newLocation = 0;
}
else
{
newLocation = _selected_range.location - 1;
}
[self setSelectedRange: NSMakeRange (newLocation, 0)];
}
- (void) moveRight: (id)sender
{
unsigned int length = [_textStorage length];
unsigned newLocation;
/* Do nothing if we are at end of text */
if (_selected_range.location == length)
return;
newLocation = MIN (NSMaxRange (_selected_range) + 1, length);
[self setSelectedRange: NSMakeRange (newLocation, 0)];
}
- (void) moveBackwardAndModifySelection: (id)sender
{
NSRange newRange;
/* Do nothing if we are at beginning of text. */
if (_selected_range.location == 0)
{
return;
}
/* Turn to select by character. */
[self setSelectionGranularity: NSSelectByCharacter];
/* Extend the selection on the left. */
newRange = NSMakeRange (_selected_range.location - 1,
_selected_range.length + 1);
newRange = [self selectionRangeForProposedRange: newRange
granularity: NSSelectByCharacter];
[self setSelectedRange: newRange];
}
- (void) moveForwardAndModifySelection: (id)sender
{
unsigned int length = [_textStorage length];
NSRange newRange;
/* Do nothing if we are at end of text */
if (_selected_range.location == length)
return;
/* Turn to select by character. */
[self setSelectionGranularity: NSSelectByCharacter];
/* Extend the selection on the right. */
newRange = NSMakeRange (_selected_range.location,
_selected_range.length + 1);
newRange = [self selectionRangeForProposedRange: newRange
granularity: NSSelectByCharacter];
[self setSelectedRange: newRange];
}
- (void) moveWordBackward: (id)sender
{
unsigned newLocation;
newLocation = [_textStorage nextWordFromIndex: _selected_range.location
forward: NO];
[self setSelectedRange: NSMakeRange (newLocation, 0)];
}
- (void) moveWordForward: (id)sender
{
unsigned newLocation;
newLocation = [_textStorage nextWordFromIndex: _selected_range.location
forward: YES];
[self setSelectedRange: NSMakeRange (newLocation, 0)];
}
- (void) moveWordBackwardAndModifySelection: (id)sender
{
unsigned newLocation;
NSRange newRange;
[self setSelectionGranularity: NSSelectByWord];
newLocation = [_textStorage nextWordFromIndex: _selected_range.location
forward: NO];
newRange = NSMakeRange (newLocation,
NSMaxRange (_selected_range) - newLocation);
newRange = [self selectionRangeForProposedRange: newRange
granularity: NSSelectByCharacter];
[self setSelectedRange: newRange];
}
- (void) moveWordForwardAndModifySelection: (id)sender
{
unsigned newMaxRange;
NSRange newRange;
[self setSelectionGranularity: NSSelectByWord];
newMaxRange = [_textStorage nextWordFromIndex: NSMaxRange (_selected_range)
forward: YES];
newRange = NSMakeRange (_selected_range.location,
newMaxRange - _selected_range.location);
newRange = [self selectionRangeForProposedRange: newRange
granularity: NSSelectByCharacter];
[self setSelectedRange: newRange];
}
- (void) moveToBeginningOfDocument: (id)sender
{
[self setSelectedRange: NSMakeRange (0, 0)];
}
- (void) moveToBeginningOfParagraph: (id)sender
{
NSRange aRange;
aRange = [[_textStorage string] lineRangeForRange: _selected_range];
[self setSelectedRange: NSMakeRange (aRange.location, 0)];
}
- (void) moveToBeginningOfLine: (id)sender
{
NSRange aRange;
NSRect ignored;
/* We do nothing if we are at the beginning of the text. */
if (_selected_range.location == 0)
{
return;
}
ignored = [_layoutManager lineFragmentRectForGlyphAtIndex:
_selected_range.location
effectiveRange: &aRange];
[self setSelectedRange: NSMakeRange (aRange.location, 0)];
}
- (void) moveToEndOfDocument: (id)sender
{
unsigned length = [_textStorage length];
if (length > 0)
{
[self setSelectedRange: NSMakeRange (length, 0)];
}
else
{
[self setSelectedRange: NSMakeRange (0, 0)];
}
}
- (void) moveToEndOfParagraph: (id)sender
{
NSRange aRange;
unsigned newLocation;
unsigned maxRange;
aRange = [[_textStorage string] lineRangeForRange: _selected_range];
maxRange = NSMaxRange (aRange);
if (maxRange == [_textStorage length])
{
/* End of text is special - we want the insertion point to
appear *after* the last character, which means as if before
the next (virtual) character after the end of text. */
//FIXME This is only true if the last character is not newline!
newLocation = maxRange;
}
else if (maxRange == 0)
{
/* Beginning of text is special only for technical reasons -
since maxRange is an unsigned, we can't safely subtract 1
from it if it is 0. */
newLocation = maxRange;
}
else
{
/* Else, we want the insertion point to appear before the last
character in the paragraph range. Normally the last character in
the paragraph range is a newline. */
newLocation = maxRange - 1;
if (newLocation < aRange.location)
{
newLocation = aRange.location;
}
}
[self setSelectedRange: NSMakeRange (newLocation, 0) ];
}
- (void) moveToEndOfLine: (id)sender
{
NSRect ignored;
NSRange line, glyphs;
unsigned newLocation;
unsigned maxRange;
/* We do nothing if we are at the end of the text. */
if (_selected_range.location == [_textStorage length])
{
return;
}
ignored = [_layoutManager lineFragmentRectForGlyphAtIndex:
_selected_range.location
effectiveRange: &glyphs];
line = [_layoutManager characterRangeForGlyphRange: glyphs
actualGlyphRange: NULL];
maxRange = NSMaxRange (line);
if (maxRange == [_textStorage length])
{
/* End of text is special - we want the insertion point to
appear *after* the last character, which means as if before
the next (virtual) character after the end of text. */
newLocation = maxRange;
}
else if (maxRange == 0)
{
/* Beginning of text is special only for technical reasons -
since maxRange is an unsigned, we can't safely subtract 1
from it if it is 0. */
newLocation = maxRange;
}
else
{
/* Else, we want the insertion point to appear before the last
character in the line range. Normally the last character in
the line range is a space or a newline. */
newLocation = maxRange - 1;
if (newLocation < line.location)
{
newLocation = line.location;
}
}
[self setSelectedRange: NSMakeRange (newLocation, 0) ];
}
- (void) pageDown: (id)sender
{
// TODO
NSLog(@"Method %s is not implemented for class %s",
"pageDown:", "NSTextView");
}
- (void) pageUp: (id)sender
{
// TODO
NSLog(@"Method %s is not implemented for class %s",
"pageUp:", "NSTextView");
}
- (void) scrollLineDown: (id)sender
{
// TODO
NSLog(@"Method %s is not implemented for class %s",
"scrollLineDown:", "NSTextView");
}
- (void) scrollLineUp: (id)sender
{
// TODO
NSLog(@"Method %s is not implemented for class %s",
"scrollLineUp:", "NSTextView");
}
- (void) scrollPageDown: (id)sender
{
// TODO
NSLog(@"Method %s is not implemented for class %s",
"scrollPageDown:", "NSTextView");
}
- (void) scrollPageUp: (id)sender
{
// TODO
NSLog(@"Method %s is not implemented for class %s",
"scrollPageUp:", "NSTextView");
}
/* -selectAll: inherited from NSText */
- (void) selectLine: (id)sender
{
if ([_textStorage length] > 0)
{
NSRange aRange;
NSRect ignored;
ignored = [_layoutManager lineFragmentRectForGlyphAtIndex:
_selected_range.location
effectiveRange: &aRange];
[self setSelectedRange: aRange];
}
}
/* The following method is bound to 'Control-t', and must work like
* pressing 'Control-t' inside Emacs. For example, say that I type
* 'Nicoal' in a NSTextView. Then, I press 'Control-t'. This should
* swap the last two characters which were inserted, thus swapping the
* 'a' and the 'l', and changing the text to read 'Nicola'. */
- (void) transpose: (id)sender
{
NSRange range;
NSString *string;
NSString *replacementString;
unichar chars[2];
unichar tmp;
/* Do nothing if we are at beginning of text. */
if (_selected_range.location < 2)
{
return;
}
range = NSMakeRange (_selected_range.location - 2, 2);
/* Get the two chars. */
string = [_textStorage string];
chars[0] = [string characterAtIndex: (_selected_range.location - 2)];
chars[1] = [string characterAtIndex: (_selected_range.location - 1)];
/* Swap them. */
tmp = chars[0];
chars[0] = chars[1];
chars[1] = tmp;
/* Replace the original chars with the swapped ones. */
replacementString = [NSString stringWithCharacters: chars length: 2];
[self replaceCharactersInRange: range withString: replacementString];
}
- (BOOL) acceptsFirstResponder
{
if (_tf.is_selectable)
{
return YES;
}
else
{
return NO;
}
}
- (BOOL) resignFirstResponder
{
if (_insertionPointTimer != nil)
{
[_insertionPointTimer invalidate];
DESTROY (_insertionPointTimer);
_drawInsertionPointNow = NO;
}
if (_tvf.multiple_textviews == YES)
{
id futureFirstResponder;
NSArray *textContainers;
int i, count;
futureFirstResponder = [_window _futureFirstResponder];
textContainers = [_layoutManager textContainers];
count = [textContainers count];
for (i = 0; i < count; i++)
{
NSTextContainer *container;
NSTextView *view;
container = (NSTextContainer *)[textContainers objectAtIndex: i];
view = [container textView];
if (view == futureFirstResponder)
{
/* NB: We do not reset the BEGAN_EDITING flag so that no
spurious notification is generated. */
return YES;
}
}
}
/* NB: Possible change: ask always - not only if editable - but we
need to change NSTextField etc to allow this. */
if ((_tf.is_editable)
&& ([_delegate respondsToSelector: @selector(textShouldEndEditing:)])
&& ([_delegate textShouldEndEditing: self] == NO))
{
return NO;
}
/* Add any clean-up stuff here */
if ([self shouldDrawInsertionPoint])
{
[self updateInsertionPointStateAndRestartTimer: NO];
}
SET_BEGAN_EDITING (NO);
/* NB: According to the doc (and to the tradition), we post this
notification even if no real editing was actually done (only
selection of text) [Note: in this case, no editing was started,
so the notification does not come after a
NSTextDidBeginEditingNotification!]. The notification only means
that we are resigning first responder status. This makes sense
because many objects inside the gui need this notification anyway
- typically, it is needed to remove a field editor (editable or
not) when the user presses TAB to move to the next view. Anyway
yes, the notification name is misleading. */
[nc postNotificationName: NSTextDidEndEditingNotification
object: _notifObject];
return YES;
}
- (BOOL) becomeFirstResponder
{
if (_tf.is_selectable == NO)
{
return NO;
}
/* NB: Notifications (NSTextBeginEditingNotification etc) are managed
on the first time the user tries to edit us. */
/* Draw selection, update insertion point */
if ([self shouldDrawInsertionPoint])
{
[self updateInsertionPointStateAndRestartTimer: YES];
}
return YES;
}
- (void) drawRect: (NSRect)rect
{
/* TODO: Only do relayout if needed */
NSRange drawnRange = [_layoutManager glyphRangeForBoundingRect: rect
inTextContainer: _textContainer];
if (_tf.draws_background)
{
/* First paint the background with the color. This is necessary
* to remove markings of old glyphs. These would not be removed
* by the following call to the layout manager because that only
* paints the background of new glyphs. Depending on the
* situation, there might be no new glyphs where the old glyphs
* were! */
[_background_color set];
NSRectFill (rect);
/* Then draw the special background of the new glyphs. */
[_layoutManager drawBackgroundForGlyphRange: drawnRange
atPoint: _textContainerOrigin];
}
[_layoutManager drawGlyphsForGlyphRange: drawnRange
atPoint: _textContainerOrigin];
if ([self shouldDrawInsertionPoint])
{
unsigned location = _selected_range.location;
if (NSLocationInRange (location, drawnRange)
|| location == NSMaxRange (drawnRange))
{
if (_drawInsertionPointNow)
{
[self drawInsertionPointInRect: _insertionPointRect
color: _caret_color
turnedOn: YES];
}
}
}
}
/*
* Ruler Views
*/
- (void) rulerView: (NSRulerView*)aRulerView
didMoveMarker: (NSRulerMarker*)aMarker
{
NSTextTab *old_tab = [aMarker representedObject];
NSTextTab *new_tab = [[NSTextTab alloc] initWithType: [old_tab tabStopType]
location: [aMarker makerLocation]];
NSRange range = [self rangeForUserParagraphAttributeChange];
unsigned loc = range.location;
NSParagraphStyle *style;
NSMutableParagraphStyle *mstyle;
[_textStorage beginEditing];
while (loc < NSMaxRange(range))
{
id value;
BOOL copiedStyle = NO;
NSRange effRange;
NSRange newRange;
value = [_textStorage attribute: NSParagraphStyleAttributeName
atIndex: loc
effectiveRange: &effRange];
newRange = NSIntersectionRange (effRange, range);
if (value == nil)
{
value = [NSMutableParagraphStyle defaultParagraphStyle];
}
else
{
value = [value mutableCopy];
copiedStyle = YES;
}
[value removeTabStop: old_tab];
[value addTabStop: new_tab];
[_textStorage addAttribute: NSParagraphStyleAttributeName
value: value
range: newRange];
if (copiedStyle == YES)
{
RELEASE(value);
}
loc = NSMaxRange (effRange);
}
[_textStorage endEditing];
[self didChangeText];
// Set the typing attributes
style = [_typingAttributes objectForKey: NSParagraphStyleAttributeName];
if (style == nil)
style = [NSParagraphStyle defaultParagraphStyle];
mstyle = [style mutableCopy];
[mstyle removeTabStop: old_tab];
[mstyle addTabStop: new_tab];
// FIXME: Should use setTypingAttributes
[_typingAttributes setObject: mstyle forKey: NSParagraphStyleAttributeName];
RELEASE (mstyle);
[aMarker setRepresentedObject: new_tab];
RELEASE(new_tab);
}
- (void) rulerView: (NSRulerView*)aRulerView
didRemoveMarker: (NSRulerMarker*)aMarker
{
NSTextTab *tab = [aMarker representedObject];
NSRange range = [self rangeForUserParagraphAttributeChange];
unsigned loc = range.location;
NSParagraphStyle *style;
NSMutableParagraphStyle *mstyle;
[_textStorage beginEditing];
while (loc < NSMaxRange(range))
{
id value;
BOOL copiedStyle = NO;
NSRange effRange;
NSRange newRange;
value = [_textStorage attribute: NSParagraphStyleAttributeName
atIndex: loc
effectiveRange: &effRange];
newRange = NSIntersectionRange (effRange, range);
if (value == nil)
{
value = [NSMutableParagraphStyle defaultParagraphStyle];
}
else
{
value = [value mutableCopy];
copiedStyle = YES;
}
[value removeTabStop: tab];
[_textStorage addAttribute: NSParagraphStyleAttributeName
value: value
range: newRange];
if (copiedStyle == YES)
{
RELEASE(value);
}
loc = NSMaxRange (effRange);
}
[_textStorage endEditing];
[self didChangeText];
// Set the typing attributes
style = [_typingAttributes objectForKey: NSParagraphStyleAttributeName];
if (style == nil)
style = [NSParagraphStyle defaultParagraphStyle];
mstyle = [style mutableCopy];
[mstyle removeTabStop: tab];
// FIXME: Should use setTypingAttributes
[_typingAttributes setObject: mstyle forKey: NSParagraphStyleAttributeName];
RELEASE (mstyle);
}
- (void)rulerView:(NSRulerView *)ruler
didAddMarker:(NSRulerMarker *)marker
{
NSTextTab *tab = [marker representedObject];
NSRange range = [self rangeForUserParagraphAttributeChange];
unsigned loc = range.location;
NSParagraphStyle *style;
NSMutableParagraphStyle *mstyle;
[_textStorage beginEditing];
while (loc < NSMaxRange(range))
{
id value;
BOOL copiedStyle = NO;
NSRange effRange;
NSRange newRange;
value = [_textStorage attribute: NSParagraphStyleAttributeName
atIndex: loc
effectiveRange: &effRange];
newRange = NSIntersectionRange (effRange, range);
if (value == nil)
{
value = [NSMutableParagraphStyle defaultParagraphStyle];
}
else
{
value = [value mutableCopy];
copiedStyle = YES;
}
[value addTabStop: tab];
[_textStorage addAttribute: NSParagraphStyleAttributeName
value: value
range: newRange];
if (copiedStyle == YES)
{
RELEASE(value);
}
loc = NSMaxRange (effRange);
}
[_textStorage endEditing];
[self didChangeText];
// Set the typing attributes
style = [_typingAttributes objectForKey: NSParagraphStyleAttributeName];
if (style == nil)
style = [NSParagraphStyle defaultParagraphStyle];
mstyle = [style mutableCopy];
[mstyle addTabStop: tab];
// FIXME: Should use setTypingAttributes
[_typingAttributes setObject: mstyle forKey: NSParagraphStyleAttributeName];
RELEASE (mstyle);
}
- (void) rulerView: (NSRulerView*)aRulerView
handleMouseDown: (NSEvent*)theEvent
{
NSPoint point = [aRulerView convertPoint: [theEvent locationInWindow]
fromView: nil];
float location = point.x;
// FIXME This image does not exist.
NSRulerMarker *marker = [[NSRulerMarker alloc]
initWithRulerView: aRulerView
markerLocation: location
image: [NSImage imageNamed: @"common_LeftTabStop"]
imageOrigin: NSMakePoint(0, 0)];
NSTextTab *tab = [[NSTextTab alloc] initWithType: NSLeftTabStopType
location: location];
[marker setRepresentedObject: tab];
[aRulerView addMarker: marker];
RELEASE(marker);
AUTORELEASE(tab);
}
- (BOOL) rulerView: (NSRulerView*)aRulerView
shouldAddMarker: (NSRulerMarker*)aMarker
{
return [self shouldChangeTextInRange: [self rangeForUserParagraphAttributeChange]
replacementString: nil];
}
- (BOOL) rulerView: (NSRulerView*)aRulerView
shouldMoveMarker: (NSRulerMarker*)aMarker
{
return [self shouldChangeTextInRange: [self rangeForUserParagraphAttributeChange]
replacementString: nil];
}
- (BOOL) rulerView: (NSRulerView*)aRulerView
shouldRemoveMarker: (NSRulerMarker*)aMarker
{
return [(id)[aMarker representedObject] isKindOfClass: [NSTextTab class]];
}
- (float) rulerView: (NSRulerView*)aRulerView
willAddMarker: (NSRulerMarker*)aMarker
atLocation: (float)location
{
NSSize size = [_textContainer containerSize];
if (location < 0.0)
return 0.0;
if (location > size.width)
return size.width;
return location;
}
- (float) rulerView: (NSRulerView*)aRulerView
willMoveMarker: (NSRulerMarker*)aMarker
toLocation: (float)location
{
NSSize size = [_textContainer containerSize];
if (location < 0.0)
return 0.0;
if (location > size.width)
return size.width;
return location;
}
- (void) setDelegate: (id)anObject
{
SEL selector;
/* Code to allow sharing the delegate */
if (_tvf.multiple_textviews && (IS_SYNCHRONIZING_DELEGATES == NO))
{
/* Invoke setDelegate: on all the textviews which share this
delegate. */
NSArray *array;
int i, count;
IS_SYNCHRONIZING_DELEGATES = YES;
array = [_layoutManager textContainers];
count = [array count];
for (i = 0; i < count; i++)
{
NSTextView *view;
view = [(NSTextContainer *)[array objectAtIndex: i] textView];
[view setDelegate: anObject];
}
IS_SYNCHRONIZING_DELEGATES = NO;
}
/* Now the real code to set the delegate */
if (_delegate != nil)
{
[nc removeObserver: _delegate name: nil object: _notifObject];
}
[super setDelegate: anObject];
selector = @selector(shouldChangeTextInRange:replacementString:);
if ([_delegate respondsToSelector: selector])
{
_tvf.delegate_responds_to_should_change = YES;
}
else
{
_tvf.delegate_responds_to_should_change = NO;
}
selector = @selector(textView:willChangeSelectionFromCharacterRange:toCharacterRange:);
if ([_delegate respondsToSelector: selector])
{
_tvf.delegate_responds_to_will_change_sel = YES;
}
else
{
_tvf.delegate_responds_to_will_change_sel = NO;
}
/* SET_DELEGATE_NOTIFICATION defined at the beginning of file */
/* NSText notifications */
SET_DELEGATE_NOTIFICATION (DidBeginEditing);
SET_DELEGATE_NOTIFICATION (DidChange);
SET_DELEGATE_NOTIFICATION (DidEndEditing);
/* NSTextView notifications */
SET_DELEGATE_NOTIFICATION (ViewDidChangeSelection);
SET_DELEGATE_NOTIFICATION (ViewWillChangeNotifyingTextView);
}
- (int) spellCheckerDocumentTag
{
if (!_spellCheckerDocumentTag)
_spellCheckerDocumentTag = [NSSpellChecker uniqueSpellDocumentTag];
return _spellCheckerDocumentTag;
}
- (BOOL) isContinuousSpellCheckingEnabled
{
// TODO
NSLog(@"Method %s is not implemented for class %s",
"isContinuousSpellCheckingEnabled", "NSTextView");
return NO;
}
- (void) setContinuousSpellCheckingEnabled: (BOOL)flag
{
// TODO
NSLog(@"Method %s is not implemented for class %s",
"setContinuousSpellCheckingEnabled:", "NSTextView");
}
- (void) toggleContinuousSpellChecking: (id)sender
{
[self setContinuousSpellCheckingEnabled:
![self isContinuousSpellCheckingEnabled]];
}
- (NSRange) selectionRangeForProposedRange: (NSRange)proposedCharRange
granularity: (NSSelectionGranularity)granul
{
unsigned index;
NSRange aRange;
NSRange newRange;
NSString *string = [self string];
unsigned length = [string length];
if (proposedCharRange.location >= length)
{
proposedCharRange.location = length;
proposedCharRange.length = 0;
return proposedCharRange;
}
if (NSMaxRange (proposedCharRange) > length)
{
proposedCharRange.length = length - proposedCharRange.location;
}
if (length == 0)
{
return proposedCharRange;
}
switch (granul)
{
case NSSelectByWord:
index = proposedCharRange.location;
if (index >= length)
{
index = length - 1;
}
newRange = [_textStorage doubleClickAtIndex: index];
if (proposedCharRange.length > 1)
{
index = NSMaxRange(proposedCharRange) - 1;
if (index >= length)
{
index = length - 1;
}
aRange = [_textStorage doubleClickAtIndex: index];
newRange = NSUnionRange(newRange, aRange);
}
return newRange;
case NSSelectByParagraph:
return [string lineRangeForRange: proposedCharRange];
case NSSelectByCharacter:
default:
if (proposedCharRange.length == 0)
return proposedCharRange;
/* Expand the beginning character */
index = proposedCharRange.location;
newRange = [string rangeOfComposedCharacterSequenceAtIndex: index];
/* If the proposedCharRange is empty we only ajust the beginning */
if (proposedCharRange.length == 0)
{
return newRange;
}
/* Expand the finishing character */
index = NSMaxRange (proposedCharRange) - 1;
aRange = [string rangeOfComposedCharacterSequenceAtIndex: index];
newRange.length = NSMaxRange(aRange) - newRange.location;
return newRange;
}
}
- (NSRange) rangeForUserCharacterAttributeChange
{
if (!_tf.is_editable || !_tf.uses_font_panel)
{
return NSMakeRange (NSNotFound, 0);
}
if (_tf.is_rich_text)
{
// This expects the selection to be already corrected to characters
return _selected_range;
}
else
{
return NSMakeRange (0, [_textStorage length]);
}
}
- (NSRange) rangeForUserParagraphAttributeChange
{
if (!_tf.is_editable || !_tf.uses_ruler)
{
return NSMakeRange (NSNotFound, 0);
}
if (_tf.is_rich_text)
{
return [self selectionRangeForProposedRange: _selected_range
granularity: NSSelectByParagraph];
}
else
{
return NSMakeRange (0, [_textStorage length]);
}
}
- (NSRange) rangeForUserTextChange
{
if (!_tf.is_editable)
{
return NSMakeRange (NSNotFound, 0);
}
// This expects the selection to be already corrected to characters
return _selected_range;
}
- (NSString*) preferredPasteboardTypeFromArray: (NSArray*)availableTypes
restrictedToTypesFromArray: (NSArray*)allowedTypes
{
NSEnumerator *enumerator;
NSString *type;
if (availableTypes == nil)
return nil;
if (allowedTypes == nil)
return [availableTypes objectAtIndex: 0];
enumerator = [allowedTypes objectEnumerator];
while ((type = [enumerator nextObject]) != nil)
{
if ([availableTypes containsObject: type])
{
return type;
}
}
return nil;
}
- (BOOL) readSelectionFromPasteboard: (NSPasteboard*)pboard
{
/*
Reads the text view's preferred type of data from the pasteboard
specified by the pboard parameter. This method invokes the
preferredPasteboardTypeFromArray: restrictedToTypesFromArray: method
to determine the text view's preferred type of data and then reads
the data using the readSelectionFromPasteboard: type:
method. Returns YES if the data was successfully read. */
NSString *type;
type = [self preferredPasteboardTypeFromArray: [pboard types]
restrictedToTypesFromArray: [self readablePasteboardTypes]];
if (type == nil)
return NO;
return [self readSelectionFromPasteboard: pboard type: type];
}
- (BOOL) readSelectionFromPasteboard: (NSPasteboard*)pboard
type: (NSString*)type
{
/*
Reads data of the given type from pboard. The new data is placed at
the current insertion point, replacing the current selection if one
exists. Returns YES if the data was successfully read.
You should override this method to read pasteboard types other than
the default types. Use the rangeForUserTextChange method to obtain
the range of characters (if any) to be replaced by the new data. */
if ([type isEqualToString: NSStringPboardType])
{
[self insertText: [pboard stringForType: NSStringPboardType]];
return YES;
}
if (_tf.is_rich_text)
{
if ([type isEqualToString: NSRTFPboardType])
{
[self replaceCharactersInRange: [self rangeForUserTextChange]
withRTF: [pboard dataForType: NSRTFPboardType]];
return YES;
}
}
if (_tf.imports_graphics)
{
if ([type isEqualToString: NSRTFDPboardType])
{
[self replaceCharactersInRange: [self rangeForUserTextChange]
withRTFD: [pboard dataForType: NSRTFDPboardType]];
return YES;
}
// FIXME: Should also support: NSTIFFPboardType
if ([type isEqualToString: NSFileContentsPboardType])
{
NSTextAttachment *attachment = [[NSTextAttachment alloc]
initWithFileWrapper:
[pboard readFileWrapper]];
[self replaceRange: [self rangeForUserTextChange]
withAttributedString:
[NSAttributedString attributedStringWithAttachment: attachment]];
RELEASE(attachment);
return YES;
}
}
// color accepting
if ([type isEqualToString: NSColorPboardType])
{
NSColor *color = [NSColor colorFromPasteboard: pboard];
NSRange aRange = [self rangeForUserCharacterAttributeChange];
NSMutableDictionary *d = [[self typingAttributes] mutableCopy];
[d setObject: color forKey: NSForegroundColorAttributeName];
[self setTypingAttributes: d];
RELEASE(d);
if (aRange.location != NSNotFound)
{
[self setTextColor: color range: aRange];
}
return YES;
}
// font pasting
if ([type isEqualToString: NSFontPboardType])
{
NSData *data = [pboard dataForType: NSFontPboardType];
NSDictionary *dict = [NSUnarchiver unarchiveObjectWithData: data];
if (dict != nil)
{
NSRange aRange = [self rangeForUserCharacterAttributeChange];
NSMutableDictionary *d;
if (aRange.location != NSNotFound)
{
[self setAttributes: dict range: aRange];
}
d = [[self typingAttributes] mutableCopy];
[d addEntriesFromDictionary: dict];
[self setTypingAttributes: d];
RELEASE(d);
return YES;
}
return NO;
}
// ruler pasting
if ([type isEqualToString: NSRulerPboardType])
{
NSData *data = [pboard dataForType: NSRulerPboardType];
NSDictionary *dict = [NSUnarchiver unarchiveObjectWithData: data];
if (dict != nil)
{
NSRange aRange = [self rangeForUserParagraphAttributeChange];
NSMutableDictionary *d;
if (aRange.location != NSNotFound)
{
[self setAttributes: dict range: aRange];
}
d = [[self typingAttributes] mutableCopy];
[d addEntriesFromDictionary: dict];
[self setTypingAttributes: d];
RELEASE(d);
return YES;
}
return NO;
}
return NO;
}
- (NSArray*) readablePasteboardTypes
{
// get default types, what are they?
NSMutableArray *ret = [NSMutableArray arrayWithObjects: NSRulerPboardType,
NSColorPboardType, NSFontPboardType, nil];
if (_tf.imports_graphics)
{
[ret addObject: NSRTFDPboardType];
//[ret addObject: NSTIFFPboardType];
[ret addObject: NSFileContentsPboardType];
}
if (_tf.is_rich_text)
[ret addObject: NSRTFPboardType];
[ret addObject: NSStringPboardType];
return ret;
}
- (NSArray*) writablePasteboardTypes
{
// the selected text can be written to the pasteboard with which types.
return [self readablePasteboardTypes];
}
- (BOOL) writeSelectionToPasteboard: (NSPasteboard*)pboard
type: (NSString*)type
{
/*
Writes the current selection to pboard using the given type. Returns YES
if the data was successfully written. You can override this method to add
support for writing new types of data to the pasteboard. You should invoke
super's implementation of the method to handle any types of data your
overridden version does not.
*/
return [self writeSelectionToPasteboard: pboard
types: [NSArray arrayWithObject: type]];
}
- (BOOL) writeSelectionToPasteboard: (NSPasteboard*)pboard
types: (NSArray*)types
{
/* Writes the current selection to pboard under each type in the types
array. Returns YES if the data for any single type was written
successfully.
You should not need to override this method. You might need to invoke this
method if you are implementing a new type of pasteboard to handle services
other than copy/paste or dragging. */
BOOL ret = NO;
NSEnumerator *enumerator;
NSString *type;
if (types == nil)
{
return NO;
}
if (_selected_range.location == NSNotFound)
{
return NO;
}
[pboard declareTypes: types owner: self];
enumerator = [types objectEnumerator];
while ((type = [enumerator nextObject]) != nil)
{
if ([type isEqualToString: NSStringPboardType])
{
ret = [pboard setString: [[self string] substringWithRange: _selected_range]
forType: NSStringPboardType] || ret;
}
if ([type isEqualToString: NSRTFPboardType])
{
ret = [pboard setData: [self RTFFromRange: _selected_range]
forType: NSRTFPboardType] || ret;
}
if ([type isEqualToString: NSRTFDPboardType])
{
ret = [pboard setData: [self RTFDFromRange: _selected_range]
forType: NSRTFDPboardType] || ret;
}
if ([type isEqualToString: NSColorPboardType])
{
NSColor *color;
color = [_textStorage attribute: NSForegroundColorAttributeName
atIndex: _selected_range.location
effectiveRange: 0];
if (color != nil)
{
[color writeToPasteboard: pboard];
ret = YES;
}
}
if ([type isEqualToString: NSFontPboardType])
{
NSDictionary *dict;
dict = [_textStorage fontAttributesInRange: _selected_range];
if (dict != nil)
{
[pboard setData: [NSArchiver archivedDataWithRootObject: dict]
forType: NSFontPboardType];
ret = YES;
}
}
if ([type isEqualToString: NSRulerPboardType])
{
NSDictionary *dict;
dict = [_textStorage rulerAttributesInRange: _selected_range];
if (dict != nil)
{
[pboard setData: [NSArchiver archivedDataWithRootObject: dict]
forType: NSRulerPboardType];
ret = YES;
}
}
}
return ret;
}
// dragging of text, colors and files
- (unsigned int) draggingEntered: (id <NSDraggingInfo>)sender
{
NSPasteboard *pboard = [sender draggingPasteboard];
NSString *type = [self preferredPasteboardTypeFromArray: [pboard types]
restrictedToTypesFromArray: [self readablePasteboardTypes]];
return [self dragOperationForDraggingInfo: sender
type: type];
}
- (unsigned int) draggingUpdated: (id <NSDraggingInfo>)sender
{
NSPasteboard *pboard = [sender draggingPasteboard];
NSString *type = [self preferredPasteboardTypeFromArray: [pboard types]
restrictedToTypesFromArray: [self readablePasteboardTypes]];
return [self dragOperationForDraggingInfo: sender
type: type];
}
- (void) draggingExited: (id <NSDraggingInfo>)sender
{
}
- (BOOL) prepareForDragOperation: (id <NSDraggingInfo>)sender
{
return YES;
}
- (BOOL) performDragOperation: (id <NSDraggingInfo>)sender
{
return [self readSelectionFromPasteboard: [sender draggingPasteboard]];
}
- (void) concludeDragOperation: (id <NSDraggingInfo>)sender
{
}
- (void) cleanUpAfterDragOperation
{
// release drag information
}
- (unsigned int) dragOperationForDraggingInfo: (id <NSDraggingInfo>)dragInfo
type: (NSString *)type
{
//FIXME
return NSDragOperationCopy | NSDragOperationGeneric;
}
- (NSImage *) dragImageForSelectionWithEvent: (NSEvent *)event
origin: (NSPoint *)origin
{
if (origin)
*origin = NSMakePoint(0, 0);
return nil;
}
- (BOOL) dragSelectionWithEvent: (NSEvent *)event
offset: (NSSize)mouseOffset
slideBack: (BOOL)slideBack
{
NSPoint point;
NSImage *image = [self dragImageForSelectionWithEvent: event
origin: &point];
NSPasteboard *pboard = [NSPasteboard pasteboardWithName: NSDragPboard];
NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil];
NSMutableArray *types = [NSMutableArray array];
if (_tf.imports_graphics)
[types addObject: NSRTFDPboardType];
if (_tf.is_rich_text)
[types addObject: NSRTFPboardType];
[types addObject: NSStringPboardType];
[self writeSelectionToPasteboard: pboard types: types];
[self dragImage: image at: location offset: mouseOffset event: event
pasteboard: pboard source: self slideBack: slideBack];
return YES;
}
// This are all the NSTextInput methods that are not implemented on NSTextView
- (NSAttributedString *) attributedSubstringFromRange: (NSRange)theRange
{
return [_textStorage attributedSubstringFromRange: theRange];
}
- (unsigned) characterIndexForPoint: (NSPoint)point
{
unsigned index;
float fraction;
index = [_layoutManager glyphIndexForPoint: point
inTextContainer: _textContainer
fractionOfDistanceThroughGlyph: &fraction];
index = [_layoutManager characterIndexForGlyphAtIndex: index];
if (fraction > 0.5)
{
index++;
}
return index;
}
- (void) setMarkedText:(NSString *)aString selectedRange:(NSRange)selRange
{
}
- (BOOL) hasMarkedText
{
return NO;
}
- (void) unmarkText
{
}
- (NSArray*) validAttributesForMarkedText
{
return nil;
}
- (long) conversationIdentifier
{
return 0;
}
- (NSRect) firstRectForCharacterRange: (NSRange)theRange
{
unsigned rectCount;
NSRect *rects = [_layoutManager
rectArrayForCharacterRange: theRange
withinSelectedCharacterRange: NSMakeRange(NSNotFound, 0)
inTextContainer: _textContainer
rectCount: &rectCount];
if (rectCount)
return rects[0];
else
return NSZeroRect;
}
// This are here to keep compiler quite.
// Somehow gcc does not recognize when a super class already implements
// a method needed by a protocol.
- (void) doCommandBySelector: (SEL)aSelector
{
[super doCommandBySelector: aSelector];
}
@end
@implementation NSTextView (GNUstepExtensions)
- (void) replaceRange: (NSRange)aRange
withAttributedString: (NSAttributedString*)attrString
{
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: [attrString string]])
return;
[_textStorage beginEditing];
if (_tf.is_rich_text)
{
[_textStorage replaceCharactersInRange: aRange
withAttributedString: attrString];
}
else
{
[_textStorage replaceCharactersInRange: aRange
withString: [attrString string]];
}
[_textStorage endEditing];
[self didChangeText];
}
- (unsigned) textLength
{
return [_textStorage length];
}
@end
@implementation NSTextView (GNUstepPrivate)
- (void) _blink: (NSTimer *)t
{
if (_drawInsertionPointNow)
{
_drawInsertionPointNow = NO;
}
else
{
_drawInsertionPointNow = YES;
}
[self setNeedsDisplayInRect: _insertionPointRect
avoidAdditionalLayout: YES];
/* Because we are called by a timer which is independent of any
event processing in the gui runloop, we need to manually update
the window. */
[self displayIfNeeded];
}
+ (NSDictionary*) defaultTypingAttributes
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSParagraphStyle defaultParagraphStyle],
NSParagraphStyleAttributeName,
[NSFont userFontOfSize: 0], NSFontAttributeName,
[NSColor textColor], NSForegroundColorAttributeName,
nil];
}
- (void) setAttributes: (NSDictionary*)attributes range: (NSRange)aRange
{
NSString *type;
id val;
NSEnumerator *enumerator = [attributes keyEnumerator];
if (aRange.location == NSNotFound)
return;
if (![self shouldChangeTextInRange: aRange
replacementString: nil])
return;
[_textStorage beginEditing];
while ((type = [enumerator nextObject]) != nil)
{
val = [attributes objectForKey: type];
[_textStorage addAttribute: type value: val range: aRange];
}
[_textStorage endEditing];
[self didChangeText];
}
- (void) _illegalMovement: (int)textMovement
{
// This is similar to [self resignFirstResponder], with the
// difference that in the notification we need to put the
// NSTextMovement, which resignFirstResponder does not. Also, if we
// are ending editing, we are going to be removed, so it's useless
// to update any drawing.
NSNumber *number;
NSDictionary *uiDictionary;
if ((_tf.is_editable)
&& ([_delegate respondsToSelector:
@selector(textShouldEndEditing:)])
&& ([_delegate textShouldEndEditing: self] == NO))
return;
/* FIXME */
if (_insertionPointTimer != nil)
{
[_insertionPointTimer invalidate];
DESTROY (_insertionPointTimer);
}
number = [NSNumber numberWithInt: textMovement];
uiDictionary = [NSDictionary dictionaryWithObject: number
forKey: @"NSTextMovement"];
[nc postNotificationName: NSTextDidEndEditingNotification
object: self userInfo: uiDictionary];
return;
}
- (NSRect) rectForCharacterRange: (NSRange)aRange
{
NSRange glyphRange;
glyphRange = [_layoutManager glyphRangeForCharacterRange: aRange
actualCharacterRange: NULL];
return [_layoutManager boundingRectForGlyphRange: glyphRange
inTextContainer: _textContainer];
}
/** Extension method that copies the current selected text to the
special section pasteboard */
- (void) copySelection
{
[self writeSelectionToPasteboard: [NSPasteboard pasteboardWithName: @"Selection"]
type: NSStringPboardType];
}
/** Extension method that pastes the current selected text from the
special section pasteboard */
- (void) pasteSelection
{
[self readSelectionFromPasteboard: [NSPasteboard pasteboardWithName: @"Selection"]
type: NSStringPboardType];
}
/** Bind other mouse up to pasteSelection. This should be done via configuation! */
- (void) otherMouseUp: (NSEvent*)theEvent
{
// Should we change the insertion point, based on the event position?
[self pasteSelection];
}
@end
|