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 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265
|
/** <title>NSMatrix</title>
<abstract>Matrix class for grouping controls</abstract>
Copyright (C) 1996-2015 Free Software Foundation, Inc.
Author: Ovidiu Predescu <ovidiu@net-community.com>
Date: March 1997
A completely rewritten version of the original source by Pascal Forget and
Scott Christley.
Modified: Felipe A. Rodriguez <far@ix.netcom.com>
Date: August 1998
Cell handling rewritten: Richard Frith-Macdonald <richard@brainstorm.co.uk>
Date: November 1999
Implementation of Editing: Nicola Pero <n.pero@mi.flashnet.it>
Date: November 1999
Modified: Mirko Viviani <mirko.viviani@rccr.cremona.it>
Date: March 2001
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 Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/* Mouse Tracking Notes:
The behaviour of mouse tracking is a bit different on OS42 and MaxOSX. The
implementation here reflects OS42 more closely (as the original code
in NSMatrix). Examples of differences:
- highlighting of NSButtonCells is different;
- OS42 makes each cell under the cursor track the mouse, MacOSX makes only
the clicked cell track it, untilMouseUp;
- if mouse goes up outside of a cell, OS42 sends the action, MacOSX does not
- keys used for selection in list mode are not the same (shift and alternate
on OS42, command and shift on MacOSX).
*/
#include "config.h"
#include <stdlib.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSException.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSKeyedArchiver.h>
#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSKeyValueObserving.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSFormatter.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSString.h>
#import <Foundation/NSZone.h>
#import "AppKit/NSApplication.h"
#import "AppKit/NSButtonCell.h"
#import "AppKit/NSColor.h"
#import "AppKit/NSCursor.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSGraphics.h"
#import "AppKit/NSKeyValueBinding.h"
#import "AppKit/NSMatrix.h"
#import "AppKit/NSWindow.h"
#include <math.h>
static NSNotificationCenter *nc;
#define NSMATRIX_STRICT_CHECKING 0
#ifdef MIN
# undef MIN
#endif
#define MIN(A,B) ({ typeof(A) __a = (A); \
typeof(B) __b = (B); \
__a < __b ? __a : __b; })
#ifdef MAX
# undef MAX
#endif
#define MAX(A,B) ({ typeof(A) __a = (A); \
typeof(B) __b = (B); \
__a < __b ? __b : __a; })
#ifdef ABS
# undef ABS
#endif
#define ABS(A) ({ typeof(A) __a = (A); __a < 0 ? -__a : __a; })
#define SIGN(x) \
({typeof(x) _SIGN_x = (x); \
_SIGN_x > 0 ? 1 : (_SIGN_x == 0 ? 0 : -1); })
#define POINT_FROM_INDEX(index) \
({MPoint point = { (index) % _numCols, (index) / _numCols }; point; })
#define INDEX_FROM_COORDS(x,y) \
((y) * _numCols + (x))
#define INDEX_FROM_POINT(point) \
((point).y * _numCols + (point).x)
/* Some stuff needed to compute the selection in the list mode. */
typedef struct {
NSInteger x;
NSInteger y;
} MPoint;
typedef struct {
NSInteger x;
NSInteger y;
NSInteger width;
NSInteger height;
} MRect;
typedef struct _GSMatrixFlags {
#if GS_WORDS_BIGENDIAN == 1
unsigned int isHighlight:1;
unsigned int isRadio:1;
unsigned int isList:1;
unsigned int allowsEmptySelection:1;
unsigned int autoScroll:1;
unsigned int selectionByRect:1;
unsigned int drawCellBackground:1;
unsigned int drawBackground:1;
unsigned int autosizesCells:1;
unsigned int drawingAncestor:1;
unsigned int tabKeyTraversesCells:1;
unsigned int tabKeyTraversesCellsExplicitly:1;
unsigned int canSearchIncrementally:1;
unsigned int unused:19;
#else
unsigned int unused:19;
unsigned int canSearchIncrementally:1;
unsigned int tabKeyTraversesCellsExplicitly:1;
unsigned int tabKeyTraversesCells:1;
unsigned int drawingAncestor:1;
unsigned int autosizesCells:1;
unsigned int drawBackground:1;
unsigned int drawCellBackground:1;
unsigned int selectionByRect:1;
unsigned int autoScroll:1;
unsigned int allowsEmptySelection:1;
unsigned int isList:1;
unsigned int isRadio:1;
unsigned int isHighlight:1;
#endif
} GSMatrixFlags;
static inline MPoint MakePoint (NSInteger x, NSInteger y)
{
MPoint point = { x, y };
return point;
}
@interface NSMatrix (PrivateMethods)
- (void) _renewRows: (NSInteger)row
columns: (NSInteger)col
rowSpace: (NSInteger)rowSpace
colSpace: (NSInteger)colSpace;
- (void) _setState: (NSInteger)state
highlight: (BOOL)highlight
startIndex: (NSInteger)start
endIndex: (NSInteger)end;
- (BOOL) _selectNextSelectableCellAfterRow: (NSInteger)row
column: (NSInteger)column;
- (BOOL) _selectPreviousSelectableCellBeforeRow: (NSInteger)row
column: (NSInteger)column;
- (void) _setKeyRow: (NSInteger)row
column: (NSInteger)column;
@end
enum {
DEFAULT_CELL_HEIGHT = 17,
DEFAULT_CELL_WIDTH = 100
};
/** <p>TODO documentation</p>
*/
@implementation NSMatrix
/* Class variables */
static Class defaultCellClass = nil;
static NSUInteger mouseDownFlags = 0;
static SEL copySel;
static SEL initSel;
static SEL allocSel;
static SEL getSel;
+ (void) initialize
{
if (self == [NSMatrix class])
{
/* Set the initial version */
[self setVersion: 1];
copySel = @selector(copyWithZone:);
initSel = @selector(init);
allocSel = @selector(allocWithZone:);
getSel = @selector(objectAtIndex:);
/*
* MacOS-X docs say default cell class is NSActionCell
*/
defaultCellClass = [NSActionCell class];
//
nc = [NSNotificationCenter defaultCenter];
[self exposeBinding: NSSelectedTagBinding];
}
}
/**<p>Returns the cell class used to create cells. By default it is a
NSActionCell class</p><p>See Also: +setCellClass:</p>
*/
+ (Class) cellClass
{
return defaultCellClass;
}
/**<p>Sets the cell class used to create cells to <var>classId</var>.
By default it is a NSActionCell class</p><p>See Also: +setCellClass:</p>
*/
+ (void) setCellClass: (Class)classId
{
defaultCellClass = classId;
if (defaultCellClass == nil)
defaultCellClass = [NSActionCell class];
}
- (id) init
{
return [self initWithFrame: NSZeroRect
mode: NSRadioModeMatrix
cellClass: [object_getClass(self) cellClass]
numberOfRows: 0
numberOfColumns: 0];
}
/** <p>Initializes and returns a NSMatrix in frame frameRect.
By default the matrix has no row and no column, the NSMatrix's mode is
NSRadioModeMatrix and the cell class is a NSActionCell class.</p><p>See
Also: -initWithFrame:mode:cellClass:numberOfRows:numberOfColumns:</p>
*/
- (id) initWithFrame: (NSRect)frameRect
{
return [self initWithFrame: frameRect
mode: NSRadioModeMatrix
cellClass: [object_getClass(self) cellClass]
numberOfRows: 0
numberOfColumns: 0];
}
- (void) _privateFrame: (NSRect)frameRect
mode: (NSMatrixMode)aMode
numberOfRows: (NSInteger)rows
numberOfColumns: (NSInteger)cols
{
_myZone = [self zone];
[self _renewRows: rows columns: cols rowSpace: 0 colSpace: 0];
_mode = aMode;
if ((_numCols > 0) && (_numRows > 0))
{
/*
We must not round the _cellSize to integers here!
Any approximation is a loss of information. We should give
to the backend as much information as possible, and trust
that it will use that information to provide the best
possible rendering on that device. Depending on the backend,
that might go up to using antialias or advanced graphics
tricks to make an advanced rendering of things not lying on
pixel boundaries. Approximating here just gives less
information to the backend, making the rendering worse.
Even if the backend is just approximating to pixels, it would
still be wrong to round _cellSize here, because rounding
sizes of rectangles without considering the origin of the
rectangles has been definitely found to be wrong and to cause
incorrect rendering. The origin of the whole matrix is very
likely a non-integer - if not originally, as a consequence of
the fact that the user resized the window - so making the
cell size integer does not cause drawing to be done on pixel
boundaries anyway, and will actually make more difficult for
the backend to render the rectangles properly since it will
be drawing approximately rectangles which are already only an
approximate description - and this first approximation having
been done incorrectly too! - of what we really want to draw.
*/
_cellSize = NSMakeSize (frameRect.size.width/_numCols,
frameRect.size.height/_numRows);
}
else
{
_cellSize = NSMakeSize (DEFAULT_CELL_WIDTH, DEFAULT_CELL_HEIGHT);
}
_intercell = NSMakeSize(1, 1);
[self setAutosizesCells: YES];
[self setFrame: frameRect];
_tabKeyTraversesCells = YES;
[self setBackgroundColor: [NSColor controlColor]];
[self setDrawsBackground: NO];
[self setCellBackgroundColor: [NSColor controlColor]];
[self setDrawsCellBackground: NO];
[self setSelectionByRect: YES];
_dottedRow = _dottedColumn = -1;
if (_mode == NSRadioModeMatrix && _numRows > 0 && _numCols > 0)
{
[self selectCellAtRow: 0 column: 0];
}
else
{
_selectedCell = nil;
_selectedRow = _selectedColumn = -1;
}
}
/**<p>Initializes and returns a new NSMatrix in the specified frame frameRect.
The <ref type="type" id="NSMatrixMode">NSMatrixMode</ref> is specified
by mode, the cell class used specified by classId and the number of rows
and columns specified by rowsHigh and colsWide respectively</p><p>See Also:
-initWithFrame:mode:prototype:numberOfRows:numberOfColumns:</p>
*/
- (id) initWithFrame: (NSRect)frameRect
mode: (NSMatrixMode)aMode
cellClass: (Class)classId
numberOfRows: (NSInteger)rowsHigh
numberOfColumns: (NSInteger)colsWide
{
if (!( self = [super initWithFrame: frameRect]))
{
return nil;
}
[self setCellClass: classId];
[self _privateFrame: frameRect
mode: aMode
numberOfRows: rowsHigh
numberOfColumns: colsWide];
return self;
}
/**<p>Initializes and returns a new NSMatrix in the specified frame frameRect.
The <ref type="type" id="NSMatrixMode">NSMatrixMode</ref> is specified
by mode, the cell used specified by aCell and the number of rows
and columns specified by rowsHigh and colsWide respectively</p><p>See Also:
-initWithFrame:mode:prototype:numberOfRows:numberOfColumns:</p>
*/
- (id) initWithFrame: (NSRect)frameRect
mode: (NSMatrixMode)aMode
prototype: (NSCell*)aCell
numberOfRows: (NSInteger)rowsHigh
numberOfColumns: (NSInteger)colsWide
{
if (!(self = [super initWithFrame: frameRect]))
{
return nil;
}
[self setPrototype: aCell];
[self _privateFrame: frameRect
mode: aMode
numberOfRows: rowsHigh
numberOfColumns: colsWide];
return self;
}
- (void) dealloc
{
int i;
if (_textObject != nil)
{
[_selectedCell endEditing: _textObject];
_textObject = nil;
}
for (i = 0; i < _maxRows; i++)
{
int j;
for (j = 0; j < _maxCols; j++)
{
[_cells[i][j] release];
}
NSZoneFree(_myZone, _cells[i]);
NSZoneFree(_myZone, _selectedCells[i]);
}
NSZoneFree(_myZone, _cells);
NSZoneFree(_myZone, _selectedCells);
[_cellPrototype release];
[_backgroundColor release];
[_cellBackgroundColor release];
if (_delegate != nil)
{
[nc removeObserver: _delegate name: nil object: self];
_delegate = nil;
}
[super dealloc];
}
/**<p>Inserts a new column after the current last column.</p>
<p>See Also: -insertColumn:withCells: </p>
*/
- (void) addColumn
{
[self insertColumn: _numCols withCells: nil];
}
/**<p>Inserts a new column of cells specified by cellArray after the current
last column.</p><p>See Also: -insertColumn:withCells: </p>
*/
- (void) addColumnWithCells: (NSArray*)cellArray
{
[self insertColumn: _numCols withCells: cellArray];
}
/**<p>Inserts a new row after the current last row.</p>
<p>See Also: -insertRow:withCells: </p>
*/
- (void) addRow
{
[self insertRow: _numRows withCells: nil];
}
/**<p>Inserts a new row of cells specified by cellArray
after the current last row.</p><p>See Also: -insertRow:withCells: </p>
*/
- (void) addRowWithCells: (NSArray*)cellArray
{
[self insertRow: _numRows withCells: cellArray];
}
/**<p>Inserts a new column at the specified column <var>column</var>.</p>
<p>See Also: -insertColumn:withCells:</p>
*/
- (void) insertColumn: (NSInteger)column
{
[self insertColumn: column withCells: nil];
}
/**<p>Inserts a new column of cells ( specified by <var>cellArray</var>)
at the specified column <var>column</var>. This method can grows
the matrix as necessay if needed</p>
<p>See Also: -insertColumn:</p>
*/
- (void) insertColumn: (NSInteger)column withCells: (NSArray*)cellArray
{
NSInteger count = [cellArray count];
NSInteger i = _numCols + 1;
if (column < 0)
{
column = 0;
#if NSMATRIX_STRICT_CHECKING == 0
NSLog(@"insert negative column (%d) in matrix", (int)column);
#else
[NSException raise: NSRangeException
format: @"insert negative column (%d) in matrix", (int)column];
#endif
}
if ((cellArray != nil) && (count != _numRows))
{
#if NSMATRIX_STRICT_CHECKING == 0
NSLog(@"Wrong number of cells (%d) in column insert in matrix", (int)count);
#else
[NSException raise: NSRangeException
format: @"Wrong number of cells (%d) in column insert in matrix", (int)count];
#endif
}
if (column >= i)
{
i = column + 1;
}
/*
* Use _renewRows:columns:rowSpace:colSpace: to grow the matrix as necessary.
* MacOS-X docs say that if the matrix is empty, we make it have one column
* and enough rows for all the elements.
*/
if (count > 0 && (_numRows == 0 || _numCols == 0))
{
[self _renewRows: count columns: 1 rowSpace: 0 colSpace: count];
}
else
{
[self _renewRows: _numRows ? _numRows : 1
columns: i
rowSpace: 0
colSpace: count];
}
/*
* Rotate the new column to the insertion point if necessary.
*/
if (_numCols != column)
{
for (i = 0; i < _numRows; i++)
{
int j = _numCols;
id old = _cells[i][j-1];
while (--j > column)
{
_cells[i][j] = _cells[i][j-1];
_selectedCells[i][j] = _selectedCells[i][j-1];
}
_cells[i][column] = old;
_selectedCells[i][column] = NO;
}
if (_selectedCell && (_selectedColumn >= column))
{
_selectedColumn++;
}
if (_dottedColumn >= column)
{
_dottedColumn++;
}
}
/*
* Now put the new cells from the array into the matrix.
*/
if (count > 0)
{
IMP getImp = [cellArray methodForSelector: getSel];
for (i = 0; i < _numRows && i < count; i++)
{
ASSIGN(_cells[i][column], (*getImp)(cellArray, getSel, i));
}
}
if (_mode == NSRadioModeMatrix && _allowsEmptySelection == NO
&& _selectedCell == nil)
{
[self selectCellAtRow: 0 column: 0];
}
[self setNeedsDisplay: YES];
}
/**<p>Inserts a new row at index <var>row</var>.</p>
<p>See Also: -insertRow:withCells: </p>
*/
- (void) insertRow: (NSInteger)row
{
[self insertRow: row withCells: nil];
}
/**<p>Inserts a new row of cells ( specified by <var>cellArray</var>)
at the specified row <var>row</var>. This method can grows
the matrix as necessay if needed</p>
<p>See Also: -insertColumn:</p>
*/
- (void) insertRow: (NSInteger)row withCells: (NSArray*)cellArray
{
NSInteger count = [cellArray count];
NSInteger i = _numRows + 1;
if (row < 0)
{
row = 0;
#if NSMATRIX_STRICT_CHECKING == 0
NSLog(@"insert negative row (%d) in matrix", (int)row);
#else
[NSException raise: NSRangeException
format: @"insert negative row (%d) in matrix", (int)row];
#endif
}
if ((cellArray != nil) && (count != _numCols))
{
#if NSMATRIX_STRICT_CHECKING == 0
NSLog(@"Wrong number of cells (%d) in row insert in matrix", (int)count);
#else
[NSException raise: NSRangeException
format: @"Wrong number of cells (%d) in row insert in matrix", (int)count];
#endif
}
if (row >= i)
{
i = row + 1;
}
/*
* Grow the matrix to have the new row.
* MacOS-X docs say that if the matrix is empty, we make it have one
* row and enough columns for all the elements.
*/
if (count > 0 && (_numRows == 0 || _numCols == 0))
{
[self _renewRows: 1 columns: count rowSpace: count colSpace: 0];
}
else
{
[self _renewRows: i
columns: _numCols ? _numCols : 1
rowSpace: count
colSpace: 0];
}
/*
* Rotate the newly created row to the insertion point if necessary.
*/
if (_numRows != row)
{
id *oldr = _cells[_numRows - 1];
BOOL *olds = _selectedCells[_numRows - 1];
for (i = _numRows - 1; i > row; i--)
{
_cells[i] = _cells[i-1];
_selectedCells[i] = _selectedCells[i-1];
}
_cells[row] = oldr;
_selectedCells[row] = olds;
if (_selectedCell && (_selectedRow >= row))
_selectedRow++;
if (_dottedRow != -1 && _dottedRow >= row)
_dottedRow++;
}
/*
* Put cells from the array into the matrix.
*/
if (count > 0)
{
IMP getImp = [cellArray methodForSelector: getSel];
for (i = 0; i < _numCols && i < count; i++)
{
ASSIGN(_cells[row][i], (*getImp)(cellArray, getSel, i));
}
}
if (_mode == NSRadioModeMatrix && !_allowsEmptySelection
&& _selectedCell == nil)
{
[self selectCellAtRow: 0 column: 0];
}
[self setNeedsDisplay: YES];
}
/**<p>Makes and returns new cell at row <var>row</var> and
column <var>column</var>.</p>
*/
- (NSCell*) makeCellAtRow: (NSInteger)row
column: (NSInteger)column
{
NSCell *aCell;
if (_cellPrototype != nil)
{
aCell = (*_cellNew)(_cellPrototype, copySel, _myZone);
}
else
{
aCell = (*_cellNew)(_cellClass, allocSel, _myZone);
if (aCell != nil)
{
aCell = (*_cellInit)(aCell, initSel);
}
}
/*
* This is only ever called when we are creating a new cell - so we know
* we can simply assign a value into the matrix without releasing an old
* value. If someone uses this method directly (which the documentation
* specifically says they shouldn't) they may produce a memory leak.
*/
_cells[row][column] = aCell;
return aCell;
}
/** <p>Returns the rectangle of the cell at row <var>row</var> and column
<var>column</var></p>
*/
- (NSRect) cellFrameAtRow: (NSInteger)row
column: (NSInteger)column
{
NSRect rect;
rect.origin.x = column * (_cellSize.width + _intercell.width);
rect.origin.y = row * (_cellSize.height + _intercell.height);
rect.size = _cellSize;
return rect;
}
/**<p>Gets the number of rows and columns of the NSMatrix</p>
<p>See Also: -numberOfColumns -numberOfRows</p>
*/
- (void) getNumberOfRows: (NSInteger*)rowCount
columns: (NSInteger*)columnCount
{
*rowCount = _numRows;
*columnCount = _numCols;
}
/**<p>Replaces the NSMatrix's cell at row <var>row</var> and column <var>
column</var> by <var>newCell</var> and mark for display the new cell.
Raises a NSRangeException if the <var>row</var> or <var>column</var>
are out of range.</p>
*/
- (void) putCell: (NSCell*)newCell
atRow: (NSInteger)row
column: (NSInteger)column
{
if (row < 0 || row >= _numRows || column < 0 || column >= _numCols)
{
[NSException raise: NSRangeException
format: @"attempt to put cell outside matrix bounds"];
}
if ((row == _selectedRow) && (column == _selectedColumn)
&& (_selectedCell != nil))
{
_selectedCell = newCell;
}
ASSIGN(_cells[row][column], newCell);
[self setNeedsDisplayInRect: [self cellFrameAtRow: row column: column]];
}
/**<p>Removes the NSMatrix's column at index <var>column</var></p>
<p>See Also: -removeRow:</p>
*/
- (void) removeColumn: (NSInteger)column
{
if (column >= 0 && column < _numCols)
{
NSInteger i;
for (i = 0; i < _maxRows; i++)
{
NSInteger j;
AUTORELEASE(_cells[i][column]);
for (j = column + 1; j < _maxCols; j++)
{
_cells[i][j-1] = _cells[i][j];
_selectedCells[i][j-1] = _selectedCells[i][j];
}
}
_numCols--;
_maxCols--;
if (_maxCols == 0)
{
_numRows = _maxRows = 0;
}
if (column == _selectedColumn)
{
_selectedCell = nil;
[self selectCellAtRow: _selectedRow column: 0];
}
if (column == _dottedColumn)
{
if (_numCols && [_cells[_dottedRow][0] acceptsFirstResponder])
_dottedColumn = 0;
else
_dottedRow = _dottedColumn = -1;
}
}
else
{
#if NSMATRIX_STRICT_CHECKING == 0
NSLog(@"remove non-existent column (%d) from matrix", (int) column);
#else
[NSException raise: NSRangeException
format: @"remove non-existent column (%d) from matrix", (int)column];
#endif
}
}
/**<p>Removes the NSMatrix's row at index <var>row</var></p>
<p>See Also: -removeColumn:</p>
*/
- (void) removeRow: (NSInteger)row
{
if (row >= 0 && row < _numRows)
{
NSInteger i;
for (i = 0; i < _maxCols; i++)
{
AUTORELEASE(_cells[row][i]);
}
NSZoneFree(_myZone, _selectedCells[row]);
NSZoneFree(_myZone, _cells[row]);
for (i = row + 1; i < _maxRows; i++)
{
_cells[i-1] = _cells[i];
_selectedCells[i-1] = _selectedCells[i];
}
_maxRows--;
_numRows--;
if (_maxRows == 0)
{
_numCols = _maxCols = 0;
}
if (row == _selectedRow)
{
_selectedCell = nil;
[self selectCellAtRow: 0 column: _selectedColumn];
}
if (row == _dottedRow)
{
if (_numRows && [_cells[0][_dottedColumn] acceptsFirstResponder])
_dottedRow = 0;
else
_dottedRow = _dottedColumn = -1;
}
}
else
{
#if NSMATRIX_STRICT_CHECKING == 0
NSLog(@"remove non-existent row (%d) from matrix", (int)row);
#else
[NSException raise: NSRangeException
format: @"remove non-existent row (%d) from matrix", (int)row];
#endif
}
}
- (void) renewRows: (NSInteger)newRows
columns: (NSInteger)newColumns
{
[self _renewRows: newRows columns: newColumns rowSpace: 0 colSpace: 0];
}
- (void) setCellSize: (NSSize)aSize
{
_cellSize = aSize;
[self sizeToCells];
}
/** <p>Sets the space size between cells to aSize and resizes the matrix to
fits the new cells spacing.</p>
<p>See Also: -intercellSpacing -sizeToCells</p>
*/
- (void) setIntercellSpacing: (NSSize)aSize
{
_intercell = aSize;
[self sizeToCells];
}
- (void) sortUsingFunction: (NSComparisonResult (*)(id element1, id element2,
void *userData))comparator
context: (void*)context
{
NSMutableArray *sorted;
IMP add;
IMP get;
NSInteger i, j, index = 0;
sorted = [NSMutableArray arrayWithCapacity: _numRows * _numCols];
add = [sorted methodForSelector: @selector(addObject:)];
get = [sorted methodForSelector: @selector(objectAtIndex:)];
for (i = 0; i < _numRows; i++)
{
for (j = 0; j < _numCols; j++)
{
(*add)(sorted, @selector(addObject:), _cells[i][j]);
}
}
[sorted sortUsingFunction: comparator context: context];
for (i = 0; i < _numRows; i++)
{
for (j = 0; j < _numCols; j++)
{
_cells[i][j] = (*get)(sorted, @selector(objectAtIndex:), index++);
}
}
}
- (void) sortUsingSelector: (SEL)comparator
{
NSMutableArray *sorted;
IMP add;
IMP get;
NSInteger i, j, index = 0;
sorted = [NSMutableArray arrayWithCapacity: _numRows * _numCols];
add = [sorted methodForSelector: @selector(addObject:)];
get = [sorted methodForSelector: @selector(objectAtIndex:)];
for (i = 0; i < _numRows; i++)
{
for (j = 0; j < _numCols; j++)
{
(*add)(sorted, @selector(addObject:), _cells[i][j]);
}
}
[sorted sortUsingSelector: comparator];
for (i = 0; i < _numRows; i++)
{
for (j = 0; j < _numCols; j++)
{
_cells[i][j] = (*get)(sorted, @selector(objectAtIndex:), index++);
}
}
}
/** <p>Gets the row and the column of the NSMatrix correponding to the
specified NSPoint aPoint. Returns YES if aPoint is within the NSMatrix,
NO otherwise</p>
*/
- (BOOL) getRow: (NSInteger*)row
column: (NSInteger*)column
forPoint: (NSPoint)aPoint
{
BOOL betweenRows;
BOOL betweenCols;
BOOL beyondRows;
BOOL beyondCols;
int approxRow = aPoint.y / (_cellSize.height + _intercell.height);
float approxRowsHeight = approxRow * (_cellSize.height + _intercell.height);
int approxCol = aPoint.x / (_cellSize.width + _intercell.width);
float approxColsWidth = approxCol * (_cellSize.width + _intercell.width);
/* First check the limit cases - is the point outside the matrix */
beyondCols = (aPoint.x > _bounds.size.width || aPoint.x < 0);
beyondRows = (aPoint.y > _bounds.size.height || aPoint.y < 0);
/* Determine if the point is inside a cell - note: if the point lies
on the cell boundaries, we consider it inside the cell. to be
outside the cell (that is, in the intercell spacing) it must be
completely in the intercell spacing - not on the border */
/* The following is non zero if the point lies between rows (not inside
a cell) */
betweenRows = (aPoint.y < approxRowsHeight
|| aPoint.y > approxRowsHeight + _cellSize.height);
betweenCols = (aPoint.x < approxColsWidth
|| aPoint.x > approxColsWidth + _cellSize.width);
if (beyondRows || betweenRows || beyondCols || betweenCols
|| (_numCols == 0) || (_numRows == 0))
{
if (row)
{
*row = -1;
}
if (column)
{
*column = -1;
}
return NO;
}
if (row)
{
if (approxRow < 0)
{
approxRow = 0;
}
else if (approxRow >= _numRows)
{
approxRow = _numRows - 1;
}
*row = approxRow;
}
if (column)
{
if (approxCol < 0)
{
approxCol = 0;
}
else if (approxCol >= _numCols)
{
approxCol = _numCols - 1;
}
*column = approxCol;
}
return YES;
}
/** <p>Gets the row and the column of the NSMatrix correponding to the
specified NSCell aCell. Returns YES if aCell is in the NSMatrix,
NO otherwise</p>
*/
- (BOOL) getRow: (NSInteger*)row
column: (NSInteger*)column
ofCell: (NSCell*)aCell
{
NSInteger i;
for (i = 0; i < _numRows; i++)
{
NSInteger j;
for (j = 0; j < _numCols; j++)
{
if (_cells[i][j] == aCell)
{
if (row)
*row = i;
if (column)
*column = j;
return YES;
}
}
}
if (row)
*row = -1;
if (column)
*column = -1;
return NO;
}
/** <p>Sets the state of the cell at row <var>row</var> and <var>column</var>
to value. If the NSMatrix's mode is NSRadioModeMatrix it deselects
the cell currently selected if needed.</p>
*/
- (void) setState: (NSInteger)value
atRow: (NSInteger)row
column: (NSInteger)column
{
NSCell *aCell = [self cellAtRow: row column: column];
if (!aCell)
{
return;
}
if (_mode == NSRadioModeMatrix)
{
if (value)
{
if (_selectedRow > -1 && _selectedColumn > -1)
{
_selectedCells[_selectedRow][_selectedColumn] = NO;
[_selectedCell setState: NSOffState];
[self setNeedsDisplayInRect:
[self cellFrameAtRow: _selectedRow
column: _selectedColumn]];
}
_selectedCell = aCell;
_selectedRow = row;
_selectedColumn = column;
[_selectedCell setState: value];
_selectedCells[row][column] = YES;
[self _setKeyRow: row column: column];
}
else if (_allowsEmptySelection)
{
[self deselectSelectedCell];
}
}
else
{
[aCell setState: value];
}
[self setNeedsDisplayInRect: [self cellFrameAtRow: row column: column]];
}
/**<p>Deselects all NSMatrix's cells. Does nothing if the NSMatrix's mode
is NSRadioModeMatrix and if it does not allows empty selection.
Except for the case, when there are no cells left at all. Then the
selection is always cleared.</p>
<p>See Also: -mode -allowsEmptySelection -setNeedsDisplayInRect:</p>
*/
- (void) deselectAllCells
{
NSInteger i;
if (_numRows > 0 && _numCols > 0 &&
!_allowsEmptySelection && _mode == NSRadioModeMatrix)
{
return;
}
for (i = 0; i < _numRows; i++)
{
NSInteger j;
for (j = 0; j < _numCols; j++)
{
if (_selectedCells[i][j])
{
NSCell *aCell = _cells[i][j];
BOOL isHighlighted = [aCell isHighlighted];
_selectedCells[i][j] = NO;
if ([aCell state] || isHighlighted)
{
[aCell setState: NSOffState];
if (isHighlighted)
{
[aCell setHighlighted: NO];
}
[self setNeedsDisplayInRect: [self cellFrameAtRow: i
column: j]];
}
}
}
}
_selectedCell = nil;
_selectedRow = -1;
_selectedColumn = -1;
}
/**<p>Deselects the selected cell.Does nothing if the NSMatrix's mode
is NSRadioModeMatrix and if it does not allows empty selection</p>
*/
- (void) deselectSelectedCell
{
NSInteger i,j;
if (!_selectedCell
|| (!_allowsEmptySelection && (_mode == NSRadioModeMatrix)))
return;
/*
* For safety (as in macosx)
*/
for (i = 0; i < _numRows; i++)
{
for (j = 0; j < _numCols; j++)
{
if (_selectedCells[i][j])
{
[_cells[i][j] setState: NSOffState];
_selectedCells[i][j] = NO;
}
}
}
_selectedCell = nil;
_selectedRow = -1;
_selectedColumn = -1;
}
/**<p>Selects all the cells and marks self for display. Does nothing if the
NSMatrix's mode is NSRadioModeMatrix</p><p>See Also:
-selectCellAtRow:column: -selectCell:</p>
*/
- (void) selectAll: (id)sender
{
NSInteger i, j;
/* Can't select all if only one can be selected. */
if (_mode == NSRadioModeMatrix)
{
return;
}
_selectedCell = nil;
_selectedRow = -1;
_selectedColumn = -1;
for (i = 0; i < _numRows; i++)
{
for (j = 0; j < _numCols; j++)
{
if ([_cells[i][j] isEnabled] == YES
&& [_cells[i][j] isEditable] == NO)
{
_selectedCell = _cells[i][j];
[_selectedCell setState: NSOnState];
_selectedCells[i][j] = YES;
_selectedRow = i;
_selectedColumn = j;
}
else
{
_selectedCells[i][j] = NO;
[_cells[i][j] setShowsFirstResponder: NO];
}
}
}
[self setNeedsDisplay: YES];
}
- (void) _selectCell: (NSCell *)aCell atRow: (NSInteger)row column: (NSInteger)column
{
if (aCell)
{
NSRect cellFrame;
if (_selectedCell && _selectedCell != aCell)
{
if (_mode == NSRadioModeMatrix && _selectedRow > -1 && _selectedColumn > -1)
{
_selectedCells[_selectedRow][_selectedColumn] = NO;
[_selectedCell setState: NSOffState];
}
[self setNeedsDisplayInRect: [self cellFrameAtRow: _selectedRow
column: _selectedColumn]];
}
_selectedCell = aCell;
_selectedRow = row;
_selectedColumn = column;
_selectedCells[row][column] = YES;
if (_mode == NSListModeMatrix || _mode == NSRadioModeMatrix)
{
[_selectedCell setState: NSOnState];
}
else
{
[_selectedCell setNextState];
}
if (_mode == NSListModeMatrix)
[aCell setHighlighted: YES];
cellFrame = [self cellFrameAtRow: row column: column];
if (_autoscroll)
[self scrollRectToVisible: cellFrame];
[self setNeedsDisplayInRect: cellFrame];
[self _setKeyRow: row column: column];
}
else
{
_selectedCell = nil;
_selectedRow = _selectedColumn = -1;
}
}
- (void) selectCell: (NSCell *)aCell
{
NSInteger row, column;
if ([self getRow: &row column: &column ofCell: aCell] == YES)
{
[self _selectCell: aCell atRow: row column: column];
// Note: we select the cell iff it is 'selectable', not 'editable'
// as macosx says. This looks definitely more appropriate.
// [This is going to start editing only if the cell is also editable,
// otherwise the text gets selected and that's all.]
[self selectTextAtRow: row column: column];
}
}
/** <p>Selects the cell and the text inside at row <var>row</var>
and column <var>column</var>. If row or column is -1 it deselects all
the cells.</p>
<p>See Also: -deselectSelectedCell -selectTextAtRow:column:</p>
*/
- (void) selectCellAtRow: (NSInteger)row column: (NSInteger)column
{
NSCell *aCell;
if ((row == -1) || (column == -1))
{
[self deselectAllCells];
return;
}
aCell = [self cellAtRow: row column: column];
if (aCell)
{
[self _selectCell: aCell atRow: row column: column];
[self selectTextAtRow: row column: column];
}
}
/**<p>Selects the cell (and the text inside) with tag <var>anInt</var>.
Return YES if the NSMatrix contains a cell with tag <var>anInt</var>,
NO otherwise.</p><p>See Also: -deselectSelectedCell
-selectTextAtRow:column:</p>
*/
- (BOOL) selectCellWithTag: (NSInteger)anInt
{
id aCell;
NSInteger i = _numRows;
while (i-- > 0)
{
NSInteger j = _numCols;
while (j-- > 0)
{
aCell = _cells[i][j];
if ([aCell tag] == anInt)
{
[self _selectCell: aCell atRow: i column: j];
[self selectTextAtRow: i column: j];
return YES;
}
}
}
return NO;
}
/**<p>Returns an array of the selected cells</p>
*/
- (NSArray*) selectedCells
{
NSMutableArray *array = [NSMutableArray array];
NSInteger i;
for (i = 0; i < _numRows; i++)
{
NSInteger j;
for (j = 0; j < _numCols; j++)
{
if (_selectedCells[i][j] == YES)
{
[array addObject: _cells[i][j]];
}
}
}
return array;
}
- (void) setSelectionFrom: (NSInteger)startPos
to: (NSInteger)endPos
anchor: (NSInteger)anchorPos
highlight: (BOOL)flag
{
/* Cells are selected from the anchor (A) to the point where the mouse
* went down (S) and then they are selected (if the mouse moves away from A)
* or deselected (if the mouse moves closer to A) until the point
* where the mouse goes up (E).
* This is inverted if flag is false (not sure about this though; if this is
* changed, mouse tracking in list mode should be changed too).
*/
/* An easy way of doing this is unselecting all cells from A to S and then
* selecting all cells from A to E. Let's try to do it in a more optimized
* way..
*/
/* Linear and rectangular selections are a bit different */
if (![self isSelectionByRect]
|| [self numberOfRows] == 1 || [self numberOfColumns] == 1)
{
/* Linear selection
* There are three possibilities (ignoring direction):
* A S E
* sssssssssss
*
* A E S
* ssssssuuuuu
*
* E A S
* ssssssuuuuu
*
* So, cells from A to E are selected and, if S is outside the
* range from A to E, cells from S to its closest point are unselected
*/
NSInteger selStart = MIN(anchorPos, endPos);
NSInteger selEnd = MAX(anchorPos, endPos);
[self _setState: flag ? NSOnState : NSOffState
highlight: flag
startIndex: selStart
endIndex: selEnd];
if (startPos > selEnd)
{
[self _setState: flag ? NSOffState : NSOnState
highlight: !flag
startIndex: selEnd+1
endIndex: startPos];
}
else if (startPos < selStart)
{
[self _setState: flag ? NSOffState : NSOnState
highlight: !flag
startIndex: startPos
endIndex: selStart-1];
}
}
else
{
/* Rectangular selection
*
* A sss
* S sss
* E sss
*
* A ssu
* E ssu
* S uuu
*
* E ss
* A ssu
* S uu
*
* A ssu
* S ssu
* E ss
*
* So, cells of the rect from A to E are selected and cells of the
* rect from A to S that are outside the first rect are unselected
*/
MPoint anchorPoint = POINT_FROM_INDEX(anchorPos);
MPoint endPoint = POINT_FROM_INDEX(endPos);
MPoint startPoint = POINT_FROM_INDEX(startPos);
NSInteger minx_AE = MIN(anchorPoint.x, endPoint.x);
NSInteger miny_AE = MIN(anchorPoint.y, endPoint.y);
NSInteger maxx_AE = MAX(anchorPoint.x, endPoint.x);
NSInteger maxy_AE = MAX(anchorPoint.y, endPoint.y);
NSInteger minx_AS = MIN(anchorPoint.x, startPoint.x);
NSInteger miny_AS = MIN(anchorPoint.y, startPoint.y);
NSInteger maxx_AS = MAX(anchorPoint.x, startPoint.x);
NSInteger maxy_AS = MAX(anchorPoint.y, startPoint.y);
[self _setState: flag ? NSOnState : NSOffState
highlight: flag
startIndex: INDEX_FROM_COORDS(minx_AE, miny_AE)
endIndex: INDEX_FROM_COORDS(maxx_AE, maxy_AE)];
if (startPoint.x > maxx_AE)
{
[self _setState: flag ? NSOffState : NSOnState
highlight: !flag
startIndex: INDEX_FROM_COORDS(maxx_AE+1, miny_AS)
endIndex: INDEX_FROM_COORDS(startPoint.x, maxy_AS)];
}
else if (startPoint.x < minx_AE)
{
[self _setState: flag ? NSOffState : NSOnState
highlight: !flag
startIndex: INDEX_FROM_COORDS(startPoint.x, miny_AS)
endIndex: INDEX_FROM_COORDS(minx_AE-1, maxy_AS)];
}
if (startPoint.y > maxy_AE)
{
[self _setState: flag ? NSOffState : NSOnState
highlight: !flag
startIndex: INDEX_FROM_COORDS(minx_AS, maxy_AE+1)
endIndex: INDEX_FROM_COORDS(maxx_AS, startPoint.y)];
}
else if (startPoint.y < miny_AE)
{
[self _setState: flag ? NSOffState : NSOnState
highlight: !flag
startIndex: INDEX_FROM_COORDS(minx_AS, startPoint.y)
endIndex: INDEX_FROM_COORDS(maxx_AS, miny_AE-1)];
}
}
/*
Update the _selectedCell and related ivars. This could be optimized a lot
in many cases, but the full search cannot be avoided in the general case,
and being correct comes first.
*/
{
NSInteger i, j;
for (i = _numRows - 1; i >= 0; i--)
{
for (j = _numCols - 1; j >= 0; j--)
{
if (_selectedCells[i][j])
{
_selectedCell = _cells[i][j];
_selectedRow = i;
_selectedColumn = j;
return;
}
}
}
_selectedCell = nil;
_selectedColumn = -1;
_selectedRow = -1;
}
}
/**<p>Returns the cell at row <var>row</var> and column <var>column</var>
Returns nil if the <var>row</var> or <var>column</var> are out of
range</p>
*/
- (id) cellAtRow: (NSInteger)row
column: (NSInteger)column
{
if (row < 0 || row >= _numRows || column < 0 || column >= _numCols)
return nil;
return _cells[row][column];
}
/**<p>Returns the cell with tag <var>anInt</var>
Returns nil if no cell has a tag <var>anInt</var></p>
*/
- (id) cellWithTag: (NSInteger)anInt
{
NSInteger i = _numRows;
while (i-- > 0)
{
NSInteger j = _numCols;
while (j-- > 0)
{
id aCell = _cells[i][j];
if ([aCell tag] == anInt)
{
return aCell;
}
}
}
return nil;
}
/** <p>Returns an array of the NSMatrix's cells</p>
*/
- (NSArray*) cells
{
NSMutableArray *c;
IMP add;
NSInteger i;
c = [NSMutableArray arrayWithCapacity: _numRows * _numCols];
add = [c methodForSelector: @selector(addObject:)];
for (i = 0; i < _numRows; i++)
{
NSInteger j;
for (j = 0; j < _numCols; j++)
{
(*add)(c, @selector(addObject:), _cells[i][j]);
}
}
return c;
}
- (void) selectText: (id)sender
{
// Attention, we are *not* doing what MacOS-X does.
// But they are *not* doing what the OpenStep specification says.
// This is a compromise -- and fully OpenStep compliant.
NSSelectionDirection s = NSDirectSelection;
if (_window)
s = [_window keyViewSelectionDirection];
switch (s)
{
// _window selecting backwards
case NSSelectingPrevious:
[self _selectPreviousSelectableCellBeforeRow: _numRows
column: _numCols];
break;
// _Window selecting forward
case NSSelectingNext:
[self _selectNextSelectableCellAfterRow: -1
column: -1];
break;
case NSDirectSelection:
// Someone else -- we have some freedom here
if ([_selectedCell isSelectable])
{
[self selectTextAtRow: _selectedRow
column: _selectedColumn];
}
else
{
if (_dottedRow != -1)
{
[self selectTextAtRow: _dottedRow column: _dottedColumn];
}
}
break;
}
}
/**<p>Select the text of the cell at row <var>row</var> and column
<var>column</var>. The cell is selected if and only if the cell
is selectable ( MacOSX select it if the cell is editable ). This
methods returns the selected cell if exists and selectable,
nil otherwise</p>
*/
- (id) selectTextAtRow: (NSInteger)row column: (NSInteger)column
{
if (row < 0 || row >= _numRows || column < 0 || column >= _numCols)
return self;
// macosx doesn't select the cell if it isn't 'editable'; instead,
// we select the cell if and only if it is 'selectable', which looks
// more appropriate. This is going to start editing if and only if
// the cell is also 'editable'.
if ([_cells[row][column] isSelectable] == NO)
{
return nil;
}
if (_textObject)
{
if (_selectedCell == _cells[row][column])
{
[_textObject selectAll: self];
return _selectedCell;
}
else
{
[self validateEditing];
[self abortEditing];
}
}
// Now _textObject == nil
{
NSText *text = [_window fieldEditor: YES
forObject: self];
NSUInteger length;
if (([text superview] != nil) && ([text resignFirstResponder] == NO))
{
return nil;
}
[self _selectCell: _cells[row][column] atRow: row column: column];
/* See comment in NSTextField */
length = [[_selectedCell stringValue] length];
_textObject = [_selectedCell setUpFieldEditorAttributes: text];
[_selectedCell selectWithFrame: [self cellFrameAtRow: _selectedRow
column: _selectedColumn]
inView: self
editor: _textObject
delegate: self
start: 0
length: length];
return _selectedCell;
}
}
- (id) keyCell
{
if (_dottedRow == -1 || _dottedColumn == -1)
{
return nil;
}
else if (_cells != 0)
{
return _cells[_dottedRow][_dottedColumn];
}
return nil;
}
- (void) setKeyCell: (NSCell *)aCell
{
BOOL isValid;
NSInteger row, column;
isValid = [self getRow: &row column: &column ofCell: aCell];
if (isValid == YES)
{
[self _setKeyRow: row column: column];
}
}
/**<p>Returns the next key view</p>
<p>See Also: -setNextText: [NSView-nextKeyView]</p>
*/
- (id) nextText
{
return [self nextKeyView];
}
/**<p>Returns the previous key view</p>
<p>See Also: -setPreviousText: [NSView-previousKeyView]</p>
*/
- (id) previousText
{
return [self previousKeyView];
}
/**<p>Invokes when the text cell starts to be editing.This methods posts
a NSControlTextDidBeginEditingNotification with a dictionary containing
the NSFieldEditor as user info </p><p>See Also:
[NSNotificationCenter-postNotificationName:object:userInfo:]</p>
*/
- (void) textDidBeginEditing: (NSNotification *)aNotification
{
[super textDidBeginEditing: aNotification];
}
/**<p>Invokes when the text cell is changed. This methods posts a
NSControlTextDidChangeNotification with a dictionary containing the
NSFieldEditor as user info </p><p>See Also:
[NSNotificationCenter-postNotificationName:object:userInfo:]</p>
*/
- (void) textDidChange: (NSNotification *)aNotification
{
NSFormatter *formatter;
// MacOS-X asks us to inform the cell if possible.
if ((_selectedCell != nil) && [_selectedCell respondsToSelector:
@selector(textDidChange:)])
{
[_selectedCell textDidChange: aNotification];
}
[super textDidChange: aNotification];
formatter = [_selectedCell formatter];
if (formatter != nil)
{
/*
* FIXME: This part needs heavy interaction with the yet to finish
* text system.
*
*/
NSString *partialString;
NSString *newString = nil;
NSString *error = nil;
BOOL wasAccepted;
partialString = [_textObject string];
wasAccepted = [formatter isPartialStringValid: partialString
newEditingString: &newString
errorDescription: &error];
if (wasAccepted == NO)
{
SEL sel = @selector(control:didFailToValidatePartialString:errorDescription:);
if ([_delegate respondsToSelector: sel])
{
[_delegate control: self
didFailToValidatePartialString: partialString
errorDescription: error];
}
}
if (newString != nil)
{
NSLog (@"Unimplemented: should set string to %@", newString);
// FIXME ! This would reset editing !
//[_textObject setString: newString];
}
else
{
if (wasAccepted == NO)
{
// FIXME: Need to delete last typed character (?!)
NSLog (@"Unimplemented: should delete last typed character");
}
}
}
}
/**<p>Invokes when the text cell is changed.
This methods posts a NSControlTextDidEndEditingNotification
a dictionary containing the NSFieldEditor as user info </p><p>See Also:
[NSNotificationCenter-postNotificationName:object:userInfo:]</p>
*/
- (void) textDidEndEditing: (NSNotification *)aNotification
{
id textMovement;
[super textDidEndEditing: aNotification];
textMovement = [[aNotification userInfo] objectForKey: @"NSTextMovement"];
if (textMovement)
{
switch ([(NSNumber *)textMovement intValue])
{
case NSReturnTextMovement:
if ([self sendAction] == NO)
{
NSEvent *event = [_window currentEvent];
if ([self performKeyEquivalent: event] == NO
&& [_window performKeyEquivalent: event] == NO)
[self selectText: self];
}
break;
case NSTabTextMovement:
if ([_selectedCell sendsActionOnEndEditing])
[self sendAction];
if (_tabKeyTraversesCells)
{
if ([self _selectNextSelectableCellAfterRow: _selectedRow
column: _selectedColumn])
break;
}
[_window selectKeyViewFollowingView: self];
if ([_window firstResponder] == _window)
{
if (_tabKeyTraversesCells)
{
if ([self _selectNextSelectableCellAfterRow: -1
column: -1])
break;
}
[self selectText: self];
}
break;
case NSBacktabTextMovement:
if ([_selectedCell sendsActionOnEndEditing])
[self sendAction];
if (_tabKeyTraversesCells)
{
if ([self _selectPreviousSelectableCellBeforeRow: _selectedRow
column: _selectedColumn])
break;
}
[_window selectKeyViewPrecedingView: self];
if ([_window firstResponder] == _window)
{
if (_tabKeyTraversesCells)
{
if ([self _selectPreviousSelectableCellBeforeRow: _numRows
column: _numCols])
break;
}
[self selectText: self];
}
break;
}
}
}
/**<p>Asks to the delegate (if it implements -control:textShouldBeginEditing: )
if the text should be edit. Returns YES if the delegate does not implement
this method</p>
*/
- (BOOL) textShouldBeginEditing: (NSText*)aTextObject
{
if (_delegate && [_delegate respondsToSelector:
@selector(control:textShouldBeginEditing:)])
{
return [_delegate control: self
textShouldBeginEditing: aTextObject];
}
return YES;
}
- (BOOL) textShouldEndEditing: (NSText *)aTextObject
{
if ([_selectedCell isEntryAcceptable: [aTextObject text]] == NO)
{
[self sendAction: _errorAction to: _target];
return NO;
}
if ([_delegate respondsToSelector:
@selector(control:textShouldEndEditing:)])
{
if ([_delegate control: self
textShouldEndEditing: aTextObject] == NO)
{
NSBeep ();
return NO;
}
}
if ([_delegate respondsToSelector:
@selector(control:isValidObject:)] == YES)
{
NSFormatter *formatter;
id newObjectValue;
formatter = [_selectedCell formatter];
if ([formatter getObjectValue: &newObjectValue
forString: [_textObject text]
errorDescription: NULL] == YES)
{
if ([_delegate control: self
isValidObject: newObjectValue] == NO)
{
return NO;
}
}
}
// In all other cases
return YES;
}
- (BOOL) tabKeyTraversesCells
{
return _tabKeyTraversesCells;
}
- (void) setTabKeyTraversesCells: (BOOL)flag
{
_tabKeyTraversesCells = flag;
}
/**<p>Sets the next key view to <var>anObject</var></p>
<p>See Also: -nextText [NSView-setNextKeyView:</p>
*/
- (void) setNextText: (id)anObject
{
[self setNextKeyView: anObject];
}
/**<p>Sets the previous key view to <var>anObject</var></p>
<p>See Also: -previousText [NSView-setPreviousKeyView:</p>
*/
- (void) setPreviousText: (id)anObject
{
[self setPreviousKeyView: anObject];
}
- (void) setValidateSize: (BOOL)flag
{
// TODO
}
- (void) sizeToCells
{
NSSize newSize;
NSInteger nc = _numCols;
NSInteger nr = _numRows;
if (!nc)
nc = 1;
if (!nr)
nr = 1;
newSize.width = nc * (_cellSize.width + _intercell.width) - _intercell.width;
newSize.height = nr * (_cellSize.height + _intercell.height) - _intercell.height;
[super setFrameSize: newSize];
}
- (void) sizeToFit
{
/*
* A simple explanation of the logic behind this method.
*
* Example of when you would like to use this method:
* you have a matrix containing radio buttons. Say that you have the
* following radio buttons -
*
* * First option
* * Second option
* * Third option
* * No thanks, no option for me
*
* this method should size the matrix so that it can comfortably
* show all the cells it contains. To do it, we must consider that
* all the cells should be given the same size, yet some cells need
* more space than the others to show their contents, so we need to
* choose the cell size as to be enough to display every cell. We
* loop on all cells, call cellSize on each (which returns the
* *minimum* comfortable size to display that cell), and choose a
* final cellSize which is enough big to be bigger than all these
* cellSizes. We resize the matrix to have that cellSize, and
* that's it. */
NSSize newSize = NSZeroSize;
NSInteger i, j;
for (i = 0; i < _numRows; i++)
{
for (j = 0; j < _numCols; j++)
{
NSSize tempSize = [_cells[i][j] cellSize];
tempSize.height = ceil(tempSize.height);
tempSize.width = ceil(tempSize.width);
if (tempSize.width > newSize.width)
{
newSize.width = tempSize.width;
}
if (tempSize.height > newSize.height)
{
newSize.height = tempSize.height;
}
}
}
[self setCellSize: newSize];
}
/**<p>Scrolls the NSMatrix to make the cell at row <var>row</var> and column
<var>column</var> visible</p>
<p>See Also: -scrollRectToVisible: -cellFrameAtRow:column:</p>
*/
- (void) scrollCellToVisibleAtRow: (NSInteger)row
column: (NSInteger)column
{
[self scrollRectToVisible: [self cellFrameAtRow: row column: column]];
}
- (void) setAutoscroll: (BOOL)flag
{
_autoscroll = flag;
}
- (void) setScrollable: (BOOL)flag
{
NSInteger i;
for (i = 0; i < _numRows; i++)
{
NSInteger j;
for (j = 0; j < _numCols; j++)
{
[_cells[i][j] setScrollable: flag];
}
}
[_cellPrototype setScrollable: flag];
}
- (void) drawRect: (NSRect)rect
{
NSInteger i, j;
NSInteger row1, col1; // The cell at the upper left corner
NSInteger row2, col2; // The cell at the lower right corner
if (_drawsBackground)
{
[_backgroundColor set];
NSRectFill(rect);
}
if (!_numRows || !_numCols)
return;
row1 = rect.origin.y / (_cellSize.height + _intercell.height);
col1 = rect.origin.x / (_cellSize.width + _intercell.width);
row2 = NSMaxY(rect) / (_cellSize.height + _intercell.height);
col2 = NSMaxX(rect) / (_cellSize.width + _intercell.width);
if (row1 < 0)
{
row1 = 0;
}
else if (row1 >= _numRows)
{
row1 = _numRows - 1;
}
if (col1 < 0)
{
col1 = 0;
}
else if (col1 >= _numCols)
{
col1 = _numCols - 1;
}
if (row2 < 0)
{
row2 = 0;
}
else if (row2 >= _numRows)
{
row2 = _numRows - 1;
}
if (col2 < 0)
{
col2 = 0;
}
else if (col2 >= _numCols)
{
col2 = _numCols - 1;
}
/* Draw the cells within the drawing rectangle. */
for (i = row1; i <= row2 && i < _numRows; i++)
{
for (j = col1; j <= col2 && j < _numCols; j++)
{
[self drawCellAtRow: i column: j];
}
}
}
- (BOOL) isOpaque
{
return _drawsBackground;
}
- (void) drawCell: (NSCell *)aCell
{
NSInteger row, column;
if ([self getRow: &row column: &column ofCell: aCell] == YES)
{
[self drawCellAtRow: row column: column];
}
}
/**<p>Draws the cell at row <var>row</var> and column <var>column</var></p>
<p>See Also: [NSCell-drawWithFrame:inView:] -setDrawsCellBackground:
-drawsCellBackground</p>
*/
- (void) drawCellAtRow: (NSInteger)row column: (NSInteger)column
{
NSCell *aCell = [self cellAtRow: row column: column];
if (aCell)
{
NSRect cellFrame = [self cellFrameAtRow: row column: column];
if (_drawsCellBackground)
{
[_cellBackgroundColor set];
NSRectFill(cellFrame);
}
if (_dottedRow == row
&& _dottedColumn == column
&& [aCell acceptsFirstResponder]
&& [_window isKeyWindow]
&& [_window firstResponder] == self)
{
[aCell setShowsFirstResponder: YES];
[aCell drawWithFrame: cellFrame inView: self];
[aCell setShowsFirstResponder: NO];
}
else
{
[aCell setShowsFirstResponder: NO];
[aCell drawWithFrame: cellFrame inView: self];
}
}
}
/** <p>(Un)Highlights the cell (if exists ) at row at row <var>row</var>
and column <var>column</var>. and maks the cell rect for display.</p>
<p>See Also: -setNeedsDisplayInRect: [NSCell-setHighlighted:]</p>
*/
- (void) highlightCell: (BOOL)flag atRow: (NSInteger)row column: (NSInteger)column
{
NSCell *aCell = [self cellAtRow: row column: column];
if (aCell)
{
[aCell setHighlighted: flag];
[self setNeedsDisplayInRect: [self cellFrameAtRow: row column: column]];
}
}
/**<p>Sends the cell action, if a NSMatrix's cell is selected
and enabled, sends the NSMatrix action otherwise. Returns YES if
the action is succesfully sent. NO if a cell is selected but not enabled
or if an action can not be sent.</p>
<p>See Also: -sendAction:to: -selectedCell</p>
*/
- (BOOL) sendAction
{
if (_selectedCell)
{
if ([_selectedCell isEnabled] == NO)
{
return NO;
}
return [self sendAction: [_selectedCell action]
to: [_selectedCell target]];
}
// _selectedCell == nil
return [super sendAction: _action to: _target];
}
- (BOOL) sendAction: (SEL)theAction
to: (id)theTarget
{
if (theAction)
{
if (theTarget)
{
return [super sendAction: theAction to: theTarget];
}
else
{
return [super sendAction: theAction to: _target];
}
}
else
{
return [super sendAction: _action to: _target];
}
}
- (void) sendAction: (SEL)aSelector
to: (id)anObject
forAllCells: (BOOL)flag
{
NSInteger i;
if (flag)
{
for (i = 0; i < _numRows; i++)
{
NSInteger j;
for (j = 0; j < _numCols; j++)
{
if (![anObject performSelector: aSelector
withObject: _cells[i][j]])
{
return;
}
}
}
}
else
{
for (i = 0; i < _numRows; i++)
{
NSInteger j;
for (j = 0; j < _numCols; j++)
{
if (_selectedCells[i][j])
{
if (![anObject performSelector: aSelector
withObject: _cells[i][j]])
{
return;
}
}
}
}
}
}
/**
*/
- (void) sendDoubleAction
{
if ([_selectedCell isEnabled] == NO)
return;
if (_doubleAction)
[self sendAction: _doubleAction to: _target];
else
[self sendAction];
}
/**<p>Returns NO if the NSMatrix's mode is <ref type="type" id="NSMatrixMode">
NSListModeMatrix</ref>, YES otherwise.</p>
<p>See Also: -setMode: -mode</p>
*/
- (BOOL) acceptsFirstMouse: (NSEvent*)theEvent
{
if (_mode == NSListModeMatrix)
return NO;
else
return YES;
}
- (void) _mouseDownNonListMode: (NSEvent *)theEvent
{
BOOL mouseUpInCell = NO, onCell, scrolling = NO, mouseUp = NO;
NSCell *mouseCell;
NSInteger mouseRow;
NSInteger mouseColumn;
NSPoint mouseLocation;
NSRect mouseCellFrame;
NSCell *originallySelectedCell = _selectedCell;
NSUInteger eventMask = NSLeftMouseUpMask | NSLeftMouseDownMask
| NSMouseMovedMask | NSLeftMouseDraggedMask;
while (!mouseUp)
{
mouseLocation = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
onCell = [self getRow: &mouseRow
column: &mouseColumn
forPoint: mouseLocation];
if (onCell)
{
mouseCellFrame = [self cellFrameAtRow: mouseRow column: mouseColumn];
mouseCell = [self cellAtRow: mouseRow column: mouseColumn];
if (_autoscroll)
{
scrolling = [self scrollRectToVisible: mouseCellFrame];
}
if ([mouseCell isEnabled])
{
int old_state;
/* Select the cell before tracking. The cell can send its action
* during tracking, and the target discovers which cell was
* clicked calling selectedCell.
* The cell calls -nextState before sending the action, so its
* state should not be changed here (except in radio mode).
*/
old_state = [mouseCell state];
[self _selectCell: mouseCell atRow: mouseRow column: mouseColumn];
if (_mode == NSRadioModeMatrix && !_allowsEmptySelection)
{
[mouseCell setState: NSOffState];
}
else
{
[mouseCell setState: old_state];
}
if (_mode != NSTrackModeMatrix)
{
[self highlightCell: YES
atRow: mouseRow
column: mouseColumn];
}
mouseUpInCell = [mouseCell trackMouse: theEvent
inRect: mouseCellFrame
ofView: self
untilMouseUp:
[[mouseCell class]
prefersTrackingUntilMouseUp]];
if (_mode != NSTrackModeMatrix)
{
[self highlightCell: NO
atRow: mouseRow
column: mouseColumn];
}
else
{
if ([mouseCell state] != old_state)
{
[self setNeedsDisplayInRect: mouseCellFrame];
}
}
mouseUp = mouseUpInCell
|| ([[NSApp currentEvent] type] == NSLeftMouseUp);
if (!mouseUpInCell)
{
_selectedCells[_selectedRow][_selectedColumn] = NO;
_selectedCell = nil;
_selectedRow = _selectedColumn = -1;
}
}
}
// if mouse didn't go up, take next event
if (!mouseUp)
{
NSEvent *newEvent;
newEvent = [NSApp nextEventMatchingMask: eventMask
untilDate: !scrolling
? [NSDate distantFuture]
: [NSDate dateWithTimeIntervalSinceNow: 0.05]
inMode: NSEventTrackingRunLoopMode
dequeue: YES];
if (newEvent != nil)
{
theEvent = newEvent;
mouseUp = ([theEvent type] == NSLeftMouseUp);
}
}
}
if (!mouseUpInCell)
{
if (_mode == NSRadioModeMatrix && !_allowsEmptySelection)
{
[self selectCell: originallySelectedCell];
}
[self sendAction]; /* like OPENSTEP, unlike MacOSX */
}
}
- (void) _mouseDownListMode: (NSEvent *) theEvent
{
NSPoint locationInWindow, mouseLocation;
NSInteger mouseRow, mouseColumn;
NSInteger mouseIndex, previousIndex = 0, anchor = 0;
id mouseCell, previousCell = nil;
BOOL onCell;
BOOL isSelecting = YES;
NSUInteger eventMask = NSLeftMouseUpMask | NSLeftMouseDownMask
| NSMouseMovedMask | NSLeftMouseDraggedMask
| NSPeriodicMask;
// List mode
// multiple cells can be selected, dragging the mouse
// cells do not track the mouse
// shift key makes expands selection noncontiguously
// alternate key expands selection contiguously
// implementation based on OS 4.2 behaviour, that is different from MacOS X
if (_autoscroll)
{
[NSEvent startPeriodicEventsAfterDelay: 0.05 withPeriod: 0.05];
}
locationInWindow = [theEvent locationInWindow];
while ([theEvent type] != NSLeftMouseUp)
{
// must convert location each time or periodic events won't work well
mouseLocation = [self convertPoint: locationInWindow fromView: nil];
onCell = [self getRow: &mouseRow
column: &mouseColumn
forPoint: mouseLocation];
if (onCell)
{
mouseCell = [self cellAtRow: mouseRow column: mouseColumn];
mouseIndex = INDEX_FROM_COORDS(mouseColumn, mouseRow);
if (_autoscroll)
{
NSRect mouseRect;
mouseRect = [self cellFrameAtRow: mouseRow column: mouseColumn];
[self scrollRectToVisible: mouseRect];
}
if (mouseCell != previousCell && [mouseCell isEnabled] == YES)
{
if (!previousCell)
{
// When the user first clicks on a cell
// we clear the existing selection
// unless the Alternate or Shift keys have been pressed.
if (!(mouseDownFlags & NSShiftKeyMask)
&& !(mouseDownFlags & NSAlternateKeyMask))
{
[self deselectAllCells];
}
/* The clicked cell is the anchor of the selection, unless
* the Alternate key is pressed, when the anchor is made
* the key cell, from which the selection will be
* extended (this is probably not the best cell when
* selection is by rect)
*/
if (!(mouseDownFlags & NSAlternateKeyMask))
{
anchor = INDEX_FROM_COORDS(mouseColumn, mouseRow);
}
else
{
if (_dottedColumn != -1)
anchor = INDEX_FROM_COORDS(_dottedColumn, _dottedRow);
else
anchor = INDEX_FROM_COORDS(0, 0);
}
/* With the shift key pressed, clicking on a selected cell
* deselects it (and inverts the selection on mouse dragging).
*/
if (mouseDownFlags & NSShiftKeyMask)
{
isSelecting = ([mouseCell state] == NSOffState);
}
else
{
isSelecting = YES;
}
previousIndex = mouseIndex;
}
[self setSelectionFrom: previousIndex
to: mouseIndex
anchor: anchor
highlight: isSelecting];
[self _setKeyRow: mouseRow column: mouseColumn];
previousIndex = mouseIndex;
previousCell = mouseCell;
}
}
theEvent = [NSApp nextEventMatchingMask: eventMask
untilDate: [NSDate distantFuture]
inMode: NSEventTrackingRunLoopMode
dequeue: YES];
NSDebugLLog(@"NSMatrix", @"matrix: got event of type: %d\n",
(int)[theEvent type]);
if ([theEvent type] != NSPeriodic)
{
locationInWindow = [theEvent locationInWindow];
}
}
if (_autoscroll)
{
[NSEvent stopPeriodicEvents];
}
[self sendAction];
}
- (void) mouseDown: (NSEvent*)theEvent
{
NSInteger row, column;
NSPoint lastLocation = [theEvent locationInWindow];
NSInteger clickCount;
/*
* Pathological case -- ignore mouse down
*/
if ((_numRows == 0) || (_numCols == 0))
{
[super mouseDown: theEvent];
return;
}
// Manage multi-click events
clickCount = [theEvent clickCount];
if (clickCount > 2)
return;
if (clickCount == 2 && (_ignoresMultiClick == NO))
{
[self sendDoubleAction];
return;
}
// From now on, code to manage simple-click events
lastLocation = [self convertPoint: lastLocation
fromView: nil];
// If mouse down was on a selectable cell, start editing/selecting.
if ([self getRow: &row
column: &column
forPoint: lastLocation])
{
if ([_cells[row][column] isEnabled])
{
if ([_cells[row][column] isSelectable])
{
NSText *t = [_window fieldEditor: YES forObject: self];
if ([t superview] != nil)
{
if ([t resignFirstResponder] == NO)
{
if ([_window makeFirstResponder: _window] == NO)
return;
}
}
// During editing, the selected cell is the cell being edited
[self _selectCell: _cells[row][column] atRow: row column: column];
_textObject = [_selectedCell setUpFieldEditorAttributes: t];
[_selectedCell editWithFrame: [self cellFrameAtRow: row
column: column]
inView: self
editor: _textObject
delegate: self
event: theEvent];
return;
}
}
}
// Paranoia check -- _textObject should already be nil, since we
// accept first responder, so NSWindow should have already given
// us first responder status (thus already ending editing with _textObject).
if (_textObject)
{
NSLog (@"Hi, I am a bug.");
[self validateEditing];
[self abortEditing];
}
mouseDownFlags = [theEvent modifierFlags];
if (_mode != NSListModeMatrix)
{
[self _mouseDownNonListMode: theEvent];
}
else
{
[self _mouseDownListMode: theEvent];
}
}
- (void) updateCell: (NSCell*)aCell
{
NSInteger row, col;
NSRect rect;
if ([self getRow: &row column: &col ofCell: aCell] == NO)
{
return; // Not a cell in this matrix - we can't update it.
}
rect = [self cellFrameAtRow: row column: col];
[self setNeedsDisplayInRect: rect];
}
/**<p>Simulates a mouse click for the first cell with the corresponding
key Equivalent.</p>
<p>See Also: [NSCell-keyEquivalent]</p>
*/
- (BOOL) performKeyEquivalent: (NSEvent*)theEvent
{
NSString *keyEquivalent = [theEvent charactersIgnoringModifiers];
NSUInteger modifiers = [theEvent modifierFlags];
int i;
NSUInteger relevantModifiersMask = NSCommandKeyMask | NSAlternateKeyMask | NSControlKeyMask;
/* Take shift key into account only for control keys and arrow and function keys */
if ((modifiers & NSFunctionKeyMask)
|| ([keyEquivalent length] > 0 && [[NSCharacterSet controlCharacterSet] characterIsMember:[keyEquivalent characterAtIndex:0]]))
relevantModifiersMask |= NSShiftKeyMask;
if ([keyEquivalent length] == 0)
return NO; // don't respond to zero-length string (such as the Windows key)
for (i = 0; i < _numRows; i++)
{
int j;
for (j = 0; j < _numCols; j++)
{
NSCell *aCell = _cells[i][j];
NSUInteger mask = 0;
if ([aCell respondsToSelector:@selector(keyEquivalentModifierMask)])
mask = [(NSButtonCell *)aCell keyEquivalentModifierMask];
if ([aCell isEnabled]
&& [[aCell keyEquivalent] isEqualToString: keyEquivalent]
&& (mask & relevantModifiersMask) == (modifiers & relevantModifiersMask))
{
NSCell *oldSelectedCell = _selectedCell;
int oldSelectedRow = _selectedRow;
int oldSelectedColumn = _selectedColumn;
_selectedCell = aCell;
[self lockFocus];
[self highlightCell: YES atRow: i column: j];
[_window flushWindow];
[aCell setNextState];
[self sendAction];
[self highlightCell: NO atRow: i column: j];
[self unlockFocus];
_selectedCell = oldSelectedCell;
_selectedRow = oldSelectedRow;
_selectedColumn = oldSelectedColumn;
return YES;
}
}
}
return NO;
}
- (void) resetCursorRects
{
NSInteger i;
for (i = 0; i < _numRows; i++)
{
NSInteger j;
for (j = 0; j < _numCols; j++)
{
NSCell *aCell = _cells[i][j];
[aCell resetCursorRect: [self cellFrameAtRow: i column: j]
inView: self];
}
}
}
- (NSString*) toolTipForCell: (NSCell*)cell
{
// FIXME
return @"";
}
- (void) setToolTip: (NSString*)toolTipString forCell: (NSCell*)cell
{
// FIXME
}
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[super encodeWithCoder: aCoder];
if ([aCoder allowsKeyedCoding])
{
GSMatrixFlags matrixFlags;
unsigned int mFlags = 0;
[aCoder encodeObject: [self backgroundColor] forKey: @"NSBackgroundColor"];
[aCoder encodeObject: [self cellBackgroundColor] forKey: @"NSCellBackgroundColor"];
[aCoder encodeObject: [self prototype] forKey: @"NSProtoCell"];
[aCoder encodeObject: NSStringFromClass([self cellClass]) forKey: @"NSCellClass"];
[aCoder encodeSize: _cellSize forKey: @"NSCellSize"];
[aCoder encodeSize: _intercell forKey: @"NSIntercellSpacing"];
/// set the flags...
matrixFlags.isRadio = ([self mode] == NSRadioModeMatrix);
matrixFlags.isList = ([self mode] == NSListModeMatrix);
matrixFlags.isHighlight = ([self mode] == NSHighlightModeMatrix);
matrixFlags.allowsEmptySelection = [self allowsEmptySelection];
matrixFlags.selectionByRect = [self isSelectionByRect];
matrixFlags.drawCellBackground = [self drawsCellBackground];
matrixFlags.drawBackground = [self drawsBackground];
matrixFlags.tabKeyTraversesCells = _tabKeyTraversesCells;
matrixFlags.autosizesCells = _autosizesCells;
// clear unused...
matrixFlags.autoScroll = 0;
matrixFlags.drawingAncestor = 0;
matrixFlags.tabKeyTraversesCellsExplicitly = 0;
matrixFlags.canSearchIncrementally = 0;
matrixFlags.unused = 0;
memcpy((void *)&mFlags,(void *)&matrixFlags,sizeof(unsigned int));
[aCoder encodeInt: mFlags forKey: @"NSMatrixFlags"];
[aCoder encodeInt: _numCols forKey: @"NSNumCols"];
[aCoder encodeInt: _numRows forKey: @"NSNumRows"];
[aCoder encodeObject: [self cells] forKey: @"NSCells"];
[aCoder encodeInt: _selectedColumn forKey: @"NSSelectedCol"];
[aCoder encodeInt: _selectedRow forKey: @"NSSelectedRow"];
}
else
{
[aCoder encodeValueOfObjCType: @encode (int) at: &_mode];
[aCoder encodeValueOfObjCType: @encode (BOOL) at: &_allowsEmptySelection];
[aCoder encodeValueOfObjCType: @encode (BOOL) at: &_selectionByRect];
[aCoder encodeValueOfObjCType: @encode (BOOL) at: &_autosizesCells];
[aCoder encodeValueOfObjCType: @encode (BOOL) at: &_autoscroll];
[aCoder encodeSize: _cellSize];
[aCoder encodeSize: _intercell];
[aCoder encodeObject: _backgroundColor];
[aCoder encodeObject: _cellBackgroundColor];
[aCoder encodeValueOfObjCType: @encode (BOOL) at: &_drawsBackground];
[aCoder encodeValueOfObjCType: @encode (BOOL) at: &_drawsCellBackground];
[aCoder encodeObject: NSStringFromClass (_cellClass)];
[aCoder encodeObject: _cellPrototype];
[aCoder encodeValueOfObjCType: @encode (int) at: &_numRows];
[aCoder encodeValueOfObjCType: @encode (int) at: &_numCols];
/* This is slower, but does not expose NSMatrix internals and will work
with subclasses */
[aCoder encodeObject: [self cells]];
[aCoder encodeConditionalObject: _delegate];
[aCoder encodeConditionalObject: _target];
[aCoder encodeValueOfObjCType: @encode (SEL) at: &_action];
[aCoder encodeValueOfObjCType: @encode (SEL) at: &_doubleAction];
[aCoder encodeValueOfObjCType: @encode (SEL) at: &_errorAction];
[aCoder encodeValueOfObjCType: @encode (BOOL) at: &_tabKeyTraversesCells];
[aCoder encodeObject: [self keyCell]];
/* We do not encode information on selected cells, because this is saved
with the cells themselves */
}
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
Class class;
id cell;
int rows = 0, columns = 0;
NSArray *array;
NSInteger i = 0, count = 0;
self = [super initWithCoder: aDecoder];
if (!self)
return nil;
if ([aDecoder allowsKeyedCoding])
{
if ([aDecoder containsValueForKey: @"NSBackgroundColor"])
{
[self setBackgroundColor: [aDecoder decodeObjectForKey: @"NSBackgroundColor"]];
}
if ([aDecoder containsValueForKey: @"NSCellBackgroundColor"])
{
[self setCellBackgroundColor: [aDecoder decodeObjectForKey: @"NSCellBackgroundColor"]];
}
if ([aDecoder containsValueForKey: @"NSProtoCell"])
{
[self setPrototype: [aDecoder decodeObjectForKey: @"NSProtoCell"]];
}
if ([aDecoder containsValueForKey: @"NSCellClass"])
{
class = NSClassFromString((NSString *)[aDecoder decodeObjectForKey: @"NSCellClass"]);
if (class != Nil)
{
[self setCellClass: class];
}
}
if ([aDecoder containsValueForKey: @"NSCellSize"])
{
// Don't use method here as this would change the frame
_cellSize = [aDecoder decodeSizeForKey: @"NSCellSize"];
}
if ([aDecoder containsValueForKey: @"NSIntercellSpacing"])
{
// Don't use method here as this would change the frame
_intercell = [aDecoder decodeSizeForKey: @"NSIntercellSpacing"];
}
if ([aDecoder containsValueForKey: @"NSMatrixFlags"])
{
int mFlags = [aDecoder decodeIntForKey: @"NSMatrixFlags"];
GSMatrixFlags matrixFlags;
memcpy((void *)&matrixFlags,(void *)&mFlags,sizeof(struct _GSMatrixFlags));
if (matrixFlags.isRadio)
{
[self setMode: NSRadioModeMatrix];
}
else if (matrixFlags.isList)
{
[self setMode: NSListModeMatrix];
}
else if (matrixFlags.isHighlight)
{
[self setMode: NSHighlightModeMatrix];
}
[self setAllowsEmptySelection: matrixFlags.allowsEmptySelection];
[self setSelectionByRect: matrixFlags.selectionByRect];
[self setDrawsCellBackground: matrixFlags.drawCellBackground];
[self setDrawsBackground: matrixFlags.drawBackground];
_autosizesCells = matrixFlags.autosizesCells;
_tabKeyTraversesCells = matrixFlags.tabKeyTraversesCells;
}
if ([aDecoder containsValueForKey: @"NSNumCols"])
{
columns = [aDecoder decodeIntForKey: @"NSNumCols"];
}
if ([aDecoder containsValueForKey: @"NSNumRows"])
{
rows = [aDecoder decodeIntForKey: @"NSNumRows"];
}
array = [aDecoder decodeObjectForKey: @"NSCells"];
[self renewRows: rows columns: columns];
count = [array count];
if (count != rows * columns)
{
NSLog (@"Trying to decode an invalid NSMatrix: cell number does not fit matrix dimension");
// Quick fix to do what we can
if (count > rows * columns)
{
count = rows * columns;
}
}
_selectedRow = _selectedColumn = -1;
for (i = 0; i < count; i++)
{
NSInteger row, column;
cell = [array objectAtIndex: i];
row = i / columns;
column = i % columns;
[self putCell: cell atRow: row column: column];
if ([cell state])
{
[self selectCellAtRow: row column: column];
}
}
// mis-use these variables for selection
rows = -1;
columns = -1;
if ([aDecoder containsValueForKey: @"NSSelectedCol"])
{
columns = [aDecoder decodeIntForKey: @"NSSelectedCol"];
}
if ([aDecoder containsValueForKey: @"NSSelectedRow"])
{
rows = [aDecoder decodeIntForKey: @"NSSelectedRow"];
}
if ((rows != -1) && (columns != -1))
[self selectCellAtRow: rows column: columns];
}
else
{
_myZone = [self zone];
[aDecoder decodeValueOfObjCType: @encode (int) at: &_mode];
[aDecoder decodeValueOfObjCType: @encode (BOOL) at: &_allowsEmptySelection];
[aDecoder decodeValueOfObjCType: @encode (BOOL) at: &_selectionByRect];
[aDecoder decodeValueOfObjCType: @encode (BOOL) at: &_autosizesCells];
[aDecoder decodeValueOfObjCType: @encode (BOOL) at: &_autoscroll];
_cellSize = [aDecoder decodeSize];
_intercell = [aDecoder decodeSize];
[aDecoder decodeValueOfObjCType: @encode (id) at: &_backgroundColor];
[aDecoder decodeValueOfObjCType: @encode (id) at: &_cellBackgroundColor];
[aDecoder decodeValueOfObjCType: @encode (BOOL) at: &_drawsBackground];
[aDecoder decodeValueOfObjCType: @encode (BOOL) at: &_drawsCellBackground];
class = NSClassFromString ((NSString *)[aDecoder decodeObject]);
if (class != Nil)
{
[self setCellClass: class];
}
cell = [aDecoder decodeObject];
if (cell != nil)
{
[self setPrototype: cell];
}
if (_cellPrototype == nil)
{
[self setCellClass: [object_getClass(self) cellClass]];
}
[aDecoder decodeValueOfObjCType: @encode (int) at: &rows];
[aDecoder decodeValueOfObjCType: @encode (int) at: &columns];
/* NB: This works without changes for NSForm */
array = [aDecoder decodeObject];
[self renewRows: rows columns: columns];
count = [array count];
if (count != rows * columns)
{
NSLog (@"Trying to decode an invalid NSMatrix: cell number does not fit matrix dimension");
// Quick fix to do what we can
if (count > rows * columns)
{
count = rows * columns;
}
}
_selectedRow = _selectedColumn = -1;
for (i = 0; i < count; i++)
{
NSInteger row, column;
cell = [array objectAtIndex: i];
row = i / columns;
column = i % columns;
[self putCell: cell atRow: row column: column];
if ([cell state])
{
[self selectCellAtRow: row column: column];
}
}
[aDecoder decodeValueOfObjCType: @encode (id) at: &_delegate];
[aDecoder decodeValueOfObjCType: @encode (id) at: &_target];
[aDecoder decodeValueOfObjCType: @encode (SEL) at: &_action];
[aDecoder decodeValueOfObjCType: @encode (SEL) at: &_doubleAction];
[aDecoder decodeValueOfObjCType: @encode (SEL) at: &_errorAction];
[aDecoder decodeValueOfObjCType: @encode (BOOL) at: &_tabKeyTraversesCells];
[self setKeyCell: [aDecoder decodeObject]];
}
return self;
}
/** <p>Sets the NSMatrix's mode to aMode. See <ref type="type"
id="NSMatrixMode">NSMatrixMode</ref> for more informations. By default
the mode is <ref type="type" id="NSMatrixMode">NSRadioModeMatrix</ref>.
</p><p>See Also: -setMode:</p>
*/
- (void) setMode: (NSMatrixMode)aMode
{
_mode = aMode;
}
/** <p>Returns the NSMatrix's mode. See <ref type="type" id="NSMatrixMode">
NSMatrixMode</ref> for more informations. By default the mode
is <ref type="type" id="NSMatrixMode">NSRadioModeMatrix</ref>.</p>
<p>See Also: -setMode:</p>
*/
- (NSMatrixMode) mode
{
return _mode;
}
/** <p>Sets the cell class used by the NSMatrix when it creates new cells
to classId. The default cell class is a NSActionCell class</p>
<p>See Also: -cellClass -setPrototype: -prototype</p>
*/
- (void) setCellClass: (Class)classId
{
_cellClass = classId;
if (_cellClass == nil)
{
_cellClass = defaultCellClass;
}
_cellNew = [_cellClass methodForSelector: allocSel];
_cellInit = [_cellClass instanceMethodForSelector: initSel];
DESTROY(_cellPrototype);
}
/** <p>Returns the cell class used by the NSMatrix when it creates new cells.
The default cell class is a NSActionCell class</p>
<p>See Also: -setCellClass: -setPrototype: -prototype</p>
*/
- (Class) cellClass
{
return _cellClass;
}
/**<p>Sets the prototype cell used by the NSMatrix when it creates new cells
to aCell. The default cell is NSActionCell</p>
<p>See Also: -cellClass -setPrototype: -prototype</p>
*/
- (void) setPrototype: (NSCell*)aCell
{
ASSIGN(_cellPrototype, aCell);
if (_cellPrototype == nil)
{
[self setCellClass: defaultCellClass];
}
else
{
_cellNew = [_cellPrototype methodForSelector: copySel];
_cellInit = 0;
_cellClass = [aCell class];
}
}
/**<p>Returns the prototype cell used by the NSMatrix when it creates new
cells. The default cell is NSActionCell</p>
<p>See Also: -cellClass -setPrototype: -prototype</p>
*/
- (id) prototype
{
return _cellPrototype;
}
/**<p>Returns the size of the NSMatrix's cells</p>
<p>See Also: -setCellSize:</p>
*/
- (NSSize) cellSize
{
return _cellSize;
}
/** <p>Returns the space size between cells.</p>
<p>See Also: -setIntercellSpacing:</p>
*/
- (NSSize) intercellSpacing
{
return _intercell;
}
/** <p>Sets the background color to <var>aColor</var> and marks self for
display. The background color is used to display the NSMatrix color
( the space between the cells), not the cells ( uses
-setCellBackgroundColor: for that)</p><p>See Also: -backgroundColor
-setCellBackgroundColor: -cellBackgroundColor -drawsBackground
-setDrawsBackground:</p>
*/
- (void) setBackgroundColor: (NSColor*)aColor
{
ASSIGN(_backgroundColor, aColor);
[self setNeedsDisplay: YES];
}
/** <p>Returns the background color The background color is used to display
the NSMatrix color ( the space between the cells), not the cells ( uses
-setCellBackgroundColor: for that)</p> <p>See Also: -setBackgroundColor:
-setCellBackgroundColor: -cellBackgroundColor -drawsBackground
-setDrawsBackground:</p>
*/
- (NSColor*) backgroundColor
{
return _backgroundColor;
}
/** <p>Sets the background color of the NSMatrix's cells to <var>aColor</var>
and marks self for display. </p><p>See Also: -cellBackgroundColor
-backgroundColor -setBackgroundColor: -setDrawsCellBackground:
-drawsCellBackground</p>
*/
- (void) setCellBackgroundColor: (NSColor*)aColor
{
ASSIGN(_cellBackgroundColor, aColor);
[self setNeedsDisplay: YES];
}
/** <p>Returns the background color of the NSMatrix's cells.</p><p>See Also:
-setCellBackgroundColor: -backgroundColor -setBackgroundColor: </p>
*/
- (NSColor*) cellBackgroundColor
{
return _cellBackgroundColor;
}
/**<p>Sets the delegate to <var>anObject</var>. The delegate is used
when editing a cell</p><p>See Also: -delegate -textDidEndEditing:
-textDidBeginEditing: -textDidChange:</p>
*/
- (void) setDelegate: (id)anObject
{
if (_delegate)
[nc removeObserver: _delegate name: nil object: self];
_delegate = anObject;
#define SET_DELEGATE_NOTIFICATION(notif_name) \
if ([_delegate respondsToSelector: @selector(controlText##notif_name:)]) \
[nc addObserver: _delegate \
selector: @selector(controlText##notif_name:) \
name: NSControlText##notif_name##Notification object: self]
if (_delegate)
{
SET_DELEGATE_NOTIFICATION(DidBeginEditing);
SET_DELEGATE_NOTIFICATION(DidEndEditing);
SET_DELEGATE_NOTIFICATION(DidChange);
}
}
/**<p>Returns the NSMatrix's delegate. delegate is used when editing a cell</p>
<p>See Also: -setDelegate: -textDidEndEditing: -textDidBeginEditing:
-textDidChange:</p>
*/
- (id) delegate
{
return _delegate;
}
- (void) setTarget: anObject
{
_target = anObject;
}
- (id) target
{
return _target;
}
/**
* Sets the message to send when a single click occurs.<br />
*/
- (void) setAction: (SEL)aSelector
{
_action = aSelector;
}
- (SEL) action
{
return _action;
}
/** <p>Sets the message to send when a double click occurs.
NB: In GNUstep the following method does *not* set
ignoresMultiClick to NO as in the MacOS-X spec.
It simply sets the doubleAction, as in OpenStep spec.</p>
<p>-doubleAction</p>
*/
- (void) setDoubleAction: (SEL)aSelector
{
_doubleAction = aSelector;
}
/** <p>Returns the action method, used when the user double clicks</p>
<p>See Also: -setDoubleAction:</p>
*/
- (SEL) doubleAction
{
return _doubleAction;
}
/**<p>Sets the error action method to <var>aSelector</var>. This error method
is used when in -textShouldEndEditing: if the selected cell doe not
have a valid text object</p>
<p>See Also: -errorAction</p>
*/
- (void) setErrorAction: (SEL)aSelector
{
_errorAction = aSelector;
}
/**<p>Returns the error action method to <var>aSelector</var>This error method
is used when in -textShouldEndEditing: if the selected cell doe not
have a valid text object</p>
<p>See Also: -setErrorAction:</p>
*/
- (SEL) errorAction
{
return _errorAction;
}
/**<p> Enables or disables all cells of the receiver. </p>
*/
- (void) setEnabled: (BOOL)flag
{
NSInteger i, j;
for (i = 0; i < _numRows; i++)
{
for (j = 0; j < _numCols; j++)
{
[_cells[i][j] setEnabled: flag];
}
}
}
/**<p> Sets a flag to indicate whether the matrix should permit empty
selections or should force one or mor cells to be selected at all times.
</p><p>See Also: -allowsEmptySelection</p>
*/
- (void) setAllowsEmptySelection: (BOOL)flag
{
_allowsEmptySelection = flag;
}
/**<p>Returns whether the matrix should permit empty selections or should
force one or mor cells to be selected at all times.</p>
<p>See Also: -setAllowsEmptySelection:</p>
*/
- (BOOL) allowsEmptySelection
{
return _allowsEmptySelection;
}
- (void) setSelectionByRect: (BOOL)flag
{
_selectionByRect = flag;
}
/**
*/
- (BOOL) isSelectionByRect
{
return _selectionByRect;
}
/** <p>Sets whether the NSMatrix draws its background and marks self for
display.</p>
<p>See Also: -drawsBackground -setDrawsCellBackground:</p>
*/
- (void) setDrawsBackground: (BOOL)flag
{
_drawsBackground = flag;
[self setNeedsDisplay: YES];
}
/** <p>Returns whether the NSMatrix draws its background</p>
<p>See Also: -setDrawsBackground: -drawsCellBackground</p>
*/
- (BOOL) drawsBackground
{
return _drawsBackground;
}
/**<p>Sets whether the NSMatrix draws cells backgrounds and marks self for
display</p><p>See Also: -drawsCellBackground -setDrawsBackground:</p>
*/
- (void) setDrawsCellBackground: (BOOL)flag
{
_drawsCellBackground = flag;
[self setNeedsDisplay: YES];
}
/**<p>Returns whether the NSMatrix draws cells backgrounds</p>
<p>See Also: -setDrawsCellBackground: -drawsBackground</p>
*/
- (BOOL) drawsCellBackground
{
return _drawsCellBackground;
}
/** <p>Sets whether the NSMatrix resizes its cells automatically</p>
<p>See Also: -autosizesCells</p>
*/
- (void) setAutosizesCells: (BOOL)flag
{
_autosizesCells = flag;
}
/** <p>Returns whether the NSMatrix resizes its cells automatically</p>
<p>See Also: -autosizesCells</p>
*/
- (BOOL) autosizesCells
{
return _autosizesCells;
}
- (BOOL) isAutoscroll
{
return _autoscroll;
}
/**<p>Returns the number of rows of the NSMatrix</p>
<p>See Also: -numberOfColumns</p>
*/
- (NSInteger) numberOfRows
{
return _numRows;
}
/**<p>Returns the number of columns of the NSMatrix</p>
<p>See Also: -numberOfRows</p>
*/
- (NSInteger) numberOfColumns
{
return _numCols;
}
- (id) selectedCell
{
return _selectedCell;
}
/**<p>Returns the column number of the selected cell or -1
if no cell is selected</p><p>See Also: -selectedRow -selectedCell</p>
*/
- (NSInteger) selectedColumn
{
return _selectedColumn;
}
/**<p>Returns the row number of the selected cell or -1
if no cell is selected</p><p>See Also: -selectedColumn -selectedCell</p>
*/
- (NSInteger) selectedRow
{
return _selectedRow;
}
- (NSInteger) mouseDownFlags
{
return mouseDownFlags;
}
- (BOOL) isFlipped
{
return YES;
}
- (void) _rebuildLayoutAfterResizing
{
if (_autosizesCells)
{
/* Keep the intercell as it is, and adjust the cell size to fit. */
if (_numRows > 1)
{
_cellSize.height = _bounds.size.height - ((_numRows - 1) * _intercell.height);
_cellSize.height = _cellSize.height / _numRows;
if (_cellSize.height < 0)
{
_cellSize.height = 0;
}
}
else
{
_cellSize.height = _bounds.size.height;
}
if (_numCols > 1)
{
_cellSize.width = _bounds.size.width - ((_numCols - 1) * _intercell.width);
_cellSize.width = _cellSize.width / _numCols;
if (_cellSize.width < 0)
{
_cellSize.width = 0;
}
}
else
{
_cellSize.width = _bounds.size.width;
}
}
}
- (void) setFrame: (NSRect)aFrame
{
[super setFrame: aFrame];
[self _rebuildLayoutAfterResizing];
}
- (void) setFrameSize: (NSSize)aSize
{
[super setFrameSize: aSize];
[self _rebuildLayoutAfterResizing];
}
- (void) _move: (unichar)pos
{
BOOL selectCell = NO;
NSInteger h, i, lastDottedRow, lastDottedColumn;
if (_mode == NSRadioModeMatrix || _mode == NSListModeMatrix)
selectCell = YES;
if (_dottedRow == -1 || _dottedColumn == -1)
{
if (pos == NSUpArrowFunctionKey || pos == NSDownArrowFunctionKey)
{
for (h = 0; h < _numCols; h++)
{
for (i = 0; i < _numRows; i++)
{
if ([_cells[i][h] acceptsFirstResponder])
{
_dottedRow = i;
_dottedColumn = h;
break;
}
}
if (i == _dottedRow)
break;
}
}
else
{
for (i = 0; i < _numRows; i++)
{
for (h = 0; h < _numCols; h++)
{
if ([_cells[i][h] acceptsFirstResponder])
{
_dottedRow = i;
_dottedColumn = h;
break;
}
}
if (h == _dottedColumn)
break;
}
}
if (_dottedRow == -1 || _dottedColumn == -1)
return;
if (selectCell)
{
if (_selectedCell)
{
[self deselectAllCells];
}
[self selectCellAtRow: _dottedRow column: _dottedColumn];
}
else
[self setNeedsDisplayInRect: [self cellFrameAtRow: _dottedRow
column: _dottedColumn]];
}
else
{
lastDottedRow = _dottedRow;
lastDottedColumn = _dottedColumn;
if (pos == NSUpArrowFunctionKey)
{
if (_dottedRow <= 0)
return;
for (i = _dottedRow-1; i >= 0; i--)
{
if ([_cells[i][_dottedColumn] acceptsFirstResponder])
{
_dottedRow = i;
break;
}
}
}
else if (pos == NSDownArrowFunctionKey)
{
if (_dottedRow >= _numRows-1)
return;
for (i = _dottedRow+1; i < _numRows; i++)
{
if ([_cells[i][_dottedColumn] acceptsFirstResponder])
{
_dottedRow = i;
break;
}
}
}
else if (pos == NSLeftArrowFunctionKey)
{
if (_dottedColumn <= 0)
return;
for (i = _dottedColumn-1; i >= 0; i--)
{
if ([_cells[_dottedRow][i] acceptsFirstResponder])
{
_dottedColumn = i;
break;
}
}
}
else
{
if (_dottedColumn >= _numCols-1)
return;
for (i = _dottedColumn+1; i < _numCols; i++)
{
if ([_cells[_dottedRow][i] acceptsFirstResponder])
{
_dottedColumn = i;
break;
}
}
}
if ((pos == NSUpArrowFunctionKey || pos == NSDownArrowFunctionKey)
&& _dottedRow != i)
return;
if ((pos == NSLeftArrowFunctionKey || pos == NSRightArrowFunctionKey)
&& _dottedColumn != i)
return;
if (selectCell)
{
if (_mode == NSRadioModeMatrix)
{
/* FIXME */
/*
NSCell *aCell = _cells[lastDottedRow][lastDottedColumn];
BOOL isHighlighted = [aCell isHighlighted];
if ([aCell state] || isHighlighted)
{
[aCell setState: NSOffState];
_selectedCells[lastDottedRow][lastDottedColumn] = NO;
_selectedRow = _selectedColumn = -1;
_selectedCell = nil;
if (isHighlighted)
[self highlightCell: NO
atRow: lastDottedRow
column: lastDottedColumn];
else
[self drawCell: aCell];
}
*/
}
else
[self deselectAllCells];
[self selectCellAtRow: _dottedRow column: _dottedColumn];
}
else
{
[self setNeedsDisplayInRect: [self cellFrameAtRow: lastDottedRow
column: lastDottedColumn]];
[self setNeedsDisplayInRect: [self cellFrameAtRow: _dottedRow
column: _dottedColumn]];
}
}
if (selectCell)
{
[self displayIfNeeded];
[self performClick: self];
}
}
- (void) moveUp: (id)sender
{
[self _move: NSUpArrowFunctionKey];
}
- (void) moveDown: (id)sender
{
[self _move: NSDownArrowFunctionKey];
}
- (void) moveLeft: (id)sender
{
[self _move: NSLeftArrowFunctionKey];
}
- (void) moveRight: (id)sender
{
[self _move: NSRightArrowFunctionKey];
}
- (void) _shiftModifier: (unichar)character
{
int i, lastDottedRow, lastDottedColumn;
lastDottedRow = _dottedRow;
lastDottedColumn = _dottedColumn;
if (character == NSUpArrowFunctionKey)
{
if (_dottedRow <= 0)
return;
for (i = _dottedRow-1; i >= 0; i--)
{
if ([_cells[i][_dottedColumn] acceptsFirstResponder])
{
_dottedRow = i;
break;
}
}
if (_dottedRow != i)
return;
}
else if (character == NSDownArrowFunctionKey)
{
if (_dottedRow < 0 || _dottedRow >= _numRows-1)
return;
for (i = _dottedRow+1; i < _numRows; i++)
{
if ([_cells[i][_dottedColumn] acceptsFirstResponder])
{
_dottedRow = i;
break;
}
}
}
else if (character == NSLeftArrowFunctionKey)
{
if (_dottedColumn <= 0)
return;
for (i = _dottedColumn-1; i >= 0; i--)
{
if ([_cells[_dottedRow][i] acceptsFirstResponder])
{
_dottedColumn = i;
break;
}
}
}
else
{
if (_dottedColumn < 0 || _dottedColumn >= _numCols-1)
return;
for (i = _dottedColumn+1; i < _numCols; i++)
{
if ([_cells[_dottedRow][i] acceptsFirstResponder])
{
_dottedColumn = i;
break;
}
}
}
[self lockFocus];
[self drawCell: _cells[lastDottedRow][lastDottedColumn]];
[self drawCell: _cells[_dottedRow][_dottedColumn]];
[self unlockFocus];
[_window flushWindow];
[self performClick: self];
}
- (void) _altModifier: (unichar)character
{
switch (character)
{
case NSUpArrowFunctionKey:
if (_dottedRow <= 0)
return;
_dottedRow--;
break;
case NSDownArrowFunctionKey:
if (_dottedRow < 0 || _dottedRow >= _numRows-1)
return;
_dottedRow++;
break;
case NSLeftArrowFunctionKey:
if (_dottedColumn <= 0)
return;
_dottedColumn--;
break;
case NSRightArrowFunctionKey:
if (_dottedColumn < 0 || _dottedColumn >= _numCols-1)
return;
_dottedColumn++;
break;
}
[self setSelectionFrom: INDEX_FROM_COORDS(_selectedColumn, _selectedRow)
to: INDEX_FROM_COORDS(_dottedColumn, _dottedRow)
anchor: INDEX_FROM_COORDS(_selectedColumn, _selectedRow)
highlight: YES];
[self displayIfNeeded];
[self performClick: self];
}
- (void) keyDown: (NSEvent *)theEvent
{
NSString *characters = [theEvent characters];
NSUInteger modifiers = [theEvent modifierFlags];
unichar character = 0;
if ([characters length] > 0)
{
character = [characters characterAtIndex: 0];
}
switch (character)
{
case NSCarriageReturnCharacter:
case NSNewlineCharacter:
case NSEnterCharacter:
[self selectText: self];
break;
case ' ':
if (_dottedRow != -1 && _dottedColumn != -1)
{
if (modifiers & NSAlternateKeyMask)
[self _altModifier: character];
else
{
switch (_mode)
{
case NSTrackModeMatrix:
case NSHighlightModeMatrix:
case NSRadioModeMatrix:
[self selectCellAtRow: _dottedRow column: _dottedColumn];
break;
case NSListModeMatrix:
if (!(modifiers & NSShiftKeyMask))
[self deselectAllCells];
break;
}
[self displayIfNeeded];
[self performClick: self];
}
return;
}
break;
case NSLeftArrowFunctionKey:
case NSRightArrowFunctionKey:
if (_numCols <= 1)
break;
case NSUpArrowFunctionKey:
case NSDownArrowFunctionKey:
if (modifiers & NSShiftKeyMask)
[self _shiftModifier: character];
else if (modifiers & NSAlternateKeyMask)
[self _altModifier: character];
else
{
if (character == NSUpArrowFunctionKey)
[self moveUp: self];
else if (character == NSDownArrowFunctionKey)
[self moveDown: self];
else if (character == NSLeftArrowFunctionKey)
[self moveLeft: self];
else
[self moveRight: self];
}
return;
case NSBackTabCharacter:
if (_tabKeyTraversesCells)
{
if ([self _selectNextSelectableCellAfterRow: _selectedRow
column: _selectedColumn])
return;
}
break;
case NSTabCharacter:
if (_tabKeyTraversesCells)
{
if ([theEvent modifierFlags] & NSShiftKeyMask)
{
if ([self _selectNextSelectableCellAfterRow: _selectedRow
column: _selectedColumn])
return;
}
else
{
if ([self _selectPreviousSelectableCellBeforeRow: _selectedRow
column: _selectedColumn])
return;
}
}
break;
default:
break;
}
[super keyDown: theEvent];
}
- (void) performClick: (id)sender
{
[super sendAction: _action to: _target];
}
- (BOOL) acceptsFirstResponder
{
// We gratefully accept keyboard events.
return YES;
}
- (void) _setNeedsDisplayDottedCell
{
if (_dottedRow != -1 && _dottedColumn != -1)
{
[self setNeedsDisplayInRect: [self cellFrameAtRow: _dottedRow
column: _dottedColumn]];
}
}
- (BOOL) becomeFirstResponder
{
[self _setNeedsDisplayDottedCell];
return YES;
}
- (BOOL) resignFirstResponder
{
[self _setNeedsDisplayDottedCell];
return YES;
}
- (void) becomeKeyWindow
{
[self _setNeedsDisplayDottedCell];
}
- (void) resignKeyWindow
{
[self _setNeedsDisplayDottedCell];
}
- (BOOL) abortEditing
{
if (_textObject)
{
[_selectedCell endEditing: _textObject];
_textObject = nil;
return YES;
}
else
return NO;
}
- (NSText *) currentEditor
{
if (_textObject && ([_window firstResponder] == _textObject))
return _textObject;
else
return nil;
}
- (void) validateEditing
{
if (_textObject)
{
NSFormatter *formatter;
NSString *string;
formatter = [_selectedCell formatter];
string = AUTORELEASE ([[_textObject text] copy]);
if (formatter == nil)
{
[_selectedCell setStringValue: string];
}
else
{
id newObjectValue;
NSString *error;
if ([formatter getObjectValue: &newObjectValue
forString: string
errorDescription: &error] == YES)
{
[_selectedCell setObjectValue: newObjectValue];
}
else
{
if ([_delegate control: self
didFailToFormatString: string
errorDescription: error] == YES)
{
[_selectedCell setStringValue: string];
}
}
}
}
}
- (void) setValue: (id)anObject forKey: (NSString*)aKey
{
if ([aKey isEqual: NSSelectedTagBinding])
{
[self selectCellWithTag: [anObject integerValue]];
}
else
{
[super setValue: anObject forKey: aKey];
}
}
- (id) valueForKey: (NSString*)aKey
{
if ([aKey isEqual: NSSelectedTagBinding])
{
return [NSNumber numberWithInteger: [self selectedTag]];
}
else
{
return [super valueForKey: aKey];
}
}
@end
@implementation NSMatrix (PrivateMethods)
/*
* Renew rows and columns, but when expanding the matrix, refrain from
* creating rowSpace items in the last row and colSpace items in the
* last column. When inserting the contents of an array into the matrix,
* this avoids creation of new cless which would immediately be replaced
* by those from the array.
* NB. new spaces in the matrix are pre-initialised with nil values so
* that replacing them doesn't cause attempts to release random memory.
*/
- (void) _renewRows: (NSInteger)row
columns: (NSInteger)col
rowSpace: (NSInteger)rowSpace
colSpace: (NSInteger)colSpace
{
NSInteger i, j;
NSInteger oldMaxC;
NSInteger oldMaxR;
SEL mkSel = @selector(makeCellAtRow:column:);
IMP mkImp = [self methodForSelector: mkSel];
//NSLog(@"%x - mr: %d mc:%d nr:%d nc:%d r:%d c:%d", (unsigned)self, _maxRows, _maxCols, _numRows, _numCols, row, col);
if (row < 0)
{
#if NSMATRIX_STRICT_CHECKING == 0
NSLog(@"renew negative row (%d) in matrix", (int)row);
#else
[NSException raise: NSRangeException
format: @"renew negative row (%d) in matrix", (int)row];
#endif
row = 0;
}
if (col < 0)
{
#if NSMATRIX_STRICT_CHECKING == 0
NSLog(@"renew negative column (%d) in matrix", (int)col);
#else
[NSException raise: NSRangeException
format: @"renew negative column (%d) in matrix", (int)col];
#endif
col = 0;
}
/*
* Update matrix dimension before we actually change it - so that
* makeCellAtRow:column: doesn't think we are trying to make a cell
* outside the array bounds.
* Our implementation doesn't care, but a subclass might use
* putCell:atRow:column: to implement it, and that checks bounds.
*/
oldMaxC = _maxCols;
_numCols = col;
if (col > _maxCols)
_maxCols = col;
oldMaxR = _maxRows;
_numRows = row;
if (row > _maxRows)
_maxRows = row;
if (col > oldMaxC)
{
NSInteger end = col - 1;
for (i = 0; i < oldMaxR; i++)
{
_cells[i] = NSZoneRealloc(_myZone, _cells[i], col * sizeof(id));
_selectedCells[i] = NSZoneRealloc(_myZone, _selectedCells[i],
col * sizeof(BOOL));
for (j = oldMaxC; j < col; j++)
{
_cells[i][j] = nil;
_selectedCells[i][j] = NO;
if (j == end && colSpace > 0)
{
colSpace--;
}
else
{
(*mkImp)(self, mkSel, i, j);
}
}
}
}
if (row > oldMaxR)
{
NSInteger end = row - 1;
_cells = NSZoneRealloc(_myZone, _cells, row * sizeof(id*));
_selectedCells
= NSZoneRealloc(_myZone, _selectedCells, row * sizeof(BOOL*));
/* Allocate the new rows and fill them */
for (i = oldMaxR; i < row; i++)
{
_cells[i] = NSZoneMalloc(_myZone, _maxCols * sizeof(id));
_selectedCells[i] = NSZoneMalloc(_myZone, _maxCols * sizeof(BOOL));
if (i == end)
{
for (j = 0; j < _maxCols; j++)
{
_cells[i][j] = nil;
_selectedCells[i][j] = NO;
if (rowSpace > 0)
{
rowSpace--;
}
else
{
(*mkImp)(self, mkSel, i, j);
}
}
}
else
{
for (j = 0; j < _maxCols; j++)
{
_cells[i][j] = nil;
_selectedCells[i][j] = NO;
(*mkImp)(self, mkSel, i, j);
}
}
}
}
[self deselectAllCells];
//NSLog(@"%x - end mr: %d mc:%d nr:%d nc:%d r:%d c:%d", (unsigned)self, _maxRows, _maxCols, _numRows, _numCols, row, col);
}
- (void) _setState: (NSInteger)state
highlight: (BOOL)highlight
startIndex: (NSInteger)start
endIndex: (NSInteger)end
{
NSInteger i;
MPoint startPoint = POINT_FROM_INDEX(start);
MPoint endPoint = POINT_FROM_INDEX(end);
for (i = startPoint.y; i <= endPoint.y; i++)
{
NSInteger j;
NSInteger colLimit;
if (_selectionByRect || i == startPoint.y)
{
j = startPoint.x;
}
else
{
j = 0;
}
if (_selectionByRect || i == endPoint.y)
colLimit = endPoint.x;
else
colLimit = _numCols - 1;
for (; j <= colLimit; j++)
{
NSCell *aCell = _cells[i][j];
if ([aCell isEnabled]
&& ([aCell state] != state || [aCell isHighlighted] != highlight
|| (state == NSOffState && _selectedCells[i][j] != NO)
|| (state != NSOffState && _selectedCells[i][j] == NO)))
{
[aCell setState: state];
if (state == NSOffState)
_selectedCells[i][j] = NO;
else
_selectedCells[i][j] = YES;
[aCell setHighlighted: highlight];
[self setNeedsDisplayInRect: [self cellFrameAtRow: i column: j]];
}
}
}
}
// Return YES on success; NO if no selectable cell found.
-(BOOL) _selectNextSelectableCellAfterRow: (NSInteger)row
column: (NSInteger)column
{
NSInteger i, j;
if (row > -1)
{
// First look for cells in the same row
for (j = column + 1; j < _numCols; j++)
{
if ([_cells[row][j] isEnabled] && [_cells[row][j] isSelectable])
{
_selectedCell = [self selectTextAtRow: row column: j];
_selectedRow = row;
_selectedColumn = j;
_selectedCells[row][j] = YES;
return YES;
}
}
}
// Otherwise, make the big cycle.
for (i = row + 1; i < _numRows; i++)
{
for (j = 0; j < _numCols; j++)
{
if ([_cells[i][j] isEnabled] && [_cells[i][j] isSelectable])
{
_selectedCell = [self selectTextAtRow: i column: j];
_selectedRow = i;
_selectedColumn = j;
_selectedCells[i][j] = YES;
return YES;
}
}
}
return NO;
}
-(BOOL) _selectPreviousSelectableCellBeforeRow: (NSInteger)row
column: (NSInteger)column
{
NSInteger i,j;
if (row < _numRows)
{
// First look for cells in the same row
for (j = column - 1; j > -1; j--)
{
if ([_cells[row][j] isEnabled] && [_cells[row][j] isSelectable])
{
_selectedCell = [self selectTextAtRow: row column: j];
_selectedRow = row;
_selectedColumn = j;
_selectedCells[row][j] = YES;
return YES;
}
}
}
// Otherwise, make the big cycle.
for (i = row - 1; i > -1; i--)
{
for (j = _numCols - 1; j > -1; j--)
{
if ([_cells[i][j] isEnabled] && [_cells[i][j] isSelectable])
{
_selectedCell = [self selectTextAtRow: i column: j];
_selectedRow = i;
_selectedColumn = j;
_selectedCells[i][j] = YES;
return YES;
}
}
}
return NO;
}
- (void) _setKeyRow: (NSInteger)row column: (NSInteger)column
{
if (_dottedRow == row && _dottedColumn == column)
{
return;
}
if ([_cells[row][column] acceptsFirstResponder])
{
if (_dottedRow != -1 && _dottedColumn != -1)
{
[self setNeedsDisplayInRect: [self cellFrameAtRow: _dottedRow
column: _dottedColumn]];
}
_dottedRow = row;
_dottedColumn = column;
[self setNeedsDisplayInRect: [self cellFrameAtRow: _dottedRow
column: _dottedColumn]];
}
}
@end
|