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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#ifdef USE_RIVET
#include "rvpgplot.h" /* This includes rivet.h (which includes tcl.h & tk.h) */
#else
#include <tk.h>
#include "tkpgplot.h" /* This includes tcl.h */
#endif
#include "pgxwin.h"
/*
* VAX VMS includes etc..
*/
#ifdef VMS
#include <descrip.h>
#include <ssdef.h>
typedef struct dsc$descriptor_s VMS_string;
#define VMS_STRING(dsc, string) \
dsc.dsc$w_length = strlen(string); \
dsc.dsc$b_dtype = DSC$K_DTYPE_T; \
dsc.dsc$b_class = DSC$K_CLASS_S; \
dsc.dsc$a_pointer = string;
#endif
/*
* Compose the pgplot-callable driver function name.
* Allow tkdriv to be calleable by FORTRAN using the two commonest
* calling conventions. Both conventions append length arguments for
* each FORTRAN string at the end of the argument list, and convert the
* name to lower-case, but one post-pends an underscore to the function
* name (PG_PPU) while the other doesn't. Note the VMS is handled
* separately below. For other calling conventions you must write a
* C wrapper routine to call tkdriv() or tkdriv_().
*/
#ifdef PG_PPU
#ifdef RIVET
#define DRIV rvdriv_ /* Rivet with PG_PPU defined */
#else
#define DRIV tkdriv_ /* Normal Tk with PG_PPU defined */
#endif
#else
#ifdef RIVET
#define DRIV rvdriv /* Rivet with PG_PPU undefined */
#else
#define DRIV tkdriv /* Normal Tk with PG_PPU undefined */
#endif
#endif
/*
* List widget defaults. Note that the macros that are prefixed
* TKPG_STR_ are for use in the configSpecs resource database. These
* have to be strings.
*/
#define TKPG_MIN_WIDTH 64 /* Minimum width (pixels) */
#define TKPG_MIN_HEIGHT 64 /* Minimum height (pixels) */
#define TKPG_DEF_WIDTH 256 /* Default width (pixels) */
#define TKPG_STR_DEF_WIDTH "256" /* String version of TKPG_DEF_WIDTH */
#define TKPG_DEF_HEIGHT 256 /* Default height (pixels) */
#define TKPG_STR_DEF_HEIGHT "256" /* String version of TKPG_DEF_HEIGHT */
#define TKPG_MIN_COLORS 2 /* Min number of colors per colormap */
#define TKPG_STR_MIN_COLORS "2" /* String version of TKPG_MIN_COLORS */
#define TKPG_DEF_COLORS 100 /* Default number of colors to try for */
#define TKPG_STR_DEF_COLORS "100" /* String version of TKPG_DEF_COLORS */
#define TKPG_MAX_COLORS 255 /* Max number of colors per colormap */
#define TKPG_DEF_HIGHLIGHT_WIDTH 2 /* Default width of traversal highlight */
#define TKPG_STR_DEF_HIGHLIGHT_WIDTH "2"/* String ver of TKPG_DEF_HIGHLIGHT_WIDTH */
#define TKPG_STR_MARGIN_DEF "20" /* The default number of pixels of */
/* extra space to allocate around the */
/* edge of the plot area. */
/*
* Specify the name to prefix errors with.
*/
#define TKPG_IDENT "PgplotWidget"
typedef struct TkPgplot TkPgplot;
/*
* Declare a container for a list of PGPLOT widgets.
*/
typedef struct {
TkPgplot *head; /* The head of the list of widgets */
} TkPgplotList;
/*
* A context descriptor for managing parent ScrolledWindow scroll-bars.
*/
typedef struct {
#ifdef RIVET
Callback xScrollCmd; /* Rivet X-axis update-scrollbar callback */
Callback yScrollCmd; /* Rivet Y-axis update-scrollbar callback */
#else
char *xScrollCmd; /* Tcl X-axis scrollbar-update command */
char *yScrollCmd; /* Tcl Y-axis scrollbar-update command */
#endif
unsigned x; /* Pixmap X coordinate of top left corner of window */
unsigned y; /* Pixmap Y coordinate of top left corner of window */
} TkpgScroll;
/*
* This container records state-values that are modified by X events.
*/
typedef struct {
unsigned long mask; /* Event mask registered to tkpg_EventHandler() */
int focus_acquired; /* True when we have keyboard-input focus */
int cursor_active; /* True when cursor augmentation is active */
} TkpgEvents;
struct TkPgplot {
#ifdef RIVET
RIVET_CLASS_DECL
#endif
/* Widget context */
Tk_Window tkwin; /* Tk's window object */
Display *display; /* The X display of the window */
Tcl_Interp *interp; /* The application's TCL interpreter */
char buffer[81]; /* A work buffer for constructing result strings */
/* Public resource attributes */
int max_colors; /* The max number of colors needed */
int min_colors; /* The min number of colors needed */
int req_width; /* The requested widget width (pixels) */
int req_height; /* The requested widget height (pixels) */
int highlight_thickness; /* The width of the highlight border */
XColor *highlightBgColor; /* The inactive traversal highlight color */
XColor *highlightColor; /* The active traversal highlight color */
XColor *normalFg; /* Normal foreground color (color index 1) */
Tk_3DBorder border; /* 3D border structure */
int borderWidth; /* The width of the 3D border */
int relief; /* Relief of the 3D border */
char *takeFocus; /* "1" to allow focus traversal, "0" to disallow */
Cursor cursor; /* The active cursor of the window */
int share; /* True if shared colors are desired */
int padx,pady; /* Extra padding margin widths (pixels) */
/* Private attributes */
TkPgplot *next; /* The next widget of a list of PGPLOT Xt widgets */
int tkslct_id; /* The device ID returned to PGPLOT by the */
/* open-workstation driver opcode, and used for */
/* subsequent device selection via the */
/* select-plot driver opcode */
int pgslct_id; /* The device ID returned to the application by */
/* pgopen() for subsequent device selection with */
/* the pgslct() function */
char *device; /* A possible PGPLOT cpgbeg() file string */
TkpgScroll scroll; /* Used to maintain parent scroll bars */
TkpgEvents events; /* X event context */
PgxWin *pgx; /* PGPLOT generic X-window context descriptor */
};
static TkPgplot *new_TkPgplot(Tcl_Interp *interp, Tk_Window main_w, char *name,
int argc, char *argv[]);
static TkPgplot *del_TkPgplot(TkPgplot *tkpg);
/*
* Describe all recognized widget resources.
*/
static Tk_ConfigSpec configSpecs[] = {
{TK_CONFIG_BORDER,
"-background", "background", "Background",
"Black", Tk_Offset(TkPgplot, border), 0},
{TK_CONFIG_SYNONYM,
"-bg", "background", (char *) NULL, NULL, 0, 0},
{TK_CONFIG_COLOR,
"-foreground", "foreground", "Foreground",
"White", Tk_Offset(TkPgplot, normalFg), 0},
{TK_CONFIG_SYNONYM,
"-fg", "foreground", (char *) NULL, NULL, 0, 0},
{TK_CONFIG_ACTIVE_CURSOR,
"-cursor", "cursor", "Cursor",
"", Tk_Offset(TkPgplot, cursor), TK_CONFIG_NULL_OK},
{TK_CONFIG_PIXELS,
"-borderwidth", "borderWidth", "BorderWidth",
"0", Tk_Offset(TkPgplot, borderWidth), 0},
{TK_CONFIG_SYNONYM,
"-bd", "borderWidth", (char *) NULL, NULL, 0, 0},
{TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
"raised", Tk_Offset(TkPgplot, relief), 0},
{TK_CONFIG_PIXELS,
"-height", "height", "Height",
TKPG_STR_DEF_HEIGHT, Tk_Offset(TkPgplot, req_height), 0},
{TK_CONFIG_PIXELS,
"-width", "width", "Width",
TKPG_STR_DEF_WIDTH, Tk_Offset(TkPgplot, req_width), 0},
{TK_CONFIG_COLOR,
"-highlightbackground", "highlightBackground", "HighlightBackground",
"grey", Tk_Offset(TkPgplot, highlightBgColor), 0},
{TK_CONFIG_COLOR,
"-highlightcolor", "highlightColor", "HighlightColor",
"White", Tk_Offset(TkPgplot, highlightColor), 0},
{TK_CONFIG_PIXELS,
"-highlightthickness", "highlightThickness", "HighlightThickness",
TKPG_STR_DEF_HIGHLIGHT_WIDTH, Tk_Offset(TkPgplot, highlight_thickness), 0},
{TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
"0", Tk_Offset(TkPgplot, takeFocus), TK_CONFIG_NULL_OK},
#ifdef RIVET
{TK_CONFIG_CUSTOM,
"-xscrollcommand", "xScrollCommand", "ScrollCommand",
0, Tk_Offset(TkPgplot, scroll.xScrollCmd),
TK_CONFIG_NULL_OK, &rivet_custom_callback_option},
#else
{TK_CONFIG_STRING,
"-xscrollcommand", "xScrollCommand", "ScrollCommand",
"", Tk_Offset(TkPgplot, scroll.xScrollCmd),
TK_CONFIG_NULL_OK},
#endif
#ifdef RIVET
{TK_CONFIG_CUSTOM,
"-yscrollcommand", "yScrollCommand", "ScrollCommand",
0, Tk_Offset(TkPgplot, scroll.yScrollCmd),
TK_CONFIG_NULL_OK, &rivet_custom_callback_option},
#else
{TK_CONFIG_STRING,
"-yscrollcommand", "yScrollCommand", "ScrollCommand",
"", Tk_Offset(TkPgplot, scroll.yScrollCmd),
TK_CONFIG_NULL_OK},
#endif
{TK_CONFIG_INT,
"-mincolors", "minColors", "MinColors",
TKPG_STR_MIN_COLORS, Tk_Offset(TkPgplot, min_colors), 0},
{TK_CONFIG_INT,
"-maxcolors", "maxColors", "MaxColors",
TKPG_STR_DEF_COLORS, Tk_Offset(TkPgplot, max_colors), 0},
{TK_CONFIG_BOOLEAN,
"-share", "share", "Share",
0, Tk_Offset(TkPgplot, share), 0},
{TK_CONFIG_PIXELS,
"-padx", "padX", "Pad",
TKPG_STR_MARGIN_DEF, Tk_Offset(TkPgplot, padx), 0},
{TK_CONFIG_PIXELS,
"-pady", "padY", "Pad",
TKPG_STR_MARGIN_DEF, Tk_Offset(TkPgplot, pady), 0},
{TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
(char *) NULL, 0, 0}
};
/* Enumerate the PGPLOT class widget lists */
#define TKPG_ACTIVE_WIDGETS 1
#define TKPG_FREE_WIDGETS 2
static TkPgplotList *tkpg_WidgetList(int type);
static TkPgplot *tkpg_FindWidgetByName(char *name, int type, TkPgplot **prev);
static TkPgplot *tkpg_FindWidgetByID(int tkslct_id, int type, TkPgplot **prev);
static TkPgplot *tkpg_RemoveWidget(char *name, int type);
static TkPgplot *tkpg_PrependWidget(TkPgplot *tkpg, int type);
static TkPgplot *tkpg_CurrentWidget(char *context);
static TkPgplot *tkpg_open_widget(char *name);
static TkPgplot *tkpg_close_widget(char *name);
static void tkpg_NewPixmap(PgxWin *pgx, unsigned width, unsigned height);
static void tkpg_update_scroll_bars(TkPgplot *tkpg);
static void tkpg_update_clip(TkPgplot *tkpg);
static void tkpg_update_border(TkPgplot *tkpg);
static int PgplotCmd(ClientData context, Tcl_Interp *interp, int argc,
char *argv[]);
static int tkpg_InstanceCommand(ClientData context, Tcl_Interp *interp,
int argc, char *argv[]);
static int tkpg_InstanceCommand_return(ClientData context, int iret);
static int tkpg_Configure(TkPgplot *tkpg, Tcl_Interp *interp,
int argc, char *argv[], int flags);
static void tkpg_expose_handler(TkPgplot *tkpg, XEvent *event);
static void tkpg_draw_focus_highlight(TkPgplot *tkpg);
static void tkpg_draw_3d_border(TkPgplot *tkpg);
static int tkpg_refresh_window(TkPgplot *tkpg);
static void tkpg_ClrCursor(TkPgplot *tkpg);
static void tkpg_EventHandler(ClientData context, XEvent *event);
static void tkpg_CursorHandler(ClientData context, XEvent *event);
static Tk_Window tkpg_toplevel_of_path(Tcl_Interp *interp, Tk_Window main_w,
char *path);
/*
* Enumerate supported pgband() cursor types.
*/
typedef enum {
TKPG_NORM_CURSOR = 0, /* Un-augmented X cursor */
TKPG_LINE_CURSOR = 1, /* Line cursor between ref and pointer */
TKPG_RECT_CURSOR = 2, /* Rectangular cursor between ref and pointer */
TKPG_YRNG_CURSOR = 3, /* Two horizontal lines, at ref.x and pointer.x */
TKPG_XRNG_CURSOR = 4, /* Two vertical lines, at ref.y and pointer.y */
TKPG_HLINE_CURSOR = 6, /* Horizontal line cursor at y=ref.y */
TKPG_VLINE_CURSOR = 5, /* Vertical line cursor at x=ref.x */
TKPG_CROSS_CURSOR = 7 /* Cross-hair cursor centered on the pointer */
} TkpgCursorMode;
static int tkpg_SetCursor(TkPgplot *tkpg, TkpgCursorMode mode,
float xref, float yref, int ci);
#ifdef RIVET
static void tkpg_FreeProc(ClientData context);
#else
static void tkpg_FreeProc(char *context);
#endif
static int tkpg_scrollbar_callback(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, char *view, int argc,
char *argv[]);
static int tkpg_scrollbar_error(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, char *view, int argc,
char *argv[]);
static int tkpg_tcl_setcursor(TkPgplot *tkpg, Tcl_Interp *interp,
int argc, char *argv[]);
static int tkpg_tcl_world(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, int argc, char *argv[]);
static int tkpg_tcl_pixel(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, int argc, char *argv[]);
static int tkpg_tcl_id(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, int argc, char *argv[]);
static int tkpg_tcl_device(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, int argc, char *argv[]);
#ifdef RIVET
static void del_RvPgplot(ClientData obj);
static Rivet_class_struct PgplotClassObj = {
0,
"Pgplot",
PgplotCmd,
tkpg_InstanceCommand,
del_RvPgplot,
0,
configSpecs,
0,
};
Rivetclass PgplotClass = &PgplotClassObj;
#endif
/*
* The following file-scope container records the list of active and
* inactive PGPLOT widgets.
*/
static struct {
int id_counter; /* Used to give widgets unique identifiers */
TkPgplotList active_widgets; /* List of active widgets */
TkPgplotList free_widgets; /* List of unnassigned widgets */
} tkPgplotClassRec = {
0, /* id_counter */
{NULL}, /* active_widgets */
{NULL}, /* free_widgets */
};
/*
* The following macro defines the event mask used by the cursor event
* handler. It is here to ensure that Tk_CreateEventHandler() and
* Tk_DeleteEventHandler() are presented with identical event masks.
*/
#define CURSOR_EVENT_MASK ((unsigned long)(EnterWindowMask | LeaveWindowMask | \
PointerMotionMask))
/*
* The following macro defines the event mask normally used by the widget.
*/
#define NORMAL_EVENT_MASK ((unsigned long)(StructureNotifyMask | \
ExposureMask | FocusChangeMask))
/*.......................................................................
* Provide a package initialization procedure. This creates the Tcl
* "pgplot" widget creation command.
*
* Input:
* interp Tcl_Interp * The TCL interpreter of the application.
* Output:
* return int TCL_OK - Success.
* TCL_ERROR - Failure.
*/
#ifdef RIVET
int Rvpgplot_Init(Tcl_Interp *interp)
#else
int Tkpgplot_Init(Tcl_Interp *interp)
#endif
{
/*
* Get the main window of the application.
*/
Tk_Window main_w = Tk_MainWindow(interp);
/*
* If Tk_Init() hasn't been called, then there won't be a main window
* yet. In such cases, Tk_MainWindow() places a suitable error message
* in interp->result.
*/
if(!main_w)
return TCL_ERROR;
/*
* Create the TCL command that is to be used for creating PGPLOT widgets.
*/
Tcl_CreateCommand(interp, "pgplot", PgplotCmd, (ClientData) main_w, 0);
return TCL_OK;
}
/*.......................................................................
* This function provides the TCL command that creates a PGPLOT widget.
*
* Input:
* context ClientData The client_data argument specified in
* TkPgplot_Init() when PgplotCmd was registered.
* This is the main window cast to (ClientData).
* interp Tcl_Interp * The TCL intrepreter.
* argc int The number of command arguments.
* argv char ** The array of 'argc' command arguments.
* argv[0] = "pgplot"
* argv[1] = the name to give the new widget.
* argv[2..argc-1] = attribute settings.
* Output:
* return int TCL_OK - Success.
* TCL_ERROR - Failure.
*/
static int PgplotCmd(ClientData context, Tcl_Interp *interp, int argc,
char *argv[])
{
Tk_Window main_tkw = (Tk_Window)context; /* The application main window */
TkPgplot *tkpg; /* The new widget instance object */
/*
* Make sure that a name for the new widget has been provided.
*/
if(argc < 2) {
Tcl_AppendResult(interp, "Wrong number of arguments - should be \'",
argv[0], " pathName \?options\?\'", NULL);
return TCL_ERROR;
};
/*
* Allocate the widget-instance object.
*/
tkpg = new_TkPgplot(interp, main_tkw, argv[1], argc-2, argv+2);
if(!tkpg)
return TCL_ERROR;
return TCL_OK;
}
/*.......................................................................
* Create a new widget instance object.
*
* Input:
* interp Tcl_Interp * The TCL interpreter object.
* main_w Tk_Window The main window of the application.
* name char * The name to give the new widget.
* argc int The number of argument in argv[]
* argv char ** Any configuration arguments.
* Output:
* return TkPgplot * The new PGPLOT widget, or NULL on error.
* If NULL is returned then the context of the
* error will have been recorded in the result
* field of the interpreter.
*/
static TkPgplot *new_TkPgplot(Tcl_Interp *interp, Tk_Window main_w, char *name,
int argc, char *argv[])
{
TkPgplot *tkpg; /* The new widget object */
PgxWin *pgx; /* The PGPLOT X window object of the widget */
Tk_Window top_w; /* The top-level window parent of 'name' */
/*
* Get the toplevel window associated with the pathname in 'name'.
*/
top_w = tkpg_toplevel_of_path(interp, main_w, name);
if(!top_w)
return NULL;
/*
* Allocate the container.
*/
tkpg = (TkPgplot *) malloc(sizeof(TkPgplot));
if(!tkpg) {
Tcl_AppendResult(interp, "Insufficient memory to create ", name, NULL);
return NULL;
};
/*
* Before attempting any operation that might fail, initialize the container
* at least up to the point at which it can safely be passed to
* del_TkPgplot().
*/
tkpg->tkwin = NULL;
tkpg->display = Tk_Display(main_w);
tkpg->interp = interp;
tkpg->max_colors = TKPG_DEF_COLORS;
tkpg->min_colors = TKPG_MIN_COLORS;
tkpg->req_width = TKPG_DEF_WIDTH;
tkpg->req_height = TKPG_DEF_HEIGHT;
tkpg->highlight_thickness = TKPG_DEF_HIGHLIGHT_WIDTH;
tkpg->highlightBgColor = NULL;
tkpg->highlightColor = NULL;
tkpg->normalFg = NULL;
tkpg->border = NULL;
tkpg->borderWidth = 0;
tkpg->relief = TK_RELIEF_RAISED;
tkpg->takeFocus = NULL;
tkpg->cursor = None;
tkpg->share = 0;
tkpg->padx = 0;
tkpg->pady = 0;
tkpg->next = NULL;
tkpg->tkslct_id = tkPgplotClassRec.id_counter++;
tkpg->pgslct_id = 0;
tkpg->device = NULL;
tkpg->scroll.xScrollCmd = NULL;
tkpg->scroll.yScrollCmd = NULL;
tkpg->scroll.x = 0;
tkpg->scroll.y = 0;
tkpg->events.mask = NoEventMask;
tkpg->events.focus_acquired = 0;
tkpg->events.cursor_active = 0;
tkpg->pgx = NULL;
/*
* Allocate the PGPLOT-window context descriptor.
*/
pgx = tkpg->pgx = new_PgxWin(tkpg->display, Tk_ScreenNumber(top_w),
(void *) tkpg, name, 0, tkpg_NewPixmap);
if(!pgx) {
Tcl_AppendResult(interp, "Unable to create Pgplot window object for: ",
name, NULL);
return NULL;
};
/*
* Compose a sample PGPLOT device-specification for use in opening this
* widget to PGPLOT.
*/
tkpg->device = (char *) malloc(sizeof(char) *
(strlen(name)+1+strlen(TK_PGPLOT_DEVICE)+1));
if(!tkpg->device) {
Tcl_AppendResult(interp, "Insufficient memory for ", name, NULL);
return NULL;
};
sprintf(tkpg->device, "%s/%s", name, TK_PGPLOT_DEVICE);
/*
* Ensure that the toplevel window parent of the new window exists,
* before attempting to determine its visual.
*/
Tk_MakeWindowExist(top_w);
/*
* Create the widget window from the specified path.
*/
tkpg->tkwin = Tk_CreateWindowFromPath(interp, main_w, name, NULL);
if(!tkpg->tkwin)
return del_TkPgplot(tkpg);
/*
* Give the widget a class name.
*/
Tk_SetClass(tkpg->tkwin, "Pgplot");
/*
* Register an event handler.
*/
tkpg->events.mask = NORMAL_EVENT_MASK;
Tk_CreateEventHandler(tkpg->tkwin, tkpg->events.mask, tkpg_EventHandler,
(ClientData) tkpg);
/*
* Create the TCL command that will allow users to configure the widget.
*/
Tcl_CreateCommand(interp, name, tkpg_InstanceCommand, (ClientData) tkpg, 0);
/*
* Parse command line defaults into tkpg so that tkpg->min_colors,
* tkpg->max_colors and tkpg->share are known.
*/
if(Tk_ConfigureWidget(interp, tkpg->tkwin, configSpecs, argc, argv,
(char *) tkpg, 0) == TCL_ERROR)
return del_TkPgplot(tkpg);
/*
* If requested, try to allocate read/write colors.
* If this fails arrange to try shared colors.
*/
if(!tkpg->share && !pgx_window_visual(pgx, Tk_WindowId(top_w),
tkpg->min_colors, tkpg->max_colors, 0))
tkpg->share = 1;
/*
* Allocate shared colors?
*/
if(tkpg->share) {
if(!pgx_window_visual(pgx, Tk_WindowId(top_w), tkpg->min_colors,
tkpg->max_colors, 1)) {
Tcl_AppendResult(interp, "Unable to allocate any colors for ",name,NULL);
return del_TkPgplot(tkpg);
};
};
/*
* Force Tk to create the window.
*/
Tk_MakeWindowExist(tkpg->tkwin);
/*
* Fill in details about the window in pgx.
*/
pgx->window = Tk_WindowId(tkpg->tkwin);
/*
* Create and initialize a graphical context descriptor. This is where
* Line widths, line styles, fill styles, plot color etc.. are
* recorded.
*/
{
XGCValues gcv;
gcv.graphics_exposures = False;
pgx_start_error_watch(pgx);
pgx->expose_gc = XCreateGC(pgx->display, pgx->window, (unsigned long)
(GCGraphicsExposures), &gcv);
if(pgx_end_error_watch(pgx) || pgx->expose_gc==NULL) {
Tcl_AppendResult(interp,
"Failed to allocate a graphical context for ", name, NULL);
return del_TkPgplot(tkpg);
};
};
/*
* Parse the command-line arguments again and install the relevant
* defaults into the color descriptor created by pgx_window_visual().
*/
if(tkpg_Configure(tkpg, interp, argc, argv, 0))
return del_TkPgplot(tkpg);
/*
* If the widget has scroll-bars make sure that they agree with the
* window.
*/
tkpg_update_scroll_bars(tkpg);
tkpg_update_clip(tkpg);
/*
* Replace the share configuration attribute with the actual
* value that was acheived.
*/
tkpg->share = pgx->color->readonly;
/*
* Prepend the new widget to the list of unassigned widgets to be
* used by pgbeg().
*/
tkpg_PrependWidget(tkpg, TKPG_FREE_WIDGETS);
/*
* Return the widget name.
*/
Tcl_SetResult(interp, Tk_PathName(tkpg->tkwin), TCL_STATIC);
return tkpg;
}
/*.......................................................................
* Delete a TkPgplot widget.
*
* Input:
* tkpg TkPgplot * The widget to be deleted.
* Output:
* return TkPgplot * Always NULL.
*/
static TkPgplot *del_TkPgplot(TkPgplot *tkpg)
{
if(tkpg) {
if(tkpg->pgx) {
PgxWin *pgx = tkpg->pgx;
/*
* Remove the device from the appropriate list of PGPLOT widgets.
*/
tkpg_RemoveWidget(pgx->name, pgx->state ? TKPG_ACTIVE_WIDGETS :
TKPG_FREE_WIDGETS);
/*
* Delete the Tcl command attached to the widget.
*/
Tcl_DeleteCommand(tkpg->interp, pgx->name);
/*
* Delete the window context descriptor.
*/
tkpg->pgx = del_PgxWin(tkpg->pgx);
};
/*
* Delete the device name string.
*/
if(tkpg->device)
free(tkpg->device);
tkpg->device = NULL;
/*
* Clear the cursor.
*/
tkpg_ClrCursor(tkpg);
/*
* Delete resource values.
*/
if(tkpg->display)
Tk_FreeOptions(configSpecs, (char *) tkpg, tkpg->display, 0);
/*
* Remove the DestroyNotify event handler before destroying the
* window. Otherwise this function would call itself recursively
* and pgx would be free'd twice.
*/
if(tkpg->events.mask != NoEventMask) {
Tk_DeleteEventHandler(tkpg->tkwin, tkpg->events.mask,
tkpg_EventHandler, (ClientData) tkpg);
tkpg->events.mask = NoEventMask;
};
/*
* Zap the window.
*/
if(tkpg->tkwin) {
Tk_DestroyWindow(tkpg->tkwin);
tkpg->tkwin = NULL;
};
/*
* Delete the container.
*/
free(tkpg);
};
return NULL;
}
#ifdef RIVET
/*.......................................................................
* This is a rivet-friendly wrapper around del_TkPgplot().
*/
static void del_RvPgplot(ClientData obj)
{
del_TkPgplot((TkPgplot *) obj);
}
#endif
/*.......................................................................
* This function is called upon by the pgxwin toolkit whenever the
* pixmap used as backing store needs to be resized.
*
* Input:
* pgx PgxWin * The pgxwin toolkit context descriptor.
* width unsigned The desired new pixmap width.
* height unsigned The desired new pixmap height.
*/
static void tkpg_NewPixmap(PgxWin *pgx, unsigned width, unsigned height)
{
TkPgplot *tkpg = (TkPgplot *) pgx->context;
/*
* Reset the scrollbars then hand the job of allocating the
* pixmap back to the pgxwin toolkit.
*/
tkpg->scroll.x = 0;
tkpg->scroll.y = 0;
tkpg_update_scroll_bars(tkpg);
pgx_new_pixmap(pgx, width, height);
return;
}
/*.......................................................................
* Whenever the size of a pixmap and/or window of a PGPLOT winget are
* changed, this function should be called to adjust scroll bars.
*
* Input:
* tkpg TkPgplot * The pgplot widget instance.
*/
static void tkpg_update_scroll_bars(TkPgplot *tkpg)
{
TkpgScroll *scroll = &tkpg->scroll;
#ifndef RIVET
char scroll_args[60]; /* Scrollbar set-command arguments */
#endif
/*
* Block widget deletion, so that if one of the scroll-bar callbacks
* deletes the widget we won't end up using a deleted tkpg pointer.
*/
Tk_Preserve((ClientData)tkpg);
/*
* Update the horizontal scroll-bar if there is one.
*/
if(scroll->xScrollCmd) {
double pixmap_width = pgx_pixmap_width(tkpg->pgx);
double first, last;
if(pixmap_width < 1.0) {
first = 0.0;
last = 1.0;
} else {
first = scroll->x / pixmap_width;
last = (scroll->x + Tk_Width(tkpg->tkwin)) / pixmap_width;
};
#ifdef RIVET
rivet_scrollbar_update((Rivetobj)tkpg, scroll->xScrollCmd, first, last);
#else
sprintf(scroll_args, " %f %f", first, last);
(void) Tcl_VarEval(tkpg->interp, scroll->xScrollCmd, scroll_args, NULL);
#endif
};
/*
* Update the vertical scroll-bar if there is one.
*/
if(scroll->yScrollCmd) {
double pixmap_height = pgx_pixmap_height(tkpg->pgx);
double first, last;
if(pixmap_height < 1.0) {
first = 0.0;
last = 1.0;
} else {
first = scroll->y / pixmap_height;
last = (scroll->y + Tk_Height(tkpg->tkwin)) / pixmap_height;
};
#ifdef RIVET
rivet_scrollbar_update((Rivetobj)tkpg, scroll->yScrollCmd, first, last);
#else
sprintf(scroll_args, " %f %f", first, last);
(void) Tcl_VarEval(tkpg->interp, scroll->yScrollCmd, scroll_args, NULL);
#endif
};
/*
* Tell pgplot about the current scroll and pan values.
*/
pgx_scroll(tkpg->pgx, scroll->x, scroll->y);
/*
* Unblock widget deletion.
*/
Tk_Release((ClientData)tkpg);
return;
}
/*.......................................................................
* Update the clip-area of the window to prevent pgxwin functions from
* drawing over the highlight-borders.
*
* Input:
* tkpg TkPgplot * The pgplot widget instance.
*/
static void tkpg_update_clip(TkPgplot *tkpg)
{
(void) pgx_update_clip(tkpg->pgx, 1, Tk_Width(tkpg->tkwin),
Tk_Height(tkpg->tkwin),
tkpg->highlight_thickness + tkpg->borderWidth);
}
/*.......................................................................
* Find an inactive PGPLOT widget of a given name, open it to PGPLOT,
* and move it to the head of the active list of widgets.
*
* Input:
* name char * The name of the widget to be opened.
* Output:
* tkpg TkPgplot * The selected widget, or NULL on error.
*/
static TkPgplot *tkpg_open_widget(char *name)
{
TkPgplot *tkpg;
/*
* Remove the named widget from the free-widget list.
*/
tkpg = tkpg_RemoveWidget(name, TKPG_FREE_WIDGETS);
if(!tkpg) {
if(tkpg_FindWidgetByName(name, TKPG_ACTIVE_WIDGETS, NULL)) {
fprintf(stderr, "%s: Widget %s is already open.\n", TKPG_IDENT, name);
} else {
fprintf(stderr, "%s: Can't open non-existent widget (%s).\n",
TKPG_IDENT, name ? name : "(null)");
};
return NULL;
};
/*
* Pre-pend the widget to the active list.
*/
tkpg_PrependWidget(tkpg, TKPG_ACTIVE_WIDGETS);
/*
* Open the connection to the PgxWin library.
*/
pgx_open(tkpg->pgx);
if(!tkpg->pgx->state)
tkpg_close_widget(name);
/*
* Reset the background and foreground colors to match the current
* configuration options.
*/
pgx_set_background(tkpg->pgx, Tk_3DBorderColor(tkpg->border));
pgx_set_foreground(tkpg->pgx, tkpg->normalFg);
/*
* Reset its scroll-bars.
*/
tkpg_update_scroll_bars(tkpg);
return tkpg;
}
/*.......................................................................
* Find an active PGPLOT widget of a given name, close it to PGPLOT and
* move it to the head of the inactive list of widgets.
*
* Input:
* name char * The name of the widget.
* Output:
* return TkPgplot * The selected widget, or NULL if not found.
*/
static TkPgplot *tkpg_close_widget(char *name)
{
TkPgplot *tkpg;
/*
* Remove the widget from the active list.
*/
tkpg = tkpg_RemoveWidget(name, TKPG_ACTIVE_WIDGETS);
if(!tkpg) {
fprintf(stderr, "%s: Request to close non-existent widget (%s).\n",
TKPG_IDENT, name ? name : "(null)");
return NULL;
};
/*
* Remove cursor handler.
*/
tkpg_ClrCursor(tkpg);
/*
* Close the connection to the PgxWin library.
*/
pgx_close(tkpg->pgx);
/*
* Invalidate the pgslct() id. The next time that the widget is opened
* to PGPLOT a different value will likely be used.
*/
tkpg->pgslct_id = 0;
/*
* Prepend the widget to the free list.
*/
tkpg_PrependWidget(tkpg, TKPG_FREE_WIDGETS);
return tkpg;
}
/*.......................................................................
* Lookup a widget by name from a given list of widgets.
*
* Input:
* name char * The name of the widget.
* type int The enumerated name of the list to search,
* from:
* TKPG_ACTIVE_WIDGETS
* TKPG_FREE_WIDGETS
* Output:
* prev TkPgplot ** *prev will either be NULL if the widget
* was at the head of the list, or be the
* widget in the list that immediately precedes
* the specified widget.
* return TkPgplot * The located widget, or NULL if not found.
*/
static TkPgplot *tkpg_FindWidgetByName(char *name, int type, TkPgplot **prev)
{
TkPgplotList *widget_list; /* The list to be searched */
widget_list = tkpg_WidgetList(type);
if(widget_list && name) {
TkPgplot *last = NULL;
TkPgplot *node = widget_list->head;
for( ; node; last = node, node = node->next) {
if(strcmp(node->pgx->name, name)==0) {
if(prev)
*prev = last;
return node;
};
};
};
/*
* Widget not found.
*/
if(prev)
*prev = NULL;
return NULL;
}
/*.......................................................................
* Lookup a widget by its PGPLOT id from a given list of widgets.
*
* Input:
* tkslct_id int The number used by PGPLOT to select the
* device.
* type int The enumerated name of the list to search,
* from:
* TKPG_ACTIVE_WIDGETS
* TKPG_FREE_WIDGETS
* Output:
* prev TkPgplot ** *prev will either be NULL if the widget
* was at the head of the list, or be the
* widget in the list that immediately precedes
* the specified widget.
* return TkPgplot * The located widget, or NULL if not found.
*/
static TkPgplot *tkpg_FindWidgetByID(int tkslct_id, int type, TkPgplot **prev)
{
TkPgplotList *widget_list; /* The list to be searched */
widget_list = tkpg_WidgetList(type);
if(widget_list) {
TkPgplot *last = NULL;
TkPgplot *node = widget_list->head;
for( ; node; last = node, node = node->next) {
if(tkslct_id == node->tkslct_id) {
if(prev)
*prev = last;
return node;
};
};
};
/*
* Widget not found.
*/
if(prev)
*prev = NULL;
return NULL;
}
/*.......................................................................
* Lookup one of the PGPLOT class widget lists by its enumerated type.
*
* Input:
* type int The enumerated name of the list, from:
* TKPG_ACTIVE_WIDGETS
* TKPG_FREE_WIDGETS
* Output:
* return TkPgplotList * The widget list, or NULL if not recognized.
*/
static TkPgplotList *tkpg_WidgetList(int type)
{
switch(type) {
case TKPG_ACTIVE_WIDGETS:
return &tkPgplotClassRec.active_widgets;
case TKPG_FREE_WIDGETS:
return &tkPgplotClassRec.free_widgets;
default:
fprintf(stderr, "tkpg_WidgetList: No such list.\n");
};
return NULL;
}
/*.......................................................................
* Remove a given widget from one of the PGPLOT class widget lists.
*
* Input:
* name char * The name of the widget to be removed from
* the list.
* type int The enumerated name of the list from which to
* remove the widget, from:
* TKPG_ACTIVE_WIDGETS
* TKPG_FREE_WIDGETS
* Output:
* return TkPgplot * The removed widget, or NULL if not found.
*/
static TkPgplot *tkpg_RemoveWidget(char *name, int type)
{
TkPgplotList *widget_list; /* The list to remove the widget from */
TkPgplot *tkpg = NULL; /* The widget being removed */
TkPgplot *prev; /* The widget preceding tkpg in the list */
/*
* Get the widget list.
*/
widget_list = tkpg_WidgetList(type);
if(widget_list) {
tkpg = tkpg_FindWidgetByName(name, type, &prev);
if(tkpg) {
if(prev) {
prev->next = tkpg->next;
} else {
widget_list->head = tkpg->next;
};
tkpg->next = NULL;
};
};
return tkpg;
}
/*.......................................................................
* Prepend a PGPLOT widget to a given PGPLOT class widget list.
*
* Input:
* tkpg TkPgplot * The widget to add to the list.
* type int The enumerated name of the list to add to, from:
* TKPG_ACTIVE_WIDGETS
* TKPG_FREE_WIDGETS
* Output:
* return TkPgplot * The added widget (the same as tkpg), or NULL
* on error.
*/
static TkPgplot *tkpg_PrependWidget(TkPgplot *tkpg, int type)
{
TkPgplotList *widget_list; /* The list to prepend the widget to */
/*
* Get the widget list.
*/
widget_list = tkpg_WidgetList(type);
if(widget_list) {
tkpg->next = widget_list->head;
widget_list->head = tkpg;
};
return tkpg;
}
/*.......................................................................
* Return the currently selected PGPLOT device.
*
* Input:
* context char * If no TkPgplot device is currently selected
* and context!=NULL then, an error message of
* the form printf("%s: ...\n", context) will
* be written to stderr reporting that no
* device is open.
* Output:
* return TkPgplot * The currently selected PGPLOT device, or
* NULL if no device is currently selected.
*/
static TkPgplot *tkpg_CurrentWidget(char *context)
{
TkPgplot *tkpg = tkPgplotClassRec.active_widgets.head;
if(!tkpg && context)
fprintf(stderr, "%s: No /%s device is currently selected.\n", context,
TK_PGPLOT_DEVICE);
return tkpg;
}
/*.......................................................................
* This is the only external entry point to the tk device driver.
* It is called by PGPLOT to open, perform operations on, return
* information about and close tk windows.
*
* Input:
* ifunc int * The PGPLOT operation code to be executed.
* Input/output:
* rbuf float * A general buffer for input/output of float values.
* nbuf int * Where relevant this is used to return the number of
* elements in rbuf[]. Also used on input to specify
* number of pixels in the line-of-pixels primitive.
* chr char * A general buffer for string I/O.
* lchr int * Where relevant this is used to send and return the
* number of significant characters in chr.
* Input:
* len int Added to the call line by the FORTRAN compiler.
* This contains the declared size of chr[].
*/
#ifdef VMS
void DRIV(ifunc, rbuf, nbuf, chrdsc, lchr)
int *ifunc;
float rbuf[];
int *nbuf;
struct dsc$descriptor_s *chrdsc; /* VMS FORTRAN string descriptor */
int *lchr;
{
int len = chrdsc->dsc$w_length;
char *chr = chrdsc->dsc$a_pointer;
#else
void DRIV(ifunc, rbuf, nbuf, chr, lchr, len)
int *ifunc, *nbuf, *lchr;
int len;
float rbuf[];
char *chr;
{
#endif
/*
* Get the active widget if there is one.
*/
TkPgplot *tkpg = tkpg_CurrentWidget(NULL);
PgxWin *pgx = tkpg ? tkpg->pgx : NULL;
int i;
/*
* Flush buffered opcodes.
*/
pgx_pre_opcode(pgx, *ifunc);
/*
* Branch on the specified PGPLOT opcode.
*/
switch(*ifunc) {
/*--- IFUNC=1, Return device name ---------------------------------------*/
case 1:
{
char *dev_name = TK_PGPLOT_DEVICE " (widget_path/" TK_PGPLOT_DEVICE ")";
strncpy(chr, dev_name, len);
*lchr = strlen(dev_name);
for(i = *lchr; i < len; i++)
chr[i] = ' ';
};
break;
/*--- IFUNC=2, Return physical min and max for plot device, and range
of color indices -----------------------------------------*/
case 2:
rbuf[0] = 0.0;
rbuf[1] = -1.0; /* Report no effective max plot width */
rbuf[2] = 0.0;
rbuf[3] = -1.0; /* Report no effective max plot height */
rbuf[4] = 0.0;
rbuf[5] = (pgx && !pgx->bad_device) ? pgx->color->ncol-1 : 1;
*nbuf = 6;
break;
/*--- IFUNC=3, Return device resolution ---------------------------------*/
case 3:
pgx_get_resolution(pgx, &rbuf[0], &rbuf[1]);
rbuf[2] = 1.0; /* Device coordinates per pixel */
*nbuf = 3;
break;
/*--- IFUNC=4, Return misc device info ----------------------------------*/
case 4:
chr[0] = 'I'; /* Interactive device */
chr[1] = 'X'; /* Cursor is available and opcode 27 is desired */
chr[2] = 'N'; /* No dashed lines */
chr[3] = 'A'; /* Area fill available */
chr[4] = 'T'; /* Thick lines */
chr[5] = 'R'; /* Rectangle fill available */
chr[6] = 'P'; /* Line of pixels available */
chr[7] = 'N'; /* Don't prompt on pgend */
chr[8] = 'Y'; /* Can return color representation */
chr[9] = 'N'; /* Not used */
chr[10]= 'S'; /* Area-scroll available */
*lchr = 11;
break;
/*--- IFUNC=5, Return default file name ---------------------------------*/
case 5:
chr[0] = '\0'; /* Default name is "" */
*lchr = 0;
break;
/*--- IFUNC=6, Return default physical size of plot ---------------------*/
case 6:
pgx_def_size(pgx, Tk_Width(tkpg->tkwin), Tk_Height(tkpg->tkwin), rbuf, nbuf);
break;
/*--- IFUNC=7, Return misc defaults -------------------------------------*/
case 7:
rbuf[0] = 1.0;
*nbuf = 1;
break;
/*--- IFUNC=8, Select plot ----------------------------------------------*/
case 8:
{
TkPgplot *new_tkpg = tkpg_FindWidgetByID((int)(rbuf[1]+0.5),
TKPG_ACTIVE_WIDGETS, NULL);
if(new_tkpg) {
new_tkpg->pgslct_id = (int) (rbuf[0]+0.5);
tkpg_RemoveWidget(new_tkpg->pgx->name, TKPG_ACTIVE_WIDGETS);
tkpg_PrependWidget(new_tkpg, TKPG_ACTIVE_WIDGETS);
} else {
fprintf(stderr, "%s: [Select plot] No such open device.\n", TKPG_IDENT);
};
};
break;
/*--- IFUNC=9, Open workstation -----------------------------------------*/
case 9:
/*
* Assign the returned device unit number and success indicator.
* Assume failure to open until the workstation is open.
*/
rbuf[0] = rbuf[1] = 0.0;
*nbuf = 2;
/*
* Prepare the display name.
*/
if(*lchr >= len) {
fprintf(stderr, "%s: Widget name too long.\n", TKPG_IDENT);
return;
} else {
chr[*lchr] = '\0';
};
/*
* Get the requested widget from the free widget list.
*/
tkpg = tkpg_open_widget(chr);
if(!tkpg)
return;
rbuf[0] = tkpg->tkslct_id; /* The number used to select this device */
rbuf[1] = 1.0;
*nbuf = 2;
break;
/*--- IFUNC=10, Close workstation ---------------------------------------*/
case 10:
/*
* Remove the device from the list of open devices.
*/
if(pgx)
tkpg_close_widget(pgx->name);
break;
/*--- IFUNC=11, Begin picture -------------------------------------------*/
case 11:
pgx_begin_picture(pgx, rbuf);
break;
/*--- IFUNC=12, Draw line -----------------------------------------------*/
case 12:
pgx_draw_line(pgx, rbuf);
break;
/*--- IFUNC=13, Draw dot ------------------------------------------------*/
case 13:
pgx_draw_dot(pgx, rbuf);
break;
/*--- IFUNC=14, End picture ---------------------------------------------*/
case 14:
break;
/*--- IFUNC=15, Select color index --------------------------------------*/
case 15:
pgx_set_ci(pgx, (int) (rbuf[0] + 0.5));
break;
/*--- IFUNC=16, Flush buffer. -------------------------------------------*/
case 16:
pgx_flush(pgx);
break;
/*--- IFUNC=17, Read cursor. --------------------------------------------*/
case 17:
if(tkpg)
tkpg_ClrCursor(tkpg);
pgx_read_cursor(pgx, rbuf, chr, nbuf, lchr);
break;
/*--- IFUNC=18, Erase alpha screen. -------------------------------------*/
/* (Not implemented: no alpha screen) */
case 18:
break;
/*--- IFUNC=19, Set line style. -----------------------------------------*/
/* (Not implemented: should not be called) */
case 19:
break;
/*--- IFUNC=20, Polygon fill. -------------------------------------------*/
case 20:
pgx_poly_fill(pgx, rbuf);
break;
/*--- IFUNC=21, Set color representation. -------------------------------*/
case 21:
{
int ci = (int)(rbuf[0]+0.5);
pgx_set_rgb(pgx, ci, rbuf[1],rbuf[2],rbuf[3]);
if(ci==0)
tkpg_update_border(tkpg);
};
break;
/*--- IFUNC=22, Set line width. -----------------------------------------*/
case 22:
pgx_set_lw(pgx, rbuf[0]);
break;
/*--- IFUNC=23, Escape --------------------------------------------------*/
/* (Not implemented: ignored) */
case 23:
break;
/*--- IFUNC=24, Rectangle Fill. -----------------------------------------*/
case 24:
pgx_rect_fill(pgx, rbuf);
break;
/*--- IFUNC=25, ---------------------------------------------------------*/
/* (Not implemented: ignored) */
case 25:
break;
/*--- IFUNC=26, Line of pixels ------------------------------------------*/
case 26:
pgx_pix_line(pgx, rbuf, nbuf);
break;
/*--- IFUNC=27, World-coordinate scaling --------------------------------*/
case 27:
pgx_set_world(pgx, rbuf);
break;
/*--- IFUNC=29, Query color representation ------------------------------*/
case 29:
pgx_get_rgb(pgx, rbuf, nbuf);
break;
/*--- IFUNC=30, Scroll rectangle ----------------------------------------*/
case 30:
pgx_scroll_rect(pgx, rbuf);
break;
/*--- IFUNC=?, ----------------------------------------------------------*/
default:
fprintf(stderr, "%s: Ignoring unimplemented opcode=%d.\n",
TKPG_IDENT, *ifunc);
*nbuf = -1;
break;
};
return;
}
/*.......................................................................
* This function services TCL commands for a given widget.
*
* Input:
* context ClientData The tkpg widget cast to (ClientData).
* interp Tcl_Interp * The TCL intrepreter.
* argc int The number of command arguments.
* argv char ** The array of 'argc' command arguments.
* Output:
* return int TCL_OK - Success.
* TCL_ERROR - Failure.
*/
static int tkpg_InstanceCommand(ClientData context, Tcl_Interp *interp,
int argc, char *argv[])
{
TkPgplot *tkpg = (TkPgplot *) context;
char *widget; /* The name of the widget */
char *command; /* The name of the command */
/*
* Get the name of the widget.
*/
widget = argv[0];
/*
* Get the name of the command.
*/
if(argc < 2) {
Tcl_AppendResult(interp, "Missing arguments to ", widget, " command.",
NULL);
return TCL_ERROR;
};
command = argv[1];
/*
* Prevent untimely deletion of the widget while this function runs.
* Note that following this statement you must return via
* tkpg_InstanceCommand_return() to ensure that Tk_Release() gets called.
*/
Tk_Preserve(context);
/*
* Check for recognized command names.
*/
if(strcmp(command, "xview") == 0) { /* X-axis scroll-bar update */
return tkpg_InstanceCommand_return(context,
tkpg_scrollbar_callback(tkpg, interp, widget, command,
argc-2, argv+2));
} else if(strcmp(command, "yview") == 0) { /* Y-axis scroll-bar update */
return tkpg_InstanceCommand_return(context,
tkpg_scrollbar_callback(tkpg, interp, widget, command,
argc-2, argv+2));
} else if(strcmp(command, "configure") == 0) { /* Configure widget */
/*
* Check the number of configure arguments.
*/
switch(argc - 2) {
case 0: /* Return the values of all configuration options */
return tkpg_InstanceCommand_return(context,
Tk_ConfigureInfo(interp, tkpg->tkwin, configSpecs,
(char *) tkpg, NULL, 0));
break;
case 1: /* Return the value of a single given configuration option */
return tkpg_InstanceCommand_return(context,
Tk_ConfigureInfo(interp, tkpg->tkwin, configSpecs,
(char *) tkpg, argv[2], 0));
break;
default: /* Change one of more of the configuration options */
return tkpg_InstanceCommand_return(context,
tkpg_Configure(tkpg, interp, argc-2, argv+2,
TK_CONFIG_ARGV_ONLY));
break;
};
} else if(strcmp(command, "cget") == 0) { /* Get a configuration value */
if(argc != 3) {
Tcl_AppendResult(interp, "Wrong number of arguments to \"", widget,
" cget\" command", NULL);
return tkpg_InstanceCommand_return(context, TCL_ERROR);
} else {
return tkpg_InstanceCommand_return(context,
Tk_ConfigureValue(interp, tkpg->tkwin, configSpecs,
(char *) tkpg, argv[2], 0));
};
} else if(strcmp(command, "setcursor") == 0) { /* Augment the cursor */
return tkpg_InstanceCommand_return(context,
tkpg_tcl_setcursor(tkpg, interp, argc - 2, argv + 2));
} else if(strcmp(command, "clrcursor") == 0) { /* Clear cursor augmentation */
tkpg_ClrCursor(tkpg);
return tkpg_InstanceCommand_return(context, TCL_OK);
} else if(strcmp(command, "world") == 0) { /* Pixel to world coordinates */
return tkpg_InstanceCommand_return(context,
tkpg_tcl_world(tkpg, interp, widget,
argc-2, argv+2));
} else if(strcmp(command, "pixel") == 0) { /* World to pixel coordinates */
return tkpg_InstanceCommand_return(context,
tkpg_tcl_pixel(tkpg, interp, widget,
argc-2, argv+2));
} else if(strcmp(command, "id") == 0) { /* PGPLOT id of widget */
return tkpg_InstanceCommand_return(context,
tkpg_tcl_id(tkpg, interp, widget,
argc-2, argv+2));
} else if(strcmp(command, "device") == 0) { /* PGPLOT name for the widget */
return tkpg_InstanceCommand_return(context,
tkpg_tcl_device(tkpg, interp, widget,
argc-2, argv+2));
};
/*
* Unknown command name.
*/
Tcl_AppendResult(interp, "Unknown command \"", widget, " ", command, "\"",
NULL);
return tkpg_InstanceCommand_return(context, TCL_ERROR);
}
/*.......................................................................
* This is a private cleanup-return function of tkpg_InstanceCommand().
* It should be used to return from said function after Tk_Preserve() has
* been called. It calls Tk_Release() on the widget to unblock deletion
* and returns the specified error code.
*
* Input:
* context ClientData The tkpg widget cast to (ClientData).
* iret int TCL_OK or TCL_ERROR.
* Output:
* return int The value of iret.
*/
static int tkpg_InstanceCommand_return(ClientData context, int iret)
{
Tk_Release(context);
return iret;
}
/*.......................................................................
* This function is services TCL commands for a given widget.
*
* Input:
* tkpg TkPgplot * The widget record to be configured.
* interp Tcl_Interp * The TCL intrepreter.
* argc int The number of configuration arguments.
* argv char ** The array of 'argc' configuration arguments.
* flags int The flags argument of Tk_ConfigureWidget():
* 0 - No flags.
* TK_CONFIG_ARGV - Override the X defaults
* database and the configSpecs
* defaults.
* Output:
* return int TCL_OK - Success.
* TCL_ERROR - Failure.
*/
static int tkpg_Configure(TkPgplot *tkpg, Tcl_Interp *interp,
int argc, char *argv[], int flags)
{
/*
* Get the X-window pgplot object.
*/
PgxWin *pgx = tkpg->pgx;
/*
* Install the new defaults in tkpg.
*/
if(Tk_ConfigureWidget(interp, tkpg->tkwin, configSpecs, argc, argv,
(char *) tkpg, flags) == TCL_ERROR)
return TCL_ERROR;
/*
* Install the background color in PGPLOT color-index 0.
*/
pgx_set_background(pgx, Tk_3DBorderColor(tkpg->border));
/*
* Install the foreground color in PGPLOT color-index 1.
*/
pgx_set_foreground(pgx, tkpg->normalFg);
/*
* Install changes to window attributes.
*/
{
XSetWindowAttributes attr; /* The attribute-value container */
unsigned long mask = 0; /* The set of attributes that have changed */
attr.background_pixel = pgx->color->pixel[0];
mask |= CWBackPixel;
attr.colormap = pgx->color->cmap;
mask |= CWColormap;
attr.border_pixel = pgx->color->pixel[0];
mask |= CWBorderPixel;
attr.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask |
KeyPressMask | KeyReleaseMask;
mask |= CWDontPropagate;
Tk_ChangeWindowAttributes(tkpg->tkwin, mask, &attr);
};
/*
* Tell Tk what window size we want.
*/
Tk_GeometryRequest(tkpg->tkwin, tkpg->req_width, tkpg->req_height);
/*
* Tell pgxwin that the clip margin may have changed.
*/
tkpg_update_clip(tkpg);
/*
* Update the optional window margins.
*/
pgx_set_margin(pgx, tkpg->padx, tkpg->pady);
/*
* Refresh the window.
*/
tkpg_refresh_window(tkpg);
return TCL_OK;
}
/*.......................................................................
* This is the main X event callback for Pgplot widgets.
*
* Input:
* context ClientData The tkpg widget cast to (ClientData).
* event XEvent * The event that triggered the callback.
*/
static void tkpg_EventHandler(ClientData context, XEvent *event)
{
TkPgplot *tkpg = (TkPgplot *) context;
/*
* Determine what type of event triggered this call.
*/
switch(event->type) {
case ConfigureNotify: /* The window has been resized */
tkpg->scroll.x = 0;
tkpg->scroll.y = 0;
tkpg_update_clip(tkpg);
tkpg_update_scroll_bars(tkpg);
tkpg_refresh_window(tkpg);
break;
case DestroyNotify: /* The window has been destroyed */
/*
* Delete the cursor event handler to prevent further use by user.
*/
tkpg_ClrCursor(tkpg);
/*
* Delete the main event handler to prevent prolonged use.
*/
Tk_DeleteEventHandler(tkpg->tkwin, tkpg->events.mask, tkpg_EventHandler,
(ClientData) tkpg);
/*
* Tell del_TkPgplot() that we have already deleted the event mask.
*/
tkpg->events.mask = NoEventMask;
/*
* Force the functions in pgxwin.c to discard subsequent graphics.
*/
if(tkpg->pgx)
tkpg->pgx->window = None;
/*
* Queue deletion of tkpg until all references to the widget have been
* completed.
*/
Tk_EventuallyFree(context, tkpg_FreeProc);
break;
case FocusIn: /* Keyboard-input focus has been acquired */
tkpg->events.focus_acquired = 1;
tkpg_draw_focus_highlight(tkpg);
break;
case FocusOut: /* Keyboard-input focus has been lost */
tkpg->events.focus_acquired = 0;
tkpg_draw_focus_highlight(tkpg);
break;
case Expose: /* Redraw the specified area */
tkpg_expose_handler(tkpg, event);
break;
};
return;
}
/*.......................................................................
* The expose-event handler for PGPLOT widgets.
*
* Input:
* tkpg TkPgplot * The Tk Pgplot widget.
* event XEvent The expose event that invoked the callback.
*/
static void tkpg_expose_handler(TkPgplot *tkpg, XEvent *event)
{
/*
* Re-draw the focus-highlight border.
*/
tkpg_draw_focus_highlight(tkpg);
/*
* Re-draw the 3D borders.
*/
tkpg_draw_3d_border(tkpg);
/*
* Re-draw the damaged area.
*/
pgx_expose(tkpg->pgx, event);
return;
}
/*.......................................................................
* Re-draw the focus highlight border if it has a finite size.
*
* Input:
* tkpg TkPgplot * The Tk Pgplot widget.
*/
static void tkpg_draw_focus_highlight(TkPgplot *tkpg)
{
Window w = Tk_WindowId(tkpg->tkwin);
/*
* Re-draw the focus-highlight border.
*/
if(tkpg->highlight_thickness != 0) {
GC gc = Tk_GCForColor(tkpg->events.focus_acquired ?
tkpg->highlightColor : tkpg->highlightBgColor,
w);
Tk_DrawFocusHighlight(tkpg->tkwin, gc, tkpg->highlight_thickness, w);
};
return;
}
/*.......................................................................
* Re-draw the 3D border if necessary.
*
* Input:
* tkpg TkPgplot * The Tk Pgplot widget.
*/
static void tkpg_draw_3d_border(TkPgplot *tkpg)
{
Tk_Window tkwin = tkpg->tkwin;
Window w = Tk_WindowId(tkwin);
/*
* Re-draw the focus-highlight border.
*/
if(tkpg->border && tkpg->borderWidth > 0) {
int margin = tkpg->highlight_thickness;
Tk_Draw3DRectangle(tkwin, w, tkpg->border, margin, margin,
Tk_Width(tkwin) - 2*margin, Tk_Height(tkwin) - 2*margin,
tkpg->borderWidth, tkpg->relief);
};
return;
}
/*.......................................................................
* Augment the cursor of a given widget.
*
* Input:
* tkpg TkPgplot * The PGPLOT widget to connect a cursor to.
* mode TkpgCursorMode The type of cursor augmentation.
* xref,yref float The world-coordinate reference point for band-type
* cursors.
* ci int The color index with which to plot the cursor,
* or -1 to select the current foreground color.
* Output:
* return int TCL_OK or TCL_ERROR.
*/
static int tkpg_SetCursor(TkPgplot *tkpg, TkpgCursorMode mode,
float xref, float yref, int ci)
{
PgxWin *pgx = tkpg->pgx;
float rbuf[2];
/*
* Remove any existing cursor augmentation.
*/
tkpg_ClrCursor(tkpg);
/*
* Mark the cursor as active.
*/
tkpg->events.cursor_active = 1;
/*
* Convert xref, yref from world coordinates to device coordinates.
*/
rbuf[0] = xref;
rbuf[1] = yref;
pgx_world2dev(pgx, rbuf);
/*
* Raise the cursor.
*/
if(pgx_set_cursor(pgx, ci, (int)mode, 0, rbuf, rbuf)) {
Tcl_AppendResult(tkpg->interp, "Unable to display cursor.\n", NULL);
tkpg_ClrCursor(tkpg);
return TCL_ERROR;
};
/*
* If the pointer is currently in the window, record its position
* and draw the cursor.
*/
if(pgx_locate_cursor(pgx))
pgx_draw_cursor(pgx);
/*
* Create an event handler to handle asychronous cursor input.
*/
Tk_CreateEventHandler(tkpg->tkwin, CURSOR_EVENT_MASK,
tkpg_CursorHandler, (ClientData) tkpg);
return TCL_OK;
}
/*.......................................................................
* This is the X event callback for Pgplot cursor events. It is called
* only when the cursor augmentation has been established by
* tkpg_SetCursor() and not cleared by tkpg_ClrCursor().
*
* Input:
* context ClientData The tkpg widget cast to (ClientData).
* event XEvent * The event that triggered the callback.
*/
static void tkpg_CursorHandler(ClientData context, XEvent *event)
{
TkPgplot *tkpg = (TkPgplot *) context;
PgxWin *pgx = tkpg->pgx;
float rbuf[2];
char key;
/*
* Handle the event. Note that button-press and keyboard events
* have not been selected so the return values are irrelevent.
*/
(void) pgx_cursor_event(pgx, event, rbuf, &key);
/*
* Handle errors.
*/
if(pgx->bad_device)
tkpg_ClrCursor(tkpg);
return;
}
/*.......................................................................
* Clear the cursor of a given widget.
*
* tkpg TkPgplot * The widget to disconnect the cursor from.
*/
static void tkpg_ClrCursor(TkPgplot *tkpg)
{
if(tkpg) {
PgxWin *pgx = tkpg->pgx;
/*
* Do nothing if the cursor is inactive.
*/
if(tkpg->events.cursor_active) {
/*
* Remove the current event handler.
*/
Tk_DeleteEventHandler(tkpg->tkwin, CURSOR_EVENT_MASK,
tkpg_CursorHandler, (ClientData) tkpg);
/*
* Reset the cursor context to its inactive state.
*/
tkpg->events.cursor_active = 0;
/*
* Erase the cursor.
*/
pgx_erase_cursor(pgx);
pgx_set_cursor(pgx, 0, TKPG_NORM_CURSOR, 0, NULL, NULL);
};
};
return;
}
/*.......................................................................
* Augment the cursor as specified in the arguments of the setcursor
* widget command.
*
* Input:
* tkpg TkPgplot * The widget record to be configured.
* interp Tcl_Interp * The TCL intrepreter.
* argc int The number of configuration arguments.
* argv char ** The array of 'argc' configuration arguments.
* [0] The type of cursor augmentation, from:
* norm - Un-augmented X cursor
* line - Line cursor between ref and pointer
* rect - Rectangle between ref and pointer
* yrng - Horizontal lines at ref.x & pointer.x
* xrng - Vertical lines at ref.y & pointer.y
* hline - Horizontal line cursor at y=ref.y
* vline - Vertical line cursor at x=ref.x
* cross - Pointer centered cross-hair
* [1] The X-axis world coordinate at which
* to anchor rect,yrng and xrng cursors.
* [2] The Y-axis world coordinate at which
* to anchor rect,yrng and xrng cursors.
* [3] The color index of the cursor.
* flags int The flags argument of Tk_ConfigureWidget():
* 0 - No flags.
* TK_CONFIG_ARGV - Override the X defaults
* database and the configSpecs
* defaults.
* Output:
* return int TCL_OK - Success.
* TCL_ERROR - Failure.
*/
static int tkpg_tcl_setcursor(TkPgplot *tkpg, Tcl_Interp *interp,
int argc, char *argv[])
{
TkpgCursorMode mode; /* Cursor augmentation mode */
double xref,yref; /* The X and Y reference positions of the cursor */
int ci; /* The color index used to draw the cursor */
int found = 0; /* True once the mode has been identified */
int i;
/*
* List the correspondence between cursor-mode names and pgband() mode
* enumerators.
*/
struct {
TkpgCursorMode mode;
char *name;
} modes[] = {
{TKPG_NORM_CURSOR, "norm"}, /* Un-augmented X cursor */
{TKPG_LINE_CURSOR, "line"}, /* Line cursor between ref and pointer */
{TKPG_RECT_CURSOR, "rect"}, /* Rectangle between ref and pointer */
{TKPG_YRNG_CURSOR, "yrng"}, /* Horizontal lines at ref.x & pointer.x */
{TKPG_XRNG_CURSOR, "xrng"}, /* Vertical lines at ref.y & pointer.y */
{TKPG_HLINE_CURSOR, "hline"},/* Horizontal line cursor at y=ref.y */
{TKPG_VLINE_CURSOR, "vline"},/* Vertical line cursor at x=ref.x */
{TKPG_CROSS_CURSOR, "cross"},/* Pointer centered cross-hair */
};
/*
* Check that we have the expected number of arguments.
*/
if(argc != 4) {
Tcl_AppendResult(interp, "Wrong number of arguments. Should be: \"",
tkpg->pgx->name, " setcursor mode x y ci",
NULL);
return TCL_ERROR;
};
/*
* Make sure that the widget is currently open to PGPLOT.
*/
if(tkpg->pgslct_id == 0) {
Tcl_AppendResult(interp, tkpg->pgx->name,
" setcursor: Widget not open to PGPLOT.", NULL);
return TCL_ERROR;
};
/*
* Lookup the cursor mode.
*/
mode = TKPG_NORM_CURSOR;
for(i=0; !found && i<sizeof(modes)/sizeof(modes[0]); i++) {
if(strcmp(modes[i].name, argv[0]) == 0) {
found = 1;
mode = modes[i].mode;
};
};
/*
* Mode not found?
*/
if(!found) {
Tcl_AppendResult(interp, "Unknown PGPLOT cursor mode \"", argv[0],
"\". Should be one of:", NULL);
for(i=0; i<sizeof(modes)/sizeof(modes[0]); i++)
Tcl_AppendResult(interp, " ", modes[i].name, NULL);
return TCL_ERROR;
};
/*
* Read the cursor X and Y coordinate.
*/
if(Tcl_GetDouble(interp, argv[1], &xref) == TCL_ERROR ||
Tcl_GetDouble(interp, argv[2], &yref) == TCL_ERROR)
return TCL_ERROR;
/*
* Get the color index to use when drawing the cursor.
*/
if(Tcl_GetInt(interp, argv[3], &ci) == TCL_ERROR)
return TCL_ERROR;
/*
* Delegate the rest of the work to tkpg_SetCursor().
*/
return tkpg_SetCursor(tkpg, mode, xref, yref, ci);
}
/*.......................................................................
* This is a Tk_FreeProc() wrapper function around del_TkPgplot(),
* suitable for use with Tk_EventuallyFree().
*
* Input:
* context ClientData The tkpg widget to be deleted, cast to
* ClientData.
*/
#ifdef RIVET
static void tkpg_FreeProc(ClientData context)
#else
static void tkpg_FreeProc(char *context)
#endif
{
(void) del_TkPgplot((TkPgplot *) context);
}
#ifdef RIVET
/*.......................................................................
* Return an unambiguous PGPLOT device-specification that can be used
* as the FILE argument of cpgbeg() to open a given Rivet PGPLOT widget.
*
* Input:
* widget Rivetobj A rivet pgplot widget.
* Output:
* return char * The PGPLOT device-specication. Note that the returned
* string is owned by the widget driver and must not be
* free()d or overwritten.
*/
char *rvp_device_name(Rivetobj widget)
{
TkPgplot *tkpg = (TkPgplot *) widget;
return tkpg->device;
}
/*.......................................................................
* Return the pgslct_id of the given Rivet pgplot widget. This can then
* be used with the cpgslct() function to select the widget as the currently
* active widget.
*
* Input:
* widget Rivetobj A rivet pgplot widget.
* Output:
* return int The PGPLOT device-id. This will be 0 if the widget
* is not currently open to PGPLOT.
*/
int rvp_device_id(Rivetobj widget)
{
TkPgplot *tkpg = (TkPgplot *) widget;
return tkpg->pgslct_id;
}
/*.......................................................................
* Convert from X window pixel coordinates to PGPLOT world coordinates.
*
* Input:
* widget Rivetobj A rivet pgplot widget.
* px, py int The X-window pixel coordinates to be converted.
* wx, wy float * The corresponding PGPLOT world coordinates are
* assigned to the variables pointed to by wx and wy.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int rvp_xwin2world(Rivetobj widget, int px, int py, float *wx, float *wy)
{
TkPgplot *tkpg = (TkPgplot *) widget;
float rbuf[2];
/*
* Convert from pixels to world coordinates.
*/
if(pgx_win2dev(tkpg->pgx, px, py, rbuf) ||
pgx_dev2world(tkpg->pgx, rbuf))
return 1;
/*
* Assign the return values.
*/
*wx = rbuf[0];
*wy = rbuf[1];
return 0;
}
/*.......................................................................
* Convert from PGPLOT world coordinates to X window pixel coordinates.
*
* Input:
* widget Rivetobj A rivet pgplot widget.
* wx, wy float The PGPLOT world coordinates to be converted.
* px, py int * The corresponding X-window pixel coordinates are
* assigned to the variables pointed to by px and py.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int rvp_world2xwin(Rivetobj widget, float wx, float wy, int *px, int *py)
{
TkPgplot *tkpg = (TkPgplot *) widget;
float rbuf[2];
/*
* Convert from world coordinates to pixel coordinates.
*/
rbuf[0] = wx;
rbuf[1] = wy;
if(pgx_world2dev(tkpg->pgx, rbuf) ||
pgx_dev2win(tkpg->pgx, rbuf, px, py))
return 1;
return 0;
}
#endif
/*.......................................................................
* Refresh the contents of the window.
*
* Input:
* tkpg TkPgplot * The widget record to be configured.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
static int tkpg_refresh_window(TkPgplot *tkpg)
{
if(Tk_IsMapped(tkpg->tkwin)) {
tkpg_draw_focus_highlight(tkpg);
tkpg_draw_3d_border(tkpg);
return pgx_scroll(tkpg->pgx, tkpg->scroll.x, tkpg->scroll.y);
};
return 0;
}
/*.......................................................................
* Whenever the color representation of the background color is changed
* via PGPLOT, this function is called to update the Tk 3D border.
*
* Input:
* tkpg TkPgplot * The associated PGPLOT widget.
*/
static void tkpg_update_border(TkPgplot *tkpg)
{
XColor *bg; /* The new background color */
char cname[20]; /* The color as a string of the form #rrrrggggbbbb */
Tk_3DBorder bd; /* The new Tk border */
/*
* Get the PGPLOT background color.
*/
bg = &tkpg->pgx->color->xcolor[0];
/*
* Tk_Get3DBorder requires a standard X color resource string.
*/
sprintf(cname, "#%4.4hx%4.4hx%4.4hx", bg->red, bg->green, bg->blue);
bd = Tk_Get3DBorder(tkpg->interp, tkpg->tkwin, cname);
if(bd) {
/*
* Replace the previous border with the new one.
*/
if(tkpg->border)
Tk_Free3DBorder(tkpg->border);
tkpg->border = bd;
tkpg_draw_3d_border(tkpg);
} else {
fprintf(stderr, "Tk_Get3DBorder failed: %s\n", tkpg->interp->result);
};
}
/*.......................................................................
* Respond to an xview or yview scrollbar command.
*
* Input:
* tkpg TkPgplot * The widget record to be configured.
* interp Tcl_Interp * The TCL intrepreter.
* widget char * The name of the PGPLOT widget.
* view char * "xview" or "yview".
* argc int The number of configuration arguments.
* argv char ** The array of 'argc' configuration arguments.
* Output:
* return int TCL_OK - Success.
* TCL_ERROR - Failure.
*/
static int tkpg_scrollbar_callback(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, char *view, int argc,
char *argv[])
{
int window_size; /* The size of the window along the direction of motion */
int pixmap_size; /* The size of the pixmap along the direction of motion */
int new_start_pos;/* The new pixmap coord of the top|left of the window */
int old_start_pos;/* The old pixmap coord of the top|left of the window */
/*
* Fill in the current scroll-statistics along the requested direction.
*/
if(*view == 'x') {
window_size = Tk_Width(tkpg->tkwin);
pixmap_size = pgx_pixmap_width(tkpg->pgx);
old_start_pos = tkpg->scroll.x;
} else {
window_size = Tk_Height(tkpg->tkwin);
pixmap_size = pgx_pixmap_height(tkpg->pgx);
old_start_pos = tkpg->scroll.y;
};
/*
* The first argument specifies what form of scrollbar command has
* been received (see 'man scrollbar' for details).
*/
if(argc < 1) {
return tkpg_scrollbar_error(tkpg, interp, widget, view, argc, argv);
/*
* The moveto command requests a new start position as a
* fraction of the pixmap size.
*/
} else if(strcmp(argv[0], "moveto")==0) {
double fractional_position;
if(argc != 2)
return tkpg_scrollbar_error(tkpg, interp, widget, view, argc, argv);
/*
* Read the fractional position.
*/
if(Tcl_GetDouble(interp, argv[1], &fractional_position) == TCL_ERROR)
return TCL_ERROR;
new_start_pos = fractional_position * pixmap_size;
/*
* The "scroll" command specifies an increment to move the pixmap by
* and the units to which the increment refers.
*/
} else if(strcmp(argv[0], "scroll")==0) {
int scroll_increment;
if(argc != 3)
return tkpg_scrollbar_error(tkpg, interp, widget, view, argc, argv);
/*
* Read the scroll-increment.
*/
if(Tcl_GetInt(interp, argv[1], &scroll_increment) == TCL_ERROR)
return TCL_ERROR;
/*
* The unit of the increment can either be "units", which in our case
* translates to a single pixel, or "pages", which corresponds to the
* width/height of the window.
*/
if(strcmp(argv[2], "units")==0) {
new_start_pos = old_start_pos + scroll_increment;
} else if(strcmp(argv[2], "pages")==0) {
int page_size = window_size - 2 *
(tkpg->highlight_thickness + tkpg->borderWidth);
if(page_size < 0)
page_size = 0;
new_start_pos = old_start_pos + scroll_increment * page_size;
} else {
return tkpg_scrollbar_error(tkpg, interp, widget, view, argc, argv);
};
} else {
Tcl_AppendResult(interp, "Unknown xview command \"", argv[0], "\"", NULL);
return TCL_ERROR;
};
/*
* Keep the pixmap visible.
*/
if(new_start_pos < 0 || window_size > pixmap_size) {
new_start_pos = 0;
} else if(new_start_pos + window_size > pixmap_size) {
new_start_pos = pixmap_size - window_size;
};
/*
* Record the top left corner of the new scrolling-area.
*/
if(*view == 'x')
tkpg->scroll.x = new_start_pos;
else
tkpg->scroll.y = new_start_pos;
/*
* Update the scrolled area and the scrollbar slider.
*/
tkpg_update_scroll_bars(tkpg);
return TCL_OK;
}
/*.......................................................................
* This is a private error-return function of tkpg_scrollbar_callback().
*
* Input:
* tkpg TkPgplot * The widget record.
* interp Tcl_Interp * The TCL intrepreter.
* widget char * The name of the PGPLOT widget.
* view char * "xview" or "yview".
* argc int The number of arguments in argv.
* argv char ** The array of 'argc' configuration arguments.
* Output:
* return int TCL_ERROR and the context of the error
* is recorded in interp->result.
*/
static int tkpg_scrollbar_error(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, char *view, int argc,
char *argv[])
{
int i;
Tcl_AppendResult(interp, "Bad command: ", widget, " ", view, NULL);
for(i=0; i<argc; i++)
Tcl_AppendResult(interp, " ", argv[i], NULL);
Tcl_AppendResult(interp, "\nAfter \"widget [xy]view\", use one of:\n \"moveto <fraction>\" or \"scroll -1|1 units|pages\"", NULL);
return TCL_ERROR;
}
/*.......................................................................
* Implement the Tcl world function. This converts an X-window
* pixel coordinate to the corresponding PGPLOT world coordinate.
*
* Input:
* tkpg TkPgplot * The widget record.
* interp Tcl_Interp * The TCL intrepreter.
* widget char * The name of the PGPLOT widget.
* argc int The number of configuration arguments.
* argv char ** The array of 'argc' configuration arguments.
* [0] The coordinate axes to convert, from:
* "x" - Convert an X-axis coord.
* "y" - Convert a Y-axis coord.
* "xy" - Convert a an X Y axis pair.
* [1] An X-axis pixel coordinate if [0][0] is
* 'x'.
* A Y-axis pixel coordinate if [0][0] is
* 'y'.
* [2] This is only expected if [0]=="xy". It
* should then contain the Y-axis
* coordinate to be converted.
* Output:
* return int TCL_OK - Success.
* TCL_ERROR - Failure.
*/
static int tkpg_tcl_world(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, int argc, char *argv[])
{
int xpix, ypix; /* The input X window coordinate */
float rbuf[2]; /* The conversion buffer */
char *axis; /* The axis specification string */
enum {BAD_AXIS, X_AXIS, Y_AXIS, XY_AXIS}; /* Enumerated axis type */
int axtype; /* The decoded axis type */
char *usage = " world [x <xpix>]|[y <xpix>]|[xy <xpix> <ypix>]";
/*
* Check that an axis specification argument has been provided.
*/
if(argc < 1) {
Tcl_AppendResult(interp, "Usage: ", widget, usage, NULL);
return TCL_ERROR;
};
/*
* Decode the axis type and check the expected argument count.
*/
axis = argv[0];
axtype = BAD_AXIS;
switch(*axis++) {
case 'x':
switch(*axis++) {
case 'y':
if(*axis == '\0' && argc == 3)
axtype = XY_AXIS;
break;
case '\0':
if(argc == 2)
axtype = X_AXIS;
break;
};
break;
case 'y':
if(*axis == '\0' && argc == 2)
axtype = Y_AXIS;
break;
};
/*
* Unrecognised axis description?
*/
if(axtype == BAD_AXIS) {
Tcl_AppendResult(interp, "Usage: ", widget, usage, NULL);
return TCL_ERROR;
};
/*
* Get the pixel coordinates to be converted.
*/
switch(axtype) {
case X_AXIS:
if(Tcl_GetInt(interp, argv[1], &xpix) == TCL_ERROR)
return TCL_ERROR;
ypix = 0;
break;
case Y_AXIS:
xpix = 0;
if(Tcl_GetInt(interp, argv[1], &ypix) == TCL_ERROR)
return TCL_ERROR;
break;
case XY_AXIS:
if(Tcl_GetInt(interp, argv[1], &xpix) == TCL_ERROR ||
Tcl_GetInt(interp, argv[2], &ypix) == TCL_ERROR)
return TCL_ERROR;
break;
};
/*
* Convert the pixel coordinates to world coordinates.
*/
pgx_win2dev(tkpg->pgx, xpix, ypix, rbuf);
pgx_dev2world(tkpg->pgx, rbuf);
/*
* Write the world coordinate(s) into the reply string.
*/
switch(axtype) {
case X_AXIS:
Tcl_PrintDouble(interp, rbuf[0], tkpg->buffer);
Tcl_AppendResult(interp, tkpg->buffer, NULL);
break;
case Y_AXIS:
Tcl_PrintDouble(interp, rbuf[1], tkpg->buffer);
Tcl_AppendResult(interp, tkpg->buffer, NULL);
break;
case XY_AXIS:
Tcl_PrintDouble(interp, rbuf[0], tkpg->buffer);
Tcl_AppendResult(interp, tkpg->buffer, NULL);
Tcl_PrintDouble(interp, rbuf[1], tkpg->buffer);
Tcl_AppendResult(interp, tkpg->buffer, NULL);
break;
};
return TCL_OK;
}
/*.......................................................................
* Implement the Tcl pixel function. This converts PGPLOT world
* coordinates to X-window pixel coordinates.
*
* Input:
* tkpg TkPgplot * The widget record.
* interp Tcl_Interp * The TCL intrepreter.
* widget char * The name of the PGPLOT widget.
* argc int The number of configuration arguments.
* argv char ** The array of 'argc' configuration arguments.
* [0] The coordinate axes to convert, from:
* "x" - Convert an X-axis coord.
* "y" - Convert a Y-axis coord.
* "xy" - Convert a an X Y axis pair.
* [1] An X-axis world coordinate if [0][0] is
* 'x'.
* A Y-axis world coordinate if [0][0] is
* 'y'.
* [2] This is only expected if [0]=="xy". It
* should then contain the Y-axis
* coordinate to be converted.
* Output:
* return int TCL_OK - Success.
* TCL_ERROR - Failure.
*/
static int tkpg_tcl_pixel(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, int argc, char *argv[])
{
double wx, wy; /* The world X and Y coordinates to be converted */
int xpix, ypix; /* The output X window coordinate */
float rbuf[2]; /* The conversion buffer */
char *axis; /* The axis specification string */
enum {BAD_AXIS, X_AXIS, Y_AXIS, XY_AXIS}; /* Enumerated axis type */
int axtype; /* The decoded axis type */
char *usage = " pixel [x <x>]|[y <x>]|[xy <x> <y>]";
/*
* Check that an axis specification argument has been provided.
*/
if(argc < 1) {
Tcl_AppendResult(interp, "Usage: ", widget, usage, NULL);
return TCL_ERROR;
};
/*
* Decode the axis type and check the expected argument count.
*/
axis = argv[0];
axtype = BAD_AXIS;
switch(*axis++) {
case 'x':
switch(*axis++) {
case 'y':
if(*axis == '\0' && argc == 3)
axtype = XY_AXIS;
break;
case '\0':
if(argc == 2)
axtype = X_AXIS;
break;
};
break;
case 'y':
if(*axis == '\0' && argc == 2)
axtype = Y_AXIS;
break;
};
/*
* Unrecognised axis description?
*/
if(axtype == BAD_AXIS) {
Tcl_AppendResult(interp, "Usage: ", widget, usage, NULL);
return TCL_ERROR;
};
/*
* Get the pixel coordinates to be converted.
*/
switch(axtype) {
case X_AXIS:
if(Tcl_GetDouble(interp, argv[1], &wx) == TCL_ERROR)
return TCL_ERROR;
wy = 0;
break;
case Y_AXIS:
wx = 0;
if(Tcl_GetDouble(interp, argv[1], &wy) == TCL_ERROR)
return TCL_ERROR;
break;
case XY_AXIS:
if(Tcl_GetDouble(interp, argv[1], &wx) == TCL_ERROR ||
Tcl_GetDouble(interp, argv[2], &wy) == TCL_ERROR)
return TCL_ERROR;
break;
};
/*
* Convert the world coordinate to pixel coordinates.
*/
rbuf[0] = wx;
rbuf[1] = wy;
pgx_world2dev(tkpg->pgx, rbuf);
pgx_dev2win(tkpg->pgx, rbuf, &xpix, &ypix);
/*
* Write the pixel coordinate(s) into the reply string.
*/
switch(axtype) {
case X_AXIS:
sprintf(tkpg->buffer, "%d", xpix);
Tcl_AppendResult(interp, tkpg->buffer, NULL);
break;
case Y_AXIS:
sprintf(tkpg->buffer, "%d", ypix);
Tcl_AppendResult(interp, tkpg->buffer, NULL);
break;
case XY_AXIS:
sprintf(tkpg->buffer, "%d %d", xpix, ypix);
Tcl_AppendResult(interp, tkpg->buffer, NULL);
break;
};
return TCL_OK;
}
/*.......................................................................
* Implement the Tcl "return PGPLOT id" function.
*
* Input:
* tkpg TkPgplot * The widget record.
* interp Tcl_Interp * The TCL intrepreter.
* widget char * The name of the PGPLOT widget.
* argc int The number of configuration arguments.
* argv char ** The array of 'argc' configuration arguments.
* (None are expected).
* Output:
* return int TCL_OK - Success.
* TCL_ERROR - Failure.
*/
static int tkpg_tcl_id(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, int argc, char *argv[])
{
/*
* There shouldn't be any arguments.
*/
if(argc != 0) {
Tcl_AppendResult(interp, "Usage: ", widget, " id", NULL);
return TCL_ERROR;
};
/*
* Return the id in the Tcl result string.
*/
sprintf(tkpg->buffer, "%d", tkpg->pgslct_id);
Tcl_AppendResult(interp, tkpg->buffer, NULL);
return TCL_OK;
}
/*.......................................................................
* Implement the Tcl "return PGPLOT device specifier" function.
*
* Input:
* tkpg TkPgplot * The widget record.
* interp Tcl_Interp * The TCL intrepreter.
* widget char * The name of the PGPLOT widget.
* argc int The number of configuration arguments.
* argv char ** The array of 'argc' configuration arguments.
* (None are expected).
* Output:
* return int TCL_OK - Success.
* TCL_ERROR - Failure.
*/
static int tkpg_tcl_device(TkPgplot *tkpg, Tcl_Interp *interp,
char *widget, int argc, char *argv[])
{
/*
* There shouldn't be any arguments.
*/
if(argc != 0) {
Tcl_AppendResult(interp, "Usage: ", widget, " device", NULL);
return TCL_ERROR;
};
/*
* Return the device specifier in the Tcl result string.
*/
Tcl_AppendResult(interp, tkpg->device, NULL);
return TCL_OK;
}
/*.......................................................................
* Return the toplevel window ID of a given tk pathname.
*
* Input:
* interp Tcl_Interp * The TCL intrepreter.
* main_w Tk_Window The main window of the application.
* path char * The tk path name of a window.
* Output:
* return Tk_Window The top-level window of the path, or NULL if
* it doesn't exist. In the latter case an error
* message will have been appended to interp->result.
*/
static Tk_Window tkpg_toplevel_of_path(Tcl_Interp *interp, Tk_Window main_w,
char *path)
{
char *endp; /* The element in path[] following the first path component */
char *first; /* A copy of the first component of the pathname */
int length; /* The length of the first component of the pathname */
Tk_Window w; /* The Tk window of the first component of the pathname */
/*
* The first character of the path should be a dot.
*/
if(!path || *path == '\0' || *path != '.') {
Tcl_AppendResult(interp, "Unknown window: ", path ? path : "(null)",
NULL);
return NULL;
};
/*
* Find the end of the first component of the pathname.
*/
for(endp=path+1; *endp && *endp != '.'; endp++)
;
length = endp - path;
/*
* Make a copy of the name of the first component of the path name.
*/
first = malloc(length + 1);
if(!first) {
Tcl_AppendResult(interp, "Ran out of memory while finding toplevel window.",
NULL);
return NULL;
};
strncpy(first, path, length);
first[length] = '\0';
/*
* Lookup the corresponding window.
*/
w = Tk_NameToWindow(interp, first, main_w);
/*
* Discard the copy.
*/
free(first);
/*
* If the window doesn't exist, Tk_NameToWindow() is documented to place
* an error message in interp->result, so just return the error condition.
*/
if(!w)
return NULL;
/*
* If the looked up window is a toplevel window return it, otherwise
* the toplevel for the specified path must be the main window.
*/
return Tk_IsTopLevel(w) ? w : main_w;
}
|