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 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
//
// Authors:
// Peter Bartok pbartok@novell.com
//
//
// NOT COMPLETE
// There's still plenty of things missing, I've got most of it planned, just hadn't had
// the time to write it all yet.
// Stuff missing (in no particular order):
// - Align text after RecalculateLine
// - Implement tag types for hotlinks, etc.
// - Implement CaretPgUp/PgDown
// NOTE:
// selection_start.pos and selection_end.pos are 0-based
// selection_start.pos = first selected char
// selection_end.pos = first NOT-selected char
//
// FormatText methods are 1-based (as are all tags, LineTag.Start is 1 for
// the first character on a line; the reason is that 0 is the position
// *before* the first character on a line
#undef Debug
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Text;
using System.Text;
using RTF=System.Windows.Forms.RTF;
namespace System.Windows.Forms {
internal enum LineColor {
Red = 0,
Black = 1
}
internal enum CaretSelection {
Position, // Selection=Caret
Word, // Selection=Word under caret
Line // Selection=Line under caret
}
[Flags]
internal enum FormatSpecified {
None,
BackColor = 2,
Font = 4,
Color = 8,
}
internal enum CaretDirection {
CharForward, // Move a char to the right
CharBack, // Move a char to the left
LineUp, // Move a line up
LineDown, // Move a line down
Home, // Move to the beginning of the line
End, // Move to the end of the line
PgUp, // Move one page up
PgDn, // Move one page down
CtrlPgUp, // Move caret to the first visible char in the viewport
CtrlPgDn, // Move caret to the last visible char in the viewport
CtrlHome, // Move to the beginning of the document
CtrlEnd, // Move to the end of the document
WordBack, // Move to the beginning of the previous word (or beginning of line)
WordForward, // Move to the beginning of the next word (or end of line)
SelectionStart, // Move to the beginning of the current selection
SelectionEnd, // Move to the end of the current selection
CharForwardNoWrap, // Move a char forward, but don't wrap onto the next line
CharBackNoWrap // Move a char backward, but don't wrap onto the previous line
}
internal enum LineEnding {
Wrap = 1, // line wraps to the next line
Limp = 2, // \r
Hard = 4, // \r\n
Soft = 8, // \r\r\n
Rich = 16, // \n
None = 0
}
internal class Document : ICloneable, IEnumerable {
#region Structures
// FIXME - go through code and check for places where
// we do explicit comparisons instead of using the compare overloads
internal struct Marker {
internal Line line;
internal LineTag tag;
internal int pos;
internal int height;
public static bool operator<(Marker lhs, Marker rhs) {
if (lhs.line.line_no < rhs.line.line_no) {
return true;
}
if (lhs.line.line_no == rhs.line.line_no) {
if (lhs.pos < rhs.pos) {
return true;
}
}
return false;
}
public static bool operator>(Marker lhs, Marker rhs) {
if (lhs.line.line_no > rhs.line.line_no) {
return true;
}
if (lhs.line.line_no == rhs.line.line_no) {
if (lhs.pos > rhs.pos) {
return true;
}
}
return false;
}
public static bool operator==(Marker lhs, Marker rhs) {
if ((lhs.line.line_no == rhs.line.line_no) && (lhs.pos == rhs.pos)) {
return true;
}
return false;
}
public static bool operator!=(Marker lhs, Marker rhs) {
if ((lhs.line.line_no != rhs.line.line_no) || (lhs.pos != rhs.pos)) {
return true;
}
return false;
}
public void Combine(Line move_to_line, int move_to_line_length) {
line = move_to_line;
pos += move_to_line_length;
tag = LineTag.FindTag(line, pos);
}
// This is for future use, right now Document.Split does it by hand, with some added shortcut logic
public void Split(Line move_to_line, int split_at) {
line = move_to_line;
pos -= split_at;
tag = LineTag.FindTag(line, pos);
}
public override bool Equals(object obj) {
return this==(Marker)obj;
}
public override int GetHashCode() {
return base.GetHashCode ();
}
public override string ToString() {
return "Marker Line " + line + ", Position " + pos;
}
}
#endregion Structures
#region Local Variables
private Line document;
private int lines;
private Line sentinel;
private int document_id;
private Random random = new Random();
internal string password_char;
private StringBuilder password_cache;
private bool calc_pass;
private int char_count;
private bool enable_links;
// For calculating widths/heights
public static readonly StringFormat string_format = new StringFormat (StringFormat.GenericTypographic);
private int recalc_suspended;
private bool recalc_pending;
private int recalc_start = 1; // This starts at one, since lines are 1 based
private int recalc_end;
private bool recalc_optimize;
private int update_suspended;
private bool update_pending;
private int update_start = 1;
internal bool multiline;
internal HorizontalAlignment alignment;
internal bool wrap;
internal UndoManager undo;
internal Marker caret;
internal Marker selection_start;
internal Marker selection_end;
internal bool selection_visible;
internal Marker selection_anchor;
internal Marker selection_prev;
internal bool selection_end_anchor;
internal int viewport_x;
internal int viewport_y; // The visible area of the document
internal int offset_x;
internal int offset_y;
internal int viewport_width;
internal int viewport_height;
internal int document_x; // Width of the document
internal int document_y; // Height of the document
internal int crlf_size; // 1 or 2, depending on whether we use \r\n or just \n
internal TextBoxBase owner; // Who's owning us?
static internal int caret_width = 1;
static internal int caret_shift = 1;
internal int left_margin = 2; // A left margin for all lines
internal int top_margin = 2;
internal int right_margin = 2;
#endregion // Local Variables
#region Constructors
internal Document (TextBoxBase owner)
{
lines = 0;
this.owner = owner;
multiline = true;
password_char = "";
calc_pass = false;
recalc_pending = false;
// Tree related stuff
sentinel = new Line (this, LineEnding.None);
sentinel.color = LineColor.Black;
document = sentinel;
// We always have a blank line
owner.HandleCreated += new EventHandler(owner_HandleCreated);
owner.VisibleChanged += new EventHandler(owner_VisibleChanged);
Add (1, String.Empty, owner.Font, owner.ForeColor, LineEnding.None);
undo = new UndoManager (this);
selection_visible = false;
selection_start.line = this.document;
selection_start.pos = 0;
selection_start.tag = selection_start.line.tags;
selection_end.line = this.document;
selection_end.pos = 0;
selection_end.tag = selection_end.line.tags;
selection_anchor.line = this.document;
selection_anchor.pos = 0;
selection_anchor.tag = selection_anchor.line.tags;
caret.line = this.document;
caret.pos = 0;
caret.tag = caret.line.tags;
viewport_x = 0;
viewport_y = 0;
offset_x = 0;
offset_y = 0;
crlf_size = 2;
// Default selection is empty
document_id = random.Next();
string_format.Trimming = StringTrimming.None;
string_format.FormatFlags = StringFormatFlags.DisplayFormatControl;
UpdateMargins ();
}
#endregion
#region Internal Properties
internal Line Root {
get {
return document;
}
set {
document = value;
}
}
// UIA: Method used via reflection in TextRangeProvider
internal int Lines {
get {
return lines;
}
}
internal Line CaretLine {
get {
return caret.line;
}
}
internal int CaretPosition {
get {
return caret.pos;
}
}
internal Point Caret {
get {
return new Point((int)caret.tag.Line.widths[caret.pos] + caret.line.X, caret.line.Y);
}
}
internal LineTag CaretTag {
get {
return caret.tag;
}
set {
caret.tag = value;
}
}
internal int CRLFSize {
get {
return crlf_size;
}
set {
crlf_size = value;
}
}
/// <summary>
/// Whether text is scanned for links
/// </summary>
internal bool EnableLinks {
get { return enable_links; }
set { enable_links = value; }
}
internal string PasswordChar {
get {
return password_char;
}
set {
password_char = value;
PasswordCache.Length = 0;
if ((password_char.Length != 0) && (password_char[0] != '\0')) {
calc_pass = true;
} else {
calc_pass = false;
}
}
}
private StringBuilder PasswordCache {
get {
if (password_cache == null)
password_cache = new StringBuilder();
return password_cache;
}
}
internal int ViewPortX {
get {
return viewport_x;
}
set {
viewport_x = value;
}
}
internal int Length {
get {
return char_count + lines - 1; // Add \n for each line but the last
}
}
private int CharCount {
get {
return char_count;
}
set {
char_count = value;
if (LengthChanged != null) {
LengthChanged(this, EventArgs.Empty);
}
}
}
internal int ViewPortY {
get {
return viewport_y;
}
set {
viewport_y = value;
}
}
internal int OffsetX
{
get
{
return offset_x;
}
set
{
offset_x = value;
}
}
internal int OffsetY
{
get
{
return offset_y;
}
set
{
offset_y = value;
}
}
internal int ViewPortWidth {
get {
return viewport_width;
}
set {
viewport_width = value;
}
}
internal int ViewPortHeight {
get {
return viewport_height;
}
set {
viewport_height = value;
}
}
internal int Width {
get {
return this.document_x;
}
}
internal int Height {
get {
return this.document_y;
}
}
internal bool SelectionVisible {
get {
return selection_visible;
}
}
internal bool Wrap {
get {
return wrap;
}
set {
wrap = value;
}
}
#endregion // Internal Properties
#region Private Methods
internal void UpdateMargins ()
{
switch (owner.actual_border_style) {
case BorderStyle.None:
left_margin = 0;
top_margin = 0;
right_margin = 1;
break;
case BorderStyle.FixedSingle:
left_margin = 2;
top_margin = 2;
right_margin = 3;
break;
case BorderStyle.Fixed3D:
left_margin = 1;
top_margin = 1;
right_margin = 2;
break;
}
}
internal void SuspendRecalc ()
{
if (recalc_suspended == 0) {
recalc_start = int.MaxValue;
recalc_end = int.MinValue;
}
recalc_suspended++;
}
internal void ResumeRecalc (bool immediate_update)
{
if (recalc_suspended > 0)
recalc_suspended--;
if (recalc_suspended == 0 && (immediate_update || recalc_pending) && !(recalc_start == int.MaxValue && recalc_end == int.MinValue)) {
RecalculateDocument (owner.CreateGraphicsInternal (), recalc_start, recalc_end, recalc_optimize);
recalc_pending = false;
}
}
internal void SuspendUpdate ()
{
update_suspended++;
}
internal void ResumeUpdate (bool immediate_update)
{
if (update_suspended > 0)
update_suspended--;
if (immediate_update && update_suspended == 0 && update_pending) {
UpdateView (GetLine (update_start), 0);
update_pending = false;
}
}
// For debugging
internal int DumpTree(Line line, bool with_tags) {
int total;
total = 1;
Console.Write("Line {0} [# {1}], Y: {2}, ending style: {3}, Text: '{4}'",
line.line_no, line.GetHashCode(), line.Y, line.ending,
line.text != null ? line.text.ToString() : "undefined");
if (line.left == sentinel) {
Console.Write(", left = sentinel");
} else if (line.left == null) {
Console.Write(", left = NULL");
}
if (line.right == sentinel) {
Console.Write(", right = sentinel");
} else if (line.right == null) {
Console.Write(", right = NULL");
}
Console.WriteLine("");
if (with_tags) {
LineTag tag;
int count;
int length;
tag = line.tags;
count = 1;
length = 0;
Console.Write(" Tags: ");
while (tag != null) {
Console.Write("{0} <{1}>-<{2}>", count++, tag.Start, tag.End
/*line.text.ToString (tag.start - 1, tag.length)*/);
length += tag.Length;
if (tag.Line != line) {
Console.Write("BAD line link");
throw new Exception("Bad line link in tree");
}
tag = tag.Next;
if (tag != null) {
Console.Write(", ");
}
}
if (length > line.text.Length) {
throw new Exception(String.Format("Length of tags more than length of text on line (expected {0} calculated {1})", line.text.Length, length));
} else if (length < line.text.Length) {
throw new Exception(String.Format("Length of tags less than length of text on line (expected {0} calculated {1})", line.text.Length, length));
}
Console.WriteLine("");
}
if (line.left != null) {
if (line.left != sentinel) {
total += DumpTree(line.left, with_tags);
}
} else {
if (line != sentinel) {
throw new Exception("Left should not be NULL");
}
}
if (line.right != null) {
if (line.right != sentinel) {
total += DumpTree(line.right, with_tags);
}
} else {
if (line != sentinel) {
throw new Exception("Right should not be NULL");
}
}
for (int i = 1; i <= this.lines; i++) {
if (GetLine(i) == null) {
throw new Exception(String.Format("Hole in line order, missing {0}", i));
}
}
if (line == this.Root) {
if (total < this.lines) {
throw new Exception(String.Format("Not enough nodes in tree, found {0}, expected {1}", total, this.lines));
} else if (total > this.lines) {
throw new Exception(String.Format("Too many nodes in tree, found {0}, expected {1}", total, this.lines));
}
}
return total;
}
private void SetSelectionVisible (bool value)
{
bool old_selection_visible = selection_visible;
selection_visible = value;
// cursor and selection are enemies, we can't have both in the same room at the same time
if (owner.IsHandleCreated && !owner.show_caret_w_selection)
XplatUI.CaretVisible (owner.Handle, !selection_visible);
if (UIASelectionChanged != null && (selection_visible || old_selection_visible))
UIASelectionChanged (this, EventArgs.Empty);
}
private void DecrementLines(int line_no) {
int current;
current = line_no;
while (current <= lines) {
GetLine(current).line_no--;
current++;
}
return;
}
private void IncrementLines(int line_no) {
int current;
current = this.lines;
while (current >= line_no) {
GetLine(current).line_no++;
current--;
}
return;
}
private void RebalanceAfterAdd(Line line1) {
Line line2;
while ((line1 != document) && (line1.parent.color == LineColor.Red)) {
if (line1.parent == line1.parent.parent.left) {
line2 = line1.parent.parent.right;
if ((line2 != null) && (line2.color == LineColor.Red)) {
line1.parent.color = LineColor.Black;
line2.color = LineColor.Black;
line1.parent.parent.color = LineColor.Red;
line1 = line1.parent.parent;
} else {
if (line1 == line1.parent.right) {
line1 = line1.parent;
RotateLeft(line1);
}
line1.parent.color = LineColor.Black;
line1.parent.parent.color = LineColor.Red;
RotateRight(line1.parent.parent);
}
} else {
line2 = line1.parent.parent.left;
if ((line2 != null) && (line2.color == LineColor.Red)) {
line1.parent.color = LineColor.Black;
line2.color = LineColor.Black;
line1.parent.parent.color = LineColor.Red;
line1 = line1.parent.parent;
} else {
if (line1 == line1.parent.left) {
line1 = line1.parent;
RotateRight(line1);
}
line1.parent.color = LineColor.Black;
line1.parent.parent.color = LineColor.Red;
RotateLeft(line1.parent.parent);
}
}
}
document.color = LineColor.Black;
}
private void RebalanceAfterDelete(Line line1) {
Line line2;
while ((line1 != document) && (line1.color == LineColor.Black)) {
if (line1 == line1.parent.left) {
line2 = line1.parent.right;
if (line2.color == LineColor.Red) {
line2.color = LineColor.Black;
line1.parent.color = LineColor.Red;
RotateLeft(line1.parent);
line2 = line1.parent.right;
}
if ((line2.left.color == LineColor.Black) && (line2.right.color == LineColor.Black)) {
line2.color = LineColor.Red;
line1 = line1.parent;
} else {
if (line2.right.color == LineColor.Black) {
line2.left.color = LineColor.Black;
line2.color = LineColor.Red;
RotateRight(line2);
line2 = line1.parent.right;
}
line2.color = line1.parent.color;
line1.parent.color = LineColor.Black;
line2.right.color = LineColor.Black;
RotateLeft(line1.parent);
line1 = document;
}
} else {
line2 = line1.parent.left;
if (line2.color == LineColor.Red) {
line2.color = LineColor.Black;
line1.parent.color = LineColor.Red;
RotateRight(line1.parent);
line2 = line1.parent.left;
}
if ((line2.right.color == LineColor.Black) && (line2.left.color == LineColor.Black)) {
line2.color = LineColor.Red;
line1 = line1.parent;
} else {
if (line2.left.color == LineColor.Black) {
line2.right.color = LineColor.Black;
line2.color = LineColor.Red;
RotateLeft(line2);
line2 = line1.parent.left;
}
line2.color = line1.parent.color;
line1.parent.color = LineColor.Black;
line2.left.color = LineColor.Black;
RotateRight(line1.parent);
line1 = document;
}
}
}
line1.color = LineColor.Black;
}
private void RotateLeft(Line line1) {
Line line2 = line1.right;
line1.right = line2.left;
if (line2.left != sentinel) {
line2.left.parent = line1;
}
if (line2 != sentinel) {
line2.parent = line1.parent;
}
if (line1.parent != null) {
if (line1 == line1.parent.left) {
line1.parent.left = line2;
} else {
line1.parent.right = line2;
}
} else {
document = line2;
}
line2.left = line1;
if (line1 != sentinel) {
line1.parent = line2;
}
}
private void RotateRight(Line line1) {
Line line2 = line1.left;
line1.left = line2.right;
if (line2.right != sentinel) {
line2.right.parent = line1;
}
if (line2 != sentinel) {
line2.parent = line1.parent;
}
if (line1.parent != null) {
if (line1 == line1.parent.right) {
line1.parent.right = line2;
} else {
line1.parent.left = line2;
}
} else {
document = line2;
}
line2.right = line1;
if (line1 != sentinel) {
line1.parent = line2;
}
}
internal void UpdateView(Line line, int pos) {
if (!owner.IsHandleCreated) {
return;
}
if (update_suspended > 0) {
update_start = Math.Min (update_start, line.line_no);
// update_end = Math.Max (update_end, line.line_no);
// recalc_optimize = true;
update_pending = true;
return;
}
// Optimize invalidation based on Line alignment
if (RecalculateDocument(owner.CreateGraphicsInternal(), line.line_no, line.line_no, true)) {
// Lineheight changed, invalidate the rest of the document
if ((line.Y - viewport_y) >=0 ) {
// We formatted something that's in view, only draw parts of the screen
owner.Invalidate(new Rectangle(
offset_x,
line.Y - viewport_y + offset_y,
viewport_width,
owner.Height - (line.Y - viewport_y)));
} else {
// The tag was above the visible area, draw everything
owner.Invalidate();
}
} else {
switch(line.alignment) {
case HorizontalAlignment.Left: {
owner.Invalidate(new Rectangle(
line.X + ((int)line.widths[pos] - viewport_x - 1) + offset_x,
line.Y - viewport_y + offset_y,
viewport_width,
line.height + 1));
break;
}
case HorizontalAlignment.Center: {
owner.Invalidate(new Rectangle(
line.X + offset_x,
line.Y - viewport_y + offset_y,
viewport_width,
line.height + 1));
break;
}
case HorizontalAlignment.Right: {
owner.Invalidate(new Rectangle(
line.X + offset_x,
line.Y - viewport_y + offset_y,
(int)line.widths[pos + 1] - viewport_x + line.X,
line.height + 1));
break;
}
}
}
}
// Update display from line, down line_count lines; pos is unused, but required for the signature
internal void UpdateView(Line line, int line_count, int pos) {
if (!owner.IsHandleCreated) {
return;
}
if (recalc_suspended > 0) {
recalc_start = Math.Min (recalc_start, line.line_no);
recalc_end = Math.Max (recalc_end, line.line_no + line_count);
recalc_optimize = true;
recalc_pending = true;
return;
}
int start_line_top = line.Y;
Line end_line = GetLine (line.line_no + line_count);
if (end_line == null)
end_line = GetLine (lines);
if (end_line == null)
return;
int end_line_bottom = end_line.Y + end_line.height;
if (RecalculateDocument(owner.CreateGraphicsInternal(), line.line_no, line.line_no + line_count, true)) {
// Lineheight changed, invalidate the rest of the document
if ((line.Y - viewport_y) >=0 ) {
// We formatted something that's in view, only draw parts of the screen
owner.Invalidate(new Rectangle(
offset_x,
line.Y - viewport_y + offset_y,
viewport_width,
owner.Height - (line.Y - viewport_y)));
} else {
// The tag was above the visible area, draw everything
owner.Invalidate();
}
} else {
int x = 0 - viewport_x + offset_x;
int w = viewport_width;
int y = Math.Min (start_line_top - viewport_y, line.Y - viewport_y) + offset_y;
int h = Math.Max (end_line_bottom - y, end_line.Y + end_line.height - y);
owner.Invalidate (new Rectangle (x, y, w, h));
}
}
/// <summary>
/// Scans the next paragraph for http:/ ftp:/ www. https:/ etc and marks the tags
/// as links.
/// </summary>
/// <param name="start_line">The line to start on</param>
/// <param name="link_changed">marks as true if something is changed</param>
private void ScanForLinks (Line start_line, ref bool link_changed)
{
Line current_line = start_line;
StringBuilder line_no_breaks = new StringBuilder ();
StringBuilder line_link_record = new StringBuilder ();
ArrayList cumulative_length_list = new ArrayList ();
bool update_caret_tag = false;
cumulative_length_list.Add (0);
while (current_line != null) {
line_no_breaks.Append (current_line.text);
if (link_changed == false)
current_line.LinkRecord (line_link_record);
current_line.ClearLinks ();
cumulative_length_list.Add (line_no_breaks.Length);
if (current_line.ending == LineEnding.Wrap)
current_line = GetLine (current_line.LineNo + 1);
else
break;
}
// search for protocols.. make sure www. is first!
string [] search_terms = new string [] { "www.", "http:/", "ftp:/", "https:/" };
int search_found = 0;
int index_found = 0;
string line_no_breaks_string = line_no_breaks.ToString ();
int line_no_breaks_index = 0;
int link_end = 0;
while (true) {
if (line_no_breaks_index >= line_no_breaks_string.Length)
break;
index_found = FirstIndexOfAny (line_no_breaks_string, search_terms, line_no_breaks_index, out search_found);
//no links found on this line
if (index_found == -1)
break;
if (search_found == 0) {
// if we are at the end of the line to analyse and the end of the line
// is "www." then there are no links here
if (line_no_breaks_string.Length == index_found + search_terms [0].Length)
break;
// if after www. we don't have a letter a digit or a @ or - or /
// then it is not a web address, we should continue searching
if (char.IsLetterOrDigit (line_no_breaks_string [index_found + search_terms [0].Length]) == false &&
"@/~".IndexOf (line_no_breaks_string [index_found + search_terms [0].Length].ToString ()) == -1) {
line_no_breaks_index = index_found + search_terms [0].Length;
continue;
}
}
link_end = line_no_breaks_string.Length - 1;
line_no_breaks_index = line_no_breaks_string.Length;
// we've found a link, we just need to find where it ends now
for (int i = index_found + search_terms [search_found].Length; i < line_no_breaks_string.Length; i++) {
if (line_no_breaks_string [i - 1] == '.') {
if (char.IsLetterOrDigit (line_no_breaks_string [i]) == false &&
"@/~".IndexOf (line_no_breaks_string [i].ToString ()) == -1) {
link_end = i - 1;
line_no_breaks_index = i;
break;
}
} else {
if (char.IsLetterOrDigit (line_no_breaks_string [i]) == false &&
"@-/:~.?=_&".IndexOf (line_no_breaks_string [i].ToString ()) == -1) {
link_end = i - 1;
line_no_breaks_index = i;
break;
}
}
}
string link_text = line_no_breaks_string.Substring (index_found, link_end - index_found + 1);
int current_cumulative = 0;
// we've found a link - index_found -> link_end
// now we just make all the tags as containing link and
// point them to the text for the whole link
current_line = start_line;
//find the line we start on
for (current_cumulative = 1; current_cumulative < cumulative_length_list.Count; current_cumulative++)
if ((int)cumulative_length_list [current_cumulative] > index_found)
break;
current_line = GetLine (start_line.LineNo + current_cumulative - 1);
// find the tag we start on
LineTag current_tag = current_line.FindTag (index_found - (int)cumulative_length_list [current_cumulative - 1] + 1);
if (current_tag.Start != (index_found - (int)cumulative_length_list [current_cumulative - 1]) + 1) {
if (current_tag == CaretTag)
update_caret_tag = true;
current_tag = current_tag.Break ((index_found - (int)cumulative_length_list [current_cumulative - 1]) + 1);
}
// set the tag
current_tag.IsLink = true;
current_tag.LinkText = link_text;
//go through each character
// find the tag we are in
// skip the number of characters in the tag
for (int i = 1; i < link_text.Length; i++) {
// on to a new word-wrapped line
if ((int)cumulative_length_list [current_cumulative] <= index_found + i) {
current_line = GetLine (start_line.LineNo + current_cumulative++);
current_tag = current_line.FindTag (index_found + i - (int)cumulative_length_list [current_cumulative - 1] + 1);
current_tag.IsLink = true;
current_tag.LinkText = link_text;
continue;
}
if (current_tag.End < index_found + 1 + i - (int)cumulative_length_list [current_cumulative - 1]) {
// skip empty tags in the middle of the URL
do {
current_tag = current_tag.Next;
} while (current_tag.Length == 0);
current_tag.IsLink = true;
current_tag.LinkText = link_text;
}
}
//if there are characters left in the tag after the link
// split the tag
// make the second part a non link
if (current_tag.End > (index_found + link_text.Length + 1) - (int)cumulative_length_list [current_cumulative - 1]) {
if (current_tag == CaretTag)
update_caret_tag = true;
current_tag.Break ((index_found + link_text.Length + 1) - (int)cumulative_length_list [current_cumulative - 1]);
}
}
if (update_caret_tag) {
CaretTag = LineTag.FindTag (CaretLine, CaretPosition);
link_changed = true;
} else {
if (link_changed == false) {
current_line = start_line;
StringBuilder new_link_record = new StringBuilder ();
while (current_line != null) {
current_line.LinkRecord (new_link_record);
if (current_line.ending == LineEnding.Wrap)
current_line = GetLine (current_line.LineNo + 1);
else
break;
}
if (new_link_record.Equals (line_link_record) == false)
link_changed = true;
}
}
}
private int FirstIndexOfAny (string haystack, string [] needles, int start_index, out int term_found)
{
term_found = -1;
int best_index = -1;
for (int i = 0; i < needles.Length; i++) {
int index = haystack.IndexOf (needles [i], start_index, StringComparison.InvariantCultureIgnoreCase);
if (index > -1) {
if (term_found > -1) {
if (index < best_index) {
best_index = index;
term_found = i;
}
} else {
best_index = index;
term_found = i;
}
}
}
return best_index;
}
private void InvalidateLinks (Rectangle clip)
{
for (int i = (owner.list_links.Count - 1); i >= 0; i--) {
TextBoxBase.LinkRectangle link = (TextBoxBase.LinkRectangle) owner.list_links [i];
if (clip.IntersectsWith (link.LinkAreaRectangle))
owner.list_links.RemoveAt (i);
}
}
#endregion // Private Methods
#region Internal Methods
internal void ScanForLinks (int start, int end, ref bool link_changed)
{
Line line = null;
LineEnding lastending = LineEnding.Rich;
// make sure we start scanning at the real begining of the line
while (true) {
if (start != 1 && GetLine (start - 1).ending == LineEnding.Wrap)
start--;
else
break;
}
for (int i = start; i <= end && i <= lines; i++) {
line = GetLine (i);
if (lastending != LineEnding.Wrap)
ScanForLinks (line, ref link_changed);
lastending = line.ending;
if (lastending == LineEnding.Wrap && (i + 1) <= end)
end++;
}
}
// Clear the document and reset state
internal void Empty() {
document = sentinel;
lines = 0;
// We always have a blank line
Add (1, String.Empty, owner.Font, owner.ForeColor, LineEnding.None);
this.RecalculateDocument(owner.CreateGraphicsInternal());
PositionCaret(0, 0);
SetSelectionVisible (false);
selection_start.line = this.document;
selection_start.pos = 0;
selection_start.tag = selection_start.line.tags;
selection_end.line = this.document;
selection_end.pos = 0;
selection_end.tag = selection_end.line.tags;
char_count = 0;
viewport_x = 0;
viewport_y = 0;
document_x = 0;
document_y = 0;
if (owner.IsHandleCreated)
owner.Invalidate ();
}
internal void PositionCaret(Line line, int pos) {
caret.tag = line.FindTag (pos);
MoveCaretToTextTag ();
caret.line = line;
caret.pos = pos;
if (owner.IsHandleCreated) {
if (owner.Focused) {
if (caret.height != caret.tag.Height)
XplatUI.CreateCaret (owner.Handle, caret_width, caret.height);
XplatUI.SetCaretPos(owner.Handle,
offset_x + (int)caret.tag.Line.widths[caret.pos] + caret.line.X - viewport_x,
offset_y + caret.line.Y + caret.tag.Shift - viewport_y + caret_shift);
}
if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
}
// We set this at the end because we use the heights to determine whether or
// not we need to recreate the caret
caret.height = caret.tag.Height;
}
internal void PositionCaret(int x, int y) {
if (!owner.IsHandleCreated) {
return;
}
caret.tag = FindCursor(x, y, out caret.pos);
MoveCaretToTextTag ();
caret.line = caret.tag.Line;
caret.height = caret.tag.Height;
if (owner.ShowSelection && (!selection_visible || owner.show_caret_w_selection)) {
XplatUI.CreateCaret (owner.Handle, caret_width, caret.height);
XplatUI.SetCaretPos(owner.Handle,
(int)caret.tag.Line.widths[caret.pos] + caret.line.X - viewport_x + offset_x,
offset_y + caret.line.Y + caret.tag.Shift - viewport_y + caret_shift);
}
if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
}
internal void CaretHasFocus() {
if ((caret.tag != null) && owner.IsHandleCreated) {
XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
XplatUI.SetCaretPos(owner.Handle,
offset_x + (int)caret.tag.Line.widths[caret.pos] + caret.line.X - viewport_x,
offset_y + caret.line.Y + caret.tag.Shift - viewport_y + caret_shift);
DisplayCaret ();
}
if (owner.IsHandleCreated && SelectionLength () > 0) {
InvalidateSelectionArea ();
}
}
internal void CaretLostFocus() {
if (!owner.IsHandleCreated) {
return;
}
XplatUI.DestroyCaret(owner.Handle);
}
internal void AlignCaret ()
{
AlignCaret (true);
}
internal void AlignCaret(bool changeCaretTag) {
if (!owner.IsHandleCreated) {
return;
}
if (changeCaretTag) {
caret.tag = LineTag.FindTag (caret.line, caret.pos);
MoveCaretToTextTag ();
}
// if the caret has had SelectionFont changed to a
// different height, we reflect changes unless the new
// font is larger than the line (line recalculations
// ignore empty tags) in which case we make it equal
// the line height and then when text is entered
if (caret.tag.Height > caret.tag.Line.Height) {
caret.height = caret.line.height;
} else {
caret.height = caret.tag.Height;
}
if (owner.Focused) {
XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
XplatUI.SetCaretPos (owner.Handle,
offset_x + (int) caret.tag.Line.widths [caret.pos] + caret.line.X - viewport_x,
offset_y + caret.line.Y + viewport_y + caret_shift);
DisplayCaret ();
}
if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
}
internal void UpdateCaret() {
if (!owner.IsHandleCreated || caret.tag == null) {
return;
}
MoveCaretToTextTag ();
if (caret.tag.Height != caret.height) {
caret.height = caret.tag.Height;
if (owner.Focused) {
XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
}
}
if (owner.Focused) {
XplatUI.SetCaretPos(owner.Handle,
offset_x + (int)caret.tag.Line.widths[caret.pos] + caret.line.X - viewport_x,
offset_y + caret.line.Y + caret.tag.Shift - viewport_y + caret_shift);
DisplayCaret ();
}
if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
}
internal void DisplayCaret() {
if (!owner.IsHandleCreated) {
return;
}
if (owner.ShowSelection && (!selection_visible || owner.show_caret_w_selection)) {
XplatUI.CaretVisible(owner.Handle, true);
}
}
internal void HideCaret() {
if (!owner.IsHandleCreated) {
return;
}
if (owner.Focused) {
XplatUI.CaretVisible(owner.Handle, false);
}
}
internal void MoveCaretToTextTag ()
{
if (caret.tag == null || caret.tag.IsTextTag)
return;
if (caret.pos < caret.tag.Start) {
caret.tag = caret.tag.Previous;
} else {
caret.tag = caret.tag.Next;
}
}
internal void MoveCaret(CaretDirection direction) {
// FIXME should we use IsWordSeparator to detect whitespace, instead
// of looking for actual spaces in the Word move cases?
bool nowrap = false;
switch(direction) {
case CaretDirection.CharForwardNoWrap:
nowrap = true;
goto case CaretDirection.CharForward;
case CaretDirection.CharForward: {
caret.pos++;
if (caret.pos > caret.line.TextLengthWithoutEnding ()) {
if (!nowrap) {
// Go into next line
if (caret.line.line_no < this.lines) {
caret.line = GetLine(caret.line.line_no+1);
caret.pos = 0;
caret.tag = caret.line.tags;
} else {
caret.pos--;
}
} else {
// Single line; we stay where we are
caret.pos--;
}
} else {
if ((caret.tag.Start - 1 + caret.tag.Length) < caret.pos) {
caret.tag = caret.tag.Next;
}
}
UpdateCaret();
return;
}
case CaretDirection.CharBackNoWrap:
nowrap = true;
goto case CaretDirection.CharBack;
case CaretDirection.CharBack: {
if (caret.pos > 0) {
// caret.pos--; // folded into the if below
if (--caret.pos > 0) {
if (caret.tag.Start > caret.pos) {
caret.tag = caret.tag.Previous;
}
}
} else {
if (caret.line.line_no > 1 && !nowrap) {
caret.line = GetLine(caret.line.line_no - 1);
caret.pos = caret.line.TextLengthWithoutEnding ();
caret.tag = LineTag.FindTag(caret.line, caret.pos);
}
}
UpdateCaret();
return;
}
case CaretDirection.WordForward: {
int len;
len = caret.line.text.Length;
if (caret.pos < len) {
while ((caret.pos < len) && (caret.line.text[caret.pos] != ' ')) {
caret.pos++;
}
if (caret.pos < len) {
// Skip any whitespace
while ((caret.pos < len) && (caret.line.text[caret.pos] == ' ')) {
caret.pos++;
}
}
caret.tag = LineTag.FindTag(caret.line, caret.pos);
} else {
if (caret.line.line_no < this.lines) {
caret.line = GetLine(caret.line.line_no + 1);
caret.pos = 0;
caret.tag = caret.line.tags;
}
}
UpdateCaret();
return;
}
case CaretDirection.WordBack: {
if (caret.pos > 0) {
caret.pos--;
while ((caret.pos > 0) && (caret.line.text[caret.pos] == ' ')) {
caret.pos--;
}
while ((caret.pos > 0) && (caret.line.text[caret.pos] != ' ')) {
caret.pos--;
}
if (caret.line.text.ToString(caret.pos, 1) == " ") {
if (caret.pos != 0) {
caret.pos++;
} else {
caret.line = GetLine(caret.line.line_no - 1);
caret.pos = caret.line.text.Length;
}
}
caret.tag = LineTag.FindTag(caret.line, caret.pos);
} else {
if (caret.line.line_no > 1) {
caret.line = GetLine(caret.line.line_no - 1);
caret.pos = caret.line.text.Length;
caret.tag = LineTag.FindTag(caret.line, caret.pos);
}
}
UpdateCaret();
return;
}
case CaretDirection.LineUp: {
if (caret.line.line_no > 1) {
int pixel;
pixel = (int)caret.line.widths[caret.pos];
PositionCaret(pixel, GetLine(caret.line.line_no - 1).Y);
DisplayCaret ();
}
return;
}
case CaretDirection.LineDown: {
if (caret.line.line_no < lines) {
int pixel;
pixel = (int)caret.line.widths[caret.pos];
PositionCaret(pixel, GetLine(caret.line.line_no + 1).Y);
DisplayCaret ();
}
return;
}
case CaretDirection.Home: {
if (caret.pos > 0) {
caret.pos = 0;
caret.tag = caret.line.tags;
UpdateCaret();
}
return;
}
case CaretDirection.End: {
if (caret.pos < caret.line.TextLengthWithoutEnding ()) {
caret.pos = caret.line.TextLengthWithoutEnding ();
caret.tag = LineTag.FindTag(caret.line, caret.pos);
UpdateCaret();
}
return;
}
case CaretDirection.PgUp: {
if (caret.line.line_no == 1 && owner.richtext) {
owner.vscroll.Value = 0;
Line line = GetLine (1);
PositionCaret (line, 0);
}
int y_offset = caret.line.Y + caret.line.height - 1 - viewport_y;
int index;
LineTag top = FindCursor ((int) caret.line.widths [caret.pos],
viewport_y - viewport_height, out index);
owner.vscroll.Value = Math.Min (top.Line.Y, owner.vscroll.Maximum - viewport_height);
PositionCaret ((int) caret.line.widths [caret.pos], y_offset + viewport_y);
return;
}
case CaretDirection.PgDn: {
if (caret.line.line_no == lines && owner.richtext) {
owner.vscroll.Value = owner.vscroll.Maximum - viewport_height + 1;
Line line = GetLine (lines);
PositionCaret (line, line.TextLengthWithoutEnding());
}
int y_offset = caret.line.Y - viewport_y;
int index;
LineTag top = FindCursor ((int) caret.line.widths [caret.pos],
viewport_y + viewport_height, out index);
owner.vscroll.Value = Math.Min (top.Line.Y, owner.vscroll.Maximum - viewport_height);
PositionCaret ((int) caret.line.widths [caret.pos], y_offset + viewport_y);
return;
}
case CaretDirection.CtrlPgUp: {
PositionCaret(0, viewport_y);
DisplayCaret ();
return;
}
case CaretDirection.CtrlPgDn: {
Line line;
LineTag tag;
int index;
tag = FindCursor (0, viewport_y + viewport_height, out index);
if (tag.Line.line_no > 1) {
line = GetLine(tag.Line.line_no - 1);
} else {
line = tag.Line;
}
PositionCaret(line, line.Text.Length);
DisplayCaret ();
return;
}
case CaretDirection.CtrlHome: {
caret.line = GetLine(1);
caret.pos = 0;
caret.tag = caret.line.tags;
UpdateCaret();
return;
}
case CaretDirection.CtrlEnd: {
caret.line = GetLine(lines);
caret.pos = caret.line.TextLengthWithoutEnding ();
caret.tag = LineTag.FindTag(caret.line, caret.pos);
UpdateCaret();
return;
}
case CaretDirection.SelectionStart: {
caret.line = selection_start.line;
caret.pos = selection_start.pos;
caret.tag = selection_start.tag;
UpdateCaret();
return;
}
case CaretDirection.SelectionEnd: {
caret.line = selection_end.line;
caret.pos = selection_end.pos;
caret.tag = selection_end.tag;
UpdateCaret();
return;
}
}
}
internal void DumpDoc ()
{
Console.WriteLine ("<doc lines='{0}'>", lines);
for (int i = 1; i <= lines ; i++) {
Line line = GetLine (i);
Console.WriteLine ("<line no='{0}' ending='{1}'>", line.line_no, line.ending);
LineTag tag = line.tags;
while (tag != null) {
Console.Write ("\t<tag type='{0}' span='{1}->{2}' font='{3}' color='{4}'>",
tag.GetType (), tag.Start, tag.Length, tag.Font, tag.Color);
Console.Write (tag.Text ());
Console.WriteLine ("</tag>");
tag = tag.Next;
}
Console.WriteLine ("</line>");
}
Console.WriteLine ("</doc>");
}
// UIA: Used via reflection by TextProviderBehavior
internal void GetVisibleLineIndexes (Rectangle clip, out int start, out int end)
{
if (multiline) {
/* Expand the region slightly to be sure to
* paint the full extent of the line of text.
* See bug 464464.
*/
start = GetLineByPixel(clip.Top + viewport_y - offset_y - 1, false).line_no;
end = GetLineByPixel(clip.Bottom + viewport_y - offset_y + 1, false).line_no;
} else {
start = GetLineByPixel(clip.Left + viewport_x - offset_x, false).line_no;
end = GetLineByPixel(clip.Right + viewport_x - offset_x, false).line_no;
}
}
internal void Draw (Graphics g, Rectangle clip)
{
Line line; // Current line being drawn
LineTag tag; // Current tag being drawn
int start; // First line to draw
int end; // Last line to draw
StringBuilder text; // String representing the current line
int line_no;
Color tag_color;
Color current_color;
// First, figure out from what line to what line we need to draw
GetVisibleLineIndexes (clip, out start, out end);
// remove links in the list (used for mouse down events) that are within the clip area.
InvalidateLinks (clip);
///
/// We draw the single border ourself
///
if (owner.actual_border_style == BorderStyle.FixedSingle) {
ControlPaint.DrawBorder (g, owner.ClientRectangle, Color.Black, ButtonBorderStyle.Solid);
}
/// Make sure that we aren't drawing one more line then we need to
line = GetLine (end - 1);
if (line != null && clip.Bottom == offset_y + line.Y + line.height - viewport_y)
end--;
line_no = start;
#if Debug
DateTime n = DateTime.Now;
Console.WriteLine ("Started drawing: {0}s {1}ms", n.Second, n.Millisecond);
Console.WriteLine ("CLIP: {0}", clip);
Console.WriteLine ("S: {0}", GetLine (start).text);
Console.WriteLine ("E: {0}", GetLine (end).text);
#endif
// Non multiline selection can be handled outside of the loop
if (!multiline && selection_visible && owner.ShowSelection) {
g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
offset_x + selection_start.line.widths [selection_start.pos] +
selection_start.line.X - viewport_x,
offset_y + selection_start.line.Y,
(selection_end.line.X + selection_end.line.widths [selection_end.pos]) -
(selection_start.line.X + selection_start.line.widths [selection_start.pos]),
selection_start.line.height);
}
while (line_no <= end) {
line = GetLine (line_no);
float line_y = line.Y - viewport_y + offset_y;
tag = line.tags;
if (!calc_pass) {
text = line.text;
} else {
if (PasswordCache.Length < line.text.Length)
PasswordCache.Append(Char.Parse(password_char), line.text.Length - PasswordCache.Length);
else if (PasswordCache.Length > line.text.Length)
PasswordCache.Remove(line.text.Length, PasswordCache.Length - line.text.Length);
text = PasswordCache;
}
int line_selection_start = text.Length + 1;
int line_selection_end = text.Length + 1;
if (selection_visible && owner.ShowSelection &&
(line_no >= selection_start.line.line_no) &&
(line_no <= selection_end.line.line_no)) {
if (line_no == selection_start.line.line_no)
line_selection_start = selection_start.pos + 1;
else
line_selection_start = 1;
if (line_no == selection_end.line.line_no)
line_selection_end = selection_end.pos + 1;
else
line_selection_end = text.Length + 1;
if (line_selection_end == line_selection_start) {
// There isn't really selection
line_selection_start = text.Length + 1;
line_selection_end = line_selection_start;
} else if (multiline) {
// lets draw some selection baby!! (non multiline selection is drawn outside the loop)
g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
offset_x + line.widths [line_selection_start - 1] + line.X - viewport_x,
line_y, line.widths [line_selection_end - 1] - line.widths [line_selection_start - 1],
line.height);
}
}
current_color = line.tags.ColorToDisplay;
while (tag != null) {
// Skip empty tags
if (tag.Length == 0) {
tag = tag.Next;
continue;
}
if (((tag.X + tag.Width) < (clip.Left - viewport_x - offset_x)) &&
(tag.X > (clip.Right - viewport_x - offset_x))) {
tag = tag.Next;
continue;
}
if (tag.BackColor != Color.Empty) {
g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (tag.BackColor),
offset_x + tag.X + line.X - viewport_x,
line_y + tag.Shift, tag.Width, line.height);
}
tag_color = tag.ColorToDisplay;
current_color = tag_color;
if (!owner.Enabled) {
Color a = tag.Color;
Color b = ThemeEngine.Current.ColorWindowText;
if ((a.R == b.R) && (a.G == b.G) && (a.B == b.B))
tag_color = ThemeEngine.Current.ColorGrayText;
}
int tag_pos = tag.Start;
current_color = tag_color;
while (tag_pos < tag.Start + tag.Length) {
int old_tag_pos = tag_pos;
if (tag_pos >= line_selection_start && tag_pos < line_selection_end) {
current_color = ThemeEngine.Current.ColorHighlightText;
tag_pos = Math.Min (tag.End, line_selection_end);
} else if (tag_pos < line_selection_start) {
current_color = tag_color;
tag_pos = Math.Min (tag.End, line_selection_start);
} else {
current_color = tag_color;
tag_pos = tag.End;
}
Rectangle text_size;
tag.Draw (g, current_color,
offset_x + line.X - viewport_x,
line_y + tag.Shift,
old_tag_pos - 1, Math.Min (tag.Start + tag.Length, tag_pos) - 1,
text.ToString (), out text_size, tag.IsLink);
if (tag.IsLink) {
TextBoxBase.LinkRectangle link = new TextBoxBase.LinkRectangle (text_size);
link.LinkTag = tag;
owner.list_links.Add (link);
}
}
tag = tag.Next;
}
line.DrawEnding (g, line_y);
line_no++;
}
}
private int GetLineEnding (string line, int start, out LineEnding ending)
{
int res;
int rich_index;
if (start >= line.Length) {
ending = LineEnding.Wrap;
return -1;
}
res = line.IndexOf ('\r', start);
rich_index = line.IndexOf ('\n', start);
// Handle the case where we find both of them, and the \n is before the \r
if (res != -1 && rich_index != -1)
if (rich_index < res) {
ending = LineEnding.Rich;
return rich_index;
}
if (res != -1) {
if (res + 2 < line.Length && line [res + 1] == '\r' && line [res + 2] == '\n') {
ending = LineEnding.Soft;
return res;
}
if (res + 1 < line.Length && line [res + 1] == '\n') {
ending = LineEnding.Hard;
return res;
}
ending = LineEnding.Limp;
return res;
}
if (rich_index != -1) {
ending = LineEnding.Rich;
return rich_index;
}
ending = LineEnding.Wrap;
return line.Length;
}
// Get the line ending, but only of the types specified
private int GetLineEnding (string line, int start, out LineEnding ending, LineEnding type)
{
int index = start;
int last_length = 0;
do {
index = GetLineEnding (line, index + last_length, out ending);
last_length = LineEndingLength (ending);
} while
((ending & type) != ending && index != -1);
return index == -1 ? line.Length : index;
}
internal int LineEndingLength (LineEnding ending)
{
switch (ending) {
case LineEnding.Limp:
case LineEnding.Rich:
return 1;
case LineEnding.Hard:
return 2;
case LineEnding.Soft:
return 3;
}
return 0;
}
internal string LineEndingToString (LineEnding ending)
{
switch (ending) {
case LineEnding.Limp:
return "\r";
case LineEnding.Hard:
return "\r\n";
case LineEnding.Soft:
return "\r\r\n";
case LineEnding.Rich:
return "\n";
}
return string.Empty;
}
internal LineEnding StringToLineEnding (string ending)
{
switch (ending) {
case "\r":
return LineEnding.Limp;
case "\r\n":
return LineEnding.Hard;
case "\r\r\n":
return LineEnding.Soft;
case "\n":
return LineEnding.Rich;
default:
return LineEnding.None;
}
}
internal void Insert (Line line, int pos, bool update_caret, string s)
{
Insert (line, pos, update_caret, s, line.FindTag (pos));
}
// Insert text at the given position; use formatting at insertion point for inserted text
internal void Insert (Line line, int pos, bool update_caret, string s, LineTag tag)
{
int break_index;
int base_line;
int old_line_count;
int count = 1;
LineEnding ending;
Line split_line;
// Don't recalculate while we mess around
SuspendRecalc ();
base_line = line.line_no;
old_line_count = lines;
// Discard chars after any possible -unlikely- end of file
int eof_index = s.IndexOf ('\0');
if (eof_index != -1)
s = s.Substring (0, eof_index);
break_index = GetLineEnding (s, 0, out ending, LineEnding.Hard | LineEnding.Rich);
// There are no line feeds in our text to be pasted
if (break_index == s.Length) {
line.InsertString (pos, s, tag);
} else {
// Add up to the first line feed to our current position
line.InsertString (pos, s.Substring (0, break_index + LineEndingLength (ending)), tag);
// Split the rest of the original line to a new line
Split (line, pos + (break_index + LineEndingLength (ending)));
line.ending = ending;
break_index += LineEndingLength (ending);
split_line = GetLine (line.line_no + 1);
// Insert brand new lines for any more line feeds in the inserted string
while (true) {
int next_break = GetLineEnding (s, break_index, out ending, LineEnding.Hard | LineEnding.Rich);
if (next_break == s.Length)
break;
string line_text = s.Substring (break_index, next_break - break_index +
LineEndingLength (ending));
Add (base_line + count, line_text, line.alignment, tag.Font, tag.Color, ending);
Line last = GetLine (base_line + count);
last.ending = ending;
count++;
break_index = next_break + LineEndingLength (ending);
}
// Add the remainder of the insert text to the split
// part of the original line
split_line.InsertString (0, s.Substring (break_index));
}
// Allow the document to recalculate things
ResumeRecalc (false);
// Update our character count
CharCount += s.Length;
UpdateView (line, lines - old_line_count + 1, pos);
// Move the caret to the end of the inserted text if requested
if (update_caret) {
Line l = GetLine (line.line_no + lines - old_line_count);
PositionCaret (l, l.text.Length);
DisplayCaret ();
}
}
// Inserts a string at the given position
internal void InsertString (Line line, int pos, string s)
{
// Update our character count
CharCount += s.Length;
// Insert the text into the Line
line.InsertString (pos, s);
}
// Inserts a character at the current caret position
internal void InsertCharAtCaret (char ch, bool move_caret)
{
caret.line.InsertString (caret.pos, ch.ToString(), caret.tag);
// Update our character count
CharCount++;
undo.RecordTyping (caret.line, caret.pos, ch);
UpdateView (caret.line, caret.pos);
if (move_caret) {
caret.pos++;
UpdateCaret ();
SetSelectionToCaret (true);
}
}
internal void InsertPicture (Line line, int pos, RTF.Picture picture)
{
//LineTag next_tag;
LineTag tag;
int len;
len = 1;
// Just a place holder basically
line.text.Insert (pos, "I");
PictureTag picture_tag = new PictureTag (line, pos + 1, picture);
tag = LineTag.FindTag (line, pos);
picture_tag.CopyFormattingFrom (tag);
/*next_tag = */tag.Break (pos + 1);
picture_tag.Previous = tag;
picture_tag.Next = tag.Next;
tag.Next = picture_tag;
//
// Picture tags need to be surrounded by text tags
//
if (picture_tag.Next == null) {
picture_tag.Next = new LineTag (line, pos + 1);
picture_tag.Next.CopyFormattingFrom (tag);
picture_tag.Next.Previous = picture_tag;
}
tag = picture_tag.Next;
while (tag != null) {
tag.Start += len;
tag = tag.Next;
}
line.Grow (len);
line.recalc = true;
UpdateView (line, pos);
}
internal void DeleteMultiline (Line start_line, int pos, int length)
{
Marker start = new Marker ();
Marker end = new Marker ();
int start_index = LineTagToCharIndex (start_line, pos);
start.line = start_line;
start.pos = pos;
start.tag = LineTag.FindTag (start_line, pos);
CharIndexToLineTag (start_index + length, out end.line,
out end.tag, out end.pos);
SuspendUpdate ();
if (start.line == end.line) {
DeleteChars (start.line, pos, end.pos - pos);
} else {
// Delete first and last lines
DeleteChars (start.line, start.pos, start.line.text.Length - start.pos);
DeleteChars (end.line, 0, end.pos);
int current = start.line.line_no + 1;
if (current < end.line.line_no) {
for (int i = end.line.line_no - 1; i >= current; i--) {
Delete (i);
}
}
// BIG FAT WARNING - selection_end.line might be stale due
// to the above Delete() call. DONT USE IT before hitting the end of this method!
// Join start and end
Combine (start.line.line_no, current);
}
ResumeUpdate (true);
}
// Deletes n characters at the given position; it will not delete past line limits
// pos is 0-based
public void DeleteChars (Line line, int pos, int count)
{
// Reduce our character count
CharCount -= count;
line.DeleteCharacters (pos, count);
if (pos >= line.TextLengthWithoutEnding ()) {
LineEnding ending = line.ending;
GetLineEnding (line.text.ToString (), 0, out ending);
if (ending != line.ending) {
line.ending = ending;
if (!multiline) {
UpdateView (line, lines, pos);
owner.Invalidate ();
return;
}
}
}
if (!multiline) {
UpdateView (line, lines, pos);
owner.Invalidate ();
} else
UpdateView (line, pos);
}
// Deletes a character at or after the given position (depending on forward); it will not delete past line limits
public void DeleteChar (Line line, int pos, bool forward)
{
if ((pos == 0 && forward == false) || (pos == line.text.Length && forward == true))
return;
undo.BeginUserAction ("Delete");
if (forward) {
undo.RecordDeleteString (line, pos, line, pos + 1);
DeleteChars (line, pos, 1);
} else {
undo.RecordDeleteString (line, pos - 1, line, pos);
DeleteChars (line, pos - 1, 1);
}
undo.EndUserAction ();
}
// Combine two lines
internal void Combine(int FirstLine, int SecondLine) {
Combine(GetLine(FirstLine), GetLine(SecondLine));
}
internal void Combine(Line first, Line second) {
LineTag last;
int shift;
// strip the ending off of the first lines text
first.text.Length = first.text.Length - LineEndingLength (first.ending);
// Combine the two tag chains into one
last = first.tags;
// Maintain the line ending style
first.ending = second.ending;
while (last.Next != null) {
last = last.Next;
}
// need to get the shift before setting the next tag since that effects length
shift = last.Start + last.Length - 1;
last.Next = second.tags;
last.Next.Previous = last;
// Fix up references within the chain
last = last.Next;
while (last != null) {
last.Line = first;
last.Start += shift;
last = last.Next;
}
// Combine both lines' strings
first.text.Insert(first.text.Length, second.text.ToString());
first.Grow(first.text.Length);
// Remove the reference to our (now combined) tags from the doomed line
second.tags = null;
// Renumber lines
DecrementLines(first.line_no + 2); // first.line_no + 1 will be deleted, so we need to start renumbering one later
// Mop up
first.recalc = true;
first.height = 0; // This forces RecalcDocument/UpdateView to redraw from this line on
first.Streamline(lines);
// Update Caret, Selection, etc
if (caret.line == second) {
caret.Combine(first, shift);
}
if (selection_anchor.line == second) {
selection_anchor.Combine(first, shift);
}
if (selection_start.line == second) {
selection_start.Combine(first, shift);
}
if (selection_end.line == second) {
selection_end.Combine(first, shift);
}
#if Debug
Line check_first;
Line check_second;
check_first = GetLine(first.line_no);
check_second = GetLine(check_first.line_no + 1);
Console.WriteLine("Pre-delete: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
#endif
this.Delete(second);
#if Debug
check_first = GetLine(first.line_no);
check_second = GetLine(check_first.line_no + 1);
Console.WriteLine("Post-delete Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
#endif
}
// Split the line at the position into two
internal void Split(int LineNo, int pos) {
Line line;
LineTag tag;
line = GetLine(LineNo);
tag = LineTag.FindTag(line, pos);
Split(line, tag, pos);
}
internal void Split(Line line, int pos) {
LineTag tag;
tag = LineTag.FindTag(line, pos);
Split(line, tag, pos);
}
///<summary>Split line at given tag and position into two lines</summary>
///if more space becomes available on previous line
internal void Split(Line line, LineTag tag, int pos) {
LineTag new_tag;
Line new_line;
bool move_caret;
bool move_sel_start;
bool move_sel_end;
move_caret = false;
move_sel_start = false;
move_sel_end = false;
#if DEBUG
SanityCheck();
if (tag.End < pos)
throw new Exception ("Split called with the wrong tag");
#endif
// Adjust selection and cursors
if (caret.line == line && caret.pos >= pos) {
move_caret = true;
}
if (selection_start.line == line && selection_start.pos > pos) {
move_sel_start = true;
}
if (selection_end.line == line && selection_end.pos > pos) {
move_sel_end = true;
}
// cover the easy case first
if (pos == line.text.Length) {
Add (line.line_no + 1, String.Empty, line.alignment, tag.Font, tag.Color, line.ending);
new_line = GetLine (line.line_no + 1);
if (move_caret) {
caret.line = new_line;
caret.tag = new_line.tags;
caret.pos = 0;
if (selection_visible == false) {
SetSelectionToCaret (true);
}
}
if (move_sel_start) {
selection_start.line = new_line;
selection_start.pos = 0;
selection_start.tag = new_line.tags;
}
if (move_sel_end) {
selection_end.line = new_line;
selection_end.pos = 0;
selection_end.tag = new_line.tags;
}
#if DEBUG
SanityCheck ();
#endif
return;
}
// We need to move the rest of the text into the new line
Add (line.line_no + 1, line.text.ToString (pos, line.text.Length - pos), line.alignment, tag.Font, tag.Color, line.ending);
// Now transfer our tags from this line to the next
new_line = GetLine(line.line_no + 1);
line.recalc = true;
new_line.recalc = true;
//make sure that if we are at the end of a tag, we start on the begining
//of a new one, if one exists... Stops us creating an empty tag and
//make the operation easier.
if (tag.Next != null && (tag.Next.Start - 1) == pos)
tag = tag.Next;
if ((tag.Start - 1) == pos) {
int shift;
// We can simply break the chain and move the tag into the next line
// if the tag we are moving is the first, create an empty tag
// for the line we are leaving behind
if (tag == line.tags) {
new_tag = new LineTag(line, 1);
new_tag.CopyFormattingFrom (tag);
line.tags = new_tag;
}
if (tag.Previous != null) {
tag.Previous.Next = null;
}
new_line.tags = tag;
tag.Previous = null;
tag.Line = new_line;
// Walk the list and correct the start location of the tags we just bumped into the next line
shift = tag.Start - 1;
new_tag = tag;
while (new_tag != null) {
new_tag.Start -= shift;
new_tag.Line = new_line;
new_tag = new_tag.Next;
}
} else {
int shift;
new_tag = new LineTag (new_line, 1);
new_tag.Next = tag.Next;
new_tag.CopyFormattingFrom (tag);
new_line.tags = new_tag;
if (new_tag.Next != null) {
new_tag.Next.Previous = new_tag;
}
tag.Next = null;
shift = pos;
new_tag = new_tag.Next;
while (new_tag != null) {
new_tag.Start -= shift;
new_tag.Line = new_line;
new_tag = new_tag.Next;
}
}
if (move_caret) {
caret.line = new_line;
caret.pos = caret.pos - pos;
caret.tag = caret.line.FindTag(caret.pos);
if (selection_visible == false) {
SetSelectionToCaret (true);
move_sel_start = false;
move_sel_end = false;
}
}
if (move_sel_start) {
selection_start.line = new_line;
selection_start.pos = selection_start.pos - pos;
if (selection_start.Equals(selection_end))
selection_start.tag = new_line.FindTag(selection_start.pos);
else
selection_start.tag = new_line.FindTag (selection_start.pos + 1);
}
if (move_sel_end) {
selection_end.line = new_line;
selection_end.pos = selection_end.pos - pos;
selection_end.tag = new_line.FindTag(selection_end.pos);
}
CharCount -= line.text.Length - pos;
line.text.Remove(pos, line.text.Length - pos);
#if DEBUG
SanityCheck ();
#endif
}
#if DEBUG
private void SanityCheck () {
for (int i = 1; i < lines; i++) {
LineTag tag = GetLine (i).tags;
if (tag.Start != 1)
throw new Exception ("Line doesn't start at the begining");
int start = 1;
tag = tag.Next;
while (tag != null) {
if (tag.Start == start)
throw new Exception ("Empty tag!");
if (tag.Start < start)
throw new Exception ("Insane!!");
start = tag.Start;
tag = tag.Next;
}
}
}
#endif
// Adds a line of text, with given font.
// Bumps any line at that line number that already exists down
internal void Add (int LineNo, string Text, Font font, Color color, LineEnding ending)
{
Add (LineNo, Text, alignment, font, color, ending);
}
internal void Add (int LineNo, string Text, HorizontalAlignment align, Font font, Color color, LineEnding ending)
{
Line add;
Line line;
int line_no;
CharCount += Text.Length;
if (LineNo<1 || Text == null) {
if (LineNo<1) {
throw new ArgumentNullException("LineNo", "Line numbers must be positive");
} else {
throw new ArgumentNullException("Text", "Cannot insert NULL line");
}
}
add = new Line (this, LineNo, Text, align, font, color, ending);
line = document;
while (line != sentinel) {
add.parent = line;
line_no = line.line_no;
if (LineNo > line_no) {
line = line.right;
} else if (LineNo < line_no) {
line = line.left;
} else {
// Bump existing line numbers; walk all nodes to the right of this one and increment line_no
IncrementLines(line.line_no);
line = line.left;
}
}
add.left = sentinel;
add.right = sentinel;
if (add.parent != null) {
if (LineNo > add.parent.line_no) {
add.parent.right = add;
} else {
add.parent.left = add;
}
} else {
// Root node
document = add;
}
RebalanceAfterAdd(add);
lines++;
}
internal virtual void Clear() {
lines = 0;
CharCount = 0;
document = sentinel;
}
public virtual object Clone() {
Document clone;
clone = new Document(null);
clone.lines = this.lines;
clone.document = (Line)document.Clone();
return clone;
}
private void Delete (int LineNo)
{
Line line;
if (LineNo > lines)
return;
line = GetLine (LineNo);
CharCount -= line.text.Length;
DecrementLines (LineNo + 1);
Delete (line);
}
private void Delete(Line line1) {
Line line2;// = new Line();
Line line3;
if ((line1.left == sentinel) || (line1.right == sentinel)) {
line3 = line1;
} else {
line3 = line1.right;
while (line3.left != sentinel) {
line3 = line3.left;
}
}
if (line3.left != sentinel) {
line2 = line3.left;
} else {
line2 = line3.right;
}
line2.parent = line3.parent;
if (line3.parent != null) {
if(line3 == line3.parent.left) {
line3.parent.left = line2;
} else {
line3.parent.right = line2;
}
} else {
document = line2;
}
if (line3 != line1) {
LineTag tag;
if (selection_start.line == line3) {
selection_start.line = line1;
}
if (selection_end.line == line3) {
selection_end.line = line1;
}
if (selection_anchor.line == line3) {
selection_anchor.line = line1;
}
if (caret.line == line3) {
caret.line = line1;
}
line1.alignment = line3.alignment;
line1.ascent = line3.ascent;
line1.hanging_indent = line3.hanging_indent;
line1.height = line3.height;
line1.indent = line3.indent;
line1.line_no = line3.line_no;
line1.recalc = line3.recalc;
line1.right_indent = line3.right_indent;
line1.ending = line3.ending;
line1.space = line3.space;
line1.tags = line3.tags;
line1.text = line3.text;
line1.widths = line3.widths;
line1.offset = line3.offset;
tag = line1.tags;
while (tag != null) {
tag.Line = line1;
tag = tag.Next;
}
}
if (line3.color == LineColor.Black)
RebalanceAfterDelete(line2);
this.lines--;
}
// Invalidates the start line until the end of the viewstate
internal void InvalidateLinesAfter (Line start) {
owner.Invalidate (new Rectangle (0, start.Y - viewport_y, viewport_width, viewport_height - start.Y));
}
// Invalidate a section of the document to trigger redraw
internal void Invalidate(Line start, int start_pos, Line end, int end_pos) {
Line l1;
Line l2;
int p1;
int p2;
if ((start == end) && (start_pos == end_pos)) {
return;
}
if (end_pos == -1) {
end_pos = end.text.Length;
}
// figure out what's before what so the logic below is straightforward
if (start.line_no < end.line_no) {
l1 = start;
p1 = start_pos;
l2 = end;
p2 = end_pos;
} else if (start.line_no > end.line_no) {
l1 = end;
p1 = end_pos;
l2 = start;
p2 = start_pos;
} else {
if (start_pos < end_pos) {
l1 = start;
p1 = start_pos;
l2 = end;
p2 = end_pos;
} else {
l1 = end;
p1 = end_pos;
l2 = start;
p2 = start_pos;
}
int endpoint = (int) l1.widths [p2];
if (p2 == l1.text.Length + 1) {
endpoint = (int) viewport_width;
}
#if Debug
Console.WriteLine("Invaliding backwards from {0}:{1} to {2}:{3} {4}",
l1.line_no, p1, l2.line_no, p2,
new Rectangle(
(int)l1.widths[p1] + l1.X - viewport_x,
l1.Y - viewport_y,
(int)l1.widths[p2],
l1.height
)
);
#endif
owner.Invalidate(new Rectangle (
offset_x + (int)l1.widths[p1] + l1.X - viewport_x,
offset_y + l1.Y - viewport_y,
endpoint - (int) l1.widths [p1] + 1,
l1.height));
return;
}
#if Debug
Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} Start => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, (int)l1.widths[p1] + l1.X - viewport_x, l1.Y - viewport_y, viewport_width, l1.height);
Console.WriteLine ("invalidate start line: {0} position: {1}", l1.text, p1);
#endif
// Three invalidates:
// First line from start
owner.Invalidate(new Rectangle(
offset_x + (int)l1.widths[p1] + l1.X - viewport_x,
offset_y + l1.Y - viewport_y,
viewport_width,
l1.height));
// lines inbetween
if ((l1.line_no + 1) < l2.line_no) {
int y;
y = GetLine(l1.line_no + 1).Y;
owner.Invalidate(new Rectangle(
offset_x,
offset_y + y - viewport_y,
viewport_width,
l2.Y - y));
#if Debug
Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} Middle => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, 0, y - viewport_y, viewport_width, l2.Y - y);
#endif
}
// Last line to end
owner.Invalidate(new Rectangle(
offset_x + (int)l2.widths[0] + l2.X - viewport_x,
offset_y + l2.Y - viewport_y,
(int)l2.widths[p2] + 1,
l2.height));
#if Debug
Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} End => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, (int)l2.widths[0] + l2.X - viewport_x, l2.Y - viewport_y, (int)l2.widths[p2] + 1, l2.height);
#endif
}
/// <summary>Select text around caret</summary>
internal void ExpandSelection(CaretSelection mode, bool to_caret) {
if (to_caret) {
// We're expanding the selection to the caret position
switch(mode) {
case CaretSelection.Line: {
// Invalidate the selection delta
if (caret > selection_prev) {
Invalidate(selection_prev.line, 0, caret.line, caret.line.text.Length);
} else {
Invalidate(selection_prev.line, selection_prev.line.text.Length, caret.line, 0);
}
if (caret.line.line_no <= selection_anchor.line.line_no) {
selection_start.line = caret.line;
selection_start.tag = caret.line.tags;
selection_start.pos = 0;
selection_end.line = selection_anchor.line;
selection_end.tag = selection_anchor.tag;
selection_end.pos = selection_anchor.pos;
selection_end_anchor = true;
} else {
selection_start.line = selection_anchor.line;
selection_start.pos = selection_anchor.height;
selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height + 1);
selection_end.line = caret.line;
selection_end.tag = caret.line.tags;
selection_end.pos = caret.line.text.Length;
selection_end_anchor = false;
}
selection_prev.line = caret.line;
selection_prev.tag = caret.tag;
selection_prev.pos = caret.pos;
break;
}
case CaretSelection.Word: {
int start_pos;
int end_pos;
start_pos = FindWordSeparator(caret.line, caret.pos, false);
end_pos = FindWordSeparator(caret.line, caret.pos, true);
// Invalidate the selection delta
if (caret > selection_prev) {
Invalidate(selection_prev.line, selection_prev.pos, caret.line, end_pos);
} else {
Invalidate(selection_prev.line, selection_prev.pos, caret.line, start_pos);
}
if (caret < selection_anchor) {
selection_start.line = caret.line;
selection_start.tag = caret.line.FindTag(start_pos + 1);
selection_start.pos = start_pos;
selection_end.line = selection_anchor.line;
selection_end.tag = selection_anchor.tag;
selection_end.pos = selection_anchor.pos;
selection_prev.line = caret.line;
selection_prev.tag = caret.tag;
selection_prev.pos = start_pos;
selection_end_anchor = true;
} else {
selection_start.line = selection_anchor.line;
selection_start.pos = selection_anchor.height;
selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height + 1);
selection_end.line = caret.line;
selection_end.tag = caret.line.FindTag(end_pos);
selection_end.pos = end_pos;
selection_prev.line = caret.line;
selection_prev.tag = caret.tag;
selection_prev.pos = end_pos;
selection_end_anchor = false;
}
break;
}
case CaretSelection.Position: {
SetSelectionToCaret(false);
return;
}
}
} else {
// We're setting the selection 'around' the caret position
switch(mode) {
case CaretSelection.Line: {
this.Invalidate(caret.line, 0, caret.line, caret.line.text.Length);
selection_start.line = caret.line;
selection_start.tag = caret.line.tags;
selection_start.pos = 0;
selection_end.line = caret.line;
selection_end.pos = caret.line.text.Length;
selection_end.tag = caret.line.FindTag(selection_end.pos);
selection_anchor.line = selection_end.line;
selection_anchor.tag = selection_end.tag;
selection_anchor.pos = selection_end.pos;
selection_anchor.height = 0;
selection_prev.line = caret.line;
selection_prev.tag = caret.tag;
selection_prev.pos = caret.pos;
this.selection_end_anchor = true;
break;
}
case CaretSelection.Word: {
int start_pos;
int end_pos;
start_pos = FindWordSeparator(caret.line, caret.pos, false);
end_pos = FindWordSeparator(caret.line, caret.pos, true);
this.Invalidate(selection_start.line, start_pos, caret.line, end_pos);
selection_start.line = caret.line;
selection_start.tag = caret.line.FindTag(start_pos + 1);
selection_start.pos = start_pos;
selection_end.line = caret.line;
selection_end.tag = caret.line.FindTag(end_pos);
selection_end.pos = end_pos;
selection_anchor.line = selection_end.line;
selection_anchor.tag = selection_end.tag;
selection_anchor.pos = selection_end.pos;
selection_anchor.height = start_pos;
selection_prev.line = caret.line;
selection_prev.tag = caret.tag;
selection_prev.pos = caret.pos;
this.selection_end_anchor = true;
break;
}
}
}
SetSelectionVisible (!(selection_start == selection_end));
}
internal void SetSelectionToCaret(bool start) {
if (start) {
// Invalidate old selection; selection is being reset to empty
this.Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
selection_start.line = caret.line;
selection_start.tag = caret.tag;
selection_start.pos = caret.pos;
// start always also selects end
selection_end.line = caret.line;
selection_end.tag = caret.tag;
selection_end.pos = caret.pos;
selection_anchor.line = caret.line;
selection_anchor.tag = caret.tag;
selection_anchor.pos = caret.pos;
} else {
// Invalidate from previous end to caret (aka new end)
if (selection_end_anchor) {
if (selection_start != caret) {
this.Invalidate(selection_start.line, selection_start.pos, caret.line, caret.pos);
}
} else {
if (selection_end != caret) {
this.Invalidate(selection_end.line, selection_end.pos, caret.line, caret.pos);
}
}
if (caret < selection_anchor) {
selection_start.line = caret.line;
selection_start.tag = caret.tag;
selection_start.pos = caret.pos;
selection_end.line = selection_anchor.line;
selection_end.tag = selection_anchor.tag;
selection_end.pos = selection_anchor.pos;
selection_end_anchor = true;
} else {
selection_start.line = selection_anchor.line;
selection_start.tag = selection_anchor.tag;
selection_start.pos = selection_anchor.pos;
selection_end.line = caret.line;
selection_end.tag = caret.tag;
selection_end.pos = caret.pos;
selection_end_anchor = false;
}
}
SetSelectionVisible (!(selection_start == selection_end));
}
internal void SetSelection(Line start, int start_pos, Line end, int end_pos) {
if (selection_visible) {
Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
}
if ((end.line_no < start.line_no) || ((end == start) && (end_pos <= start_pos))) {
selection_start.line = end;
selection_start.tag = LineTag.FindTag(end, end_pos);
selection_start.pos = end_pos;
selection_end.line = start;
selection_end.tag = LineTag.FindTag(start, start_pos);
selection_end.pos = start_pos;
selection_end_anchor = true;
} else {
selection_start.line = start;
selection_start.tag = LineTag.FindTag(start, start_pos);
selection_start.pos = start_pos;
selection_end.line = end;
selection_end.tag = LineTag.FindTag(end, end_pos);
selection_end.pos = end_pos;
selection_end_anchor = false;
}
selection_anchor.line = start;
selection_anchor.tag = selection_start.tag;
selection_anchor.pos = start_pos;
if (((start == end) && (start_pos == end_pos)) || start == null || end == null) {
SetSelectionVisible (false);
} else {
SetSelectionVisible (true);
Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
}
}
internal void SetSelectionStart(Line start, int start_pos, bool invalidate) {
// Invalidate from the previous to the new start pos
if (invalidate)
Invalidate(selection_start.line, selection_start.pos, start, start_pos);
selection_start.line = start;
selection_start.pos = start_pos;
selection_start.tag = LineTag.FindTag(start, start_pos);
selection_anchor.line = start;
selection_anchor.pos = start_pos;
selection_anchor.tag = selection_start.tag;
selection_end_anchor = false;
if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
SetSelectionVisible (true);
} else {
SetSelectionVisible (false);
}
if (invalidate)
Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
}
internal void SetSelectionStart(int character_index, bool invalidate) {
Line line;
LineTag tag;
int pos;
if (character_index < 0) {
return;
}
CharIndexToLineTag(character_index, out line, out tag, out pos);
SetSelectionStart(line, pos, invalidate);
}
internal void SetSelectionEnd(Line end, int end_pos, bool invalidate) {
if (end == selection_end.line && end_pos == selection_start.pos) {
selection_anchor.line = selection_start.line;
selection_anchor.tag = selection_start.tag;
selection_anchor.pos = selection_start.pos;
selection_end.line = selection_start.line;
selection_end.tag = selection_start.tag;
selection_end.pos = selection_start.pos;
selection_end_anchor = false;
} else if ((end.line_no < selection_anchor.line.line_no) || ((end == selection_anchor.line) && (end_pos <= selection_anchor.pos))) {
selection_start.line = end;
selection_start.tag = LineTag.FindTag(end, end_pos);
selection_start.pos = end_pos;
selection_end.line = selection_anchor.line;
selection_end.tag = selection_anchor.tag;
selection_end.pos = selection_anchor.pos;
selection_end_anchor = true;
} else {
selection_start.line = selection_anchor.line;
selection_start.tag = selection_anchor.tag;
selection_start.pos = selection_anchor.pos;
selection_end.line = end;
selection_end.tag = LineTag.FindTag(end, end_pos);
selection_end.pos = end_pos;
selection_end_anchor = false;
}
if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
SetSelectionVisible (true);
if (invalidate)
Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
} else {
SetSelectionVisible (false);
// ?? Do I need to invalidate here, tests seem to work without it, but I don't think they should :-s
}
}
internal void SetSelectionEnd(int character_index, bool invalidate) {
Line line;
LineTag tag;
int pos;
if (character_index < 0) {
return;
}
CharIndexToLineTag(character_index, out line, out tag, out pos);
SetSelectionEnd(line, pos, invalidate);
}
internal void SetSelection(Line start, int start_pos) {
if (selection_visible) {
Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
}
selection_start.line = start;
selection_start.pos = start_pos;
selection_start.tag = LineTag.FindTag(start, start_pos);
selection_end.line = start;
selection_end.tag = selection_start.tag;
selection_end.pos = start_pos;
selection_anchor.line = start;
selection_anchor.tag = selection_start.tag;
selection_anchor.pos = start_pos;
selection_end_anchor = false;
SetSelectionVisible (false);
}
internal void InvalidateSelectionArea() {
Invalidate (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
}
// Return the current selection, as string
internal string GetSelection() {
// We return String.Empty if there is no selection
if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
return string.Empty;
}
if (selection_start.line == selection_end.line) {
return selection_start.line.text.ToString (selection_start.pos, selection_end.pos - selection_start.pos);
} else {
StringBuilder sb;
int i;
int start;
int end;
sb = new StringBuilder();
start = selection_start.line.line_no;
end = selection_end.line.line_no;
sb.Append(selection_start.line.text.ToString(selection_start.pos, selection_start.line.text.Length - selection_start.pos));
if ((start + 1) < end) {
for (i = start + 1; i < end; i++) {
sb.Append(GetLine(i).text.ToString());
}
}
sb.Append(selection_end.line.text.ToString(0, selection_end.pos));
return sb.ToString();
}
}
internal void ReplaceSelection(string s, bool select_new) {
int i;
int selection_start_pos = LineTagToCharIndex (selection_start.line, selection_start.pos);
SuspendRecalc ();
// First, delete any selected text
if ((selection_start.pos != selection_end.pos) || (selection_start.line != selection_end.line)) {
if (selection_start.line == selection_end.line) {
undo.RecordDeleteString (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
DeleteChars (selection_start.line, selection_start.pos, selection_end.pos - selection_start.pos);
// The tag might have been removed, we need to recalc it
selection_start.tag = selection_start.line.FindTag(selection_start.pos + 1);
} else {
int start;
int end;
start = selection_start.line.line_no;
end = selection_end.line.line_no;
undo.RecordDeleteString (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
InvalidateLinesAfter(selection_start.line);
// Delete first line
DeleteChars (selection_start.line, selection_start.pos, selection_start.line.text.Length - selection_start.pos);
selection_start.line.recalc = true;
// Delete last line
DeleteChars(selection_end.line, 0, selection_end.pos);
start++;
if (start < end) {
for (i = end - 1; i >= start; i--) {
Delete(i);
}
}
// BIG FAT WARNING - selection_end.line might be stale due
// to the above Delete() call. DONT USE IT before hitting the end of this method!
// Join start and end
Combine(selection_start.line.line_no, start);
}
}
Insert(selection_start.line, selection_start.pos, false, s);
undo.RecordInsertString (selection_start.line, selection_start.pos, s);
ResumeRecalc (false);
Line begin_update_line = selection_start.line;
int begin_update_pos = selection_start.pos;
if (!select_new) {
CharIndexToLineTag(selection_start_pos + s.Length, out selection_start.line,
out selection_start.tag, out selection_start.pos);
selection_end.line = selection_start.line;
selection_end.pos = selection_start.pos;
selection_end.tag = selection_start.tag;
selection_anchor.line = selection_start.line;
selection_anchor.pos = selection_start.pos;
selection_anchor.tag = selection_start.tag;
SetSelectionVisible (false);
} else {
CharIndexToLineTag(selection_start_pos, out selection_start.line,
out selection_start.tag, out selection_start.pos);
CharIndexToLineTag(selection_start_pos + s.Length, out selection_end.line,
out selection_end.tag, out selection_end.pos);
selection_anchor.line = selection_start.line;
selection_anchor.pos = selection_start.pos;
selection_anchor.tag = selection_start.tag;
SetSelectionVisible (true);
}
PositionCaret (selection_start.line, selection_start.pos);
UpdateView (begin_update_line, selection_end.line.line_no - begin_update_line.line_no, begin_update_pos);
}
internal void CharIndexToLineTag(int index, out Line line_out, out LineTag tag_out, out int pos) {
Line line;
LineTag tag;
int i;
int chars;
int start;
chars = 0;
for (i = 1; i <= lines; i++) {
line = GetLine(i);
start = chars;
chars += line.text.Length;
if (index <= chars) {
// we found the line
tag = line.tags;
while (tag != null) {
if (index < (start + tag.Start + tag.Length - 1)) {
line_out = line;
tag_out = LineTag.GetFinalTag (tag);
pos = index - start;
return;
}
if (tag.Next == null) {
Line next_line;
next_line = GetLine(line.line_no + 1);
if (next_line != null) {
line_out = next_line;
tag_out = LineTag.GetFinalTag (next_line.tags);
pos = 0;
return;
} else {
line_out = line;
tag_out = LineTag.GetFinalTag (tag);
pos = line_out.text.Length;
return;
}
}
tag = tag.Next;
}
}
}
line_out = GetLine(lines);
tag = line_out.tags;
while (tag.Next != null) {
tag = tag.Next;
}
tag_out = tag;
pos = line_out.text.Length;
}
internal int LineTagToCharIndex(Line line, int pos) {
int i;
int length;
// Count first and last line
length = 0;
// Count the lines in the middle
for (i = 1; i < line.line_no; i++) {
length += GetLine(i).text.Length;
}
length += pos;
return length;
}
internal int SelectionLength() {
if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
return 0;
}
if (selection_start.line == selection_end.line) {
return selection_end.pos - selection_start.pos;
} else {
int i;
int start;
int end;
int length;
// Count first and last line
length = selection_start.line.text.Length - selection_start.pos + selection_end.pos + crlf_size;
// Count the lines in the middle
start = selection_start.line.line_no + 1;
end = selection_end.line.line_no;
if (start < end) {
for (i = start; i < end; i++) {
Line line = GetLine (i);
length += line.text.Length + LineEndingLength (line.ending);
}
}
return length;
}
}
// UIA: Method used via reflection in TextRangeProvider
/// <summary>Give it a Line number and it returns the Line object at with that line number</summary>
internal Line GetLine(int LineNo) {
Line line = document;
while (line != sentinel) {
if (LineNo == line.line_no) {
return line;
} else if (LineNo < line.line_no) {
line = line.left;
} else {
line = line.right;
}
}
return null;
}
/// <summary>Retrieve the previous tag; walks line boundaries</summary>
internal LineTag PreviousTag(LineTag tag) {
Line l;
if (tag.Previous != null) {
return tag.Previous;
}
// Next line
if (tag.Line.line_no == 1) {
return null;
}
l = GetLine(tag.Line.line_no - 1);
if (l != null) {
LineTag t;
t = l.tags;
while (t.Next != null) {
t = t.Next;
}
return t;
}
return null;
}
/// <summary>Retrieve the next tag; walks line boundaries</summary>
internal LineTag NextTag(LineTag tag) {
Line l;
if (tag.Next != null) {
return tag.Next;
}
// Next line
l = GetLine(tag.Line.line_no + 1);
if (l != null) {
return l.tags;
}
return null;
}
internal Line ParagraphStart(Line line) {
Line lastline = line;
do {
if (line.line_no <= 1)
break;
line = lastline;
lastline = GetLine (line.line_no - 1);
} while (lastline.ending == LineEnding.Wrap);
return line;
}
internal Line ParagraphEnd(Line line) {
Line l;
while (line.ending == LineEnding.Wrap) {
l = GetLine(line.line_no + 1);
if ((l == null) || (l.ending != LineEnding.Wrap)) {
break;
}
line = l;
}
return line;
}
/// <summary>Give it a pixel offset coordinate and it returns the Line covering that are (offset
/// is either X or Y depending on if we are multiline
/// </summary>
internal Line GetLineByPixel (int offset, bool exact)
{
Line line = document;
Line last = null;
if (multiline) {
while (line != sentinel) {
last = line;
if ((offset >= line.Y) && (offset < (line.Y+line.height))) {
return line;
} else if (offset < line.Y) {
line = line.left;
} else {
line = line.right;
}
}
} else {
while (line != sentinel) {
last = line;
if ((offset >= line.X) && (offset < (line.X + line.Width)))
return line;
else if (offset < line.X)
line = line.left;
else
line = line.right;
}
}
if (exact) {
return null;
}
return last;
}
// UIA: Method used via reflection in TextProviderBehavior
// Give it x/y pixel coordinates and it returns the Tag at that position
internal LineTag FindCursor (int x, int y, out int index)
{
Line line;
x -= offset_x;
y -= offset_y;
line = GetLineByPixel (multiline ? y : x, false);
LineTag tag = line.GetTag (x);
if (tag.Length == 0 && tag.Start == 1)
index = 0;
else
index = tag.GetCharIndex (x - line.align_shift);
return tag;
}
/// <summary>Format area of document in specified font and color</summary>
/// <param name="start_pos">1-based start position on start_line</param>
/// <param name="end_pos">1-based end position on end_line </param>
internal void FormatText (Line start_line, int start_pos, Line end_line, int end_pos, Font font,
Color color, Color back_color, FormatSpecified specified)
{
Line l;
// First, format the first line
if (start_line != end_line) {
// First line
LineTag.FormatText(start_line, start_pos, start_line.text.Length - start_pos + 1, font, color, back_color, specified);
// Format last line
LineTag.FormatText(end_line, 1, end_pos, font, color, back_color, specified);
// Now all the lines inbetween
for (int i = start_line.line_no + 1; i < end_line.line_no; i++) {
l = GetLine(i);
LineTag.FormatText(l, 1, l.text.Length, font, color, back_color, specified);
}
} else {
// Special case, single line
LineTag.FormatText(start_line, start_pos, end_pos - start_pos, font, color, back_color, specified);
if ((end_pos - start_pos) == 0 && CaretTag.Length != 0)
CaretTag = CaretTag.Next;
}
}
internal void RecalculateAlignments ()
{
Line line;
int line_no;
line_no = 1;
while (line_no <= lines) {
line = GetLine(line_no);
if (line != null) {
switch (line.alignment) {
case HorizontalAlignment.Left:
line.align_shift = 0;
break;
case HorizontalAlignment.Center:
line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
break;
case HorizontalAlignment.Right:
line.align_shift = viewport_width - (int)line.widths[line.text.Length] - right_margin;
break;
}
}
line_no++;
}
return;
}
/// <summary>Calculate formatting for the whole document</summary>
internal bool RecalculateDocument(Graphics g) {
return RecalculateDocument(g, 1, this.lines, false);
}
/// <summary>Calculate formatting starting at a certain line</summary>
internal bool RecalculateDocument(Graphics g, int start) {
return RecalculateDocument(g, start, this.lines, false);
}
/// <summary>Calculate formatting within two given line numbers</summary>
internal bool RecalculateDocument(Graphics g, int start, int end) {
return RecalculateDocument(g, start, end, false);
}
/// <summary>With optimize on, returns true if line heights changed</summary>
internal bool RecalculateDocument(Graphics g, int start, int end, bool optimize) {
Line line;
int line_no;
int offset;
int new_width;
bool changed;
int shift;
if (recalc_suspended > 0) {
recalc_pending = true;
recalc_start = Math.Min (recalc_start, start);
recalc_end = Math.Max (recalc_end, end);
recalc_optimize = optimize;
return false;
}
// Fixup the positions, they can go kinda nuts
// (this is suspend and resume recalc - they set them to 1 and max)
start = Math.Max (start, 1);
end = Math.Min (end, lines);
offset = GetLine(start).offset;
line_no = start;
new_width = 0;
shift = this.lines;
if (!optimize) {
changed = true; // We always return true if we run non-optimized
} else {
changed = false;
}
while (line_no <= (end + this.lines - shift)) {
line = GetLine(line_no++);
line.offset = offset;
// if we are not calculating a password
if (!calc_pass) {
if (!optimize) {
line.RecalculateLine(g, this);
} else {
if (line.recalc && line.RecalculateLine(g, this)) {
changed = true;
// If the height changed, all subsequent lines change
end = this.lines;
shift = this.lines;
}
}
} else {
if (!optimize) {
line.RecalculatePasswordLine(g, this);
} else {
if (line.recalc && line.RecalculatePasswordLine(g, this)) {
changed = true;
// If the height changed, all subsequent lines change
end = this.lines;
shift = this.lines;
}
}
}
if (line.widths[line.text.Length] > new_width) {
new_width = (int)line.widths[line.text.Length];
}
// Calculate alignment
if (line.alignment != HorizontalAlignment.Left) {
if (line.alignment == HorizontalAlignment.Center) {
line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
} else {
line.align_shift = viewport_width - (int)line.widths[line.text.Length] - 1;
}
}
if (multiline)
offset += line.height;
else
offset += (int) line.widths [line.text.Length];
if (line_no > lines) {
break;
}
}
if (document_x != new_width) {
document_x = new_width;
if (WidthChanged != null) {
WidthChanged(this, null);
}
}
RecalculateAlignments();
line = GetLine(lines);
if (document_y != line.Y + line.height) {
document_y = line.Y + line.height;
if (HeightChanged != null) {
HeightChanged(this, null);
}
}
// scan for links and tell us if its all
// changed, so we can update everything
if (EnableLinks)
ScanForLinks (start, end, ref changed);
UpdateCaret();
return changed;
}
internal int Size() {
return lines;
}
private void owner_HandleCreated(object sender, EventArgs e) {
RecalculateDocument(owner.CreateGraphicsInternal());
AlignCaret();
}
private void owner_VisibleChanged(object sender, EventArgs e) {
if (owner.Visible) {
RecalculateDocument(owner.CreateGraphicsInternal());
}
}
internal static bool IsWordSeparator (char ch)
{
switch (ch) {
case ' ':
case '\t':
case '(':
case ')':
case '\r':
case '\n':
return true;
default:
return false;
}
}
internal int FindWordSeparator(Line line, int pos, bool forward) {
int len;
len = line.text.Length;
if (forward) {
for (int i = pos + 1; i < len; i++) {
if (IsWordSeparator(line.Text[i])) {
return i + 1;
}
}
return len;
} else {
for (int i = pos - 1; i > 0; i--) {
if (IsWordSeparator(line.Text[i - 1])) {
return i;
}
}
return 0;
}
}
/* Search document for text */
internal bool FindChars(char[] chars, Marker start, Marker end, out Marker result) {
Line line;
int line_no;
int pos;
int line_len;
// Search for occurence of any char in the chars array
result = new Marker();
line = start.line;
line_no = start.line.line_no;
pos = start.pos;
while (line_no <= end.line.line_no) {
line_len = line.text.Length;
while (pos < line_len) {
for (int i = 0; i < chars.Length; i++) {
if (line.text[pos] == chars[i]) {
// Special case
if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
return false;
}
result.line = line;
result.pos = pos;
return true;
}
}
pos++;
}
pos = 0;
line_no++;
line = GetLine(line_no);
}
return false;
}
// This version does not build one big string for searching, instead it handles
// line-boundaries, which is faster and less memory intensive
// FIXME - Depending on culture stuff we might have to create a big string and use culturespecific
// search stuff and change it to accept and return positions instead of Markers (which would match
// RichTextBox behaviour better but would be inconsistent with the rest of TextControl)
internal bool Find(string search, Marker start, Marker end, out Marker result, RichTextBoxFinds options) {
Marker last;
string search_string;
Line line;
int line_no;
int pos;
int line_len;
int current;
bool word;
bool word_option;
bool ignore_case;
bool reverse;
char c;
result = new Marker();
word_option = ((options & RichTextBoxFinds.WholeWord) != 0);
ignore_case = ((options & RichTextBoxFinds.MatchCase) == 0);
reverse = ((options & RichTextBoxFinds.Reverse) != 0);
line = start.line;
line_no = start.line.line_no;
pos = start.pos;
current = 0;
// Prep our search string, lowercasing it if we do case-independent matching
if (ignore_case) {
StringBuilder sb;
sb = new StringBuilder(search);
for (int i = 0; i < sb.Length; i++) {
sb[i] = Char.ToLower(sb[i]);
}
search_string = sb.ToString();
} else {
search_string = search;
}
// We need to check if the character before our start position is a wordbreak
if (word_option) {
if (line_no == 1) {
if ((pos == 0) || (IsWordSeparator(line.text[pos - 1]))) {
word = true;
} else {
word = false;
}
} else {
if (pos > 0) {
if (IsWordSeparator(line.text[pos - 1])) {
word = true;
} else {
word = false;
}
} else {
// Need to check the end of the previous line
Line prev_line;
prev_line = GetLine(line_no - 1);
if (prev_line.ending == LineEnding.Wrap) {
if (IsWordSeparator(prev_line.text[prev_line.text.Length - 1])) {
word = true;
} else {
word = false;
}
} else {
word = true;
}
}
}
} else {
word = false;
}
// To avoid duplication of this loop with reverse logic, we search
// through the document, remembering the last match and when returning
// report that last remembered match
last = new Marker();
last.height = -1; // Abused - we use it to track change
while (line_no <= end.line.line_no) {
if (line_no != end.line.line_no) {
line_len = line.text.Length;
} else {
line_len = end.pos;
}
while (pos < line_len) {
if (word_option && (current == search_string.Length)) {
if (IsWordSeparator(line.text[pos])) {
if (!reverse) {
goto FindFound;
} else {
last = result;
current = 0;
}
} else {
current = 0;
}
}
if (ignore_case) {
c = Char.ToLower(line.text[pos]);
} else {
c = line.text[pos];
}
if (c == search_string[current]) {
if (current == 0) {
result.line = line;
result.pos = pos;
}
if (!word_option || (word_option && (word || (current > 0)))) {
current++;
}
if (!word_option && (current == search_string.Length)) {
if (!reverse) {
goto FindFound;
} else {
last = result;
current = 0;
}
}
} else {
current = 0;
}
pos++;
if (!word_option) {
continue;
}
if (IsWordSeparator(c)) {
word = true;
} else {
word = false;
}
}
if (word_option) {
// Mark that we just saw a word boundary
if (line.ending != LineEnding.Wrap || line.line_no == lines - 1) {
word = true;
}
if (current == search_string.Length) {
if (word) {
if (!reverse) {
goto FindFound;
} else {
last = result;
current = 0;
}
} else {
current = 0;
}
}
}
pos = 0;
line_no++;
line = GetLine(line_no);
}
if (reverse) {
if (last.height != -1) {
result = last;
return true;
}
}
return false;
FindFound:
if (!reverse) {
// if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
// return false;
// }
return true;
}
result = last;
return true;
}
/* Marker stuff */
internal void GetMarker(out Marker mark, bool start) {
mark = new Marker();
if (start) {
mark.line = GetLine(1);
mark.tag = mark.line.tags;
mark.pos = 0;
} else {
mark.line = GetLine(lines);
mark.tag = mark.line.tags;
while (mark.tag.Next != null) {
mark.tag = mark.tag.Next;
}
mark.pos = mark.line.text.Length;
}
}
#endregion // Internal Methods
#region Events
internal event EventHandler CaretMoved;
internal event EventHandler WidthChanged;
internal event EventHandler HeightChanged;
internal event EventHandler LengthChanged;
internal event EventHandler UIASelectionChanged;
#endregion // Events
#region Administrative
public IEnumerator GetEnumerator() {
// FIXME
return null;
}
public override bool Equals(object obj) {
if (obj == null) {
return false;
}
if (!(obj is Document)) {
return false;
}
if (obj == this) {
return true;
}
if (ToString().Equals(((Document)obj).ToString())) {
return true;
}
return false;
}
public override int GetHashCode() {
return document_id;
}
public override string ToString() {
return "document " + this.document_id;
}
#endregion // Administrative
}
internal class PictureTag : LineTag {
internal RTF.Picture picture;
internal PictureTag (Line line, int start, RTF.Picture picture) : base (line, start)
{
this.picture = picture;
}
public override bool IsTextTag {
get { return false; }
}
public override SizeF SizeOfPosition (Graphics dc, int pos)
{
return picture.Size;
}
internal override int MaxHeight ()
{
return (int) (picture.Height + 0.5F);
}
public override void Draw (Graphics dc, Color color, float xoff, float y, int start, int end)
{
picture.DrawImage (dc, xoff + Line.widths [start], y, false);
}
public override void Draw (Graphics dc, Color color, float xoff, float y, int start, int end, string text)
{
picture.DrawImage (dc, xoff + + Line.widths [start], y, false);
}
public override string Text ()
{
return "I";
}
}
internal class UndoManager {
internal enum ActionType {
Typing,
// This is basically just cut & paste
InsertString,
DeleteString,
UserActionBegin,
UserActionEnd
}
internal class Action {
internal ActionType type;
internal int line_no;
internal int pos;
internal object data;
}
#region Local Variables
private Document document;
private Stack undo_actions;
private Stack redo_actions;
//private int caret_line;
//private int caret_pos;
// When performing an action, we lock the queue, so that the action can't be undone
private bool locked;
#endregion // Local Variables
#region Constructors
internal UndoManager (Document document)
{
this.document = document;
undo_actions = new Stack (50);
redo_actions = new Stack (50);
}
#endregion // Constructors
#region Properties
internal bool CanUndo {
get { return undo_actions.Count > 0; }
}
internal bool CanRedo {
get { return redo_actions.Count > 0; }
}
internal string UndoActionName {
get {
foreach (Action action in undo_actions) {
if (action.type == ActionType.UserActionBegin)
return (string) action.data;
if (action.type == ActionType.Typing)
return Locale.GetText ("Typing");
}
return String.Empty;
}
}
internal string RedoActionName {
get {
foreach (Action action in redo_actions) {
if (action.type == ActionType.UserActionBegin)
return (string) action.data;
if (action.type == ActionType.Typing)
return Locale.GetText ("Typing");
}
return String.Empty;
}
}
#endregion // Properties
#region Internal Methods
internal void Clear ()
{
undo_actions.Clear();
redo_actions.Clear();
}
internal bool Undo ()
{
Action action;
bool user_action_finished = false;
if (undo_actions.Count == 0)
return false;
locked = true;
do {
Line start;
action = (Action) undo_actions.Pop ();
// Put onto redo stack
redo_actions.Push(action);
// Do the thing
switch(action.type) {
case ActionType.UserActionBegin:
user_action_finished = true;
break;
case ActionType.UserActionEnd:
// noop
break;
case ActionType.InsertString:
start = document.GetLine (action.line_no);
document.SuspendUpdate ();
document.DeleteMultiline (start, action.pos, ((string) action.data).Length + 1);
document.PositionCaret (start, action.pos);
document.SetSelectionToCaret (true);
document.ResumeUpdate (true);
break;
case ActionType.Typing:
start = document.GetLine (action.line_no);
document.SuspendUpdate ();
document.DeleteMultiline (start, action.pos, ((StringBuilder) action.data).Length);
document.PositionCaret (start, action.pos);
document.SetSelectionToCaret (true);
document.ResumeUpdate (true);
// This is an open ended operation, so only a single typing operation can be undone at once
user_action_finished = true;
break;
case ActionType.DeleteString:
start = document.GetLine (action.line_no);
document.SuspendUpdate ();
Insert (start, action.pos, (Line) action.data, true);
document.ResumeUpdate (true);
break;
}
} while (!user_action_finished && undo_actions.Count > 0);
locked = false;
return true;
}
internal bool Redo ()
{
Action action;
bool user_action_finished = false;
if (redo_actions.Count == 0)
return false;
locked = true;
do {
Line start;
int start_index;
action = (Action) redo_actions.Pop ();
undo_actions.Push (action);
switch (action.type) {
case ActionType.UserActionBegin:
// Noop
break;
case ActionType.UserActionEnd:
user_action_finished = true;
break;
case ActionType.InsertString:
start = document.GetLine (action.line_no);
document.SuspendUpdate ();
start_index = document.LineTagToCharIndex (start, action.pos);
document.InsertString (start, action.pos, (string) action.data);
document.CharIndexToLineTag (start_index + ((string) action.data).Length,
out document.caret.line, out document.caret.tag,
out document.caret.pos);
document.UpdateCaret ();
document.SetSelectionToCaret (true);
document.ResumeUpdate (true);
break;
case ActionType.Typing:
start = document.GetLine (action.line_no);
document.SuspendUpdate ();
start_index = document.LineTagToCharIndex (start, action.pos);
document.InsertString (start, action.pos, ((StringBuilder) action.data).ToString ());
document.CharIndexToLineTag (start_index + ((StringBuilder) action.data).Length,
out document.caret.line, out document.caret.tag,
out document.caret.pos);
document.UpdateCaret ();
document.SetSelectionToCaret (true);
document.ResumeUpdate (true);
// This is an open ended operation, so only a single typing operation can be undone at once
user_action_finished = true;
break;
case ActionType.DeleteString:
start = document.GetLine (action.line_no);
document.SuspendUpdate ();
document.DeleteMultiline (start, action.pos, ((Line) action.data).text.Length);
document.PositionCaret (start, action.pos);
document.SetSelectionToCaret (true);
document.ResumeUpdate (true);
break;
}
} while (!user_action_finished && redo_actions.Count > 0);
locked = false;
return true;
}
#endregion // Internal Methods
#region Private Methods
public void BeginUserAction (string name)
{
if (locked)
return;
// Nuke the redo queue
redo_actions.Clear ();
Action ua = new Action ();
ua.type = ActionType.UserActionBegin;
ua.data = name;
undo_actions.Push (ua);
}
public void EndUserAction ()
{
if (locked)
return;
Action ua = new Action ();
ua.type = ActionType.UserActionEnd;
undo_actions.Push (ua);
}
// start_pos, end_pos = 1 based
public void RecordDeleteString (Line start_line, int start_pos, Line end_line, int end_pos)
{
if (locked)
return;
// Nuke the redo queue
redo_actions.Clear ();
Action a = new Action ();
// We cant simply store the string, because then formatting would be lost
a.type = ActionType.DeleteString;
a.line_no = start_line.line_no;
a.pos = start_pos;
a.data = Duplicate (start_line, start_pos, end_line, end_pos);
undo_actions.Push(a);
}
public void RecordInsertString (Line line, int pos, string str)
{
if (locked || str.Length == 0)
return;
// Nuke the redo queue
redo_actions.Clear ();
Action a = new Action ();
a.type = ActionType.InsertString;
a.data = str;
a.line_no = line.line_no;
a.pos = pos;
undo_actions.Push (a);
}
public void RecordTyping (Line line, int pos, char ch)
{
if (locked)
return;
// Nuke the redo queue
redo_actions.Clear ();
Action a = null;
if (undo_actions.Count > 0)
a = (Action) undo_actions.Peek ();
if (a == null || a.type != ActionType.Typing) {
a = new Action ();
a.type = ActionType.Typing;
a.data = new StringBuilder ();
a.line_no = line.line_no;
a.pos = pos;
undo_actions.Push (a);
}
StringBuilder data = (StringBuilder) a.data;
data.Append (ch);
}
// start_pos = 1-based
// end_pos = 1-based
public Line Duplicate(Line start_line, int start_pos, Line end_line, int end_pos)
{
Line ret;
Line line;
Line current;
LineTag tag;
LineTag current_tag;
int start;
int end;
int tag_start;
line = new Line (start_line.document, start_line.ending);
ret = line;
for (int i = start_line.line_no; i <= end_line.line_no; i++) {
current = document.GetLine(i);
if (start_line.line_no == i) {
start = start_pos;
} else {
start = 0;
}
if (end_line.line_no == i) {
end = end_pos;
} else {
end = current.text.Length;
}
if (end_pos == 0)
continue;
// Text for the tag
line.text = new StringBuilder (current.text.ToString (start, end - start));
// Copy tags from start to start+length onto new line
current_tag = current.FindTag (start + 1);
while ((current_tag != null) && (current_tag.Start <= end)) {
if ((current_tag.Start <= start) && (start < (current_tag.Start + current_tag.Length))) {
// start tag is within this tag
tag_start = start;
} else {
tag_start = current_tag.Start;
}
tag = new LineTag(line, tag_start - start + 1);
tag.CopyFormattingFrom (current_tag);
current_tag = current_tag.Next;
// Add the new tag to the line
if (line.tags == null) {
line.tags = tag;
} else {
LineTag tail;
tail = line.tags;
while (tail.Next != null) {
tail = tail.Next;
}
tail.Next = tag;
tag.Previous = tail;
}
}
if ((i + 1) <= end_line.line_no) {
line.ending = current.ending;
// Chain them (we use right/left as next/previous)
line.right = new Line (start_line.document, start_line.ending);
line.right.left = line;
line = line.right;
}
}
return ret;
}
// Insert multi-line text at the given position; use formatting at insertion point for inserted text
internal void Insert(Line line, int pos, Line insert, bool select)
{
Line current;
LineTag tag;
int offset;
int lines;
Line first;
// Handle special case first
if (insert.right == null) {
// Single line insert
document.Split(line, pos);
if (insert.tags == null) {
return; // Blank line
}
//Insert our tags at the end
tag = line.tags;
while (tag.Next != null) {
tag = tag.Next;
}
offset = tag.Start + tag.Length - 1;
tag.Next = insert.tags;
line.text.Insert(offset, insert.text.ToString());
// Adjust start locations
tag = tag.Next;
while (tag != null) {
tag.Start += offset;
tag.Line = line;
tag = tag.Next;
}
// Put it back together
document.Combine(line.line_no, line.line_no + 1);
if (select) {
document.SetSelectionStart (line, pos, false);
document.SetSelectionEnd (line, pos + insert.text.Length, false);
}
document.UpdateView(line, pos);
return;
}
first = line;
lines = 1;
current = insert;
while (current != null) {
if (current == insert) {
// Inserting the first line we split the line (and make space)
document.Split(line.line_no, pos);
//Insert our tags at the end of the line
tag = line.tags;
if (tag != null && tag.Length != 0) {
while (tag.Next != null) {
tag = tag.Next;
}
offset = tag.Start + tag.Length - 1;
tag.Next = current.tags;
tag.Next.Previous = tag;
tag = tag.Next;
} else {
offset = 0;
line.tags = current.tags;
line.tags.Previous = null;
tag = line.tags;
}
line.ending = current.ending;
} else {
document.Split(line.line_no, 0);
offset = 0;
line.tags = current.tags;
line.tags.Previous = null;
line.ending = current.ending;
tag = line.tags;
}
// Adjust start locations and line pointers
while (tag != null) {
tag.Start += offset - 1;
tag.Line = line;
tag = tag.Next;
}
line.text.Insert(offset, current.text.ToString());
line.Grow(line.text.Length);
line.recalc = true;
line = document.GetLine(line.line_no + 1);
// FIXME? Test undo of line-boundaries
if ((current.right == null) && (current.tags.Length != 0)) {
document.Combine(line.line_no - 1, line.line_no);
}
current = current.right;
lines++;
}
// Recalculate our document
document.UpdateView(first, lines, pos);
return;
}
#endregion // Private Methods
}
}
|