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
|
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#ifndef convex
#include <string.h>
#endif
/* X-Window include files */
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include "pgxwin.h"
#define PGX_IDENT "pgxwin"
#define PGX_IMAGE_LEN 1280 /* Length of the line-of-pixels buffer */
#define PGX_COLORMULT 65535 /* Normalized color intensity multiplier */
#define PGX_NCOLORS 16 /* Number of pre-defined PGPLOT colors */
/* A container used to record the geometry of the Pixmap */
typedef struct {
int xpix_per_inch; /* Number of pixels per inch along X */
int ypix_per_inch; /* Number of pixels per inch along Y */
unsigned int width; /* Width of window (pixels) */
unsigned int height; /* Height of window (pixels) */
int xmargin; /* X-axis 1/4" margin in pixels */
int ymargin; /* Y-axis 1/4" margin in pixels */
int xmin,xmax; /* Min/max X-axis pixels excluding 1/4" margins */
int ymin,ymax; /* Min/max X-axis pixels excluding 1/4" margins */
} XWgeom;
/*
* Declare a colormap update descriptor.
* This keeps a record of the number of buffered colormap updates
* that need to be sent to the display. This allows color-representations
* from multiple consecutive calls to pgscr() and pgshls() to be cached.
*/
typedef struct {
int nbuff; /* The number of buffered color representation updates */
int sbuff; /* The index of the first buffered color representation */
} XWcolor;
/*
* Declare a polygon descriptor.
*/
typedef struct {
XPoint *points; /* Temporary array of polygon vertexes */
int npoint; /* Number of points in polygon */
int ndone; /* The number of points received so far */
} XWpoly;
/*
* Declare a container used to record the extent of the rectangular
* pixmap area that has been modified since the last pgx_flush().
*/
typedef struct {
int modified; /* True if 'pixmap' has been modified since last update */
int xmin,xmax; /* X-axis extent of modified region (pixels) */
int ymin,ymax; /* Y-axis extent of modified region (pixels) */
} XWupdate;
/*
* Declare a cursor state container.
*/
typedef struct {
int drawn; /* True when the cursor is drawn */
GC gc; /* The graphical context of the cursor-band lines */
int warp; /* True to warp the cursor on first window entry */
int type; /* The cursor banding type PGX_..._CURSOR */
XPoint vbeg, vend; /* Start and end vertices of band cursors */
} XWcursor;
/*
* Declare a world-coordinate coordinate conversion object.
*/
typedef struct {
float xoff, xdiv; /* world_x = (device_x - xoff) / xdiv */
float yoff, ydiv; /* world_y = (device_y - yoff) / ydiv */
} XWworld;
/*
* Declare a container to encapsulate the buffers needed to
* draw a line of pixels.
*/
typedef struct {
int npix; /* The max number of pixels in buff[] */
unsigned char *buff; /* The image buffer registered to xi[] */
XImage *xi; /* Line of pixels Xlib image object */
} XWimage;
/*
* Declare a function type, instances of which are to be called to flush
* buffered opcodes, and return 0 if OK, or 1 on error.
*/
typedef int (*Flush_Opcode_fn) ARGS((PgxWin *));
/*
* The following container is used to retain state information for /xw
* connections.
*/
struct PgxState {
XWgeom geom; /* Pixmap geometry */
XWcolor color; /* Colormap state descriptor */
XWpoly poly; /* Polygon-fill accumulation descriptor */
XWupdate update; /* Descriptor of un-drawn area of pixmap */
XWcursor cursor; /* Cursor state context descriptor */
XWworld world; /* World-coordinate conversion descriptor */
XWimage image; /* Line of pixels container */
XGCValues gcv; /* Publicly visible contents of 'gc' */
GC gc; /* Graphical context descriptor */
int last_opcode; /* Index of last opcode */
Flush_Opcode_fn flush_opcode_fn; /* Function to flush a buffered opcode */
};
static int pgx_error_handler ARGS((Display *display, XErrorEvent *event));
static PgxColor *pgx_find_visual ARGS((PgxWin *pgx, int class, int min_col, \
int max_col));
static int pgx_get_colorcells ARGS((PgxWin *pgx, PgxColor *color, \
int min_col, int max_col));
static XVisualInfo *pgx_visual_info ARGS((Display *display, int screen, \
VisualID vid));
static int pgx_parse_visual ARGS((char *str));
static void pgx_xy_to_XPoint ARGS((PgxWin *pgx, float *xy, XPoint *xp));
static void pgx_XPoint_to_xy ARGS((PgxWin *pgx, XPoint *xp, float *xy));
static void pgx_mark_modified ARGS((PgxWin *pgx, int x, int y, int diameter));
static int pgx_init_colors ARGS((PgxWin *pgx));
static int pgx_update_colors ARGS((PgxWin *pgx));
static int pgx_flush_colors ARGS((PgxWin *pgx, int ci_start, int ncol));
static int pgx_restore_line ARGS((PgxWin *pgx, int xa, int ya, int xb, int yb));
static int pgx_handle_cursor ARGS((PgxWin *pgx, float *rbuf, char *key));
static void pgx_limit_pcoords ARGS((PgxWin *pgx, XPoint *coord));
static void pgx_limit_wcoords ARGS((PgxWin *pgx, XPoint *coord));
static void pgx_window_to_pixmap ARGS((PgxWin *pgx, XPoint *w_coord, \
XPoint *p_coord));
static void pgx_pixmap_to_window ARGS((PgxWin *pgx, XPoint *p_coord, \
XPoint *w_coord));
static int pgx_copy_area ARGS((PgxWin *pgx, int px, int py, unsigned w,
unsigned h, int wx, int wy));
static int pgx_clear_area(PgxWin *pgx, int x, int y, unsigned w, unsigned h);
PgxColor *new_PgxColor ARGS((PgxWin *pgx, int max_col));
static PgxColor *del_PgxColor ARGS((PgxWin *pgx, PgxColor *color));
static int pgx_default_class ARGS((PgxWin *pgx));
static int pgx_nint ARGS((float f));
/*
* pgx_ready() accepts a state bitmask that contains a union of the
* following enumerated resource requirement bits.
*/
#define PGX_NEED_COLOR 1 /* Colormap allocated */
#define PGX_NEED_WINDOW 2 /* Window created */
#define PGX_NEED_PIXMAP 4 /* A valid pixmap exists */
#define PGX_NEED_PGOPEN 8 /* Open to PGPLOT. Note that */
static int pgx_ready ARGS((PgxWin *pgx, int state));
/*.......................................................................
* This function should be called to instantiate PgxWin::state when
* an existing device is opened with pgbeg() or pgopen(). Note that all
* but the pixmap and state members of the passed PgxWin structure must have
* been instantiated. The pixmap member may either be instantiated or
* be initialized with the null-atom constant: None. The state member
* must be NULL.
*
* Input:
* pgx PgxWin * The PGPLOT window context to connect to.
* Output:
* return PgxState * The PGPLOT drawing-state descriptor, or NULL
* on error.
*/
#ifdef __STDC__
PgxState *pgx_open(PgxWin *pgx)
#else
PgxState *pgx_open(pgx)
PgxWin *pgx;
#endif
{
PgxState *state; /* The new state descriptor */
/*
* Check the validity of the PgxWin structure.
*/
if(!pgx || !pgx->display || pgx->window==None || !pgx->expose_gc ||
pgx->bad_device || !pgx->name || !pgx->color) {
fprintf(stderr, "pgx_open: Bad PgxWin descriptor.\n");
return NULL;
};
if(pgx->state) {
fprintf(stderr, "pgx_open: The specified device is already open.\n");
return NULL;
};
/*
* Allocate the state container.
*/
state = (PgxState *) malloc(sizeof(PgxState));
if(!state) {
fprintf(stderr, "pgx_open: Insufficient memory.\n");
return NULL;
};
/*
* Before attempting any operation that might fail, initialize
* the container at least up to the point at which it is
* safe to pass it to pgx_close().
*/
pgx->state = state;
state->geom.xpix_per_inch = 0;
state->geom.ypix_per_inch = 0;
state->geom.width = 0;
state->geom.height = 0;
state->geom.xmargin = 0;
state->geom.ymargin = 0;
state->geom.xmin = 0;
state->geom.xmax = 0;
state->geom.ymin = 0;
state->geom.ymax = 0;
state->color.nbuff = 0;
state->color.sbuff = 0;
state->poly.points = NULL;
state->poly.npoint = 0;
state->poly.ndone = 0;
state->update.modified = 0;
state->update.xmin = 0;
state->update.xmax = 0;
state->update.ymin = 0;
state->update.ymax = 0;
state->cursor.drawn = 0;
state->cursor.gc = NULL;
state->cursor.type = PGX_NORM_CURSOR;
state->world.xoff = 0.0;
state->world.yoff = 0.0;
state->world.xdiv = 1.0;
state->world.ydiv = 1.0;
state->image.npix = 0;
state->image.buff = NULL;
state->image.xi = NULL;
state->gc = NULL;
state->last_opcode = 0;
state->flush_opcode_fn = 0;
/*
* Create and initialize a graphical context descriptor. This is where
* Line widths, line styles, fill styles, plot color etc.. are
* recorded.
*/
state->gcv.line_width = 1;
state->gcv.cap_style = CapRound;
state->gcv.join_style = JoinRound;
state->gcv.fill_rule = EvenOddRule;
state->gcv.graphics_exposures = False;
state->gcv.foreground = WhitePixel(pgx->display, pgx->screen);
/*
* Bracket the actual creation with pgx_start/end_error() calls, to
* determine whether any allocation errors occur.
*/
pgx_start_error_watch(pgx);
state->gc = XCreateGC(pgx->display, pgx->window,
(unsigned long) (GCLineWidth | GCCapStyle |
GCJoinStyle | GCFillRule |
GCGraphicsExposures | GCForeground),
&state->gcv);
if(pgx_end_error_watch(pgx) || !state->gc) {
fprintf(stderr, "%s: Failed to allocate graphical context.\n", PGX_IDENT);
return pgx_close(pgx);
};
/*
* Determine the required size of the buffer. This is determined by
* the size of a pixel (the depth of the window), the number of
* pixels required, and the max depth that xwdriv allows (32-bit).
* In order to allow for 32-bit pixels round up the total number of
* bytes to an integral number of 32-bit words.
*/
state->image.npix = PGX_IMAGE_LEN;
{
unsigned nbyte = ((pgx->color->vi->depth * state->image.npix + 31)/32) * 4;
state->image.buff = (unsigned char *) malloc(nbyte * sizeof(char));
if(!state->image.buff) {
fprintf(stderr, "%s: Failed to allocate image buffer.\n", PGX_IDENT);
return pgx_close(pgx);
};
};
/*
* Create an X image container for use in transfering pixels to and from
* state->image.buff[].
*/
pgx_start_error_watch(pgx);
state->image.xi = XCreateImage(pgx->display, pgx->color->vi->visual,
(unsigned)pgx->color->vi->depth, ZPixmap, 0,
(char *)state->image.buff,
(unsigned)state->image.npix, 1, 32, 0);
if(pgx_end_error_watch(pgx) || !state->image.xi) {
fprintf(stderr, "%s: Failed to allocate XImage descriptor.\n", PGX_IDENT);
return pgx_close(pgx);
};
/*
* Create the cursor graphical context.
*/
pgx_start_error_watch(pgx);
{
XGCValues gcv;
gcv.line_width = 0;
gcv.graphics_exposures = False;
gcv.foreground = WhitePixel(pgx->display, pgx->screen);
state->cursor.gc = XCreateGC(pgx->display, pgx->window,
(unsigned long) (GCLineWidth | GCGraphicsExposures |
GCForeground), &gcv);
};
if(pgx_end_error_watch(pgx) || !state->cursor.gc) {
fprintf(stderr, "%s: Failed to allocate graphical context.\n", PGX_IDENT);
return pgx_close(pgx);
};
/*
* Determine the current window size.
*/
/*
* Initialize attributes.
*/
pgx_init_colors(pgx);
pgx_set_ci(pgx, 1);
pgx_set_lw(pgx, 1);
pgx_set_cursor(pgx, 0, PGX_NORM_CURSOR, 0, NULL, NULL);
return state;
}
/*.......................................................................
* This function should be called to delete a pgx->state PGPLOT
* drawing-state descriptor when pgend() is called. pgx->state will
* also be assigned NULL.
*
* Input:
* pgx PgxWin * The PGPLOT window context to disconnect.
* Output:
* return PgxState * The deleted PGPLOT drawing-state descriptor.
* Always NULL.
*/
#ifdef __STDC__
PgxState *pgx_close(PgxWin *pgx)
#else
PgxState *pgx_close(pgx)
PgxWin *pgx;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
PgxState *state = pgx->state;
/*
* Delete the graphical context descriptor.
*/
if(state->gc)
XFreeGC(pgx->display, state->gc);
state->gc = NULL;
/*
* Delete the image descriptor.
*/
if(state->image.xi)
XFree((char *)state->image.xi);
state->image.xi = NULL;
if(state->image.buff)
free((char *)state->image.buff);
state->image.buff = NULL;
/*
* Check for un-freed polygon points.
*/
if(state->poly.points)
free((char *)state->poly.points);
state->poly.points = NULL;
/*
* Delete the cursor graphical context descriptor.
*/
if(state->cursor.gc)
XFreeGC(pgx->display, state->cursor.gc);
state->cursor.gc = NULL;
/*
* Delete the container.
*/
free(state);
pgx->state = NULL;
};
return NULL;
}
/*.......................................................................
* This function must be called before each opcode is handled by the
* device-specific driver dispatch function.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* opcode int The new opcode.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_pre_opcode(PgxWin *pgx, int opcode)
#else
int pgx_pre_opcode(pgx, opcode)
PgxWin *pgx; int opcode;
#endif
{
/*
* If there is a buffered opcode and the latest opcode is not the same
* as the last opcode, call the given flush function for the
* buffered opcode.
*/
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
PgxState *state = pgx->state;
if(state->last_opcode != opcode) {
if(state->flush_opcode_fn != (Flush_Opcode_fn) 0) {
(*state->flush_opcode_fn)(pgx);
state->flush_opcode_fn = (Flush_Opcode_fn) 0;
};
/*
* Record the current opcode for next time.
*/
state->last_opcode = opcode;
};
};
return 0;
}
/*.......................................................................
* This function returns non-zero if the specified descriptor is not
* NULL, not marked as bad via pgx->bad_device, and has all of the
* specified resources.
*
* Input:
* pgx PgxWin * The device descriptor to be checked.
* state int A bitmask of resources that are required.
* PGX_NEED_COLOR - Colormap allocated.
* PGX_NEED_WINDOW - Window created.
* PGX_NEED_PIXMAP - A valid pixmap exists.
* PGX_NEED_PGOPEN - Open to PGPLOT. Note that
* this implies that a colormap and window
* exist but not that a pixmap exists.
* Output:
* return int 1 - Descriptor OK.
* 0 - Error - don't use pgx.
*/
#ifdef __STDC__
static int pgx_ready(PgxWin *pgx, int state)
#else
static int pgx_ready(pgx, state)
PgxWin *pgx; int state;
#endif
{
if(!pgx || pgx->bad_device)
return 0;
if(state & PGX_NEED_COLOR && !pgx->color)
return 0;
if(state & PGX_NEED_WINDOW && pgx->window == None)
return 0;
if(state & PGX_NEED_PIXMAP && pgx->pixmap == None)
return 0;
if(state & PGX_NEED_PGOPEN && !pgx->state)
return 0;
return 1;
}
/*.......................................................................
* Call this function when an Expose event is received. It will then
* re-draw the exposed region from the pgx->pixmap, taking acount of any
* scroll offsets in pgx->scroll. The device need not be open to PGPLOT.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* event XEvent * The expose event.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_expose(PgxWin *pgx, XEvent *event)
#else
int pgx_expose(pgx, event)
PgxWin *pgx; XEvent *event;
#endif
{
/*
* Device error?
*/
if(pgx_ready(pgx, PGX_NEED_PIXMAP | PGX_NEED_WINDOW) && event->type==Expose) {
pgx_copy_area(pgx, (int)(event->xexpose.x + pgx->scroll.x),
(int)(event->xexpose.y + pgx->scroll.y),
(unsigned) event->xexpose.width,
(unsigned) event->xexpose.height,
event->xexpose.x, event->xexpose.y);
/*
* Re-draw the possibly damaged cursor augmentation.
*/
pgx_refresh_cursor(pgx);
/*
* Ensure that the window responds immediately to the update.
*/
XFlush(pgx->display);
if(pgx->bad_device)
return 1;
};
return 0;
}
/*.......................................................................
* Scroll the pixmap within the window area.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* x,y unsigned The position of the top left corner of the window
* within the pixmap. This allows for clients that
* want to scroll the pixmap within the window area.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_scroll(PgxWin *pgx, unsigned x, unsigned y)
#else
int pgx_scroll(pgx, x, y)
PgxWin *pgx; unsigned x; unsigned y;
#endif
{
if(pgx_ready(pgx, 0)) {
XEvent event;
/*
* Record the new scroll and pan values.
*/
pgx->scroll.x = x;
pgx->scroll.y = y;
/*
* Redraw the scrolled contents of the window.
*/
if(pgx_ready(pgx, PGX_NEED_WINDOW | PGX_NEED_PIXMAP)) {
XPoint brc; /* Bottom right corner of pixmap */
/*
* We need to know the size of the window and the size of the pixmap.
*/
XWindowAttributes attr;
XGetWindowAttributes(pgx->display, pgx->window, &attr);
if(pgx->bad_device)
return 1;
/*
* Record the bottom-right-corner pixmap coordinate in brc.
*/
{
Window root;
int x_root, y_root;
unsigned width, height, border, depth;
XGetGeometry(pgx->display, pgx->pixmap, &root, &x_root, &y_root,
&width, &height, &border, &depth);
brc.x = width - 1;
brc.y = height - 1;
};
/*
* Determine the scrolled window coordinate at which the bottom right
* corner of the pixmap lies.
*/
pgx_pixmap_to_window(pgx, &brc, &brc);
/*
* Clear the parts of the window that will not be covered by the pixmap.
* Given that scroll.x and scroll.y are unsigned this can only be
* areas to the left and bottom of the drawn area.
*/
if(brc.x < attr.width) {
pgx_clear_area(pgx, (brc.x + 1), 0,
(unsigned) (attr.width - brc.x - 1),
(unsigned) (attr.height));
};
if(brc.y < attr.height) {
pgx_clear_area(pgx, 0, (brc.y + 1),
(unsigned)(attr.width),
(unsigned)(attr.height - brc.y - 1));
};
/*
* Set up a fake expose event to have the new pixmap area drawn.
*/
event.type = Expose;
event.xexpose.x = 0;
event.xexpose.y = 0;
event.xexpose.width = brc.x + 1;
event.xexpose.height = brc.y + 1;
return pgx_expose(pgx, &event);
};
};
return 1;
}
/*.......................................................................
* Update the recorded extent of the drawable area of the window.
* This must be called whenever the window is resized.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* doclip int 0 - Disable clipping entirely.
* 1 - Clip all graphics outside the specified region.
* width unsigned The width of the window.
* height unsigned The height of the window.
* border unsigned The width of the window border.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_update_clip(PgxWin *pgx, int doclip, unsigned width, unsigned height,
unsigned border)
#else
int pgx_update_clip(pgx, doclip, width, height, border)
PgxWin *pgx; int doclip; unsigned width; unsigned height; unsigned border;
#endif
{
if(pgx_ready(pgx, 0)) {
pgx->clip.doclip = doclip;
pgx->clip.xmin = border;
pgx->clip.ymin = border;
pgx->clip.xmax = width - border - 1;
pgx->clip.ymax = height - border - 1;
return 0;
};
return 1;
}
/*.......................................................................
* Install a temporary error handler for use in detecting resource
* allocation errors. The error handler will maintain a count of
* errors, which will be returned by the matching function
* pgx_end_error_watch(). These functions are intended to be used to
* bracket resource allocation functions, in order to allow resource
* allocation failures to be detected.
*
* Input:
* pgx PgxWin * The PGPLOT window context descriptor.
*/
#ifdef __STDC__
void pgx_start_error_watch(PgxWin *pgx)
#else
void pgx_start_error_watch(pgx)
PgxWin *pgx;
#endif
{
if(pgx_ready(pgx, 0)) {
/*
* Force all errors from previous events to be flushed.
*/
XSync(pgx->display, False);
/*
* Clear the error-handler internal error count.
*/
pgx_error_handler(pgx->display, (XErrorEvent *) 0);
/*
* Install the error handler.
*/
pgx->old_handler = XSetErrorHandler(pgx_error_handler);
};
return;
}
/*.......................................................................
* This function is paired with pgx_start_error_watch() and and after
* ensuring that all errors have been trapped by calling XSync(), it
* returns the error count recorded by the error handler that said function
* installed. It then removes the error handler.
*
* Input:
* pgx PgxWin * The PGPLOT window context descriptor.
* Output:
* return int The number of errors that were counted.
*/
#ifdef __STDC__
int pgx_end_error_watch(PgxWin *pgx)
#else
int pgx_end_error_watch(pgx)
PgxWin *pgx;
#endif
{
if(pgx_ready(pgx, 0)) {
/*
* Ensure that all error-events have delivered.
*/
XSync(pgx->display, False);
/*
* De-install the error handler and re-instate the one that it displaced.
*/
XSetErrorHandler(pgx->old_handler);
pgx->old_handler = 0;
/*
* Return the current error-count.
*/
return pgx_error_handler(pgx->display, (XErrorEvent *) 0);
};
return 0;
}
/*.......................................................................
* This function is called by X whenever a non-fatal error occurs
* on a given display connection. For the moment it does nothing but
* count such errors in an internal static error counter. This counter
* can then be queried and reset by sending a NULL error event pointer.
*
* Input:
* display Display * The display connection on which the error occured.
* event XErrorEvent * The descriptor of the error event, or NULL to
* request that the error counter be queried and reset.
* Output:
* return int The return value is not specified by Xlib, so
* for Xlib calls we will simply return 0. For
* none Xlib calls (distinguishable by sending
* event==NULL), the value of the error counter
* is returned.
*/
#ifdef __STDC__
static int pgx_error_handler(Display *display, XErrorEvent *event)
#else
static int pgx_error_handler(display, event)
Display *display; XErrorEvent *event;
#endif
{
static int error_count = 0;
/*
* To query and reset the error counter, this program calls pgx_error_handler()
* with a NULL error event pointer. This distinguishes it from a call
* from Xlib.
*/
if(!event) {
int ret_count = error_count;
error_count = 0; /* Reset the error counter */
return ret_count; /* Return the pre-reset value of the error counter */
#ifdef DEBUG
} else {
char errtxt[81]; /* Buffer to receive error message in */
/*
* Get a message describing the error.
*/
XGetErrorText(display, (int)event->error_code, errtxt, (int)sizeof(errtxt));
fprintf(stderr, "%s: XErrorEvent: %s\n", ERROR_PREFIX, errtxt);
/*
* Report the operation that caused it. These opcode numbers are listed in
* <X11/Xproto.h>.
*/
fprintf(stderr, "%s: Major opcode: %d, Resource ID: 0x%lx%s.\n",
ERROR_PREFIX, (int) event->request_code,
(unsigned long) event->resourceid,
(event->resourceid==DefaultRootWindow(display)?" (Root window)":""));
#endif
};
/*
* Keep a record of the number of errors that have occurred since the
* error counter was last cleared.
*/
error_count++;
return 0;
}
/*.......................................................................
* Allocate a visual/colormap context and initialize it with defaults.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* max_col int The maximum number of colors to allow for.
* Output:
* return PgxColor * The new context descriptor, or NULL on error.
*/
#ifdef __STDC__
PgxColor *new_PgxColor(PgxWin *pgx, int max_col)
#else
PgxColor *new_PgxColor(pgx, max_col)
PgxWin *pgx; int max_col;
#endif
{
PgxColor *color; /* The new context descriptor */
if(!pgx_ready(pgx, 0))
return NULL;
/*
* Allocate the context descriptor.
*/
color = (PgxColor *) malloc(sizeof(PgxColor));
if(!color) {
fprintf(stderr, "%s: (new_PgxColor) Insufficient memory.\n", PGX_IDENT);
return NULL;
};
/*
* Before attempting any operation that might fail, initialize the
* color descriptor at least up to the point at which it can safely be
* passed to del_PgxColor().
*/
color->vi = NULL;
color->cmap = None;
color->private = 0;
color->ncol = 2;
color->monochrome = 1;
color->pixel = NULL;
color->npixel = 0;
color->xcolor = NULL;
color->initialized = 0;
color->default_class = 0;
/*
* We can only handle between 2 and 256 colors.
*/
if(max_col < 2)
max_col = 2;
else if(max_col > 256)
max_col = 256;
/*
* Determine the class of the default visual.
*/
color->default_class = pgx_default_class(pgx);
/*
* Allocate an array to store pixel indexes in.
*/
color->pixel = (unsigned long *) malloc(sizeof(unsigned long) * max_col);
if(color->pixel==NULL) {
fprintf(stderr, "%s: Insufficient memory for new PGPLOT window.\n",
PGX_IDENT);
return del_PgxColor(pgx, color);
};
/*
* Allocate an array to store color representations in.
*/
color->xcolor = (XColor *) malloc(sizeof(XColor) * max_col);
if(!color->xcolor) {
fprintf(stderr, "%s: Insufficient memory for new PGPLOT window.\n",
PGX_IDENT);
return del_PgxColor(pgx, color);
};
/*
* Leave the rest of the initialization to specific functions.
*/
return color;
}
/*.......................................................................
* Delete a color/visual context descriptor.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* color PgxColor * The descriptor to be deleted.
* Output:
* return PgxColor * The deleted descriptor (always NULL).
*/
#ifdef __STDC__
static PgxColor *del_PgxColor(PgxWin *pgx, PgxColor *color)
#else
static PgxColor *del_PgxColor(pgx, color)
PgxWin *pgx; PgxColor *color;
#endif
{
if(color) {
if(color->vi) {
if(color->cmap) {
/*
* Release allocated colorcells.
*/
if(pgx->display && color->pixel && color->npixel > 0) {
switch(color->vi->class) {
case PseudoColor:
case GrayScale:
case DirectColor:
XFreeColors(pgx->display, color->cmap, color->pixel,
color->npixel, (unsigned long)0);
break;
};
};
};
/*
* Delete the colormap only if we allocated it.
*/
if(pgx->display && color->private &&
color->cmap != DefaultColormap(pgx->display, pgx->screen))
XFreeColormap(pgx->display, color->cmap);
/*
* Delete the visual information descriptor.
*/
XFree((char *) color->vi);
};
/*
* Delete the pixel and color representation arrays.
*/
if(color->pixel)
free(color->pixel);
if(color->xcolor)
free(color->xcolor);
/*
* Delete the container.
*/
free(color);
};
return NULL;
}
/*.......................................................................
* Return the colormap class of the default visual.
*
* Input:
* pgx PgxWin * The PGPLOT window context descriptor.
* Output:
* return int The colormap class.
*/
#ifdef __STDC__
static int pgx_default_class(PgxWin *pgx)
#else
static int pgx_default_class(pgx)
PgxWin *pgx;
#endif
{
int class;
XVisualInfo *vi = pgx_visual_info(pgx->display, pgx->screen,
XVisualIDFromVisual(DefaultVisual(pgx->display, pgx->screen)));
if(!vi)
return PseudoColor;
class = vi->class;
XFree((char *) vi);
return class;
}
/*.......................................................................
* Search for an appropriate visual for a PGPLOT window and create a
* colormap for it (unless the default visual is used).
*
* Input:
* pgx PgxWin * The PGPLOT window context (We need the display
* and screen members).
* class_name char * The name of the desired visual class type.
* min_col int The minimum acceptable number of colors before
* switching to monochrome.
* max_col int The maximum number of colors to allocate.
* Output:
* return PgxColor * The color/visual context descriprot, or NULL
* on error.
*/
#ifdef __STDC__
PgxColor *pgx_new_visual(PgxWin *pgx, char *class_name, int min_col,
int max_col)
#else
PgxColor *pgx_new_visual(pgx, class_name, min_col, max_col)
PgxWin *pgx; char *class_name; int min_col; int max_col;
#endif
{
PgxColor *color = NULL; /* The new visual/colormap context descriptor */
int default_class; /* The class of the default visual */
int visual_class; /* The desired visual class */
if(!pgx_ready(pgx, 0))
return NULL;
/*
* Limit the number of colors to allowed values.
*/
if(min_col < 0)
min_col = 0;
else if(min_col > 256)
min_col = 256;
if(max_col < 0)
max_col = 0;
else if(max_col > 256)
max_col = 256;
/*
* Determine the class of the default visual.
*/
default_class = pgx_default_class(pgx);
/*
* Decode the desired visual-class type.
*/
visual_class = pgx_parse_visual(class_name);
/*
* Use a specific visual class?
*/
if(visual_class >= 0)
color = pgx_find_visual(pgx, visual_class, min_col, max_col);
/*
* Should we perform a search for a suitable visual class?
*/
if(visual_class == -1 || !color) {
/*
* Color display?
*/
switch(default_class) {
case PseudoColor:
case StaticColor:
case DirectColor:
case TrueColor:
color = pgx_find_visual(pgx, PseudoColor, min_col, max_col);
if(!color)
color = pgx_find_visual(pgx, StaticColor, min_col, max_col);
if(!color)
color = pgx_find_visual(pgx, TrueColor, min_col, max_col);
break;
/*
* Gray-scale display?
*/
case GrayScale:
case StaticGray:
color = pgx_find_visual(pgx, GrayScale, min_col, max_col);
if(!color)
color = pgx_find_visual(pgx, StaticGray, min_col, max_col);
break;
};
};
/*
* If requested, or we failed to acquire the desired visual
* use black and white colors from the default visual.
*/
return color ? color : pgx_bw_visual(pgx);
}
/*.......................................................................
* Return a black and white visual/color context descriptor.
*
* Input:
* pgx PgxWin * The PGPLOT window context (We need the display
* and screen members).
* Output:
* return PgxColor * The context descriptor, or NULL on error.
*/
#ifdef __STDC__
PgxColor *pgx_bw_visual(PgxWin *pgx)
#else
PgxColor *pgx_bw_visual(pgx)
PgxWin *pgx;
#endif
{
PgxColor *color; /* The new descriptor */
if(!pgx_ready(pgx, 0))
return NULL;
/*
* Allocate the context descriptor.
*/
color = new_PgxColor(pgx, 2);
if(!color)
return NULL;
/*
* Get the visual-info context of the default visual.
*/
color->vi = pgx_visual_info(pgx->display, pgx->screen,
XVisualIDFromVisual(DefaultVisual(pgx->display, pgx->screen)));
if(!color->vi)
return del_PgxColor(pgx, color);
/*
* Record the default-colormap ID.
*/
color->cmap = DefaultColormap(pgx->display, pgx->screen);
/*
* Record the two available colors.
*/
color->pixel[0] = BlackPixel(pgx->display, pgx->screen);
color->pixel[1] = WhitePixel(pgx->display, pgx->screen);
color->ncol = 2;
/*
* Record the fact that we are using monochrome.
*/
color->monochrome = 1;
/*
* Install the color context.
*/
pgx->color = color;
/*
* Initialize the colors.
*/
if(pgx_init_colors(pgx))
return pgx_del_visual(pgx);
return color;
}
/*.......................................................................
* Return a visual/color context descriptor for colors allocated from
* the default colormap of the screen.
*
* Input:
* pgx PgxWin * The PGPLOT window context (We need the display
* and screen members).
* min_col int The minimum acceptable number of colors before
* switching to monochrome.
* max_col int The maximum number of colors to allocate.
* Output:
* return PgxColor * The context descriptor, or NULL on error.
*/
#ifdef __STDC__
PgxColor *pgx_default_visual(PgxWin *pgx, int min_col, int max_col)
#else
PgxColor *pgx_default_visual(pgx, min_col, max_col)
PgxWin *pgx; int min_col; int max_col;
#endif
{
PgxColor *color; /* The new descriptor */
if(!pgx_ready(pgx, 0))
return NULL;
/*
* Limit the number of colors to allowed values.
*/
if(min_col < 0)
min_col = 0;
else if(min_col > 256)
min_col = 256;
if(max_col < 0)
max_col = 0;
else if(max_col > 256)
max_col = 256;
/*
* Allocate the context descriptor.
*/
color = new_PgxColor(pgx, max_col);
if(!color)
return NULL;
/*
* Get the visual-info context of the default visual.
*/
color->vi = pgx_visual_info(pgx->display, pgx->screen,
XVisualIDFromVisual(DefaultVisual(pgx->display, pgx->screen)));
if(!color->vi)
return del_PgxColor(pgx, color);
/*
* Record the default-colormap ID.
*/
color->cmap = DefaultColormap(pgx->display, pgx->screen);
/*
* Attempt to allocate colorcells from the default colormap.
* If this fails return a visual that uses the black and white
* pixels of the default visual.
*/
if(pgx_get_colorcells(pgx, color, min_col, max_col)) {
color = del_PgxColor(pgx, color);
return pgx_bw_visual(pgx);
};
/*
* Record the fact that we haven't reverted to monochrome.
*/
color->monochrome = 0;
/*
* Install the color context.
*/
pgx->color = color;
/*
* Initialize the colors.
*/
if(pgx_init_colors(pgx))
return pgx_del_visual(pgx);
return color;
}
/*.......................................................................
* Allocate colors from and return a visual/colormap context descriptor
* for a specified existing visual and colormap.
*
* Input:
* pgx PgxWin * The PGPLOT window context (We need the display
* and screen members).
* vid VisualID The ID of the visual to be used. (Note that
* XGetVisualIDFromVisual() can be used to get the
* visual ID of a visual from a (Visual *).
* cmap Colormap The colormap to be used. This must be compatible
* with the specified visual.
* min_col int The minimum acceptable number of colors before
* switching to monochrome.
* max_col int The maximum number of colors to allocate.
* Output:
* return PgxColor * The context descriptor, or NULL on error.
*/
#ifdef __STDC__
PgxColor *pgx_adopt_visual(PgxWin *pgx, VisualID vid, Colormap cmap,
int min_col, int max_col)
#else
PgxColor *pgx_adopt_visual(pgx, vid, cmap, min_col, max_col)
PgxWin *pgx; VisualID vid; Colormap cmap; int min_col; int max_col;
#endif
{
PgxColor *color; /* The new descriptor */
if(!pgx_ready(pgx, 0))
return NULL;
/*
* Limit the number of colors to allowed values.
*/
if(min_col < 0)
min_col = 0;
else if(min_col > 256)
min_col = 256;
if(max_col < 0)
max_col = 0;
else if(max_col > 256)
max_col = 256;
/*
* Allocate the context descriptor.
*/
color = new_PgxColor(pgx, max_col);
if(!color)
return NULL;
/*
* Enquire about the specified visual.
*/
color->vi = pgx_visual_info(pgx->display, pgx->screen, vid);
if(!color->vi)
return del_PgxColor(pgx, color);
/*
* Record the colormap ID.
*/
color->cmap = cmap;
/*
* Attempt to allocate colors from the colormap.
*/
if(pgx_get_colorcells(pgx, color, min_col, max_col))
return del_PgxColor(pgx, color);
/*
* Install the color context.
*/
pgx->color = color;
/*
* Initialize the colors.
*/
if(pgx_init_colors(pgx))
return pgx_del_visual(pgx);
return color;
}
/*.......................................................................
* Allocate colors from and return a visual/colormap context descriptor
* describing the visual and colormap of a specified other window.
*
* Input:
* pgx PgxWin * The PGPLOT window context (We need the display
* and screen members).
* w Window The window to inherit the colormap and visual from.
* min_col int The minimum acceptable number of colors before
* switching to monochrome.
* max_col int The maximum number of colors to allocate.
* Output:
* return PgxColor * The context descriptor, or NULL on error.
*/
#ifdef __STDC__
PgxColor *pgx_window_visual(PgxWin *pgx, Window w, int min_col, int max_col)
#else
PgxColor *pgx_window_visual(pgx, w, min_col, max_col)
PgxWin *pgx; Window w; int min_col; int max_col;
#endif
{
XWindowAttributes attr; /* The attributes of the specified window */
if(!pgx_ready(pgx, 0))
return NULL;
/*
* Acquire the attributes of the specified window.
*/
if(!XGetWindowAttributes(pgx->display, w, &attr)) {
fprintf(stderr,
"%s: (pgx_window_visual) Unable to get attributes of window: 0x%lx.\n",
PGX_IDENT, (unsigned long) w);
return NULL;
};
/*
* Install the visual and colormap recorded in the returned attributes.
*/
return pgx_adopt_visual(pgx, XVisualIDFromVisual(attr.visual), attr.colormap,
min_col, max_col);
}
/*.......................................................................
* Private function of pgx_new_visual(), used to find a visual of a given
* class and at least min_col colors, allocate colors and return a
* visual/colormap context descriptor for it.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* class int The type of colormap required, chosen from:
* PseudoColor,StaticColor,GrayScale,StaticGray.
* min_col int The minimum acceptable number of colors before
* switching to monochrome.
* max_col int The maximum number of colors to allocate.
* Input/Output:
* return PgxColor * The context of the new visual/colormap, or NULL
* if sufficient colors could not be obtained.
*/
#ifdef __STDC__
static PgxColor *pgx_find_visual(PgxWin *pgx, int class, int min_col,
int max_col)
#else
static PgxColor *pgx_find_visual(pgx, class, min_col, max_col)
PgxWin *pgx; int class; int min_col; int max_col;
#endif
{
PgxColor *color = NULL; /* The colormap/visual context to be returned */
XVisualInfo vi_template; /* Visual search template */
XVisualInfo *vi_list = NULL; /* List of matching visuals */
int nmatch; /* Number of matching visuals in vi_list[] */
VisualID vid; /* The id of a chosen private visual */
/*
* If the default colormap has the right class, see if sufficient
* colors can be allocated from it.
*/
if(class == pgx_default_class(pgx)) {
color = pgx_default_visual(pgx, min_col, max_col);
if(color->ncol >= max_col)
return color;
else
color = del_PgxColor(pgx, color);
};
/*
* We need a private colormap.
* Get a list of all visuals of the requested class.
*/
vi_template.class = class;
vi_list = XGetVisualInfo(pgx->display, (long)VisualClassMask, &vi_template,
&nmatch);
if(!vi_list)
return NULL;
/*
* Search the list for a visual that has a colormap size that
* best matches max_col. Note that the colormap_size memeber of
* the visual info structure effectively provides the number of
* "independant" color table entries. Thus the following algorithm
* works even for colormaps of TrueColor and DirectColor where the
* colormap_size attribute refers to the size of a single primary color
* table.
*/
{
XVisualInfo *vi_below = NULL;
XVisualInfo *vi_above = NULL;
XVisualInfo *vi = NULL;
for(vi=vi_list; vi<vi_list+nmatch; vi++) {
if(vi->colormap_size < max_col) {
if(!vi_below || vi->colormap_size > vi_below->colormap_size)
vi_below = vi;
} else {
if(!vi_above || vi->colormap_size < vi_above->colormap_size)
vi_above = vi;
};
};
/*
* If available, use a visual that has at least max_col independant
* colors.
*/
if(vi_above)
vi = vi_above;
else if(vi_below)
vi = vi_below;
else
vi = NULL;
/*
* Get the ID of the visual if suitable.
*/
vid = (vi && vi->colormap_size > 2) ? vi->visualid : None;
XFree((char *) vi_list);
/*
* Did we fail to get a usable visual?
*/
if(vid == None)
return NULL;
};
/*
* Allocate a visual/colormap context descriptor.
*/
color = new_PgxColor(pgx, max_col);
if(!color)
return NULL;
/*
* Get a new visual-info descriptor for the visual.
*/
color->vi = pgx_visual_info(pgx->display, pgx->screen, vid);
if(!color->vi)
return del_PgxColor(pgx, color);
/*
* Bracket the colormap acquisition with pgx_start/end_error() calls, to
* determine whether any allocation errors occur.
*/
pgx_start_error_watch(pgx);
color->cmap = XCreateColormap(pgx->display, DefaultRootWindow(pgx->display),
color->vi->visual, AllocNone);
if(pgx_end_error_watch(pgx) || color->cmap == None) {
fprintf(stderr,
"%s: XCreateColormap failed for visual: id=0x%lx class=%d depth=%u.\n",
PGX_IDENT, (unsigned long)color->vi->visualid, color->vi->class,
color->vi->depth);
color->cmap = None;
return del_PgxColor(pgx, color);
};
/*
* Allocate color-cells in the new colormap.
*/
if(pgx_get_colorcells(pgx, color, min_col, max_col))
return del_PgxColor(pgx, color);
color->private = 1;
color->monochrome = 0;
/*
* Install the color context.
*/
pgx->color = color;
/*
* Initialize the colors.
*/
if(pgx_init_colors(pgx))
return pgx_del_visual(pgx);
return color;
}
/*.......................................................................
* Private function of pgx_find_visual(), used to allocate color cells for a
* given colormap and return a count of the number allocated.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* color PgxColor * The visual/colormap context descriptor.
* The cmap and vi fields must be initialized before
* calling this function.
* min_col int The minimum acceptable number of colors.
* max_col int The maximum number of colors to allocate.
* Output:
* color->pixel[] The colorcell indexes.
* color->ncol The number of color pixels to use from color->pixel[].
* color->npixel The number of private color-cells allocated in pixel[].
* return int 0 - OK.
* 1 - Unable to acquire at least min_col colors.
*/
#ifdef __STDC__
static int pgx_get_colorcells(PgxWin *pgx, PgxColor *color,
int min_col, int max_col)
#else
static int pgx_get_colorcells(pgx, color, min_col, max_col)
PgxWin *pgx; PgxColor *color; int min_col; int max_col;
#endif
{
XVisualInfo *vi; /* Visual information (color->vi) */
Colormap cmap; /* Colormap ID (color->cmap) */
unsigned long maxcol; /* The max number of cells to attempt to allocate */
int ncol; /* The number of color-cells allocated */
/*
* Get local pointers to relevant parts of the color context
* descriptor.
*/
vi = color->vi;
cmap = color->cmap;
/*
* Record the fact that no colors have been allocated.
*/
color->ncol = color->npixel = 0;
/*
* Determine the number of color cells in the colormap.
*/
switch(vi->class) {
case PseudoColor:
case GrayScale:
case StaticColor:
case StaticGray:
maxcol = vi->colormap_size;
break;
case TrueColor:
case DirectColor:
/*
* Determine the maximum number of significant colors available
* by looking at the total number of bits set in the pixel bit-masks.
*/
maxcol = 1;
{
unsigned long rgb_mask = (vi->red_mask | vi->green_mask | vi->blue_mask);
do {
if(rgb_mask & (unsigned long)0x1)
maxcol <<= (unsigned long)1;
} while(maxcol < max_col && (rgb_mask >>= (unsigned long)1) != 0);
};
break;
default:
maxcol = 0;
break;
};
/*
* Limit the number of colorcells to the size of the color->pixel[] array.
*/
if(maxcol > max_col)
maxcol = max_col;
/*
* Don't try to allocate anything if there are too few colors available.
*/
if(maxcol < min_col) {
ncol = 0;
} else {
unsigned long planes[1];
unsigned int nplanes = 0;
/*
* Dynamic colormaps require one to allocate cells explicitly.
* Allocate up to maxcol color cells.
*/
switch(vi->class) {
case PseudoColor:
case GrayScale:
case DirectColor:
/*
* See if we can get all of the colors requested.
*/
if(XAllocColorCells(pgx->display, cmap, False, planes, nplanes,
color->pixel, (unsigned) maxcol)) {
ncol = maxcol;
/*
* If there aren't at least min_col color cells available, then
* give up on this colormap.
*/
} else if(!XAllocColorCells(pgx->display, cmap, False, planes, nplanes,
color->pixel, (unsigned) min_col)) {
ncol = 0;
} else {
/*
* Since we were able to allocate min_col cells, we may be able to
* allocate more. First discard the min_col cells, so that we can
* try for a bigger number.
*/
XFreeColors(pgx->display, cmap, color->pixel, (int) min_col,
(unsigned long)0);
/*
* Since there is no direct method to determine the number of allocatable
* color cells available in a colormap, perform a binary search for the
* max number that can be allocated. Note that it is possible that another
* client may allocate colors from the same colormap while we search. This
* invalidates the result of the search and is the reason for the outer
* while loop.
*/
ncol = 0;
do {
int lo = min_col;
int hi = maxcol;
while(lo<=hi) {
int mid = (lo+hi)/2;
if(XAllocColorCells(pgx->display, cmap, False, planes, nplanes,
color->pixel, (unsigned) mid)) {
ncol = mid;
lo = mid + 1;
XFreeColors(pgx->display, cmap, color->pixel, mid,
(unsigned long)0);
} else {
hi = mid - 1;
};
};
} while(ncol >= min_col &&
!XAllocColorCells(pgx->display, cmap, False, planes, nplanes,
color->pixel, (unsigned) ncol));
};
/*
* Record the number of allocated cells so that del_PgxColor() knows
* how many to free.
*/
if(ncol >= min_col)
color->npixel = ncol;
break;
/*
* For static color maps, color-cell pixel indexes will be assigned later
* with XAllocColor() in pgx_set_rgb(). For now simply assign 0 to all
* pixels.
*/
case StaticColor:
case TrueColor:
case StaticGray:
for(ncol=0; ncol<maxcol; ncol++)
color->pixel[ncol] = 0;
ncol = maxcol;
color->npixel = 0; /* No pixels needed to be allocated */
break;
default:
ncol = 0;
break;
};
};
/*
* Too few colors?
*/
if(ncol < min_col)
return 1;
/*
* Record what we got.
*/
color->ncol = ncol;
color->monochrome = 0;
return 0;
}
/*.......................................................................
* Return a dynamically allocated visual info structure for a given
* visual. This is simply a more convenient interface to XGetVisualInfo()
* and XVisualIDFromVisual().
*
* Input:
* display Display * The display connection to which the visual
* belongs.
* screen int The screen to which the visual belongs.
* vid VisualID The ID of the visual for which information is
* required. Note that the ID of a visual can
* be obtained from XVisualIDFromVisual().
* Output:
* return XVisualInfo * The required information descriptor, or NULL
* on error.
*/
#ifdef __STDC__
static XVisualInfo *pgx_visual_info(Display *display, int screen, VisualID vid)
#else
static XVisualInfo *pgx_visual_info(display, screen, vid)
Display *display; int screen; VisualID vid;
#endif
{
XVisualInfo *vi=NULL; /* The return descriptor */
XVisualInfo template; /* The search template */
int nret = 0; /* The number of descriptors returned */
/*
* Using the visual ID and the screen should unambiguously select the
* information for the specified visual.
*/
template.visualid = vid;
template.screen = screen;
vi = XGetVisualInfo(display, (long)(VisualIDMask | VisualScreenMask),
&template, &nret);
if(vi == NULL || nret < 1) {
fprintf(stderr,
"%s: Error getting visual information for visual ID 0x%lx, screen %d.\n",
PGX_IDENT, (unsigned long)template.visualid, screen);
vi = NULL;
};
return vi;
}
/*.......................................................................
* Delete the contents of a pgx->color structure.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* return PgxColor * The deleted color context (always NULL).
*/
#ifdef __STDC__
PgxColor *pgx_del_visual(PgxWin *pgx)
#else
PgxColor *pgx_del_visual(pgx)
PgxWin *pgx;
#endif
{
if(pgx)
pgx->color = del_PgxColor(pgx, pgx->color);
return NULL;
}
/*.......................................................................
* Check a resource string value against visual class names.
*
* Input:
* str char * The string value to be tested (NULL is ok).
* Output:
* return int The Visual class parsed, or -1 to select the default.
*/
#ifdef __STDC__
static int pgx_parse_visual(char *str)
#else
static int pgx_parse_visual(str)
char *str;
#endif
{
/*
* Create a lookup table of recognised visual classes.
*/
static struct {
char *name; /* Name of visual class */
int class; /* Enumerated identifier of visual class */
} classes[] = {
{"monochrome", -2},
{"default", -1},
{"pseudocolor", PseudoColor},
{"directcolor", TrueColor}, /* We can't handle DirectColor */
{"staticcolor", StaticColor},
{"truecolor", TrueColor},
{"grayscale", GrayScale},
{"staticgray", StaticGray}
};
int i;
/*
* Lookup the given class name.
*/
if(str) {
for(i=0; i<sizeof(classes)/sizeof(classes[0]); i++) {
if(pgx_same_string(str, classes[i].name))
return classes[i].class;
};
/*
* Class name not recognised.
*/
fprintf(stderr, "%s: Unrecognised visual type: \"%s\".\n",
PGX_IDENT, str);
};
return -1;
}
/*.......................................................................
* Perform a case-insensitive string comparison. Leading and trailing
* white-space are not significant.
*
* Input:
* s1,s2 char * The strings to be compared.
* Output:
* return int 0 - The strings are not the same.
* 1 - The strings are the same.
*/
#ifdef __STDC__
int pgx_same_string(char *s1, char *s2)
#else
int pgx_same_string(s1, s2)
char *s1; char *s2;
#endif
{
/*
* If either of the strings are NULL pointers, report that they are equal
* only if both are NULL.
*/
if(s1==NULL || s2==NULL)
return s1==NULL && s2==NULL;
/*
* Skip leading white-space.
*/
while(*s1 && isspace(*s1))
s1++;
while(*s2 && isspace(*s2))
s2++;
/*
* Find the section of the strings that are identical.
*/
while(*s1 && *s2 &&
(islower(*s1) ? toupper(*s1) : *s1) == (islower(*s2) ? toupper(*s2) : *s2)) {
s1++;
s2++;
};
/*
* Skip trailing white-space.
*/
while(*s1 && isspace(*s1))
s1++;
while(*s2 && isspace(*s2))
s2++;
/*
* Are the strings equal?
*/
return (*s1=='\0' && *s2=='\0');
}
/*.......................................................................
* After a fatal error has occured, this function should be called to
* mark the specified device as unusable. It emits an error message
* and sets pgx->bad_device=1.
*
* Input:
* pgx PgxWin * The descriptor of the device on which the error
* occurred.
* Output:
* pgx->bad_device This flag is set to 1.
* return int Allways 1 (intended as a boolean to say that the
* device is unusable). This can be used as the return
* value for functions that use 1 to denote an error
* return. eg.
* if(error_occurred)
* return pgx_bad_device(pgx);
*/
#ifdef __STDC__
int pgx_bad_device(PgxWin *pgx)
#else
int pgx_bad_device(pgx)
PgxWin *pgx;
#endif
{
/*
* Only report an error if this is the first time that this function
* has been called on this device.
*/
if(pgx && !pgx->bad_device) {
fprintf(stderr, "%s: Lost PGPLOT window.\n", PGX_IDENT);
pgx->bad_device = 1;
};
return 1;
}
/*.......................................................................
* Draw a line segment in an open PGPLOT window.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* rbuf float * The array of float arguments sent by the PGPLOT
* GREXEC() subroutine.
*/
#ifdef __STDC__
void pgx_draw_line(PgxWin *pgx, float *rbuf)
#else
void pgx_draw_line(pgx, rbuf)
PgxWin *pgx; float *rbuf;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP)) {
PgxState *state = pgx->state;
/*
* Convert from PGPLOT coordinates to X coordinates.
*/
XPoint start;
XPoint end;
pgx_xy_to_XPoint(pgx, &rbuf[0], &start);
pgx_xy_to_XPoint(pgx, &rbuf[2], &end);
/*
* Draw the line segment.
*/
XDrawLine(pgx->display, pgx->pixmap, state->gc,
start.x, start.y, end.x, end.y);
/*
* Record the extent of the modified region of the pixmap.
*/
pgx_mark_modified(pgx, start.x, start.y, state->gcv.line_width);
pgx_mark_modified(pgx, end.x, end.y, state->gcv.line_width);
};
return;
}
/*.......................................................................
* Draw a single dot in an open PGPLOT window.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* rbuf float * The array of float arguments sent by the PGPLOT
* GREXEC() subroutine.
*/
#ifdef __STDC__
void pgx_draw_dot(PgxWin *pgx, float *rbuf)
#else
void pgx_draw_dot(pgx, rbuf)
PgxWin *pgx; float *rbuf;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP)) {
PgxState *state = pgx->state;
XPoint xp;
/*
* Workd out the radius of the dot.
*/
int radius = state->gcv.line_width/2;
/*
* Convert from PGPLOT coordinates to window coordinates.
*/
pgx_xy_to_XPoint(pgx, rbuf, &xp);
/*
* Draw a pixel-sized point, or a larger circular dot?
*/
if(radius < 1) {
XDrawPoint(pgx->display, pgx->pixmap, state->gc, xp.x, xp.y);
} else {
unsigned int diameter = radius*2;
int x = xp.x - radius;
int y = xp.y - radius;
XFillArc(pgx->display, pgx->pixmap, state->gc, x, y,
diameter, diameter, 0, 23040);
};
/*
* Record the extent of the modified region of the pixmap.
*/
pgx_mark_modified(pgx, xp.x, xp.y, state->gcv.line_width);
};
return;
}
/*.......................................................................
* Convert from the coordinates sent by PGPLOT in rbuf[...] to an
* X-windows point in the coordinate system of the pixmap.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* xy float [2] Array of two floats containing PGPLOT coordinates
* arranged as x followed by y.
* Output:
* xp XPoint * The converted coordinates will be assigned to xp->x
* and xp->y.
*/
#ifdef __STDC__
static void pgx_xy_to_XPoint(PgxWin *pgx, float *xy, XPoint *xp)
#else
static void pgx_xy_to_XPoint(pgx, xy, xp)
PgxWin *pgx; float *xy; XPoint *xp;
#endif
{
PgxState *state = pgx->state;
float x = xy[0];
float y = xy[1];
/*
* Limit the coordinates to lie within the pixmap.
*/
if(x < 0)
x = 0;
if(x >= state->geom.width)
x = state->geom.width;
if(y < 0)
y = 0;
if(y >= state->geom.height)
y = state->geom.height;
/*
* Convert to pixmap coordinates.
*/
xp->x = state->geom.xmin + (int)(x + 0.5);
xp->y = state->geom.ymax - (int)(y + 0.5);
}
/*.......................................................................
* Convert from pixmap pixel coordinates to PGPLOT coordinates, in a
* form that can be returned to PGPLOT via rbuf[...].
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* xp XPoint * The pixmap pixel-coordinates to be converted.
* Output:
* xy float [2] Output array of two floats in which to place the
* PGPLOT coordinates, arranged as x followed by y.
*/
#ifdef __STDC__
static void pgx_XPoint_to_xy(PgxWin *pgx, XPoint *xp, float *xy)
#else
static void pgx_XPoint_to_xy(pgx, xp, xy)
PgxWin *pgx; XPoint *xp; float *xy;
#endif
{
PgxState *state = pgx->state;
xy[0] = (float) (xp->x - state->geom.xmin);
xy[1] = (float) (state->geom.ymax - xp->y);
}
/*.......................................................................
* Update the vertices of the rectangular area that has been modified
* since the last time the window was updated from the pixmap.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* x int The x-axis pixel index that the rectangular update area
* must be extended to include.
* y int The y-axis pixel index that the rectangular update area
* must be extended to include.
* diameter int The diameter of the locus in pixels. For line or
* point drawing operations this is usually the line width.
*/
#ifdef __STDC__
static void pgx_mark_modified(PgxWin *pgx, int x, int y, int diameter)
#else
static void pgx_mark_modified(pgx, x, y, diameter)
PgxWin *pgx; int x; int y; int diameter;
#endif
{
PgxState *state = pgx->state;
int radius = diameter/2;
/*
* Expand the current rectangle to include point (x,y).
*/
if(state->update.modified) {
if(x - radius < state->update.xmin)
state->update.xmin = x - radius;
if(x + radius > state->update.xmax)
state->update.xmax = x + radius;
if(y - radius < state->update.ymin)
state->update.ymin = y - radius;
if(y + radius > state->update.ymax)
state->update.ymax = y + radius;
} else {
state->update.xmin = x - radius;
state->update.xmax = x + radius;
state->update.ymin = y - radius;
state->update.ymax = y + radius;
state->update.modified = 1;
};
return;
}
/*.......................................................................
* Flush changes in the pixmap to the window.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_flush(PgxWin *pgx)
#else
int pgx_flush(pgx)
PgxWin *pgx;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP)) {
PgxState *state = pgx->state;
/*
* Flush buffered opcodes if necessary.
*/
if(state->flush_opcode_fn != (Flush_Opcode_fn) 0) {
(*state->flush_opcode_fn)(pgx);
state->flush_opcode_fn = (Flush_Opcode_fn) 0;
if(pgx->bad_device)
return 1;
};
/*
* Copy the modified rectangular area of the pixmap to the PGPLOT window.
*/
if(state->update.modified) {
/*
* Enforce bounds on the area to be updated.
*/
if(state->update.xmin < 0)
state->update.xmin = 0;
if(state->update.ymin < 0)
state->update.ymin = 0;
if(state->update.xmax > state->geom.width - 1)
state->update.xmax = state->geom.width - 1;
if(state->update.ymax > state->geom.height - 1)
state->update.ymax = state->geom.height - 1;
/*
* Copy the area to be updated from the pixmap to the window.
*/
if(!pgx->bad_device) {
pgx_copy_area(pgx, state->update.xmin, state->update.ymin,
(unsigned) (state->update.xmax - state->update.xmin + 1),
(unsigned) (state->update.ymax - state->update.ymin + 1),
(int) (state->update.xmin - pgx->scroll.x),
(int) (state->update.ymin - pgx->scroll.y));
if(pgx->bad_device)
return 1;
};
state->update.modified = 0;
};
/*
* Redraw the potentially damaged rubber-band cursor if it is active.
*/
pgx_refresh_cursor(pgx);
/*
* Make sure that the window is up to date.
*/
XFlush(pgx->display);
if(pgx->bad_device)
return 1;
};
return 0;
}
/*.......................................................................
* Set the foreground color.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* ci int The PGPLOT color index to instate as the foreground
* color.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_set_ci(PgxWin *pgx, int ci)
#else
int pgx_set_ci(pgx, ci)
PgxWin *pgx; int ci;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
PgxState *state = pgx->state;
/*
* Assign white to out-of range color indexes.
*/
if(ci < 0 || ci >= pgx->color->ncol)
ci = 1;
/*
* Determine the color pixel associated with the given color index.
*/
state->gcv.foreground = pgx->color->pixel[ci];
/*
* Instate the new foreground color.
*/
XSetForeground(pgx->display, state->gc, state->gcv.foreground);
if(pgx->bad_device)
return 1;
};
return 0;
}
/*.......................................................................
* This function is called mulitple times to accumulate a list of
* polygon vertices and finally draw them. This protocol is mandated
* by the PGPLOT GREXEC driver dispatch function.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* rbuf float * The array of float arguments sent by the PGPLOT
* GREXEC() subroutine.
*/
#ifdef __STDC__
void pgx_poly_fill(PgxWin *pgx, float *rbuf)
#else
void pgx_poly_fill(pgx, rbuf)
PgxWin *pgx; float *rbuf;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP)) {
PgxState *state = pgx->state;
/*
* The first call specifies just the number of vertixes in the polygon.
*/
if(state->poly.npoint == 0) {
state->poly.npoint = (int) (rbuf[0] + 0.5);
state->poly.points = (XPoint *) malloc(sizeof(XPoint) * state->poly.npoint);
if(state->poly.points == NULL)
fprintf(stderr, "%s: Insufficient memory for polygon points.\n",
PGX_IDENT);
state->poly.ndone = 0;
/*
* The next state->poly.npoint calls specify the vertexes of the polygon.
*/
} else {
/*
* Ignore the points if the above malloc() failed.
*/
if(state->poly.points) {
XPoint *xp = &state->poly.points[state->poly.ndone];
pgx_xy_to_XPoint(pgx, rbuf, xp);
pgx_mark_modified(pgx, xp->x, xp->y, 1);
};
/*
* Maintain the count of the number of points, even if no memory for the
* points is available. Thus we can just ignore all calls until
* state->poly.ndone == state->poly.npoint.
*/
state->poly.ndone++;
/*
* On the last call display the filled polygon and release the memory used
* to store its vertexes.
*/
if(state->poly.ndone >= state->poly.npoint) {
if(state->poly.points) {
XFillPolygon(pgx->display, pgx->pixmap, state->gc, state->poly.points,
state->poly.npoint, Complex, CoordModeOrigin);
free((char *)state->poly.points);
state->poly.points = NULL;
};
state->poly.npoint = 0;
};
};
};
return;
}
/*.......................................................................
* Draw a filled rectangle.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* rbuf float * The array of float arguments sent by the PGPLOT
* GREXEC() subroutine.
*/
#ifdef __STDC__
void pgx_rect_fill(PgxWin *pgx, float *rbuf)
#else
void pgx_rect_fill(pgx, rbuf)
PgxWin *pgx; float *rbuf;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP)) {
/*
* Convert from PGPLOT coordinates to X coordinates.
*/
XPoint blc;
XPoint trc;
pgx_xy_to_XPoint(pgx, &rbuf[0], &blc);
pgx_xy_to_XPoint(pgx, &rbuf[2], &trc);
/*
* Fill the rectangle in the pixmap.
*/
XFillRectangle(pgx->display, pgx->pixmap, pgx->state->gc, blc.x, trc.y,
(unsigned)(trc.x-blc.x+1), (unsigned)(blc.y-trc.y+1));
/*
* Record the extent of the modified part of the pixmap.
*/
pgx_mark_modified(pgx, blc.x, blc.y, 1);
pgx_mark_modified(pgx, trc.x, trc.y, 1);
};
return;
}
/*.......................................................................
* Set the line width for subsequent drawing.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* lw int The new line width in units of 0.005 inches.
*/
#ifdef __STDC__
void pgx_set_lw(PgxWin *pgx, int lw)
#else
void pgx_set_lw(pgx, lw)
PgxWin *pgx; int lw;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
PgxState *state = pgx->state;
/*
* The line width is provided in multiples of 0.005 inches.
*/
state->gcv.line_width = lw * 0.005 * state->geom.xpix_per_inch;
XChangeGC(pgx->display, state->gc, (unsigned long)GCLineWidth, &state->gcv);
};
return;
}
/*.......................................................................
* Render a line of pixels.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* rbuf float * The array of float arguments sent by the PGPLOT
* GREXEC() subroutine.
* nbuf int * The number of floats passed in rbuf[] by GREXEC.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_pix_line(PgxWin *pgx, float *rbuf, int *nbuf)
#else
int pgx_pix_line(pgx, rbuf, nbuf)
PgxWin *pgx; float *rbuf; int *nbuf;
#endif
{
int i;
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP) &&
!pgx->color->monochrome) {
PgxState *state = pgx->state; /* The PGPLOT drawing-state context */
int ndone; /* The number of pixels drawn so far */
/*
* Extract the array of pixels and the recorded number thereof.
*/
float *cells = rbuf + 2; /* Array of pixels */
int ncell = *nbuf - 2; /* The number of pixels in cells[] */
/*
* Get the container of the X image used to transport lines of pixels.
*/
XWimage *image = &pgx->state->image;
/*
* The first two elements of the rbuf[] array contain the
* X and Y PGPLOT coordinates of the start of the line of pixels.
* Convert this to X coordinates.
*/
XPoint start;
pgx_xy_to_XPoint(pgx, rbuf, &start);
/*
* Draw up to PGX_IMAGE_LEN pixels at a time. This is the size of the
* buffer: state->xi->data[].
*/
for(ndone=0; !pgx->bad_device && ndone<ncell; ndone += image->npix) {
int ntodo = ncell-ndone;
int nimage = ntodo < image->npix ? ntodo : image->npix;
/*
* Load the image buffer with the color cell indexes assigned to the
* given PGPLOT color indexes.
*/
if(pgx->color->vi->depth == 8) {
for(i=0; i<nimage; i++)
image->buff[i] = pgx->color->pixel[(int) (cells[ndone+i] + 0.5)];
} else {
for(i=0; i<nimage; i++) {
XPutPixel(image->xi, i, 0,
pgx->color->pixel[(int) (cells[ndone+i] + 0.5)]);
};
};
/*
* Display the image.
*/
XPutImage(pgx->display, pgx->pixmap, state->gc, image->xi, 0, 0,
start.x+ndone, start.y, (unsigned) nimage, (unsigned) 1);
};
/*
* Extend the region to be updated on the next flush.
*/
pgx_mark_modified(pgx, start.x, start.y, 1);
pgx_mark_modified(pgx, start.x + ncell - 1, start.y, 1);
};
if(pgx->bad_device)
return 1;
return 0;
}
/*.......................................................................
* Record the latest world-coordinate conversion parameters as provided
* by the PGPLOT driver opcode 27. Note that opcode 27 only gets invoked
* by PGPLOT if the second character of the attribute string returned by
* opcode 4 is set to 'X'.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* rbuf float * The array of float arguments sent by the PGPLOT
* GREXEC() subroutine for opcode 27. In order these
* are: xoff, xdiv, yoff and ydiv; where:
* world_x = (device_x - xoff) / xdiv
* world_y = (device_y - yoff) / ydiv
*/
#ifdef __STDC__
void pgx_set_world(PgxWin *pgx, float *rbuf)
#else
void pgx_set_world(pgx, rbuf)
PgxWin *pgx; float *rbuf;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
XWworld *world = &pgx->state->world;
world->xoff = rbuf[0];
world->xdiv = rbuf[1];
world->yoff = rbuf[2];
world->ydiv = rbuf[3];
};
}
/*.......................................................................
* Assign a given RGB color representation to a given color index in
* pgx->color->xcolor and if the device is open to PGPLOT, record the
* extent of the modifications in pgx->state->update and
* register pgx_update_colors() to pgx->state->flush_opcode_fn().
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* ci int The color index to assign the color to. Out of range
* indexes are quietly ignored.
* red float The fractional red brightness 0..1.
* green float The fractional green brightness 0..1.
* blue float The fractional blue brightness 0..1.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_set_rgb(PgxWin *pgx, int ci, float red, float green, float blue)
#else
int pgx_set_rgb(pgx, ci, red, green, blue)
PgxWin *pgx; int ci; float red; float green; float blue;
#endif
{
float gray; /* Gray-scale intensity */
XColor *xc; /* The descriptor of the new color */
/*
* Do we have a valid device.
*/
if(pgx_ready(pgx, PGX_NEED_COLOR)) {
PgxState *state = pgx->state;
/*
* Limit RGB values to be between 0 and 1.
*/
if(red < 0.0) red = 0.0;
if(green < 0.0) green = 0.0;
if(blue < 0.0) blue = 0.0;
if(red > 1.0) red = 1.0;
if(green > 1.0) green = 1.0;
if(blue > 1.0) blue = 1.0;
/*
* Color index in range?
*/
if(!pgx->color->monochrome && ci >= 0 && ci < pgx->color->ncol) {
/*
* Get the color representation descriptor.
*/
xc = &pgx->color->xcolor[ci];
/*
* Get the pixel to be assigned the new color representation.
*/
xc->pixel = pgx->color->pixel[ci];
xc->flags = DoRed | DoGreen | DoBlue;
xc->pad = 0;
/*
* Determine the appropriate RGB values for the type of colormap.
*/
switch(pgx->color->vi->class) {
case PseudoColor:
case StaticColor:
case DirectColor:
case TrueColor:
xc->red = (int) (red * PGX_COLORMULT + 0.5);
xc->green = (int) (green * PGX_COLORMULT + 0.5);
xc->blue = (int) (blue * PGX_COLORMULT + 0.5);
break;
case GrayScale:
case StaticGray:
/*
* For gray-scale colormaps the red,green and blue intensities must all be
* equal. Weight the colors so that what is brightest to the eye, is also
* brighter in grayscale, and so that different colors of equal intensity
* appear different in grayscale. Note that the 3 weights must add up to 1.0.
* The black and white TV standard says to use 0.3*R+0.59*G+0.11*B.
* Unfortunately blue pretty much dissapears in this scheme. The following
* is a compromise between making all colors visible and making different
* colors look different in grayscale.
*/
gray = 0.35*red + 0.40*green + 0.25*blue;
xc->red = xc->green = xc->blue = (int) (gray * PGX_COLORMULT + 0.5);
break;
};
/*
* Update the recorded range of color indexes whose color representations
* have been changed since the last call to pgx_update_colors().
*/
if(state) {
if(state->color.nbuff<=0) {
state->color.sbuff = ci;
state->color.nbuff = 1;
} else if(ci < state->color.sbuff) {
state->color.nbuff += state->color.sbuff - ci;
state->color.sbuff = ci;
} else if(ci > state->color.sbuff + state->color.nbuff-1) {
state->color.nbuff = ci - state->color.sbuff + 1;
};
/*
* Register pgx_update_colors() to be called to flush the colors to the
* window.
*/
state->flush_opcode_fn = (Flush_Opcode_fn) pgx_update_colors;
};
};
};
return 0;
}
/*.......................................................................
* Initialize the color representations in the color table.
* pgx_get_visual() must have been called prior to calling this function,
* so that we have a visual and colormap to define the colors in.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* pgx->color->xcolor[0..ncol] The color pixel definitions.
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
static int pgx_init_colors(PgxWin *pgx)
#else
static int pgx_init_colors(pgx)
PgxWin *pgx;
#endif
{
/*
* Define the standard PGPLOT line colors (RGB).
*/
static float ctable[PGX_NCOLORS][3] = {
{0.0,0.0,0.0}, {1.0,1.0,1.0}, {1.0,0.0,0.0}, {0.0,1.0,0.0},
{0.0,0.0,1.0}, {0.0,1.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,0.0},
{1.0,0.5,0.0}, {0.5,1.0,0.0}, {0.0,1.0,0.5}, {0.0,0.5,1.0},
{0.5,0.0,1.0}, {1.0,0.0,0.5}, {0.333,0.333,0.333},
{0.667,0.667,0.667}
};
int i;
if(pgx_ready(pgx, PGX_NEED_COLOR)) {
/*
* Initialize the color-table with the standard PGPLOT line colors.
*/
if(!pgx->color->monochrome) {
int ncol = (PGX_NCOLORS < pgx->color->ncol) ? PGX_NCOLORS:pgx->color->ncol;
for(i=0; i<ncol; i++) {
if(pgx_set_rgb(pgx, i, ctable[i][0], ctable[i][1], ctable[i][2]))
return 1;
};
/*
* Initialize the rest of the colors with a grey-scale ramp.
*/
for(i=ncol; i<pgx->color->ncol; i++) {
float grey= (float) (i-PGX_NCOLORS) /
(float) (pgx->color->ncol-1-PGX_NCOLORS);
if(pgx_set_rgb(pgx, i, grey, grey, grey))
return 1;
};
};
/*
* Flush the new color definitions to the display.
*/
if(pgx_flush_colors(pgx, 0, pgx->color->ncol))
return 1;
/*
* Start with the foreground color set to white.
*/
if(pgx_set_ci(pgx, 1))
return 1;
/*
* Record the color-cells as allocated.
*/
pgx->color->initialized = 1;
};
return 0;
}
/*.......................................................................
* Return the color representation of a given color index.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Input/Output:
* rbuf float * The return array passed from GREXEC.
* nbuf int * The output number of float results for GREXEC.
*/
#ifdef __STDC__
void pgx_get_rgb(PgxWin *pgx, float *rbuf, int *nbuf)
#else
void pgx_get_rgb(pgx, rbuf, nbuf)
PgxWin *pgx; float *rbuf; int *nbuf;
#endif
{
if(pgx_ready(pgx, PGX_NEED_COLOR)) {
int ci = (int) (rbuf[0] + 0.5);
rbuf[1] = (float) pgx->color->xcolor[ci].red / (float) PGX_COLORMULT;
rbuf[2] = (float) pgx->color->xcolor[ci].green / (float) PGX_COLORMULT;
rbuf[3] = (float) pgx->color->xcolor[ci].blue / (float) PGX_COLORMULT;
} else {
rbuf[1] = rbuf[2] = rbuf[3] = 0;
};
*nbuf = 4;
return;
}
/*.......................................................................
* Flush color-representation changes made by xw_set_rgb() to the window.
* This updates the window colormap. If color index 0 is changed
* then the background color is also updated.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* ci_start int The first color index to update.
* ncol int The number of colors to update.
* Color indexes ci_state -> ci_start + ncol - 1 will
* be updated.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
static int pgx_flush_colors(PgxWin *pgx, int ci_start, int ncol)
#else
static int pgx_flush_colors(pgx, ci_start, ncol)
PgxWin *pgx; int ci_start; int ncol;
#endif
{
int bad_colors = 0; /* The number of failed color assignments */
int i;
if(pgx_ready(pgx, PGX_NEED_COLOR) && !pgx->color->monochrome) {
/*
* If the range of color indexes is invalid, warn the user and
* then arrange to update the whole colormap.
*/
if(ci_start < 0 || ci_start + ncol - 1 >= pgx->color->ncol) {
fprintf(stderr, "%s: pgx_flush_colors: color indexes out of range.\n",
PGX_IDENT);
ci_start = 0;
ncol = pgx->color->ncol;
};
/*
* Are there any colors to be updated?
*/
if(ncol > 0) {
XColor *xc = &pgx->color->xcolor[ci_start];
unsigned long *pixel = &pgx->color->pixel[ci_start];
/*
* Install the colors in the color map.
*/
switch(pgx->color->vi->class) {
case PseudoColor:
case GrayScale:
case DirectColor:
XStoreColors(pgx->display, pgx->color->cmap, xc, ncol);
break;
case StaticColor:
case StaticGray:
case TrueColor:
for(i=0; i<ncol && !pgx->bad_device; i++) {
if(XAllocColor(pgx->display, pgx->color->cmap, &xc[i])) {
if(pgx->color->initialized)
XFreeColors(pgx->display, pgx->color->cmap, &pixel[i], 1, (long)0);
pixel[i] = xc[i].pixel;
} else {
bad_colors++;
};
};
break;
};
/*
* Device error?
*/
if(pgx->bad_device)
return 1;
/*
* Update the background color?
*/
if(ci_start == 0 && pgx->window != None)
XSetWindowBackground(pgx->display, pgx->window, pixel[0]);
/*
* Did any of the color assignments fail?
*/
if(bad_colors > 0) {
fprintf(stderr,
"%s: Error setting the color representations of %d colors.\n",
PGX_IDENT, bad_colors);
};
};
};
return pgx->bad_device!=0;
}
/*.......................................................................
* This is a front end to pgx_flush_colors() to be used when pgplot has
* the window open and there are buffered colormap updates from previous
* pgscr() opcodes.
*
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
static int pgx_update_colors(PgxWin *pgx)
#else
static int pgx_update_colors(pgx)
PgxWin *pgx;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
PgxState *state = pgx->state;
/*
* Are there any colors to be updated?
*/
if(state->color.nbuff > 0) {
if(pgx_flush_colors(pgx, state->color.sbuff, state->color.nbuff))
return 1;
/*
* Reset buffer pointers.
*/
state->color.nbuff = 0;
state->color.sbuff = 0;
};
};
return 0;
}
/*.......................................................................
* Clear a PGPLOT window and (if allocated) the associated pixmap, to
* the background color of the window. This function may be called
* before pgx_open() is called.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_clear_window(PgxWin *pgx)
#else
int pgx_clear_window(pgx)
PgxWin *pgx;
#endif
{
if(pgx_ready(pgx, PGX_NEED_WINDOW)) {
/*
* Fill the pixmap with the background color.
*/
if(pgx_ready(pgx, PGX_NEED_COLOR | PGX_NEED_PIXMAP)) {
Window root;
int x, y;
unsigned width, height, border, depth;
/*
* Determine the size of the pixmap.
*/
XGetGeometry(pgx->display, pgx->pixmap, &root, &x, &y, &width, &height,
&border, &depth);
/*
* Clear the pixmap by drawing an opaque rectangle over it in the background
* color. Use the exposure graphical context to avoid changing the
* current foreground color and to allow this function to be called
* before pgx_open().
*/
XSetForeground(pgx->display, pgx->expose_gc, pgx->color->pixel[0]);
XFillRectangle(pgx->display, pgx->pixmap, pgx->expose_gc, 0, 0,
width, height);
if(pgx->bad_device)
return 1;
/*
* Mark the pixmap as unmodified.
*/
if(pgx->state)
pgx->state->update.modified = 0;
};
/*
* Clear the window itself.
*/
if(pgx->clip.doclip) {
pgx_clear_area(pgx, pgx->clip.xmin, pgx->clip.ymin,
(pgx->clip.xmax - pgx->clip.xmin + 1),
(pgx->clip.ymax - pgx->clip.ymin + 1));
} else {
XClearWindow(pgx->display, pgx->window);
};
if(pgx->bad_device)
return 1;
XFlush(pgx->display);
if(pgx->bad_device)
return 1;
};
return pgx->bad_device ? 1 : 0;
}
/*.......................................................................
* Return the device resolution in pixels per inch. This can be used
* to implement PGPLOT opcode 3.
* If pgx==NULL or pgx->display==NULL or pgx->bad_device!=0, then dummy
* values will be returned.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Input/Output:
* xpix_per_inch int * The number of pixels per inch along X.
* ypix_per_inch int * The number of pixels per inch along Y.
*/
#ifdef __STDC__
void pgx_get_resolution(PgxWin *pgx, int *xpix_per_inch, int *ypix_per_inch)
#else
void pgx_get_resolution(pgx, xpix_per_inch, ypix_per_inch)
PgxWin *pgx; int *xpix_per_inch; int *ypix_per_inch;
#endif
{
if(pgx && pgx->display && !pgx->bad_device) {
Display *display = pgx->display;
int screen = DefaultScreen(display);
unsigned int d_pix_width = DisplayWidth(display, screen);
unsigned int d_pix_height = DisplayHeight(display, screen);
unsigned int d_mm_width = DisplayWidthMM(display, screen);
unsigned int d_mm_height = DisplayHeightMM(display, screen);
/*
* Determine the device resolution in pixels per inch.
*/
if(xpix_per_inch)
*xpix_per_inch = 25.4 * ((double)d_pix_width / (double)d_mm_width);
if(ypix_per_inch)
*ypix_per_inch = 25.4 * ((double)d_pix_height / (double)d_mm_height);
return;
} else {
if(xpix_per_inch)
*xpix_per_inch = 1;
if(ypix_per_inch)
*ypix_per_inch = 1;
};
return;
}
/*.......................................................................
* Return the default size of the plot area in the form required by
* PGPLOT opcode 6.
*
* If pgx==NULL, pgx->display==NULL or pgx->bad_device!=0, then specified
* default values will be returned.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* d_width unsigned The default width to be specified if the
* device is not open or is in an innapropriate
* state.
* d_height unsigned The default height to be specified if the
* device is not open or is in an innapropriate
* state.
* Input/Output:
* rbuf float * The return array passed from GREXEC.
* nbuf int * The output number of float results for GREXEC.
*/
#ifdef __STDC__
void pgx_def_size(PgxWin *pgx, unsigned d_width, unsigned d_height,
float *rbuf, int *nbuf)
#else
void pgx_def_size(pgx, d_width, d_height, rbuf, nbuf)
PgxWin *pgx; unsigned d_width; unsigned d_height; float *rbuf; int *nbuf;
#endif
{
/*
* Set invariants.
*/
rbuf[0] = 0.0;
rbuf[2] = 0.0;
*nbuf = 4;
/*
* If we have sufficient information, return the current size of the
* window excluding margins.
*/
if(pgx && pgx->display && !pgx->bad_device) {
XWindowAttributes attr;
int xpix_per_inch;
int ypix_per_inch;
pgx_get_resolution(pgx, &xpix_per_inch, &ypix_per_inch);
XGetWindowAttributes(pgx->display, pgx->window, &attr);
if(!pgx->bad_device) {
rbuf[1] = (float) (attr.width - 2 * pgx->margin * xpix_per_inch);
rbuf[3] = (float) (attr.height - 2 * pgx->margin * ypix_per_inch);
if(rbuf[1] > 2 && rbuf[3] > 2)
return;
};
};
/*
* If the device is not yet open, or if an error occured above, substitute
* the default dimensions.
*/
rbuf[1] = d_width;
rbuf[3] = d_height;
return;
}
/*.......................................................................
* Start a new page of a specified size. This includes resizing the
* window and pixmap if necessary and clearing them. It also includes
* initializing pgx->state->geom to reflect the size of the pixmap.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* rbuf float * The array of float arguments sent by the PGPLOT
* GREXEC() subroutine.
*/
#ifdef __STDC__
void pgx_begin_picture(PgxWin *pgx, float *rbuf)
#else
void pgx_begin_picture(pgx, rbuf)
PgxWin *pgx; float *rbuf;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
PgxState *state = pgx->state;
/*
* Determine the device resolution.
*/
pgx_get_resolution(pgx, &state->geom.xpix_per_inch,
&state->geom.ypix_per_inch);
/*
* Determine the X and Y axis margins.
*/
state->geom.xmargin = state->geom.xpix_per_inch * pgx->margin;
state->geom.ymargin = state->geom.ypix_per_inch * pgx->margin;
/*
* Convert the passed max X and Y coordinates into the total width of the
* new window and pixmap. Add margins to the requested area.
*/
state->geom.width = (int) (rbuf[0] + 0.5) + 2 * state->geom.xmargin;
state->geom.height = (int) (rbuf[1] + 0.5) + 2 * state->geom.ymargin;
/*
* Record the coordinate bounds of the required window area.
*/
state->geom.xmin = state->geom.xmargin;
state->geom.xmax = state->geom.width - state->geom.xmargin;
state->geom.ymin = state->geom.ymargin;
state->geom.ymax = state->geom.height - state->geom.ymargin;
/*
* Resize the window if necessary.
*/
if(!pgx->bad_device) {
XWindowAttributes attr;
XGetWindowAttributes(pgx->display, pgx->window, &attr);
if(!pgx->bad_device)
if(pgx->resize_fn && (attr.width != state->geom.width ||
attr.height != state->geom.height)) {
(*pgx->resize_fn)(pgx, state->geom.width, state->geom.height);
};
};
/*
* If a pixmap exists and has a different size to that requested,
* delete it.
*/
if(!pgx->bad_device && pgx->pixmap != None) {
Window root;
int x, y;
unsigned width, height, border, depth;
/*
* Determine the size of the existing pixmap.
*/
XGetGeometry(pgx->display, pgx->pixmap, &root, &x, &y, &width, &height,
&border, &depth);
/*
* Delete it if it has the wrong size.
*/
if(width != state->geom.width || height != state->geom.height) {
XFreePixmap(pgx->display, pgx->pixmap);
pgx->pixmap = None;
};
};
/*
* Create a new pixmap if necessary.
*/
if(!pgx->bad_device && pgx->pixmap==None) {
if(!pgx->new_pixmap_fn)
pgx->new_pixmap_fn = pgx_new_pixmap;
(*pgx->new_pixmap_fn)(pgx, state->geom.width, state->geom.height);
};
};
/*
* Clear the window and pixmap.
*/
pgx_clear_window(pgx);
/*
* Reset the scroll and pan offsets.
*/
pgx_scroll(pgx, 0, 0);
return;
}
/*.......................................................................
* Allocate a new Pixmap for a PGPLOT window of a given size.
* Note that pgx->pixmap should be deleted and assigned None before
* this function is called.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* width unsigned The required width of the pixmap (pixels).
* height unsigned The required height of the pixmap (pixels).
*/
#ifdef __STDC__
void pgx_new_pixmap(PgxWin *pgx, unsigned width, unsigned height)
#else
void pgx_new_pixmap(pgx, width, height)
PgxWin *pgx; unsigned width; unsigned height;
#endif
{
if(pgx_ready(pgx, PGX_NEED_COLOR | PGX_NEED_WINDOW)) {
/*
* Bracket the pixmap acquisition with pgx_start/end_error() calls, to
* determine whether any allocation errors occur.
*/
pgx_start_error_watch(pgx);
pgx->pixmap = XCreatePixmap(pgx->display, pgx->window, width, height,
(unsigned) pgx->color->vi->depth);
if(pgx_end_error_watch(pgx) || pgx->pixmap==None) {
fprintf(stderr, "%s: Failed to allocate %dx%d pixmap.\n", PGX_IDENT,
width, height);
pgx->pixmap = None;
};
};
return;
}
/*.......................................................................
* Change the appearance and position of specific graphical augmentations
* to the cursor.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* ci int The color index to use when drawing the cursor,
* or -1 to select the current foreground color.
* type int A cursor type from:
* PGX_NORM_CURSOR - No augmentation will be drawn.
* PGX_LINE_CURSOR - Line cursor between rbeg and rend.
* PGX_RECT_CURSOR - Rectangular cursor with opposing
* vertices at rbeg and rend.
* PGX_VLINE_CURSOR - Vertical line cursor at x=rbeg[0].
* PGX_HLINE_CURSOR - Horizontal line cursor at y=rbeg[1].
* PGX_CROSS_CURSOR - Cross-hair cursor at rbeg[0],rbeg.[1]
* warp int If true, the cursor will be warped to rbeg when it
* first enters the window.
* rbeg float * The PGPLOT x,y device coordinates of the origin
* of the cursor, as rbeg[0]=x and rbeg[1]=y.
* This can be NULL for type==PGX_NORM_CURSOR.
* rend float * The PGPLOT x,y device coordinates of the end-point
* of the cursor, as rend[0]=x and rend[1]=y. This
* can be NULL for cursor types that only need rbeg.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_set_cursor(PgxWin *pgx, int ci, int type, int warp,
float *rbeg, float *rend)
#else
int pgx_set_cursor(pgx, ci, type, warp, rbeg, rend)
PgxWin *pgx; int ci; int type; int warp; float *rbeg; float *rend;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
PgxState *state = pgx->state;
state->cursor.type = PGX_NORM_CURSOR;
state->cursor.warp = warp;
/*
* Record the cursor coordinates according to cursor type.
*/
switch(type) {
case PGX_NORM_CURSOR:
break;
case PGX_LINE_CURSOR:
case PGX_RECT_CURSOR:
case PGX_YRNG_CURSOR:
case PGX_XRNG_CURSOR:
if(rbeg && rend) {
state->cursor.type = type;
pgx_xy_to_XPoint(pgx, rbeg, &state->cursor.vbeg);
pgx_xy_to_XPoint(pgx, rend, &state->cursor.vend);
};
break;
case PGX_HLINE_CURSOR:
case PGX_VLINE_CURSOR:
case PGX_CROSS_CURSOR:
if(rbeg) {
state->cursor.type = type;
pgx_xy_to_XPoint(pgx, rbeg, &state->cursor.vbeg);
};
break;
};
/*
* Establish a color for drawing the cursor.
*/
if(type != PGX_NORM_CURSOR) {
unsigned long pixel; /* The colormap pixel to draw with */
if(ci < 0)
pixel = state->gcv.foreground; /* The current foreground color */
else if(ci < pgx->color->ncol)
pixel = pgx->color->pixel[ci]; /* The specified color index */
else
pixel = pgx->color->pixel[1]; /* Out-of range, so use color index 1 */
XSetForeground(pgx->display, state->cursor.gc, pixel);
};
return pgx->bad_device!=0;
};
return 1;
}
/*.......................................................................
* If the cursor is currently marked as having been drawn, redraw it to
* account for damage from expose events or pgplot drawing operations
* that have taken place since the cursor was last drawn.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* rbeg float * The PGPLOT x,y device coordinates of the origin
* of the cursor, as rbeg[0]=x and rbeg[1]=y.
* This can be NULL for type==PGX_NORM_CURSOR.
* rend float * The PGPLOT x,y device coordinates of the end-point
* of the cursor, as rend[0]=x and rend[1]=y. This
* can be NULL for cursor types that only need rbeg.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_refresh_cursor(PgxWin *pgx)
#else
int pgx_refresh_cursor(pgx)
PgxWin *pgx;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
PgxState *state = pgx->state;
if(state->cursor.drawn && pgx_draw_cursor(pgx))
return 1;
};
return 0;
}
/*.......................................................................
* Augment the X cursor with non-destructive line graphics by drawing
* the graphics directly to the X window instead of to the pixmap. THe
* underlying graphics can then be completely restored from the pixmap.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_draw_cursor(PgxWin *pgx)
#else
int pgx_draw_cursor(pgx)
PgxWin *pgx;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP)) {
PgxState *state = pgx->state;
XWcursor *cursor = &state->cursor;
int ymin, ymax;
int xmin, xmax;
/*
* Convert from pixmap coordinates to window coordinates.
*/
XPoint vbeg;
XPoint vend;
pgx_pixmap_to_window(pgx, &cursor->vbeg, &vbeg);
pgx_pixmap_to_window(pgx, &cursor->vend, &vend);
/*
* Get the drawable limits of the window.
*/
if(pgx->clip.doclip) {
xmin = pgx->clip.xmin;
xmax = pgx->clip.xmax;
ymin = pgx->clip.ymin;
ymax = pgx->clip.ymax;
} else {
xmin = 0;
xmax = (int)state->geom.width - 1;
ymin = 0;
ymax = (int)state->geom.height - 1;
};
/*
* Record the fact that the cursor is being drawn.
*/
cursor->drawn = 1;
/*
* Draw the requested cursor augmentation graphics.
*/
switch(cursor->type) {
case PGX_NORM_CURSOR:
default:
break;
case PGX_LINE_CURSOR:
XDrawLine(pgx->display, pgx->window, cursor->gc,
vbeg.x, vbeg.y,
vend.x, vend.y);
break;
case PGX_RECT_CURSOR: /* Draw a rectangle */
{
int x = vbeg.x<vend.x ? vbeg.x : vend.x;
int y = vbeg.y<vend.y ? vbeg.y : vend.y;
unsigned width = (unsigned int) abs(vbeg.x - vend.x);
unsigned height = (unsigned int) abs(vbeg.y - vend.y);
XDrawRectangle(pgx->display, pgx->window, cursor->gc, x, y,
width, height);
};
break;
case PGX_YRNG_CURSOR: /* Two horizontal lines */
XDrawLine(pgx->display, pgx->window, cursor->gc, xmin, vend.y,
xmax, vend.y);
if(pgx->bad_device)
return 1;
XDrawLine(pgx->display, pgx->window, cursor->gc, xmin, vbeg.y,
xmax, vbeg.y);
break;
case PGX_XRNG_CURSOR: /* Two vertical lines */
XDrawLine(pgx->display, pgx->window, cursor->gc, vend.x, ymin,
vend.x, ymax);
if(pgx->bad_device)
return 1;
XDrawLine(pgx->display, pgx->window, cursor->gc, vbeg.x, ymin,
vbeg.x, ymax);
break;
case PGX_HLINE_CURSOR: /* One horizontal line through the cursor */
XDrawLine(pgx->display, pgx->window, cursor->gc, xmin, vend.y,
xmax, vend.y);
break;
case PGX_VLINE_CURSOR: /* One vertical line through the cursor */
XDrawLine(pgx->display, pgx->window, cursor->gc, vend.x, ymin,
vend.x, ymax);
break;
case PGX_CROSS_CURSOR: /* Cross hair */
XDrawLine(pgx->display, pgx->window, cursor->gc, xmin, vend.y,
xmax, vend.y);
if(pgx->bad_device)
return 1;
XDrawLine(pgx->display, pgx->window, cursor->gc, vend.x, ymin,
vend.x, ymax);
break;
};
XFlush(pgx->display);
return pgx->bad_device != 0;
};
return 1;
}
/*.......................................................................
* Erase graphics drawn by pgx_draw_cursor() by restoring the damaged
* region from the underlying pixmap.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_erase_cursor(PgxWin *pgx)
#else
int pgx_erase_cursor(pgx)
PgxWin *pgx;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP)) {
PgxState *state = pgx->state;
XWcursor *cursor = &state->cursor;
/*
* Convert from pixmap coordinates to window coordinates.
*/
XPoint vbeg;
XPoint vend;
pgx_pixmap_to_window(pgx, &cursor->vbeg, &vbeg);
pgx_pixmap_to_window(pgx, &cursor->vend, &vend);
/*
* Record the erasure.
*/
cursor->drawn = 0;
/*
* Erase the cursor.
*/
switch(cursor->type) {
case PGX_NORM_CURSOR:
default:
break;
case PGX_LINE_CURSOR: /* Line cursor */
if(pgx_restore_line(pgx, vbeg.x, vbeg.y,
vend.x, vend.y))
return 1;
break;
case PGX_RECT_CURSOR: /* Rectangle cursor */
if(pgx_restore_line(pgx, vbeg.x, vbeg.y,
vbeg.x, vend.y) ||
pgx_restore_line(pgx, vbeg.x, vend.y,
vend.x, vend.y) ||
pgx_restore_line(pgx, vend.x, vend.y,
vend.x, vbeg.y) ||
pgx_restore_line(pgx, vend.x, vbeg.y,
vbeg.x, vbeg.y))
return 1;
break;
case PGX_YRNG_CURSOR: /* Two horizontal lines */
if(pgx_restore_line(pgx, 0, vend.y,
(int)state->geom.width-1, vend.y) ||
pgx_restore_line(pgx, 0, vbeg.y,
(int)state->geom.width-1,vbeg.y))
return 1;
break;
case PGX_XRNG_CURSOR: /* Two vertical lines */
if(pgx_restore_line(pgx, vend.x, 0,
vend.x, (int)state->geom.height-1) ||
pgx_restore_line(pgx, vbeg.x, 0,
vbeg.x, (int)state->geom.height-1))
return 1;
break;
case PGX_HLINE_CURSOR: /* One horizontal line through the cursor */
if(pgx_restore_line(pgx, 0, vend.y,
(int)state->geom.width-1,vend.y))
return 1;
break;
case PGX_VLINE_CURSOR: /* One vertical line through the cursor */
if(pgx_restore_line(pgx, vend.x, 0,
vend.x, (int)state->geom.height-1))
return 1;
break;
case PGX_CROSS_CURSOR: /* Cross hair */
if(pgx_restore_line(pgx, 0, vend.y,
(int)state->geom.width-1, vend.y) ||
pgx_restore_line(pgx, vend.x, 0,
vend.x, (int)state->geom.height-1))
return 1;
break;
};
return pgx->bad_device != 0;
};
return 1;
}
/*.......................................................................
* Restore the pixels under a given line.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* xa, ya int The start pixel of the line (window coordinates).
* xb, yb int The end pixel of the line (window coordinates).
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
static int pgx_restore_line(PgxWin *pgx, int xa, int ya, int xb, int yb)
#else
static int pgx_restore_line(pgx, xa, ya, xb, yb)
PgxWin *pgx; int xa; int ya; int xb; int yb;
#endif
{
int xlen = xb - xa; /* X-axis displacement of line */
int ylen = yb - ya; /* Y-axis displacement of line */
int xmin,xmax; /* Min/max X-axis end points */
int ymin,ymax; /* Min/max Y-axis end points */
#define PIXINC 51
/*
* Device error?
*/
if(pgx->bad_device)
return 1;
/*
* Get sorted versions of xa and xb.
*/
if(xlen > 0) {
xmin = xa;
xmax = xb;
} else {
xmin = xb;
xmax = xa;
};
/*
* Get sorted versions of ya and yb.
*/
if(ylen > 0) {
ymin = ya;
ymax = yb;
} else {
ymin = yb;
ymax = ya;
};
/*
* Vertical line?
*/
if(xlen==0) {
pgx_copy_area(pgx, (int)(xmin + pgx->scroll.x), (int)(ymin + pgx->scroll.y),
(unsigned) 1, (unsigned) (ymax-ymin+1),
xmin, ymin);
}
/*
* Horizontal line?
*/
else if(ylen==0) {
pgx_copy_area(pgx, (int)(xmin + pgx->scroll.x), (int)(ymin + pgx->scroll.y),
(unsigned) (xmax-xmin+1), (unsigned) 1,
xmin, ymin);
}
/*
* Diagonal line encompasing fewer x-axis lines that y-axis lines?
*/
else if(abs(xlen) <= abs(ylen)) {
int x; /* The X coordinate of the line of pixels being drawn */
int y1,y2; /* The start and end Y coordinates of the pixel line */
double yperx = (double) ylen / (double) xlen;
double yhalf = 0.5 * yperx; /* Y-step over half a pixel */
double ydelt = (PIXINC+0.5) * yperx; /* Y-step over PIXINC+0.5 pixels */
double ylo = yperx > 0 ? yhalf : -ydelt;
double yhi = yperx > 0 ? ydelt : -yhalf;
/*
* Draw the block of pixels that encompases the line between X-axis
* pixels the outer edges of pixels x -> x+PIXINC, for each consecutive
* block of PIXINC pixels along X.
*/
for(x=xmin; x <= xmax; x += PIXINC+1) {
double ycent = ya + (x - xa) * yperx;
y1 = (int)(ycent - ylo); /* Note round-down semantics */
y2 = (int)(ycent + yhi+0.5);/* Note round-up semantics */
pgx_copy_area(pgx, (int)(x + pgx->scroll.x), (int)(y1 + pgx->scroll.y),
(unsigned) (PIXINC+1), (unsigned) (y2-y1+1),
x, y1);
};
/*
* Diagonal line encompasing fewer y-axis lines that x-axis lines?
*/
} else {
int y; /* The Y coordinate of the line of pixels being drawn */
int x1,x2; /* The start and end X coordinates of the pixel line */
double xpery = (double) xlen / (double) ylen;
double xhalf = 0.5 * xpery; /* X-step over half a pixel */
double xdelt = (PIXINC+0.5) * xpery; /* X-step over PIXINC+0.5 pixels */
double xlo = xpery > 0 ? xhalf : -xdelt;
double xhi = xpery > 0 ? xdelt : -xhalf;
/*
* Draw the block of pixels that encompases the line between Y-axis
* pixels the outer edges of pixels y -> y+PIXINC, for each consecutive
* block of PIXINC pixels along Y.
*/
for(y=ymin; y <= ymax; y += PIXINC+1) {
double xcent = xa + (y - ya) * xpery;
x1 = (int)(xcent - xlo); /* Note round-down semantics */
x2 = (int)(xcent + xhi+0.5);/* Note round-up semantics */
pgx_copy_area(pgx, (int)(x1 + pgx->scroll.x), (int)(y + pgx->scroll.y),
(unsigned) (x2-x1+1), (unsigned) (PIXINC+1),
x1, y);
};
};
/*
* Check for device errors.
*/
if(pgx->bad_device)
return 1;
return 0;
}
/*.......................................................................
* When the cursor is active, this function should be called whenever
* one of the following event types are received.
*
* ButtonPress, KeyPress, MotionNotify, EnterWindow, LeaveWindow.
*
* In addition, Expose events will be handled if presented. Other event
* types are quietly ignored.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* event XEvent * The X input event to be processed.
* Input/Output:
* rbuf float * If event->type is ButtonPress, KeyPress or
* MotionNotify and rbuf!=NULL then the device
* coordinates of the input event will be encoded
* in PGPLOT x,y device coordinates as rbuf[0]=x
* and rbuf[1]=y.
* key char * If key!=NULL then:
* 1. If the event is a key-press or key-release
* event and the key is a single-ascii
* character then *key will contain that
* character.
* 2. If the event is a button-press or
* button-release event then the button will be
* encoded as characters in *key as:
* Button 1 => 'A',
* Button 2 => 'D',
* Button 3 => 'X'.
* 3. Otherwise *key='\0'.
* Output:
* return int 0 - No position selected.
* 1 - rbuf and key contain the latest cursor
* selection details.
*/
#ifdef __STDC__
int pgx_cursor_event(PgxWin *pgx, XEvent *event, float *rbuf, char *key)
#else
int pgx_cursor_event(pgx, event, rbuf, key)
PgxWin *pgx; XEvent *event; float *rbuf; char *key;
#endif
{
char ret_key = '\0'; /* The key to be returned */
XPoint coord; /* Pointer coordinate */
int have_posn = 0; /* If true, a position has been encoded in coord */
/*
* Cursor ready?
*/
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP)) {
PgxState *state = pgx->state;
XWcursor *cursor = &state->cursor;
/*
* Place the pointer position in coord.x,y.
*/
switch(event->type) {
case Expose:
if(pgx_expose(pgx, event))
return 0;
break;
case KeyPress:
coord.x = event->xkey.x;
coord.y = event->xkey.y;
/*
* Get the ASCII encoding associated with the key.
*/
{
char buffer[10]; /* Buffer to read key definition into */
KeySym keysym; /* Key code of pressed keyboard key */
int nret; /* The number of characters returned in buffer[]*/
nret = XLookupString(&event->xkey, buffer,
(int) (sizeof(buffer)/sizeof(char)), &keysym, NULL);
if(pgx->bad_device)
return 0;
/*
* Ignore modifier keys and all but single character keys.
*/
if(nret==1 && (keysym < XK_Shift_L || keysym > XK_Hyper_R)) {
ret_key = buffer[0];
have_posn = 1;
};
};
break;
case ButtonPress:
coord.x = event->xbutton.x;
coord.y = event->xbutton.y;
have_posn = 1;
switch(event->xbutton.button) {
case Button1:
ret_key = 'A';
break;
case Button2:
ret_key = 'D';
break;
default:
ret_key = 'X';
break;
};
break;
case EnterNotify:
/*
* The cursor may still be drawn if a button was pressed when the
* cursor was last moved out of the window. The resulting
* passive grab will have continued to deliver motion events to
* the PGPLOT window.
*/
if(pgx_erase_cursor(pgx))
return 0;
/*
* If the cursor is in the window, locate its position and record it
* in pgx->state->cursor.vend. If this is the first time that the
* cursor has been in the window and warping has been requested,
* this also inolves pre-positioning the cursor.
*/
if(pgx_locate_cursor(pgx)) {
/*
* Draw the cursor if it isn't already drawn.
*/
if(pgx->bad_device || pgx_draw_cursor(pgx))
return 0;
};
break;
case LeaveNotify:
if(pgx_erase_cursor(pgx))
return 0;
break;
case MotionNotify:
/*
* Discard all but the last MotionNotify event.
*/
while(XCheckWindowEvent(pgx->display, pgx->window,
(long)(PointerMotionMask), event) == True);
if(pgx->bad_device || pgx_erase_cursor(pgx))
return 0;
/*
* Erase the out-of-date cursor.
*/
if(pgx_erase_cursor(pgx))
return 0;
/*
* Convert from window coordinates to pixmap coordinates.
*/
cursor->vend.x = event->xmotion.x + pgx->scroll.x;
cursor->vend.y = event->xmotion.y + pgx->scroll.y;
/*
* Redraw the cursor at the new position.
*/
if(pgx_draw_cursor(pgx))
return 0;
break;
default:
break;
};
/*
* Convert new pointer coordinates to pgplot device coordinates.
*/
if(have_posn) {
/*
* Convert from window coordinates to pixmap coordinates.
*/
coord.x = coord.x + pgx->scroll.x;
coord.y = coord.y + pgx->scroll.y;
/*
* Convert to PGPLOT device coordinates.
*/
if(rbuf)
pgx_XPoint_to_xy(pgx, &coord, rbuf);
if(key)
*key = ret_key;
return 1;
};
};
return 0;
}
/*.......................................................................
* Determine whether the cursor is within the plot window. If it is
* and (cursor=&pgx->state->cursor) cursor->warp is true, warp the cursor
* to cursor->vbeg then reset cursor->warp to 0.
* Record the final position of the cursor in cursor->vend.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* return int 0 - Cursor not in window.
* 1 - Cursor is in window.
*/
#ifdef __STDC__
int pgx_locate_cursor(PgxWin *pgx)
#else
int pgx_locate_cursor(pgx)
PgxWin *pgx;
#endif
{
XPoint pointer; /* Pointer coordinates */
Window parent; /* The parent window */
/*
* The following are all for use with XQueryPointer().
*/
Window p_child; /* The child of pgx->window (None) */
int p_win_x, p_win_y; /* The pointer coordinates in pgx->window */
int p_root_x, p_root_y; /* The pointer coordinates in the root window */
Window p_root_win; /* The root window containing the cursor */
unsigned int p_mask; /* Bit mask of button states etc.. */
/*
* Device error?
*/
if(pgx_ready(pgx, PGX_NEED_WINDOW | PGX_NEED_PGOPEN)) {
PgxState *state = pgx->state;
XWcursor *cursor = &state->cursor;
/*
* Get the parent window.
*/
parent = pgx_parent_window(pgx);
if(parent == None)
return 0;
/*
* See if the pointer is currently in the PGPLOT window.
*/
XQueryPointer(pgx->display, parent, &p_root_win, &p_child,
&p_root_x, &p_root_y, &p_win_x, &p_win_y, &p_mask);
if(pgx->bad_device)
return 0;
if(p_child==pgx->window) {
/*
* Determine the current position of the pointer within the PGPLOT window.
*/
XQueryPointer(pgx->display, pgx->window, &p_root_win, &p_child,
&p_root_x, &p_root_y, &p_win_x, &p_win_y, &p_mask);
if(pgx->bad_device)
return 0;
/*
* Record the pointer coordinates.
*/
pointer.x = p_win_x;
pointer.y = p_win_y;
/*
* Warp the cursor?
*/
if(cursor->warp) {
XWindowAttributes attr; /* Current window attributes */
XPoint warp_coord; /* The window coordinates to warp to */
/*
* Query the current state of the window.
*/
XGetWindowAttributes(pgx->display, pgx->window, &attr);
if(pgx->bad_device)
return 0;
/*
* Convert the warp pixmap coordinates to window coordinates.
*/
pgx_pixmap_to_window(pgx, &cursor->vbeg, &warp_coord);
/*
* Disable subsequent warping.
*/
cursor->warp = 0;
/*
* Don't warp the cursor unless the warp target location is visible.
*/
if(warp_coord.x < 0 || warp_coord.x >= attr.width ||
warp_coord.y < 0 || warp_coord.y >= attr.height) {
pgx_window_to_pixmap(pgx, &pointer, &cursor->vend);
} else {
XWarpPointer(pgx->display, None, pgx->window, 0, 0, 0, 0,
warp_coord.x, warp_coord.y);
if(pgx->bad_device)
return 0;
/*
* Record the new coordinates.
*/
pgx_window_to_pixmap(pgx, &warp_coord, &cursor->vend);
};
/*
* Return the current position of the cursor without warping.
*/
} else {
pgx_window_to_pixmap(pgx, &pointer, &cursor->vend);
};
return 1; /* The pointer is in the window */
};
};
return 0; /* The pointer is not in the window */
}
/*.......................................................................
* Install a new event mask. This function can be used to add to remove
* from or replace the existing event mask. When adding to or replacing
* the existing event mask, care is taken to check for the addition of
* events that X only allows one client to select. If these can not
* be selected, they will silently be removed from the event mask.
* The pertinent masks are: ButtonPressMask, SubstructureRedirectMask and
* ResizeRedirectMask.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* oper int The operation to be performed, from:
* PGX_ADD_EVENTS - Form a union of the existing
* event mask and 'events'.
* PGX_REM_EVENTS - Remove the events in 'events'
* from the existing event mask.
* PGX_SET_EVENTS - Replace the existing event
* mask with 'events'.
* events unsigned long The event mask to use as specified by 'oper'.
* Output:
* return unsigned long The old event mask.
*/
#ifdef __STDC__
unsigned long pgx_select_events(PgxWin *pgx, int oper, long events)
#else
unsigned long pgx_select_events(pgx, oper, events)
PgxWin *pgx; int oper; long events;
#endif
{
unsigned long incr_mask; /* The events to be added to the old mask */
unsigned long decr_mask; /* The events to be removed from the old mask */
unsigned long new_mask = 0; /* The new trial event mask */
unsigned long old_mask = 0; /* The old event mask to be returned */
/*
* We need a window to select events from.
*/
if(pgx_ready(pgx, PGX_NEED_WINDOW)) {
XWindowAttributes attr; /* Current window attributes */
/*
* Get the current window attributes.
*/
XGetWindowAttributes(pgx->display, pgx->window, &attr);
if(pgx->bad_device)
return old_mask;
/*
* Record the existing event mask.
*/
old_mask = attr.your_event_mask;
/*
* Decompose the mask-update into a mask of events that need to be
* added and a mask of events that need to be removed.
*/
switch(oper) {
case PGX_SET_EVENTS:
default:
incr_mask = ~old_mask & events;
decr_mask = ~events & old_mask;
break;
case PGX_ADD_EVENTS:
incr_mask = ~old_mask & (old_mask | events);
decr_mask = 0;
break;
case PGX_REM_EVENTS:
incr_mask = 0;
decr_mask = events;
break;
};
/*
* Form a new mask from the old mask by removing the events in decr_mask
* but adding only events from the incremental mask that do not have the
* potential to cause protocol errors.
*/
new_mask = (old_mask & ~decr_mask) |
(incr_mask & ~(unsigned long)(ButtonPressMask |
SubstructureRedirectMask | ResizeRedirectMask));
/*
* If the incremental event mask contains events that can only be selected
* by one client at a time, we must try each one, one at a time and
* only add the event to the final mask if it can be accomodated.
*/
if(incr_mask & ButtonPressMask) {
pgx_start_error_watch(pgx);
XSelectInput(pgx->display, pgx->window,
(long) (new_mask | ButtonPressMask));
if(!pgx_end_error_watch(pgx))
new_mask |= ButtonPressMask;
};
if(incr_mask & SubstructureRedirectMask) {
pgx_start_error_watch(pgx);
XSelectInput(pgx->display, pgx->window,
(long) (new_mask | SubstructureRedirectMask));
if(!pgx_end_error_watch(pgx))
new_mask |= SubstructureRedirectMask;
};
if(incr_mask & ResizeRedirectMask) {
pgx_start_error_watch(pgx);
XSelectInput(pgx->display, pgx->window,
(long)(new_mask | ResizeRedirectMask));
if(!pgx_end_error_watch(pgx))
new_mask |= ResizeRedirectMask;
};
/*
* Try to select the new mask.
*/
pgx_start_error_watch(pgx);
XSelectInput(pgx->display, pgx->window, (long) new_mask);
if(pgx_end_error_watch(pgx)) {
fprintf(stderr, "pgx_select_events: Error selecting events.\n");
return old_mask;
};
};
return old_mask;
}
/*.......................................................................
* Perform a blocking cursor read to implement the read-cursor PGPLOT
* driver opcode. The function will not return until the user presses a
* pointer button or keyboard key within the window.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Input/Output:
* rbuf float * The array of float arguments sent by the PGPLOT
* GREXEC() subroutine.
* On input:
* rbuf[0] = Initial X position of cursor.
* rbuf[1] = Initial Y position of cursor.
* rbuf[2] = Reference X position for rubber-banding.
* rbuf[3] = Reference Y position for rubber-banding.
* rbuf[4] = Cursor banding mode as enumerated in
* pgxwin.h as PGX_*_CURSOR.
* rbuf[5] = Pre-position cursor if > 0.
* On output:
* rbuf[0] = X position of cursor.
* rbuf[1] = Y position of cursor.
* chr char * On output *chr will be assigned the character
* associated with the key or button that the user
* typed.
* nbuf int * On output this will be set to 2 to tell PGPLOT that
* two elements of rbuf are being returned.
* lchr int * On output this will be set to 1 to tell PGPLOT that
* chr[] contains a single character.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_read_cursor(PgxWin *pgx, float *rbuf, char *chr, int *nbuf, int *lchr)
#else
int pgx_read_cursor(pgx, rbuf, chr, nbuf, lchr)
PgxWin *pgx; float *rbuf; char *chr; int *nbuf; int *lchr;
#endif
{
int waserr = 0;
/*
* Preset predetermined return values.
*/
if(nbuf)
*nbuf = 2;
if(lchr)
*lchr = 1;
if(chr)
*chr = '\0';
/*
* Read the cursor if possible.
*/
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP)) {
unsigned long old_mask;
/*
* Raise the cursor.
*/
if(pgx_set_cursor(pgx, -1, (int)(rbuf[4]+0.5), (int)(rbuf[5]+0.5)>0,
&rbuf[2], &rbuf[0]))
return 1;
/*
* Draw the cursor if it is in the window.
*/
pgx_locate_cursor(pgx);
/*
* Augment the event mask with the events that we need.
*/
old_mask = pgx_select_events(pgx, PGX_ADD_EVENTS, (long)
(ExposureMask | KeyPressMask | ButtonPressMask |
EnterWindowMask | LeaveWindowMask |
PointerMotionMask));
/*
* Handle the above events until the user selects a position by pressing
* a mouse button or key.
*/
waserr = waserr || pgx_handle_cursor(pgx, rbuf, chr);
/*
* Remove any cursor augmentation.
*/
waserr = waserr || pgx_erase_cursor(pgx) ||
pgx_set_cursor(pgx, 0, PGX_NORM_CURSOR, 0, NULL, NULL);
/*
* Reinstall the original event mask.
*/
pgx_select_events(pgx, PGX_SET_EVENTS, (long) old_mask);
};
return waserr != 0;
}
/*.......................................................................
* This is the private function of pgx_read_cursor() which maintains
* the display of the cursor and returns when a valid keyboard or
* button-press event has been received.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Input/Output:
* rbuf float * The return position array.
* key char * The key that caused the cursor to be selected.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
static int pgx_handle_cursor(PgxWin *pgx, float *rbuf, char *key)
#else
static int pgx_handle_cursor(pgx, rbuf, key)
PgxWin *pgx; float *rbuf; char *key;
#endif
{
XEvent event;
/*
* Discard un-handled ButtonPress, KeyPress and MotionNotify events
* without blocking.
*/
while(XCheckWindowEvent(pgx->display, pgx->window, (long)
(ButtonPressMask | KeyPressMask | PointerMotionMask), &event))
;
if(pgx->bad_device)
return 1;
/*
* Wait for further events.
*/
while(!pgx->bad_device) {
XWindowEvent(pgx->display, pgx->window, (long)
(ExposureMask | KeyPressMask | ButtonPressMask |
EnterWindowMask | LeaveWindowMask | PointerMotionMask),
&event);
if(pgx->bad_device)
return 1;
if(pgx_cursor_event(pgx, &event, rbuf, key) && *key!='\0')
return 0;
};
return 1;
}
/*.......................................................................
* Limit pixmap coordinates to lie within the pixmap area.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Input/Output:
* coord XPoint * The coordinates to be modified.
*/
#ifdef __STDC__
static void pgx_limit_pcoords(PgxWin *pgx, XPoint *coord)
#else
static void pgx_limit_pcoords(pgx, coord)
PgxWin *pgx; XPoint *coord;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
PgxState *state = pgx->state;
if(coord->x < 0)
coord->x = 0;
if(coord->y < 0)
coord->y = 0;
if(coord->x >= state->geom.width)
coord->x = state->geom.width - 1;
if(coord->y >= state->geom.height)
coord->y = state->geom.height - 1;
};
return;
}
/*.......................................................................
* Limit window coordinates to lie within the drawable window area.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Input/Output:
* coord XPoint * The coordinates to be modified.
*/
#ifdef __STDC__
static void pgx_limit_wcoords(PgxWin *pgx, XPoint *coord)
#else
static void pgx_limit_wcoords(pgx, coord)
PgxWin *pgx; XPoint *coord;
#endif
{
if(pgx_ready(pgx, 0) && pgx->clip.doclip) {
if(coord->x >= pgx->clip.xmax)
coord->x = pgx->clip.xmax;
if(coord->y >= pgx->clip.ymax)
coord->y = pgx->clip.ymax;
if(coord->x < pgx->clip.xmin)
coord->x = pgx->clip.xmin;
if(coord->y < pgx->clip.ymin)
coord->y = pgx->clip.ymin;
};
return;
}
/*.......................................................................
* Convert from scrolled window coordinates to pixmap coordinates.
* The pixmap coordinates will be limited to the bounds of the pixmap.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* w_coord XPoint * The window coordinate to be converted.
* Input/Output:
* p_coord XPoint * The resulting pixmap coordinates.
*/
#ifdef __STDC__
static void pgx_window_to_pixmap(PgxWin *pgx, XPoint *w_coord, XPoint *p_coord)
#else
static void pgx_window_to_pixmap(pgx, w_coord, p_coord)
PgxWin *pgx; XPoint *w_coord; XPoint *p_coord;
#endif
{
if(pgx) {
p_coord->x = w_coord->x + pgx->scroll.x;
p_coord->y = w_coord->y + pgx->scroll.y;
} else {
p_coord->x = w_coord->x;
p_coord->y = w_coord->y;
};
pgx_limit_pcoords(pgx, p_coord);
return;
}
/*.......................................................................
* Convert from pixmap coordinates to scrolled window coordinates.
* The coordinates will be bounded to the size of the pixmap
* before the conversion and bounded to the drawable area of the
* pixmap after the conversion.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* p_coord XPoint * The pixmap coordinate to be converted.
* Input/Output:
* w_coord XPoint * The resulting window coordinates.
*/
#ifdef __STDC__
static void pgx_pixmap_to_window(PgxWin *pgx, XPoint *p_coord, XPoint *w_coord)
#else
static void pgx_pixmap_to_window(pgx, p_coord, w_coord)
PgxWin *pgx; XPoint *p_coord; XPoint *w_coord;
#endif
{
if(pgx) {
pgx_limit_pcoords(pgx, p_coord);
w_coord->x = p_coord->x - pgx->scroll.x;
w_coord->y = p_coord->y - pgx->scroll.y;
pgx_limit_wcoords(pgx, w_coord);
} else {
w_coord->x = p_coord->x;
w_coord->y = p_coord->y;
};
return;
}
/*.......................................................................
* Create and initialize an empty PGPLOT window context descriptor
* at least up to the point at which it can safely be passed to
* del_PgxWin().
*
* Input:
* display Display * The display to associate with the window.
* screen int The screen to associate with the window.
* name char * A name to refer to the window by.
* resize_fn PgxResizeWindowFn The function to call when the window
* needs resizing, or 0 if window resizing is to be
* disallowed.
* pixmap_fn PgxNewPixmapFn The function to call to allocate a new
* pixmap. If no special action is required,
* send 0 and pgx_new_pixmap will be substituted.
* Output:
* return PgxWin * The new container ready to be filled, or NULL
* on error.
*/
#ifdef __STDC__
PgxWin *new_PgxWin(Display *display, int screen, void *context, char *name,
PgxResizeWindowFn resize_fn, PgxNewPixmapFn pixmap_fn)
#else
PgxWin *new_PgxWin(display, screen, context, name, resize_fn, pixmap_fn)
Display *display; int screen; void *context; char *name;
PgxResizeWindowFn resize_fn; PgxNewPixmapFn pixmap_fn;
#endif
{
PgxWin *pgx; /* The new descriptor */
/*
* Check arguments.
*/
if(!display) {
fprintf(stderr, "new_PgxWin: NULL Display intercepted.\n");
return NULL;
};
/*
* Allocate the container.
*/
pgx = (PgxWin *) malloc(sizeof(PgxWin));
if(!pgx) {
fprintf(stderr, "new_PgxWin: Insufficient memory for new PGPLOT window.\n");
return NULL;
};
/*
* Before attemping anything that might fail, initialize the container
* at least up to the point at which it can safely be passed to
* del_PgxWin().
*/
pgx->context = context;
pgx->display = display;
pgx->screen = screen;
pgx->window = None;
pgx->pixmap = None;
pgx->expose_gc = NULL;
pgx->bad_device = 0;
pgx->name = NULL;
pgx->margin = 0.25;
pgx->color = NULL;
pgx->clip.doclip = 0;
pgx->clip.xmin = pgx->clip.xmax = 0;
pgx->clip.ymin = pgx->clip.ymax = 0;
pgx->resize_fn = resize_fn;
pgx->new_pixmap_fn = pixmap_fn ? pixmap_fn : pgx_new_pixmap;
pgx->old_handler = 0;
pgx->state = NULL;
/*
* Allocate a copy of the specified name.
*/
if(!name)
name = "pgxwin";
pgx->name = (char *) malloc(strlen(name) + 1);
if(!pgx->name) {
fprintf(stderr, "new_PgxWin: Insufficient memory to name window.\n");
return del_PgxWin(pgx);
};
strcpy(pgx->name, name);
/*
* The rest of the initialization is driver-specific.
*/
return pgx;
}
/*.......................................................................
* Delete a PgxWin PGPLOT window context container and its contents.
* Note that it is the responsibility of the caller to keep a record of
* and delete the window pgx->window and close the display pgx->display
* when appropriate.
*
* Input:
* pgx PgxWin * The container to be deleted.
* Output:
* return PgxWin * The deleted container (Always NULL).
*/
#ifdef __STDC__
PgxWin *del_PgxWin(PgxWin *pgx)
#else
PgxWin *del_PgxWin(pgx)
PgxWin *pgx;
#endif
{
if(pgx) {
/*
* Make sure that the window is closed to PGPLOT.
* This deletes pgx->state and its contents.
*/
pgx_close(pgx);
/*
* Destroy the pixmap.
*/
if(pgx->display && pgx->pixmap != None)
XFreePixmap(pgx->display, pgx->pixmap);
/*
* Discard the graphical context.
*/
if(pgx->display && pgx->expose_gc)
XFreeGC(pgx->display, pgx->expose_gc);
/*
* Delete the colormap/visual context.
*/
pgx->color = del_PgxColor(pgx, pgx->color);
/*
* Release the memory taken by the window-name string.
*/
if(pgx->name)
free(pgx->name);
/*
* Just in case an application continues to use this descriptor after
* it has been free()'d mark it as bad. This isn't foolproof since the
* next malloc() will probably reuse this memory, but anything that
* might help track down such a lethal problem is worth doing.
*/
pgx->bad_device = 1;
/*
* Finally, free the container.
*/
free(pgx);
};
return NULL;
}
/*.......................................................................
* When copying the off-screen pixmap to the window this function
* should be used in place of XCopyArea() [which it calls]. This function
* clips the copied area to the dimensions of the window clipping
* area in pgx->clip.
*
* Input:
* px,py int The top-left corner of the area to be copied
* from the pixmap.
* w,h unsigned The width and height of the area to be copied.
* wx,wy int The top-left corner of the destination area
* in the window.
*/
#ifdef __STDC__
static int pgx_copy_area(PgxWin *pgx, int px, int py, unsigned w, unsigned h,
int wx, int wy)
#else
static int pgx_copy_area(pgx, px, py, w, h, wx, wy)
PgxWin *pgx; int px; int py; unsigned w; unsigned h;
int wx; int wy;
#endif
{
if(pgx_ready(pgx, PGX_NEED_WINDOW | PGX_NEED_PIXMAP)) {
/*
* Limit the copied area to the drawable area of the window as listed
* in pgx->clip.
*
* First clip the destination X-axis extent to between pgx->clip.xmin and
* pgx->clip.xmax and adjust the source coordinates to suit.
*/
if(pgx->clip.doclip) {
long height = h; /* Signed version of h */
long width = w; /* Signed version of w */
if(wx < pgx->clip.xmin) {
int xdiff = pgx->clip.xmin - wx;
wx += xdiff;
px += xdiff;
width -= xdiff;
};
if(wx + width - 1 > pgx->clip.xmax)
width = pgx->clip.xmax - wx + 1;
/*
* Now clip the destination Y-axis extent to between pgx->clip.ymin and
* pgx->clip.ymax and adjust the source coordinates to suit.
*/
if(wy < pgx->clip.ymin) {
int ydiff = pgx->clip.ymin - wy;
wy += ydiff;
py += ydiff;
height -= ydiff;
};
if(wy + height - 1 > pgx->clip.ymax)
height = pgx->clip.ymax - wy + 1;
/*
* Nothing visible?
*/
if(width <= 0 || height <= 0)
return 0;
w = width;
h = height;
};
/*
* Perform the requested copy.
*/
XCopyArea(pgx->display, pgx->pixmap, pgx->window, pgx->expose_gc,
px, py, w, h, wx, wy);
};
return 0;
}
/*.......................................................................
* When clearing a region of the window this function should be used in
* place of XClearArea() [which it calls]. This function clips the copied
* area to the dimensions of the window clipping area in pgx->clip.
*
* Input:
* x,y int The top-left corner of the area to be cleared
* in the window (window pixel coordinates).
* w,h unsigned The width and height of the area to be cleared.
*/
#ifdef __STDC__
static int pgx_clear_area(PgxWin *pgx, int x, int y, unsigned w, unsigned h)
#else
static int pgx_clear_area(pgx, x, y, w, h)
PgxWin *pgx; int x; int y; unsigned w; unsigned h;
#endif
{
if(pgx_ready(pgx, PGX_NEED_WINDOW)) {
/*
* Limit the cleared area to the drawable area of the window as listed
* in pgx->clip.
*
* First clip the destination X-axis extent to between pgx->clip.xmin and
* pgx->clip.xmax.
*/
if(pgx->clip.doclip) {
long height = h; /* Signed version of h */
long width = w; /* Signed version of w */
if(x < pgx->clip.xmin) {
int xdiff = pgx->clip.xmin - x;
x += xdiff;
width -= xdiff;
};
if(x + width - 1 > pgx->clip.xmax)
width = pgx->clip.xmax - x + 1;
/*
* Now clip the destination Y-axis extent to between pgx->clip.ymin and
* pgx->clip.ymax and adjust the source coordinates to suit.
*/
if(y < pgx->clip.ymin) {
int ydiff = pgx->clip.ymin - y;
y += ydiff;
height -= ydiff;
};
if(y + height - 1 > pgx->clip.ymax)
height = pgx->clip.ymax - y + 1;
/*
* Nothing visible?
*/
if(width <= 0 || height <= 0)
return 0;
w = width;
h = height;
};
/*
* Perform the requested clear.
*/
XClearArea(pgx->display, pgx->window, x, y, w, h, False);
};
return 0;
}
/*.......................................................................
* Change the background color of a window.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* xc XColor * The new background color.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_set_background(PgxWin *pgx, XColor *xc)
#else
int pgx_set_background(pgx, xc)
PgxWin *pgx; XColor *xc;
#endif
{
if(!xc) {
fprintf(stderr, "%s: pgx_set_background: NULL xc argument.", PGX_IDENT);
return 1;
};
if(pgx_ready(pgx, PGX_NEED_COLOR)) {
float r = (float) xc->red / PGX_COLORMULT;
float g = (float) xc->green / PGX_COLORMULT;
float b = (float) xc->blue / PGX_COLORMULT;
/*
* Register the desired color in pgx->color->xcolor[0].
*/
if(pgx_set_rgb(pgx, 0, r, g, b))
return 1;
/*
* Flush the changed color to the X server.
*/
if(pgx->state ? pgx_update_colors(pgx) : pgx_flush_colors(pgx, 0, 1))
return 1;
};
return 0;
}
/*.......................................................................
* Change the foreground color of a window.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* xc XColor * The new foreground color.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_set_foreground(PgxWin *pgx, XColor *xc)
#else
int pgx_set_foreground(pgx, xc)
PgxWin *pgx; XColor *xc;
#endif
{
if(!xc) {
fprintf(stderr, "%s: pgx_set_foreground: NULL xc argument.", PGX_IDENT);
return 1;
};
if(pgx_ready(pgx, PGX_NEED_COLOR)) {
float r = (float) xc->red / PGX_COLORMULT;
float g = (float) xc->green / PGX_COLORMULT;
float b = (float) xc->blue / PGX_COLORMULT;
/*
* Register the desired color in pgx->color->xcolor[0].
*/
if(pgx_set_rgb(pgx, 1, r, g, b))
return 1;
/*
* Flush the changed color to the X server.
*/
if(pgx->state ? pgx_update_colors(pgx) : pgx_flush_colors(pgx, 0, 1))
return 1;
};
return 0;
}
/*.......................................................................
* Return the width of the current pixmap.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* unsigned int The width of the pixmap, or 0 if no pixmap currently
* exists.
*/
#ifdef __STDC__
unsigned pgx_pixmap_width(PgxWin *pgx)
#else
unsigned pgx_pixmap_width(pgx)
PgxWin *pgx;
#endif
{
if(!pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP))
return 0;
return pgx->state->geom.width;
}
/*.......................................................................
* Return the height of the current pixmap.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* unsigned int The height of the pixmap, or 0 if no pixmap currently
* exists.
*/
#ifdef __STDC__
unsigned pgx_pixmap_height(PgxWin *pgx)
#else
unsigned pgx_pixmap_height(pgx)
PgxWin *pgx;
#endif
{
if(!pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP))
return 0;
return pgx->state->geom.height;
}
/*.......................................................................
* Convert from window pixel coordinates to PGPLOT device coordinates.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* x,y int The X window pixel coordinates.
* Input/Output:
* rbuf float * The return array for the X and Y device coordinates.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_win2dev(PgxWin *pgx, int x, int y, float *rbuf)
#else
int pgx_win2dev(pgx, x, y, rbuf)
PgxWin *pgx; int x; int y; float *rbuf;
#endif
{
if(!rbuf) {
fprintf(stderr, "pgx_win2dev: NULL rbuf[].\n");
return 1;
};
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
XPoint coord;
/*
* Convert from window coordinates to pixmap coordinates.
*/
coord.x = x + pgx->scroll.x;
coord.y = y + pgx->scroll.y;
/*
* Convert to PGPLOT device coordinates.
*/
pgx_XPoint_to_xy(pgx, &coord, rbuf);
} else {
rbuf[0] = rbuf[1] = 0.0;
};
return 0;
}
/*.......................................................................
* Convert from PGPLOT device coordinates to X window coordinates.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* rbuf float * The PGPLOT X and Y device coordinates.
* rbuf[0] = X device coordinate.
* rbuf[1] = Y device coordinate.
* Input/Output:
* x,y int * The corresponding X window coordinates.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_dev2win(PgxWin *pgx, float *rbuf, int *x, int *y)
#else
int pgx_dev2win(pgx, rbuf, x, y)
PgxWin *pgx; float *rbuf; int *x; int *y;
#endif
{
if(!rbuf || !x || !y) {
fprintf(stderr, "pgx_dev2win: NULL %s.\n",
!rbuf ? "rbuf[]" : (!x ? "x":"y"));
return 1;
};
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
XPoint coord;
/*
* Convert from PGPLOT device coordinates to pixmap coordinates.
*/
pgx_xy_to_XPoint(pgx, rbuf, &coord);
/*
* Convert from pixmap coordinates to window coordinates.
*/
*x = coord.x - pgx->scroll.x;
*y = coord.y - pgx->scroll.y;
} else {
*x = *y = 0.0;
};
return 0;
}
/*.......................................................................
* Convert from PGPLOT device coordinates to PGPLOT world coordinates.
*
* Note that use of this function requires that opcode 27 be enabled and
* set up to update the world-coordinate scaling via pgx_set_world().
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Input/Output:
* rbuf float * On input this should contain the PGPLOT device
* x and y coordinates. On output it will contain the
* corresponding world coordinates.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_dev2world(PgxWin *pgx, float *rbuf)
#else
int pgx_dev2world(pgx, rbuf)
PgxWin *pgx; float *rbuf;
#endif
{
if(!rbuf) {
fprintf(stderr, "pgx_dev2world: NULL rbuf[].\n");
return 1;
};
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
XWworld *world = &pgx->state->world;
/*
* Convert device coordinates to world coordinates.
*/
rbuf[0] = (rbuf[0] - world->xoff) / world->xdiv;
rbuf[1] = (rbuf[1] - world->yoff) / world->ydiv;
} else {
rbuf[0] = rbuf[1] = 0.0;
};
return 0;
}
/*.......................................................................
* Convert from PGPLOT world coordinates to PGPLOT device coordinates.
*
* Note that use of this function requires that opcode 27 be enabled and
* set up to update the world-coordinate scaling via pgx_set_world().
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Input/Output:
* rbuf float * On input this should contain the PGPLOT world
* x and y coordinates. On output it will contain the
* corresponding PGPLOT device coordinates.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
#ifdef __STDC__
int pgx_world2dev(PgxWin *pgx, float *rbuf)
#else
int pgx_world2dev(pgx, rbuf)
PgxWin *pgx; float *rbuf;
#endif
{
if(!rbuf) {
fprintf(stderr, "pgx_world2dev: NULL rbuf[].\n");
return 1;
};
if(pgx_ready(pgx, PGX_NEED_PGOPEN)) {
XWworld *world = &pgx->state->world;
/*
* Convert world coordinates to device coordinates.
*/
rbuf[0] = rbuf[0] * world->xdiv + world->xoff;
rbuf[1] = rbuf[1] * world->ydiv + world->yoff;
} else {
rbuf[0] = rbuf[1] = 0.0;
};
return 0;
}
/*.......................................................................
* Scroll a rectangular area vertically and/or horizontally.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* rbuf float * The array of float arguments sent by the PGPLOT
* GREXEC() subroutine.
*/
#ifdef __STDC__
void pgx_scroll_rect(PgxWin *pgx, float *rbuf)
#else
void pgx_scroll_rect(pgx, rbuf)
PgxWin *pgx; float *rbuf;
#endif
{
if(pgx_ready(pgx, PGX_NEED_PGOPEN | PGX_NEED_PIXMAP | PGX_NEED_COLOR)) {
XPoint blc, trc; /* The bottom left and top right rectangle corners */
XPoint blc_orig, trc_orig; /* The vertices of the rectangle to be copied */
XPoint blc_dest, trc_dest; /* The vertices of the destination of the copy */
int dx, dy; /* The amounts to scroll right and down */
/*
* Convert the rectangle vertices from PGPLOT coordinates to X coordinates.
*/
pgx_xy_to_XPoint(pgx, &rbuf[0], &blc);
pgx_xy_to_XPoint(pgx, &rbuf[2], &trc);
/*
* Get the scroll offsets in X coordinates.
*/
dx = pgx_nint(rbuf[4]);
dy = pgx_nint(-rbuf[5]);
/*
* Selected parts of the pixmap will need to be erased by drawing an
* opaque rectangle over them in the background color. Establish
* the background color in the exposure graphical context to avoid
* changing the current foreground color.
*/
XSetForeground(pgx->display, pgx->expose_gc, pgx->color->pixel[0]);
/*
* If either scroll extent exceeds the length of the associated
* axis, then fill the area with the background color.
*/
if(abs(dx) > trc.x - blc.x || abs(dy) > blc.y - trc.y) {
/*
* Fill the rectangle in the pixmap.
*/
XFillRectangle(pgx->display, pgx->pixmap, pgx->expose_gc, blc.x, trc.y,
(unsigned)(trc.x-blc.x+1), (unsigned)(blc.y-trc.y+1));
} else {
/*
* Calculate the vertices of the source and destination rectangles to
* be copied.
*/
blc_orig = blc_dest = blc;
trc_orig = trc_dest = trc;
if(dx > 0) {
trc_orig.x = trc.x - dx;
blc_dest.x = blc.x + dx;
} else if(dx < 0) {
blc_orig.x = blc.x - dx;
trc_dest.x = trc.x + dx;
};
if(dy > 0) {
blc_orig.y = blc.y - dy;
trc_dest.y = trc.y + dy;
} else if(dy < 0) {
trc_orig.y = trc.y - dy;
blc_dest.y = blc.y + dy;
};
/*
* Constrain the coordinates to lie within the pixmap.
*/
pgx_limit_pcoords(pgx, &blc_orig);
pgx_limit_pcoords(pgx, &blc_dest);
pgx_limit_pcoords(pgx, &trc_orig);
pgx_limit_pcoords(pgx, &trc_dest);
/*
* Scroll the rectangle to its shifted location.
*/
XCopyArea(pgx->display, pgx->pixmap, pgx->pixmap, pgx->expose_gc,
blc_orig.x, trc_orig.y,
trc_orig.x - blc_orig.x + 1,
blc_orig.y - trc_orig.y + 1,
blc_dest.x, trc_dest.y);
/*
* Clear the vacated area to the left or right of the copied area.
*/
if(dx > 0) {
XFillRectangle(pgx->display, pgx->pixmap, pgx->expose_gc,
blc.x, trc.y,
(unsigned) dx,
(unsigned) (blc.y - trc.y + 1));
} else if(dx < 0) {
XFillRectangle(pgx->display, pgx->pixmap, pgx->expose_gc,
trc_dest.x, trc.y,
(unsigned) (-dx),
(unsigned) (blc.y - trc.y + 1));
};
/*
* Clear the vacated area above or below the copied area.
*/
if(dy > 0) {
XFillRectangle(pgx->display, pgx->pixmap, pgx->expose_gc,
blc.x, trc.y,
(unsigned) (trc.x - blc.x + 1),
(unsigned) dy);
} else if(dy < 0) {
XFillRectangle(pgx->display, pgx->pixmap, pgx->expose_gc,
blc.x, blc_dest.y,
(unsigned) (trc.x - blc.x + 1),
(unsigned) (-dy));
};
};
/*
* Record the extent of the modified part of the pixmap.
*/
pgx_mark_modified(pgx, blc.x, blc.y, 1);
pgx_mark_modified(pgx, trc.x, trc.y, 1);
};
return;
}
/*.......................................................................
* Return the nearest integer to a given floating point number.
*
* Input:
* f float The floating point number to be rounded.
* Output:
* return int The nearest integer to f.
*/
#ifdef __STDC__
static int pgx_nint(float f)
#else
static int pgx_nint(f)
float f;
#endif
{
return (int) (f >= 0.0 ? (f + 0.5) : (f - 0.5));
}
/*.......................................................................
* Find the parent window of a PGPLOT window. Note that we can't cache
* this because reparenting window managers change the parent of
* top-level windows.
*
* Input:
* pgx PgxWin * The PGPLOT window context.
* Output:
* return Window The parent window, or None on error.
*/
#ifdef __STDC__
Window pgx_parent_window(PgxWin *pgx)
#else
Window pgx_parent_window(pgx)
PgxWin *pgx;
#endif
{
Window root;
Window parent;
Window *children;
unsigned int nchildren;
if(!XQueryTree(pgx->display, pgx->window, &root, &parent,
&children, &nchildren)) {
fprintf(stderr, "pgx_parent_window: XQueryTree failed.\n");
pgx->bad_device = 1;
return None;
};
/*
* Discard the unwanted list of children.
*/
XFree(children);
return parent;
}
|