1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727
|
/*
* gretl -- Gnu Regression, Econometrics and Time-series Library
* Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* gpt_control.c for gretl -- gnuplot controller */
#include "gretl.h"
#include "plotspec.h"
#include "gpt_control.h"
#include "session.h"
#include "gpt_dialog.h"
#include "fileselect.h"
#include "calculator.h"
#include "guiprint.h"
#include "textbuf.h"
#include "graphics.h"
#include "boxplots.h"
#include "dlgutils.h"
#include "winstack.h"
#include "toolbar.h"
#include "clipboard.h"
#ifdef G_OS_WIN32
# include <io.h>
# include "gretlwin32.h"
#endif
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdkkeysyms.h>
#include <errno.h>
#define GPDEBUG 0
#define POINTS_DEBUG 0
/* the following needs more testing */
#define HANDLE_HEREDATA 1
enum {
PLOT_SAVED = 1 << 0,
PLOT_ZOOMED = 1 << 1,
PLOT_ZOOMING = 1 << 2,
PLOT_PNG_COORDS = 1 << 3,
PLOT_HAS_XRANGE = 1 << 4,
PLOT_HAS_YRANGE = 1 << 5,
PLOT_DONT_ZOOM = 1 << 6,
PLOT_DONT_EDIT = 1 << 7,
PLOT_DONT_MOUSE = 1 << 8,
PLOT_POSITIONING = 1 << 9,
PLOT_CURSOR_LABEL = 1 << 10
} plot_status_flags;
enum {
PLOT_TITLE = 1 << 0,
PLOT_XLABEL = 1 << 1,
PLOT_YLABEL = 1 << 2,
PLOT_Y2AXIS = 1 << 3,
PLOT_Y2LABEL = 1 << 4,
PLOT_MARKERS_UP = 1 << 5,
PLOT_POLAR = 1 << 6
} plot_format_flags;
#define MAX_MARKERS 250
#define plot_is_zoomed(p) (p->status & PLOT_ZOOMED)
#define plot_is_zooming(p) (p->status & PLOT_ZOOMING)
#define plot_has_png_coords(p) (p->status & PLOT_PNG_COORDS)
#define plot_has_xrange(p) (p->status & PLOT_HAS_XRANGE)
#define plot_has_yrange(p) (p->status & PLOT_HAS_YRANGE)
#define plot_not_editable(p) (p->status & PLOT_DONT_EDIT)
#define plot_is_editable(p) (!(p->status & PLOT_DONT_EDIT))
#define plot_doing_position(p) (p->status & PLOT_POSITIONING)
#define plot_has_title(p) (p->format & PLOT_TITLE)
#define plot_has_xlabel(p) (p->format & PLOT_XLABEL)
#define plot_has_ylabel(p) (p->format & PLOT_YLABEL)
#define plot_has_y2axis(p) (p->format & PLOT_Y2AXIS)
#define plot_has_y2label(p) (p->format & PLOT_Y2LABEL)
#define plot_labels_shown(p) (p->format & PLOT_MARKERS_UP)
#define plot_is_polar(p) (p->format & PLOT_POLAR)
#define plot_is_range_mean(p) (p->spec->code == PLOT_RANGE_MEAN)
#define plot_is_hurst(p) (p->spec->code == PLOT_HURST)
#define plot_is_roots(p) (p->spec->code == PLOT_ROOTS)
#define plot_has_regression_list(p) (p->spec->reglist != NULL)
#define labels_frozen(p) (p->spec->flags & GPT_PRINT_MARKERS)
#define cant_do_labels(p) (p->err || p->spec->markers == NULL)
#define plot_show_cursor_label(p) (p->status & PLOT_CURSOR_LABEL)
#define plot_has_controller(p) (p->editor != NULL)
enum {
PNG_START,
PNG_ZOOM,
PNG_UNZOOM,
PNG_REDISPLAY
} png_zoom_codes;
struct png_plot_t {
GtkWidget *shell;
GtkWidget *canvas;
GtkWidget *popup;
GtkWidget *statusarea;
GtkWidget *statusbar;
GtkWidget *cursor_label;
GtkWidget *pos_entry;
GtkWidget *editor;
GtkWidget *up_icon;
GtkWidget *down_icon;
GdkWindow *window;
cairo_t *cr;
#if GTK_MAJOR_VERSION >= 3
cairo_surface_t *cs;
#else
GdkPixmap *pixmap;
GdkPixbuf *savebuf;
#endif
GPT_SPEC *spec;
double xmin, xmax;
double ymin, ymax;
int pixel_width, pixel_height;
int pixel_xmin, pixel_xmax;
int pixel_ymin, pixel_ymax;
int xint, yint;
int pd;
int err;
guint cid;
double zoom_xmin, zoom_xmax;
double zoom_ymin, zoom_ymax;
int screen_x0, screen_y0; /* to define selection */
unsigned long status;
unsigned char format;
};
static int render_pngfile (png_plot *plot, int view);
static int repaint_png (png_plot *plot, int view);
static int zoom_replaces_plot (png_plot *plot);
static void prepare_for_zoom (png_plot *plot);
static int get_plot_ranges (png_plot *plot, PlotType ptype);
static void graph_display_pdf (GPT_SPEC *spec);
static void plot_do_rescale (png_plot *plot, int mod);
#ifdef G_OS_WIN32
static void win32_process_graph (GPT_SPEC *spec, int dest);
#else
static void set_plot_for_copy (png_plot *plot);
#endif
static void build_plot_menu (png_plot *plot);
enum {
GRETL_PNG_OK,
GRETL_PNG_NO_OPEN,
GRETL_PNG_NOT_PNG,
GRETL_PNG_NO_COMMENTS,
GRETL_PNG_BAD_COMMENTS,
GRETL_PNG_NO_COORDS
};
typedef struct png_bounds_t png_bounds;
struct png_bounds_t {
int xleft;
int xright;
int ybot;
int ytop;
double xmin;
double xmax;
double ymin;
double ymax;
};
typedef struct linestyle_ linestyle;
struct linestyle_ {
char lc[8]; /* color */
float lw; /* line width */
int dt; /* dash type */
int pt; /* point type */
};
#define MAX_STYLES N_GP_COLORS
static int get_png_bounds_info (png_bounds *bounds);
#define PLOTSPEC_DETAILS_IN_MEMORY(s) (s->lines != NULL)
static void terminate_plot_positioning (png_plot *plot)
{
if (plot->status & PLOT_POSITIONING) {
plot->status ^= PLOT_POSITIONING;
plot->pos_entry = NULL;
gdk_window_set_cursor(plot->window, NULL);
gtk_statusbar_pop(GTK_STATUSBAR(plot->statusbar), plot->cid);
if (plot->editor != NULL) {
gtk_window_present(GTK_WINDOW(plot->editor));
}
}
}
GtkWidget *plot_get_shell (png_plot *plot)
{
return plot->shell;
}
GPT_SPEC *plot_get_spec (png_plot *plot)
{
return plot->spec;
}
int plot_is_saved (const png_plot *plot)
{
return (plot->status & PLOT_SAVED);
}
int plot_is_mouseable (const png_plot *plot)
{
return !(plot->status & PLOT_DONT_MOUSE);
}
double plot_get_xmin (png_plot *plot)
{
return (plot != NULL)? plot->xmin : -1;
}
double plot_get_ymin (png_plot *plot)
{
return (plot != NULL)? plot->ymin : -1;
}
/* apparatus for graph toolbar */
static void graph_enlarge_callback (GtkWidget *w, png_plot *plot)
{
plot_do_rescale(plot, 1);
}
static void graph_shrink_callback (GtkWidget *w, png_plot *plot)
{
plot_do_rescale(plot, -1);
}
static void graph_edit_callback (GtkWidget *w, png_plot *plot)
{
start_editing_png_plot(plot);
}
static void graph_popup_callback (GtkWidget *w, png_plot *plot)
{
build_plot_menu(plot);
gtk_menu_popup(GTK_MENU(plot->popup), NULL, NULL, NULL, NULL,
1, gtk_get_current_event_time());
}
static void plot_winlist_popup (GtkWidget *w, png_plot *plot)
{
window_list_popup(w, NULL, plot->shell);
}
static GretlToolItem plotbar_items[] = {
{ N_("Menu"), GRETL_STOCK_MENU, G_CALLBACK(graph_popup_callback), 0 },
{ N_("Bigger"), GRETL_STOCK_BIGGER, G_CALLBACK(graph_enlarge_callback), 0 },
{ N_("Smaller"), GRETL_STOCK_SMALLER, G_CALLBACK(graph_shrink_callback), 0 },
#if 0
{ N_("Copy"), GTK_STOCK_COPY, G_CALLBACK(graph_copy_callback), 0 },
{ N_("Display PDF"), GRETL_STOCK_PDF, G_CALLBACK(show_pdf_callback), 0 },
{ N_("Edit"), GTK_STOCK_EDIT, G_CALLBACK(graph_edit_callback), 0 },
#endif
{ N_("Windows"), GRETL_STOCK_WINLIST, G_CALLBACK(plot_winlist_popup), 0 }
};
static void add_graph_toolbar (GtkWidget *hbox, png_plot *plot)
{
GtkWidget *tbar, *button;
GretlToolItem *item;
int i, n = G_N_ELEMENTS(plotbar_items);
tbar = gretl_toolbar_new(NULL);
for (i=0; i<n; i++) {
item = &plotbar_items[i];
if (item->func == G_CALLBACK(graph_edit_callback) &&
plot_not_editable(plot)) {
continue;
}
if ((item->func == G_CALLBACK(graph_enlarge_callback) ||
item->func == G_CALLBACK(graph_shrink_callback)) &&
plot_not_editable(plot)) {
continue;
}
button = gretl_toolbar_insert(tbar, item, item->func, plot, -1);
if (item->func == G_CALLBACK(graph_enlarge_callback)) {
plot->up_icon = button;
} else if (item->func == G_CALLBACK(graph_shrink_callback)) {
plot->down_icon = button;
}
}
gtk_box_pack_start(GTK_BOX(hbox), tbar, FALSE, FALSE, 5);
}
/* end apparatus for graph toolbar */
/* Provide the data coordinates for a gretl/gnuplot
graph, if they are all positive, otherwise return
non-zero.
*/
int plot_get_coordinates (png_plot *plot,
double *xmin,
double *xmax,
double *ymin,
double *ymax)
{
int err = 0;
if (plot != NULL && plot->xmin > 0 &&
plot->xmax > plot->xmin &&
plot->ymax > plot->ymin) {
*xmin = plot->xmin;
*xmax = plot->xmax;
*ymin = plot->ymin;
*ymax = plot->ymax;
} else {
err = E_DATA;
gretl_errmsg_set("Couldn't get plot coordinates");
}
return err;
}
void set_plot_has_y2_axis (png_plot *plot, gboolean s)
{
if (s == TRUE) {
plot->format |= PLOT_Y2AXIS;
} else {
plot->format &= ~PLOT_Y2AXIS;
}
}
void plot_position_click (GtkWidget *w, png_plot *plot)
{
if (plot != NULL) {
GtkWidget *entry;
GdkCursor* cursor;
cursor = gdk_cursor_new(GDK_CROSSHAIR);
gdk_window_set_cursor(plot->window, cursor);
gdk_cursor_unref(cursor);
entry = g_object_get_data(G_OBJECT(w), "pos_entry");
plot->pos_entry = entry;
plot->status |= PLOT_POSITIONING;
gtk_statusbar_push(GTK_STATUSBAR(plot->statusbar), plot->cid,
_(" Click to set position"));
}
}
static FILE *open_gp_file (const char *fname, const char *mode)
{
FILE *fp = gretl_fopen(fname, mode);
if (fp == NULL) {
if (*mode == 'w') {
file_write_errbox(fname);
} else {
file_read_errbox(fname);
}
}
return fp;
}
static int commented_term_line (const char *s)
{
return !strncmp(s, "# set term png", 14);
}
static int set_output_line (const char *s)
{
return !strncmp(s, "set output", 10);
}
static int set_encoding_line (const char *s)
{
return !strncmp(s, "set encod", 9);
}
static int set_print_line (const char *s)
{
return (!strncmp(s, "set print ", 10) ||
!strncmp(s, "print \"pixe", 11) ||
!strncmp(s, "print \"data", 11));
}
enum {
REMOVE_PNG,
ADD_PNG
};
static int is_png_term_line (const char *s)
{
return !strncmp(s, "set term png", 12);
}
#ifdef G_OS_WIN32
static void check_win32_png_spec (char *s)
{
if (!strncmp("set term png ", s, 13)) {
/* should now be pngcairo */
*s = '\0';
}
}
#endif
static int
add_or_remove_png_term (const char *fname, int action, GPT_SPEC *spec)
{
FILE *fsrc, *ftmp;
char temp[MAXLEN], fline[MAXLEN];
GptFlags flags = 0;
int err = 0;
sprintf(temp, "%sgpttmp", gretl_dotdir());
ftmp = gretl_tempfile_open(temp);
if (ftmp == NULL) {
return 1;
}
fsrc = open_gp_file(fname, "r");
if (fsrc == NULL) {
fclose(ftmp);
return 1;
}
if (action == ADD_PNG) {
/* see if there's already a png term setting, possibly commented
out, that can be reused */
char restore_line[MAXLEN];
int add_line_styles = 1;
*restore_line = '\0';
while (fgets(fline, sizeof fline, fsrc)) {
if (is_png_term_line(fline) && *restore_line == '\0') {
strcat(restore_line, fline);
} else if (commented_term_line(fline) && *restore_line == '\0') {
strcat(restore_line, fline + 2);
} else if (strstr(fline, "letterbox")) {
flags = GPT_LETTERBOX;
} else if (strstr(fline, "large")) {
flags = GPT_XL;
} else if (strstr(fline, "extra-large")) {
flags = GPT_XXL;
} else if (strstr(fline, "extra-wide")) {
flags = GPT_XW;
} else if (!strncmp(fline, "set style line", 14) ||
!strncmp(fline, "set linetype", 12)) {
add_line_styles = 0;
} else if (!strncmp(fline, "plot", 4)) {
break;
}
}
rewind(fsrc);
#ifdef G_OS_WIN32
/* check for obsolete png term specification (as may be found
in an old session file) */
check_win32_png_spec(restore_line);
#endif
if (*restore_line != '\0') {
fputs(restore_line, ftmp);
fputs("set encoding utf8\n", ftmp);
} else {
const char *tline;
if (spec != NULL) {
tline = get_png_line_for_plotspec(spec);
} else {
tline = gretl_gnuplot_term_line(GP_TERM_PNG,
PLOT_REGULAR,
flags);
}
fprintf(ftmp, "%s\n", tline);
if (strstr(tline, "encoding") == NULL) {
fputs("set encoding utf8\n", ftmp);
}
}
/* Note: we want "set encoding" to be done before we
write the line specifying the output destination
since the filename may be UTF-8.
*/
write_plot_output_line(NULL, ftmp);
if (add_line_styles) {
write_plot_line_styles(spec->code, ftmp);
}
/* now for the body of the plot file */
while (fgets(fline, sizeof fline, fsrc)) {
if (set_print_line(fline)) {
; /* skip it (portability) */
} else if (is_png_term_line(fline)) {
; /* handled above */
} else if (commented_term_line(fline)) {
; /* handled above */
} else if (set_encoding_line(fline)) {
; /* handled above */
} else if (set_output_line(fline)) {
; /* handled above */
} else {
fputs(fline, ftmp);
}
}
write_plot_bounding_box_request(ftmp);
} else {
/* not ADD_PNG: we're removing the png term line */
int printit, png_line_saved = 0;
while (fgets(fline, sizeof fline, fsrc)) {
printit = 1;
if (is_png_term_line(fline)) {
if (!png_line_saved) {
/* comment it out, for future reference */
fprintf(ftmp, "# %s", fline);
png_line_saved = 1;
}
printit = 0;
} else if (commented_term_line(fline)) {
if (png_line_saved) {
printit = 0;
}
} else if (set_output_line(fline)) {
printit = 0;
} else if (spec != NULL && (spec->flags & GPT_FIT_HIDDEN)
&& is_auto_fit_string(fline)) {
printit = 0;
} else if (set_print_line(fline)) {
printit = 0;
}
if (printit) {
fputs(fline, ftmp);
}
}
}
fclose(fsrc);
fclose(ftmp);
/* delete the original */
gretl_remove(fname);
/* and rename the new to the original name */
err = gretl_rename(temp, fname);
if (err) {
fprintf(stderr, "warning: rename failed\n");
}
return err;
}
static int add_png_term_to_plot (const char *fname)
{
return add_or_remove_png_term(fname, ADD_PNG, NULL);
}
static int remove_png_term_from_plot (const char *fname, GPT_SPEC *spec)
{
return add_or_remove_png_term(fname, REMOVE_PNG, spec);
}
/* public because called from session.c when editing plot commands */
int remove_png_term_from_plot_by_name (const char *fname)
{
return add_or_remove_png_term(fname, REMOVE_PNG, NULL);
}
static void mark_plot_as_saved (GPT_SPEC *spec)
{
png_plot *plot = (png_plot *) spec->ptr;
plot->status |= PLOT_SAVED;
}
static int gnuplot_png_init (png_plot *plot, FILE **fpp)
{
GPT_SPEC *spec = plot->spec;
char fname[FILENAME_MAX];
if (plot_is_saved(plot)) {
/* session graph: ensure we're writing to the correct
directory */
session_graph_make_path(fname, spec->fname);
} else {
strcpy(fname, spec->fname);
}
*fpp = gretl_fopen(fname, "w");
if (*fpp == NULL) {
file_write_errbox(fname);
return 1;
}
fprintf(*fpp, "%s\n", get_png_line_for_plotspec(spec));
write_plot_output_line(NULL, *fpp);
return 0;
}
int gp_term_code (gpointer p, int action)
{
GPT_SPEC *spec;
if (action == SAVE_GNUPLOT) {
spec = (GPT_SPEC *) p;
} else {
/* EPS/PDF saver */
spec = graph_saver_get_plotspec(p);
}
return spec->termtype;
}
void filter_gnuplot_file (int mono, FILE *fpin, FILE *fpout)
{
char pline[512];
while (fgets(pline, sizeof pline, fpin)) {
if (set_print_line(pline)) {
break;
}
if (!strncmp(pline, "set term", 8) ||
!strncmp(pline, "set enco", 8) ||
!strncmp(pline, "set outp", 8)) {
continue;
}
if (mono) {
if ((strstr(pline, "set style line") || strstr(pline, "set linetype"))
&& strstr(pline, "rgb")) {
continue;
} else if (strstr(pline, "set style fill solid")) {
fputs("set style fill solid 0.3\n", fpout);
continue;
}
}
fputs(pline, fpout);
}
}
/* note @termstr will be non-NULL only if we're being
called from the interactive PDF/EPS preview or save
context
*/
static int revise_plot_file (GPT_SPEC *spec,
const char *inpname,
const char *outname,
const char *termstr)
{
FILE *fpin = NULL;
FILE *fpout = NULL;
int mono = (spec->flags & GPT_MONO);
int err = 0;
fpin = gretl_fopen(spec->fname, "r");
if (fpin == NULL) {
file_read_errbox(spec->fname);
return 1;
}
fpout = gretl_fopen(inpname, "w");
if (fpout == NULL) {
fclose(fpin);
file_write_errbox(inpname);
return 1;
}
if (outname != NULL && *outname != '\0') {
if (termstr == NULL) {
termstr = gretl_gnuplot_term_line(spec->termtype,
spec->code,
spec->flags);
}
fprintf(fpout, "%s\n", termstr);
if (mono) {
fputs("set mono\n", fpout);
}
if (strstr(termstr, "set encoding") == NULL) {
fputs("set encoding utf8\n", fpout);
}
write_plot_output_line(outname, fpout);
}
filter_gnuplot_file(mono, fpin, fpout);
fclose(fpin);
fclose(fpout);
return err;
}
void save_graph_to_file (gpointer data, const char *fname)
{
GPT_SPEC *spec = (GPT_SPEC *) data;
char pltname[FILENAME_MAX];
int err = 0;
sprintf(pltname, "%sgptout.tmp", gretl_dotdir());
err = revise_plot_file(spec, pltname, fname, NULL);
if (!err) {
gchar *plotcmd;
plotcmd = g_strdup_printf("\"%s\" \"%s\"",
gretl_gnuplot_path(),
pltname);
err = gretl_spawn(plotcmd);
gretl_remove(pltname);
g_free(plotcmd);
if (err) {
gui_errmsg(err);
}
}
}
#define GRETL_PDF_TMP "gretltmp.pdf"
#define GRETL_EPS_TMP "gretltmp.eps"
static void graph_display_pdf (GPT_SPEC *spec)
{
char pdfname[FILENAME_MAX];
char plttmp[FILENAME_MAX];
gchar *plotcmd;
int err = 0;
spec->termtype = GP_TERM_PDF;
gretl_build_path(plttmp, gretl_dotdir(), "gptout.tmp", NULL);
gretl_build_path(pdfname, gretl_dotdir(), GRETL_PDF_TMP, NULL);
err = revise_plot_file(spec, plttmp, pdfname, NULL);
if (err) {
return;
}
plotcmd = g_strdup_printf("\"%s\" \"%s\"",
gretl_gnuplot_path(),
plttmp);
err = gretl_spawn(plotcmd);
gretl_remove(plttmp);
g_free(plotcmd);
if (err) {
gui_errmsg(err);
return;
}
#if defined(G_OS_WIN32)
win32_open_file(pdfname);
#elif defined(OS_OSX)
osx_open_file(pdfname);
#else
gretl_fork("viewpdf", pdfname, NULL);
#endif
}
void saver_preview_graph (GPT_SPEC *spec, char *termstr)
{
char grfname[FILENAME_MAX];
char plttmp[FILENAME_MAX];
gchar *plotcmd;
int err = 0;
gretl_build_path(plttmp, gretl_dotdir(), "gptout.tmp", NULL);
if (spec->termtype == GP_TERM_EPS) {
gretl_build_path(grfname, gretl_dotdir(), GRETL_EPS_TMP, NULL);
} else {
gretl_build_path(grfname, gretl_dotdir(), GRETL_PDF_TMP, NULL);
}
err = revise_plot_file(spec, plttmp, grfname, termstr);
if (err) {
return;
}
plotcmd = g_strdup_printf("\"%s\" \"%s\"", gretl_gnuplot_path(),
plttmp);
err = gretl_spawn(plotcmd);
gretl_remove(plttmp);
g_free(plotcmd);
if (err) {
gui_errmsg(err);
return;
}
#if defined(G_OS_WIN32)
win32_open_file(grfname);
#elif defined(OS_OSX)
osx_open_file(grfname);
#else
if (spec->termtype == GP_TERM_EPS) {
gretl_fork("viewps", grfname, NULL);
} else {
gretl_fork("viewpdf", grfname, NULL);
}
#endif
}
int saver_save_graph (GPT_SPEC *spec, char *termstr, const char *fname)
{
char plttmp[FILENAME_MAX];
int err;
gretl_build_path(plttmp, gretl_dotdir(), "gptout.tmp", NULL);
err = revise_plot_file(spec, plttmp, fname, termstr);
if (!err) {
gchar *plotcmd;
plotcmd = g_strdup_printf("\"%s\" \"%s\"", gretl_gnuplot_path(),
plttmp);
err = gretl_spawn(plotcmd);
gretl_remove(plttmp);
g_free(plotcmd);
if (err) {
gui_errmsg(err);
}
}
return err;
}
/* we're looking for an uncommented "set term ..." */
static int is_term_line (const char *s, int *batch)
{
int ret = 0;
while (isspace(*s)) s++;
if (*s != '#') {
s = strstr(s, "set term");
if (s != NULL) {
*batch = ret = 1;
s += 8;
s += strcspn(s, " "); /* skip "inal"? */
s += strspn(s, " "); /* skip space */
if (!strncmp(s, "win", 3) ||
!strncmp(s, "x11", 3) ||
!strncmp(s, "wxt", 3) ||
!strncmp(s, "qt", 2)) {
/* these are all interactive */
*batch = 0;
}
}
}
return ret;
}
/* Check whether (a) we might want to prepend a "set
term" statement (not if the user has already given
one) and (b) whether we might want to append
"pause mouse close" (not if a batch-type terminal
has been specified).
*/
static void pre_test_plot_buffer (const char *buf,
int *addpause,
int *putterm)
{
char bufline[512];
int batch = 0;
bufgets_init(buf);
while (bufgets(bufline, sizeof bufline, buf)) {
if (is_term_line(bufline, &batch)) {
*putterm = 0;
if (*addpause && batch) {
*addpause = 0;
}
} else if (*addpause && strstr(bufline, "pause ")) {
*addpause = 0;
}
}
bufgets_finalize(buf);
}
/* dump_plot_buffer: this is used when we're taking the material from
an editor window containing gnuplot commands, and either (a)
sending it to gnuplot for execution, or (b) saving it to a "user
file". In the saving-to-file case @addpause will be 0.
This function handles the addition of "pause mouse close",
if @addpause is non-zero.
*/
int dump_plot_buffer (const char *buf, const char *fname,
int addpause)
{
FILE *fp = gretl_fopen(fname, "wb");
int putterm = addpause;
int wxt_ok = 0;
if (fp == NULL) {
file_write_errbox(fname);
return E_FOPEN;
}
if (addpause) {
wxt_ok = gnuplot_has_wxt();
pre_test_plot_buffer(buf, &addpause, &putterm);
if (putterm && wxt_ok) {
fputs("set term wxt size 640,420 noenhanced\n", fp);
}
}
fputs(buf, fp);
if (addpause) {
fputs("pause mouse close\n", fp);
}
fclose(fp);
return 0;
}
#ifdef G_OS_WIN32
static int real_send_to_gp (const char *fname, int persist)
{
return win32_run_async(gretl_gnuplot_path(), fname);
}
#else
static void gnuplot_done (GPid pid, gint status, gpointer p)
{
if (p != NULL) {
gint err_fd = GPOINTER_TO_INT(p);
if (err_fd > 0) {
if (status != 0) {
char buf[128] = {0};
if (read(err_fd, buf, 127) > 0) {
errbox(buf);
}
}
close(err_fd);
}
}
g_spawn_close_pid(pid);
}
static int real_send_to_gp (const char *fname, int persist)
{
const char *gp = gretl_gnuplot_path();
GError *error = NULL;
gchar *argv[4];
GPid pid = 0;
gint fd = -1;
gboolean run;
int err = 0;
argv[0] = g_strdup(gp);
argv[1] = g_strdup(fname);
argv[2] = persist ? g_strdup("-persist") : NULL;
argv[3] = NULL;
run = g_spawn_async_with_pipes(NULL, argv, NULL,
G_SPAWN_SEARCH_PATH |
G_SPAWN_DO_NOT_REAP_CHILD,
NULL, NULL, &pid,
NULL, NULL,
&fd, &error);
if (error != NULL) {
errbox(error->message);
g_error_free(error);
err = 1;
} else if (!run) {
errbox(_("gnuplot command failed"));
err = 1;
} else if (pid > 0) {
gpointer p = fd > 0 ? GINT_TO_POINTER(fd) : NULL;
g_child_watch_add(pid, gnuplot_done, p);
}
g_free(argv[0]);
g_free(argv[1]);
g_free(argv[2]);
return err;
}
#endif
/* Callback for execute icon in window editing gnuplot
commands: send script in @buf to gnuplot itself
*/
void run_gnuplot_script (gchar *buf)
{
gchar *tmpfile;
int err;
tmpfile = g_strdup_printf("%sshowtmp.gp", gretl_dotdir());
err = dump_plot_buffer(buf, tmpfile, 1);
if (!err) {
err = real_send_to_gp(tmpfile, 1);
}
g_free(tmpfile);
}
#ifdef G_OS_WIN32
/* common code for sending an EMF file to the clipboard,
or printing an EMF, on MS Windows
*/
static void win32_process_graph (GPT_SPEC *spec, int dest)
{
char emfname[FILENAME_MAX];
char plttmp[FILENAME_MAX];
gchar *plotcmd;
int err = 0;
spec->termtype = GP_TERM_EMF;
gretl_build_path(plttmp, gretl_dotdir(), "gptout.tmp", NULL);
gretl_build_path(emfname, gretl_dotdir(), "gpttmp.emf", NULL);
err = revise_plot_file(spec, plttmp, emfname, NULL);
if (err) {
return;
}
plotcmd = g_strdup_printf("\"%s\" \"%s\"",
gretl_gnuplot_path(),
plttmp);
err = gretl_spawn(plotcmd);
g_free(plotcmd);
gretl_remove(plttmp);
if (err) {
errbox(_("Gnuplot error creating graph"));
} else if (dest == WIN32_TO_CLIPBOARD) {
err = emf_to_clipboard(emfname);
} else if (dest == WIN32_TO_PRINTER) {
err = win32_print_graph(emfname);
}
gretl_remove(emfname);
}
#else /* ! MS Windows */
static GPT_SPEC *copyspec;
static gboolean cb_image_mono;
/* Here we're just posting the information that an image
should be available for pasting (and also recording
if the user wanted it to be monochrome).
*/
static void set_plot_for_copy (png_plot *plot)
{
copyspec = plot->spec;
cb_image_mono = copyspec->flags & GPT_MONO ? 1 : 0;
flag_image_available();
}
/* Here we're responding to a request to paste the plot
advertised above; @target tells us which of the posted
formats the application has selected.
*/
int write_plot_for_copy (int target)
{
GPT_SPEC *spec = copyspec;
char outname[FILENAME_MAX];
char inpname[FILENAME_MAX];
GptFlags saveflags;
double savescale;
int saveterm;
int err = 0;
if (spec == NULL) {
fprintf(stderr, "retrieve_plot_for_copy: no data\n");
return 1;
}
saveflags = spec->flags;
saveterm = spec->termtype;
savescale = spec->scale;
if (target == TARGET_SVG) {
spec->termtype = GP_TERM_SVG;
} else if (target == TARGET_EMF) {
spec->termtype = GP_TERM_EMF;
} else if (target == TARGET_PNG) {
spec->termtype = GP_TERM_PNG;
spec->scale = 0.8;
} else if (target == TARGET_EPS) {
spec->termtype = GP_TERM_EPS;
} else if (target == TARGET_PDF) {
spec->termtype = GP_TERM_PDF;
} else {
fprintf(stderr, "write_plot_for_copy: unsupported type\n");
return 1;
}
if (cb_image_mono) {
spec->flags |= GPT_MONO;
}
gretl_build_path(inpname, gretl_dotdir(), "gptinp.tmp", NULL);
gretl_build_path(outname, gretl_dotdir(), "gptout.tmp", NULL);
err = revise_plot_file(spec, inpname, outname, NULL);
spec->flags = saveflags;
spec->termtype = saveterm;
spec->scale = savescale;
if (!err) {
gchar *plotcmd = g_strdup_printf("\"%s\" \"%s\"",
gretl_gnuplot_path(),
inpname);
err = gretl_spawn(plotcmd);
g_free(plotcmd);
gretl_remove(inpname);
}
if (err) {
errbox(_("Gnuplot error creating graph"));
} else {
err = image_file_to_clipboard(outname);
}
gretl_remove(outname);
return err;
}
#endif /* Windows vs not */
/* chop trailing comma, if present; return 1 if comma chopped,
zero otherwise */
static int chop_comma (char *str)
{
size_t i, n = strlen(str);
for (i=n-1; i>0; i--) {
if (isspace((unsigned char) str[i])) {
continue;
}
if (str[i] == ',') {
str[i] = 0;
return 1;
} else {
break;
}
}
return 0;
}
/* returns non-zero on obtaining a marker */
static int get_gpt_marker (const char *line, char *label,
const char *format)
{
const char *p = strchr(line, '#');
#if GPDEBUG > 1
fprintf(stderr, "get_gpt_marker, p='%s'\n", p);
#endif
*label = '\0';
if (p != NULL) {
sscanf(p + 2, format, label);
#if GPDEBUG > 1
fprintf(stderr, "read marker: '%s'\n", label);
#endif
}
return *label != '\0';
}
/* special graphs for which editing via GUI is not supported */
#define cant_edit(p) (p == PLOT_CORRELOGRAM || \
p == PLOT_LEVERAGE || \
p == PLOT_MULTI_IRF || \
p == PLOT_MULTI_SCATTER || \
p == PLOT_PANEL || \
p == PLOT_TRI_GRAPH || \
p == PLOT_BI_GRAPH || \
p == PLOT_ELLIPSE || \
p == PLOT_RQ_TAU || \
p == PLOT_3D || \
p == PLOT_HEATMAP)
/* graphs where we don't attempt to find data coordinates */
#define no_readback(p) (p == PLOT_CORRELOGRAM || \
p == PLOT_LEVERAGE || \
p == PLOT_MULTI_IRF || \
p == PLOT_MULTI_SCATTER || \
p == PLOT_PANEL || \
p == PLOT_TRI_GRAPH || \
p == PLOT_BI_GRAPH || \
p == PLOT_STACKED_BAR || \
p == PLOT_BAR || \
p == PLOT_3D)
#define gp_missing(s) (s[0] == '?' || !strcmp(s, "NaN"))
static int get_gpt_data (GPT_SPEC *spec,
int *do_markers,
const char *buf)
{
char s[MAXLEN];
char *got = NULL;
double *x[5] = { NULL };
char test[5][32];
char obsfmt[12] = {0};
int started_data_lines = 0;
int i, j, t, imin = 0;
int err = 0;
spec->okobs = spec->nobs;
gretl_push_c_numeric_locale();
/* first handle "shaded bars" info, if present */
for (i=0; i<spec->nbars && !err; i++) {
double y1, y2, dx[2];
/* start date */
got = bufgets(s, sizeof s, buf);
if (got == NULL ||
sscanf(s, "%lf %lf %lf", &dx[0], &y1, &y2) != 3) {
err = 1;
break;
}
/* stop date */
got = bufgets(s, sizeof s, buf);
if (got == NULL ||
sscanf(s, "%lf %lf %lf", &dx[1], &y1, &y2) != 3) {
err = 1;
break;
}
/* trailing 'e' */
got = bufgets(s, sizeof s, buf);
if (got) {
plotspec_set_bar_info(spec, i, dx[0], dx[1]);
} else {
err = 1;
}
}
if (*do_markers) {
sprintf(obsfmt, "%%%d[^\r\n]", OBSLEN - 1);
}
/* then get the regular plot data */
for (i=0; i<spec->n_lines && !err; i++) {
int ncols = gp_line_data_columns(spec, i);
int okobs = spec->nobs;
int offset = 1;
if (ncols == 0) {
if (i == 0) {
imin = 1;
}
continue;
}
#if GPDEBUG
fprintf(stderr, "reading data, line %d\n", i);
#endif
if (!started_data_lines) {
offset = 0;
x[0] = spec->data->val;
x[1] = x[0] + spec->nobs;
started_data_lines = 1;
}
x[2] = x[1] + spec->nobs;
x[3] = x[2] + spec->nobs;
x[4] = x[3] + spec->nobs;
for (t=0; t<spec->nobs && !err; t++) {
int missing = 0;
int nf = 0;
got = bufgets(s, sizeof s, buf);
if (got == NULL) {
err = 1;
break;
}
nf = 0;
if (ncols == 5) {
nf = sscanf(s, "%31s %31s %31s %31s %31s", test[0], test[1], test[2],
test[3], test[4]);
} else if (ncols == 4) {
nf = sscanf(s, "%31s %31s %31s %31s", test[0], test[1], test[2], test[3]);
} else if (ncols == 3) {
nf = sscanf(s, "%31s %31s %31s", test[0], test[1], test[2]);
} else if (ncols == 2) {
nf = sscanf(s, "%31s %31s", test[0], test[1]);
}
if (nf != ncols) {
err = 1;
}
for (j=offset; j<nf && !err; j++) {
if (gp_missing(test[j])) {
x[j][t] = NADBL;
missing++;
} else if (j == 0 && (spec->flags & GPT_TIMEFMT)) {
/* allow for backward compatibility */
x[j][t] = gnuplot_time_from_date(test[j], spec->timefmt);
if (na(x[j][t])) {
err = E_DATA;
}
} else {
x[j][t] = atof(test[j]);
}
}
if (missing) {
okobs--;
}
if (i <= imin && *do_markers) {
*do_markers = get_gpt_marker(s, spec->markers[t], obsfmt);
if (spec->code == PLOT_FACTORIZED && imin == 0) {
imin = 1;
}
}
}
if (okobs < spec->okobs) {
spec->okobs = okobs;
}
/* trailer line for data block */
bufgets(s, sizeof s, buf);
/* shift 'y' writing location */
x[1] += (ncols - 1) * spec->nobs;
}
#if GPDEBUG
gretl_matrix_print(spec->data, "spec->data");
#endif
gretl_pop_c_numeric_locale();
return err;
}
static int get_gpt_heredata (GPT_SPEC *spec,
int *do_markers,
long barpos,
long datapos,
const char *buf)
{
gretl_matrix *m = spec->data;
char line[MAXLEN], test[32];
char obsfmt[12] = {0};
char *s, *got = NULL;
double xij;
int i, j;
int err = 0;
spec->okobs = spec->nobs;
gretl_push_c_numeric_locale();
/* first handle "shaded bars" info, if present */
if (barpos > 0) {
bufseek(buf, barpos);
for (i=0; i<spec->nbars && !err; i++) {
double y1, y2, dx[2];
/* start date */
got = bufgets(line, sizeof line, buf);
if (got == NULL ||
sscanf(line, "%lf %lf %lf", &dx[0], &y1, &y2) != 3) {
err = 1;
break;
}
/* stop date */
got = bufgets(line, sizeof line, buf);
if (got == NULL ||
sscanf(line, "%lf %lf %lf", &dx[1], &y1, &y2) != 3) {
err = 1;
break;
}
plotspec_set_bar_info(spec, i, dx[0], dx[1]);
}
}
if (*do_markers) {
sprintf(obsfmt, "%%%d[^\r\n]", OBSLEN - 1);
}
/* then get the regular plot data */
bufseek(buf, datapos);
for (i=0; i<m->rows && !err; i++) {
got = bufgets(line, sizeof line, buf);
if (got == NULL) {
err = 1;
}
s = line;
for (j=0; j<m->cols && !err; j++) {
s += strspn(s, " ");
sscanf(s, "%31s", test);
if (gp_missing(test)) {
xij = NADBL;
} else if (j == 0 && (spec->flags & GPT_TIMEFMT)) {
xij = gnuplot_time_from_date(test, spec->timefmt);
} else {
xij = atof(test);
}
gretl_matrix_set(m, i, j, xij);
s += strlen(test);
if (*do_markers) {
*do_markers = get_gpt_marker(s, spec->markers[i], obsfmt);
}
}
}
gretl_pop_c_numeric_locale();
#if GPDEBUG
gretl_matrix_print(m, "gp heredata");
#endif
gretl_pop_c_numeric_locale();
return err;
}
static int line_starts_heredata (const char *line,
char **eod)
{
int ret = 0;
if (*line == '$') {
const char *p = strstr(line, "<<");
if (p != NULL) {
int len;
p += 2;
p += strspn(p, " ");
len = gretl_namechar_spn(p);
if (len > 0) {
*eod = gretl_strndup(p, len);
ret = 1;
}
}
}
return ret;
}
/* read a gnuplot source line specifying a text label */
static int parse_label_line (GPT_SPEC *spec, const char *line)
{
const char *p, *s;
char *text = NULL;
double x, y;
int nc, just = GP_JUST_LEFT;
int err = 0;
/* Examples:
set label "this is a label" at 1998.26,937.557 left front
set label 'foobar' at 1500,350 left
*/
/* find first double or single quote */
p = strchr(line, '"');
if (p == NULL) {
p = strchr(line, '\'');
}
if (p == NULL) {
/* no label text found */
return 1;
}
text = gretl_quoted_string_strdup(p, &s);
if (text == NULL) {
return 1;
}
/* get the position */
p = strstr(s, "at");
if (p == NULL) {
err = E_DATA;
} else {
p += 2;
gretl_push_c_numeric_locale();
nc = sscanf(p, "%lf,%lf", &x, &y);
gretl_pop_c_numeric_locale();
if (nc != 2) {
err = E_DATA;
}
}
if (!err) {
/* justification */
if (strstr(p, "right")) {
just = GP_JUST_RIGHT;
} else if (strstr(p, "center")) {
just = GP_JUST_CENTER;
}
}
if (!err) {
err = plotspec_add_label(spec);
if (!err) {
int i = spec->n_labels - 1;
strncat(spec->labels[i].text, text, PLOT_LABEL_TEXT_LEN);
spec->labels[i].pos[0] = x;
spec->labels[i].pos[1] = y;
spec->labels[i].just = just;
}
}
free(text);
return err;
}
/* read a gnuplot source line specifying an arrow */
static int parse_arrow_line (GPT_SPEC *spec, const char *line)
{
double x0, y0, x1, y1;
char head[12] = {0};
int n, err = 0;
/* Example:
set arrow from 2000,150 to 2000,500 nohead [ lt 0 ]
*/
gretl_push_c_numeric_locale();
n = sscanf(line, "set arrow from %lf,%lf to %lf,%lf %s",
&x0, &y0, &x1, &y1, head);
gretl_pop_c_numeric_locale();
if (n < 5) {
err = E_DATA;
} else {
err = plotspec_add_arrow(spec);
}
if (!err) {
int flags = 0;
if (strcmp(head, "nohead")) {
flags |= GP_ARROW_HEAD;
}
if (strstr(line, "lt 0")) {
flags |= GP_ARROW_DOTS;
}
n = spec->n_arrows - 1;
spec->arrows[n].x0 = x0;
spec->arrows[n].y0 = y0;
spec->arrows[n].x1 = x1;
spec->arrows[n].y1 = y1;
spec->arrows[n].flags = flags;
}
return err;
}
static int
read_plotspec_range (const char *obj, const char *s, GPT_SPEC *spec)
{
double r0, r1;
int i = 0, err = 0;
if (!strcmp(obj, "xrange")) {
i = 0;
} else if (!strcmp(obj, "yrange")) {
i = 1;
} else if (!strcmp(obj, "y2range")) {
i = 2;
} else if (!strcmp(obj, "trange")) {
i = 3;
} else if (!strcmp(obj, "x2range")) {
i = 4;
} else {
err = 1;
}
if (!strcmp(s, "[*:*]")) {
r0 = r1 = NADBL;
} else {
gretl_push_c_numeric_locale();
if (!err && sscanf(s, "[%lf:%lf]", &r0, &r1) != 2) {
err = 1;
}
gretl_pop_c_numeric_locale();
}
if (!err) {
spec->range[i][0] = r0;
spec->range[i][1] = r1;
}
return err;
}
static int read_plot_logscale (const char *s, GPT_SPEC *spec)
{
char axis[3] = {0};
double base = 0;
int i, n, err = 0;
n = sscanf(s, "%2s %lf", axis, &base);
if (n < 1 || (n == 2 && base < 1.1)) {
err = 1;
} else {
if (n == 1) {
base = 10.0;
}
if (!strcmp(axis, "x")) {
i = 0;
} else if (!strcmp(axis, "y")) {
i = 1;
} else if (!strcmp(axis, "y2")) {
i = 2;
} else {
err = 1;
}
}
if (!err) {
spec->logbase[i] = base;
}
return err;
}
static int read_plot_format (const char *s, GPT_SPEC *spec,
int timefmt)
{
char fmt[16];
char axis = '\0';
int n, err = 0;
if (timefmt) {
/* including 'x' axis here is actually wrong,
but it may be in some old plot files */
s += strspn(s, " ");
if (*s == 'x') {
n = sscanf(s, "%c \"%15[^\"]", &axis, fmt);
err = n < 2;
} else {
n = sscanf(s, "%15s", fmt);
err = n < 1;
}
} else {
n = sscanf(s, "%c \"%15[^\"]", &axis, fmt);
err = n < 2;
}
if (!err) {
if (timefmt) {
*spec->timefmt = '\0';
strncat(spec->timefmt, fmt, 15);
} else if (axis == 'x') {
*spec->xfmt = '\0';
strncat(spec->xfmt, fmt, 15);
} else if (axis == 'y') {
*spec->yfmt = '\0';
strncat(spec->yfmt, fmt, 15);
}
}
return err;
}
static int catch_value (char *targ, const char *src, int maxlen)
{
int i, n, q = 0;
int ret = 0;
src += strspn(src, " \t\r\n");
if (*src == '\'' || *src == '"') {
q = *src;
src++;
}
*targ = '\0';
if (*src != '\0') {
strncat(targ, src, maxlen - 1);
n = strlen(targ);
for (i=n-1; i>=0; i--) {
if (isspace((unsigned char) targ[i])) {
targ[i] = '\0';
} else {
break;
}
}
if (targ[i] == '\'' || targ[i] == '"') {
if (i == 0 && targ[i] == q) {
ret = 1; /* accept empty string as "value" */
}
targ[i] = '\0';
}
}
return ret || (*targ != '\0');
}
static void fix_old_roots_plot (GPT_SPEC *spec)
{
strings_array_free(spec->literal, spec->n_literal);
spec->literal = NULL;
spec->n_literal = 0;
spec->flags |= (GPT_POLAR | GPT_XZEROAXIS | GPT_YZEROAXIS);
spec->border = 0;
spec->keyspec = GP_KEY_NONE;
strcpy(spec->xtics, "none");
strcpy(spec->ytics, "none");
}
static int unhandled_gp_line_error (const char *s)
{
int n = strlen(s);
if (s[n-1] == '\n') {
char *tmp = gretl_strndup(s, n-1);
fprintf(stderr, "unhandled gnuplot command: '%s'\n", tmp);
free(tmp);
} else {
fprintf(stderr, "unhandled gnuplot command: '%s'\n", s);
}
return 1;
}
static int parse_gp_unset_line (GPT_SPEC *spec, const char *s)
{
char key[16] = {0};
int err = 0;
if (sscanf(s + 6, "%15s", key) != 1) {
err = 1;
} else if (!strcmp(key, "border")) {
spec->border = 0;
} else if (!strcmp(key, "key")) {
spec->keyspec = GP_KEY_NONE;
} else if (!strcmp(key, "xtics")) {
strcpy(spec->xtics, "none");
} else if (!strcmp(key, "ytics")) {
strcpy(spec->ytics, "none");
} else {
/* unrecognized "unset" parameter */
err = 1;
}
if (err) {
unhandled_gp_line_error(s);
}
return err;
}
static void read_xtics_setting (GPT_SPEC *spec,
const char *key,
const char *val)
{
if (!strcmp(key, "x2tics")) {
spec->x2ticstr = gretl_strdup(val);
} else if (*val == '(') {
spec->xticstr = gretl_strdup(val);
} else {
*spec->xtics = '\0';
strncat(spec->xtics, val, sizeof(spec->xtics) - 1);
}
}
/* Try to accept variant RGB specifications besides the
one that's standard in gretl plot files, namely
"#RRGGBB" in hex.
*/
static int verify_rgb (char *rgb)
{
char *s = rgb;
char delim = s[0];
int err = 0;
if (delim == '"' || delim == '\'') {
const char *p;
s++;
p = strchr(s, delim);
if (p != NULL) {
char test[16] = {0};
int len = p - s;
if (len > 0 && len < 16) {
strncat(test, s, len);
err = parse_gnuplot_color(test, rgb);
} else {
err = E_DATA;
}
} else {
err = E_DATA;
}
}
return err;
}
static int parse_linetype (const char *s, linestyle *styles)
{
const char *p;
int n, i = 0;
int err = 0;
n = sscanf(s, " %d", &i);
if (n != 1 || i <= 0 || i > MAX_STYLES) {
/* get out on error */
return 1;
} else {
/* convert index to zero-based */
i -= 1;
}
if (!err && (p = strstr(s, " lc ")) != NULL) {
char lc[20];
p += 4;
if (!strncmp(p, "rgb ", 4)) {
p += 4;
}
p += strspn(p, " ");
if (sscanf(p, "%19s", lc)) {
err = verify_rgb(lc);
if (!err) {
strcpy(styles[i].lc, lc);
}
} else {
err = 1;
}
}
if (!err && (p = strstr(s, " lw ")) != NULL) {
float lw = 0.0;
if (sscanf(p + 4, "%f", &lw)) {
styles[i].lw = lw;
} else {
err = 1;
}
}
if (!err && (p = strstr(s, " dt ")) != NULL) {
int dt = 0;
if (sscanf(p + 4, "%d", &dt)) {
styles[i].dt = dt;
} else {
err = 1;
}
}
if (!err && (p = strstr(s, " pt ")) != NULL) {
int pt = 0;
if (sscanf(p + 4, "%d", &pt)) {
styles[i].pt = pt;
} else {
err = 1;
}
}
return err;
}
static int parse_gp_set_line (GPT_SPEC *spec,
const char *s,
linestyle *styles,
int *unhandled)
{
char key[16] = {0};
char val[MAXLEN] = {0};
int lt_pos = 0;
int err = 0;
if (!strncmp(s, "set linetype", 12)) {
/* e.g. set linetype 1 lc rgb "#ff0000" */
lt_pos = 12;
} else if (!strncmp(s, "set style line", 14)) {
/* legacy: e.g. set style line 1 lc rgb "#ff0000" */
lt_pos = 14;
}
if (lt_pos > 0) {
err = parse_linetype(s + lt_pos, styles);
if (err && unhandled != NULL) {
*unhandled = 1;
}
return err;
}
if (unhandled != NULL) {
/* we're peeking at "literal" lines */
if (strstr(s, "set style data histogram")) {
spec->code = PLOT_BAR;
*unhandled = 1;
return 0;
} else if (strstr(s, "histogram rowstacked")) {
spec->code = PLOT_STACKED_BAR;
*unhandled = 1;
return 0;
}
}
if (sscanf(s + 4, "%11s", key) != 1) {
return unhandled_gp_line_error(s);
}
#if GPDEBUG
fprintf(stderr, "parse_gp_set_line: key = '%s'\n", key);
#endif
if (!strcmp(key, "term") ||
!strcmp(key, "output") ||
!strcmp(key, "encoding") ||
!strcmp(key, "datafile")) {
/* we ignore these */
return 0;
}
/* first, settings that don't require a parameter */
if (!strcmp(key, "y2tics")) {
spec->flags |= GPT_Y2AXIS;
return 0;
} else if (!strcmp(key, "parametric")) {
spec->flags |= GPT_PARAMETRIC;
return 0;
} else if (!strcmp(key, "polar")) {
spec->flags |= GPT_POLAR;
return 0;
} else if (!strcmp(key, "mono") ||
!strcmp(key, "monochrome")) {
spec->flags |= GPT_MONO;
return 0;
} else if (!strcmp(key, "xzeroaxis")) {
spec->flags |= GPT_XZEROAXIS;
return 0;
} else if (!strcmp(key, "yzeroaxis")) {
spec->flags |= GPT_YZEROAXIS;
return 0;
} else if (!strcmp(key, "noxtics")) {
strcpy(spec->xtics, "none");
return 0;
} else if (!strcmp(key, "noytics")) {
strcpy(spec->ytics, "none");
return 0;
} else if (!strcmp(key, "nokey")) {
spec->keyspec = GP_KEY_NONE;
return 0;
} else if (!strcmp(key, "label")) {
parse_label_line(spec, s);
return 0;
} else if (!strcmp(key, "arrow")) {
parse_arrow_line(spec, s);
return 0;
}
/* grid lines: parameter is optional */
if (!strcmp(key, "grid")) {
if (catch_value(val, s + 4 + strlen(key), MAXLEN)) {
if (!strcmp(val, "ytics")) {
spec->flags |= GPT_GRID_Y;
} else if (!strcmp(val, "xtics")) {
spec->flags |= GPT_GRID_X;
}
} else {
spec->flags |= GPT_GRID_Y;
spec->flags |= GPT_GRID_X;
}
}
/* now catch value for settings that need a parameter */
if (!catch_value(val, s + 4 + strlen(key), MAXLEN)) {
return 0;
}
#if GPDEBUG
fprintf(stderr, " value = '%s'\n", val);
#endif
if (strstr(key, "range")) {
if (read_plotspec_range(key, val, spec)) {
return unhandled_gp_line_error(s);
}
} else if (!strcmp(key, "logscale")) {
if (read_plot_logscale(val, spec)) {
return unhandled_gp_line_error(s);
}
} else if (!strcmp(key, "format")) {
if (read_plot_format(val, spec, 0)) {
return unhandled_gp_line_error(s);
}
} else if (!strcmp(key, "timefmt")) {
if (read_plot_format(val, spec, 1)) {
return unhandled_gp_line_error(s);
}
} else if (!strcmp(key, "title")) {
strcpy(spec->titles[0], val);
} else if (!strcmp(key, "xlabel")) {
strcpy(spec->titles[1], val);
*spec->xvarname = '\0';
strncat(spec->xvarname, val, MAXDISP-1);
} else if (!strcmp(key, "ylabel")) {
strcpy(spec->titles[2], val);
*spec->yvarname = '\0';
strncat(spec->yvarname, val, MAXDISP-1);
} else if (!strcmp(key, "y2label")) {
strcpy(spec->titles[3], val);
} else if (!strcmp(key, "x2label")) {
strcpy(spec->titles[4], val);
} else if (!strcmp(key, "key")) {
spec->keyspec = gp_keypos_from_name(val);
} else if (!strcmp(key, "xtics") || !strcmp(key, "x2tics")) {
read_xtics_setting(spec, key, val);
} else if (!strcmp(key, "mxtics")) {
*spec->mxtics = '\0';
strncat(spec->mxtics, val, sizeof(spec->mxtics) - 1);
} else if (!strcmp(key, "ytics")) {
*spec->ytics = '\0';
strncat(spec->ytics, val, sizeof(spec->ytics) - 1);
} else if (!strcmp(key, "border")) {
spec->border = atoi(val);
} else if (!strcmp(key, "bmargin")) {
spec->bmargin = atoi(val);
} else if (!strcmp(key, "boxwidth")) {
spec->boxwidth = (float) atof(val);
if (strstr(s, "absolute")) {
spec->boxwidth = -spec->boxwidth;
}
} else if (!strcmp(key, "samples")) {
spec->samples = atoi(val);
} else if (!strcmp(key, "xdata")) {
if (!strncmp(val, "time", 4)) {
spec->flags |= GPT_TIMEFMT;
}
} else if (unhandled != NULL) {
*unhandled = 1;
}
return 0;
}
/* allocate markers for identifying particular data points */
static int plotspec_allocate_markers (GPT_SPEC *spec)
{
spec->markers = strings_array_new_with_length(spec->nobs,
OBSLEN);
if (spec->markers == NULL) {
spec->n_markers = 0;
return E_ALLOC;
} else {
spec->n_markers = spec->nobs;
return 0;
}
}
static void plotspec_destroy_markers (GPT_SPEC *spec)
{
strings_array_free(spec->markers, spec->n_markers);
spec->markers = NULL;
spec->n_markers = 0;
}
/* Determine the number of data points in a plot. While we're at it,
determine the type of plot, check whether there are any
data-point markers along with the data, and see if there are
are definitions of time-series vertical bars.
*/
static void get_plot_nobs (GPT_SPEC *spec,
const char *buf,
int *do_markers,
long *barpos,
long *datapos)
{
int n = 0, started = -1;
int startmin = 1;
long auxpos = 0;
char line[MAXLEN], test[12];
char *eod = NULL;
char *p = NULL;
spec->code = PLOT_REGULAR;
*do_markers = 0;
while (bufgets(line, MAXLEN - 1, buf)) {
if (*line == '#' && spec->code == PLOT_REGULAR) {
tailstrip(line);
spec->code = plot_type_from_string(line);
}
if (sscanf(line, "# n_bars = %d", &spec->nbars) == 1) {
/* for old-style data representation */
startmin += spec->nbars;
continue;
}
if (spec->nbars > 0 && !strncmp(line, "$bars <", 7)) {
*barpos = buftell(buf);
}
if (!strncmp(line, "# auxdata", 9)) {
auxpos = buftell(buf);
}
if (started < 0 && line_starts_heredata(line, &eod)) {
/* new method of handling plot data */
#if GPDEBUG
fprintf(stderr, "*** got heredata!\n");
#endif
spec->heredata = 1;
*datapos = buftell(buf);
continue;
}
if (eod != NULL) {
if (!strncmp(line, eod, strlen(eod))) {
#if GPDEBUG
fprintf(stderr, "*** reached end of heredata (%s)\n", eod);
#endif
free(eod);
eod = NULL;
break;
} else if (*do_markers == 0 && (p = strchr(line, '#')) != NULL) {
if (sscanf(p + 1, "%8s", test) == 1) {
*do_markers = 1;
}
}
n++;
} else {
if (!strncmp(line, "plot", 4) || !strncmp(line, "splot", 5)) {
started = 0;
}
if (started == 0 && strchr(line, '\\') == NULL) {
started = 1;
continue;
}
if (started > 0 && started < startmin) {
if (*line == 'e') {
started++;
continue;
}
}
if (started == startmin) {
if (*line == 'e' || !strncmp(line, "set ", 4)) {
/* end of data, or onto "set print" for bounds */
break;
} else if (*do_markers == 0 && (p = strchr(line, '#')) != NULL) {
if (sscanf(p + 1, "%11s", test) == 1) {
*do_markers = 1;
}
}
n++;
}
}
}
spec->nobs = n;
if (spec->code == PLOT_BOXPLOTS) {
/* In the case of boxplots the gnuplot file may contain extra
(outlier) data, which have to be read into an auxiliary
matrix.
*/
if (auxpos > 0) {
/* we already went past it */
bufseek(buf, auxpos);
buf_back_lines(buf, 1);
bufgets(line, MAXLEN - 1, buf);
} else if (!spec->heredata) {
/* try reading further */
while (bufgets(line, MAXLEN - 1, buf)) {
if (!strncmp(line, "# auxdata", 9)) {
auxpos = buftell(buf);
break;
}
}
}
if (auxpos > 0) {
double x, y;
int i, n, r = 0, c = 0;
int err = 0;
n = sscanf(line + 9, "%d %d", &r, &c);
if (n == 2 && r > 0 && c == 2) {
spec->auxdata = gretl_matrix_alloc(r, c);
if (spec->auxdata != NULL) {
gretl_push_c_numeric_locale();
if (spec->heredata) {
/* skip a line: "$aux << EOA" */
if (bufgets(line, MAXLEN - 1, buf) == NULL) {
err = E_DATA;
}
}
for (i=0; i<r && !err; i++) {
if (bufgets(line, MAXLEN - 1, buf) == NULL) {
err = E_DATA;
} else if (sscanf(line, "%lf %lf", &x, &y) != 2) {
err = E_DATA;
} else {
gretl_matrix_set(spec->auxdata, i, 0, x);
gretl_matrix_set(spec->auxdata, i, 1, y);
}
}
gretl_pop_c_numeric_locale();
if (err) {
gretl_matrix_free(spec->auxdata);
spec->auxdata = NULL;
}
}
}
}
}
}
static int grab_fit_coeffs (GPT_SPEC *spec, const char *s)
{
gretl_matrix *b = NULL;
int k, f = spec->fit;
int n = 0, err = 0;
k = (f == PLOT_FIT_CUBIC)? 4 : (f == PLOT_FIT_QUADRATIC)? 3 : 2;
b = gretl_column_vector_alloc(k);
if (b == NULL) {
return E_ALLOC;
}
if (!strncmp(s, "exp(", 4)) {
s += 4;
}
gretl_push_c_numeric_locale();
if (k == 2) {
n = sscanf(s, "%lf + %lf", &b->val[0], &b->val[1]);
} else if (k == 3) {
n = sscanf(s, "%lf + %lf*x + %lf", &b->val[0],
&b->val[1], &b->val[2]);
} else if (k == 4) {
n = sscanf(s, "%lf + %lf*x + %lf*x**2 + %lf", &b->val[0],
&b->val[1], &b->val[2], &b->val[3]);
}
gretl_pop_c_numeric_locale();
if (n != k) {
err = E_DATA;
gretl_matrix_free(b);
spec->flags &= ~GPT_AUTO_FIT;
} else if (f == PLOT_FIT_OLS) {
spec->b_ols = b;
} else if (f == PLOT_FIT_INVERSE) {
spec->b_inv = b;
} else if (f == PLOT_FIT_LOGLIN) {
spec->b_log = b;
} else if (f == PLOT_FIT_QUADRATIC) {
spec->b_quad = b;
} else if (f == PLOT_FIT_CUBIC) {
spec->b_cub = b;
} else if (f == PLOT_FIT_LINLOG) {
spec->b_linlog = b;
}
return err;
}
/* scan the stuff after "title '" or 'title "' */
static void grab_line_title (char *targ, const char *src)
{
char *fmt;
if (*src == '\'') {
fmt = "%79[^']'";
} else {
fmt = "%79[^\"]\"";
}
sscanf(src + 1, fmt, targ);
}
static void grab_line_rgb (char *targ, const char *src)
{
if (*src == '"') {
sscanf(src + 1, "%7s", targ);
}
}
/* Examples:
using 1:2
using 1:n:m:p
using 1:xtic(...)
using 1:2:xtic(""):ytic("")
using 1:($2+x)
we need to extract the data column numbers, and if we
find special stuff inlinerecord the 'using' string as
a whole
*/
static int process_using_spec (const char **ps,
GPT_SPEC *spec,
int i)
{
GPT_LINE *line = &spec->lines[i];
const char *f, *p, *s = *ps;
int *cols = NULL;
int inparen = 0;
int anyparen = 0;
int ticspecs = 0;
int n_uniq = 0;
int k, n = 0;
int err = 0;
if (isdigit(*s)) {
k = atoi(s);
gretl_list_append_term(&cols, k);
n_uniq++;
}
p = s;
while (*s) {
if (*s == '(') {
anyparen = 1;
inparen++;
} else if (*s == ')') {
inparen--;
} else if (!inparen) {
if (*s == ':') {
f = s + 1;
if (*f == 'x' || *f == 'y') {
ticspecs++;
} else {
k = (*f == '$')? atoi(f + 1) : atoi(f);
if (k > 0) {
if (!in_gretl_list(cols, k)) {
n_uniq++;
}
gretl_list_append_term(&cols, k);
}
}
} else if (isspace(*s) || *s == ',') {
break;
}
} else if (*s == '$' && isdigit(*(s+1))) {
/* inparen: a tic specification may make reference
to a data column */
k = atoi(s + 1);
if (k > 0) {
if (!in_gretl_list(cols, k)) {
n_uniq++;
}
gretl_list_append_term(&cols, k);
}
}
s++;
n++;
}
/* pass back pointer to remainder of line, if any */
*ps = s;
line->style = GP_STYLE_AUTO;
if (n > 0 && (anyparen || ticspecs > 0)) {
/* record the 'using' string as is */
char *ustr = gretl_strndup(p, n);
#if GPDEBUG
fprintf(stderr, "saving ustr = '%s'\n", ustr);
#endif
line->ustr = ustr;
}
if (cols != NULL) {
line->ncols = n_uniq;
if (cols[0] == 5 && n_uniq == 2) {
/* boxplot special */
line->flags |= GP_LINE_BOXDATA;
}
/* cumulate total data columns */
if (spec->datacols > 0 && in_gretl_list(cols, 1)) {
/* col 1 should already be counted? */
spec->datacols += line->ncols - 1;
} else {
spec->datacols += line->ncols;
}
#if GPDEBUG
fprintf(stderr, "number of unique columns %d\n", n_uniq);
printlist(cols, "cols list");
fprintf(stderr, "spec->datacols now = %d\n", spec->datacols);
#endif
if (line->ustr == NULL) {
line->mcols = cols;
} else {
free(cols);
}
}
return err;
}
/* parse the "using..." portion of plot specification for a
given plot line: full form is like:
using XX axes XX title XX w XX lt XX lw XX
*/
static int parse_gp_line_line (const char *s, GPT_SPEC *spec,
int auto_linewidth)
{
GPT_LINE *line;
char tmp[16];
const char *p;
int i, err;
#if GPDEBUG
fprintf(stderr, "parse_gp_line_line, starting\n");
#endif
err = plotspec_add_line(spec);
if (err) {
return err;
}
i = spec->n_lines - 1;
line = &spec->lines[i];
if (!strncmp(s, "plot ", 5)) {
s += 5;
}
s += strspn(s, " ");
if ((p = strstr(s, " using "))) {
/* data column spec */
p += 7;
err = process_using_spec(&p, spec, i);
s = p; /* remainder of line */
} else if (*s == '\'' || *s == '"') {
/* name of data file, without 'using': implicitly
using cols 1 and 2
*/
if (*(s+1) != '-') {
fprintf(stderr, "plotting datafile, not supported\n");
} else {
line->ncols = 2;
spec->datacols += 2;
}
} else {
/* absence of "using" should mean that the line plots
a formula, not a set of data columns
*/
/* get the formula: it runs up to "title" or "notitle" */
p = strstr(s, " title");
if (p == NULL) {
p = strstr(s, " notitle");
}
if (p != NULL) {
strncat(line->formula, s, p - s);
if (i == 1 && spec->flags & GPT_AUTO_FIT) {
grab_fit_coeffs(spec, line->formula);
}
} else {
/* title must be implicit? */
char fmt[8];
sprintf(fmt, "%%%ds", GP_MAXFORMULA - 1);
sscanf(s, fmt, line->formula);
strcpy(fmt, "%79s");
sscanf(s, fmt, line->title);
}
}
if (strstr(s, "axes x1y2")) {
line->yaxis = 2;
}
if ((p = strstr(s, " title "))) {
grab_line_title(line->title, p + 7);
}
if ((p = strstr(s, " w "))) {
sscanf(p + 3, "%15[^, ]", tmp);
line->style = gp_style_index_from_name(tmp);
}
if ((p = strstr(s, " lt "))) {
sscanf(p + 4, "%d", &line->type);
} else if ((p = strstr(s, " lc rgb "))) {
grab_line_rgb(line->rgb, p + 8);
}
if ((p = strstr(s, " ps "))) {
sscanf(p + 4, "%f", &line->pscale);
}
if ((p = strstr(s, " pt "))) {
sscanf(p + 4, "%d", &line->ptype);
}
if ((p = strstr(s, " dt "))) {
sscanf(p + 4, "%d", &line->dtype);
}
if (!auto_linewidth && (p = strstr(s, " lw "))) {
sscanf(p + 4, "%f", &line->width);
}
if ((p = strstr(s, " whiskerbars "))) {
double ww;
sscanf(p + 13, "%lf", &ww);
line->whiskwidth = (float) ww;
}
if (line->ncols == 0 && line->formula[0] == '\0') {
/* got neither data column spec nor formula */
err = 1;
}
#if GPDEBUG
fprintf(stderr, "parse_gp_line_line, returning %d\n", err);
#endif
return err;
}
/* We got a special comment supposedly indicating the name and ID
number of the X or Y variable in an OLS fitted line. Here we check
this info for validity: @vname should be the name of a bona fide
series variable, and its ID number should match the given @v. We
return 1 if this works out OK, otherwise 0.
*/
static int plot_ols_var_ok (const char *vname, int v)
{
int vcheck = current_series_index(dataset, vname);
return vcheck == v;
}
static void maybe_set_add_fit_ok (GPT_SPEC *spec)
{
if (spec->n_lines == 2 && spec->fit != PLOT_FIT_NONE) {
; /* already OK */
} else if (spec->data != NULL &&
spec->code == PLOT_REGULAR &&
spec->n_lines == 1 &&
spec->lines[0].ncols == 2) {
if (spec->flags & GPT_TS) {
spec->fit = dataset_is_time_series(dataset) ?
PLOT_FIT_NONE : PLOT_FIT_NA;
} else {
spec->fit = PLOT_FIT_NONE;
}
} else {
spec->fit = PLOT_FIT_NA;
}
}
static int plot_get_data_and_markers (GPT_SPEC *spec,
const char *buf,
int *do_markers,
long barpos,
long datapos)
{
int err = 0;
if (spec->nobs == 0 || spec->datacols == 0) {
/* nothing to be done */
return 0;
}
#if GPDEBUG
fprintf(stderr, "plot_get_data, allocating: nobs=%d, datacols=%d\n",
spec->nobs, spec->datacols);
#endif
/* allocate for the plot data... */
spec->data = gretl_matrix_alloc(spec->nobs, spec->datacols);
if (spec->data == NULL) {
err = E_ALLOC;
}
/* and markers if any */
if (!err && *do_markers) {
err = plotspec_allocate_markers(spec);
}
/* and time-series bars, if any */
if (!err && spec->nbars > 0) {
err = plotspec_allocate_bars(spec);
}
/* read the data (and perhaps markers) from the plot file */
if (!err) {
if (spec->heredata) {
err = get_gpt_heredata(spec, do_markers,
barpos, datapos, buf);
} else {
err = get_gpt_data(spec, do_markers, buf);
}
} else {
gretl_matrix_free(spec->data);
spec->data = NULL;
}
if (spec->markers != NULL && *do_markers == 0) {
/* something must have gone wrong: clean up */
plotspec_destroy_markers(spec);
}
#if GPDEBUG
fprintf(stderr, "plot_get_data_and_markers:\n"
" spec->data = %p, spec->markers = %p, spec->n_markers = %d, err = %d\n",
spec->data, (void *) spec->markers, spec->n_markers, err);
#endif
return err;
}
/* Parse special comment to get the 0-based index numbers of
any user-defined lines that have been added to the plot.
The comment looks like:
# %d user-defined lines: %d %d ...
*/
static int *get_user_lines_list (const char *s)
{
int *list = NULL;
char id[6];
int i, n = 0;
sscanf(s, "# %d", &n);
if (n > 0) {
s = strchr(s, ':');
if (s != NULL) {
list = gretl_list_new(n);
if (list != NULL) {
s++;
for (i=0; i < n && *s != '\0'; i++) {
s++;
if (sscanf(s, "%5[^ ]", id)) {
list[i+1] = atoi(id);
s += strlen(id);
}
}
}
}
}
return list;
}
static FitType recognize_fit_string (const char *s)
{
if (strstr(s, "OLS")) {
return PLOT_FIT_OLS;
} else if (strstr(s, "quadratic")) {
return PLOT_FIT_QUADRATIC;
} else if (strstr(s, "cubic")) {
return PLOT_FIT_CUBIC;
} else if (strstr(s, "inverse")) {
return PLOT_FIT_INVERSE;
} else if (strstr(s, "loess")) {
return PLOT_FIT_LOESS;
} else if (strstr(s, "semilog")) {
return PLOT_FIT_LOGLIN;
} else if (strstr(s, "linlog")) {
return PLOT_FIT_LINLOG;
} else {
return PLOT_FIT_NONE;
}
}
static void check_for_plot_size (GPT_SPEC *spec, gchar *buf)
{
char line[128];
int i = 0;
while (bufgets(line, sizeof line, buf) && i < 6) {
if (!strncmp(line, "# multiple ", 11)) {
if (strstr(line, "extra-large")) {
spec->flags |= GPT_XXL;
break;
} else if (strstr(line, "large")) {
spec->flags |= GPT_XL;
break;
}
} else if (strstr(line, "extra-wide")) {
spec->flags |= GPT_XW;
break;
}
i++;
}
}
static void check_for_plot_time_data (GPT_SPEC *spec, gchar *buf)
{
char line[128];
while (bufgets(line, sizeof line, buf)) {
if (!strncmp(line, "set timefmt \"%s\"", 16)) {
spec->flags |= GPT_TIMEFMT;
} else if (*line == '#' && strstr(line, "letterbox")) {
spec->flags |= GPT_LETTERBOX;
} else if (!strncmp(line, "plot", 4)) {
break;
}
}
}
static void linestyle_init (linestyle *ls)
{
ls->lc[0] = '\0';
ls->lw = 0;
ls->dt = 0;
ls->pt = 0;
}
static int push_z_row (gretl_matrix *z, int i, int n, char *line)
{
char *p = line;
double x;
int j, err = 0;
errno = 0;
for (j=0; j<n && *p; j++) {
if (*p == ' ') {
p++;
}
if (*p == '?') {
x = NADBL;
p += 2;
} else if (!strncmp(p, "NaN", 3)) {
x = NADBL;
p += 4;
} else {
x = strtod(p, &p);
if (errno) {
err = 1;
break;
}
}
gretl_matrix_set(z, i, j, x);
}
return err;
}
static void get_heatmap_matrix (GPT_SPEC *spec, gchar *buf,
char *line, size_t len,
long datapos)
{
gretl_matrix *z;
int n = spec->nobs;
int i, err = 0;
z = gretl_matrix_alloc(n, n);
if (z == NULL) {
return;
}
bufseek(buf, datapos);
gretl_push_c_numeric_locale();
for (i=0; i<n && !err; i++) {
bufgets(line, len, buf);
err = push_z_row(z, i, n, line);
}
gretl_pop_c_numeric_locale();
if (err) {
gretl_matrix_free(z);
} else {
spec->auxdata = z;
}
}
/* Here we're seeing if we should "promote" literal plot
lines to supplement or override "set" variables that
are recognized by gretl and form part of @spec. If we
don't do this the GUI plot editor may be broken.
*/
static void maybe_promote_literal_lines (GPT_SPEC *spec,
linestyle *styles)
{
const char *line;
int *rmlines = NULL;
int unhandled;
int i, err = 0;
for (i=0; i<spec->n_literal; i++) {
line = spec->literal[i];
if (!strncmp(line, "set ", 4)) {
unhandled = 0;
gretl_push_c_numeric_locale();
err = parse_gp_set_line(spec, line, styles, &unhandled);
gretl_pop_c_numeric_locale();
if (!err && !unhandled) {
gretl_list_append_term(&rmlines, i);
}
} else if (!strncmp(line, "unset ", 6)) {
err = parse_gp_unset_line(spec, line);
if (!err) {
gretl_list_append_term(&rmlines, i);
}
}
}
if (rmlines != NULL) {
/* at least one "literal" line has been promoted */
if (rmlines[0] == spec->n_literal) {
strings_array_free(spec->literal, spec->n_literal);
spec->literal = NULL;
spec->n_literal = 0;
} else {
char **S = spec->literal;
int ns, orig_ns = spec->n_literal;
ns = orig_ns - rmlines[0];
spec->literal = strings_array_new(ns);
if (spec->literal == NULL) {
/* unlikely, but... */
spec->literal = S;
} else {
/* reduced version of the "literal" array */
int j = 0;
for (i=0; i<orig_ns; i++) {
if (in_gretl_list(rmlines, i)) {
free(S[i]);
} else {
spec->literal[j++] = S[i];
}
S[i] = NULL;
}
strings_array_free(S, orig_ns);
spec->n_literal = ns;
}
}
}
}
static void plot_get_ols_info (const char *line,
int *reglist)
{
char fmt[16], vname[VNAMELEN];
int v;
sprintf(fmt, "'%%%d[^\\']' (%%d)", VNAMELEN - 1);
if (sscanf(line + 6, fmt, vname, &v) == 2) {
if (line[2] == 'X') {
if (plot_ols_var_ok(vname, v)) {
reglist[3] = v;
}
} else {
/* 'Y' */
if (reglist[3] > 0 && plot_ols_var_ok(vname, v)) {
reglist[0] = 3;
reglist[1] = v;
}
}
}
}
#define plot_needs_obs(c) (c != PLOT_ELLIPSE && \
c != PLOT_PROB_DIST && \
c != PLOT_CURVE && \
c != PLOT_USER)
/* Read plotspec struct from gnuplot command file. This is not a
general parser for gnuplot files; it is designed specifically for
files auto-generated by gretl and even then there are some sorts
of plot that are not fully handled.
*/
static int read_plotspec_from_file (GPT_SPEC *spec, int *plot_pd)
{
linestyle styles[MAX_STYLES];
int do_markers = 0;
int auto_linewidth = 0;
int reglist[4] = {0};
int *uservec = NULL;
long barpos = 0;
long datapos = 0;
char gpline[MAXLEN];
gchar *buf = NULL;
char *got = NULL;
#if HANDLE_HEREDATA
char *eod = NULL;
#endif
int ignore = 0;
int i, done;
int err = 0;
#if GPDEBUG
fprintf(stderr, "read_plotspec_from_file: spec=%p\n",
(void *) spec);
#endif
/* check: are we already done? */
if (PLOTSPEC_DETAILS_IN_MEMORY(spec)) {
#if GPDEBUG
fprintf(stderr, " info already in memory, returning 0\n");
#endif
return 0;
}
/* get the content of the plot file */
err = gretl_file_get_contents(spec->fname, &buf, NULL);
if (err) {
return err;
}
spec->datacols = 0;
bufgets_init(buf);
/* get the number of data-points, plot type, and check for
observation markers and time-series bars */
get_plot_nobs(spec, buf, &do_markers, &barpos, &datapos);
if (spec->nobs == 0 && plot_needs_obs(spec->code)) {
/* failed reading plot data */
#if GPDEBUG
fprintf(stderr, " got spec->nobs = 0\n");
#endif
err = 1;
goto bailout;
}
if (do_markers && spec->nobs > MAX_MARKERS) {
do_markers = 0;
}
if (cant_edit(spec->code)) {
fprintf(stderr, "read_plotspec_from_file: plot is not editable\n");
if (maybe_big_multiplot(spec->code)) {
buf_rewind(buf);
check_for_plot_size(spec, buf);
} else if (spec->code == PLOT_BAND) {
buf_rewind(buf);
check_for_plot_time_data(spec, buf);
} else if (spec->code == PLOT_HEATMAP) {
buf_rewind(buf);
get_heatmap_matrix(spec, buf, gpline, sizeof gpline, datapos);
}
goto bailout;
} else {
buf_rewind(buf);
check_for_plot_size(spec, buf);
}
for (i=0; i<MAX_STYLES; i++) {
linestyle_init(&styles[i]);
}
buf_rewind(buf);
/* get the preamble and "set" lines */
while ((got = bufgets(gpline, sizeof gpline, buf)) && !err) {
#if GPDEBUG
tailstrip(gpline);
fprintf(stderr, "gpline: '%s'\n", gpline);
#endif
if (ignore) {
if (!strncmp(gpline, "# end inline", 12)) {
ignore = 0;
}
} else if (!strncmp(gpline, "# start inline", 14)) {
ignore = 1;
} else if (!strncmp(gpline, "# timeseries", 12)) {
if (sscanf(gpline, "# timeseries %d", &spec->pd)) {
*plot_pd = spec->pd;
}
spec->flags |= GPT_TS;
if (strstr(gpline, "letterbox")) {
spec->flags |= GPT_LETTERBOX;
}
} else if (!strncmp(gpline, "# multiple timeseries", 21)) {
if (sscanf(gpline, "# multiple timeseries %d", &spec->pd)) {
*plot_pd = spec->pd;
}
spec->flags |= GPT_TS;
} else if (!strncmp(gpline, "# scale = ", 10)) {
double x = dot_atof(gpline + 10);
if (x >= 0.5 && x <= 2.0) {
spec->scale = x;
}
} else if (!strncmp(gpline, "# auto linewidth", 16)) {
auto_linewidth = 1;
} else if (!strncmp(gpline, "# fontspec: ", 12)) {
free(spec->fontstr);
spec->fontstr = gretl_strdup(gpline + 12);
tailstrip(spec->fontstr);
} else if (!strncmp(gpline, "# boxplots", 10)) {
; /* OK */
} else if (!strncmp(gpline, "# X = ", 6) ||
!strncmp(gpline, "# Y = ", 6)) {
plot_get_ols_info(gpline, reglist);
} else if (sscanf(gpline, "# literal lines = %d", &spec->n_literal)) {
spec->literal = strings_array_new(spec->n_literal);
if (spec->literal == NULL) {
err = E_ALLOC;
} else {
for (i=0; i<spec->n_literal; i++) {
if (!bufgets(gpline, MAXLEN - 1, buf)) {
errbox(_("Plot file is corrupted"));
} else {
top_n_tail(gpline, 0, NULL);
spec->literal[i] = g_strdup(gpline);
}
}
}
} else if (!strncmp(gpline, "# start literal lines", 21) &&
spec->literal == NULL) {
for (i=0; ; i++) {
if (!bufgets(gpline, MAXLEN - 1, buf)) {
errbox(_("Plot file is corrupted"));
} else if (!strncmp(gpline, "# end literal lines", 19)) {
break;
} else {
top_n_tail(gpline, 0, NULL);
strings_array_add(&spec->literal, &spec->n_literal,
gpline);
}
}
} else if (strstr(gpline, "automatic fit")) {
spec->flags |= GPT_AUTO_FIT;
spec->fit = recognize_fit_string(gpline);
} else if (strstr(gpline, "user-defined")) {
uservec = get_user_lines_list(gpline);
} else if (strstr(gpline, "printing data labels")) {
spec->flags |= GPT_PRINT_MARKERS;
} else if (!strncmp(gpline, "# ", 2)) {
; /* ignore unknown comment lines */
} else if (!strncmp(gpline, "set ", 4)) {
gretl_push_c_numeric_locale();
err = parse_gp_set_line(spec, gpline, styles, NULL);
gretl_pop_c_numeric_locale();
} else if (!strncmp(gpline, "unset ", 6)) {
err = parse_gp_unset_line(spec, gpline);
} else {
/* done reading "set" lines? */
break;
}
}
if (!err && got == NULL) {
err = E_DATA;
}
if (err) {
goto bailout;
}
if ((spec->flags & GPT_TIMEFMT) && *spec->timefmt == '\0') {
fprintf(stderr, "got 'xdata time' but no timefmt\n");
err = E_DATA;
goto bailout;
}
for (i=0; i<4; i++) {
if (spec->titles[i][0] != '\0') {
gretl_delchar('"', spec->titles[i]);
}
}
#if HANDLE_HEREDATA
if (line_starts_heredata(gpline, &eod)) {
while (bufgets(gpline, MAXLEN - 1, buf) != NULL) {
if (!strncmp(gpline, eod, strlen(eod))) {
bufgets(gpline, MAXLEN - 1, buf);
break;
}
}
free(eod);
}
#endif
/* then get the "plot" lines */
if (strncmp(gpline, "plot ", 5) ||
(strlen(gpline) < 10 && bufgets(gpline, MAXLEN - 1, buf) == NULL)) {
err = unhandled_gp_line_error(gpline);
goto bailout;
}
/* if there are any time-series bars, skip past them */
for (i=0; i<spec->nbars; i++) {
if ((got = bufgets(gpline, MAXLEN - 1, buf)) == NULL) {
err = E_DATA;
}
}
done = 0;
while (!err) {
top_n_tail(gpline, 0, NULL);
if (!chop_comma(gpline)) {
/* line did not end with comma -> no continuation of
the plot command */
done = 1;
}
err = parse_gp_line_line(gpline, spec, auto_linewidth);
if (err || done || (got = bufgets(gpline, MAXLEN - 1, buf)) == NULL) {
break;
}
}
if (!err && got == NULL) {
err = E_DATA;
}
if (!err && spec->literal != NULL) {
maybe_promote_literal_lines(spec, styles);
}
/* transcribe styles info into lines for use in the
GUI plot-editor */
for (i=0; i<spec->n_lines && !err; i++) {
GPT_LINE *line = &spec->lines[i];
int idx = line->type; /* this will be 1-based */
if (idx == LT_AUTO) {
/* automatic sequential line-types */
idx = i + 1;
}
if (uservec != NULL && in_gretl_list(uservec, i)) {
line->flags |= GP_LINE_USER;
}
if (idx > 0 && idx < MAX_STYLES) {
int j = idx - 1;
if (line->rgb[0] == '\0') {
strcpy(line->rgb, styles[j].lc);
}
if (line->width == 1 && styles[j].lw > 0) {
line->width = styles[j].lw;
}
if (line->dtype == 0 && styles[j].dt > 0) {
line->dtype = styles[j].dt;
}
if (line->ptype == 0 && styles[j].pt > 0) {
line->ptype = styles[j].pt;
}
}
if (spec->auxdata != NULL && i == spec->n_lines - 1) {
/* the last "line" doesn't use the regular
data mechanism */
line->flags = GP_LINE_AUXDATA;
continue;
}
}
if (!err) {
err = plot_get_data_and_markers(spec, buf, &do_markers,
barpos, datapos);
}
if (!err && reglist[0] > 0) {
spec->reglist = gretl_list_copy(reglist);
}
if (!err && spec->fit == PLOT_FIT_NONE) {
maybe_set_add_fit_ok(spec);
}
if (!err && spec->code == PLOT_ROOTS && spec->n_literal == 8) {
/* backward compatibility */
fprintf(stderr, "old roots plot: fixing\n");
fix_old_roots_plot(spec);
}
bailout:
if (err) {
fprintf(stderr, "read_plotspec_from_file: err = %d\n", err);
}
bufgets_finalize(buf);
g_free(buf);
free(uservec);
return err;
}
#define has_log_axis(s) (s->logbase[0] != 0 || s->logbase[1] != 0)
static int get_data_xy (png_plot *plot, int x, int y,
double *data_x, double *data_y)
{
double xmin, xmax;
double ymin, ymax;
double dx = NADBL;
double dy = NADBL;
int ok = 1;
if (plot_is_zoomed(plot)) {
xmin = plot->zoom_xmin;
xmax = plot->zoom_xmax;
ymin = plot->zoom_ymin;
ymax = plot->zoom_ymax;
} else {
xmin = plot->xmin;
xmax = plot->xmax;
ymin = plot->ymin;
ymax = plot->ymax;
}
#if POINTS_DEBUG
if (plot_doing_position(plot)) {
fprintf(stderr, "get_data_xy:\n"
" plot->xmin=%g, plot->xmax=%g, plot->ymin=%g, plot->ymax=%g\n",
plot->xmin, plot->xmax, plot->ymin, plot->ymax);
}
#endif
if (xmin == 0.0 && xmax == 0.0) {
fprintf(stderr, "get_data_xy: unknown x range\n");
} else {
dx = xmin + ((double) x - plot->pixel_xmin) /
(plot->pixel_xmax - plot->pixel_xmin) * (xmax - xmin);
}
if (!na(dx)) {
if (ymin == 0.0 && ymax == 0.0) {
fprintf(stderr, "get_data_xy: unknown y range\n");
} else {
dy = ymax - ((double) y - plot->pixel_ymin) /
(plot->pixel_ymax - plot->pixel_ymin) * (ymax - ymin);
}
}
if (na(dx) || na(dx)) {
ok = 0;
} else if (has_log_axis(plot->spec)) {
double base, dprop, lr;
base = plot->spec->logbase[0];
if (base != 0) {
if (xmin > 0) {
dprop = (dx - xmin) / (xmax - xmin);
lr = log(xmax / xmin) / log(base);
dx = pow(base, dprop * lr);
} else {
dx = NADBL;
ok = 0;
}
}
base = plot->spec->logbase[1];
if (base != 0) {
if (ymin > 0) {
dprop = (dy - ymin) / (ymax - ymin);
lr = log(ymax / ymin) / log(base);
dy = pow(base, dprop * lr);
} else {
dy = NADBL;
ok = 0;
}
}
} else if (plot_is_polar(plot)) {
double px = atan2(dy, dx);
double py = sqrt(dx * dx + dy * dy);
dx = px;
dy = py;
}
*data_x = dx;
*data_y = dy;
return ok;
}
static void x_to_date (double x, int pd, char *str)
{
int yr = (int) x;
double t, frac = 1.0 / pd;
int subper = (int) ((x - yr + frac) * pd);
static int decpoint;
if (decpoint == 0) {
decpoint = get_local_decpoint();
}
t = yr + subper / ((pd < 10)? 10.0 : 100.0);
sprintf(str, "%.*f", (pd < 10)? 1 : 2, t);
gretl_charsub(str, decpoint, ':');
}
static void redraw_plot_rectangle (png_plot *plot, GdkRectangle *r)
{
if (r != NULL) {
gdk_window_invalidate_rect(plot->window, r, FALSE);
} else {
GdkRectangle rt = {
0, 0, plot->pixel_width, plot->pixel_height
};
gdk_window_invalidate_rect(plot->window, &rt, FALSE);
}
}
static void make_cairo_rectangle (png_plot *plot,
GdkRectangle *r)
{
cairo_move_to(plot->cr, r->x, r->y);
cairo_line_to(plot->cr, r->x, r->y + r->height);
cairo_line_to(plot->cr, r->x + r->width, r->y + r->height);
cairo_line_to(plot->cr, r->x + r->width, r->y);
cairo_close_path(plot->cr);
cairo_stroke(plot->cr);
}
# if GTK_MAJOR_VERSION == 2
static void copy_state_to_pixbuf (png_plot *plot)
{
if (plot->savebuf != NULL) {
g_object_unref(plot->savebuf);
}
plot->savebuf =
gdk_pixbuf_get_from_drawable(NULL,
plot->pixmap,
NULL,
0, 0, 0, 0,
plot->pixel_width,
plot->pixel_height);
}
# endif
/* Note that the screen coordinates as of the last mouse
button press are recorded in plot->screen_x0 and
plot->screen_y0. These represent the constant corner of
the box that should be drawn.
*/
static void draw_selection_box (png_plot *plot, int x, int y)
{
GdkRectangle r;
r.x = MIN(plot->screen_x0, x);
r.y = MIN(plot->screen_y0, y);
r.width = abs(x - plot->screen_x0);
r.height = abs(y - plot->screen_y0);
#if GTK_MAJOR_VERSION >= 3
plot->cr = gdk_cairo_create(plot->window);
cairo_set_source_rgba(plot->cr, 0.3, 0.3, 0.3, 0.3);
make_cairo_rectangle(plot, &r);
redraw_plot_rectangle(plot, &r);
cairo_destroy(plot->cr);
#else
plot->cr = gdk_cairo_create(plot->pixmap);
if (plot->savebuf != NULL) {
/* restore state prior to zoom start */
gdk_cairo_set_source_pixbuf(plot->cr, plot->savebuf, 0, 0);
cairo_paint(plot->cr);
} else {
copy_state_to_pixbuf(plot);
}
cairo_set_source_rgba(plot->cr, 0.3, 0.3, 0.3, 0.3);
make_cairo_rectangle(plot, &r);
redraw_plot_rectangle(plot, NULL);
cairo_destroy(plot->cr);
#endif
}
static int make_alt_label (gchar *alt, const gchar *label)
{
double x, y;
int err = 0;
gretl_push_c_numeric_locale();
if (sscanf(label, "%lf,%lf", &x, &y) != 2) {
err = 1;
}
gretl_pop_c_numeric_locale();
if (!err) {
if (get_local_decpoint() != '.') {
sprintf(alt, "%.2f %.2f", x, y);
} else {
sprintf(alt, "%.2f,%.2f", x, y);
}
}
return err;
}
#if GTK_MAJOR_VERSION >= 3
/* given a GdkPixbuf read from file, create a corresponding cairo
surface, attached to @plot as plot->cs. This will be used as
the "backing store" for re-draws of the plot.
*/
static int copy_pixbuf_to_surface (png_plot *plot,
GdkPixbuf *pixbuf)
{
cairo_format_t format;
guchar *p_data, *s_data;
int nc, ps, ss;
int width, height;
int i, j;
width = gdk_pixbuf_get_width(pixbuf);
height = gdk_pixbuf_get_height(pixbuf);
nc = gdk_pixbuf_get_n_channels(pixbuf);
ps = gdk_pixbuf_get_rowstride(pixbuf);
format = (nc == 3)? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32;
if (plot->cs != NULL) {
cairo_surface_destroy(plot->cs);
}
plot->cs = cairo_image_surface_create(format, width, height);
if (plot->cs == NULL ||
cairo_surface_status(plot->cs) != CAIRO_STATUS_SUCCESS) {
fprintf(stderr, "copy_pixbuf_to_surface: failed\n");
return 1;
}
ss = cairo_image_surface_get_stride(plot->cs);
p_data = gdk_pixbuf_get_pixels(pixbuf);
s_data = cairo_image_surface_get_data(plot->cs);
for (j=0; j<height; j++) {
guchar *p_iter = p_data + j * ps,
*s_iter = s_data + j * ss;
for (i=0; i<width; i++) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
/* BGR(A) -> RGB(A) */
s_iter[0] = p_iter[2];
s_iter[1] = p_iter[1];
s_iter[2] = p_iter[0];
if (nc == 4) {
s_iter[3] = p_iter[3];
}
#else
/* (A)RGB -> RGB(A) */
if (nc == 4) {
s_iter[0] = p_iter[3];
}
s_iter[1] = p_iter[0];
s_iter[2] = p_iter[1];
s_iter[3] = p_iter[2];
#endif
p_iter += nc;
s_iter += 4;
}
}
return 0;
}
#endif
/* implements "brushing-in" of data-point labels with the mouse */
static void
write_label_to_plot (png_plot *plot, int i, gint x, gint y)
{
GdkRectangle r = {x, y, 0, 0};
const gchar *label = plot->spec->markers[i];
PangoContext *context;
PangoLayout *pl;
if (plot_is_roots(plot)) {
gchar alt_label[12];
if (make_alt_label(alt_label, label)) {
return;
}
label = alt_label;
}
context = gtk_widget_get_pango_context(plot->shell);
pl = pango_layout_new(context);
pango_layout_set_text(pl, label, -1);
/* add the label, then show the modified image */
#if GTK_MAJOR_VERSION >= 3
plot->cr = cairo_create(plot->cs);
#else
plot->cr = gdk_cairo_create(plot->pixmap);
#endif
cairo_move_to(plot->cr, x, y);
pango_cairo_show_layout(plot->cr, pl);
cairo_fill(plot->cr);
pango_layout_get_pixel_size(pl, &r.width, &r.height);
redraw_plot_rectangle(plot, &r);
cairo_destroy(plot->cr);
/* trash the pango layout */
g_object_unref(G_OBJECT(pl));
/* record that a label is shown */
plot->format |= PLOT_MARKERS_UP;
}
#define TOLDIST 0.01
static gint identify_point (png_plot *plot, int pixel_x, int pixel_y,
double x, double y)
{
const double *data_x = NULL;
const double *data_y = NULL;
double xrange, yrange;
double xdiff, ydiff;
double dist, mindist = DBL_MAX;
int best_match = -1;
int done = 0, bank = 1;
int t;
#if GPDEBUG > 2
fprintf(stderr, "identify_point: pixel_x = %d (x=%g), pixel_y = %d (y=%g)\n",
pixel_x, x, pixel_y, y);
#endif
if (plot->err || plot->spec->markers == NULL) {
return TRUE;
}
/* need array to keep track of which points are labeled */
if (plot->spec->labeled == NULL) {
plot->spec->labeled = calloc(plot->spec->nobs, 1);
if (plot->spec->labeled == NULL) {
return TRUE;
}
}
if (plot_is_zoomed(plot)) {
xrange = plot->zoom_xmax - plot->zoom_xmin;
yrange = plot->zoom_ymax - plot->zoom_ymin;
} else {
xrange = plot->xmax - plot->xmin;
yrange = plot->ymax - plot->ymin;
}
data_x = plot->spec->data->val;
data_y = data_x + plot->spec->nobs;
if (plot_has_y2axis(plot)) {
/* use first y-var that's on y1 axis, if any */
int i, got_y = 0;
for (i=0; i<plot->spec->n_lines; i++) {
if (plot->spec->lines[i].yaxis == 1) {
got_y = 1;
break;
}
if (plot->spec->lines[i].ncols > 0) {
data_y += (plot->spec->lines[i].ncols - 1) * plot->spec->nobs;
}
}
if (!got_y) {
data_y = NULL;
return TRUE;
}
}
repeat:
/* find the best-matching data point */
for (t=0; t<plot->spec->nobs; t++) {
if (na(data_x[t]) || na(data_y[t])) {
continue;
}
xdiff = fabs(data_x[t] - x);
ydiff = fabs(data_y[t] - y);
dist = sqrt(xdiff * xdiff + ydiff * ydiff);
#if GPDEBUG > 4
fprintf(stderr, " obs %d: x=%g, y=%g, dist=%g\n",
t, data_x[t], data_y[t], dist);
#endif
if (dist < mindist) {
mindist = dist;
best_match = t;
bank = done ? 2 : 1;
}
}
if (plot->spec->code == PLOT_FACTORIZED && !done) {
/* check the second "bank" of data also */
data_y += plot->spec->nobs;
done = 1;
goto repeat;
}
t = best_match;
data_y = data_x + bank * plot->spec->nobs;
/* if the closest point is already labeled, get out */
if (t >= 0 && plot->spec->labeled[t]) {
return TRUE;
}
#if GPDEBUG > 2
if (t >= 0) {
fprintf(stderr, " best_match=%d, with data_x[%d]=%g, data_y[%d]=%g\n",
t, t, data_x[t], t, data_y[t]);
} else {
fprintf(stderr, " no 'best match' for %g, %g\n", x, y);
}
#endif
/* if the match is good enough, show the label */
if (best_match >= 0) {
xdiff = fabs(data_x[t] - x);
ydiff = fabs(data_y[t] - y);
if (xdiff < TOLDIST * xrange && ydiff < TOLDIST * yrange) {
write_label_to_plot(plot, t, pixel_x, pixel_y);
/* flag the point as labeled already */
plot->spec->labeled[t] = 1;
}
}
return TRUE;
}
static inline int use_integer_format (int xint, double xdata)
{
return (xint && fabs(xdata) < 1.0e7);
}
static void heatmap_show_z (png_plot *plot, double x, double y,
gchar *label)
{
gretl_matrix *z = plot->spec->auxdata;
double zij;
int i = nearbyint(y);
int j = nearbyint(x);
int n = z->rows;
if (i >= 0 && i < n && j >= 0 && j < n) {
zij = gretl_matrix_get(z, i, j);
if (isnan(zij)) {
gtk_label_set_text(GTK_LABEL(plot->cursor_label), "");
} else {
sprintf(label, "r = %.3f", zij);
gtk_label_set_text(GTK_LABEL(plot->cursor_label), label);
}
}
}
static gint
plot_motion_callback (GtkWidget *widget, GdkEventMotion *event, png_plot *plot)
{
GdkModifierType state;
gchar label[48], label_y[24];
const char *xfmt = NULL;
const char *yfmt = NULL;
int do_label;
int x, y;
if (plot->err) {
return TRUE;
}
if (event->is_hint) {
gdk_window_get_pointer(event->window, &x, &y, &state);
} else {
x = event->x;
y = event->y;
state = event->state;
}
if (plot->spec->xfmt[0] != '\0') {
xfmt = plot->spec->xfmt;
}
if (plot->spec->yfmt[0] != '\0') {
yfmt = plot->spec->yfmt;
}
*label = '\0';
do_label = plot_show_cursor_label(plot);
if (x > plot->pixel_xmin && x < plot->pixel_xmax &&
y > plot->pixel_ymin && y < plot->pixel_ymax) {
double data_x, data_y;
get_data_xy(plot, x, y, &data_x, &data_y);
if (na(data_x)) {
return TRUE;
}
if (plot->spec->code == PLOT_HEATMAP) {
if (plot->spec->auxdata != NULL) {
heatmap_show_z(plot, data_x, data_y, label);
}
return TRUE;
}
if (!cant_do_labels(plot) && !labels_frozen(plot) &&
!plot_is_zooming(plot) && !na(data_y)) {
identify_point(plot, x, y, data_x, data_y);
}
if (do_label) {
if (plot->pd == 4 || plot->pd == 12) {
x_to_date(data_x, plot->pd, label);
} else if (plot->spec->flags & GPT_TIMEFMT) {
date_from_gnuplot_time(label, sizeof label,
"%Y-%m-%d", data_x);
} else if (xfmt != NULL) {
sprintf(label, xfmt, data_x);
} else if (use_integer_format(plot->xint, data_x)) {
sprintf(label, "%7d", (int) data_x);
} else {
sprintf(label, "%#7.4g", data_x);
}
}
if (do_label && !na(data_y)) {
if (plot_has_png_coords(plot)) {
if (yfmt != NULL) {
sprintf(label_y, yfmt, data_y);
} else if (use_integer_format(plot->yint, data_y)) {
sprintf(label_y, " %-7d", (int) data_y);
} else {
sprintf(label_y, " %#-7.4g", data_y);
}
} else if (use_integer_format(plot->yint, data_y)) {
sprintf(label_y, " %-7d", (int) data_y);
} else {
sprintf(label_y, " %#-6.3g", data_y);
}
if (strlen(label) + strlen(label_y) < sizeof label) {
strcat(label, label_y);
} else {
fprintf(stderr, "label='%s', label_y='%s'\n", label, label_y);
strcpy(label, label_y);
}
}
if (plot_is_zooming(plot) && (state & GDK_BUTTON1_MASK)) {
draw_selection_box(plot, x, y);
}
}
if (do_label) {
gtk_label_set_text(GTK_LABEL(plot->cursor_label), label);
}
return TRUE;
}
static void set_plot_format_flags (png_plot *plot)
{
plot->format = 0;
if (!string_is_blank(plot->spec->titles[0])) {
plot->format |= PLOT_TITLE;
}
if (!string_is_blank(plot->spec->titles[1])) {
plot->format |= PLOT_XLABEL;
}
if (!string_is_blank(plot->spec->titles[2])) {
plot->format |= PLOT_YLABEL;
}
if (!string_is_blank(plot->spec->titles[3])) {
plot->format |= PLOT_Y2LABEL;
}
if (plot->spec->flags & GPT_Y2AXIS) {
plot->format |= PLOT_Y2AXIS;
}
if (plot->spec->flags & GPT_PRINT_MARKERS) {
plot->format |= PLOT_MARKERS_UP;
}
if (plot->spec->flags & GPT_POLAR) {
plot->format |= PLOT_POLAR;
}
}
/* called from png plot popup menu, also toolbar item */
void start_editing_png_plot (png_plot *plot)
{
#if GPDEBUG
fprintf(stderr, "start_editing_png_plot: plot = %p\n", (void *) plot);
#endif
if (!PLOTSPEC_DETAILS_IN_MEMORY(plot->spec)) {
errbox(_("Couldn't access graph info"));
plot->err = 1;
return;
}
if (plot->editor != NULL) {
gtk_window_present(GTK_WINDOW(plot->editor));
} else {
plot->editor = plot_add_editor(plot);
if (plot->editor != NULL) {
g_signal_connect(G_OBJECT(plot->editor), "destroy",
G_CALLBACK(gtk_widget_destroyed),
&plot->editor);
g_signal_connect_swapped(G_OBJECT(plot->editor), "destroy",
G_CALLBACK(terminate_plot_positioning),
plot);
}
}
}
static int substitute_graph_font (char *line, const gchar *fstr)
{
char *p = strstr(line, " font ");
if (p != NULL) {
char tmp[256];
*tmp = '\0';
strncat(tmp, line, p - line + 7);
strcat(tmp, fstr); /* replacement font string */
p = strchr(p + 7, '"'); /* closing quote of original font string */
if (p != NULL) {
strcat(tmp, p);
strcpy(line, tmp);
}
}
return (p == NULL)? E_DATA : 0;
}
/* Filter the original font spec out of the plot file, substituting
the user's choice from a font selection dialog, then recreate
the on-screen PNG. We do this for plots for which we can't offer
the full-blown graph editor.
*/
void activate_plot_font_choice (png_plot *plot, const char *grfont)
{
FILE *fp = NULL;
FILE *ftmp = NULL;
char tmpname[FILENAME_MAX];
char line[256], fontspec[128];
int gotterm = 0;
int err = 0;
adjust_fontspec_string(fontspec, grfont, ADD_COMMA);
#if 0
fprintf(stderr, "font choice: grfont='%s', fontspec='%s'\n",
grfont, fontspec);
#endif
fp = gretl_fopen(plot->spec->fname, "r");
if (fp == NULL) {
file_read_errbox(plot->spec->fname);
return;
}
sprintf(tmpname, "%sgpttmp", gretl_dotdir());
ftmp = gretl_tempfile_open(tmpname);
if (ftmp == NULL) {
fclose(fp);
gui_errmsg(E_FOPEN);
return;
}
while (fgets(line, sizeof line, fp) && !err) {
if (!gotterm && strncmp(line, "set term", 8) == 0) {
err = substitute_graph_font(line, fontspec);
fputs(line, ftmp);
gotterm = 1;
} else {
fputs(line, ftmp);
}
}
fclose(fp);
fclose(ftmp);
if (err) {
gretl_remove(tmpname);
} else {
gchar *plotcmd;
err = gretl_rename(tmpname, plot->spec->fname);
if (!err) {
plotcmd = g_strdup_printf("\"%s\" \"%s\"",
gretl_gnuplot_path(),
plot->spec->fname);
err = gretl_spawn(plotcmd);
g_free(plotcmd);
}
}
if (err) {
gui_errmsg(err);
} else {
free(plot->spec->fontstr);
plot->spec->fontstr = gretl_strdup(fontspec);
render_pngfile(plot, PNG_REDISPLAY);
}
}
static const gchar *menu_item_get_text (GtkMenuItem *item)
{
GtkWidget *label = gtk_bin_get_child(GTK_BIN(item));
return gtk_label_get_text(GTK_LABEL(label));
}
static gint color_popup_activated (GtkMenuItem *item, gpointer data)
{
png_plot *plot = g_object_get_data(G_OBJECT(item), "plot");
GtkWidget *parent = data;
const gchar *item_string = NULL;
const gchar *menu_string = NULL;
item_string = menu_item_get_text(item);
menu_string = menu_item_get_text(GTK_MENU_ITEM(parent));
if (!strcmp(item_string, _("monochrome"))) {
plot->spec->flags |= GPT_MONO;
}
if (!strcmp(menu_string, _("Save as Windows metafile (EMF)..."))) {
plot->spec->termtype = GP_TERM_EMF;
file_selector_with_parent(SAVE_GNUPLOT, FSEL_DATA_MISC,
plot->spec, plot->shell);
} else if (!strcmp(menu_string, _("Copy to clipboard"))) {
#ifdef G_OS_WIN32
win32_process_graph(plot->spec, WIN32_TO_CLIPBOARD);
#else
set_plot_for_copy(plot);
#endif
}
#ifdef G_OS_WIN32
else if (!strcmp(menu_string, _("Print"))) {
win32_process_graph(plot->spec, WIN32_TO_PRINTER);
}
#endif
plot->spec->flags &= ~GPT_MONO;
return TRUE;
}
static void show_numbers_from_markers (GPT_SPEC *spec)
{
PRN *prn;
double x, y, pi2;
double mod, freq;
int dcomma = 0;
int i, err = 0;
if (bufopen(&prn)) {
return;
}
pi2 = 2.0 * M_PI;
dcomma = get_local_decpoint() != '.';
pputs(prn, _("roots (real, imaginary, modulus, frequency)"));
pputs(prn, "\n\n");
if (dcomma) {
gretl_push_c_numeric_locale();
}
for (i=0; i<spec->n_markers; i++) {
if (sscanf(spec->markers[i], "%lf,%lf", &x, &y) == 2) {
freq = gretl_matrix_get(spec->data, i, 0) / pi2;
mod = gretl_matrix_get(spec->data, i, 1);
if (dcomma) {
gretl_pop_c_numeric_locale();
pprintf(prn, "%2d: (%7.4f %7.4f %7.4f %7.4f)\n", i+1,
x, y, mod, freq);
gretl_push_c_numeric_locale();
} else {
pprintf(prn, "%2d: (%7.4f, %7.4f, %7.4f, %7.4f)\n", i+1,
x, y, mod, freq);
}
} else {
err = E_DATA;
break;
}
}
if (dcomma) {
gretl_pop_c_numeric_locale();
}
if (err) {
gui_errmsg(err);
gretl_print_destroy(prn);
} else {
gchar *title = g_strdup_printf("gretl: %s", _("roots"));
view_buffer(prn, 72, 340, title, PRINT, NULL);
g_free(title);
}
}
static void boxplot_show_summary (GPT_SPEC *spec)
{
PRN *prn = NULL;
int err;
if (bufopen(&prn)) {
return;
}
err = boxplot_numerical_summary(spec->fname, prn);
if (err) {
gui_errmsg(err);
gretl_print_destroy(prn);
} else {
view_buffer(prn, 78, 240, _("gretl: boxplot data"), PRINT, NULL);
}
}
static void add_to_session_callback (GPT_SPEC *spec)
{
char fullname[MAXLEN] = {0};
int err, type;
type = (spec->code == PLOT_BOXPLOTS)? GRETL_OBJ_PLOT :
GRETL_OBJ_GRAPH;
err = gui_add_graph_to_session(spec->fname, fullname, type);
if (!err) {
remove_png_term_from_plot(fullname, spec);
mark_plot_as_saved(spec);
}
}
static double graph_scales[] = {
0.8, 1.0, 1.1, 1.2, 1.4
};
int get_graph_scale (int i, double *s)
{
int n = G_N_ELEMENTS(graph_scales);
if (i >= 0 && i < n) {
*s = graph_scales[i];
return 1;
} else {
return 0;
}
}
static void plot_do_rescale (png_plot *plot, int mod)
{
int n = G_N_ELEMENTS(graph_scales);
FILE *fp = NULL;
if (mod == 0) {
/* reset to default */
if (plot->spec->scale == 1.0) {
/* no-op */
return;
} else {
plot->spec->scale = 1.0;
}
} else {
/* enlarge or shrink */
int i;
for (i=0; i<n; i++) {
if (plot->spec->scale == graph_scales[i]) {
break;
}
}
if (mod == 1 && i < n - 1) {
plot->spec->scale = graph_scales[i+1];
} else if (mod == -1 && i > 0) {
plot->spec->scale = graph_scales[i-1];
} else {
gdk_window_beep(plot->window);
return;
}
}
gnuplot_png_init(plot, &fp);
if (fp == NULL) {
gui_errmsg(E_FOPEN);
return;
}
gtk_widget_set_sensitive(plot->up_icon, plot->spec->scale != graph_scales[n-1]);
gtk_widget_set_sensitive(plot->down_icon, plot->spec->scale != graph_scales[0]);
set_png_output(plot->spec);
plotspec_print(plot->spec, fp);
fclose(fp);
unset_png_output(plot->spec);
repaint_png(plot, PNG_REDISPLAY);
}
static void show_all_labels (png_plot *plot)
{
FILE *fp;
if (plot->spec->labeled != NULL) {
free(plot->spec->labeled);
plot->spec->labeled = NULL;
}
plot->spec->flags |= GPT_PRINT_MARKERS;
gnuplot_png_init(plot, &fp);
if (fp == NULL) {
gui_errmsg(E_FOPEN);
return;
}
plotspec_print(plot->spec, fp);
fclose(fp);
repaint_png(plot, PNG_REDISPLAY);
plot->format |= PLOT_MARKERS_UP;
}
static void clear_labels (png_plot *plot)
{
if (plot->spec->flags & GPT_PRINT_MARKERS) {
FILE *fp;
plot->spec->flags &= ~GPT_PRINT_MARKERS;
gnuplot_png_init(plot, &fp);
if (fp == NULL) {
gui_errmsg(E_FOPEN);
return;
}
plotspec_print(plot->spec, fp);
fclose(fp);
}
repaint_png(plot, PNG_REDISPLAY);
plot->format &= ~PLOT_MARKERS_UP;
}
static void prepare_for_zoom (png_plot *plot)
{
GdkCursor* cursor = gdk_cursor_new(GDK_CROSSHAIR);
gdk_window_set_cursor(plot->window, cursor);
gdk_cursor_unref(cursor);
plot->status |= PLOT_ZOOMING;
gtk_statusbar_push(GTK_STATUSBAR(plot->statusbar), plot->cid,
_(" Drag to define zoom rectangle"));
}
static gint plot_popup_activated (GtkMenuItem *item, gpointer data)
{
png_plot *plot = (png_plot *) data;
const gchar *item_string = NULL;
int killplot = 0;
item_string = menu_item_get_text(item);
if (!strcmp(item_string, _("Add another curve..."))) {
dist_graph_add(plot);
} else if (!strcmp(item_string, _("Save as PNG..."))) {
plot->spec->termtype = GP_TERM_PNG;
file_selector_with_parent(SAVE_GNUPLOT, FSEL_DATA_MISC,
plot->spec, plot->shell);
} else if (!strcmp(item_string, _("Save as PDF..."))) {
plot->spec->termtype = GP_TERM_PDF;
pdf_ps_dialog(plot->spec, plot->shell);
} else if (!strcmp(item_string, _("Save as postscript (EPS)..."))) {
plot->spec->termtype = GP_TERM_EPS;
pdf_ps_dialog(plot->spec, plot->shell);
} else if (!strcmp(item_string, _("Save to session as icon"))) {
add_to_session_callback(plot->spec);
} else if (plot_is_range_mean(plot) && !strcmp(item_string, _("Help"))) {
show_gui_help(RMPLOT);
} else if (plot_is_hurst(plot) && !strcmp(item_string, _("Help"))) {
show_gui_help(HURST);
} else if (!strcmp(item_string, _("Freeze data labels"))) {
plot->spec->flags |= GPT_PRINT_MARKERS;
redisplay_edited_plot(plot);
} else if (!strcmp(item_string, _("Clear data labels"))) {
clear_labels(plot);
} else if (!strcmp(item_string, _("All data labels"))) {
show_all_labels(plot);
} else if (!strcmp(item_string, _("Zoom..."))) {
prepare_for_zoom(plot);
} else if (!strcmp(item_string, _("Restore full view"))) {
repaint_png(plot, PNG_UNZOOM);
} else if (!strcmp(item_string, _("Replace full view"))) {
zoom_replaces_plot(plot);
} else if (!strcmp(item_string, _("Display PDF"))) {
graph_display_pdf(plot->spec);
} else if (!strcmp(item_string, _("OLS estimates"))) {
if (plot->spec != NULL) {
do_graph_model(plot->spec->reglist, plot->spec->fit);
}
} else if (!strcmp(item_string, _("Numerical values"))) {
show_numbers_from_markers(plot->spec);
} else if (!strcmp(item_string, _("Numerical summary"))) {
boxplot_show_summary(plot->spec);
} else if (!strcmp(item_string, _("Edit"))) {
start_editing_png_plot(plot);
} else if (!strcmp(item_string, _("Font"))) {
plot_show_font_selector(plot, plot->spec->fontstr);
} else if (!strcmp(item_string, _("Close"))) {
killplot = 1;
}
if (killplot) {
gtk_widget_destroy(plot->shell);
}
return TRUE;
}
static void attach_color_popup (GtkWidget *w, png_plot *plot)
{
GtkWidget *item, *cpopup;
const char *color_items[] = {
N_("color"),
N_("monochrome")
};
int i;
cpopup = gtk_menu_new();
for (i=0; i<2; i++) {
item = gtk_menu_item_new_with_label(_(color_items[i]));
g_signal_connect(G_OBJECT(item), "activate",
G_CALLBACK(color_popup_activated), w);
g_object_set_data(G_OBJECT(item), "plot", plot);
gtk_widget_show(item);
gtk_menu_shell_append(GTK_MENU_SHELL(cpopup), item);
}
gtk_menu_item_set_submenu(GTK_MENU_ITEM(w), cpopup);
}
#define showing_all_labels(p) (p->spec != NULL && \
(p->spec->flags & GPT_PRINT_MARKERS) && \
p->spec->labeled == NULL)
#define graph_model_ok(f) (f == PLOT_FIT_OLS || \
f == PLOT_FIT_QUADRATIC || \
f == PLOT_FIT_INVERSE)
#define plot_not_zoomable(p) ((p->status & PLOT_DONT_ZOOM) || \
(p->spec != NULL && \
p->spec->code == PLOT_ROOTS))
static void build_plot_menu (png_plot *plot)
{
GtkWidget *item;
const char *regular_items[] = {
N_("Add another curve..."),
#ifdef G_OS_WIN32
N_("Save as Windows metafile (EMF)..."),
#endif
N_("Save as PNG..."),
N_("Save as postscript (EPS)..."),
N_("Save as PDF..."),
#ifndef G_OS_WIN32
N_("Save as Windows metafile (EMF)..."),
#endif
N_("Copy to clipboard"),
N_("Save to session as icon"),
N_("Freeze data labels"),
N_("All data labels"),
N_("Clear data labels"),
N_("Zoom..."),
#ifdef G_OS_WIN32
N_("Print"),
#endif
N_("Display PDF"),
N_("OLS estimates"),
N_("Numerical values"),
N_("Numerical summary"),
N_("Edit"),
N_("Font"),
N_("Help"),
N_("Close"),
NULL
};
const char *zoomed_items[] = {
N_("Restore full view"),
N_("Replace full view"),
N_("Close"),
NULL
};
const char **plot_items;
int i;
plot->popup = gtk_menu_new();
if (plot_is_zoomed(plot)) {
plot_items = zoomed_items;
} else {
plot_items = regular_items;
}
i = 0;
while (plot_items[i]) {
int colorpop = 0;
if (plot->spec->code != PLOT_PROB_DIST &&
!strcmp(plot_items[i], "Add another curve...")) {
i++;
continue;
}
if (plot_not_zoomable(plot) &&
!strcmp(plot_items[i], "Zoom...")) {
i++;
continue;
}
if (!(plot_is_range_mean(plot) || plot_is_hurst(plot)) &&
!strcmp(plot_items[i], "Help")) {
i++;
continue;
}
if (plot_is_saved(plot) &&
!strcmp(plot_items[i], "Save to session as icon")) {
i++;
continue;
}
if ((plot_has_controller(plot) || plot_not_editable(plot)) &&
!strcmp(plot_items[i], "Edit")) {
i++;
continue;
}
if ((plot_has_controller(plot) || plot_is_editable(plot)) &&
!strcmp(plot_items[i], "Font")) {
i++;
continue;
}
if (!plot_labels_shown(plot) &&
(!strcmp(plot_items[i], "Freeze data labels") ||
!strcmp(plot_items[i], "Clear data labels"))) {
/* no labels displayed, so these items are not relevant */
i++;
continue;
}
if (labels_frozen(plot) &&
!strcmp(plot_items[i], "Freeze data labels")) {
/* labels are frozen so this item inapplicable */
i++;
continue;
}
if ((cant_edit(plot->spec->code) ||
cant_do_labels(plot) ||
showing_all_labels(plot)) &&
!strcmp(plot_items[i], "All data labels")) {
i++;
continue;
}
if ((!plot_has_regression_list(plot) ||
!graph_model_ok(plot->spec->fit)) &&
!strcmp(plot_items[i], "OLS estimates")) {
i++;
continue;
}
if (!plot_is_roots(plot) &&
!strcmp(plot_items[i], "Numerical values")) {
i++;
continue;
}
if (plot->spec->code != PLOT_BOXPLOTS &&
!strcmp(plot_items[i], "Numerical summary")) {
i++;
continue;
}
item = gtk_menu_item_new_with_label(_(plot_items[i]));
#ifdef G_OS_WIN32
if (!strcmp(plot_items[i], "Copy to clipboard") ||
!strcmp(plot_items[i], "Save as Windows metafile (EMF)...") ||
!strcmp(plot_items[i], "Print")) {
colorpop = 1;
}
#else
if (!strcmp(plot_items[i], "Copy to clipboard") ||
!strcmp(plot_items[i], "Save as Windows metafile (EMF)...")) {
colorpop = 1;
}
#endif
if (colorpop) {
/* special: items with color sub-menu */
attach_color_popup(item, plot);
} else {
/* all other menu items */
g_signal_connect(G_OBJECT(item), "activate",
G_CALLBACK(plot_popup_activated),
plot);
}
gtk_widget_show(item);
gtk_menu_shell_append(GTK_MENU_SHELL(plot->popup), item);
i++;
}
g_signal_connect(G_OBJECT(plot->popup), "destroy",
G_CALLBACK(gtk_widget_destroyed),
&plot->popup);
}
int redisplay_edited_plot (png_plot *plot)
{
gchar *plotcmd;
FILE *fp;
int err = 0;
#if GPDEBUG
fprintf(stderr, "redisplay_edited_plot: plot = %p\n", (void *) plot);
#endif
/* open file in which to dump plot specification */
gnuplot_png_init(plot, &fp);
if (fp == NULL) {
return 1;
}
/* dump the edited plot details to file */
set_png_output(plot->spec);
plotspec_print(plot->spec, fp);
fclose(fp);
/* get gnuplot to create a new PNG graph */
plotcmd = g_strdup_printf("\"%s\" \"%s\"",
gretl_gnuplot_path(),
plot->spec->fname);
err = gretl_spawn(plotcmd);
g_free(plotcmd);
if (err) {
gui_errmsg(err);
return err;
}
/* reset format flags */
set_plot_format_flags(plot);
/* grab (possibly modified) data ranges */
get_plot_ranges(plot, plot->spec->code);
/* put the newly created PNG onto the plot canvas */
return render_pngfile(plot, PNG_REDISPLAY);
}
/* preparation for redisplaying graph: here we handle the case where
we're switching to a zoomed view (by use of a temporary gnuplot
source file); then we get gnuplot to create a new PNG.
*/
static int repaint_png (png_plot *plot, int view)
{
char zoomname[MAXLEN];
gchar *plotcmd = NULL;
int err = 0;
if (view == PNG_ZOOM) {
FILE *fpin, *fpout;
char line[MAXLEN];
fpin = gretl_fopen(plot->spec->fname, "r");
if (fpin == NULL) {
return 1;
}
gretl_build_path(zoomname, gretl_dotdir(), "zoomplot.gp", NULL);
fpout = gretl_fopen(zoomname, "w");
if (fpout == NULL) {
fclose(fpin);
return 1;
}
/* write zoomed range into auxiliary gnuplot source file */
gretl_push_c_numeric_locale();
fprintf(fpout, "set xrange [%g:%g]\n", plot->zoom_xmin,
plot->zoom_xmax);
fprintf(fpout, "set yrange [%g:%g]\n", plot->zoom_ymin,
plot->zoom_ymax);
gretl_pop_c_numeric_locale();
while (fgets(line, MAXLEN-1, fpin)) {
if (strncmp(line, "set xrange", 10) &&
strncmp(line, "set yrange", 10))
fputs(line, fpout);
}
fclose(fpout);
fclose(fpin);
plotcmd = g_strdup_printf("\"%s\" \"%s\"",
gretl_gnuplot_path(),
zoomname);
} else {
/* PNG_UNZOOM, PNG_START or PNG_REDISPLAY */
plotcmd = g_strdup_printf("\"%s\" \"%s\"",
gretl_gnuplot_path(),
plot->spec->fname);
}
err = gretl_spawn(plotcmd);
g_free(plotcmd);
if (view == PNG_ZOOM) {
gretl_remove(zoomname);
}
if (err) {
gui_errmsg(err);
return err;
}
return render_pngfile(plot, view);
}
/* with a zoomed version of the current plot in place,
replace the full version wiuth the zoom
*/
static int zoom_replaces_plot (png_plot *plot)
{
FILE *fpin, *fpout;
char temp[MAXLEN], line[MAXLEN];
int err = 0;
fpin = gretl_fopen(plot->spec->fname, "r");
if (fpin == NULL) {
return 1;
}
sprintf(temp, "%szoomtmp", gretl_dotdir());
fpout = gretl_tempfile_open(temp);
if (fpout == NULL) {
fclose(fpin);
return 1;
}
/* write zoomed range into temporary file */
gretl_push_c_numeric_locale();
fprintf(fpout, "set xrange [%g:%g]\n", plot->zoom_xmin,
plot->zoom_xmax);
fprintf(fpout, "set yrange [%g:%g]\n", plot->zoom_ymin,
plot->zoom_ymax);
gretl_pop_c_numeric_locale();
while (fgets(line, MAXLEN-1, fpin)) {
if (strncmp(line, "set xrange", 10) &&
strncmp(line, "set yrange", 10)) {
fputs(line, fpout);
}
}
fclose(fpout);
fclose(fpin);
/* and copy over original graph source file */
err = gretl_copy_file(temp, plot->spec->fname);
if (err) {
gui_errmsg(err);
} else {
plot->xmin = plot->zoom_xmin;
plot->xmax = plot->zoom_xmax;
plot->ymin = plot->zoom_ymin;
plot->ymax = plot->zoom_ymax;
plot->zoom_xmin = plot->zoom_xmax = 0.0;
plot->zoom_ymin = plot->zoom_ymax = 0.0;
plot->status ^= PLOT_ZOOMED;
}
gretl_remove(temp);
return err;
}
static gint plot_button_release (GtkWidget *widget, GdkEventButton *event,
png_plot *plot)
{
if (plot_is_zooming(plot)) {
double z;
if (!get_data_xy(plot, event->x, event->y,
&plot->zoom_xmax, &plot->zoom_ymax)) {
return TRUE;
}
/* flip the selected rectangle if required */
if (plot->zoom_xmin > plot->zoom_xmax) {
z = plot->zoom_xmax;
plot->zoom_xmax = plot->zoom_xmin;
plot->zoom_xmin = z;
}
if (plot->zoom_ymin > plot->zoom_ymax) {
z = plot->zoom_ymax;
plot->zoom_ymax = plot->zoom_ymin;
plot->zoom_ymin = z;
}
if (plot->zoom_xmin != plot->zoom_xmax &&
plot->zoom_ymin != plot->zoom_ymax) {
repaint_png(plot, PNG_ZOOM);
}
plot->status ^= PLOT_ZOOMING;
gdk_window_set_cursor(plot->window, NULL);
gtk_statusbar_pop(GTK_STATUSBAR(plot->statusbar), plot->cid);
}
return TRUE;
}
static gint plot_button_press (GtkWidget *widget, GdkEventButton *event,
png_plot *plot)
{
if (plot_is_zooming(plot)) {
if (get_data_xy(plot, event->x, event->y,
&plot->zoom_xmin, &plot->zoom_ymin)) {
plot->screen_x0 = event->x;
plot->screen_y0 = event->y;
}
return TRUE;
}
if (plot_doing_position(plot)) {
if (plot->pos_entry != NULL) {
double dx, dy;
if (get_data_xy(plot, event->x, event->y, &dx, &dy)) {
gchar *posstr;
posstr = g_strdup_printf("%g %g", dx, dy);
gtk_entry_set_text(GTK_ENTRY(plot->pos_entry), posstr);
g_free(posstr);
}
}
terminate_plot_positioning(plot);
return TRUE;
}
if (plot->popup != NULL) {
gtk_widget_destroy(plot->popup);
plot->popup = NULL;
}
if (right_click(event)) {
build_plot_menu(plot);
gtk_menu_popup(GTK_MENU(plot->popup), NULL, NULL, NULL, NULL,
event->button, event->time);
}
return TRUE;
}
static gboolean
plot_key_handler (GtkWidget *w, GdkEventKey *key, png_plot *plot)
{
guint k = key->keyval;
if (plot_is_editable(plot) &&
(k == GDK_plus || k == GDK_greater ||
k == GDK_minus || k == GDK_less ||
k == GDK_equal || k == GDK_0)) {
int rk = 1;
if (k == GDK_minus || k == GDK_less) {
rk = -1;
} else if (k == GDK_0) {
rk = 0;
}
plot_do_rescale(plot, rk);
return TRUE;
}
switch (k) {
case GDK_q:
case GDK_Q:
#ifdef MAC_NATIVE
case GDK_w:
case GDK_W:
#endif
gtk_widget_destroy(w);
break;
case GDK_s:
case GDK_S:
add_to_session_callback(plot->spec);
break;
case GDK_z:
case GDK_Z:
prepare_for_zoom(plot);
break;
#ifdef G_OS_WIN32
case GDK_c:
win32_process_graph(plot->spec, WIN32_TO_CLIPBOARD);
break;
#endif
default:
break;
}
return TRUE;
}
#if GTK_MAJOR_VERSION >= 3
static
void plot_draw (GtkWidget *canvas, cairo_t *cr, gpointer data)
{
png_plot *plot = data;
cairo_set_source_surface(cr, plot->cs, 0, 0);
cairo_paint(cr);
}
#else /* transitional use of cairo */
static
void plot_expose (GtkWidget *canvas, GdkEventExpose *event,
gpointer data)
{
png_plot *plot = data;
plot->cr = gdk_cairo_create(plot->window);
gdk_cairo_set_source_pixmap(plot->cr, plot->pixmap, 0, 0);
gdk_cairo_rectangle(plot->cr, &event->area);
cairo_fill(plot->cr);
cairo_destroy(plot->cr);
}
#endif
static GdkPixbuf *gretl_pixbuf_new_from_file (const gchar *fname)
{
GdkPixbuf *pbuf = NULL;
GError *gerr = NULL;
pbuf = gdk_pixbuf_new_from_file(fname, &gerr);
if (gerr != NULL) {
errbox(gerr->message);
g_error_free(gerr);
} else if (pbuf == NULL) {
file_read_errbox(fname);
}
return pbuf;
}
static int resize_png_plot (png_plot *plot, int width, int height)
{
png_bounds b;
plot->pixel_width = width;
plot->pixel_height = height;
gtk_widget_set_size_request(GTK_WIDGET(plot->canvas),
plot->pixel_width, plot->pixel_height);
#if GTK_MAJOR_VERSION == 2
g_object_unref(plot->pixmap);
plot->pixmap = gdk_pixmap_new(plot->window,
plot->pixel_width,
plot->pixel_height,
-1);
#endif
if (plot->status & (PLOT_DONT_ZOOM | PLOT_DONT_MOUSE)) {
return 0;
}
/* try revising the gnuplot bounds info? */
if (plot_has_png_coords(plot) &&
get_png_bounds_info(&b) == GRETL_PNG_OK) {
plot->status |= PLOT_PNG_COORDS;
plot->pixel_xmin = b.xleft;
plot->pixel_xmax = b.xright;
plot->pixel_ymin = plot->pixel_height - b.ytop;
plot->pixel_ymax = plot->pixel_height - b.ybot;
plot->xmin = b.xmin;
plot->xmax = b.xmax;
plot->ymin = b.ymin;
plot->ymax = b.ymax;
} else {
plot->status |= (PLOT_DONT_ZOOM | PLOT_DONT_MOUSE);
}
return 0;
}
/* The last step in displaying a graph (or redisplaying after some
change has been made): grab the gnuplot-generated PNG file, make a
pixbuf out of it, and draw the pixbuf onto the canvas of the plot
window.
*/
static int render_pngfile (png_plot *plot, int view)
{
gint width, height;
GdkPixbuf *pbuf;
char pngname[MAXLEN];
gretl_build_path(pngname, gretl_dotdir(), "gretltmp.png", NULL);
pbuf = gretl_pixbuf_new_from_file(pngname);
if (pbuf == NULL) {
gretl_remove(pngname);
return 1;
}
width = gdk_pixbuf_get_width(pbuf);
height = gdk_pixbuf_get_height(pbuf);
if (width == 0 || height == 0) {
errbox(_("Malformed PNG file for graph"));
g_object_unref(pbuf);
gretl_remove(pngname);
return 1;
}
if (width != plot->pixel_width || height != plot->pixel_height) {
resize_png_plot(plot, width, height);
}
/* scrap any old record of which points are labeled */
if (plot->spec->labeled != NULL) {
free(plot->spec->labeled);
plot->spec->labeled = NULL;
if (!(plot->spec->flags & GPT_PRINT_MARKERS)) {
/* any markers will have disappeared on reprinting */
plot->format &= ~PLOT_MARKERS_UP;
}
}
#if GTK_MAJOR_VERSION >= 3
copy_pixbuf_to_surface(plot, pbuf);
#else
plot->cr = gdk_cairo_create(plot->pixmap);
gdk_cairo_set_source_pixbuf(plot->cr, pbuf, 0, 0);
cairo_paint(plot->cr);
cairo_destroy(plot->cr);
if (plot->savebuf != NULL) {
g_object_unref(plot->savebuf);
plot->savebuf = NULL;
}
#endif
g_object_unref(pbuf);
gretl_remove(pngname);
if (view != PNG_START) {
/* we're changing the view, so refresh the whole canvas */
redraw_plot_rectangle(plot, NULL);
if (view == PNG_ZOOM) {
plot->status |= PLOT_ZOOMED;
} else if (view == PNG_UNZOOM) {
plot->status ^= PLOT_ZOOMED;
}
}
#ifdef G_OS_WIN32
/* somehow the plot can end up underneath */
gtk_window_present(GTK_WINDOW(plot->shell));
#endif
return 0;
}
static void destroy_png_plot (GtkWidget *w, png_plot *plot)
{
/* delete temporary plot source file? */
if (!plot_is_saved(plot)) {
gretl_remove(plot->spec->fname);
}
#if GPDEBUG
fprintf(stderr, "destroy_png_plot: plot = %p, spec = %p\n",
(void *) plot, (void *) plot->spec);
#endif
#ifndef G_OS_WIN32
if (copyspec == plot->spec) {
copyspec = NULL;
}
#endif
plotspec_destroy(plot->spec);
#if GTK_MAJOR_VERSION >= 3
if (plot->cs != NULL) {
cairo_surface_destroy(plot->cs);
}
#else
if (plot->savebuf != NULL) {
g_object_unref(plot->savebuf);
}
#endif
g_object_unref(plot->shell);
free(plot);
}
/* Do a partial parse of the gnuplot source file: enough to determine
the data ranges so we can read back the mouse pointer coordinates
when the user moves the pointer over the graph.
*/
static int get_plot_ranges (png_plot *plot, PlotType ptype)
{
FILE *fp;
char line[MAXLEN];
int got_x = 0;
int got_y = 0;
int annual = 0;
png_bounds b;
int err = 0;
#if GPDEBUG
fprintf(stderr, "get_plot_ranges: plot=%p, plot->spec=%p\n",
(void *) plot, (void *) plot->spec);
#endif
plot->xmin = plot->xmax = 0.0;
plot->ymin = plot->ymax = 0.0;
plot->xint = plot->yint = 0;
if (no_readback(plot->spec->code)) {
plot->status |= (PLOT_DONT_ZOOM | PLOT_DONT_MOUSE);
return 1;
}
fp = gretl_fopen(plot->spec->fname, "r");
if (fp == NULL) {
plot->status |= (PLOT_DONT_ZOOM | PLOT_DONT_MOUSE);
return 1;
}
gretl_push_c_numeric_locale();
while (fgets(line, MAXLEN-1, fp) && strncmp(line, "plot ", 5)) {
if (sscanf(line, "set xrange [%lf:%lf]",
&plot->xmin, &plot->xmax) == 2) {
got_x = 1;
} else if (!strncmp(line, "# timeseries 1", 13)) {
annual = 1;
}
}
gretl_pop_c_numeric_locale();
fclose(fp);
/* now try getting accurate coordinate info from
auxiliary file (or maybe PNG file)
*/
if (get_png_bounds_info(&b) == GRETL_PNG_OK) {
plot->status |= PLOT_PNG_COORDS;
got_x = got_y = 1;
plot->pixel_xmin = b.xleft;
plot->pixel_xmax = b.xright;
plot->pixel_ymin = plot->pixel_height - b.ytop;
plot->pixel_ymax = plot->pixel_height - b.ybot;
plot->xmin = b.xmin;
plot->xmax = b.xmax;
plot->ymin = b.ymin;
plot->ymax = b.ymax;
# if POINTS_DEBUG
fprintf(stderr, "get_png_bounds_info():\n"
" xmin=%d xmax=%d ymin=%d ymax=%d\n",
plot->pixel_xmin, plot->pixel_xmax,
plot->pixel_ymin, plot->pixel_ymax);
fprintf(stderr, "using px_height %d, px_width %d\n",
plot->pixel_height, plot->pixel_width);
# endif
}
/* If got_x = 0 at this point, we didn't get an x-range out of
gnuplot, so we might as well give up.
*/
if (!got_x) {
plot->status |= (PLOT_DONT_ZOOM | PLOT_DONT_MOUSE);
return 1;
}
if (plot_has_png_coords(plot)) {
plot->status |= PLOT_HAS_XRANGE;
plot->status |= PLOT_HAS_YRANGE;
if (ptype != PLOT_ROOTS && ptype != PLOT_QQ) {
plot->status |= PLOT_CURSOR_LABEL;
}
if (annual) {
/* years on x-axis: show as integer */
plot->xint = 1;
} else if ((plot->xmax - plot->xmin) /
(plot->pixel_xmax - plot->pixel_xmin) >= 1.0) {
/* show x-axis variable as integer */
plot->xint = 1;
}
if ((plot->ymax - plot->ymin) /
(plot->pixel_ymax - plot->pixel_ymin) >= 1.0) {
/* show y-axis variable as integer */
plot->yint = 1;
}
} else {
plot->status |= (PLOT_DONT_ZOOM | PLOT_DONT_MOUSE);
#if POINTS_DEBUG
fputs("get_plot_ranges: setting PLOT_DONT_ZOOM, PLOT_DONT_MOUSE\n",
stderr);
#endif
}
return err;
}
static png_plot *png_plot_new (void)
{
png_plot *plot = mymalloc(sizeof *plot);
if (plot == NULL) {
return NULL;
}
plot->shell = NULL;
plot->canvas = NULL;
plot->popup = NULL;
plot->statusarea = NULL;
plot->statusbar = NULL;
plot->cursor_label = NULL;
plot->cr = NULL;
#if GTK_MAJOR_VERSION >= 3
plot->cs = NULL;
#else
plot->pixmap = NULL;
plot->savebuf = NULL;
#endif
plot->spec = NULL;
plot->editor = NULL;
plot->window = NULL;
plot->up_icon = NULL;
plot->down_icon = NULL;
plot->pixel_width = GP_WIDTH;
plot->pixel_height = GP_HEIGHT;
plot->xmin = plot->xmax = 0.0;
plot->ymin = plot->ymax = 0.0;
plot->xint = plot->yint = 0;
plot->zoom_xmin = plot->zoom_xmax = 0.0;
plot->zoom_ymin = plot->zoom_ymax = 0.0;
plot->screen_x0 = plot->screen_y0 = 0;
plot->pd = 0;
plot->err = 0;
plot->cid = 0;
plot->status = 0;
plot->format = 0;
return plot;
}
/* note: @fname is the name of the file containing the
plot commands.
*/
static int gnuplot_show_png (const char *fname,
const char *name,
void *session_ptr)
{
GtkWidget *vbox;
GtkWidget *canvas_hbox;
GtkWidget *status_hbox = NULL;
gchar *title = NULL;
png_plot *plot;
int err = 0;
if (*fname == '\0') {
return 0;
}
gretl_error_clear();
#if GPDEBUG
fprintf(stderr, "gnuplot_show_png:\n fname='%s', saved=%d\n",
fname, (session_ptr != NULL));
#endif
plot = png_plot_new();
if (plot == NULL) {
return E_ALLOC;
}
plot->spec = plotspec_new();
if (plot->spec == NULL) {
free(plot);
return E_ALLOC;
}
strcpy(plot->spec->fname, fname);
if (session_ptr != NULL) {
plot->status |= PLOT_SAVED;
}
/* make png plot struct accessible via spec */
plot->spec->ptr = plot;
/* Parse the gnuplot source file. If we hit errors here,
flag this, but it's not necessarily a show-stopper in
terms of simply displaying the graph -- unless we get
E_FOPEN.
*/
plot->err = read_plotspec_from_file(plot->spec, &plot->pd);
if (plot->err == E_FOPEN) {
plotspec_destroy(plot->spec);
free(plot);
return E_FOPEN;
}
#if GPDEBUG
fprintf(stderr, "gnuplot_show_png: read_plotspec_from_file returned %d\n",
plot->err);
#endif
if (plot->err || cant_edit(plot->spec->code)) {
plot->status |= (PLOT_DONT_EDIT | PLOT_DONT_ZOOM | PLOT_DONT_MOUSE);
} else {
set_plot_format_flags(plot);
}
if (plot->spec->code == PLOT_PERIODOGRAM) {
/* the x2 axis gets broken, and also the x axis if it's
in degrees or radians, on zooming
*/
plot->status |= PLOT_DONT_ZOOM;
} else if (plot->spec->code == PLOT_ROOTS ||
plot->spec->code == PLOT_QQ) {
plot->pixel_width = plot->pixel_height = GP_SQ_SIZE;
}
if (plot->spec->flags & GPT_LETTERBOX) {
plot->pixel_width = GP_LB_WIDTH;
plot->pixel_height = GP_LB_HEIGHT;
} else if (plot->spec->flags & GPT_XXL) {
plot->pixel_width = GP_XXL_WIDTH;
plot->pixel_height = GP_XXL_HEIGHT;
} else if (plot->spec->flags & GPT_XL) {
plot->pixel_width = GP_XL_WIDTH;
plot->pixel_height = GP_XL_HEIGHT;
} else if (plot->spec->flags & GPT_XW) {
plot->pixel_width = GP_XW_WIDTH;
plot->pixel_height = GP_HEIGHT;
}
if (plot->spec->scale != 1.0) {
plot_get_scaled_dimensions(&plot->pixel_width,
&plot->pixel_height,
plot->spec->scale);
}
if (!plot->err) {
int range_err = get_plot_ranges(plot, plot->spec->code);
#if GPDEBUG
fprintf(stderr, "range_err = %d\n", range_err);
#endif
if (plot->spec->nbars > 0) {
if (range_err) {
plot->spec->nbars = 0;
} else {
plotspec_set_bars_limits(plot->spec,
plot->xmin, plot->xmax,
plot->ymin, plot->ymax);
}
}
}
plot->shell = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/* note need for corresponding unref */
g_object_ref(plot->shell);
if (name != NULL) {
title = g_strdup_printf("gretl: %s", name);
} else {
title = g_strdup(_("gretl: graph"));
}
gtk_window_set_title(GTK_WINDOW(plot->shell), title);
gtk_window_set_resizable(GTK_WINDOW(plot->shell), FALSE);
g_signal_connect(G_OBJECT(plot->shell), "destroy",
G_CALLBACK(destroy_png_plot), plot);
g_free(title);
vbox = gtk_vbox_new(FALSE, 2);
gtk_container_add(GTK_CONTAINER(plot->shell), vbox);
/* box to hold canvas */
canvas_hbox = gtk_hbox_new(FALSE, 1);
gtk_box_pack_start(GTK_BOX(vbox), canvas_hbox, TRUE, TRUE, 0);
/* eventbox and hbox for status area */
plot->statusarea = gtk_event_box_new();
gtk_box_pack_start(GTK_BOX(vbox), plot->statusarea, FALSE, FALSE, 0);
status_hbox = gtk_hbox_new(FALSE, 2);
gtk_container_add(GTK_CONTAINER(plot->statusarea), status_hbox);
/* Create drawing-area widget */
plot->canvas = gtk_drawing_area_new();
gtk_widget_set_size_request(GTK_WIDGET(plot->canvas),
plot->pixel_width, plot->pixel_height);
gtk_widget_set_events (plot->canvas, GDK_EXPOSURE_MASK
| GDK_LEAVE_NOTIFY_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
| GDK_POINTER_MOTION_MASK
| GDK_POINTER_MOTION_HINT_MASK);
gtk_widget_set_can_focus(plot->canvas, TRUE);
g_signal_connect(G_OBJECT(plot->canvas), "button-press-event",
G_CALLBACK(plot_button_press), plot);
g_signal_connect(G_OBJECT(plot->canvas), "button-release-event",
G_CALLBACK(plot_button_release), plot);
gtk_box_pack_start(GTK_BOX(canvas_hbox), plot->canvas, FALSE, FALSE, 0);
if (plot_show_cursor_label(plot)) {
/* cursor label (graph position indicator) */
GtkWidget *frame = gtk_frame_new(NULL);
gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
plot->cursor_label = gtk_label_new(" ");
gtk_widget_set_size_request(plot->cursor_label, 160, -1);
gtk_container_add(GTK_CONTAINER(frame), plot->cursor_label);
gtk_box_pack_start(GTK_BOX(status_hbox), frame, FALSE, FALSE, 0);
}
/* the statusbar */
plot->statusbar = gtk_statusbar_new();
gtk_box_pack_start(GTK_BOX(status_hbox), plot->statusbar, TRUE, TRUE, 0);
add_graph_toolbar(status_hbox, plot);
#if GTK_MAJOR_VERSION < 3
gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(plot->statusbar), FALSE);
#endif
plot->cid = gtk_statusbar_get_context_id(GTK_STATUSBAR(plot->statusbar),
"plot_message");
#if 0
gtk_statusbar_push(GTK_STATUSBAR(plot->statusbar),
plot->cid, _(" Right-click on graph for menu"));
#endif
if (plot_has_xrange(plot)) {
g_signal_connect(G_OBJECT(plot->canvas), "motion-notify-event",
G_CALLBACK(plot_motion_callback), plot);
}
gtk_widget_realize(plot->canvas);
plot->window = gtk_widget_get_window(plot->canvas);
#if GTK_MAJOR_VERSION < 3
gdk_window_set_back_pixmap(plot->window, NULL, FALSE);
#endif
/* finish setup of plot->shell */
g_object_set_data(G_OBJECT(plot->shell), "plot-filename",
plot->spec->fname);
window_list_add(plot->shell, GNUPLOT);
g_signal_connect(G_OBJECT(plot->shell), "key-press-event",
G_CALLBACK(plot_key_handler), plot);
gtk_widget_show_all(plot->shell);
#if 0
/* set the focus to the canvas area */
gtk_widget_grab_focus(plot->canvas);
#endif
#if GTK_MAJOR_VERSION >= 3
g_signal_connect(G_OBJECT(plot->canvas), "draw",
G_CALLBACK(plot_draw), plot);
#else
plot->pixmap = gdk_pixmap_new(plot->window,
plot->pixel_width,
plot->pixel_height,
-1);
g_signal_connect(G_OBJECT(plot->canvas), "expose-event",
G_CALLBACK(plot_expose), plot);
#endif
err = render_pngfile(plot, PNG_START);
if (err) {
gtk_widget_destroy(plot->shell);
} else {
g_object_set_data(G_OBJECT(plot->shell), "object", plot);
if (session_ptr != NULL) {
g_object_set_data(G_OBJECT(plot->shell),
"session-ptr", session_ptr);
}
}
return err;
}
/* Called on a newly created PNG graph; note that
the filename returned by gretl_plotfile() is the
input file (set of gnuplot commands).
*/
void register_graph (void)
{
gnuplot_show_png(gretl_plotfile(), NULL, NULL);
}
/* @fname is the name of a plot command file from the
current session and @title is its display name;
@session_ptr is a pointer to the session object.
*/
void display_session_graph (const char *fname,
const char *title,
void *session_ptr)
{
char fullname[MAXLEN];
gchar *plotcmd;
int err = 0;
if (g_path_is_absolute(fname)) {
strcpy(fullname, fname);
} else {
sprintf(fullname, "%s%s", gretl_dotdir(), fname);
}
err = add_png_term_to_plot(fullname);
if (err) {
return;
}
plotcmd = g_strdup_printf("\"%s\" \"%s\"",
gretl_gnuplot_path(),
fullname);
err = gretl_spawn(plotcmd);
g_free(plotcmd);
if (err) {
/* display the bad plot file */
view_file(fullname, 0, 0, 78, 350, VIEW_FILE);
} else {
err = gnuplot_show_png(fullname, title, session_ptr);
}
if (err) {
gui_errmsg(err);
}
}
static int get_png_plot_bounds (const char *str, png_bounds *bounds)
{
int ret = GRETL_PNG_OK;
bounds->xleft = bounds->xright = 0;
bounds->ybot = bounds->ytop = 0;
if (sscanf(str, "pixel_bounds: %d %d %d %d",
&bounds->xleft, &bounds->xright,
&bounds->ybot, &bounds->ytop) != 4) {
ret = GRETL_PNG_BAD_COMMENTS;
}
if (ret == GRETL_PNG_OK && bounds->xleft == 0 &&
bounds->xright == 0 && bounds->ybot == 0 &&
bounds->ytop == 0) {
ret = GRETL_PNG_NO_COORDS;
}
#if POINTS_DEBUG
fprintf(stderr, "Got: xleft=%d, xright=%d, ybot=%d, ytop=%d\n",
bounds->xleft, bounds->xright, bounds->ybot, bounds->ytop);
#endif
return ret;
}
static int get_png_data_bounds (char *str, png_bounds *bounds)
{
char *p = str;
int ret = GRETL_PNG_OK;
while (*p) {
if (*p == ',') *p = '.';
p++;
}
bounds->xmin = bounds->xmax = 0.0;
bounds->ymin = bounds->ymax = 0.0;
gretl_push_c_numeric_locale();
if (sscanf(str, "data_bounds: %lf %lf %lf %lf",
&bounds->xmin, &bounds->xmax,
&bounds->ymin, &bounds->ymax) != 4) {
ret = GRETL_PNG_BAD_COMMENTS;
}
if (ret == GRETL_PNG_OK && bounds->xmin == 0.0 &&
bounds->xmax == 0.0 && bounds->ymin == 0.0 &&
bounds->ymax == 0.0) {
ret = GRETL_PNG_NO_COORDS;
}
#if POINTS_DEBUG
fprintf(stderr, "Got: xmin=%g, xmax=%g, ymin=%g, ymax=%g\n",
bounds->xmin, bounds->xmax, bounds->ymin, bounds->ymax);
#endif
gretl_pop_c_numeric_locale();
return ret;
}
static int get_png_bounds_info (png_bounds *bounds)
{
FILE *fp;
char bbname[MAXLEN], line[128];
int plot_ret = -1, data_ret = -1;
int ret = GRETL_PNG_OK;
gretl_build_path(bbname, gretl_dotdir(), "gretltmp.png.bounds", NULL);
fp = gretl_fopen(bbname, "r");
if (fp == NULL) {
fprintf(stderr, "couldn't open %s\n", bbname);
return GRETL_PNG_NO_COMMENTS;
}
if (fgets(line, sizeof line, fp) == NULL) {
plot_ret = GRETL_PNG_NO_COMMENTS;
} else {
plot_ret = get_png_plot_bounds(line, bounds);
}
if (fgets(line, sizeof line, fp) == NULL) {
data_ret = GRETL_PNG_NO_COMMENTS;
} else {
data_ret = get_png_data_bounds(line, bounds);
}
if (plot_ret == GRETL_PNG_NO_COORDS && data_ret == GRETL_PNG_NO_COORDS) {
/* comments were present and correct, but all zero */
ret = GRETL_PNG_NO_COORDS;
} else if (plot_ret != GRETL_PNG_OK || data_ret != GRETL_PNG_OK) {
/* one or both set of coordinates bad or missing */
if (plot_ret >= 0 || data_ret >= 0) {
ret = GRETL_PNG_BAD_COMMENTS;
} else {
ret = GRETL_PNG_NO_COMMENTS;
}
}
fclose(fp);
gretl_remove(bbname);
return ret;
}
#ifndef G_OS_WIN32
#include <errno.h>
static int get_terminal (char *s)
{
const gchar *terms[] = {
"xterm",
"rxvt",
"gnome-terminal",
"kterm",
"urxvt",
NULL
};
gchar *test;
int i;
for (i=0; terms[i] != NULL; i++) {
test = g_find_program_in_path(terms[i]);
if (test != NULL) {
g_free(test);
strcpy(s, terms[i]);
return 0;
}
}
#ifdef OS_OSX
/* fallback for XQuartz: may not be in PATH */
strcpy(s, "/opt/X11/bin/xterm");
if (gretl_file_exists(s)) {
return 0;
} else {
*s = '\0';
}
#endif
errbox(_("Couldn't find a usable terminal program"));
return 1;
}
#endif /* !G_OS_WIN32 */
#ifdef MAC_NATIVE
static void mac_do_gp_script (const char *plotfile)
{
gchar *buf = NULL;
gsize sz = 0;
if (g_file_get_contents(plotfile, &buf, &sz, NULL)) {
run_gnuplot_script(buf);
g_free(buf);
}
}
#endif
/* Callback for "Gnuplot" item in Tools menu: open a
gnuplot session and let the user do whatever. In
this case we need a controlling terminal window if
we're not on MS Windows.
*/
void launch_gnuplot_interactive (void)
{
#if defined(G_OS_WIN32)
win32_run_async(gretl_gnuplot_path(), NULL);
#elif defined(MAC_NATIVE)
const char *gppath = gretl_gnuplot_path();
gchar *gpline;
# ifdef PKGBUILD
/* call driver script to set environment correctly -- and
in addition prepend a full path spec if necessary
*/
if (g_path_is_absolute(gppath)) {
gpline = g_strdup_printf("open -a Terminal.app \"%s.sh\"",
gppath);
} else {
gpline = g_strdup_printf("open -a Terminal.app \"%s%s.sh\"",
gretl_bindir(), gppath);
}
# else
gpline = g_strdup_printf("open -a Terminal.app \"%s\"", gppath);
# endif
system(gpline);
g_free(gpline);
#else /* neither WIN32 nor MAC_NATIVE */
char term[32];
int err;
err = get_terminal(term);
if (!err) {
const char *gp = gretl_gnuplot_path();
GError *error = NULL;
gchar *argv[6];
# ifdef OS_OSX
char *altgp = g_strdup_printf("%s.sh", gp);
if (gretl_file_exists(altgp)) {
gp = altgp;
} else {
g_free(altgp);
altgp = NULL;
}
# endif
if (strstr(term, "gnome")) {
/* gnome-terminal */
argv[0] = term;
argv[1] = "--title=\"gnuplot: type q to quit\"";
argv[2] = "-x";
argv[3] = (char *) gp;
argv[4] = NULL;
} else {
/* xterm, rxvt, kterm */
argv[0] = term;
argv[1] = "-title";
argv[2] = "gnuplot: type q to quit";
argv[3] = "-e";
argv[4] = (char *) gp;
argv[5] = NULL;
}
g_spawn_async(NULL, /* working dir */
argv,
NULL, /* env */
G_SPAWN_SEARCH_PATH,
NULL, /* child_setup */
NULL, /* user_data */
NULL, /* child_pid ptr */
&error);
if (error != NULL) {
errbox(error->message);
g_error_free(error);
}
# ifdef OS_OSX
g_free(altgp);
# endif
}
#endif /* !(G_OS_WIN32 or MAC_NATIVE) */
}
void gnuplot_view_3d (const char *plotfile)
{
#if defined(G_OS_WIN32)
win32_run_async(gretl_gnuplot_path(), plotfile);
#elif defined(MAC_NATIVE) && !defined(GNUPLOT3D)
mac_do_gp_script(plotfile);
#else
real_send_to_gp(plotfile, 0);
#endif /* !(G_OS_WIN32 or MAC_NATIVE) */
}
|