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
|
/*** /
This file is part of Golly, a Game of Life Simulator.
Copyright (C) 2008 Andrew Trevorrow and Tomas Rokicki.
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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Web site: http://sourceforge.net/projects/golly
Authors: rokicki@gmail.com andrew@trevorrow.com
/ ***/
#include "wx/wxprec.h" // for compilers that support precompilation
#ifndef WX_PRECOMP
#include "wx/wx.h" // for all others include the necessary headers
#endif
#include "wx/file.h" // for wxFile
#include "wx/dcbuffer.h" // for wxBufferedPaintDC
#include "bigint.h"
#include "lifealgo.h"
#include "qlifealgo.h"
#include "hlifealgo.h"
#include "viewport.h"
#include "liferules.h" // for global_liferules
#include "wxgolly.h" // for mainptr, statusptr
#include "wxutils.h" // for Warning, Fatal
#include "wxprefs.h" // for showgridlines, canchangerule, etc
#include "wxhelp.h" // for ShowHelp
#include "wxmain.h" // for mainptr->...
#include "wxstatus.h" // for statusptr->...
#include "wxrender.h" // for CreatePasteImage, DrawView, DrawSelection
#include "wxscript.h" // for inscript, PassKeyToScript
#include "wxedit.h" // for Selection
#include "wxundo.h" // for currlayer->undoredo->...
#include "wxlayer.h" // for currlayer, ResizeLayers, etc
#include "wxview.h"
#ifdef __WXMAC__
#include <Carbon/Carbon.h> // for Button
#endif
// This module implements a viewport window for editing and viewing patterns.
// -----------------------------------------------------------------------------
const int DRAG_RATE = 20; // call OnDragTimer 50 times per sec
static bool stop_drawing = false; // terminate a draw done while generating?
static wxString oldrule; // rule before readclipboard is called
static wxString newrule; // rule after readclipboard is called
// -----------------------------------------------------------------------------
// event table and handlers:
BEGIN_EVENT_TABLE(PatternView, wxWindow)
EVT_PAINT ( PatternView::OnPaint)
EVT_SIZE ( PatternView::OnSize)
EVT_KEY_DOWN ( PatternView::OnKeyDown)
EVT_KEY_UP ( PatternView::OnKeyUp)
EVT_CHAR ( PatternView::OnChar)
EVT_LEFT_DOWN ( PatternView::OnMouseDown)
EVT_LEFT_DCLICK ( PatternView::OnMouseDown)
EVT_LEFT_UP ( PatternView::OnMouseUp)
#if wxCHECK_VERSION(2, 8, 0)
EVT_MOUSE_CAPTURE_LOST ( PatternView::OnMouseCaptureLost)
#endif
EVT_RIGHT_DOWN ( PatternView::OnRMouseDown)
EVT_RIGHT_DCLICK ( PatternView::OnRMouseDown)
EVT_MOTION ( PatternView::OnMouseMotion)
EVT_ENTER_WINDOW ( PatternView::OnMouseEnter)
EVT_LEAVE_WINDOW ( PatternView::OnMouseExit)
EVT_MOUSEWHEEL ( PatternView::OnMouseWheel)
EVT_TIMER (wxID_ANY, PatternView::OnDragTimer)
EVT_SCROLLWIN ( PatternView::OnScroll)
EVT_ERASE_BACKGROUND ( PatternView::OnEraseBackground)
END_EVENT_TABLE()
// -----------------------------------------------------------------------------
// most editing operations are limited to absolute coordinates <= 10^9 because
// getcell and setcell only take int parameters (the limits must be smaller
// than INT_MIN and INT_MAX to avoid boundary conditions)
static bigint min_coord = -1000000000;
static bigint max_coord = +1000000000;
bool PatternView::OutsideLimits(bigint& t, bigint& l, bigint& b, bigint& r)
{
return ( t < min_coord || l < min_coord ||
b > max_coord || r > max_coord );
}
// -----------------------------------------------------------------------------
bool PatternView::SelectionExists()
{
return currlayer->currsel.Exists();
}
// -----------------------------------------------------------------------------
bool PatternView::CopyRect(int itop, int ileft, int ibottom, int iright,
lifealgo* srcalgo, lifealgo* destalgo,
bool erasesrc, const wxString& progmsg)
{
int wd = iright - ileft + 1;
int ht = ibottom - itop + 1;
int cx, cy;
double maxcount = (double)wd * (double)ht;
int cntr = 0;
bool abort = false;
// copy (and erase if requested) live cells from given rect
// in source universe to same rect in destination universe
BeginProgress(progmsg);
for ( cy=itop; cy<=ibottom; cy++ ) {
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = srcalgo->nextcell(cx, cy);
if (skip + cx > iright)
skip = -1; // pretend we found no more live cells
if (skip >= 0) {
// found next live cell
cx += skip;
destalgo->setcell(cx, cy, 1);
if (erasesrc) srcalgo->setcell(cx, cy, 0);
} else {
cx = iright + 1; // done this row
}
cntr++;
if ((cntr % 4096) == 0) {
double prog = ((cy - itop) * (double)(iright - ileft + 1) +
(cx - ileft)) / maxcount;
abort = AbortProgress(prog, wxEmptyString);
if (abort) break;
}
}
if (abort) break;
}
if (erasesrc) srcalgo->endofpattern();
destalgo->endofpattern();
EndProgress();
return !abort;
}
// -----------------------------------------------------------------------------
void PatternView::CopyAllRect(int itop, int ileft, int ibottom, int iright,
lifealgo* srcalgo, lifealgo* destalgo,
const wxString& progmsg)
{
int wd = iright - ileft + 1;
int ht = ibottom - itop + 1;
int cx, cy;
double maxcount = (double)wd * (double)ht;
int cntr = 0;
bool abort = false;
// copy all cells from given rect in srcalgo to same rect in destalgo
BeginProgress(progmsg);
for ( cy=itop; cy<=ibottom; cy++ ) {
for ( cx=ileft; cx<=iright; cx++ ) {
destalgo->setcell(cx, cy, srcalgo->getcell(cx, cy));
cntr++;
if ((cntr % 4096) == 0) {
abort = AbortProgress((double)cntr / maxcount, wxEmptyString);
if (abort) break;
}
}
if (abort) break;
}
destalgo->endofpattern();
EndProgress();
}
// -----------------------------------------------------------------------------
void PatternView::ClearSelection()
{
currlayer->currsel.Clear();
}
// -----------------------------------------------------------------------------
void PatternView::ClearOutsideSelection()
{
currlayer->currsel.ClearOutside();
}
// -----------------------------------------------------------------------------
void PatternView::CutSelection()
{
if (!SelectionExists()) return;
if (mainptr->generating) {
// terminate generating loop and set command_pending flag
mainptr->Stop();
mainptr->command_pending = true;
mainptr->cmdevent.SetId(ID_CUT);
return;
}
currlayer->currsel.CopyToClipboard(true);
}
// -----------------------------------------------------------------------------
void PatternView::CopySelection()
{
if (!SelectionExists()) return;
if (mainptr->generating) {
// terminate generating loop and set command_pending flag
mainptr->Stop();
mainptr->command_pending = true;
mainptr->cmdevent.SetId(ID_COPY);
return;
}
currlayer->currsel.CopyToClipboard(false);
}
// -----------------------------------------------------------------------------
void PatternView::SetPasteRect(wxRect& rect, bigint& wd, bigint& ht)
{
int x, y, pastewd, pasteht;
int mag = currlayer->view->getmag();
// find cell coord of current paste cursor position
pair<bigint, bigint> pcell = currlayer->view->at(pastex, pastey);
// determine bottom right cell
bigint right = pcell.first; right += wd; right -= 1;
bigint bottom = pcell.second; bottom += ht; bottom -= 1;
// best to use same method as in Selection::Visible
pair<int,int> lt = currlayer->view->screenPosOf(pcell.first, pcell.second, currlayer->algo);
pair<int,int> rb = currlayer->view->screenPosOf(right, bottom, currlayer->algo);
if (mag > 0) {
// move rb to pixel at bottom right corner of cell
rb.first += (1 << mag) - 1;
rb.second += (1 << mag) - 1;
if (mag > 1) {
// avoid covering gaps at scale 1:4 and above
rb.first--;
rb.second--;
}
}
x = lt.first;
y = lt.second;
pastewd = rb.first - lt.first + 1;
pasteht = rb.second - lt.second + 1;
// this should never happen but play safe
if (pastewd <= 0) pastewd = 1;
if (pasteht <= 0) pasteht = 1;
rect = wxRect(x, y, pastewd, pasteht);
int xoffset, yoffset;
int cellsize = 1 << mag; // only used if mag > 0
int gap = 1; // ditto
if (mag == 1) gap = 0; // but no gap between cells at scale 1:2
switch (plocation) {
case TopLeft:
break;
case TopRight:
xoffset = mag > 0 ? -(pastewd - cellsize + gap) : -pastewd + 1;
rect.Offset(xoffset, 0);
break;
case BottomRight:
xoffset = mag > 0 ? -(pastewd - cellsize + gap) : -pastewd + 1;
yoffset = mag > 0 ? -(pasteht - cellsize + gap) : -pasteht + 1;
rect.Offset(xoffset, yoffset);
break;
case BottomLeft:
yoffset = mag > 0 ? -(pasteht - cellsize + gap) : -pasteht + 1;
rect.Offset(0, yoffset);
break;
case Middle:
xoffset = mag > 0 ? -(pastewd / cellsize / 2) * cellsize : -pastewd / 2;
yoffset = mag > 0 ? -(pasteht / cellsize / 2) * cellsize : -pasteht / 2;
rect.Offset(xoffset, yoffset);
break;
}
}
// -----------------------------------------------------------------------------
void PatternView::PasteTemporaryToCurrent(lifealgo* tempalgo, bool toselection,
bigint top, bigint left, bigint bottom, bigint right)
{
// make sure given edges are within getcell/setcell limits
if ( OutsideLimits(top, left, bottom, right) ) {
statusptr->ErrorMessage(_("Clipboard pattern is too big."));
return;
}
int itop = top.toint();
int ileft = left.toint();
int ibottom = bottom.toint();
int iright = right.toint();
bigint wd = iright - ileft + 1;
bigint ht = ibottom - itop + 1;
if ( toselection ) {
if ( !currlayer->currsel.CanPaste(wd, ht, top, left) ) {
statusptr->ErrorMessage(_("Clipboard pattern is bigger than selection."));
return;
}
// top and left have been set to the selection's top left corner
} else {
// ask user where to paste the clipboard pattern
statusptr->DisplayMessage(_("Click where you want to paste..."));
// temporarily change cursor to cross
wxCursor* savecurs = currlayer->curs;
currlayer->curs = curs_cross;
// CheckCursor(true); // probs on Mac if Paste menu item selected
#ifdef __WXMAC__
wxSetCursor(*currlayer->curs);
#endif
SetCursor(*currlayer->curs);
// create image for drawing pattern to be pasted; note that given box
// is not necessarily the minimal bounding box because clipboard pattern
// might have blank borders (in fact it could be empty)
wxRect bbox = wxRect(ileft, itop, wd.toint(), ht.toint());
CreatePasteImage(tempalgo, bbox);
waitingforclick = true;
mainptr->EnableAllMenus(false); // disable all menu items
mainptr->UpdateToolBar(false); // disable all tool bar buttons
UpdateLayerBar(false); // disable all layer bar buttons
CaptureMouse(); // get mouse down event even if outside view
pasterect = wxRect(-1,-1,0,0);
while (waitingforclick) {
wxPoint pt = ScreenToClient( wxGetMousePosition() );
pastex = pt.x;
pastey = pt.y;
if (PointInView(pt.x, pt.y)) {
// determine new paste rectangle
wxRect newrect;
SetPasteRect(newrect, wd, ht);
if (newrect != pasterect) {
// draw new pasterect
pasterect = newrect;
Refresh(false);
// don't update immediately
}
} else {
// mouse outside viewport so erase old pasterect if necessary
if ( pasterect.width > 0 ) {
pasterect = wxRect(-1,-1,0,0);
Refresh(false);
// don't update immediately
}
}
wxMilliSleep(10); // don't hog CPU
wxGetApp().Yield(true);
// make sure viewport retains focus so we can use keyboard shortcuts
SetFocus();
// waitingforclick becomes false if OnMouseDown is called
#ifdef __WXMAC__
// need to check for click here because OnMouseDown does not
// get called if click is in menu bar or in another window;
// is this a CaptureMouse bug in wxMac???
if ( waitingforclick && Button() ) {
pt = ScreenToClient( wxGetMousePosition() );
pastex = pt.x;
pastey = pt.y;
waitingforclick = false;
FlushEvents(mDownMask + mUpMask, 0); // avoid wx seeing click
}
#endif
}
if ( HasCapture() ) ReleaseMouse();
mainptr->EnableAllMenus(true);
DestroyPasteImage();
// restore cursor
currlayer->curs = savecurs;
CheckCursor(mainptr->IsActive());
if ( pasterect.width > 0 ) {
// erase old pasterect
Refresh(false);
// no need to update immediately
// Update();
}
if ( pastex < 0 || pastex > currlayer->view->getxmax() ||
pastey < 0 || pastey > currlayer->view->getymax() ) {
statusptr->DisplayMessage(_("Paste aborted."));
return;
}
// set paste rectangle's top left cell coord
pair<bigint, bigint> clickpos = currlayer->view->at(pastex, pastey);
top = clickpos.second;
left = clickpos.first;
bigint halfht = ht;
bigint halfwd = wd;
halfht.div2();
halfwd.div2();
if (currlayer->view->getmag() > 1) {
if (ht.even()) halfht -= 1;
if (wd.even()) halfwd -= 1;
}
switch (plocation) {
case TopLeft: /* no change*/ break;
case TopRight: left -= wd; left += 1; break;
case BottomRight: left -= wd; left += 1; top -= ht; top += 1; break;
case BottomLeft: top -= ht; top += 1; break;
case Middle: left -= halfwd; top -= halfht; break;
}
}
// check that paste rectangle is within edit limits
bottom = top; bottom += ht; bottom -= 1;
right = left; right += wd; right -= 1;
if ( OutsideLimits(top, left, bottom, right) ) {
statusptr->ErrorMessage(_("Pasting is not allowed outside +/- 10^9 boundary."));
return;
}
// set pastex,pastey to top left cell of paste rectangle
pastex = left.toint();
pastey = top.toint();
// save cell changes if undo/redo is enabled and script isn't constructing a pattern
bool savecells = allowundo && !currlayer->stayclean;
if (savecells && inscript) SavePendingChanges();
// copy pattern from temporary universe to current universe
int tx, ty, cx, cy;
double maxcount = wd.todouble() * ht.todouble();
int cntr = 0;
bool abort = false;
bool pattchanged = false;
BeginProgress(_("Pasting pattern"));
// we can speed up pasting sparse patterns by using nextcell in these cases:
// - if using Or mode
// - if current universe is empty
// - if paste rect is outside current pattern edges
bool usenextcell;
if ( pmode == Or || currlayer->algo->isEmpty() ) {
usenextcell = true;
} else {
bigint ctop, cleft, cbottom, cright;
currlayer->algo->findedges(&ctop, &cleft, &cbottom, &cright);
usenextcell = top > cbottom || bottom < ctop || left > cright || right < cleft;
}
lifealgo* curralgo = currlayer->algo;
if ( usenextcell ) {
cy = pastey;
for ( ty=itop; ty<=ibottom; ty++ ) {
cx = pastex;
for ( tx=ileft; tx<=iright; tx++ ) {
int skip = tempalgo->nextcell(tx, ty);
if (skip + tx > iright)
skip = -1; // pretend we found no more live cells
if (skip >= 0) {
// found next live cell so paste it into current universe
tx += skip;
cx += skip;
if (curralgo->getcell(cx, cy) != 1) {
curralgo->setcell(cx, cy, 1);
pattchanged = true;
if (savecells) currlayer->undoredo->SaveCellChange(cx, cy);
}
cx++;
} else {
tx = iright + 1; // done this row
}
cntr++;
if ((cntr % 4096) == 0) {
double prog = ((ty - itop) * (double)(iright - ileft + 1) +
(tx - ileft)) / maxcount;
abort = AbortProgress(prog, wxEmptyString);
if (abort) break;
}
}
if (abort) break;
cy++;
}
} else {
// have to use slower getcell/setcell calls
int tempstate, currstate;
cy = pastey;
for ( ty=itop; ty<=ibottom; ty++ ) {
cx = pastex;
for ( tx=ileft; tx<=iright; tx++ ) {
tempstate = tempalgo->getcell(tx, ty);
currstate = curralgo->getcell(cx, cy);
switch (pmode) {
case Copy:
if (tempstate != currstate) {
curralgo->setcell(cx, cy, tempstate);
pattchanged = true;
if (savecells) currlayer->undoredo->SaveCellChange(cx, cy);
}
break;
case Or:
// Or mode is done using above nextcell loop;
// we only include this case to avoid compiler warning
break;
case Xor:
if (tempstate == currstate) {
if (currstate != 0) {
curralgo->setcell(cx, cy, 0);
pattchanged = true;
if (savecells) currlayer->undoredo->SaveCellChange(cx, cy);
}
} else {
if (currstate != 1) {
curralgo->setcell(cx, cy, 1);
pattchanged = true;
if (savecells) currlayer->undoredo->SaveCellChange(cx, cy);
}
}
break;
}
cx++;
cntr++;
if ( (cntr % 4096) == 0 ) {
abort = AbortProgress((double)cntr / maxcount, wxEmptyString);
if (abort) break;
}
}
if (abort) break;
cy++;
}
}
if (pattchanged) currlayer->algo->endofpattern();
EndProgress();
// tidy up and display result
statusptr->ClearMessage();
if (pattchanged) {
if (savecells) currlayer->undoredo->RememberCellChanges(_("Paste"), currlayer->dirty);
MarkLayerDirty(); // calls SetWindowTitle
mainptr->UpdatePatternAndStatus();
}
// pasting clipboard pattern can also cause a rule change
if (canchangerule > 0 && oldrule != newrule) {
currlayer->algo->setrule( newrule.mb_str(wxConvLocal) );
if (global_liferules.hasB0notS8 && currlayer->hash) {
currlayer->algo->setrule( oldrule.mb_str(wxConvLocal) );
Warning(_("B0-not-S8 rules are not allowed when hashing."));
} else {
// show new rule in title bar
mainptr->SetWindowTitle(wxEmptyString);
if (savecells) currlayer->undoredo->RememberRuleChange(oldrule);
}
}
}
// -----------------------------------------------------------------------------
bool PatternView::GetClipboardPattern(lifealgo** tempalgo,
bigint* t, bigint* l, bigint* b, bigint* r)
{
#ifdef __WXX11__
if ( !wxFileExists(mainptr->clipfile) ) return false;
#else
wxTextDataObject data;
if ( !mainptr->GetTextFromClipboard(&data) ) return false;
// copy clipboard data to temporary file so we can handle all formats
// supported by readclipboard
wxFile tmpfile(mainptr->clipfile, wxFile::write);
if ( !tmpfile.IsOpened() ) {
Warning(_("Could not create temporary file for clipboard data!"));
return false;
}
if ( !tmpfile.Write(data.GetText()) ) {
Warning(_("Could not write clipboard data to temporary file! Maybe disk is full?"));
tmpfile.Close();
return false;
}
tmpfile.Close();
#endif
// remember rule before readclipboard changes it (all algos share a global rule)
oldrule = wxString(currlayer->algo->getrule(), wxConvLocal);
const char* err = readclipboard(mainptr->clipfile.mb_str(wxConvLocal),
**tempalgo, t, l, b, r);
if (err && strcmp(err,cannotreadhash) == 0) {
// clipboard contains macrocell data so we have to use hlife
delete *tempalgo;
*tempalgo = CreateNewUniverse(true);
err = readclipboard(mainptr->clipfile.mb_str(wxConvLocal),
**tempalgo, t, l, b, r);
}
if (canchangerule > 0) {
// set newrule for later use in PasteTemporaryToCurrent
if (canchangerule == 1 && !currlayer->algo->isEmpty()) {
// don't change rule if universe isn't empty
newrule = oldrule;
} else {
// remember rule set by readclipboard
newrule = wxString(currlayer->algo->getrule(), wxConvLocal);
}
}
// restore rule now in case error occurred
currlayer->algo->setrule( oldrule.mb_str(wxConvLocal) );
#ifdef __WXX11__
// don't delete clipboard file
#else
wxRemoveFile(mainptr->clipfile);
#endif
if (err) {
// note that error could be due to bad rule string in clipboard data
Warning(wxString(err,wxConvLocal));
return false;
}
return true;
}
// -----------------------------------------------------------------------------
void PatternView::PasteClipboard(bool toselection)
{
if (waitingforclick || !mainptr->ClipboardHasText()) return;
if (toselection && !SelectionExists()) return;
if (mainptr->generating) {
// terminate generating loop and set command_pending flag
mainptr->Stop();
mainptr->command_pending = true;
mainptr->cmdevent.SetId(toselection ? ID_PASTE_SEL : ID_PASTE);
return;
}
// create a temporary universe for storing clipboard pattern;
// use qlife because its setcell/getcell calls are faster
lifealgo* tempalgo = CreateNewUniverse(false);
// read clipboard pattern into temporary universe;
// note that tempalgo will be deleted and re-created as a hlifealgo
// if clipboard contains macrocell data
bigint top, left, bottom, right;
if ( GetClipboardPattern(&tempalgo, &top, &left, &bottom, &right) ) {
PasteTemporaryToCurrent(tempalgo, toselection, top, left, bottom, right);
}
delete tempalgo;
}
// -----------------------------------------------------------------------------
void PatternView::CyclePasteLocation()
{
if (plocation == TopLeft) {
plocation = TopRight;
if (!waitingforclick) statusptr->DisplayMessage(_("Paste location is Top Right."));
} else if (plocation == TopRight) {
plocation = BottomRight;
if (!waitingforclick) statusptr->DisplayMessage(_("Paste location is Bottom Right."));
} else if (plocation == BottomRight) {
plocation = BottomLeft;
if (!waitingforclick) statusptr->DisplayMessage(_("Paste location is Bottom Left."));
} else if (plocation == BottomLeft) {
plocation = Middle;
if (!waitingforclick) statusptr->DisplayMessage(_("Paste location is Middle."));
} else {
plocation = TopLeft;
if (!waitingforclick) statusptr->DisplayMessage(_("Paste location is Top Left."));
}
if (waitingforclick) {
// force redraw of paste rectangle if mouse is inside viewport
pasterect = wxRect(-1,-1,0,0);
}
}
// -----------------------------------------------------------------------------
void PatternView::CyclePasteMode()
{
if (pmode == Copy) {
pmode = Or;
if (!waitingforclick) statusptr->DisplayMessage(_("Paste mode is Or."));
} else if (pmode == Or) {
pmode = Xor;
if (!waitingforclick) statusptr->DisplayMessage(_("Paste mode is Xor."));
} else {
pmode = Copy;
if (!waitingforclick) statusptr->DisplayMessage(_("Paste mode is Copy."));
}
if (waitingforclick) {
// force redraw of paste rectangle if mouse is inside viewport
pasterect = wxRect(-1,-1,0,0);
}
}
// -----------------------------------------------------------------------------
void PatternView::DisplaySelectionSize()
{
if (waitingforclick || inscript || currlayer->undoredo->doingscriptchanges)
return;
currlayer->currsel.DisplaySize();
}
// -----------------------------------------------------------------------------
void PatternView::SaveCurrentSelection()
{
if (allowundo && !currlayer->stayclean) {
currlayer->savesel = currlayer->currsel;
}
}
// -----------------------------------------------------------------------------
void PatternView::RememberNewSelection(const wxString& action)
{
if (allowundo && !currlayer->stayclean) {
if (inscript) SavePendingChanges();
currlayer->undoredo->RememberSelection(action);
}
}
// -----------------------------------------------------------------------------
void PatternView::SelectAll()
{
SaveCurrentSelection();
if (SelectionExists()) {
currlayer->currsel.Deselect();
mainptr->UpdatePatternAndStatus();
}
if (currlayer->algo->isEmpty()) {
statusptr->ErrorMessage(empty_pattern);
RememberNewSelection(_("Deselection"));
return;
}
bigint top, left, bottom, right;
currlayer->algo->findedges(&top, &left, &bottom, &right);
currlayer->currsel.SetEdges(top, left, bottom, right);
RememberNewSelection(_("Select All"));
DisplaySelectionSize();
mainptr->UpdatePatternAndStatus();
}
// -----------------------------------------------------------------------------
void PatternView::RemoveSelection()
{
if (SelectionExists()) {
SaveCurrentSelection();
currlayer->currsel.Deselect();
RememberNewSelection(_("Deselection"));
mainptr->UpdatePatternAndStatus();
}
}
// -----------------------------------------------------------------------------
void PatternView::ShrinkSelection(bool fit)
{
currlayer->currsel.Shrink(fit);
}
// -----------------------------------------------------------------------------
void PatternView::RandomFill()
{
currlayer->currsel.RandomFill();
}
// -----------------------------------------------------------------------------
bool PatternView::FlipSelection(bool topbottom, bool inundoredo)
{
return currlayer->currsel.Flip(topbottom, inundoredo);
}
// -----------------------------------------------------------------------------
bool PatternView::RotateSelection(bool clockwise, bool inundoredo)
{
return currlayer->currsel.Rotate(clockwise, inundoredo);
}
// -----------------------------------------------------------------------------
void PatternView::SetCursorMode(wxCursor* cursor)
{
currlayer->curs = cursor;
}
// -----------------------------------------------------------------------------
void PatternView::CycleCursorMode()
{
if (drawingcells || selectingcells || movingview || waitingforclick)
return;
if (currlayer->curs == curs_pencil)
currlayer->curs = curs_cross;
else if (currlayer->curs == curs_cross)
currlayer->curs = curs_hand;
else if (currlayer->curs == curs_hand)
currlayer->curs = curs_zoomin;
else if (currlayer->curs == curs_zoomin)
currlayer->curs = curs_zoomout;
else
currlayer->curs = curs_pencil;
}
// -----------------------------------------------------------------------------
void PatternView::ZoomOut()
{
TestAutoFit();
currlayer->view->unzoom();
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::ZoomIn()
{
TestAutoFit();
if (currlayer->view->getmag() < MAX_MAG) {
currlayer->view->zoom();
mainptr->UpdateEverything();
} else {
wxBell();
}
}
// -----------------------------------------------------------------------------
void PatternView::SetPixelsPerCell(int pxlspercell)
{
int mag = 0;
while (pxlspercell > 1) {
mag++;
pxlspercell >>= 1;
}
if (mag == currlayer->view->getmag()) return;
TestAutoFit();
currlayer->view->setmag(mag);
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::FitPattern()
{
currlayer->algo->fit(*currlayer->view, 1);
// best not to call TestAutoFit
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::FitSelection()
{
if (!SelectionExists()) return;
currlayer->currsel.Fit();
TestAutoFit();
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::ViewOrigin()
{
// put 0,0 cell in middle of view
if ( currlayer->originx == bigint::zero && currlayer->originy == bigint::zero ) {
currlayer->view->center();
} else {
// put cell saved by ChangeOrigin in middle
currlayer->view->setpositionmag(currlayer->originx, currlayer->originy,
currlayer->view->getmag());
}
TestAutoFit();
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::ChangeOrigin()
{
if (waitingforclick) return;
// change cell under cursor to 0,0
wxPoint pt = ScreenToClient( wxGetMousePosition() );
if ( pt.x < 0 || pt.x > currlayer->view->getxmax() ||
pt.y < 0 || pt.y > currlayer->view->getymax() ) {
statusptr->ErrorMessage(_("Origin not changed."));
} else {
pair<bigint, bigint> cellpos = currlayer->view->at(pt.x, pt.y);
currlayer->originx = cellpos.first;
currlayer->originy = cellpos.second;
statusptr->DisplayMessage(_("Origin changed."));
if ( GridVisible() )
mainptr->UpdatePatternAndStatus();
else
statusptr->UpdateXYLocation();
}
}
// -----------------------------------------------------------------------------
void PatternView::RestoreOrigin()
{
if (waitingforclick) return;
if (currlayer->originx != bigint::zero || currlayer->originy != bigint::zero) {
currlayer->originx = 0;
currlayer->originy = 0;
statusptr->DisplayMessage(origin_restored);
if ( GridVisible() )
mainptr->UpdatePatternAndStatus();
else
statusptr->UpdateXYLocation();
}
}
// -----------------------------------------------------------------------------
bool PatternView::GridVisible()
{
return ( showgridlines && currlayer->view->getmag() >= mingridmag );
}
// -----------------------------------------------------------------------------
void PatternView::ToggleGridLines()
{
showgridlines = !showgridlines;
if ( (currlayer->view->getmag() >= mingridmag) ||
// also update everything if drawing all layers
(numlayers > 1 && (stacklayers || tilelayers))
)
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::ToggleCellColors()
{
swapcolors = !swapcolors;
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::ToggleBuffering()
{
buffered = !buffered;
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
bool PatternView::GetCellPos(bigint& xpos, bigint& ypos)
{
wxPoint pt = ScreenToClient( wxGetMousePosition() );
if (PointInView(pt.x, pt.y)) {
// get mouse location in cell coords
pair<bigint, bigint> cellpos = currlayer->view->at(pt.x, pt.y);
xpos = cellpos.first;
ypos = cellpos.second;
return true;
} else {
// mouse not in viewport
return false;
}
}
// -----------------------------------------------------------------------------
bool PatternView::PointInView(int x, int y)
{
return (x >= 0) && (x <= currlayer->view->getxmax()) &&
(y >= 0) && (y <= currlayer->view->getymax());
}
// -----------------------------------------------------------------------------
void PatternView::CheckCursor(bool active)
{
if (active) {
// make sure cursor is up to date
wxPoint pt = ScreenToClient( wxGetMousePosition() );
if (PointInView(pt.x, pt.y)) {
if (numlayers > 1 && tilelayers && tileindex != currindex) {
// show arrow cursor if over tile border (ie. bigview) or non-current tile
#ifdef __WXMAC__
// wxMac bug??? need this to fix probs after toggling status/tool bar
wxSetCursor(*wxSTANDARD_CURSOR);
#endif
SetCursor(*wxSTANDARD_CURSOR);
} else {
#ifdef __WXMAC__
// wxMac bug??? need this to fix probs after toggling status/tool bar
wxSetCursor(*currlayer->curs);
#endif
SetCursor(*currlayer->curs);
}
} else {
#ifdef __WXMAC__
wxSetCursor(*wxSTANDARD_CURSOR);
#endif
}
} else {
// main window is not active so don't change cursor
}
}
// -----------------------------------------------------------------------------
int PatternView::GetMag()
{
return currlayer->view->getmag();
}
// -----------------------------------------------------------------------------
void PatternView::SetMag(int mag)
{
TestAutoFit();
if (mag > MAX_MAG) mag = MAX_MAG;
currlayer->view->setmag(mag);
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::SetPosMag(const bigint& x, const bigint& y, int mag)
{
currlayer->view->setpositionmag(x, y, mag);
}
// -----------------------------------------------------------------------------
void PatternView::GetPos(bigint& x, bigint& y)
{
x = currlayer->view->x;
y = currlayer->view->y;
}
// -----------------------------------------------------------------------------
void PatternView::FitInView(int force)
{
currlayer->algo->fit(*currlayer->view, force);
}
// -----------------------------------------------------------------------------
int PatternView::CellVisible(const bigint& x, const bigint& y)
{
return currlayer->view->contains(x, y);
}
// -----------------------------------------------------------------------------
// scrolling functions:
void PatternView::PanUp(int amount)
{
TestAutoFit();
currlayer->view->move(0, -amount);
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::PanDown(int amount)
{
TestAutoFit();
currlayer->view->move(0, amount);
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::PanLeft(int amount)
{
TestAutoFit();
currlayer->view->move(-amount, 0);
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void PatternView::PanRight(int amount)
{
TestAutoFit();
currlayer->view->move(amount, 0);
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
int PatternView::SmallScroll(int xysize)
{
int amount;
int mag = currlayer->view->getmag();
if (mag > 0) {
// scroll an integral number of cells (1 cell = 2^mag pixels)
if (mag < 3) {
amount = ((xysize >> mag) / 20) << mag;
if (amount == 0) amount = 1 << mag;
return amount;
} else {
// grid lines are visible so scroll by only 1 cell
return 1 << mag;
}
} else {
// scroll by approx 5% of current wd/ht
amount = xysize / 20;
if (amount == 0) amount = 1;
return amount;
}
}
// -----------------------------------------------------------------------------
int PatternView::BigScroll(int xysize)
{
int amount;
int mag = currlayer->view->getmag();
if (mag > 0) {
// scroll an integral number of cells (1 cell = 2^mag pixels)
amount = ((xysize >> mag) * 9 / 10) << mag;
if (amount == 0) amount = 1 << mag;
return amount;
} else {
// scroll by approx 90% of current wd/ht
amount = xysize * 9 / 10;
if (amount == 0) amount = 1;
return amount;
}
}
// -----------------------------------------------------------------------------
void PatternView::UpdateScrollBars()
{
if (mainptr->fullscreen) return;
int viewwd, viewht;
int mag = currlayer->view->getmag();
if (mag > 0) {
// scroll by integral number of cells to avoid rounding probs
viewwd = currlayer->view->getwidth() >> mag;
viewht = currlayer->view->getheight() >> mag;
} else {
viewwd = currlayer->view->getwidth();
viewht = currlayer->view->getheight();
}
// keep thumb boxes in middle of scroll bars
hthumb = (thumbrange - 1) * viewwd / 2;
vthumb = (thumbrange - 1) * viewht / 2;
// only big viewport window has scroll bars
bigview->SetScrollbar(wxHORIZONTAL, hthumb, viewwd, thumbrange * viewwd, true);
bigview->SetScrollbar(wxVERTICAL, vthumb, viewht, thumbrange * viewht, true);
}
// -----------------------------------------------------------------------------
void PatternView::ProcessKey(int key, int modifiers)
{
mainptr->showbanner = false;
// WARNING: ProcessKey can be called while running a script or while
// waiting for a paste click, so we must avoid doing any actions that
// could cause havoc at such times.
action_info action = FindAction(key, modifiers);
switch (action.id) {
case DO_NOTHING:
// any unassigned key turns off full screen mode
if (mainptr->fullscreen) mainptr->ToggleFullScreen();
break;
case DO_OPENFILE:
{ wxString ext = action.file.AfterLast(wxT('.'));
if ( ext.IsSameAs(wxT("html"),false) || ext.IsSameAs(wxT("htm"),false) ) {
// show HTML file in help window
if (!waitingforclick) ShowHelp(action.file);
} else {
// load pattern or run script
if (!inscript) mainptr->OpenFile(action.file, true);
}
}
break;
// File menu actions
case DO_NEWPATT: if (!inscript) mainptr->NewPattern(); break;
case DO_OPENPATT: if (!inscript) mainptr->OpenPattern(); break;
case DO_OPENCLIP: if (!inscript) mainptr->OpenClipboard(); break;
case DO_SAVE: if (!inscript) mainptr->SavePattern(); break;
case DO_SAVEXRLE: if (!inscript) savexrle = !savexrle; break;
case DO_RUNSCRIPT: if (!inscript) mainptr->OpenScript(); break;
case DO_RUNCLIP: if (!inscript) mainptr->RunClipboard(); break;
case DO_PREFS: mainptr->ShowPrefsDialog(); break;
case DO_PATTERNS: mainptr->ToggleShowPatterns(); break;
case DO_PATTDIR: mainptr->ChangePatternDir(); break;
case DO_SCRIPTS: mainptr->ToggleShowScripts(); break;
case DO_SCRIPTDIR: mainptr->ChangeScriptDir(); break;
case DO_QUIT: mainptr->QuitApp(); break;
// Edit menu actions
case DO_UNDO: if (!inscript) currlayer->undoredo->UndoChange(); break;
case DO_REDO: if (!inscript) currlayer->undoredo->RedoChange(); break;
case DO_DISABLE: if (!inscript) mainptr->ToggleAllowUndo(); break;
case DO_CUT: if (!inscript) CutSelection(); break;
case DO_COPY: if (!inscript) CopySelection(); break;
case DO_CLEAR: if (!inscript) ClearSelection(); break;
case DO_CLEAROUT: if (!inscript) ClearOutsideSelection(); break;
case DO_PASTE: if (!inscript) PasteClipboard(false); break;
case DO_PASTESEL: if (!inscript) PasteClipboard(true); break;
case DO_SELALL: if (!inscript) SelectAll(); break;
case DO_REMOVESEL: if (!inscript) RemoveSelection(); break;
case DO_SHRINK: if (!inscript) ShrinkSelection(false); break;
case DO_SHRINKFIT: if (!inscript) ShrinkSelection(true); break;
case DO_RANDFILL: if (!inscript) RandomFill(); break;
case DO_FLIPTB: if (!inscript) FlipSelection(true); break;
case DO_FLIPLR: if (!inscript) FlipSelection(false); break;
case DO_ROTATECW: if (!inscript) RotateSelection(true); break;
case DO_ROTATEACW: if (!inscript) RotateSelection(false); break;
case DO_ADVANCE: if (!inscript) currlayer->currsel.Advance(); break;
case DO_ADVANCEOUT: if (!inscript) currlayer->currsel.AdvanceOutside(); break;
case DO_CURSDRAW: SetCursorMode(curs_pencil); break;
case DO_CURSSEL: SetCursorMode(curs_cross); break;
case DO_CURSMOVE: SetCursorMode(curs_hand); break;
case DO_CURSIN: SetCursorMode(curs_zoomin); break;
case DO_CURSOUT: SetCursorMode(curs_zoomout); break;
case DO_CURSCYCLE: CycleCursorMode(); break;
case DO_PASTEMODE: CyclePasteMode(); break;
case DO_PASTELOC: CyclePasteLocation(); break;
// Control menu actions
case DO_STARTSTOP:
if (!inscript) {
if (mainptr->generating) {
mainptr->Stop();
} else {
mainptr->GeneratePattern();
}
}
break;
case DO_NEXTGEN:
case DO_NEXTSTEP:
if (!inscript) {
if (mainptr->generating) {
mainptr->Stop();
} else {
mainptr->NextGeneration(action.id == DO_NEXTSTEP);
}
}
break;
case DO_RESET: if (!inscript) mainptr->ResetPattern(); break;
case DO_SETGEN: if (!inscript) mainptr->SetGeneration(); break;
case DO_FASTER: mainptr->GoFaster(); break;
case DO_SLOWER: mainptr->GoSlower(); break;
case DO_AUTOFIT: mainptr->ToggleAutoFit(); break;
case DO_HASHING: if (!inscript) mainptr->ToggleHashing(); break;
case DO_HYPER: mainptr->ToggleHyperspeed(); break;
case DO_HASHINFO: mainptr->ToggleHashInfo(); break;
case DO_RULE: if (!inscript) mainptr->ShowRuleDialog(); break;
case DO_TIMING: if (!inscript) mainptr->DisplayTimingInfo(); break;
// View menu actions
case DO_LEFT: PanLeft( SmallScroll(currlayer->view->getwidth()) ); break;
case DO_RIGHT: PanRight( SmallScroll(currlayer->view->getwidth()) ); break;
case DO_UP: PanUp( SmallScroll(currlayer->view->getheight()) ); break;
case DO_DOWN: PanDown( SmallScroll(currlayer->view->getheight()) ); break;
case DO_FULLSCREEN: mainptr->ToggleFullScreen(); break;
case DO_FIT: FitPattern(); break;
case DO_FITSEL: FitSelection(); break;
case DO_MIDDLE: ViewOrigin(); break;
case DO_CHANGE00: ChangeOrigin(); break;
case DO_RESTORE00: RestoreOrigin(); break;
case DO_ZOOMIN: ZoomIn(); break;
case DO_ZOOMOUT: ZoomOut(); break;
case DO_SCALE1: SetPixelsPerCell(1); break;
case DO_SCALE2: SetPixelsPerCell(2); break;
case DO_SCALE4: SetPixelsPerCell(4); break;
case DO_SCALE8: SetPixelsPerCell(8); break;
case DO_SCALE16: SetPixelsPerCell(16); break;
case DO_SHOWTOOL: mainptr->ToggleToolBar(); break;
case DO_SHOWLAYER: ToggleLayerBar(); break;
case DO_SHOWSTATUS: mainptr->ToggleStatusBar(); break;
case DO_SHOWEXACT: mainptr->ToggleExactNumbers(); break;
case DO_SHOWGRID: ToggleGridLines(); break;
case DO_SWAPCOLORS: ToggleCellColors(); break;
case DO_BUFFERED: ToggleBuffering(); break;
case DO_INFO: mainptr->ShowPatternInfo(); break;
// Layer menu actions
case DO_ADD: if (!inscript) AddLayer(); break;
case DO_CLONE: if (!inscript) CloneLayer(); break;
case DO_DUPLICATE: if (!inscript) DuplicateLayer(); break;
case DO_DELETE: if (!inscript) DeleteLayer(); break;
case DO_DELOTHERS: if (!inscript) DeleteOtherLayers(); break;
case DO_MOVELAYER: if (!inscript) MoveLayerDialog(); break;
case DO_NAMELAYER: if (!inscript) NameLayerDialog(); break;
case DO_SYNCVIEWS: if (!inscript) ToggleSyncViews(); break;
case DO_SYNCCURS: if (!inscript) ToggleSyncCursors(); break;
case DO_STACK: if (!inscript) ToggleStackLayers(); break;
case DO_TILE: if (!inscript) ToggleTileLayers(); break;
// Help menu actions
case DO_HELP:
if (!waitingforclick) {
// if help window is open then bring it to the front,
// otherwise open it and display most recent help file
ShowHelp(wxEmptyString);
}
break;
case DO_ABOUT: if (!inscript) ShowAboutBox(); break;
default: Warning(_("Bug detected in ProcessKey!"));
}
}
// -----------------------------------------------------------------------------
void PatternView::ShowDrawing()
{
currlayer->algo->endofpattern();
// update status bar
if (showstatus) statusptr->Refresh(false);
#if MAC_OS_X_VERSION_MIN_REQUIRED == 1030
// assume makefile-mac1039 has been used to build Golly,
// so use UpdateView to avoid wxMac bug on Mac OS 10.3.9
UpdateView();
#else
if (numlayers > 1 && (stacklayers || (numclones > 0 && tilelayers))) {
// update all layers; this is rather slow but most people won't be
// drawing cells when all layers are displayed
UpdateView();
}
#endif
MarkLayerDirty();
}
// -----------------------------------------------------------------------------
void PatternView::DrawOneCell(int cx, int cy, wxDC& dc)
{
// remember this cell for later undo/redo
if (allowundo) currlayer->undoredo->SaveCellChange(cx, cy);
#if MAC_OS_X_VERSION_MIN_REQUIRED == 1030
// assume makefile-mac1039 has been used to build Golly,
// so use UpdateView to avoid wxMac bug on Mac OS 10.3.9
return;
#else
if (numlayers > 1 && (stacklayers || (numclones > 0 && tilelayers))) {
// drawing must be done via UpdateView in ShowDrawing
return;
}
#endif
int cellsize = 1 << currlayer->view->getmag();
// convert given cell coords to view coords
pair<bigint, bigint> lefttop = currlayer->view->at(0, 0);
wxCoord x = (cx - lefttop.first.toint()) * cellsize;
wxCoord y = (cy - lefttop.second.toint()) * cellsize;
if (cellsize > 2) cellsize--; // allow for gap between cells
dc.DrawRectangle(x, y, cellsize, cellsize);
// overlay selection image if cell is within selection
if ( SelectionExists() && currlayer->currsel.ContainsCell(cx, cy) ) {
wxRect r = wxRect(x, y, cellsize, cellsize);
DrawSelection(dc, r);
}
}
// -----------------------------------------------------------------------------
void PatternView::StartDrawingCells(int x, int y)
{
pair<bigint, bigint> cellpos = currlayer->view->at(x, y);
// check that cellpos is within getcell/setcell limits
if ( OutsideLimits(cellpos.second, cellpos.first, cellpos.second, cellpos.first) ) {
statusptr->ErrorMessage(_("Drawing is not allowed outside +/- 10^9 boundary."));
return;
}
// ShowDrawing will call MarkLayerDirty so we need to save dirty state now
// for later use by RememberCellChanges
if (allowundo) currlayer->savedirty = currlayer->dirty;
cellx = cellpos.first.toint();
celly = cellpos.second.toint();
drawstate = 1 - currlayer->algo->getcell(cellx, celly);
currlayer->algo->setcell(cellx, celly, drawstate);
wxClientDC dc(this);
dc.SetPen(*wxTRANSPARENT_PEN);
dc.SetBrush(drawstate == (int)swapcolors ? *deadbrush : *livebrush[currindex]);
DrawOneCell(cellx, celly, dc);
dc.SetBrush(wxNullBrush); // restore brush
dc.SetPen(wxNullPen); // restore pen
ShowDrawing();
drawingcells = true;
CaptureMouse(); // get mouse up event even if outside view
dragtimer->Start(DRAG_RATE); // see OnDragTimer
if (stop_drawing) {
// mouse up event has already been seen so terminate drawing immediately
stop_drawing = false;
StopDraggingMouse();
}
}
// -----------------------------------------------------------------------------
void PatternView::DrawCells(int x, int y)
{
pair<bigint, bigint> cellpos = currlayer->view->at(x, y);
if ( currlayer->view->getmag() < 0 ||
OutsideLimits(cellpos.second, cellpos.first, cellpos.second, cellpos.first) ) {
return;
}
int newx = cellpos.first.toint();
int newy = cellpos.second.toint();
if ( newx != cellx || newy != celly ) {
wxClientDC dc(this);
dc.SetPen(*wxTRANSPARENT_PEN);
dc.SetBrush(drawstate == (int)swapcolors ? *deadbrush : *livebrush[currindex]);
int numchanged = 0;
// draw a line of cells using Bresenham's algorithm;
// this code comes from Guillermo Garcia's Life demo supplied with wx
int d, ii, jj, di, ai, si, dj, aj, sj;
di = newx - cellx;
ai = abs(di) << 1;
si = (di < 0)? -1 : 1;
dj = newy - celly;
aj = abs(dj) << 1;
sj = (dj < 0)? -1 : 1;
ii = cellx;
jj = celly;
lifealgo* curralgo = currlayer->algo;
if (ai > aj) {
d = aj - (ai >> 1);
while (ii != newx) {
if ( curralgo->getcell(ii, jj) != drawstate) {
curralgo->setcell(ii, jj, drawstate);
numchanged++;
DrawOneCell(ii, jj, dc);
}
if (d >= 0) {
jj += sj;
d -= ai;
}
ii += si;
d += aj;
}
} else {
d = ai - (aj >> 1);
while (jj != newy) {
if ( curralgo->getcell(ii, jj) != drawstate) {
curralgo->setcell(ii, jj, drawstate);
numchanged++;
DrawOneCell(ii, jj, dc);
}
if (d >= 0) {
ii += si;
d -= aj;
}
jj += sj;
d += ai;
}
}
cellx = newx;
celly = newy;
if ( curralgo->getcell(cellx, celly) != drawstate) {
curralgo->setcell(cellx, celly, drawstate);
numchanged++;
DrawOneCell(cellx, celly, dc);
}
dc.SetBrush(wxNullBrush); // restore brush
dc.SetPen(wxNullPen); // restore pen
if ( numchanged > 0 ) ShowDrawing();
}
}
// -----------------------------------------------------------------------------
void PatternView::StartSelectingCells(int x, int y, bool shiftdown)
{
pair<bigint, bigint> cellpos = currlayer->view->at(x, y);
anchorx = cellpos.first;
anchory = cellpos.second;
// save original selection so it can be restored if user hits escape;
// also used by RememberNewSelection
currlayer->savesel = currlayer->currsel;
// reset previous selection
prevsel.Deselect();
// for avoiding 1x1 selection if mouse doesn't move much
initselx = x;
initsely = y;
// allow changing size in any direction
forceh = false;
forcev = false;
if (SelectionExists()) {
if (shiftdown) {
// modify current selection
currlayer->currsel.Modify(cellpos.first, cellpos.second,
anchorx, anchory, &forceh, &forcev);
DisplaySelectionSize();
} else {
// remove current selection
currlayer->currsel.Deselect();
}
// allow mouse interaction if script is running
bool saveinscript = inscript;
inscript = false;
mainptr->UpdatePatternAndStatus();
inscript = saveinscript;
}
selectingcells = true;
CaptureMouse(); // get mouse up event even if outside view
dragtimer->Start(DRAG_RATE); // see OnDragTimer
}
// -----------------------------------------------------------------------------
void PatternView::SelectCells(int x, int y)
{
if ( abs(initselx - x) < 2 && abs(initsely - y) < 2 && !SelectionExists() ) {
// avoid 1x1 selection if mouse hasn't moved much
return;
}
pair<bigint, bigint> cellpos = currlayer->view->at(x, y);
if (!forcev) currlayer->currsel.SetLeftRight(cellpos.first, anchorx);
if (!forceh) currlayer->currsel.SetTopBottom(cellpos.second, anchory);
if (currlayer->currsel != prevsel) {
// selection has changed
DisplaySelectionSize();
// allow mouse interaction if script is running
bool saveinscript = inscript;
inscript = false;
mainptr->UpdatePatternAndStatus();
inscript = saveinscript;
prevsel = currlayer->currsel;
}
}
// -----------------------------------------------------------------------------
void PatternView::StartMovingView(int x, int y)
{
pair<bigint, bigint> cellpos = currlayer->view->at(x, y);
bigcellx = cellpos.first;
bigcelly = cellpos.second;
movingview = true;
CaptureMouse(); // get mouse up event even if outside view
dragtimer->Start(DRAG_RATE); // see OnDragTimer
}
// -----------------------------------------------------------------------------
void PatternView::MoveView(int x, int y)
{
pair<bigint, bigint> cellpos = currlayer->view->at(x, y);
bigint newx = cellpos.first;
bigint newy = cellpos.second;
bigint xdelta = bigcellx;
bigint ydelta = bigcelly;
xdelta -= newx;
ydelta -= newy;
int xamount, yamount;
int mag = currlayer->view->getmag();
if (mag >= 0) {
// move an integral number of cells
xamount = xdelta.toint() << mag;
yamount = ydelta.toint() << mag;
} else {
// convert cell deltas to screen pixels
xdelta >>= -mag;
ydelta >>= -mag;
xamount = xdelta.toint();
yamount = ydelta.toint();
}
if ( xamount != 0 || yamount != 0 ) {
currlayer->view->move(xamount, yamount);
// allow mouse interaction if script is running
bool saveinscript = inscript;
inscript = false;
mainptr->UpdatePatternAndStatus();
inscript = saveinscript;
cellpos = currlayer->view->at(x, y);
bigcellx = cellpos.first;
bigcelly = cellpos.second;
}
}
// -----------------------------------------------------------------------------
void PatternView::StopDraggingMouse()
{
if (selectingcells) {
if (allowundo) RememberNewSelection(_("Selection"));
selectingcells = false; // tested by CanUndo
mainptr->UpdateMenuItems(true); // enable various Edit menu items
}
if (drawingcells && allowundo) {
// MarkLayerDirty (in ShowDrawing) has set dirty flag, so we need to
// pass in the flag state saved before drawing started
currlayer->undoredo->RememberCellChanges(_("Drawing"), currlayer->savedirty);
drawingcells = false; // tested by CanUndo
mainptr->UpdateMenuItems(true); // enable Undo item
}
drawingcells = false;
selectingcells = false;
movingview = false;
if ( HasCapture() ) ReleaseMouse();
if ( dragtimer->IsRunning() ) dragtimer->Stop();
}
// -----------------------------------------------------------------------------
void PatternView::RestoreSelection()
{
currlayer->currsel = currlayer->savesel;
StopDraggingMouse();
// allow mouse interaction if script is running
bool saveinscript = inscript;
inscript = false;
mainptr->UpdatePatternAndStatus();
inscript = saveinscript;
statusptr->DisplayMessage(_("New selection aborted."));
}
// -----------------------------------------------------------------------------
void PatternView::TestAutoFit()
{
if (currlayer->autofit && mainptr->generating) {
// assume user no longer wants us to do autofitting
currlayer->autofit = false;
}
}
// -----------------------------------------------------------------------------
void PatternView::ZoomInPos(int x, int y)
{
// zoom in so that clicked cell stays under cursor
TestAutoFit();
if (currlayer->view->getmag() < MAX_MAG) {
currlayer->view->zoom(x, y);
// allow mouse interaction if script is running
bool saveinscript = inscript;
inscript = false;
mainptr->UpdateEverything();
inscript = saveinscript;
} else {
wxBell(); // can't zoom in any further
}
}
// -----------------------------------------------------------------------------
void PatternView::ZoomOutPos(int x, int y)
{
// zoom out so that clicked cell stays under cursor
TestAutoFit();
currlayer->view->unzoom(x, y);
// allow mouse interaction if script is running
bool saveinscript = inscript;
inscript = false;
mainptr->UpdateEverything();
inscript = saveinscript;
}
// -----------------------------------------------------------------------------
void PatternView::SetViewSize(int wd, int ht)
{
if (tileindex < 0) {
// use main viewport window's size to reset viewport in each layer
ResizeLayers(wd, ht);
}
// only autofit when generating
if (currlayer->autofit && mainptr && mainptr->generating)
currlayer->algo->fit(*currlayer->view, 0);
}
// -----------------------------------------------------------------------------
void PatternView::OnPaint(wxPaintEvent& WXUNUSED(event))
{
/* avoid unwanted drawing in certain situations???
if (ignorepaint) {
ignorepaint = false;
wxPaintDC dc(this);
return;
}
*/
int wd, ht;
GetClientSize(&wd, &ht);
// wd or ht might be < 1 on Win/X11 platforms
if (wd < 1) wd = 1;
if (ht < 1) ht = 1;
if ( numclones > 0 && numlayers > 1 && (stacklayers || tilelayers) )
SyncClones();
if ( numlayers > 1 && tilelayers ) {
if ( tileindex >= 0 && ( wd != GetLayer(tileindex)->view->getwidth() ||
ht != GetLayer(tileindex)->view->getheight() ) ) {
// might happen on Win/GTK???
GetLayer(tileindex)->view->resize(wd, ht);
}
} else if ( wd != currlayer->view->getwidth() || ht != currlayer->view->getheight() ) {
// need to change viewport size;
// can happen on Windows when resizing/maximizing main window
SetViewSize(wd, ht);
}
#if defined(__WXMAC__) || defined(__WXGTK__)
// windows on Mac OS X and GTK+ 2.0 are automatically buffered
wxPaintDC dc(this);
DrawView(dc, tileindex);
#else
if ( buffered || waitingforclick || GridVisible() || currlayer->currsel.Visible(NULL) ||
(numlayers > 1 && (stacklayers || tilelayers)) ) {
// use wxWidgets buffering to avoid flicker
if (wd != viewbitmapwd || ht != viewbitmapht) {
// need to create a new bitmap for current viewport
if (viewbitmap) delete viewbitmap;
viewbitmap = new wxBitmap(wd, ht);
if (viewbitmap == NULL) Fatal(_("Not enough memory to do buffering!"));
viewbitmapwd = wd;
viewbitmapht = ht;
}
wxBufferedPaintDC dc(this, *viewbitmap);
DrawView(dc, tileindex);
} else {
wxPaintDC dc(this);
DrawView(dc, tileindex);
}
#endif
}
// -----------------------------------------------------------------------------
void PatternView::OnSize(wxSizeEvent& event)
{
int wd, ht;
GetClientSize(&wd, &ht);
// wd or ht might be < 1 on Win/X11 platforms
if (wd < 1) wd = 1;
if (ht < 1) ht = 1;
// resize this viewport
SetViewSize(wd, ht);
event.Skip();
}
// -----------------------------------------------------------------------------
void PatternView::OnKeyDown(wxKeyEvent& event)
{
statusptr->ClearMessage();
realkey = event.GetKeyCode();
#if wxUSE_HOTKEY
int mods = event.GetModifiers();
#else
int mods = 0;
#endif
if (realkey == WXK_SHIFT) {
// pressing shift key temporarily toggles zoom in/out cursor;
// some platforms (eg. WinXP) send multiple key-down events while
// a key is pressed so we must be careful to toggle only once
if (currlayer->curs == curs_zoomin && oldzoom == NULL) {
oldzoom = curs_zoomin;
SetCursorMode(curs_zoomout);
mainptr->UpdateUserInterface(mainptr->IsActive());
} else if (currlayer->curs == curs_zoomout && oldzoom == NULL) {
oldzoom = curs_zoomout;
SetCursorMode(curs_zoomin);
mainptr->UpdateUserInterface(mainptr->IsActive());
}
}
if (debuglevel == 1) {
// set debugkey now but don't show it until OnChar
debugkey = wxString::Format(_("OnKeyDown: key=%d (%c) mods=%d"),
realkey, realkey < 128 ? wxChar(realkey) : wxChar('?'), mods);
}
// WARNING: logic must match that in KeyComboCtrl::OnKeyDown in wxprefs.cpp
#if wxUSE_HOTKEY
if (mods == wxMOD_NONE || realkey == WXK_ESCAPE || realkey > 127) {
// tell OnChar handler to ignore realkey
realkey = 0;
}
#endif
#ifdef __WXMSW__
// on Windows, OnChar is NOT called for some ctrl-key combos like
// ctrl-0..9 or ctrl-alt-key, so we call OnChar ourselves
if (realkey > 0 && (mods & wxMOD_CONTROL)) {
OnChar(event);
return;
}
#endif
#ifdef __WXMAC__
// allow option-E/I/N/U/` (OnChar is not called for those key combos
// although the prefs dialog KeyComboCtrl::OnChar *is* called)
if (mods == wxMOD_ALT && (realkey == 'E' || realkey == 'I' || realkey == 'N' ||
realkey == 'U' || realkey == '`')) {
OnChar(event);
return;
}
#endif
#ifdef __WXGTK__
if (realkey == ' ' && mods == wxMOD_SHIFT) {
// fix wxGTK bug (curiously, the bug isn't seen in the prefs dialog);
// OnChar won't see the shift modifier, so set realkey to a special
// value to tell OnChar that shift-space was pressed
realkey = -666;
}
#endif
event.Skip();
}
// -----------------------------------------------------------------------------
void PatternView::OnKeyUp(wxKeyEvent& event)
{
int key = event.GetKeyCode();
if (key == WXK_SHIFT) {
// releasing shift key sets zoom in/out cursor back to original state
if (oldzoom) {
SetCursorMode(oldzoom);
oldzoom = NULL;
mainptr->UpdateUserInterface(mainptr->IsActive());
}
}
event.Skip();
}
// -----------------------------------------------------------------------------
void PatternView::OnChar(wxKeyEvent& event)
{
// get translated keyboard event
int key = event.GetKeyCode();
#if wxUSE_HOTKEY
int mods = event.GetModifiers();
#else
int mods = 0;
#endif
if (debuglevel == 1) {
debugkey += wxString::Format(_("\nOnChar: key=%d (%c) mods=%d"),
key, key < 128 ? wxChar(key) : wxChar('?'), mods);
Warning(debugkey);
}
// WARNING: logic must match that in KeyComboCtrl::OnChar in wxprefs.cpp
#if wxUSE_HOTKEY
if (realkey > 0 && mods != wxMOD_NONE) {
#ifdef __WXGTK__
// sigh... wxGTK returns inconsistent results for shift-comma combos
// so we assume that '<' is produced by pressing shift-comma
// (which might only be true for US keyboards)
if (key == '<' && (mods & wxMOD_SHIFT)) realkey = ',';
#endif
#ifdef __WXMSW__
// sigh... wxMSW returns inconsistent results for some shift-key combos
// so again we assume we're using a US keyboard
if (key == '~' && (mods & wxMOD_SHIFT)) realkey = '`';
if (key == '+' && (mods & wxMOD_SHIFT)) realkey = '=';
#endif
if (mods == wxMOD_SHIFT && key != realkey) {
// use translated key code but remove shift key;
// eg. we want shift-'/' to be seen as '?'
mods = wxMOD_NONE;
} else {
// use key code seen by OnKeyDown
key = realkey;
if (key >= 'A' && key <= 'Z') key += 32; // convert A..Z to a..z
}
}
#endif
#ifdef __WXGTK__
if (realkey == -666) {
// OnKeyDown saw that shift-space was pressed but for some reason
// OnChar doesn't see the modifier (ie. mods is wxMOD_NONE)
key = ' ';
mods = wxMOD_SHIFT;
}
#endif
// do this check first because we allow user to make a selection while
// generating a pattern or running a script
if ( selectingcells && key == WXK_ESCAPE ) {
RestoreSelection();
return;
}
if (inscript) {
#ifdef __WXX11__
// sigh... pressing shift key by itself causes key = 306, control key = 308
// and other keys like caps lock and option = -1
if ( key < 0 || key > 255 ) return;
#endif
// let script decide what to do with the key
PassKeyToScript(key);
return;
}
// test waitingforclick before mainptr->generating so user can cancel
// a paste operation while generating
if ( waitingforclick && key == WXK_ESCAPE ) {
// cancel paste
pastex = -1;
pastey = -1;
waitingforclick = false;
return;
}
if ( mainptr->generating && key == WXK_ESCAPE ) {
mainptr->Stop();
return;
}
ProcessKey(key, mods);
mainptr->UpdateUserInterface(mainptr->IsActive());
}
// -----------------------------------------------------------------------------
void PatternView::ProcessControlClick(int x, int y)
{
if (currlayer->curs == curs_zoomin) {
ZoomOutPos(x, y);
} else if (currlayer->curs == curs_zoomout) {
ZoomInPos(x, y);
}
}
// -----------------------------------------------------------------------------
void PatternView::ProcessClick(int x, int y, bool shiftdown)
{
// user has clicked somewhere in viewport
if (currlayer->curs == curs_pencil) {
if (inscript) {
// statusptr->ErrorMessage does nothing if inscript is true
Warning(_("Drawing is not allowed while a script is running."));
return;
}
if (currlayer->view->getmag() < 0) {
statusptr->ErrorMessage(_("Drawing is not allowed at scales greater than 1 cell per pixel."));
return;
}
if (mainptr->generating) {
// we now allow drawing while generating
// statusptr->ErrorMessage(_("Drawing is not allowed while a pattern is generating."));
mainptr->Stop();
mainptr->draw_pending = true;
mainptr->mouseevent.m_x = x;
mainptr->mouseevent.m_y = y;
return;
}
StartDrawingCells(x, y);
} else if (currlayer->curs == curs_cross) {
TestAutoFit();
StartSelectingCells(x, y, shiftdown);
} else if (currlayer->curs == curs_hand) {
TestAutoFit();
StartMovingView(x, y);
} else if (currlayer->curs == curs_zoomin) {
ZoomInPos(x, y);
} else if (currlayer->curs == curs_zoomout) {
ZoomOutPos(x, y);
}
}
// -----------------------------------------------------------------------------
void PatternView::OnMouseDown(wxMouseEvent& event)
{
if (waitingforclick) {
// save paste location
pastex = event.GetX();
pastey = event.GetY();
waitingforclick = false;
} else {
statusptr->ClearMessage();
mainptr->showbanner = false;
if (numlayers > 1 && tilelayers && tileindex < 0) {
// ignore click in tile border
return;
}
if (tileindex >= 0 && tileindex != currindex) {
// switch current layer to clicked tile
SwitchToClickedTile(tileindex);
return;
}
#ifdef __WXX11__
// control-click is detected here rather than in OnRMouseDown
if ( event.ControlDown() ) {
ProcessControlClick(event.GetX(), event.GetY());
return;
}
#endif
ProcessClick(event.GetX(), event.GetY(), event.ShiftDown());
mainptr->UpdateUserInterface(mainptr->IsActive());
}
}
// -----------------------------------------------------------------------------
void PatternView::OnMouseUp(wxMouseEvent& WXUNUSED(event))
{
if (drawingcells || selectingcells || movingview) {
StopDraggingMouse();
} else if (mainptr->draw_pending) {
// this can happen if user does a quick click while pattern is generating,
// so set a special flag to force drawing to terminate
stop_drawing = true;
}
}
// -----------------------------------------------------------------------------
#if wxCHECK_VERSION(2, 8, 0)
// mouse capture can be lost on Windows before mouse-up event
void PatternView::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
{
if (drawingcells || selectingcells || movingview) {
StopDraggingMouse();
}
}
#endif
// -----------------------------------------------------------------------------
void PatternView::OnRMouseDown(wxMouseEvent& event)
{
// this is equivalent to control-click in wxMac/wxMSW but not in wxX11 -- sigh
statusptr->ClearMessage();
mainptr->showbanner = false;
if (numlayers > 1 && tilelayers && tileindex < 0) {
// ignore click in tile border
return;
}
if (tileindex >= 0 && tileindex != currindex) {
// switch current layer to clicked tile
SwitchToClickedTile(tileindex);
return;
}
ProcessControlClick(event.GetX(), event.GetY());
}
// -----------------------------------------------------------------------------
void PatternView::OnMouseWheel(wxMouseEvent& event)
{
// wheelpos should be persistent, because in theory we should keep track of
// the remainder if the amount scrolled was not an even number of deltas.
static int wheelpos = 0;
int delta;
if (mousewheelmode == 0) {
// ignore wheel, according to user preference
event.Skip();
return;
}
// delta is the amount that represents one "step" of rotation. Normally 120.
delta = event.GetWheelDelta();
if (mousewheelmode == 2)
wheelpos -= event.GetWheelRotation();
else
wheelpos += event.GetWheelRotation();
while (wheelpos >= delta) {
wheelpos -= delta;
TestAutoFit();
currlayer->view->unzoom();
}
while (wheelpos <= -delta) {
wheelpos += delta;
TestAutoFit();
if (currlayer->view->getmag() < MAX_MAG) {
currlayer->view->zoom();
} else {
wxBell();
break; // best not to beep lots of times
}
}
// allow mouse interaction if script is running
bool saveinscript = inscript;
inscript = false;
mainptr->UpdateEverything();
inscript = saveinscript;
}
// -----------------------------------------------------------------------------
void PatternView::OnMouseMotion(wxMouseEvent& WXUNUSED(event))
{
statusptr->CheckMouseLocation(mainptr->IsActive());
}
// -----------------------------------------------------------------------------
void PatternView::OnMouseEnter(wxMouseEvent& WXUNUSED(event))
{
// Win bug??? we don't get this event if CaptureMouse has been called
CheckCursor(mainptr->IsActive());
// no need to call CheckMouseLocation here (OnMouseMotion will be called)
}
// -----------------------------------------------------------------------------
void PatternView::OnMouseExit(wxMouseEvent& WXUNUSED(event))
{
// Win bug??? we don't get this event if CaptureMouse has been called
CheckCursor(mainptr->IsActive());
statusptr->CheckMouseLocation(mainptr->IsActive());
#ifdef __WXX11__
// make sure viewport keeps keyboard focus
if ( mainptr->IsActive() ) SetFocus();
#endif
}
// -----------------------------------------------------------------------------
void PatternView::OnDragTimer(wxTimerEvent& WXUNUSED(event))
{
// called periodically while drawing/selecting/moving
wxPoint pt = ScreenToClient( wxGetMousePosition() );
int x = pt.x;
int y = pt.y;
// don't test "!PointInView(x, y)" here -- we want to allow scrolling
// in full screen mode when mouse is at outer edge of view
if ( x <= 0 || x >= currlayer->view->getxmax() ||
y <= 0 || y >= currlayer->view->getymax() ) {
// user can disable scrolling
if ( drawingcells && !scrollpencil ) {
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > currlayer->view->getxmax()) x = currlayer->view->getxmax();
if (y > currlayer->view->getymax()) y = currlayer->view->getymax();
DrawCells(x, y);
return;
}
if ( selectingcells && !scrollcross ) {
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > currlayer->view->getxmax()) x = currlayer->view->getxmax();
if (y > currlayer->view->getymax()) y = currlayer->view->getymax();
SelectCells(x, y);
return;
}
if ( movingview && !scrollhand ) {
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > currlayer->view->getxmax()) x = currlayer->view->getxmax();
if (y > currlayer->view->getymax()) y = currlayer->view->getymax();
MoveView(x, y);
return;
}
// scroll view
int xamount = 0;
int yamount = 0;
if (x <= 0) xamount = -SmallScroll( currlayer->view->getwidth() );
if (y <= 0) yamount = -SmallScroll( currlayer->view->getheight() );
if (x >= currlayer->view->getxmax())
xamount = SmallScroll( currlayer->view->getwidth() );
if (y >= currlayer->view->getymax())
yamount = SmallScroll( currlayer->view->getheight() );
if ( drawingcells ) {
currlayer->view->move(xamount, yamount);
mainptr->UpdatePatternAndStatus();
} else if ( selectingcells ) {
currlayer->view->move(xamount, yamount);
// no need to call UpdatePatternAndStatus() here because
// it will be called soon in SelectCells, except in this case:
if (forceh || forcev) {
// selection might not change so must update pattern
RefreshView();
// need to update now if script is running
if (inscript) {
inscript = false;
mainptr->UpdatePatternAndStatus();
inscript = true;
}
}
} else if ( movingview ) {
// scroll in opposite direction, and if both amounts are non-zero then
// set both to same (larger) absolute value so user can scroll at 45 degrees
if ( xamount != 0 && yamount != 0 ) {
if ( abs(xamount) > abs(yamount) ) {
yamount = yamount < 0 ? -abs(xamount) : abs(xamount);
} else {
xamount = xamount < 0 ? -abs(yamount) : abs(yamount);
}
}
currlayer->view->move(-xamount, -yamount);
// allow mouse interaction if script is running
bool saveinscript = inscript;
inscript = false;
mainptr->UpdatePatternAndStatus();
inscript = saveinscript;
// adjust x,y and bigcellx,bigcelly for MoveView call below
x += xamount;
y += yamount;
pair<bigint, bigint> cellpos = currlayer->view->at(x, y);
bigcellx = cellpos.first;
bigcelly = cellpos.second;
}
}
if ( drawingcells ) {
// only draw cells within view
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > currlayer->view->getxmax()) x = currlayer->view->getxmax();
if (y > currlayer->view->getymax()) y = currlayer->view->getymax();
DrawCells(x, y);
} else if ( selectingcells ) {
// only select cells within view
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > currlayer->view->getxmax()) x = currlayer->view->getxmax();
if (y > currlayer->view->getymax()) y = currlayer->view->getymax();
SelectCells(x, y);
} else if ( movingview ) {
MoveView(x, y);
}
}
// -----------------------------------------------------------------------------
void PatternView::OnScroll(wxScrollWinEvent& event)
{
WXTYPE type = event.GetEventType();
int orient = event.GetOrientation();
if (type == wxEVT_SCROLLWIN_LINEUP) {
if (orient == wxHORIZONTAL)
PanLeft( SmallScroll(currlayer->view->getwidth()) );
else
PanUp( SmallScroll(currlayer->view->getheight()) );
} else if (type == wxEVT_SCROLLWIN_LINEDOWN) {
if (orient == wxHORIZONTAL)
PanRight( SmallScroll(currlayer->view->getwidth()) );
else
PanDown( SmallScroll(currlayer->view->getheight()) );
} else if (type == wxEVT_SCROLLWIN_PAGEUP) {
if (orient == wxHORIZONTAL)
PanLeft( BigScroll(currlayer->view->getwidth()) );
else
PanUp( BigScroll(currlayer->view->getheight()) );
} else if (type == wxEVT_SCROLLWIN_PAGEDOWN) {
if (orient == wxHORIZONTAL)
PanRight( BigScroll(currlayer->view->getwidth()) );
else
PanDown( BigScroll(currlayer->view->getheight()) );
} else if (type == wxEVT_SCROLLWIN_THUMBTRACK) {
int newpos = event.GetPosition();
int amount = newpos - (orient == wxHORIZONTAL ? hthumb : vthumb);
if (amount != 0) {
TestAutoFit();
if (currlayer->view->getmag() > 0) {
// amount is in cells so convert to pixels
amount = amount << currlayer->view->getmag();
}
if (orient == wxHORIZONTAL) {
hthumb = newpos;
currlayer->view->move(amount, 0);
// don't call UpdateEverything here because it calls UpdateScrollBars
RefreshView();
// don't Update() immediately -- more responsive, especially on X11
} else {
vthumb = newpos;
currlayer->view->move(0, amount);
// don't call UpdateEverything here because it calls UpdateScrollBars
RefreshView();
// don't Update() immediately -- more responsive, especially on X11
}
}
} else if (type == wxEVT_SCROLLWIN_THUMBRELEASE) {
// now we can call UpdateScrollBars
mainptr->UpdateEverything();
}
// need an update if script is running
if (inscript && type != wxEVT_SCROLLWIN_THUMBTRACK) {
inscript = false;
mainptr->UpdatePatternAndStatus();
bigview->UpdateScrollBars();
inscript = true;
}
}
// -----------------------------------------------------------------------------
void PatternView::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
{
// do nothing because we'll be painting the entire viewport
}
// -----------------------------------------------------------------------------
// create the viewport window
PatternView::PatternView(wxWindow* parent, wxCoord x, wxCoord y, int wd, int ht, long style)
: wxWindow(parent, wxID_ANY, wxPoint(x,y), wxSize(wd,ht), style)
{
dragtimer = new wxTimer(this, wxID_ANY);
if (dragtimer == NULL) Fatal(_("Failed to create drag timer!"));
// avoid erasing background on GTK+
SetBackgroundStyle(wxBG_STYLE_CUSTOM);
// force viewbitmap to be created in first OnPaint call
viewbitmap = NULL;
viewbitmapwd = -1;
viewbitmapht = -1;
drawingcells = false; // not drawing cells
selectingcells = false; // not selecting cells
movingview = false; // not moving view
waitingforclick = false; // not waiting for user to click
nopattupdate = false; // enable pattern updates
oldzoom = NULL; // not shift zooming
}
// -----------------------------------------------------------------------------
// destroy the viewport window
PatternView::~PatternView()
{
if (dragtimer) delete dragtimer;
if (viewbitmap) delete viewbitmap;
}
|