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 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090
|
/*
Copyright (C) 1999-2002 Ricardo Ueda Karpischek
This is free software; you can redistribute it and/or modify
it under the terms of the version 2 of the GNU General Public
License as published by the Free Software Foundation.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
*/
/*
redraw.c: The function redraw.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "common.h"
#include "gui.h"
/*
Menu flags.
*/
char *cm_v_small = NULL,
*cm_v_medium = NULL,
*cm_v_large = NULL,
*cm_v_def = NULL,
*cm_v_hide = NULL,
*cm_v_of = NULL,
*cm_v_wclip = NULL,
*cm_v_map = NULL,
*cm_v_vhs = NULL,
*cm_v_cc = NULL,
*cm_v_mat = NULL,
*cm_v_mat_k = NULL,
*cm_v_cmp_k = NULL,
*cm_v_cmp = NULL,
*cm_v_st = NULL,
*cm_e_od = NULL,
*cm_e_rescan = NULL,
*cm_e_pp = NULL,
*cm_e_pp_c = NULL,
*cm_e_fill = NULL,
*cm_e_fill_c = NULL,
*cm_e_sm = NULL,
*cm_e_st = NULL,
*cm_e_sp = NULL,
*cm_e_sw = NULL,
*cm_e_sh = NULL,
*cm_e_sn = NULL,
*cm_e_ac = NULL,
*cm_o_curr = NULL,
*cm_o_all = NULL,
*cm_o_dkeys = NULL,
*cm_o_amenu = NULL,
*cm_o_pgo = NULL,
*cm_a_arabic = NULL,
*cm_a_cyrillic = NULL,
*cm_a_greek = NULL,
*cm_a_hebrew = NULL,
*cm_a_latin = NULL,
*cm_a_kana = NULL,
*cm_a_abbrev = NULL,
*cm_a_number = NULL,
*cm_a_ideogram = NULL,
*cm_b_skel = NULL,
*cm_b_border = NULL,
*cm_b_hs = NULL,
*cm_b_hb = NULL,
*cm_d_pixels = NULL,
*cm_d_closures = NULL,
*cm_d_symbols = NULL,
*cm_d_words = NULL,
*cm_d_ptype = NULL,
*cm_d_rs = NULL,
*cm_d_bb = NULL,
*cm_g_glines = NULL,
*cm_g_lb = NULL,
*cm_g_abagar = NULL,
*cm_g_align = NULL,
*cm_g_bo = NULL,
*cm_g_io = NULL,
*cm_g_sum = NULL,
*cm_g_vocab = NULL,
*cm_g_log = NULL;
/*
Arrows used as labels in some buttons (deprecated).
*/
short gldbl[] = {
0,2,1,2,2,2,3,2,4,2,5,2,6,2,7,2,8,2,9,2,10,2,11,2,12,2,
1,1,2,1,3,0,4,0,
3,1,4,1,
1,3,2,3,3,4,4,4,
3,3,4,3,
6,1,7,1,8,0,9,0,
8,1,9,1,
6,3,7,3,8,4,9,4,
8,3,9,3
};
short glend[] = {
0,2,1,2,2,2,3,2,4,2,5,2,6,2,7,2,8,2,9,2,10,2,11,2,12,2,
2,1,3,1,4,0,5,0,
4,1,5,1,
2,3,3,3,4,4,5,4,
4,3,5,3,
0,1,0,0,
0,3,0,4
};
short glleft[] = {
0,2,1,2,2,2,3,2,4,2,5,2,6,2,7,2,8,2,9,2,10,2,11,2,12,2,
2,1,3,1,4,0,5,0,
4,1,5,1,
2,3,3,3,4,4,5,4,
4,3,5,3,
};
/*
Menus stuff
-----------
CX0 and CY0 are the top-left window-relative coordinates of the
current context menu. When the user invokes one menu clicking the
menu bar or another sensitive region, CX0 and CY0 store the
pointer position. But when the function redraw effectively pops
the menu, it choose a "best" placement and may change CX0 and
CY0. However, theoriginal pointer position is stored into
CX0_orig and CY0_orig.
CW and CH are the width ahd height of the current menu.
The current menu (if any) is pointed by cmenu. If there is no
current menu, cmenu is NULL.
The array CM stores all registered menus. The size of CM is CM_SZ
and the top of CM (largest index of CM entry in use) is TOP_CM.
*/
int CX0,CY0,CX0_orig=-1,CY0_orig=-1,CW,CH;
cmdesc *cmenu=NULL;
cmdesc *CM=NULL;
int TOP_CM=-1,CM_SZ=0;
/*
Sliding mode
0 .. not sliding
1 .. sliding horizontally
2 .. sliding vertically
3 .. resizing PAGE/OUTPUT junction
4 .. resizing OUTPUT/SYMBOL junction
*/
int sliding = 0,s0;
/* (devel)
Geometry of the application window
----------------------------------
The source code frequently refers some global variables that define
the position and size of the main componts (the plate, buttons,
etc). Most of these variables are set by comp_wnd_size. The variables
are:
WH .. application window height
WW .. application window width
PH .. plate height
PW .. plate width
BW .. button width
BH .. button width
MRF .. maximum reduction factor
TW .. tab width
TH .. tab height
PM .. plate horizontal margin
PT .. plate top margin
RW .. scrollbar width
MH .. menubar heigth
MRF applies to the scanned document and to the web clip.
*/
int WH,WW,WX,WY,PH,PW;
int BW,BH,MRF=4,TW,TH,PM,PT;
int RW=12;
int MH;
/*
BUTTONS
*/
char *button = NULL;
int BUTT_PER_COL=0,bl_sz=0;
char **BL = NULL;
/* number of alphabets */
int nalpha;
/*
Divisors on PAGE tab.
*/
int page_j1=-1,page_j2=-1,opage_j1=-1,opage_j2=-1;
int LAST_PAGE_DT,LAST_PAGE_DM,LAST_PAGE_DH;
int LAST_OUTPUT_DT,LAST_OUTPUT_DH;
int LAST_SYMBOL_DT,LAST_SYMBOL_DH;
/*
Really thin (1-pixel) things (like "i" or "l" or "." as
renderized by the GUI using small fonts) are not displayed unless
this flag is set.
*/
int draw_thin=1;
/*
Make sure that the current window does not surpass the
document limits, changing X0 and Y0 if necessary.
If cursoron is nonzero, make the best efforts to make
sure that the cursor in within the window, changing X0 and
Y0 if necessary.
*/
void check_dlimits(int cursoron)
{
/* make sure the current item is visible */
if (cursoron) {
/* PAGE */
if ((CDW == PAGE) && (0 <= curr_mc) && (curr_mc <= tops)) {
if (mc[curr_mc].l < X0)
X0 = mc[curr_mc].l - HR/2;
if (mc[curr_mc].r >= X0 + HR)
X0 = mc[curr_mc].r - HR/2;
if (mc[curr_mc].b < Y0)
Y0 = mc[curr_mc].t - VR/2;
if (mc[curr_mc].t >= Y0 + VR)
Y0 = mc[curr_mc].b - VR/2;
}
/* HTML windows */
else if ((HTML) && (0 <= curr_hti) && (curr_hti <= topge)) {
if (ge[curr_hti].l < X0)
X0 = ge[curr_hti].l - HR/2;
if (ge[curr_hti].r >= X0 + HR)
X0 = ge[curr_hti].r - HR/2;
if (ge[curr_hti].b < Y0)
Y0 = ge[curr_hti].t - VR/2;
if (ge[curr_hti].t >= Y0 + VR)
Y0 = ge[curr_hti].b - VR/2;
}
}
if (X0 > (GRX-HR))
X0 = GRX - HR;
if (X0 < 0)
X0 = 0;
if (Y0 > (GRY-VR))
Y0 = GRY - VR;
if (Y0 < 0)
Y0 = 0;
}
/*
Draw button
-----------
i i+w-1
j +-+-----+-+
+ +-----+ |
| | | |
| | | |
| | | |
| | | |
| | | |
+ +-----+ |
j+h-1 +---------+
If p is 0, draw the button "unpressed". If p is 1, draw the
button "pressed" ("on"). If p is 2, draw the button "checked"
using an "x" character. If p is 3, draw the button "checked" but
building an "x" using XDrawLine.
*/
void draw_button(int i,int j,int w,int h,int p,char *l)
{
/* button background */
XSetForeground(xd,xgc,gray.pixel);
XFillRectangle(xd,xw,xgc,i+2,j+2,w-3,h-3);
/* shadow */
XSetLineAttributes(xd,xgc,1,LineSolid,CapButt,JoinRound);
XSetForeground(xd,xgc,((p==1)?white.pixel:vdgray.pixel));
XDrawLine(xd,xw,xgc,i,j+h-1,i+w,j+h-1);
XDrawLine(xd,xw,xgc,i+1,j+h-2,i+w-2,j+h-2);
XDrawLine(xd,xw,xgc,i+w-1,j+h-1,i+w-1,j);
XDrawLine(xd,xw,xgc,i+w-2,j+h-2,i+w-2,j+1);
/* light */
XSetForeground(xd,xgc,((p==1)?vdgray.pixel:white.pixel));
XDrawLine(xd,xw,xgc,i,j,i+w-1,j);
XDrawLine(xd,xw,xgc,i+1,j+1,i+w-2,j+1);
XDrawLine(xd,xw,xgc,i,j,i,j+h-1);
XDrawLine(xd,xw,xgc,i+1,j+1,i+1,j+h-2);
/* check mark */
if (p == 2) {
int x,y,u,v;
if (w&1) {
x = i+3;
u = i+w-5;
}
else {
x = i+4;
u = i+w-5;
}
if (h&1) {
y = j+3;
v = j+h-5;
}
else {
y = j+4;
v = j+h-5;
}
XSetForeground(xd,xgc,black.pixel);
XDrawLine(xd,xw,xgc,x,y,u+1,v+1);
XDrawLine(xd,xw,xgc,x,v,u+1,y-1);
}
else if (p == 3)
l = "x";
/* label */
if (l != NULL) {
XTextItem xti;
XSetForeground (xd,xgc,black.pixel);
xti.delta = 0;
xti.font = dfont->fid;
xti.chars = l;
if ((xti.nchars = strlen(xti.chars)) > 0)
XDrawText(xd,xw,xgc,i+(w-DFW*xti.nchars)/2,
j+(h-DFH)/2+(dfont->max_bounds).ascent,&xti,1);
}
}
/*
Draw arrow
t: type (0=double arrow, 1=end arrow, 2=left arrow)
0: orientation (0=left, 1=right, 2=up, 3=down)
*/
void draw_arrow(int t,int x,int y,int o)
{
int i,s;
XPoint dl[37];
short *arr;
arr = (t == 0) ? gldbl : ((t == 1) ? glend : glleft);
s = (t == 0) ? 37 : ((t == 1) ? 29 : 25);
if (o == 0) {
for (i=0; i<s; ++i) {
dl[i].x = arr[2*i]+x;
dl[i].y = arr[2*i+1]+y-2;
}
}
else if (o == 1) {
for (i=0; i<s; ++i) {
dl[i].x = (12-arr[2*i])+x;
dl[i].y = arr[2*i+1]+y-2;
}
}
else if (o == 2) {
for (i=0; i<s; ++i) {
dl[i].x = arr[2*i+1]+x-2;
dl[i].y = arr[2*i]+y;
}
}
else if (o == 3) {
for (i=0; i<s; ++i) {
dl[i].x = arr[2*i+1]+x-2;
dl[i].y = (12-arr[2*i])+y;
}
}
XDrawPoints(xd,xw,xgc,dl,s,CoordModeOrigin);
}
/*
Draw frame. A frame of width t is draw around the rectangle
with top left (i,j) and width w and heigth h. This rectangle
remains untouched. The frame is draw using t pixels to the
left, right, top and bottom out of the rectangle. So the
frame will be (frame pixels are shown with 'X'):
i-t i i+w-1 i+w-1+t
| | | |
j-t - XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX
j - XXX+----------+XXX
XXX| |XXX
XXX| |XXX
XXX| |XXX
XXX| |XXX
XXX| |XXX
XXX| |XXX
j+h-1 - XXX+----------+XXX
XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX
j+h-1+t - XXXXXXXXXXXXXXXXXX
*/
void draw_frame(int i,int j,int w,int h,int t)
{
XFillRectangle(xd,xw,xgc,i-t,j-t,w+t,t);
XFillRectangle(xd,xw,xgc,i+w,j-t,t,h+t);
XFillRectangle(xd,xw,xgc,i-t,j,t,h+t);
XFillRectangle(xd,xw,xgc,i,j+h,w+t,t);
}
/*
Draw buttonbar
*/
void draw_bb(int x,int y,char *BL[],int b0)
{
int i,j;
XTextItem xti;
xti.delta = 0;
xti.font = dfont->fid;
for (i=j=0; i<BUTT_PER_COL; ++i) {
int k,lk,t,f;
char tl[64];
/* for multi-label buttons the flag f must contain 0*/
f = 0;
/* btype label is handled separately */
if (i == btype) {
sprintf(tl,"%d",button[btype]);
xti.chars = tl;
xti.nchars = strlen(xti.chars);
f = 1;
}
/* BL stores the labels of the other buttons */
else {
/* extract current set of labels */
xti.chars = BL[i];
xti.nchars = strlen(xti.chars);
/* extract sublabel */
for (k=lk=t=0; k<=xti.nchars; ++k) {
if ((k==xti.nchars) || (xti.chars[k] == ':')) {
f |= (k<xti.nchars);
if (t == button[i+b0]) {
xti.chars += lk;
xti.nchars = k-lk;
}
else
lk = k+1;
++t;
}
}
}
/* draw button with label */
if ((redraw_button < 0) || (redraw_button == i+b0)) {
XSetForeground(xd,xgc,gray.pixel);
draw_button(x,y+i*(BH+VS),BW,BH,(!f) && (button[i+b0]!=0),NULL);
XSetForeground(xd,xgc,black.pixel);
XDrawText(xd,xw,xgc,x+(BW-DFW*xti.nchars)/2,
y+i*(BH+VS)+(BH-DFH)/2+(dfont->max_bounds).ascent,&xti,1);
}
}
/* buttonbar frame */
if (redraw_button < 0) {
XSetForeground(xd,xgc,vdgray.pixel);
draw_frame(x,y,BW,BUTT_PER_COL*(BH+VS)-VS,1);
XSetForeground(xd,xgc,white.pixel);
draw_frame(x-1,y-1,BW+2,BUTT_PER_COL*(BH+VS)-VS+2,1);
}
}
/*
Draw tab t at coordinates (x,y).
*/
void draw_tab(int x,int y,int t) {
XTextItem xti;
int k;
short arc[] = {
0,0,0,1,0,2,1,0,2,0
};
short arc2[] = {
0,3,1,3,1,2,1,1,2,1,3,1,3,0
};
/* prepare label */
xti.delta = 0;
xti.font = dfont->fid;
if (tab == t)
xti.chars = dwname(CDW);
else
xti.chars = tabl[t];
xti.nchars = strlen(xti.chars);
/* tab background and label */
XSetForeground(xd,xgc,gray.pixel);
XFillRectangle(xd,xw,xgc,x,y,TW,(tab!=t)?TH-2:TH+1);
XSetForeground(xd,xgc,black.pixel);
XDrawText(xd,xw,xgc,x+(TW-4-DFW*xti.nchars)/2,y+DFH,&xti,1);
/* corners */
XSetForeground (xd,xgc,darkgray.pixel);
for (k=0; k<5; ++k) {
XDrawPoint(xd,xw,xgc,x+arc[2*k],y+arc[2*k+1]);
XDrawPoint(xd,xw,xgc,x+TW-1-arc[2*k],y+arc[2*k+1]);
}
/* current tab decoration */
if (tab == t) {
/* white portion frame */
XSetForeground (xd,xgc,white.pixel);
for (k=0; k<7; ++k)
XDrawPoint(xd,xw,xgc,x+arc2[2*k],y+arc2[2*k+1]);
XSetLineAttributes(xd,xgc,2,LineSolid,CapNotLast,JoinRound);
XDrawLine(xd,xw,xgc,x+4,y,x+TW-1-3,y);
XDrawLine(xd,xw,xgc,x,y+4,x,y+TH+1);
/* top line of the plate frame */
XDrawLine(xd,xw,xgc,PM,PT+TH,x,PT+TH);
XDrawLine(xd,xw,xgc,x+TW,PT+TH,PM+PW-1,PT+TH);
/* shadowed portion of the frame */
XSetForeground (xd,xgc,vdgray.pixel);
for (k=0; k<7; ++k)
XDrawPoint(xd,xw,xgc,x+TW-1-arc2[2*k],y+arc2[2*k+1]);
XDrawLine(xd,xw,xgc,x+TW-1,y+4,x+TW-1,y+TH+1);
XSetLineAttributes(xd,xgc,1,LineSolid,CapNotLast,JoinRound);
}
}
/*
Draw the bitmap r using 4 colors. The bitmap r is assumed to be a
matrix RCYxRCX of chars.
*/
void draw_bm(unsigned char *r,int RCX,int RCY,int x,int y)
{
int i,j,u,v;
XSetForeground (xd,xgc,white.pixel);
for (i=u=0; u<RCX; ++i,u+=plist_rf)
for (j=v=0; v<RCY; ++j,v+=plist_rf)
if (r[u+v*RCX] == WHITE)
XDrawPoint(xd,xw,xgc,x+i,y+j);
XSetForeground (xd,xgc,gray.pixel);
for (i=u=0; u<RCX; ++i,u+=plist_rf)
for (j=v=0; v<RCY; ++j,v+=plist_rf)
if (r[u+v*RCX] == GRAY)
XDrawPoint(xd,xw,xgc,x+i,y+j);
XSetForeground (xd,xgc,vdgray.pixel);
for (i=u=0; u<RCX; ++i,u+=plist_rf)
for (j=v=0; v<RCY; ++j,v+=plist_rf)
if (r[u+v*RCX] == VDGRAY)
XDrawPoint(xd,xw,xgc,x+i,y+j);
XSetForeground (xd,xgc,black.pixel);
for (i=u=0; u<RCX; ++i,u+=plist_rf)
for (j=v=0; v<RCY; ++j,v+=plist_rf)
if (r[u+v*RCX] == BLACK)
XDrawPoint(xd,xw,xgc,x+i,y+j);
}
/*
Draw the pixmap r using 4 colors. The bitmap r is assumed to be a
matrix RCYxRCX of chars.
*/
void draw_pm(unsigned char *r,int RCX,int RCY,int x,int y)
{
int i,j,t,u,v,rf,m,bpl;
unsigned char *p;
/* make sure CDW is the correct index */
CDW = PAGE;
rf = dw[PAGE].rf;
/* black-and-white slow code, works on any X display */
if ((disp_opt == 0) || (pm_t == 1)) {
int np,T;
XPoint xp[100];
XSetForeground (xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,DM,DT,DW,DH);
XSetForeground (xd,xgc,black.pixel);
if (pm_t == 8) {
/*
thresholding value.
*/
/*
if ((ocring) &&
(onlystep == OCR_OTHER) &&
(ocr_other == 3))
T = thresh_val;
else
T = 128;
*/
T = thresh_val;
for (np=0, i=0, u=X0; (u<RCX) && (i<DW); ++i,u+=rf) {
for (j=0, v=Y0; (v<RCY) && (j<DH); ++j,v+=rf)
if (r[u+v*RCX] < T) {
if (np >= 100) {
XDrawPoints(xd,xw,xgc,xp,np,CoordModeOrigin);
np = 0;
}
xp[np].x = DM+i;
xp[np].y = DT+j;
++np;
}
}
if (np > 0) {
XDrawPoints(xd,xw,xgc,xp,np,CoordModeOrigin);
}
}
else {
for (np=0, i=0, u=X0; (u<RCX) && (i<DW); ++i,u+=rf) {
m = (1 << (7-u%8));
bpl = (XRES/8) + ((XRES%8) != 0);
p = pixmap + Y0*bpl + u/8;
for (j=0, v=Y0; (v<RCY) && (j<DH); ++j,v+=rf,p+=RF*bpl)
if (*p & m) {
if (np >= 100) {
XDrawPoints(xd,xw,xgc,xp,np,CoordModeOrigin);
np = 0;
}
xp[np].x = DM+i;
xp[np].y = DT+j;
++np;
}
}
if (np > 0) {
XDrawPoints(xd,xw,xgc,xp,np,CoordModeOrigin);
}
}
}
/* optimized per-depth code */
else {
/* color masks and visual (for truecolor) */
static int ht;
static int rd,gd,bd,rw,gw,bw;
static Visual *tv;
/* other */
static XImage *image = NULL;
static unsigned char *imb = NULL;
static int cw=-1,ch=-1,imbsz=0,d=-1,pad,bs;
static unsigned long cmap[256];
int bpl;
/* one-time initializer */
if (d < 0) {
XVisualInfo *vinfo;
XVisualInfo tmpl;
int n;
long m;
/* do we have truecolor? */
tmpl.screen = DefaultScreen(xd);
tmpl.class = TrueColor;
m = VisualClassMask;
vinfo = XGetVisualInfo(xd,m,&tmpl,&n);
/* yes: get visual, depth and RGB info */
if (vinfo != NULL) {
unsigned long rm,gm,bm;
ht = 1;
tv = vinfo[0].visual;
rm = vinfo[0].red_mask;
gm = vinfo[0].green_mask;
bm = vinfo[0].blue_mask;
d = vinfo[0].depth;
XFree(vinfo);
/* RGB displacement and width */
for (rd=0; ((rm & 1) == 0); ++rd, rm >>= 1);
for (rw=0; ((rm & 1) == 1); ++rw, rm >>= 1);
for (gd=0; ((gm & 1) == 0); ++gd, gm >>= 1);
for (gw=0; ((gm & 1) == 1); ++gw, gm >>= 1);
for (bd=0; ((bm & 1) == 0); ++bd, bm >>= 1);
for (bw=0; ((bm & 1) == 1); ++bw, bm >>= 1);
/* if nonspecified, choose number of gray levels */
if (glevels < 0) {
if ((d==15) || (d==16))
glevels = 32;
else if ((d==24) || (d==32))
glevels = 256;
}
}
/* no: get default visual */
else {
ht = 0;
tv = NULL;
tv = DefaultVisual(xd,DefaultScreen(xd));
d = DefaultDepth(xd,DefaultScreen(xd));
glevels = 4;
}
/* padding */
pad = BitmapPad(xd);
}
/* colormap for instant thresholding */
if ((ocring) &&
(onlystep == OCR_OTHER) &&
(ocr_other == 3)) {
for (i=0; i<thresh_val; ++i)
cmap[i] = black.pixel;
for (; i<256; ++i)
cmap[i] = white.pixel;
}
/* black-and-white colormap */
else if (glevels == 2) {
for (i=0; i<128; ++i)
cmap[i] = black.pixel;
for (; i<256; ++i)
cmap[i] = white.pixel;
}
/* 32 graylevels (truecolor 15 or 16bpp) */
else if (glevels == 32) {
for (i=j=0; i<32; ++i) {
for (t=0; t<8; ++t,++j) {
cmap[j] = (((rw==5) ? i:2*i) << rd) +
(((gw==5) ? i:2*i) << gd) +
(((bw==5) ? i:2*i) << bd);
}
}
}
/* 256 graylevels (truecolor 24bpp) */
else if (glevels == 256) {
int i;
for (i=0; i<256; ++i) {
cmap[i] = (i << rd) +
(i << gd) +
(i << bd);
}
}
/* standard 4-colors colormap */
else {
for (i=0; i<64; ++i)
cmap[i] = black.pixel;
for (; i<128; ++i)
cmap[i] = vdgray.pixel;
for (; i<192; ++i)
cmap[i] = gray.pixel;
for (; i<256; ++i)
cmap[i] = white.pixel;
}
/*
bytes per line
(sparse 24bpp by GL)
QUESTION: is it ok drop (d==24) and (d==32)?
*/
if (d == 4)
bpl = DW;
else if (d == 8)
bpl = DW;
else if ((15<=d) && (d<=16))
bpl = 2*DW;
else if ((d == 24) && (pad == 24))
bpl = 3*DW;
else if ((d == 32) || (pad == 32))
bpl = 4*DW;
else {
fatal(XE,"unable to handle depth %d\n",d);
/* just to avoid a compilation warning */
bpl = 0;
}
/* pad bpl */
if (pad == 16)
bpl += (bpl % 2);
else if (pad == 24)
bpl += (3 - (bpl % 3));
else if (pad == 32)
bpl += (4 - (bpl % 4));
/* required buffer size */
bs = bpl * DH;
/* need to reallocate the X image */
if ((DW != cw) || (DH != ch)) {
if (image != NULL) {
image->data = NULL;
XDestroyImage(image);
}
image = XCreateImage(xd,tv,d,ZPixmap,0,NULL,DW,DH,pad,bpl);
}
/* need to enlarge image buffer */
if (bs > imbsz) {
imb = c_realloc(imb,imbsz=bs,NULL);
}
/* attach buffer */
image->data = imb;
/* copy image, 4bpp */
if (d == 4) {
unsigned char *map;
int rf2;
rf2 = 2*rf;
for (j=0, v=Y0; (v<RCY) && (j<DH); ++j,v+=rf) {
map = (unsigned char *) ((image->data) + (j*bpl));
for (i=0, u=X0; (u<RCX) && (i<bpl); ++i,u+=rf) {
map[i] = cmap[r[u+v*RCX]];
}
}
}
/* copy image, 4 bpp */
else if (d == 8) {
unsigned char *map;
for (j=0, v=Y0; (v<RCY) && (j<DH); ++j,v+=rf) {
map = (unsigned char *) ((image->data) + (j*bpl));
for (i=0, u=X0; (u<RCX) && (i<DW); ++i,u+=rf) {
map[i] = cmap[r[u+v*RCX]];
}
}
}
/* copy image, 15 or 16 bpp */
else if ((15<=d) && (d<=16)) {
unsigned short *map;
for (j=0, v=Y0; (v<RCY) && (j<DH); ++j,v+=rf) {
map = (unsigned short *) ((image->data) + (j*bpl));
for (i=0, u=X0; (u<RCX) && (i<DW); ++i,u+=rf) {
map[i] = cmap[r[u+v*RCX]];
}
}
}
/* copy image, 24 bpp */
else if (d == 24) {
unsigned char *map;
unsigned long c;
for (j=0, v=Y0; (v<RCY) && (j<DH); ++j,v+=rf) {
map = (unsigned char *) ((image->data) + (j*bpl));
for (i=0, u=X0; (u<RCX) && (i<DW); ++i,u+=rf) {
c = cmap[r[u+v*RCX]];
*(map++) = c & 255;
*(map++) = (c >>= 8) & 255;
*(map++) = (c >> 8) & 255;
/* sparse 24bpp (GL) */
if (pad == 32)
++map;
}
}
}
/* copy image, 32 bpp */
else {
unsigned *map;
for (j=0, v=Y0; (v<RCY) && (j<DH); ++j,v+=rf) {
map = (unsigned *) ((image->data) + (j*bpl));
for (i=0, u=X0; (u<RCX) && (i<DW); ++i,u+=rf) {
map[i] = cmap[r[u+v*RCX]];
}
}
}
/* copy to X display */
XPutImage(xd,xw,xgc,image,0,0,DM,DT,DW,DH);
}
}
/*
Draw a 3-d frame around (outside) the rectangle with top left corner
(x,y), width w and height h. The frame if formed by an innermost
vdgray rectangle and an outermost white rectangle. Both have a
1-pixel width.
*/
void draw_dframe(int x,int y,int w,int h)
{
XSetForeground(xd,xgc,vdgray.pixel);
draw_frame(x,y,w,h,1);
XSetForeground(xd,xgc,white.pixel);
draw_frame(x-1,y-1,w+2,h+2,1);
}
/*
Draw frames around the window. windows have three frames. The innermost
is drawn with the window background color, so it's in fact a 1-pixel
separation from the window contents and the frames. The other two
frames are the Clara GUI standard frames (vdgray and white).
*/
void draw_dw_frames(void)
{
if (CDW == PAGE)
XSetForeground(xd,xgc,white.pixel);
else
XSetForeground(xd,xgc,gray.pixel);
draw_frame(DM,DT,DW,DH,1);
XSetForeground(xd,xgc,vdgray.pixel);
draw_frame(DM-1,DT-1,DW+2,DH+2,1);
XSetForeground(xd,xgc,white.pixel);
draw_frame(DM-2,DT-2,DW+4,DH+4,1);
}
/*
Draw stepper.
*/
void draw_stepper(int x,int y,int o,int RW)
{
int m = RW/2;
XPoint t[3];
/* draw stepper pointing to left (9 o'clock) */
if (o == 9) {
/*
1. draw stepper foreground ('+')
+
+++
+++++
+++
+
*/
XSetForeground(xd,xgc,gray.pixel);
t[0].x = x;
t[0].y = y;
t[1].x = x+RW-2;
t[1].y = y-m;
t[2].x = x+RW-2;
t[2].y = y+m;
XFillPolygon(xd,xw,xgc,t,3,Convex,CoordModeOrigin);
/*
2. draw white decoration (light reflecting, '.')
.
..+
..+++
..+++++
+++
+
*/
XSetForeground(xd,xgc,white.pixel);
XDrawLine(xd,xw,xgc,x,y,x+RW-2,y-m);
XDrawLine(xd,xw,xgc,x+1,y,x+RW-2,y-m+1);
/*
3. draw dark decoration (inferior shadow, '*')
.
..+
..+++
**+++++
**+++
**+
*
*/
XSetForeground(xd,xgc,vdgray.pixel);
XDrawLine(xd,xw,xgc,x,y,x+RW-2,y+m);
XDrawLine(xd,xw,xgc,x+1,y,x+RW-2,y+m-1);
/*
3. draw dark decoration (vertical shadow, '*')
.**
..+**
..+++**
**+++++**
**+++**
**+**
***
*/
XDrawLine(xd,xw,xgc,x+RW-2,y-m,x+RW-2,y+m);
XDrawLine(xd,xw,xgc,x+RW-3,y-m+1,x+RW-3,y+m-1);
}
/* draw stepper pointing to right (3 o'clock), see comments above */
else if (o == 3) {
XSetForeground(xd,xgc,gray.pixel);
t[0].x = x;
t[0].y = y;
t[1].x = x-RW+2;
t[1].y = y-m;
t[2].x = x-RW+2;
t[2].y = y+m;
XFillPolygon(xd,xw,xgc,t,3,Convex,CoordModeOrigin);
XSetForeground(xd,xgc,white.pixel);
XDrawLine(xd,xw,xgc,x,y,x-RW+2,y-m);
XDrawLine(xd,xw,xgc,x-1,y,x-RW+2,y-m+1);
XSetForeground(xd,xgc,vdgray.pixel);
XDrawLine(xd,xw,xgc,x,y,x-RW+2,y+m);
XDrawLine(xd,xw,xgc,x-1,y,x-RW+2,y+m-1);
XDrawLine(xd,xw,xgc,x-RW+2,y-m,x-RW+2,y+m);
XDrawLine(xd,xw,xgc,x-RW+3,y-m+1,x-RW+3,y+m-1);
}
/* draw stepper pointing to top (12 o'clock), see comments above */
else if (o == 12) {
XSetForeground(xd,xgc,gray.pixel);
t[0].x = x;
t[0].y = y;
t[1].x = x-m;
t[1].y = y+RW-2;
t[2].x = x+m;
t[2].y = y+RW-2;
XFillPolygon(xd,xw,xgc,t,3,Convex,CoordModeOrigin);
XSetForeground(xd,xgc,white.pixel);
XDrawLine(xd,xw,xgc,x,y,x-m,y+RW-2);
XDrawLine(xd,xw,xgc,x,y+1,x-m+1,y+RW-2);
XSetForeground(xd,xgc,vdgray.pixel);
XDrawLine(xd,xw,xgc,x,y,x+m,y+RW-2);
XDrawLine(xd,xw,xgc,x,y+1,x+m-1,y+RW-2);
XDrawLine(xd,xw,xgc,x-m,y+RW-2,x+m,y+RW-2);
XDrawLine(xd,xw,xgc,x-m+1,y+RW-3,x+m-1,y+RW-3);
}
/* draw stepper pointing to bottom (6 o'clock), see comments above */
else if (o == 6) {
XSetForeground(xd,xgc,gray.pixel);
t[0].x = x;
t[0].y = y;
t[1].x = x-m;
t[1].y = y-RW+2;
t[2].x = x+m;
t[2].y = y-RW+2;
XFillPolygon(xd,xw,xgc,t,3,Convex,CoordModeOrigin);
XSetForeground(xd,xgc,white.pixel);
XDrawLine(xd,xw,xgc,x,y,x-m,y-RW+2);
XDrawLine(xd,xw,xgc,x,y-1,x-m+1,y-RW+2);
XSetForeground(xd,xgc,vdgray.pixel);
XDrawLine(xd,xw,xgc,x,y,x+m,y-RW+2);
XDrawLine(xd,xw,xgc,x,y-1,x+m-1,y-RW+2);
XDrawLine(xd,xw,xgc,x-m,y-RW+2,x+m,y-RW+2);
XDrawLine(xd,xw,xgc,x-m+1,y-RW+3,x+m-1,y-RW+3);
}
}
/*
Draw horizontal and vertical scrollbars.
*/
void draw_sbars(void)
{
int a,b;
if (*cm_v_hide == 'X')
return;
/*
horizontal scrollbar
--------------------
We'll discuss in detail only the drawing of the horizontal
scrollbar. The vertical is analogous.
What's the length (b) of the cursor to display on the
horizontal scrollbar? Well, the horizontal scrollbar length
is DW-2*RW. The document horizontal resolution is GRX, and the
portion being displayed on the window has horizontal resolution
HR. So the following relation applies:
DW-2*RW relates to GRX
as b relates to HR
Then
HR * (DW-2*RW)
b = --------------
GRX
Let's now compute the position (a) on the horizontal scrollbar
where the cursor will be displayed. The following relation
applies:
DW-2*RW relates to GRX
as a relates to X0
Then
X0 * (DW-2*RW)
a = --------------
GRX
In some cases however the document width GRX may be smaller
than DW. That happens when the window is resized but the
document (when it's from a ML source) is not regenerated.
So this special case must be handled separately.
*/
if (GRX > DW) {
/* cursor leftmost coordinate (a) and length (b) */
a = DM+RW+X0*(DW-2*RW)/GRX;
b = HR*(DW-2*RW)/GRX;
}
else {
/* cursor leftmost coordinate (a) and length (b) */
a = DM+RW;
b = DW-2*RW;
}
if (b < 5)
b = 5;
/*
Paint scrollbar background avoiding overwrite the
cursor position to not generate flicks.
*/
XSetForeground(xd,xgc,darkgray.pixel);
XFillRectangle(xd,xw,xgc,DM-1,DT+DH+9,a-DM+1,RW+2);
XFillRectangle(xd,xw,xgc,a+b,DT+DH+9,DM-1+DW+2-a-b,RW+2);
XFillRectangle(xd,xw,xgc,a,DT+DH+9,b,1);
XFillRectangle(xd,xw,xgc,a,DT+DH+10+RW,b,1);
/* draw the cursor */
draw_button(a,DT+DH+10,b,RW,0,NULL);
/* scrollbar frames */
XSetForeground(xd,xgc,vdgray.pixel);
draw_frame(DM-1,DT+DH+9,DW+2,RW+2,1);
XSetForeground(xd,xgc,white.pixel);
draw_frame(DM-2,DT+DH+8,DW+4,RW+4,1);
/* vertical scrollbar */
if (GRY > DH) {
/* cursor topmost (a) coordinate and length (b) */
a = DT+RW+Y0*(DH-2*RW)/GRY;
b = VR*(DH-2*RW)/GRY;
}
else {
/* cursor topmost (a) coordinate and length (b) */
a = DT+RW;
b = DH-2*RW;
}
if (b < 5)
b = 5;
/*
Paint scrollbar background avoiding overwrite the
cursor position to not generate flicks.
*/
XSetForeground(xd,xgc,darkgray.pixel);
XFillRectangle(xd,xw,xgc,DM+DW+9,DT-1,RW+2,a-DT+1);
XFillRectangle(xd,xw,xgc,DM+DW+9,a+b,RW+2,DT-1+DH+2-a-b);
XFillRectangle(xd,xw,xgc,DM+DW+9,a,1,b);
XFillRectangle(xd,xw,xgc,DM+DW+10+RW,a,1,b);
/* draw the cursor */
draw_button(DM+DW+10,a,RW,b,0,NULL);
/* scrollbar frames */
XSetForeground(xd,xgc,vdgray.pixel);
draw_frame(DM+DW+9,DT-1,RW+2,DH+2,1);
XSetForeground(xd,xgc,white.pixel);
draw_frame(DM+DW+8,DT-2,RW+4,DH+4,1);
/* draw steppers */
draw_stepper(DM,DT+DH+10+RW/2,9,RW);
draw_stepper(DM+DW,DT+DH+10+RW/2,3,RW);
draw_stepper(DM+DW+10+RW/2,DT,12,RW);
draw_stepper(DM+DW+10+RW/2,DT+DH,6,RW);
}
/*
Draw menubar.
*/
void draw_mbar(void)
{
int i,n,p;
XTextItem xti;
/* clear menu bar area */
XSetForeground(xd,xgc,gray.pixel);
XFillRectangle(xd,xw,xgc,0,0,WW,MH);
/* draw menu bar */
XSetForeground(xd,xgc,black.pixel);
for (i=n=0, p=1; i<=TOP_CM; ++i) {
if (CM[i].a == 1) {
/* prepare */
xti.delta = 0;
xti.font = dfont->fid;
xti.chars = CM[i].tt;
xti.nchars = strlen(xti.chars);
/* title background */
if (CM+i == cmenu) {
XSetForeground(xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,(p-1)*DFW+2,3,
(xti.nchars+2)*DFW-4,MH-6);
XSetForeground(xd,xgc,black.pixel);
}
/* menu title and absciss */
if (xti.nchars > 0)
XDrawText(xd,xw,xgc,p*DFW,DFH+2,&xti,1);
CM[i].p = p;
p += strlen(xti.chars)+2;
++n;
}
}
/* menu bar frames */
XSetForeground(xd,xgc,vdgray.pixel);
draw_frame(2,2,WW-4,MH-4,1);
XSetForeground(xd,xgc,white.pixel);
draw_frame(1,1,WW-2,MH-2,1);
}
/*
Compute menu width and height.
BUG: We reserve 5 pixels on the
top and on the bottom of the menu for a white frame,
however the routines that display the labels put
the bottommost label too near the menu bottom. As
we could not discover why that happens, we've just
added 10 pixels instead of 5 when computing CH.
*/
void comp_menu_size(cmdesc *c)
{
int tw,i;
CW = 2*DFW;
for (i=0; i<c->n; ++i) {
tw = 5 + strlen(c->l+i*(MAX_MT+1))*DFW + 5;
if (tw > CW)
CW = tw;
}
CH = 5 + DFH + (c->n-1)*(DFH+VS) + 10;
}
/*
Set mclip to f.
When a menu is dismissed, the GUI must redraw the area occupied
by it. So menus are clipped in order to achieve better visual
results. This is especially useful when using unbuffered X I/O.
The clipping works as follows: by default the clipping is
inactive. Each event handler may activate it in order to the
function redraw do not touch the areas outside the menu
rectangle. The function redraw destroys the menu clipping (if
any) on finish.
*/
void set_mclip(int f)
{
static XRectangle mrec;
/* save menu placement on mrec and set mclip */
if (f == 2) {
mrec.x = CX0-2;
mrec.y = CY0-2;
mrec.width = CW+4;
mrec.height = CH+4;
mclip = 1;
if (cmenu->a == 1) {
mrec.y -= MH;
mrec.height += MH;
}
XSetClipRectangles(xd,xgc,0,0,&mrec,1,Unsorted);
}
/* just set mclip using mrec */
else if (f) {
mclip = 1;
XSetClipRectangles(xd,xgc,0,0,&mrec,1,Unsorted);
}
/* unset mclip */
else {
XRectangle r;
r.x = 0;
r.y = 0;
r.width = WW;
r.height = WH;
XSetClipRectangles(xd,xgc,0,0,&r,1,Unsorted);
mclip = 0;
}
}
/*
Draw current menu.
*/
void draw_menu(void)
{
int i;
XTextItem xti;
static int ft=1,s=-1;
/* compute the placement of the menu */
if (CY0_orig < 0) {
comp_menu_size(cmenu);
/* save original coordinates */
CX0_orig = CX0;
CY0_orig = CY0;
/* menu placement for menu bar menus */
if (cmenu->a == 1) {
CX0 = (cmenu->p-1)*DFW;
CY0 = MH;
}
/* menu placement for select boxes */
else if ((cmenu->a == 2) && (0 <= curr_hti) && (curr_hti <= topge)) {
CX0 = DM + ge[curr_hti].l - X0 +
(ge[curr_hti].r - ge[curr_hti].l - CW)/2;
CY0 = DT + ge[curr_hti].b - Y0;
}
/* try to make the menu fit on the application window */
if (CX0 + CW + 5 > WW)
CX0 = WW - CW - 5;
if (CX0 < 5)
CX0 = 5;
if (CY0 + CH + 5 > WH - 10 - DFH)
CY0 = WH - 10 - DFH - CH - 5;
if (CY0 < 5)
CY0 = 5;
/* save menu placement and set mclip */
set_mclip(2);
set_mclip(0);
/* no selected item by now */
cmenu->c = -1;
/* first-time flag and previously selected item */
ft = 1;
s = -1;
}
/*
Redraw entirely. This is a provision for expose events.
When the application get focus, the menu must be entirely
redraw.
*/
else if (redraw_menu == 2) {
ft = 1;
}
if (ft) {
/* menu frame */
XSetForeground(xd,xgc,vdgray.pixel);
draw_frame(CX0,CY0,CW,CH,1);
XSetForeground(xd,xgc,white.pixel);
draw_frame(CX0-1,CY0-1,CW+2,CH+2,1);
/* menu background */
XFillRectangle(xd,xw,xgc,CX0,CY0,CW,CH);
}
/* clear previously selected item */
if ((0 <= s) && (s < cmenu->n)) {
XSetForeground(xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,CX0+5,
CY0+5+s*(DFH+VS)+DFH-(dfont->max_bounds).ascent,
CW-10,DFH-1);
}
/* emphasize selected item */
if ((0 <= cmenu->c) && (cmenu->c < cmenu->n)) {
XSetForeground(xd,xgc,gray.pixel);
XFillRectangle(xd,xw,xgc,CX0+5,
CY0+5+cmenu->c*(DFH+VS)+DFH-(dfont->max_bounds).ascent,
CW-10,DFH-1);
}
/* labels and rules */
XSetForeground(xd,xgc,black.pixel);
for (i=0; i<cmenu->n; ++i) {
int y;
xti.delta = 0;
xti.font = dfont->fid;
xti.chars = cmenu->l+i*(MAX_MT+1);
xti.nchars = strlen(xti.chars);
y = CY0+5+i*(DFH+VS);
/*
Draw only nonempty labels. Draw all labels if first time,
otherwise draw only the just selected/unselected item.
*/
if ((xti.nchars > 0) && (ft || (i==s) || (i==cmenu->c))) {
/*
Change color if hidden
*/
if (cma(i+((cmenu-CM) << CM_IT_WD),1) == 0) {
if (i==cmenu->c)
XSetForeground(xd,xgc,white.pixel);
else
XSetForeground(xd,xgc,gray.pixel);
}
else
XSetForeground(xd,xgc,black.pixel);
XDrawText(xd,xw,xgc,CX0+5,y+DFH,&xti,1);
}
/* new group starting: draw separator (horizontal line) */
if ((cmenu->t)[i] & CM_NG) {
XSetLineAttributes(xd,xgc,1,LineSolid,CapButt,JoinRound);
XSetForeground(xd,xgc,darkgray.pixel);
XDrawLine(xd,xw,xgc,CX0,y+1,CX0+CW,y+1);
XSetForeground(xd,xgc,black.pixel);
}
}
ft = 0;
s = cmenu->c;
}
/*
Draw only one cfont pixel.
*/
void draw_cfont_at(int i,int j,int d)
{
int p;
if ((i<0) || (HR<=i) || (j<0) || (VR<=j)) {
return;
}
p = cfont[i+j*HR]&C_MASK;
if (d) {
if ((p == GRAY) || (p == WHITE))
p = BLACK;
else
p = WHITE;
}
/* white pixels */
if (p == WHITE) {
XSetForeground (xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,DM+i*GS+GW,DT+j*GS+GW,GS-GW,GS-GW);
}
/* gray pixels */
else if (p == GRAY) {
XSetForeground (xd,xgc,gray.pixel);
XFillRectangle(xd,xw,xgc,DM+i*GS+GW,DT+j*GS+GW,GS-GW,GS-GW);
}
/* black pixels */
else if (p == BLACK) {
XSetForeground (xd,xgc,black.pixel);
XFillRectangle(xd,xw,xgc,DM+i*GS+GW,DT+j*GS+GW,GS-GW,GS-GW);
}
/* distinguished pixels */
if (((cfont[i+j*HR])&D_MASK) == D_MASK) {
if (p == WHITE)
XSetForeground (xd,xgc,black.pixel);
else if (p == BLACK)
XSetForeground (xd,xgc,white.pixel);
else if (p == GRAY)
XSetForeground (xd,xgc,black.pixel);
XDrawLine(xd,xw,xgc,DM+i*GS+1,DT+j*GS+1,DM+(i+1)*GS,DT+(j+1)*GS);
XDrawLine(xd,xw,xgc,DM+(i+1)*GS,DT+j*GS,DM+i*GS+1,DT+(j+1)*GS-1);
}
}
/*
Draw the flea.
*/
void draw_flea(int i,int j,int d) {
int t;
/* arms */
for (t=1; t<=2; ++t) {
if (i >= t) {
if (j >= t)
draw_cfont_at(i-t,j-t,d);
if ((j+t) < VR)
draw_cfont_at(i-t,j+t,d);
}
if ((i+t) < HR) {
if (j > 0)
draw_cfont_at(i+t,j-t,d);
if ((j+1) < VR)
draw_cfont_at(i+t,j+t,d);
}
}
/* center */
draw_cfont_at(i,j,d);
}
/*
Draw cfont and the grid in fat bit mode.
*/
void draw_cfont(char *cfont)
{
int p,i,j;
if (redraw_grid != 0) {
XSetLineAttributes(xd,xgc,1,LineSolid,CapNotLast,JoinRound);
XSetForeground (xd,xgc,gray.pixel);
/* grid vertical lines */
for (i=0; i<=HR; ++i)
XDrawLine(xd,xw,xgc,DM+i*GS,DT,DM+i*GS,DT+DH);
/* grid horizontal lines */
for (j=0; j<=VR; ++j)
XDrawLine(xd,xw,xgc,DM,DT+j*GS,DM+DW,DT+j*GS);
redraw_grid = 0;
}
/* white pixels */
XSetForeground (xd,xgc,white.pixel);
for (i=0; i<HR; ++i) {
for (j=0; j<VR; ++j)
if ((cfont[i+j*HR]&C_MASK) == WHITE)
XFillRectangle(xd,xw,xgc,DM+i*GS+GW,DT+j*GS+GW,GS-GW,GS-GW);
}
/* gray pixels */
XSetForeground (xd,xgc,gray.pixel);
for (i=0; i<HR; ++i) {
for (j=0; j<VR; ++j)
if ((cfont[i+j*HR]&C_MASK) == GRAY)
XFillRectangle(xd,xw,xgc,DM+i*GS+GW,DT+j*GS+GW,GS-GW,GS-GW);
}
/* black pixels */
XSetForeground (xd,xgc,black.pixel);
for (i=0; i<HR; ++i) {
for (j=0; j<VR; ++j)
if ((cfont[i+j*HR]&C_MASK) == BLACK)
XFillRectangle(xd,xw,xgc,DM+i*GS+GW,DT+j*GS+GW,GS-GW,GS-GW);
}
/* distinguished pixels */
for (i=0; i<HR; ++i) {
for (j=0; j<VR; ++j) {
if (((p=cfont[i+j*HR])&D_MASK) == D_MASK) {
if ((p&C_MASK) == WHITE)
XSetForeground (xd,xgc,black.pixel);
else if ((p&C_MASK) == BLACK)
XSetForeground (xd,xgc,white.pixel);
else if ((p&C_MASK) == GRAY)
XSetForeground (xd,xgc,black.pixel);
XDrawLine(xd,xw,xgc,DM+i*GS+1,DT+j*GS+1,DM+(i+1)*GS,DT+(j+1)*GS);
XDrawLine(xd,xw,xgc,DM+(i+1)*GS,DT+j*GS,DM+i*GS+1,DT+(j+1)*GS-1);
}
}
}
/* frames */
XSetForeground(xd,xgc,vdgray.pixel);
draw_frame(DM+GW,DT+GW,GS*HR,GS*VR,1);
XSetForeground(xd,xgc,white.pixel);
draw_frame(DM+GW-1,DT+GW-1,GS*HR+2,GS*VR+2,1);
}
/*
Draw the bitmap bm reduced rf times. The reduction is performed
sampling one pixel on each rf*rf tile.
bm .. a raw bitmap.
bpl .. the bytes per line rate of the bitmap.
rh .. number of lines of the bitmap (or 0).
x .. x coordinate to sample on each rf*rf tile.
y .. y coordinate to sample on each rf*rf tile.
w .. width (in pixels) of the image to produce.
h .. height (in pixels) of the image to produce.
dx0 .. window-relative x display origin.
dy0 .. window-relative y display origin.
rf .. reduction factor.
The caller is allowed to inform w or h larger than the reduced
bitmap. The initial clipping code will detect (and correct) a too
large w or h.
*/
void draw_rb(unsigned char *bm,int bpl,int rh,int x,int y,int w,int h,int dx0,int dy0,int rf)
{
int a,k,i,j,np,c;
unsigned char *p,m;
XPoint xp[100];
/*
Clip horizontally. For each i (inner loop), we need
(c/8 < bpl). Then we have (x + i*rf)/8 < bpl, or
i < (8*bpl-x)/rf. So (8*bpl-x)/rf cannot be larger
than w.
*/
a = (8*bpl-x);
a = (a/rf) - ((a%rf) == 0);
if (a < w)
w = a;
/*
Clip vertically. For each j (outer loop), we need
(y+j*rf)<rh. Then we have j < (rh-y)/rf. So
(rh-y)/rf cannot be larger than h. The caller is
allowed to inform rh==0 when vertical clipping is
not needed.
*/
if (rh > 0) {
a = (rh-y);
a = (a/rf) - ((a%rf) == 0);
if (a < h)
h = a;
}
for (np=0, j=0; j<=h; ++j) {
k = (y+j*rf) * bpl;
for (i=0, c=x; (i<=w); ++i, c+=rf) {
p = bm + k + c/8;
m = ((unsigned) 1) << (7 - (c%8));
if ((*p & m) == m) {
if (np >= 100) {
XDrawPoints(xd,xw,xgc,xp,np,CoordModeOrigin);
np = 0;
}
xp[np].x = dx0+i;
xp[np].y = dy0+j;
++np;
}
}
}
if (np > 0) {
XDrawPoints(xd,xw,xgc,xp,np,CoordModeOrigin);
}
}
/*
Draw closure b. The closure is drawn only if it intersects the
display rectangle with top left (PDM,PDT), width HR2*RF and
height VR2*RF. Generally we'll have PDM==DM, PDT==DT, PHR*RF==DW
and PVR*RF==DH. However, when redrawing only part of the PAGE
window, a caller may change these variables (and restore them
afterwards) to make the redraw area smaller and the operation
faster.
*/
void draw_closure(cldesc *b,int a)
{
int dx0,dy0,x0,y0;
int bpl,x,y,w,h;
/* Clip the visible portion of the closure on x */
x0 = X0 + (PDM-DM) * RF;
x = (b->l < x0) ? x0 : b->l;
if ((x%RF) > 0)
x += RF - (x%RF);
if ((w=x0+PHR) > b->r)
w = b->r;
if ((w-=x) >= RF)
w /= RF;
else if (!draw_thin)
return;
dx0 = PDM + (x-X0)/RF;
/* Clip the visible portion of the closure on y */
y0 = Y0 + (PDT-DT) * RF;
y = (b->t < y0) ? y0 : b->t;
if ((y % RF) > 0)
y += RF - (y%RF);
if ((h=y0+PVR) > b->b)
h = b->b;
if ((h-=y) >= RF)
h /= RF;
else if (!draw_thin)
return;
dy0 = PDT + (y-y0)/RF;
/*
At this point we have:
(x,y) .. the (page) coordinates of the top left
pixel of the closure that will be
sampled when drawing the closure.
w .. the number of horizontal sampled lines less 1.
h .. the number of vertical sampled lines less 1.
(dx0,dy0) .. the display coordinates where the
closure pixel with page coordinates
(x,y) will be drawn.
*/
/* draw the bitmap */
if (a <= 0) {
bpl = ((b->r-b->l+1)/8 + (((b->r-b->l+1)%8)!=0));
if (a == 0)
XSetForeground(xd,xgc,black.pixel);
else if (a == -1)
XSetForeground(xd,xgc,white.pixel);
else
XSetForeground(xd,xgc,darkgray.pixel);
draw_rb(b->bm,bpl,0,x-b->l,y-b->t,w,h,dx0,dy0,RF);
}
}
/*
Draw symbol b.
The parameter a is used as follows:
value action
---------------------------------------------
0 draw closure using black foreground
1 draw ellipse using darkgray foreground
2 draw ellipse using black foreground
-1 draw closure using white foreground
-2 draw closure using darkgray foreground
-3,-4 clear the box
-4 ignore cm_o_bb flag and use black foreground
*/
void draw_symbol(sdesc *s,int a)
{
int dx0,dy0,x0,y0;
int x,y,w,h;
/* draw the bounding box */
if (*cm_d_bb != ' ') {
int l,r,t,b;
l = DM+(s->l-X0)/RF;
r = DM+(s->r-X0)/RF;
t = DT+(s->t-Y0)/RF;
b = DT+(s->b-Y0)/RF;
if ((a==-3) || (a==-4)) {
XSetForeground(xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,l,t,r-l+1,b-t+1);
}
if (a != -4) {
XSetForeground(xd,xgc,black.pixel);
XDrawRectangle(xd,xw,xgc,l,t,r-l,b-t);
}
}
/* draw the closures */
if ((*cm_d_bb == ' ') || (a == -4)) {
if (a==-4)
a = 0;
if (a <= 0) {
int i;
cldesc *c;
for (i=0; i<s->ncl; ++i) {
c = cl + (s->cl)[i];
draw_closure(c,a);
}
}
}
/* Clip the visible portion of the closure on x */
x0 = X0 + (PDM-DM) * RF;
x = (s->l < x0) ? x0 : s->l;
if ((x%RF) > 0)
x += RF - (x%RF);
if ((w=x0+PHR) > s->r)
w = s->r;
if ((w-=x) >= RF)
w /= RF;
else if (!draw_thin)
return;
dx0 = PDM + (x-X0)/RF;
/* Clip the visible portion of the closure on y */
y0 = Y0 + (PDT-DT) * RF;
y = (s->t < y0) ? y0 : s->t;
if ((y % RF) > 0)
y += RF - (y%RF);
if ((h=y0+PVR) > s->b)
h = s->b;
if ((h-=y) >= RF)
h /= RF;
else if (!draw_thin)
return;
dy0 = PDT + (y-y0)/RF;
/*
At this point we have:
(x,y) .. the (page) coordinates of the top left
pixel of the closure that will be
sampled when drawing the closure.
w .. the number of horizontal sampled lines less 1.
h .. the number of vertical sampled lines less 1.
(dx0,dy0) .. the display coordinates where the
closure pixel with page coordinates
(x,y) will be drawn.
*/
/*
Draw an ellipse around the symbol. The ellipse is
centered at (dx0+w/2,dy0+h/2).
*/
/*
if (a > 0) {
int dx,dy,a1,a2;
dx = w/4 + 3;
dy = h/4 + 3;
if (a==1)
XSetForeground(xd,xgc,darkgray.pixel);
else
XSetForeground(xd,xgc,black.pixel);
XFillArc(xd,xw,xgc,dx0-dx,dy0-dy,w+2*dx,h+2*dy,0,23040);
}
*/
/*
Draw an ellipse around the closure.
*/
if (a > 0) {
int dx,dy;
w = (s->r - s->l + 1) / RF;
h = (s->b - s->t + 1) / RF;
dx = w/4 + 3;
dy = h/4 + 3;
if (a==1)
XSetForeground(xd,xgc,darkgray.pixel);
else
XSetForeground(xd,xgc,black.pixel);
XFillArc(xd,xw,xgc,
DM+(s->l-X0)/RF-dx/2,DT+(s->t-Y0)/RF-dy/2,
w+dx,h+dy,0,23040);
}
}
/*
Draw input field.
*/
void draw_inp(edesc *c)
{
char d[MFTL+2];
int x,y,dx,dy;
XTextItem xti;
/* input field corner, width and height */
x = DM + c->l - X0 + 2;
y = DT + c->t - Y0 + 2;
dx = c->r-c->l+1 - 4;
dy = c->b-c->t+1 - VS - 4;
/* paint input field background */
XSetForeground (xd,xgc,darkgray.pixel);
XFillRectangle(xd,xw,xgc,x,y,dx,dy);
/* display text and cursor */
XSetForeground (xd,xgc,black.pixel);
xti.delta = 0;
if (c->txt != NULL)
strcpy(d,c->txt);
else
d[0] = 0;
if (c-ge == curr_hti)
strcat(d,"_");
xti.font = dfont->fid;
xti.chars = d;
if ((xti.nchars = strlen(d)) > 0)
XDrawText(xd,xw,xgc,x,y+(dfont->max_bounds).ascent,&xti,1);
/* display frames around it */
XSetForeground(xd,xgc,white.pixel);
draw_frame(x,y,dx,dy,1);
XSetForeground(xd,xgc,vdgray.pixel);
draw_frame(x-1,y-1,dx+2,dy+2,1);
/* oh! ugly! */
if (use_xb) {
XCopyArea(xd,xw,XW,xgc,x-1,y-1,dx+2,dy+2,x-1,y-1);
}
}
/*
Draw checkboxes, select boxes and submission buttons.
*/
void draw_cbox(edesc *c)
{
int x,y,dx,dy;
cmdesc *cm;
/* input field corner, width and height */
x = DM + c->l - X0 + 2;
y = DT + c->t - Y0 + 2;
dx = c->r-c->l+1 - 4;
dy = c->b-c->t+1 - 4;
/* select box */
if (c->type == GE_SELECT) {
cm = CM + c->iarg;
if ((0 <= cm->c) && (cm->c < cm->n))
draw_button(x,y,dx,dy,0,cm->l+cm->c*(MAX_MT+1));
else
draw_button(x,y,dx,dy,0,NULL);
}
/* submission button */
else if (c->type == GE_SUBMIT) {
draw_button(x,y,dx,dy,0,c->txt);
}
/* checkbox */
else {
draw_button(x,y,dx,dy,c->iarg,NULL);
}
}
/*
Draw Graphic element.
*/
void draw_ge(edesc *t)
{
XTextItem xti;
/* ignore elements outside the current window */
if ((intersize(t->l,t->r,X0,X0+DW-1,NULL,NULL) <= 0) ||
(intersize(t->t,t->b,Y0,Y0+DH-1,NULL,NULL) <= 0)) {
return;
}
/* draw text GC */
if (t->type == GE_TEXT) {
int y;
if (text_switch) {
int l;
l = topdv + strlen(t->txt) + 2;
if (l >= dvsz) {
dvsz = l + 4096;
dv = c_realloc(dv,dvsz,NULL);
}
if (topdv < 0)
dvfl = t - ge;
topdv += sprintf(dv+topdv+1,"%s\n",t->txt);
dv[topdv+1] = 0;
}
else {
/* draw text */
xti.delta = 0;
xti.font = dfont->fid;
xti.chars = t->txt;
xti.nchars = strlen(xti.chars);
XSetForeground(xd,xgc,black.pixel);
y = DT+t->t+(dfont->max_bounds).ascent-Y0;
XDrawText(xd,xw,xgc,DM+t->l-X0,y,&xti,1);
/* underline anchored texts */
if ((t->a) && ULINK) {
XSetLineAttributes(xd,xgc,1,LineSolid,CapNotLast,JoinRound);
XDrawLine(xd,xw,xgc,DM+t->l-X0,
y+1,
DM+t->l-X0+xti.nchars*DFW,
y+1);
}
}
}
/* draw special PBM image */
if (t->type == GE_IMG) {
/* draw web clip */
if (strcmp(t->txt,"internal") == 0)
draw_bm(rw,RWX,RWY,DM+t->l-X0+2,DT+t->t-Y0+2);
/* draw pattern */
else if (strncmp(t->txt,"pattern/",8) == 0) {
int i,id;
id = atoi(t->txt+8);
i = id2idx(id);
if ((0 <= i) && (i <= topp)) {
XSetForeground(xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,DM+t->l-X0+2,DT+t->t-Y0+2,
FS/plist_rf,FS/plist_rf);
XSetForeground(xd,xgc,black.pixel);
draw_rb(pattern[i].b,FS/8,0,0,0,FS/plist_rf-1,FS/plist_rf-1,
DM+t->l-X0+2,DT+t->t-Y0+2,plist_rf);
}
}
/* draw symbol */
else if (strncmp(t->txt,"symbol/",7) == 0) {
int id,j;
cldesc *c;
id = atoi(t->txt+7);
if ((0 <= id) && (id <= tops)) {
XSetForeground(xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,DM+t->l-X0+2,DT+t->t-Y0+2,
FS/plist_rf,FS/plist_rf);
XSetForeground(xd,xgc,black.pixel);
for (j=0; j<mc[id].ncl; ++j) {
c = cl + mc[id].cl[j];
draw_rb(c->bm,byteat(c->r-c->l+1),c->b-c->t+1,0,0,FS/plist_rf-1,
FS/plist_rf-1,DM+t->l-X0+2,DT+t->t-Y0+2,plist_rf);
}
}
}
/* draw aligned pattern */
else if (strncmp(t->txt,"apatt/",6) == 0) {
int i,id,w;
id = atoi(t->txt+6);
i = id2idx(id);
if ((0 <= i) && (i <= topp)) {
w = t->r - t->l + 1;
XSetForeground(xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,DM+t->l-X0+2,DT+t->t-Y0+2,w,FS/plist_rf);
XSetForeground(xd,xgc,black.pixel);
draw_rb(pattern[i].b,FS/8,0,0,0,w,FS/plist_rf-1,
DM+t->l-X0+2,DT+t->t-Y0+2,plist_rf);
}
}
/* draw word */
else if (strncmp(t->txt,"word/",5) == 0) {
int a,i,j;
wdesc *W;
cldesc *c;
int bpl;
i = atoi(t->txt+5);
#ifdef MEMCHECK
checkidx(i,topw+1,"draw word");
#endif
W = word + i;
XSetForeground(xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,DM+t->l-X0+2,DT+t->t-Y0+2,(W->r-W->l+1)/plist_rf,
(W->b-W->t+1)/plist_rf);
for (a=W->F; a>=0; a=mc[a].E) {
if (uncertain(mc[a].tc))
XSetForeground(xd,xgc,gray.pixel);
else
XSetForeground(xd,xgc,black.pixel);
for (j=0; j<mc[a].ncl; ++j) {
c = cl + (mc[a].cl)[j];
bpl = ((c->r-c->l+1)/8 + (((c->r-c->l+1)%8)!=0));
draw_rb(c->bm,bpl,0,0,0,(c->r-c->l+1)/plist_rf,
(c->b-c->t+1)/plist_rf,DM+t->l-X0+2+(c->l-W->l)/plist_rf,
DT+t->t-Y0+2+(c->t-W->t)/plist_rf,plist_rf);
}
}
}
}
/* draw input field */
else if (t->type == GE_INPUT) {
draw_inp(t);
}
/* draw checkbox */
else if ((t->type == GE_CBOX) || (t->type == GE_RADIO)) {
draw_cbox(t);
}
/* draw select */
else if (t->type == GE_SELECT) {
draw_cbox(t);
}
/* draw submission button */
else if (t->type == GE_SUBMIT) {
draw_cbox(t);
}
/* draw rectangle background */
else if (t->type == GE_RECT) {
int w,h;
if (t->bg >= 0) {
if (t->bg == WHITE)
XSetForeground(xd,xgc,white.pixel);
else if (t->bg == BLACK)
XSetForeground(xd,xgc,black.pixel);
else if (t->bg == GRAY)
XSetForeground(xd,xgc,gray.pixel);
else if (t->bg == DARKGRAY)
XSetForeground(xd,xgc,darkgray.pixel);
else if (t->bg == VDGRAY)
XSetForeground(xd,xgc,vdgray.pixel);
else
XSetForeground(xd,xgc,darkgray.pixel);
w = t->r - t->l;
h = t->b - t->t;
XFillRectangle(xd,xw,xgc,DM+t->l-X0,DT+t->t-Y0,w,h);
}
}
/* draw stepper */
else if (t->type == GE_STEPPER) {
int x,y;
x = DM + t->l - X0;
y = DT + t->t + (t->b-t->t)/2 - Y0;
if (t->iarg == 9)
draw_stepper(x,y,9,DFH-2);
else
draw_stepper(x+DFH-1,y,3,DFH-2);
}
}
/*
Clip or unclip the current window.
*/
void clip_dw(int a)
{
XRectangle r;
/* clip the window */
if (a) {
/* clip the current window */
r.x = DM-1;
r.y = DT-1;
r.width = DW+2;
r.height = DH+2;
XSetClipRectangles(xd,xgc,0,0,&r,1,Unsorted);
}
/* clip the entire X window */
else {
r.x = 0;
r.y = 0;
r.width = WW;
r.height = WH;
XSetClipRectangles(xd,xgc,0,0,&r,1,Unsorted);
}
}
/*
Compute the bounding box of the anchored text starting at
gc[a].
*/
void ge_bb(int a,int *l,int *r,int *t,int *b)
{
edesc *c;
c = ge + a;
*l = c->l;
*r = c->r;
*t = c->t;
*b = c->b;
for (++c; c->a==HTA_HREF_C; ++c) {
if (c->l < *l)
*l = c->l;
if (c->r > *r)
*r = c->r;
if (c->t < *t)
*t = c->t;
if (c->b > *b)
*b = c->b;
}
}
/*
Highlight graphic element.
BUG: perform synchronous X I/O.
*/
void hl_gc(edesc *c)
{
int l,r,t,b;
/* anchored text */
if ((c->type == GE_TEXT) &&
((c->a == HTA_HREF) || (c->a == HTA_HREF_C))) {
/* get first word in chain */
if (c->a == HTA_HREF_C)
c = ge + c->iarg;
/* message on status line */
show_hint(0,c->arg);
/* compute chain bounding box and clear it */
ge_bb(c->iarg,&l,&r,&t,&b);
clip_dw(1);
XSetForeground(xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,DM+l-X0,DT+t-Y0,r-l,b-t);
/* redraw chain */
do {
draw_ge(c);
} while ((++c)->a == HTA_HREF_C);
if (use_xb) {
XCopyArea(xd,xw,XW,xgc,DM+l-X0,DT+t-Y0,r-l,b-t,DM+l-X0,DT+t-Y0);
}
}
/* text input field */
else if (c->type == GE_INPUT) {
draw_inp(c);
show_hint(0,"Fill in and (optionally) press ENTER to consist");
}
/* submission button or anchored image */
else if ((c->type == GE_SUBMIT) ||
((c->type == GE_IMG) && (c->arg != NULL))) {
/* message on status line */
show_hint(0,c->arg);
}
/* document analisys (experimental) */
else if (CDW == DEBUG) {
/*
printf("now at line %d \"%s\"\n",c-ge-dvfl,c->txt);
*/
}
}
/*
Unhighlight graphic element.
BUG: perform synchronous X I/O.
*/
void unhl_gc(edesc *c)
{
int l,r,t,b;
/* anchored text */
if ((c->type == GE_TEXT) &&
((c->a == HTA_HREF) || (c->a == HTA_HREF_C))) {
/* get first word in chain */
if (c->a == HTA_HREF_C)
c = ge + c->iarg;
/* compute chain bounding box and clear it */
ge_bb(c->iarg,&l,&r,&t,&b);
clip_dw(1);
XSetForeground(xd,xgc,gray.pixel);
XFillRectangle(xd,xw,xgc,DM+l-X0,DT+t-Y0,r-l,b-t);
/* redraw chain */
do {
draw_ge(c);
} while ((++c)->a == HTA_HREF_C);
if (use_xb) {
XCopyArea(xd,xw,XW,xgc,DM+l-X0,DT+t-Y0,r-l,b-t,DM+l-X0,DT+t-Y0);
}
/* remove message on status line */
show_hint(0,"");
}
/* text input field */
else if (c->type == GE_INPUT) {
mb[0] = 0;
redraw_stline = 1;
draw_inp(c);
show_hint(0,"");
}
/* submission button */
else if (c->type == GE_SUBMIT) {
/* message on status line */
show_hint(0,"");
}
}
/*
Show info about the current HTML item or the current symbol on the
PAGE window.
This is the service responsible to "activate" a clickable element,
displayin a message about it, like "load/0" (see also hl_gc).
*/
void show_htinfo(int cx,int cy) {
edesc *c;
int i,k,f;
if (CDW==PAGE) {
c = NULL;
k = symbol_at(X0+(cx-DM)*RF,Y0+(cy-DT)*RF);
}
else {
/* search the item that contains (cx,cy) */
cx = cx - DM + X0;
cy = cy - DT + Y0;
/* TODO: something better than linear search.. */
for (k=-1, i=0; (k<0) && (i<=topge); ++i) {
c = ge + i;
if ((c->type != GE_RECT) &&
(c->l<=cx) && (cx<=c->r) && (c->t<=cy) && (cy<=c->b)) {
k = i;
}
}
}
/* for debugging */
/*
{
static int last_k=-1;
if (k != last_k) {
printf("current item is %d, type %d\n",k,ge[k].type);
last_k = k;
}
}
*/
/* flush flag */
f = 0;
/* unhilight last_k */
if ((curr_hti >= 0) && (k != curr_hti)) {
if (CDW==PAGE) {
draw_symbol(mc+curr_hti,-3);
}
else {
c = ge + curr_hti;
curr_hti = -1;
unhl_gc(c);
c = ge + k;
f = 1;
}
}
/* highlight k */
if ((k >= 0) && (k != curr_hti)) {
curr_hti = k;
if (CDW==PAGE) {
draw_symbol(mc+curr_hti,-4);
}
else {
c = ge + curr_hti;
hl_gc(c);
f = 1;
}
}
/* no current component */
if (k < 0)
curr_hti = -1;
/*
make sure the update is not buffered
BAD: synchronous I/O
*/
if (f) {
clip_dw(0);
XFlush(xd);
}
}
/*
Set the status of the buttons that carry symbol properties from the
pattern p properties (if s==0) or from the symbol p properties (if
s!=0).
*/
void set_buttons(int s,int p)
{
int i,a,f,t;
/*
Copy the symbol transliteration properties to a and f.
*/
if (s) {
sdesc *m;
/* just ignore invalid indexes */
if ((curr_mc<0) || (tops<curr_mc))
return;
m = mc + curr_mc;
/*
When untransliterated, copy the properties of the
surrounding word properties (TO BE DONE) or
use default values.
*/
if (m->tr == NULL) {
a = 0;
t = 0;
f = 0;
}
else {
a = (m->tr)->a;
f = (m->tr)->f;
t = (m->bm >= 0) ? pattern[m->bm].pt : 0;
}
}
/*
Copy the pattern transliteration properties to a and f.
*/
else {
pdesc *s;
/* just ignore invalid indexes */
if ((cdfc<0) || (topp<cdfc))
return;
s = pattern + cdfc;
a = s->a;
f = s->f;
t = s->pt;
}
/* set the alphabet */
for (i=0; (i<nalpha) && (inv_balpha[i] != a); ++i);
if (i >= nalpha)
i = 0;
button[balpha] = i;
/* set the pattern type */
button[btype] = t;
/* bad flag */
button[bbad] = ((f & F_BAD) != 0);
/* redraw all buttons */
redraw_button = -1;
}
/*
Set the status of the buttons that carry symbol properties from the
symbol s properties.
*/
void symb2buttons(int s)
{
trdesc *t;
int i;
/* read values from the preferred transliteration */
if ((t=mc[s].tr) != NULL) {
for (i=0; (i<nalpha) && (inv_balpha[i] != t->a); ++i);
if (i >= nalpha)
i = 0;
button[balpha] = i;
}
/* TODO: read default values from surrounding word */
else {
}
/* must redraw */
redraw_button = -1;
}
/*
Draw window.
*/
void draw_dw(int bg,int inp)
{
int i;
if (mclip == 0) {
clip_dw(1);
}
if (HTML) {
/* clear the window background */
if (bg) {
XSetForeground (xd,xgc,gray.pixel);
XFillRectangle(xd,xw,xgc,DM-1,DT-1,DW+2,DH+2);
}
for (i=0; i<=topge; ++i) {
if ((!inp) ||
(ge[i].type == GE_INPUT) ||
(ge[i].type == GE_CBOX) ||
(ge[i].type == GE_RADIO)) {
draw_ge(((edesc *)ge)+i);
}
}
}
/* draw the scanned page */
else if ((CDW == PAGE) && (cl_ready)) {
int i,id,p,f;
/*
Clip the window because the graphic cursor may
surpass the window.
*/
if (mclip == 0) {
XRectangle r;
r.x = DM;
r.y = DT;
r.width = DW;
r.height = DH;
XSetClipRectangles(xd,xgc,0,0,&r,1,Unsorted);
}
/* clear background */
if (bg) {
XSetForeground (xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,DM-1,DT-1,DW+2,DH+2);
}
/*
when sliding or drawing bounding boxes instead of symbols,
various visual features are switched off.
*/
f = ((!sliding) && (*cm_d_bb == ' '));
/* get the active class */
if ((curr_mc >= 0) && ((id = mc[curr_mc].bm) >= 0)) {
p = id2idx(id);
}
else {
id = -1;
p = -1;
}
/*
Draw gray ellipses to enclose the symbols with
unknown alignment.
*/
if ((f) && (*cm_g_align != ' ')) {
for (i=0; i<=topps; ++i) {
if (mc[ps[i]].va < 0)
draw_symbol(mc+ps[i],1);
}
}
/*
Draw gray ellipses to enclose the symbols of the
current class.
*/
else if ((f) && (*cm_v_cc != ' ') && (p>=0)) {
for (i=0; i<=topps; ++i) {
if (mc[ps[i]].bm == id)
draw_symbol(mc+ps[i],1);
}
}
/*
Draw a black ellipse to enclose the current symbol.
*/
if ((f) && (curr_mc >= 0)) {
draw_symbol(mc+curr_mc,2);
}
/*
Draw the symbols
*/
list_s(X0,Y0,HR,VR);
for (i=0; i<list_s_sz; ++i) {
int c;
c = list_s_r[i];
/* the character under the cursor */
if ((f) && (c == curr_mc)) {
/* darkgray if UNDEF */
if (uncertain(mc[c].tc))
draw_symbol(mc+c,-2);
/* white */
else
draw_symbol(mc+c,-1);
}
/*
Symbol with unknown alignment or on the
current class.
*/
else if ((f) &&
(((*cm_g_align != ' ') && (mc[c].va < 0)) ||
((p>=0) && (*cm_v_cc != ' ') && (mc[c].bm==id)))) {
/* white if UNDEF */
if (uncertain(mc[c].tc))
draw_symbol(mc+c,-1);
/* black otherwise */
else
draw_symbol(mc+c,0);
}
/* UNDEF symbols are darkgray */
else if ((f) && (uncertain(mc[c].tc)))
draw_symbol(mc+c,-2);
/* others are black */
else
draw_symbol(mc+c,0);
}
/*
Draw ellipses around words, if
requested.
*/
if ((sliding == 0) && (*cm_d_words != ' ')) {
int i;
XSetForeground(xd,xgc,black.pixel);
/* draw ellipses and arrows */
for (i=0; i<=topw; ++i) {
wdesc *a,*b;
int x,y;
if ((word[i].F >= 0) &&
((*cm_g_bo == ' ') || (word[i].f & F_BOLD)) &&
((*cm_g_io == ' ') || (word[i].f & F_ITALIC))) {
a = word + i;
/* draw ellipse */
if (word[i].F != word[i].L)
XDrawArc(xd,xw,xgc,DM+(a->l-X0)/RF,DT+(a->t-Y0)/RF,
(a->r-a->l)/RF,(a->b-a->t)/RF,0,23040);
else {
int dx,dy;
dx = (a->r - a->l) / 5;
dy = (a->b - a->t) / 5;
XDrawArc(xd,xw,xgc,DM+(a->l-X0-dx)/RF,
DT+(a->t-Y0-dy)/RF,(a->r-a->l+2*dx)/RF,
(a->b-a->t+2*dy)/RF,0,23040);
}
/* draw arrow to next word */
if (a->E >= 0) {
b = word + a->E;
x = (b->l - a->r);
y = (((a->b>b->b) ? a->b : b->b) -
((a->t<b->t) ? a->t : b->t));
XDrawArc(xd,xw,xgc,DM+(a->r-X0)/RF,DT+(a->t-Y0)/RF,
x/RF,y/RF,1920,7680);
}
}
}
}
/*
Draw ellipses around symbols,
if requested.
*/
else if ((sliding == 0) && (*cm_d_symbols != ' ')) {
int i;
XSetForeground(xd,xgc,black.pixel);
/* draw ellipses */
for (i=0; i<list_s_sz; ++i) {
sdesc *a;
a = mc + list_s_r[i];
if ((a->r < X0+HR) &&
(a->l >= X0) &&
(a->t < Y0+VR) &&
(a->b >= Y0))
{
/* draw ellipse */
if (mc[i].ncl > 1)
XDrawArc(xd,xw,xgc,DM+(a->l-X0)/RF,DT+(a->t-Y0)/RF,
(a->r-a->l)/RF,(a->b-a->t)/RF,0,23040);
else {
int dx,dy;
dx = (a->r - a->l) / 5;
dy = (a->b - a->t) / 5;
XDrawArc(xd,xw,xgc,DM+(a->l-X0-dx)/RF,
DT+(a->t-Y0-dy)/RF,(a->r-a->l+2*dx)/RF,
(a->b-a->t+2*dy)/RF,0,23040);
}
/* draw arrow to next symbol */
/*
if (a->E >= 0) {
b = mc + a->E;
x = (b->l - a->r);
y = (((a->b>b->b) ? a->b : b->b) -
((a->t<b->t) ? a->t : b->t));
XDrawArc(xd,xw,xgc,DM+(a->r-X0)/RF,DT+(a->t-Y0)/RF,
x/RF,y/RF,1920,7680);
}
*/
}
}
}
/*
Draw ellipses around closures,
if requested.
*/
else if ((sliding == 0) &&
(*cm_d_closures != ' ')) {
int i;
XSetForeground(xd,xgc,black.pixel);
/* draw ellipses */
for (i=0; i<=list_cl_sz; ++i) {
cldesc *a;
a = cl + list_cl_r[i];
if ((a->r < X0+HR) &&
(a->l >= X0) &&
(a->t < Y0+VR) &&
(a->b >= Y0))
{
/* draw ellipse */
if (mc[i].ncl > 1)
XDrawArc(xd,xw,xgc,DM+(a->l-X0)/RF,DT+(a->t-Y0)/RF,
(a->r-a->l)/RF,(a->b-a->t)/RF,0,23040);
else {
int dx,dy;
dx = (a->r - a->l) / 5;
dy = (a->b - a->t) / 5;
XDrawArc(xd,xw,xgc,DM+(a->l-X0-dx)/RF,
DT+(a->t-Y0-dy)/RF,(a->r-a->l+2*dx)/RF,
(a->b-a->t+2*dy)/RF,0,23040);
}
}
}
}
/*
Draw ellipses around lines (geometrical),
if requested.
*/
else if ((sliding == 0) && (*cm_g_glines != ' ')) {
int a,l,r,t,b,lm,rm;
if ((0 <= curr_mc) && (curr_mc <= tops)) {
/* search leftmost symbol */
for (lm=curr_mc; (a=lsymb(lm,1)) >= 0; lm=a);
/* search rightmost symbol */
for (rm=curr_mc; (a=rsymb(rm,1)) >= 0; rm=a);
l = mc[lm].l;
r = mc[rm].r;
t = mc[curr_mc].t;
b = mc[curr_mc].b;
XDrawArc(xd,xw,xgc,DM+(l-X0)/RF,DT+(t-Y0)/RF,
(r-l)/RF,(b-t)/RF,0,23040);
}
}
/* remove clipping */
if (mclip == 0) {
XRectangle r;
r.x = 0;
r.y = 0;
r.width = WW;
r.height = WH;
XSetClipRectangles(xd,xgc,0,0,&r,1,Unsorted);
}
redraw_zone = 1;
}
/* the scanned page (pixmap) */
else if ((CDW == PAGE) && (pixmap != NULL)) {
draw_pm(pixmap,XRES,YRES,DM,DT);
redraw_zone = 1;
}
/*
Set the buttons accordingly to the current symbol.
BUG: if the user change the status of the "italic"
button, then resize the window the status of the
"italic" button will change (really?).
*/
if (CDW == PATTERN)
set_buttons(0,cdfc);
else if (CDW == PAGE)
set_buttons(1,curr_mc);
else if (!dw[PAGE].v) {
button[balpha] = 0;
button[btype] = 0;
}
/* restore default redraw area */
if (mclip == 0) {
clip_dw(0);
}
}
/*
Free global resources allocated by graphic elements.
*/
void free_earray(void)
{
int i;
/* just a workaround by now */
for (i=0; i<=topge; ++i)
if (ge[i].type == GE_SELECT)
--TOP_CM;
topge = -1;
}
/*
Get image size.
*/
void img_size(char *img,int *w,int *h)
{
if (strcmp(img,"internal") == 0) {
*w = (RWX + 4 + 1) / plist_rf;
*h = (RWY + 4 + 1) / plist_rf;
}
else if ((strncmp(img,"pattern/",8) == 0) || (strncmp(img,"symbol/",7) == 0)) {
*w = FS/plist_rf;
*h = FS/plist_rf;
}
else if (strncmp(img,"apatt/",6) == 0) {
int i;
i = id2idx(atoi(img+6));
if ((0<=i) && (i<=topp))
*w = pattern[i].bw/plist_rf;
else
*w = FS/plist_rf;
*h = FS/plist_rf;
}
else if (strncmp(img,"word/",5) == 0) {
int i;
wdesc *W;
i = id2idx(atoi(img+5));
W = word + i;
*w = (W->r - W->l + 1) / plist_rf;
*h = (W->b - W->t + 1) / plist_rf;
}
}
/*
Add text line to the welcome page.
*/
void add_text_welcome(char *s,int y)
{
XTextItem xti;
int t;
xti.delta = 0;
xti.font = dfont->fid;
xti.chars = s;
xti.nchars = t = strlen(xti.chars);
XSetForeground(xd,xgc,black.pixel);
if (((DT-PT-TH) > 2*DFH) && (DW > t*DFW)) {
XDrawText(xd,xw,xgc,DM+(DW-t*DFW)/2,y,&xti,1);
}
}
/*
*/
void move_closure(cldesc *c)
{
c->l -= 10;
if ((c->r -= 10) < 0) {
c->l += 1200;
c->r += 1200;
}
}
/*
Draw the welcome window.
*/
void draw_welcome(void)
{
char v[20];
/* closures */
XSetForeground(xd,xgc,white.pixel);
XFillRectangle(xd,xw,xgc,DM,DT,DW,DH);
draw_closure(&c12,0);
draw_closure(&c13,0);
draw_closure(&c14,0);
draw_closure(&c15,0);
draw_closure(&c16,0);
draw_closure(&c17,0);
draw_closure(&c18,0);
draw_closure(&c19,0);
/* text messages */
add_text_welcome("W E L C O M E T O",DT-DFH-VS);
add_text_welcome("( C L A R A O C R )",DT+DH+10+DFH+VS);
snprintf(v,19,"version %s",version);
v[19] = 0;
add_text_welcome(v,PT+PH-6*(DFH+VS));
add_text_welcome("(C) 1999-2002 Ricardo Ueda Karpischek and others",PT+PH-5*(DFH+VS));
add_text_welcome("Clara OCR comes with ABSOLUTELY NO WARRANTY",PT+PH-4*(DFH+VS));
add_text_welcome("type L to see the conditions",PT+PH-3*(DFH+VS));
/* provision for one more line */
/* add_text_welcome("new line",PT+PH-2*(DFH+VS)); */
#ifdef FUN_CODES
/* move banner */
if (fun_code == 1) {
move_closure(&c12);
move_closure(&c13);
move_closure(&c14);
move_closure(&c15);
move_closure(&c16);
move_closure(&c17);
move_closure(&c18);
move_closure(&c19);
}
#endif
}
/*
Draw the alphabet map.
*/
void draw_map(void)
{
int i,j,k,l,t;
/* background */
if (*cm_v_map != ' ') {
XSetForeground(xd,xgc,darkgray.pixel);
XFillRectangle(xd,xw,xgc,PM-52,PT-1,42,(max_sz*15+max_sz-1)+4);
}
/* draw the map */
if ((*cm_v_map != ' ') && (alpha != NULL)) {
/* cells */
XSetForeground(xd,xgc,gray.pixel);
for (i=0; i<alpha_sz; ++i) {
for (j=0; j<alpha_cols; ++j)
XFillRectangle(xd,xw,xgc,PM-50+j*13,PT+i*16+1,12,15);
}
/* letters */
XSetForeground(xd,xgc,black.pixel);
for (i=0; i<alpha_sz; ++i) {
/* capital letters */
for (j=0; j<16; ++j) {
t = alpha[i].cbm[j];
for (k=0; k<8; ++k) {
if (t & 1) {
XDrawPoint(xd,xw,xgc,PM-50+9-k,PT+i*16+j-1);
}
t >>= 1;
}
}
/* small leters */
if (alpha_cols >= 2)
for (j=0; j<16; ++j) {
t = alpha[i].sbm[j];
for (k=0; k<8; ++k) {
if (t & 1) {
XDrawPoint(xd,xw,xgc,PM-37+9-k,PT+i*16+j-1);
}
t >>= 1;
}
}
/* latin keyboard mapping */
if (alpha_cols >= 3)
for (j=0; j<16; ++j) {
for (l=0; (l<latin_sz) && (alpha[i].l!=latin[l].l); ++l);
if (l < latin_sz) {
t = latin[l].cbm[j];
for (k=0; k<8; ++k) {
if (t & 1) {
XDrawPoint(xd,xw,xgc,PM-24+9-k,PT+i*16+j-1);
}
t >>= 1;
}
}
}
}
/* frames */
if (alpha != NULL)
draw_dframe(PM-50,PT+1,alpha_cols*12+alpha_cols-1,alpha_sz*15+alpha_sz-1);
}
}
/*
Redraw junction when resizing windows.
*/
void redraw_junction(int j,int oj,int tdw,int bdw)
{
int cur_dw,sb,a,b;
XRectangle r;
cur_dw = CDW;
if (*cm_v_hide == ' ')
sb = 10+RW;
else
sb = 0;
/* compute the bottom of the top window before the resize */
if (tdw == PAGE)
a = LAST_PAGE_DT + LAST_PAGE_DH - 1;
else if (tdw == PAGE_OUTPUT)
a = LAST_OUTPUT_DT + LAST_OUTPUT_DH - 1;
else {
db("invalid context for redraw_junction");
return;
}
/* compute the top of the bottom window before the resize */
if (bdw == PAGE_OUTPUT)
b = LAST_OUTPUT_DT;
else if (bdw == PAGE_SYMBOL)
b = LAST_SYMBOL_DT;
else {
db("invalid context for redraw_junction");
return;
}
/*
Set private clip to go from the topmost of the old and
new bottom of the top window to the bottommost of the old and
new top of the bottom window.
*/
r.x = PM+4;
r.width = PW-8;
if (j > oj) {
if (tdw == PAGE) {
r.y = dw[tdw].dt;
r.height = dw[bdw].dt-r.y;
PDT = a;
PVR = (dw[PAGE].dt + dw[PAGE].dh - PDT) * dw[PAGE].rf;
}
else {
r.y = a;
r.height = dw[bdw].dt-a;
}
}
else {
r.y = dw[tdw].dt + dw[tdw].dh - 1;
r.height = b - dw[tdw].dt + dw[tdw].dh - 1;
}
XSetClipRectangles(xd,xgc,0,0,&r,1,Unsorted);
mclip = 1;
/* repaint background avoiding to overwrite the components */
{
XSetForeground(xd,xgc,gray.pixel);
/* clearance over the new top of the bottom window */
XFillRectangle(xd,xw,xgc,PM+4,dw[bdw].dt-10+2,PW-8,6);
/*
When the PAGE window is narrower than PAGE_OUTPUT, the
background must be redrawn over the left and right
clearances.
*/
if (tdw == PAGE) {
int l,h;
if (j > oj) {
/* left clearance */
l = dw[PAGE].dm - dw[PAGE_OUTPUT].dm + 3;
h = b - dw[PAGE_OUTPUT].dt;
XFillRectangle(xd,xw,xgc,PM+4,b-3,l,h);
/* right clearance */
XFillRectangle(xd,xw,xgc,dw[PAGE].dm+dw[PAGE].dw,b-3,l,h);
}
else if (sb > 0) {
/* background on the corner of the two scrollbars */
XFillRectangle(xd,xw,xgc,dw[PAGE].dm+dw[PAGE].dw+7,dw[PAGE_OUTPUT].dt-10-RW-3,RW+6,RW+6);
}
}
if (sb > 0) {
XFillRectangle(xd,xw,xgc,PM+4,dw[bdw].dt-10+2,PW-8,6);
/* background between the top window and the scrollbar */
XFillRectangle(xd,xw,xgc,PM+4,dw[bdw].dt-10-sb+2,PW-8,6);
/* background on the corner of the two scrollbars */
XFillRectangle(xd,xw,xgc,PM+PW-10-RW-10+2,dw[bdw].dt-sb-2,8+RW+4,RW+4);
}
}
/* redraw top window */
if (j > oj) {
r.x = DM-1;
r.width = DW+2;
r.y = oj-5-sb;
r.height = j-oj;
XSetClipRectangles(xd,xgc,0,0,&r,1,Unsorted);
mclip = 1;
CDW = tdw;
draw_dw(1,0);
}
/* unset private clip */
set_mclip(0);
if (tdw == PAGE) {
PDT = dw[PAGE].dt;
PVR = dw[PAGE].vr;
}
/* top window frames */
CDW = tdw;
draw_dw_frames();
/* redraw bottom window */
CDW = bdw;
draw_dw(1,0);
draw_dw_frames();
/* top window scrollbars */
CDW = tdw;
draw_sbars();
/* bottom window scrollbars */
CDW = bdw;
draw_sbars();
/* restore CDW */
CDW = cur_dw;
}
/*
Draw the zone limits[0..7].
*/
void draw_zone(int *limits)
{
int i,j,l,r,t,b;
int x,y,x0,y0,dx,dx2,dy,dy2,k,vx,vy,sy,sx;
int f,fx,fy,lx,ly;
/* line attributes and color to use */
XSetLineAttributes(xd,xgc,1,LineSolid,CapNotLast,JoinRound);
XSetForeground(xd,xgc,black.pixel);
/*
Rectangular code
----------------
This code is currently unused. It draws only rectangular zones.
*/
if (0) {
/* horizontal */
if (! (((limits[0]-X0)/RF > DW) || (limits[4] < X0))) {
l = (limits[0] < X0) ? 0 : ((limits[0]-X0) / RF);
if ((r = (limits[4]-X0) / RF) >= DW)
r = DW - 1;
for (i=1; i<=5; i+=4)
if ((Y0 <= limits[i]) && ((t=(limits[i]-Y0)/RF) < DH))
XDrawLine(xd,xw,xgc,DM+l,DT+t,DM+r,DT+t);
}
/* vertical */
if (! (((limits[1]-Y0)/RF > DH) || (limits[5] < Y0))) {
t = (limits[1] < Y0) ? 0 : ((limits[1]-Y0) / RF);
if ((b = (limits[5]-Y0) / RF) >= DH)
b = DH - 1;
for (i=0; i<=4; i+=4)
if ((X0 <= limits[i]) && ((l=(limits[i]-X0)/RF) < DW))
XDrawLine(xd,xw,xgc,DM+l,DT+t,DM+l,DT+b);
}
}
/*
Non-rectangular
---------------
This is a brute-force approach. This code draws each line using a
Bresenham-like method, in order to detect the first and last visible
pixels.
*/
else {
for (j=0; j<4; ++j) {
/* draw (x0,y0) -> (x0+dx,y0+dy) */
x0 = limits[2*j];
y0 = limits[2*j+1];
dx = limits[(2*(j+1))%8] - x0;
dy = limits[(2*(j+1)+1)%8] - y0;
/* variation */
vx = (dx > 0) ? 1 : ((dx < 0) ? -1 : 0);
vy = (dy > 0) ? 1 : ((dy < 0) ? -1 : 0);
/* prepare */
dx = abs(dx);
dy = abs(dy);
dx2 = 2*dx;
dy2 = 2*dy;
x = x0;
y = y0;
i = 0;
f = 0;
sx = sy = 0;
/* just to avoid a compilation warning */
fx = fy = lx = ly = 0;
/* use x as reference */
if (dx >= dy) {
for (k=0; k<=dx; ++k) {
/* found visible pixel */
if ((X0<=x) && (x-X0<HR) && (Y0<=y) && (y-Y0<VR)) {
/* the first pixel inside */
if (f==0) {
fx = x;
fy = y;
f = 1;
}
/* remember because maybe this is the last one inside.. */
lx = x;
ly = y;
}
/* the first pixel outside, get out */
else if (f) {
k = dx + 1;
}
/* compute next pixel */
x += vx;
sy += dy2;
if ((i == 0) && (sy >= dx)) {
y += vy;
i = 1;
}
if (sy >= dx2) {
sy -= dx2;
i = 0;
}
}
}
/* use y as reference */
else {
for (x=x0, y=y0, k=0, i=0, f=0, l=0, sx=0; k<=dy; ++k) {
/* found visible pixel */
if ((X0<=x) && (x-X0<HR) && (Y0<=y) && (y-Y0<VR)) {
/* the first pixel inside */
if (f==0) {
fx = x;
fy = y;
f = 1;
}
/* remember because maybe this is the last one inside.. */
lx = x;
ly = y;
}
/* the first pixel outside, get out */
else if (f) {
k = dy + 1;
}
/* compute next pixel */
y += vy;
sx += dx2;
if ((i == 0) && (sx >= dy)) {
x += vx;
i = 1;
}
if (sx >= dy2) {
sx -= dy2;
i = 0;
}
}
}
if (f) {
XDrawLine(xd,xw,xgc,DM+(fx-X0)/RF,DT+(fy-Y0)/RF,DM+(lx-X0)/RF,DT+(ly-Y0)/RF);
}
}
}
}
/*
The function redraw
-------------------
The function "redraw" redraws all graphic components as requested
by the redraw flags (redraw_button, redraw_dw, etc). These flags
are set by the procedures that request display. For instance, if
a procedure must put a message on the status line, it will copy
the message to the buffer mb and set redraw_stline to 1. From
time to time the function redraw must be called to perform the
redraw as determined by the flags and the associated data
structures.
The function redraw can be called at anytime by anyone. The only
possible side effect will be wasting system resources. Currently
redraw is called only by xevents.
*/
void redraw(void)
{
int i;
XTextItem xti;
/*
From time to time we use this block for debugging..
*/
if ((redraw_tab) ||
(redraw_button) ||
(redraw_inp) ||
(redraw_dw) ||
(redraw_j1) ||
(redraw_j2) ||
(redraw_zone) ||
(redraw_stline) ||
(redraw_pbar) ||
(redraw_flea) ||
(redraw_menu)) {
/* db("updating screen"); */
}
else {
set_mclip(0);
return;
}
#ifdef FUN_CODES
/* menus stop the flea to avoid trashing the window */
if ((cmenu != NULL) && (fun_code == 3)) {
redraw_flea = 1;
fun_code = 0;
}
#endif
/*
Redraw background
-----------------
*/
if (redraw_bg) {
int a;
/* window background color */
XSetForeground(xd,xgc,darkgray.pixel);
/* an expensive method.. */
if (1) {
XFillRectangle(xd,xw,xgc,0,MH,WW,WH-MH);
}
/* a bad method.. */
else {
XFillRectangle(xd,xw,xgc,0,MH,WW,10+TH);
XFillRectangle(xd,xw,xgc,0,MH,10,WH-MH);
XFillRectangle(xd,xw,xgc,0,MH,10+BW,10);
a = MH+10+BUTT_PER_COL*BH;
XFillRectangle(xd,xw,xgc,0,a,10+BW+10,WH-a);
a = MH+10+PH;
XFillRectangle(xd,xw,xgc,0,a,WW,WH-a);
XFillRectangle(xd,xw,xgc,WW-10,MH,10,WH-MH);
}
/* plate background */
XSetForeground (xd,xgc,gray.pixel);
XFillRectangle(xd,xw,xgc,PM,PT+TH,PW,PH-TH);
/* plate frame */
XSetLineAttributes(xd,xgc,2,LineSolid,CapNotLast,JoinRound);
XSetForeground (xd,xgc,vdgray.pixel);
XDrawLine(xd,xw,xgc,PM,PT+PH-1,PM+PW-1,PT+PH-1);
XDrawLine(xd,xw,xgc,PM+PW-1,PT+TH,PM+PW-1,PT+PH-1);
XSetForeground (xd,xgc,white.pixel);
XDrawLine(xd,xw,xgc,PM,PT+TH,PM,PT+PH-1);
XSetLineAttributes(xd,xgc,1,LineSolid,CapNotLast,JoinRound);
}
/*
Redraw tabs
-----------
*/
if (redraw_tab) {
XSetForeground (xd,xgc,darkgray.pixel);
XFillRectangle(xd,xw,xgc,PM-2,PT,PW+4,TH);
for (i=0; i<TABN; ++i) {
draw_tab(PM+10+i*(10+TW+10),PT,i);
}
}
/*
Redraw windows
--------------
*/
if (redraw_dw) {
int i,cur_dw;
/* welcome page */
if (dw[WELCOME].v) {
draw_welcome();
draw_dw_frames();
}
/* scanned page */
if (dw[PAGE].v) {
cur_dw = CDW;
CDW = PAGE;
draw_dw(1,0);
draw_sbars();
draw_dw_frames();
CDW = cur_dw;
}
/* PAGE fat bits */
if (dw[PAGE_FATBITS].v) {
if (dw[PAGE_FATBITS].rg) {
#ifdef FUN_CODES
/* stop the flea */
if (fun_code == 3) {
/* stop it */
redraw_flea = 1;
fun_code = 0;
}
#endif
copychar();
}
draw_cfont(cfont);
draw_sbars();
}
/* pattern in fat bit mode */
if (dw[TUNE_PATTERN].v) {
/* regenerate the fat bit image */
if (dw[TUNE_PATTERN].rg) {
/* compute the skeleton and copy to cfont */
if ((0<=cdfc) && (cdfc<=topp)) {
pskel(cdfc);
p2cf();
}
/* no valid pattern: clear cfont */
else
memset(cfont,WHITE,FS*FS);
dw[TUNE_PATTERN].rg = 0;
}
cur_dw = CDW;
CDW = TUNE_PATTERN;
draw_cfont(cfont);
CDW = cur_dw;
}
/* MATCHES */
if (dw[PAGE_MATCHES].v) {
draw_cfont(cfont);
}
/* HTML windows */
for (i=0; i<=TOP_DW; ++i) {
if ((dw[i].html) && (dw[i].v)) {
cur_dw = CDW;
CDW = i;
if (RG) {
curr_hti = -1;
if (CDW == PAGE_OUTPUT)
mk_page_output(0);
else if (CDW == PATTERN_LIST)
mk_pattern_list();
else if (CDW == PATTERN)
mk_pattern_props();
else if (CDW == PATTERN_TYPES)
mk_pattern_types();
else if (CDW == PATTERN_ACTION)
mk_pattern_action();
else if (CDW == TUNE_SKEL)
mk_tune_skel();
else if (CDW == TUNE)
mk_tune();
else if (CDW == TUNE_ACTS)
mk_history(curr_act);
else if (CDW == PAGE_LIST)
mk_page_list();
else if (CDW == PAGE_SYMBOL)
mk_page_symbol(curr_mc);
else if (CDW == PAGE_DOUBTS)
mk_page_doubts();
else if (CDW == DEBUG)
mk_debug();
/* HTML renderization */
if ((HS) && (*cm_v_vhs == ' '))
html2ge(text,HM);
/* text renderization */
else
text2ge((CDW==GPL) ? gpl : text);
check_dlimits(0);
RG = 0;
}
draw_dw(1,0);
if (dw[CDW].ds)
draw_sbars();
draw_dw_frames();
CDW = cur_dw;
}
}
/* no need to redraw input fields if we redraw all windows */
redraw_inp = 0;
}
/*
Redraw the flea
---------------
*/
if (redraw_flea) {
if (CDW == PAGE_FATBITS) {
/* someone stopped the flea: clear last and current positions */
if (fun_code != 3) {
if ((0<=last_fp) && (last_fp<=topfp)) {
draw_flea(fp[2*last_fp],fp[2*last_fp+1],0);
last_fp = -1;
}
if ((0<=curr_fp) && (curr_fp<=topfp))
draw_flea(fp[2*curr_fp],fp[2*curr_fp+1],0);
}
/* the flea moved */
else {
/* clear last */
if ((0<=last_fp) && (last_fp<=topfp)) {
draw_flea(fp[2*last_fp],fp[2*last_fp+1],0);
last_fp = -1;
}
/* draw current */
if ((0<=curr_fp) && (curr_fp<=topfp))
draw_flea(fp[2*curr_fp],fp[2*curr_fp+1],1);
}
}
redraw_flea = 0;
}
/*
Redraw buttons
--------------
*/
if (redraw_button) {
/* draw buttonbars */
draw_bb(10,PT,BL,0);
/*
Deprecated "arrow" buttons. Maybe this code will
become useful in the future.
*/
{
/* prepare to draw arrows */
/*
XSetForeground(xd,xgc,black.pixel);
XSetLineAttributes(xd,xgc,1,LineSolid,CapNotLast,JoinRound);
*/
/* left */
/*
j = PT+bprev*(BH+VS)+BH/2;
draw_arrow(2,10+(BW-20)/2,j,0);
*/
/* right */
/*
j = PT+bnext*(BH+VS)+BH/2;
draw_arrow(2,10+(BW-20)/2,j,1);
*/
/* start and end of page */
/*
j = PT+sarrow*(BH+VS)+BH/2;
draw_arrow(1,15,j,0);
draw_arrow(1,10+BW-20,j,1);
*/
/* top and bottom */
/*
j = PT+tarrow*(BH+VS)+(BH-12)/2;
draw_arrow(1,20-2,j,2);
draw_arrow(1,10+BW-10-2,j,3);
*/
}
}
/*
Redraw input fields
-------------------
*/
if (redraw_inp) {
int cur_dw;
/* redraw input fields on visible HTML windows */
for (i=0; i<=TOP_DW; ++i) {
if ((dw[i].html) && (dw[i].v)) {
cur_dw = CDW;
CDW = i;
draw_dw(0,1);
CDW = cur_dw;
}
}
}
/* redraw PAGE/PAGE_OUTPUT junction */
if (redraw_j1) {
redraw_junction(page_j1,opage_j1,PAGE,PAGE_OUTPUT);
}
/* redraw PAGE_OUTPUT/PAGE_SYMBOL junction */
if (redraw_j2) {
redraw_junction(page_j2,opage_j2,PAGE_OUTPUT,PAGE_SYMBOL);
}
/*
Redraw the rectangle around the current zone
--------------------------------------------
*/
if (redraw_zone) {
int cur_dw,t;
/* redraw OCR zone */
if ((dw[PAGE].v) && (zones > 0)) {
cur_dw = CDW;
CDW = PAGE;
for (t=0; t<zones; ++t) {
draw_zone(limits+t*8);
}
CDW = cur_dw;
}
}
/*
Redraw status line
------------------
*/
if (redraw_stline) {
/* clear message area */
XSetForeground(xd,xgc,darkgray.pixel);
XFillRectangle(xd,xw,xgc,10,WH-10-DFH-2,WW-20,DFH+4);
/* message */
XSetForeground(xd,xgc,black.pixel);
xti.delta = 0;
xti.font = dfont->fid;
xti.chars = mb;
if ((xti.nchars = strlen(xti.chars)) > 0) {
XDrawText(xd,xw,xgc,10,WH-10-DFH+DFA,&xti,1);
}
}
/*
Redraw progress bar
-------------------
*/
if (redraw_pbar) {
/* clear message area */
XSetForeground(xd,xgc,darkgray.pixel);
XFillRectangle(xd,xw,xgc,10,WH-10-DFH-2,WW-20,DFH+4);
/* progress bar */
XSetForeground(xd,xgc,black.pixel);
xti.delta = 0;
xti.font = dfont->fid;
xti.chars = mb;
if ((xti.nchars = strlen(xti.chars)) > 0) {
XDrawText(xd,xw,xgc,10,WH-10-DFH+DFA,&xti,1);
}
XSetLineAttributes(xd,xgc,2,LineSolid,CapNotLast,JoinRound);
XSetForeground (xd,xgc,black.pixel);
XDrawLine(xd,xw,xgc,10,WH-10,10+redraw_pbar,WH-10);
XSetLineAttributes(xd,xgc,1,LineSolid,CapNotLast,JoinRound);
}
/*
Redraw alphabet mapping
-----------------------
remark: the menu must be the last component draw by the
function redraw.
*/
if (redraw_map) {
draw_map();
}
/*
Redraw context menu
-------------------
*/
if (redraw_menu) {
/* redraw menu bar */
if ((cmenu==NULL) || (CY0_orig < 0) || (redraw_menu==2)) {
draw_mbar();
}
/* redraw menu */
if (cmenu != NULL) {
draw_menu();
}
}
/*
Copy updated areas
*/
if (use_xb) {
/* copy the entire window */
if (redraw_bg) {
XCopyArea(xd,xw,XW,xgc,0,0,WW,WH,0,0);
}
/* copy the menu */
else if (mclip) {
if (cmenu != NULL) {
XCopyArea(xd,xw,XW,xgc,CX0-1,CY0-1,CW+2,CH+2,CX0-1,CY0-1);
}
}
else {
/* copy the menu bar and the menu (if any) */
if (redraw_menu) {
XCopyArea(xd,xw,XW,xgc,0,0,WW,MH,0,0);
if (cmenu != NULL) {
XCopyArea(xd,xw,XW,xgc,CX0-1,CY0-1,CW+2,CH+2,CX0-1,CY0-1);
}
}
/* copy the buttonbar */
if (redraw_button) {
XCopyArea(xd,xw,XW,xgc,9,PT-1,BW+2,BUTT_PER_COL*(BH+VS)-VS+2,9,PT-1);
}
/* copy the tabs */
if (redraw_tab) {
XCopyArea(xd,xw,XW,xgc,PM-2,PT,PW+4,TH,PM-2,PT);
}
/*
Copy the plate. TODO: redraw_inp should not require
redrawing the windows!
*/
if ((redraw_dw) ||
(redraw_j1) ||
(redraw_j2) ||
(redraw_inp)) {
XCopyArea(xd,xw,XW,xgc,PM-2,PT+TH,PW+4,PH-TH,PM-2,PT+TH);
}
/* copy the status line */
if (redraw_stline) {
XCopyArea(xd,xw,XW,xgc,10,WH-10-DFH-2,WW-20,DFH+4,10,WH-10-DFH-2);
}
/* copy the progress bar */
if (redraw_pbar) {
XCopyArea(xd,xw,XW,xgc,10,WH-10-DFH-2,WW-20,DFH+4,10,WH-10-DFH-2);
}
}
}
/* done */
redraw_bg = 0;
redraw_tab = 0;
redraw_dw = 0;
redraw_button = 0;
redraw_inp = 0;
redraw_j1 = 0;
redraw_j2 = 0;
redraw_zone = 0;
redraw_stline = 0;
redraw_pbar = 0;
redraw_map = 0;
redraw_menu = 0;
/* make sure that mclip is unset */
set_mclip(0);
/* flush outgoing events */
XFlush(xd);
}
/*
Set the geometry of the PAGE tab.
*/
void set_pagetab(int y1,int y2,int u)
{
int i,sb;
int cur_dw;
/*
These are required to optimize window resize.
*/
if (y1 != page_j1) {
opage_j1 = page_j1;
page_j1 = y1;
LAST_PAGE_DT = dw[PAGE].dt;
LAST_PAGE_DM = dw[PAGE].dm;
LAST_PAGE_DH = dw[PAGE].dh;
LAST_OUTPUT_DT = dw[PAGE_OUTPUT].dt;
LAST_OUTPUT_DH = dw[PAGE_OUTPUT].dh;
}
else {
opage_j2 = page_j2;
page_j2 = y2;
LAST_OUTPUT_DT = dw[PAGE_OUTPUT].dt;
LAST_OUTPUT_DH = dw[PAGE_OUTPUT].dh;
LAST_SYMBOL_DT = dw[PAGE_SYMBOL].dt;
LAST_SYMBOL_DH = dw[PAGE_SYMBOL].dh;
}
if (*cm_v_hide == ' ')
sb = 10 + RW;
else
sb = 0;
/* remember CDW */
cur_dw = CDW;
/*
PAGE
*/
{
/* current window */
CDW = PAGE;
/*
PAGE bottom will be y1-5. Then subtract the horizontal
scrollbar width and separation (if any), the tab height and
the top separation (10) to obtain the PAGE height.
*/
DH = y1-5-sb-10-TH-PT;
DW = PW-20-sb;
/* maximum reduction factor */
{
int b;
MRF = YRES/DH + ((YRES % DH) != 0);
b = XRES/DW + ((XRES % DW) != 0);
if (b > MRF)
MRF = b;
if (RF > MRF)
RF = MRF;
}
/* window size in document pixels */
VR = DH * RF;
if (VR > YRES)
VR = YRES;
HR = DW * RF;
if (HR > XRES)
HR = XRES;
/* grid separation and width */
GS = 1;
GW = 0;
/* recompute geometry from VR and HR */
DW = HR/RF;
DH = VR/RF;
HR = DW*RF;
VR = DH*RF;
/* window margins */
DM = PM+(PW-(DW+sb))/2;
DT = PT+TH+(y1+5-PT-TH-(DH+sb))/2;
/* current tab and visible windows */
tab = PAGE;
for (i=0; i<=TOP_DW; ++i)
dw[i].v = 0;
dw[PAGE].v = 1;
/* document total size in document pixels */
GRX = XRES;
GRY = YRES;
/* HTML mode */
HTML = 0;
/* restricted clip window */
PHR = HR;
PVR = VR;
PDM = DM;
PDT = DT;
}
/*
PAGE (OUTPUT)
*/
if (u == 0) {
/* current window */
CDW = PAGE_OUTPUT;
/* window size, measured in document pixels */
VR = y2-5-sb-y1-5;
HR = PW-20-sb;
/* grid separation and width */
GS = 1;
GW = 0;
/* window size in display pixels */
DW = HR;
DH = VR;
/* window margins */
DM = PM+(PW-(DW+sb))/2;
DT = y1 + 5;
/* PAGE_OUTPUT is visible */
dw[PAGE_OUTPUT].v = 1;
/* reduction factor */
RF = 1;
/* HTML mode */
HTML = 1;
/* do not underline links */
ULINK = 0;
}
else
dw[PAGE_OUTPUT].v = 0;
/*
PAGE (SYMBOL)
*/
if (u == 0) {
/* current window */
CDW = PAGE_SYMBOL;
/* window size, measured in document pixels */
VR = PT+PH-10-sb-y2-5;
HR = PW-20-sb;
/* grid separation and width */
GS = 1;
GW = 0;
/* window size in display pixels */
DW = HR;
DH = VR;
/* window margins */
DM = PM+(PW-(DW+sb))/2;
DT = y2+5;
/* PAGE_SYMBOL is visible */
dw[PAGE_SYMBOL].v = 1;
/* reduction factor */
RF = 1;
/* position */
X0 = Y0 = 0;
/* HTML mode */
HTML = 1;
}
else
dw[PAGE_SYMBOL].v = 0;
/* recover DW */
CDW = cur_dw;
}
/*
The function force_redraw
-------------------------
This service just set all redraw flags to force the redraw of
all components (buttons, grid, context menu, etc) in the next
call of redraw. So the effets of force_redraw are
asynchronous. The function force_redraw receives no parameter.
*/
void force_redraw(void)
{
/*db("force_redraw requested");*/
/* flags */
redraw_tab = 1;
redraw_button = -1;
redraw_bg = 1;
redraw_grid = 1;
redraw_stline = 1;
redraw_dw = 1;
redraw_menu = 2;
redraw_map = 1;
}
/*
Sliding actions. These may be scrolling through the
scrollbar cursor or window resizing.
*/
void slide(void)
{
static struct timeval mrt = {0,0};
struct timeval t;
int SL,CL;
if ((0 <= CDW) && (CDW <= TOP_DW) && (sliding <= 2)) {
/* sliding horizontally */
if (sliding == 1) {
unsigned d;
int x,dx;
float fx;
x = xev.xbutton.x;
dx = x - s0;
/*
When moving the cursor on the scrollbar, each display
pixel moved will correspond to how many pixels to move
the document? The scrollbar length is SL=DW-2*RW and
the cursor length is CL=HR*(DW-2*RW)/GRX (if GRX>0)
or CL=DW-2*RW if (GRX==0), both measured in display
pixels. As difference SL-CL must correspond to GRX-HR
pixels on the document, each display pixel moved will
correspond to (GRX-HR)/(SL-CL) pixels to move the
document.
*/
SL = DW-2*RW;
if (GRX > 0)
CL = (HR*(DW-2*RW))/GRX;
else
CL = SL;
if (SL > CL)
fx = ((float)(GRX-HR)) / (SL-CL);
else
fx = 1;
dx *= fx;
gettimeofday(&t,NULL);
d = (t.tv_sec - mrt.tv_sec) * 1000000;
d += (t.tv_usec - mrt.tv_usec);
if ((dx != 0) && (d > 200000)) {
X0 += dx;
check_dlimits(0);
s0 = x;
memcpy(&mrt,&t,sizeof(mrt));
redraw_dw = 1;
if (CDW == PAGE_FATBITS)
RG = 1;
}
}
/* sliding vertically */
else if (sliding == 2) {
unsigned d;
int y,dy;
float fy;
y = xev.xbutton.y;
dy = y - s0;
SL = DH-2*RW;
if (GRY > 0)
CL = (VR*(DH-2*RW))/GRY;
else
CL = SL;
if (SL > CL)
fy = ((float)(GRY-VR)) / (SL-CL);
else
fy = 1;
dy *= fy;
gettimeofday(&t,NULL);
d = (t.tv_sec - mrt.tv_sec) * 1000000;
d += (t.tv_usec - mrt.tv_usec);
if ((dy != 0) && (d > 200000)) {
Y0 += dy;
check_dlimits(0);
s0 = y;
memcpy(&mrt,&t,sizeof(mrt));
redraw_dw = 1;
if (CDW == PAGE_FATBITS)
RG = 1;
}
}
}
/* resizing PAGE/PAGE_OUTPUT junction */
else if (sliding == 3) {
int x,y;
int sb;
int cur_dw;
if (*cm_v_hide == ' ')
sb = 10+RW;
else
sb = 0;
x = xev.xbutton.x;
y = xev.xbutton.y;
if ((y > PT+TH+2*RW+20+10+RW) &&
(y < page_j2-2*RW-20-10-RW)) {
set_pagetab(y,page_j2,0);
cur_dw = CDW;
CDW = PAGE;
if ((DT == LAST_PAGE_DT) &&
(DM == LAST_PAGE_DM)) {
redraw_j1 = 1;
redraw_zone = 1;
}
else
force_redraw();
CDW = cur_dw;
}
}
/* resizing PAGE_OUTPUT/PAGE_SYMBOL junction */
else if (sliding == 4) {
int x,y;
int sb;
if (*cm_v_hide == ' ')
sb = 10+RW;
else
sb = 0;
x = xev.xbutton.x;
y = xev.xbutton.y;
if ((y > page_j1+2*RW+20+10+RW) &&
(y < PT+PH-2*RW-20-10-RW)) {
set_pagetab(page_j1,y,0);
redraw_j2 = 1;
redraw_zone = 1;
}
}
}
/* (devel)
The function setview
--------------------
As each window is displayed on only one mode and each mode belongs
to only one tab, in order to set a given mode or a given tab,
just call setview informing one window present on that mode as
parameter. That is the only parameter received by setview.
The geometry of each window will be re-computed by setview, so
setview is not called only to change the current mode, but
also after operations that change the geometry of the windows,
just like resizing the application X window or hiding the
scrollbars, or resizing the PAGE window, etc.
*/
void setview(int mode)
{
int a,i,sb;
/* submit current form before changing mode */
form_auto_submit();
/* for geometric computations */
if (*cm_v_hide == ' ')
sb = 10 + RW;
else
sb = 0;
/*
To welcome. The window in this case contains only the
typewriter "CLARA OCR" image. The text messages
are displayed outside the window.
*/
if (mode == WELCOME) {
int w,h;
/* current window */
CDW = WELCOME;
/* grid separation and width */
GS = 1;
GW = 0;
/* window size in display pixels */
DH = PH - TH - 10*(DFH+VS);
if (DH < 0)
DH = DFH;
DW = PW - 40;
/* reduction factor */
RF = 1;
w = wx1 - wx0 + (wx1-wx0)/4;
h = wy1 - wy0 + (wy1-wy0)/2;
a = w/DW + (w%DW != 0);
if (a > RF)
RF = a;
a = h/DH + (h%DH != 0);
if (a > RF)
RF = a;
/* resize window */
DH = h / RF + 1;
DW = w / RF + 1;
/* window size in document pixels */
HR = DW*RF;
VR = DH*RF;
/* top left */
if ((X0 = wx0-(HR-wx1+wx0)/2) < 0)
X0 = 0;
if ((Y0 = wy0-(VR-wy1+wy0)/2) < 0)
Y0 = 0;
/* window margins */
DM = PM+(PW-DW)/2;
DT = PT+TH+(PH-TH-DH-8*(DFH+VS))/2+DFH+VS;
/* current tab and visible windows */
tab = -1;
for (i=0; i<=TOP_DW; ++i)
dw[i].v = 0;
dw[WELCOME].v = 1;
/* HTML mode */
HTML = 0;
/* restricted clip window */
PHR = HR;
PVR = VR;
PDM = DM;
PDT = DT;
}
/* to fat bits */
else if (mode == PAGE_FATBITS) {
/* current window */
CDW = PAGE_FATBITS;
PAGE_CM = PAGE_FATBITS;
/* grid separation and width */
GS = ZPS + 1;
GW = 1;
/* window size measured in document pixels */
if ((*cm_b_skel == 'X') ||
(*cm_b_border == 'X') ||
(*cm_b_hs == 'X') ||
(*cm_b_hb == 'X')) {
HR = VR = FS;
}
else {
VR = (PH-TH-20-sb) / GS;
HR = (PW-20-sb) / GS;
if (VR > 4*FS)
VR = 4*FS;
if (HR > 4*FS)
HR = 4*FS;
}
/* window size in display pixels */
DW = HR*GS;
DH = VR*GS;
/* horizontal and vertical margins */
DM = PM+(PW-(DW+sb))/2;
DT = PT+TH+(PH-TH-(DH+sb))/2;
/* current tab and visible windows */
tab = PAGE;
for (i=0; i<=TOP_DW; ++i)
dw[i].v = 0;
dw[CDW].v = 1;
/* reduction factor */
RF = 1;
/* document total size in document pixels */
GRX = XRES;
GRY = YRES;
/* HTML mode */
HTML = 0;
}
/* to matches */
else if (mode == PAGE_MATCHES) {
/* current window */
CDW = PAGE_MATCHES;
/* window size measured in document pixels */
VR = FS;
HR = FS;
/* grid separation and width */
GS = ZPS + 1;
GW = 1;
/* window size in display pixels */
DW = HR*GS;
DH = VR*GS;
/* horizontal and vertical margins */
DM = PM+(PW-DW)/2;
DT = PT+TH+(PH-TH-DH)/2;
/* current tab and visible windows */
tab = PAGE;
for (i=0; i<=TOP_DW; ++i)
dw[i].v = 0;
dw[CDW].v = 1;
/* reduction factor */
RF = 1;
/* document total size in document pixels */
GRX = FS;
GRY = FS;
/* HTML mode */
HTML = 0;
}
/*
Modes featuring just one big HTML window.
*/
else if ((mode == DEBUG) ||
(mode == GPL) ||
(mode == PATTERN) ||
(mode == PATTERN_LIST) ||
(mode == PATTERN_TYPES) ||
(mode == PATTERN_ACTION) ||
(mode == PAGE_LIST) ||
(mode == PAGE_DOUBTS) ||
(mode == TUNE) ||
(mode == TUNE_ACTS)) {
/* current window and tab */
CDW = mode;
if ((mode == PATTERN) ||
(mode==PATTERN_LIST) ||
(mode == PATTERN_TYPES)) {
PATTERN_CM = mode;
tab = PATTERN;
}
else if (mode == PAGE_LIST) {
PAGE_CM = mode;
tab = PAGE;
}
else if ((mode == GPL) ||
(mode == PATTERN_ACTION) ||
(mode == DEBUG)) {
tab = -1;
}
else if ((mode == TUNE) || (mode == TUNE_ACTS)) {
TUNE_CM = mode;
tab = TUNE;
}
/* window size, measured in document pixels */
VR = PH-TH-20-sb;
HR = PW-20-sb;
/* grid separation and width */
GS = 1;
GW = 0;
/* window size in display pixels */
DW = HR;
DH = VR;
/* window margins */
DM = PM+(PW-(DW+sb))/2;
DT = PT+TH+(PH-TH-(DH+sb))/2;
/* current tab and visible windows */
for (i=0; i<=TOP_DW; ++i)
dw[i].v = 0;
dw[CDW].v = 1;
/* reduction factor */
RF = 1;
/* HTML mode and source */
HTML = 1;
HS = ((mode != GPL) && (mode != DEBUG));
/* has scrollbars */
DS = 1;
}
/* to PAGE or PAGE_OUTPUT or PAGE_SYMBOL */
else if ((mode == PAGE) ||
(mode == PAGE_OUTPUT) ||
(mode == PAGE_SYMBOL)) {
CDW = mode;
PAGE_CM = PAGE;
if (*cm_o_pgo == ' ')
set_pagetab(page_j1,page_j2,0);
else
set_pagetab(PT+PH-5,PT+PH-5,1);
}
/* To skeleton tuning */
else if ((mode == TUNE_PATTERN) || (mode == TUNE_SKEL)) {
/* current window */
CDW = TUNE_SKEL;
TUNE_CM = TUNE_SKEL;
/* window size in display pixels */
DW = PW - 10 - FS*(ZPS+1) - 10 - sb - 10 - 4;
DH = PH-TH-20-sb;
/* window size measured in document pixels */
VR = DH;
HR = DW;
/* grid separation and width */
GS = 1;
GW = 1;
/* horizontal and vertical margins */
DM = PM+10+FS*(ZPS+1)+10+2;
DT = PT+TH+(PH-TH-DH-sb)/2;
/* reduction factor */
RF = 1;
/* HTML mode */
HTML = 1;
/* has scrollbars */
DS = 1;
/* current window */
CDW = TUNE_PATTERN;
/* window size measured in document pixels */
VR = FS;
HR = FS;
/* grid separation and width */
GS = ZPS + 1;
GW = 1;
/* window size in display pixels */
DW = HR*GS;
DH = VR*GS;
/* horizontal and vertical margins */
DM = PM+10; /*PM+(PW-DW)/2;*/
DT = dw[TUNE_SKEL].dt;
/* DT = PT+TH+(PH-TH-DH)/2; */
/* current tab and visible windows */
tab = TUNE;
for (i=0; i<=TOP_DW; ++i)
dw[i].v = 0;
dw[TUNE_PATTERN].v = 1;
dw[TUNE_SKEL].v = 1;
/* reduction factor */
RF = 1;
/* HTML mode */
HTML = 0;
/* no scrollbars */
DS = 0;
/* mode requested */
CDW = mode;
}
/* force redraw */
if (batch_mode == 0) {
force_redraw();
}
/* check_dlimits(NULL); */
}
/*
Display cb.
*/
void display_cb(void)
{
int i,j;
if (tab != PAGE_FATBITS)
setview(PAGE_FATBITS);
for (i=0; i<FS; ++i)
for (j=0; j<FS; ++j)
cfont[i+j*FS] = cb[i+j*LFS];
redraw_dw = 1;
redraw();
}
/*
Dismiss current menu.
*/
void dismiss(int f)
{
int m,T;
/* no current menu to dismiss */
if (cmenu == NULL)
return;
/* conditional dismiss */
if ((!f) /* && (cmenu->a == 1) */) {
int x,y;
/* get pointer position */
get_pointer(&x,&y);
/* tolerance */
T = 5;
/* become unconditional */
m = mb_item(x,y);
if ((m == -1) &&
((x < (CX0-T)) || ((CX0+CW+T) < x) || (y < (CY0-T)) || ((CY0+CH+T) < y)))
f = 1;
}
/* unconditional dismiss */
if (f) {
cmenu = NULL;
CX0_orig = CY0_orig = -1;
force_redraw();
set_mclip(1);
}
}
/*
Build the list of labels available for the alphabets button. This
list is maintained in the local static variable b, and the balpha
button label is pointed to b, so the standard support for drawing
button labels is used.
*/
void set_bl_alpha(void)
{
static char b[256];
int n;
b[255] = 0;
nalpha = 0;
strncpy(b,aname(OTHER),255);
n = 255-strlen(b);
BL[balpha] = b;
inv_balpha[nalpha] = OTHER;
++nalpha;
if (*cm_a_latin != ' ') {
strncat(b,":",n);
strncat(b,aname(LATIN),--n);
n = 255-strlen(b);
inv_balpha[nalpha] = LATIN;
++nalpha;
}
if (*cm_a_greek != ' ') {
strncat(b,":",n);
strncat(b,aname(GREEK),--n);
n = 255-strlen(b);
inv_balpha[nalpha] = GREEK;
++nalpha;
}
if (*cm_a_cyrillic != ' ') {
strncat(b,":",n);
strncat(b,aname(CYRILLIC),--n);
n = 255-strlen(b);
inv_balpha[nalpha] = CYRILLIC;
++nalpha;
}
if (*cm_a_hebrew != ' ') {
strncat(b,":",n);
strncat(b,aname(HEBREW),--n);
n = 255-strlen(b);
inv_balpha[nalpha] = HEBREW;
++nalpha;
}
if (*cm_a_arabic != ' ') {
strncat(b,":",n);
strncat(b,aname(ARABIC),--n);
n = 255-strlen(b);
inv_balpha[nalpha] = ARABIC;
++nalpha;
}
if (*cm_a_kana != ' ') {
strncat(b,":",n);
strncat(b,aname(KANA),n);
n = 255-strlen(b);
inv_balpha[nalpha] = KANA;
++nalpha;
}
if (*cm_a_number != ' ') {
strncat(b,":",n);
strncat(b,aname(NUMBER),--n);
n = 255-strlen(b);
inv_balpha[nalpha] = NUMBER;
++nalpha;
}
if (*cm_a_ideogram != ' ') {
strncat(b,":",n);
strncat(b,aname(IDEOGRAM),--n);
n = 255-strlen(b);
inv_balpha[nalpha] = IDEOGRAM;
++nalpha;
}
button[balpha] = (nalpha > 1) ? 1 : 0;
set_alpha();
redraw_button = balpha;
}
/*
Copy the region around the symbol mm of the current page to the
global buffer rw, and optionally dumps it as an asc pgm file.
*/
int wrmc8(int mm,char *s1,char *s2)
{
int i0,j0,l,r,t,b,w,h;
unsigned char g[200];
int k;
int a,rf;
sdesc *m;
cldesc *y;
int cmap[5];
memset(rw,WHITE,RWX*RWY);
if ((mm < 0) || (tops < mm))
return(0);
/* symbol limits */
m = mc + mm;
l = m->l;
r = m->r;
t = m->t;
b = m->b;
w = r-l+1;
h = b-t+1;
/*
Compute reduction factor to the symbol fit in
the buffer confortably.
*/
rf = 1;
if ((a = (w / (2*RWX/3)) + ((w % (2*RWX/3)) != 0)) > rf)
rf = a;
if ((a = (h / (2*RWY/3)) + ((h % (2*RWY/3)) != 0)) > rf)
rf = a;
/*
if (mm==2284)
++rf;
*/
/*
The page region we're interested on has
size (RWX*rf) x (RWY*rf). The sampled pixels will
be (m->l+i*rf,m->t+j*rf), where
i = i0, i0+1, ..., i0+RWX-1
j = j0, j0+1, ..., j0+RWY-1
and
i0 = -(RWX-((w/rf)+(w%rf!=0))) / 2
j0 = -(RWY-((h/rf)+(h%rf!=0))) / 2
Note that (w/rf)+(w%rf!=0) is just the number of sampled
pixels per line on mm. The number RWX-((w/rf)+(w%rf!=0))
is the horizontal excess, so i0 is just half this excess
(to centralize the symbol on the buffer).
*/
i0 = -(RWX-((w/rf)+(w%rf!=0))) / 2;
j0 = -(RWY-((h/rf)+(h%rf!=0))) / 2;
/*
Now we compute the absolute limits l, r, t and b of the
region we are interested in. After that the sampled
pixels will be (l+i*rf,t+j*rf) where
i = 0, 1, ..., RWX-1
j = 0, 1, ..., RWY-1
*/
l = m->l + i0*rf;
r = m->l + (i0+RWX-1)*rf;
t = m->t + j0*rf;
b = m->t + (j0+RWY-1)*rf;
/* list the closures that intersect the rectangle (l,r,t,b) */
list_cl(l,t,r-l+1,b-t+1,0);
/* clip and copy each such closure */
for (k=0; k<list_cl_sz; ++k) {
int i,j;
int ia,ib,ja,jb;
int black;
y = cl + list_cl_r[k];
/* choose colors */
black = GRAY;
for (i=0; i<m->ncl; ++i) {
if ((m->cl)[i] == list_cl_r[k]) {
black = BLACK;
i = m->ncl;
}
}
/*
Now we need to compute the values of i and j for which
(l+i*rf,t+j*rf) is a point of the closure k and of
the rectangle (l,r,t,b). The minimum and maximum such
values will be ia, ib, ja and jb:
i = ia, ia+1, ..., ib
j = ja, ja+1, ..., jb
*/
if (y->l <= l)
ia = 0;
else
ia = ((y->l-l)/rf) + (((y->l-l) % rf) != 0);
if (y->r >= r)
ib = RWX-1;
else
ib = (y->r-l) / rf;
if (y->t <= t)
ja = 0;
else
ja = ((y->t-t)/rf) + (((y->t-t) % rf) != 0);
if (y->b >= b)
jb = RWY-1;
else
jb = (y->b-t) / rf;
/* copy the pixels to the buffer */
for (i=ia; i<=ib; ++i) {
for (j=ja; j<=jb; ++j) {
if (pixel(y,l+i*rf,t+j*rf) == BLACK)
rw[i+j*RWX] = black;
}
}
}
/* dump to file */
if (s1 != NULL) {
int F,i,j;
/* colormap */
cmap[WHITE] = 255;
cmap[BLACK] = 0;
cmap[GRAY] = 180;
cmap[DARKGRAY] = 110;
cmap[VDGRAY] = 40;
F = open(s1,O_WRONLY|O_CREAT|O_TRUNC,0644);
sprintf((char *)g,"P2\n%d %d\n255\n",RWX,RWY);
write(F,g,strlen((char *)g));
for (i=0; i < RWY; ++i) {
for (j=0; j < RWX; ++j) {
sprintf((char *)g,"%d\n",cmap[rw[j+i*RWX]]);
write(F,g,strlen((char *)g));
}
}
close(F);
}
return(1);
}
/*
Prepare inp_str to be displayed on the message area.
*/
void redraw_inp_str(void)
{
strncpy(mb,inp_str,MMB);
mb[MMB] = 0;
strncat(mb,"_",MMB-strlen(mb));
redraw_stline = 1;
}
|