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
|
// SCCS Id: @(#)qt_win.cpp 3.4 1999/11/19
// Copyright (c) Warwick Allison, 1999.
// NetHack may be freely redistributed. See license for details.
// Qt Binding for NetHack 3.4
//
// Copyright (C) 1996-2001 by Warwick W. Allison (warwick@troll.no)
//
// Contributors:
// Michael Hohmuth <hohmuth@inf.tu-dresden.de>
// - Userid control
// Svante Gerhard <svante@algonet.se>
// - .nethackrc tile and font size settings
// Dirk Schoenberger <schoenberger@signsoft.com>
// - KDE support
// - SlashEm support
// and many others for bug reports.
//
// Unfortunately, this doesn't use Qt as well as I would like,
// primarily because NetHack is fundamentally a getkey-type program
// rather than being event driven (hence the ugly key and click buffer)
// and also because this is my first major application of Qt.
//
// The problem of NetHack's getkey requirement is solved by intercepting
// key events by overiding QApplicion::notify(...), and putting them in
// a buffer. Mouse clicks on the map window are treated with a similar
// buffer. When the NetHack engine calls for a key, one is taken from
// the buffer, or if that is empty, QApplication::enter_loop() is called.
// Whenever keys or clicks go into the buffer, QApplication::exit_loop()
// is called.
//
// Another problem is that some NetHack players are decade-long players who
// demand complete keyboard control (while Qt and X11 conspire to make this
// difficult by having widget-based focus rather than application based -
// a good thing in general). This problem is solved by again using the key
// event buffer.
//
// Out of all this hackery comes a silver lining however, as macros for
// the super-expert and menus for the ultra-newbie are also made possible
// by the key event buffer.
//
extern "C" {
// This includes all the definitions we need from the NetHack main
// engine. We pretend MSC is a STDC compiler, because C++ is close
// enough, and we undefine NetHack macros which conflict with Qt
// identifiers.
#define alloc hide_alloc // avoid treading on STL symbol
#define lock hide_lock // avoid treading on STL symbol
#ifdef _MSC_VER
#define NHSTDC
#endif
#include "hack.h"
#include "func_tab.h"
#include "dlb.h"
#include "date.h"
#include "patchlevel.h"
#include "tile2x11.h"
#undef Invisible
#undef Warning
#undef red
#undef green
#undef blue
#undef Black
#undef curs
#undef TRUE
#undef FALSE
#undef min
#undef max
#undef alloc
#undef lock
#undef yn
}
#include "qt_win.h"
#include <qregexp.h>
#include <qpainter.h>
#include <qdir.h>
#include <qbitmap.h>
#include <qkeycode.h>
#include <qmenubar.h>
#include <qpopupmenu.h>
#include <qlayout.h>
#include <qheader.h>
#include <qradiobutton.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qcombobox.h>
#include <qvbox.h>
#include <qdragobject.h>
#include <qtextbrowser.h>
#include <qhbox.h>
#include <qsignalmapper.h>
//#include <qgrid.h>
//#include <qlabelled.h>
#include <ctype.h>
#include "qt_clust.h"
#include "qt_xpms.h"
#include <dirent.h>
#ifdef Q_WS_MACX
# include <sys/malloc.h>
#else
# include <malloc.h>
#endif
#ifdef _WS_X11_
// For userid control
#include <unistd.h>
#endif
// Some distributors released Qt 2.1.0beta4
#if QT_VERSION < 220
# define nh_WX11BypassWM 0x01000000
#else
# define nh_WX11BypassWM WX11BypassWM
#endif
#ifdef USER_SOUNDS
# if QT_VERSION < 220
# undef USER_SOUNDS
# else
# include <qsound.h>
# endif
#endif
#ifdef USER_SOUNDS
extern "C" void play_sound_for_message(const char* str);
#endif
// Warwick prefers it this way...
#define QT_CHOOSE_RACE_FIRST
static const char nh_attribution[] = "<center><big>" DEF_GAME_NAME "</big>"
"<br><small>by the " DEF_GAME_NAME " DevTeam</small></center>";
static QString
aboutMsg()
{
QString msg;
msg.sprintf(
"Qt " DEF_GAME_NAME " is a version of " DEF_GAME_NAME " built\n"
#ifdef KDE
"using KDE and the Qt GUI toolkit.\n"
#else
"using the Qt GUI toolkit.\n"
#endif
"This is version " VERSION_STRING "\n\n"
"Homepage:\n http://avrc.city.ac.uk/slashem.html\n\n"
"Qt NetHack:\n http://trolls.troll.no/warwick/nethack\n"
#ifdef KDE
"KDE:\n http://www.kde.org\n"
#endif
"Qt:\n http://www.troll.no\n"
"Slash'EM:\n http://www.slashem.org/");
return msg;
}
static void
centerOnMain( QWidget* w )
{
QWidget* m = qApp->mainWidget();
if (!m) m = qApp->desktop();
QPoint p = m->mapToGlobal(QPoint(0,0));
w->move( p.x() + m->width()/2 - w->width()/2,
p.y() + m->height()/2 - w->height()/2 );
}
NetHackQtLineEdit::NetHackQtLineEdit() :
QLineEdit(0)
{
}
NetHackQtLineEdit::NetHackQtLineEdit(QWidget* parent, const char* name) :
QLineEdit(parent,name)
{
}
void NetHackQtLineEdit::fakeEvent(int key, int ascii, int state)
{
QKeyEvent fake(QEvent::KeyPress,key,ascii,state);
keyPressEvent(&fake);
}
extern "C" {
/* Used by tile/font-size patch below and in ../../src/files.c */
char *qt_tilewidth=NULL;
char *qt_tileheight=NULL;
char *qt_fontsize=NULL;
#if defined(QWS)
int qt_compact_mode = 1;
#else
int qt_compact_mode = 0;
#endif
extern const char *enc_stat[]; /* from botl.c */
extern const char *hu_stat[]; /* from eat.c */
extern const char *killed_by_prefix[];
extern int total_tiles_used; // from tile.c
extern int tiles_per_row; // from tile.c
extern int tiles_per_col; // from tile.c
extern short glyph2tile[]; // from tile.c
}
static int tilefile_tile_W=16;
static int tilefile_tile_H=16;
#define TILEWMIN 1
#define TILEHMIN 1
/* XPM */
static const char * nh_icon[] = {
"40 40 6 1",
" s None c none",
". c #ffffff",
"X c #dadab6",
"o c #916cb6",
"O c #6c476c",
"+ c #000000",
" ",
" ",
" ",
" . .X..XX.XX X ",
" .. .....X.XXXXXX XX ",
" ... ....X..XX.XXXXX XXX ",
" .. ..........X.XXXXXXXXXXX XX ",
" .... ........X..XX.XXXXXXXXX XXXX ",
" .... ..........X.XXXXXXXXXXX XXXX ",
" ooOOO..ooooooOooOOoOOOOOOOXX+++OO++ ",
" ooOOO..ooooooooOoOOOOOOOOOXX+++OO++ ",
" ....O..ooooooOooOOoOOOOOOOXX+XXXX++ ",
" ....O..ooooooooOoOOOOOOOOOXX+XXXX++ ",
" ..OOO..ooooooOooOOoOOOOOOOXX+++XX++ ",
" ++++..ooooooooOoOOOOOOOOOXX+++ +++ ",
" +++..ooooooOooOOoOOOOOOOXX+++ + ",
" ++..ooooooooOoOOOOOOOOOXX+++ ",
" ..ooooooOooOOoOOOOOOOXX+++ ",
" ..ooooooooOoOOOOOOOOOXX+++ ",
" ..ooooooOooOOoOOOOOOOXX+++ ",
" ..ooooooooOoOOOOOOOOOXX+++ ",
" ..oooooOooOOoOOOOOOXX+++ ",
" ..oooooooOoOOOOOOOOXX+++ ",
" ..ooooOooOOoOOOOOXX+++ ",
" ..ooooooOoOOOOOOOXX++++ ",
" ..o..oooOooOOoOOOOXX+XX+++ ",
" ...o..oooooOoOOOOOXX++XXX++ ",
" ....OO..ooOooOOoOOXX+++XXXX++ ",
" ...oo..+..oooOoOOOXX++XXooXXX++ ",
" ...ooo..++..OooOOoXX+++XXooOXXX+ ",
" ..oooOOXX+++....XXXX++++XXOOoOOXX+ ",
" ..oooOOXX+++ ...XXX+++++XXOOooOXX++ ",
" ..oooOXXX+++ ..XX+++ +XXOOooOXX++ ",
" .....XXX++++ XXXXXXX++ ",
" ....XX++++ XXXXXXX+ ",
" ...XX+++ XXXXX++ ",
" ",
" ",
" ",
" "};
/* XPM */
static const char * nh_icon_small[] = {
/* width height ncolors chars_per_pixel */
"16 16 16 1",
/* colors */
" c #587070",
". c #D1D5C9",
"X c #8B8C84",
"o c #2A2A28",
"O c #9AABA9",
"+ c #6A8FB2",
"@ c #C4CAC4",
"# c #B6BEB6",
"$ c None",
"% c #54564E",
"& c #476C6C",
"* c #ADB2AB",
"= c #ABABA2",
"- c #5E8295",
"; c #8B988F",
": c #E8EAE7",
/* pixels */
"$$$$$$$$$$$$$$$$",
"$$$.$#::.#==*$$$",
"$.*:::::....#*=$",
"$@#:..@#*==#;XX;",
"$@O:+++- &&; X%X",
"$#%.+++- &&;% oX",
"$$o.++-- &&;%%X$",
"$$$:++-- &&;%%$$",
"$$$.O++- &&=o $$",
"$$$=:++- & XoX$$",
"$$*:@O-- ;%Xo$$",
"$*:O#$+--;oOOX $",
"$:+ =o::=oo=-;%X",
"$::.%o$*;X;##@%$",
"$$@# ;$$$$$=*;X$",
"$$$$$$$$$$$$$$$$"
};
/* XPM */
static const char * map_xpm[] = {
"12 13 4 1",
". c None",
" c #000000000000",
"X c #0000B6DAFFFF",
"o c #69A69248B6DA",
" .",
" XXXXX ooo ",
" XoooX o ",
" XoooX o o ",
" XoooX ooo ",
" XXoXX o ",
" oooooXXX ",
" oo o oooX ",
" o XooX ",
" oooo XooX ",
" o o XXXX ",
" ",
". "};
/* XPM */
static const char * msg_xpm[] = {
"12 13 4 1",
". c None",
" c #FFFFFFFFFFFF",
"X c #69A69248B6DA",
"o c #000000000000",
" .",
" XXX XXX X o",
" o",
" XXXXX XX o",
" o",
" XX XXXXX o",
" o",
" XXXXXX o",
" o",
" XX XXX XX o",
" o",
" o",
".ooooooooooo"};
/* XPM */
static const char * stat_xpm[] = {
"12 13 5 1",
" c None",
". c #FFFF00000000",
"X c #000000000000",
"o c #FFFFFFFF0000",
"O c #69A6FFFF0000",
" ",
" ",
"... ",
"...X ",
"...X ... ",
"oooX oooX",
"oooXooo oooX",
"OOOXOOOXOOOX",
"OOOXOOOXOOOX",
"OOOXOOOXOOOX",
"OOOXOOOXOOOX",
"OOOXOOOXOOOX",
" XXXXXXXXXXX"};
/* XPM */
static const char * info_xpm[] = {
"12 13 4 1",
" c None",
". c #00000000FFFF",
"X c #FFFFFFFFFFFF",
"o c #000000000000",
" ... ",
" ....... ",
" ...XXX... ",
" .........o ",
"...XXXX.... ",
"....XXX....o",
"....XXX....o",
"....XXX....o",
" ...XXX...oo",
" ..XXXXX..o ",
" .......oo ",
" o...ooo ",
" ooo "};
/* XPM */
static const char * again_xpm[] = {
"12 13 2 1",
" c None",
". c #000000000000",
" .. ",
" .. ",
" ..... ",
" ....... ",
"... .. .. ",
".. .. .. ",
".. ..",
".. ..",
".. ..",
" .. .. ",
" .......... ",
" ...... ",
" "};
/* XPM */
static const char * kick_xpm[] = {
"12 13 3 1",
" c None",
". c #000000000000",
"X c #FFFF6DB60000",
" ",
" ",
" . . . ",
" ... . . ",
" ... . ",
" ... . ",
" ... ",
"XXX ... ",
"XXX. ... ",
"XXX. ... ",
"XXX. .. ",
" ... ",
" "};
/* XPM */
static const char * throw_xpm[] = {
"12 13 3 1",
" c None",
". c #FFFF6DB60000",
"X c #000000000000",
" ",
" ",
" ",
" ",
".... X ",
"....X X ",
"....X XXXXXX",
"....X X ",
" XXXX X ",
" ",
" ",
" ",
" "};
/* XPM */
static const char * fire_xpm[] = {
"12 13 5 1",
" c None",
". c #B6DA45140000",
"X c #FFFFB6DA9658",
"o c #000000000000",
"O c #FFFF6DB60000",
" . ",
" X. ",
" X . ",
" X .o ",
" X . o ",
" X .o o ",
"OOOOOOOOoooo",
" X .o o ",
" X . o o ",
" X .o ",
" X. o ",
" . o ",
" o "};
/* XPM */
static const char * get_xpm[] = {
"12 13 3 1",
" c None",
". c #000000000000",
"X c #FFFF6DB60000",
" ",
" . ",
" ... ",
" . . . ",
" . ",
" . ",
" ",
" XXXXX ",
" XXXXX. ",
" XXXXX. ",
" XXXXX. ",
" ..... ",
" "};
/* XPM */
static const char * drop_xpm[] = {
"12 13 3 1",
" c None",
". c #FFFF6DB60000",
"X c #000000000000",
" ",
" ..... ",
" .....X ",
" .....X ",
" .....X ",
" XXXXX ",
" ",
" X ",
" X ",
" X X X ",
" XXX ",
" X ",
" "};
/* XPM */
static const char * eat_xpm[] = {
"12 13 4 1",
" c None",
". c #000000000000",
"X c #FFFFB6DA9658",
"o c #FFFF6DB60000",
" .X. .. ",
" .X. .. ",
" .X. .. ",
" .X. .. ",
" ... .. ",
" .. .. ",
" .. .. ",
" oo oo ",
" oo oo ",
" oo oo ",
" oo oo ",
" oo oo ",
" oo oo "};
/* XPM */
static const char * rest_xpm[] = {
"12 13 2 1",
" c None",
". c #000000000000",
" ..... ",
" . ",
" . ",
" . ....",
" ..... . ",
" . ",
" ....",
" ",
" .... ",
" . ",
" . ",
" .... ",
" "};
/* XPM */
static const char * cast_a_xpm[] = {
"12 13 3 1",
" c None",
". c #FFFF6DB60000",
"X c #000000000000",
" . ",
" . ",
" .. ",
" .. ",
" .. . ",
" .. . ",
" ...... ",
" .. .. XX ",
" .. X X ",
" .. X X ",
" .. XXXX ",
" . X X ",
" . X X "};
/* XPM */
static const char * cast_b_xpm[] = {
"12 13 3 1",
" c None",
". c #FFFF6DB60000",
"X c #000000000000",
" . ",
" . ",
" .. ",
" .. ",
" .. . ",
" .. . ",
" ...... ",
" .. .. XXX ",
" .. X X ",
" .. XXX ",
" .. X X ",
" . X X ",
" . XXX "};
/* XPM */
static const char * cast_c_xpm[] = {
"12 13 3 1",
" c None",
". c #FFFF6DB60000",
"X c #000000000000",
" . ",
" . ",
" .. ",
" .. ",
" .. . ",
" .. . ",
" ...... ",
" .. .. XX ",
" .. X X ",
" .. X ",
" .. X ",
" . X X ",
" . XX "};
NetHackQtSettings::NetHackQtSettings(int w, int h) :
tilewidth(TILEWMIN,64,1,this),
tileheight(TILEHMIN,64,1,this),
widthlbl(&tilewidth,"&Width:",this),
heightlbl(&tileheight,"&Height:",this),
whichsize("&Zoomed",this),
fontsize(this),
normal("times"),
#ifdef WS_WIN
normalfixed("courier new"),
#else
normalfixed("fixed"),
#endif
large("times"),
theglyphs(0)
{
int default_fontsize;
if (w<=300) {
// ~240x320
default_fontsize=4;
tilewidth.setValue(8);
tileheight.setValue(12);
} else if (w<=700) {
// ~640x480
default_fontsize=3;
tilewidth.setValue(8);
tileheight.setValue(14);
} else if (w<=900) {
// ~800x600
default_fontsize=3;
tilewidth.setValue(10);
tileheight.setValue(17);
} else if (w<=1100) {
// ~1024x768
default_fontsize=2;
tilewidth.setValue(12);
tileheight.setValue(22);
} else if (w<=1200) {
// ~1152x900
default_fontsize=1;
tilewidth.setValue(14);
tileheight.setValue(26);
} else {
// ~1280x1024 and larger
default_fontsize=0;
tilewidth.setValue(16);
tileheight.setValue(30);
}
// Tile/font sizes read from .nethackrc
if (qt_tilewidth != NULL) {
tilewidth.setValue(atoi(qt_tilewidth));
free(qt_tilewidth);
}
if (qt_tileheight != NULL) {
tileheight.setValue(atoi(qt_tileheight));
free(qt_tileheight);
}
if (qt_fontsize != NULL) {
switch (tolower(qt_fontsize[0])) {
case 'h': default_fontsize = 0; break;
case 'l': default_fontsize = 1; break;
case 'm': default_fontsize = 2; break;
case 's': default_fontsize = 3; break;
case 't': default_fontsize = 4; break;
}
free(qt_fontsize);
}
theglyphs=new NetHackQtGlyphs();
resizeTiles();
connect(&tilewidth,SIGNAL(valueChanged(int)),this,SLOT(resizeTiles()));
connect(&tileheight,SIGNAL(valueChanged(int)),this,SLOT(resizeTiles()));
connect(&whichsize,SIGNAL(toggled(bool)),this,SLOT(setGlyphSize(bool)));
fontsize.insertItem("Huge");
fontsize.insertItem("Large");
fontsize.insertItem("Medium");
fontsize.insertItem("Small");
fontsize.insertItem("Tiny");
fontsize.setCurrentItem(default_fontsize);
connect(&fontsize,SIGNAL(activated(int)),this,SIGNAL(fontChanged()));
QGridLayout* grid = new QGridLayout(this, 5, 2, 8);
grid->addMultiCellWidget(&whichsize, 0, 0, 0, 1);
grid->addWidget(&tilewidth, 1, 1); grid->addWidget(&widthlbl, 1, 0);
grid->addWidget(&tileheight, 2, 1); grid->addWidget(&heightlbl, 2, 0);
QLabel* flabel=new QLabel(&fontsize, "&Font:",this);
grid->addWidget(flabel, 3, 0); grid->addWidget(&fontsize, 3, 1);
QPushButton* dismiss=new QPushButton("Dismiss",this);
dismiss->setDefault(TRUE);
grid->addMultiCellWidget(dismiss, 4, 4, 0, 1);
grid->setRowStretch(4,0);
grid->setColStretch(1,1);
grid->setColStretch(2,2);
grid->activate();
connect(dismiss,SIGNAL(clicked()),this,SLOT(accept()));
resize(150,140);
}
NetHackQtGlyphs& NetHackQtSettings::glyphs()
{
return *theglyphs;
}
void NetHackQtSettings::resizeTiles()
{
int w = tilewidth.value();
int h = tileheight.value();
theglyphs->setSize(w,h);
emit tilesChanged();
}
void NetHackQtSettings::toggleGlyphSize()
{
whichsize.toggle();
}
void NetHackQtSettings::setGlyphSize(bool which)
{
QSize n = QSize(tilewidth.value(),tileheight.value());
if ( othersize.isValid() ) {
tilewidth.blockSignals(TRUE);
tileheight.blockSignals(TRUE);
tilewidth.setValue(othersize.width());
tileheight.setValue(othersize.height());
tileheight.blockSignals(FALSE);
tilewidth.blockSignals(FALSE);
resizeTiles();
}
othersize = n;
}
const QFont& NetHackQtSettings::normalFont()
{
static int size[]={ 18, 14, 12, 10, 8 };
normal.setPointSize(size[fontsize.currentItem()]);
return normal;
}
const QFont& NetHackQtSettings::normalFixedFont()
{
static int size[]={ 18, 14, 13, 10, 8 };
normalfixed.setPointSize(size[fontsize.currentItem()]);
return normalfixed;
}
const QFont& NetHackQtSettings::largeFont()
{
static int size[]={ 24, 18, 14, 12, 10 };
large.setPointSize(size[fontsize.currentItem()]);
return large;
}
bool NetHackQtSettings::ynInMessages()
{
return !qt_compact_mode;
}
// Check to see if tile set has changed and update tiles if necessary. --ALI
void NetHackQtSettings::updateTiles()
{
if (strcmp(theglyphs->tileSet(),tileset)) {
delete theglyphs;
theglyphs=new NetHackQtGlyphs();
resizeTiles();
}
}
NetHackQtSettings* qt_settings;
NetHackQtKeyBuffer::NetHackQtKeyBuffer() :
in(0), out(0)
{
}
bool NetHackQtKeyBuffer::Empty() const { return in==out; }
bool NetHackQtKeyBuffer::Full() const { return (in+1)%maxkey==out; }
void NetHackQtKeyBuffer::Put(int k, int a, int state)
{
if ( Full() ) return; // Safety
key[in]=k;
ascii[in]=a;
in=(in+1)%maxkey;
}
void NetHackQtKeyBuffer::Put(char a)
{
Put(0,a,0);
}
void NetHackQtKeyBuffer::Put(const char* str)
{
while (*str) Put(*str++);
}
int NetHackQtKeyBuffer::GetKey()
{
if ( Empty() ) return 0;
int r=TopKey();
out=(out+1)%maxkey;
return r;
}
int NetHackQtKeyBuffer::GetAscii()
{
if ( Empty() ) return 0; // Safety
int r=TopAscii();
out=(out+1)%maxkey;
return r;
}
int NetHackQtKeyBuffer::GetState()
{
if ( Empty() ) return 0;
int r=TopState();
out=(out+1)%maxkey;
return r;
}
int NetHackQtKeyBuffer::TopKey() const
{
if ( Empty() ) return 0;
return key[out];
}
int NetHackQtKeyBuffer::TopAscii() const
{
if ( Empty() ) return 0;
return ascii[out];
}
int NetHackQtKeyBuffer::TopState() const
{
if ( Empty() ) return 0;
return state[out];
}
NetHackQtClickBuffer::NetHackQtClickBuffer() :
in(0), out(0)
{
}
bool NetHackQtClickBuffer::Empty() const { return in==out; }
bool NetHackQtClickBuffer::Full() const { return (in+1)%maxclick==out; }
void NetHackQtClickBuffer::Put(int x, int y, int mod)
{
click[in].x=x;
click[in].y=y;
click[in].mod=mod;
in=(in+1)%maxclick;
}
int NetHackQtClickBuffer::NextX() const { return click[out].x; }
int NetHackQtClickBuffer::NextY() const { return click[out].y; }
int NetHackQtClickBuffer::NextMod() const { return click[out].mod; }
void NetHackQtClickBuffer::Get()
{
out=(out+1)%maxclick;
}
class NhPSListViewItem : public QListViewItem {
public:
NhPSListViewItem( QListView* parent, const QString& name ) :
QListViewItem(parent, name)
{
}
void setGlyph(int g)
{
NetHackQtGlyphs& glyphs = qt_settings->glyphs();
int gw = glyphs.width();
int gh = glyphs.height();
QPixmap pm(gw,gh);
QPainter p(&pm);
glyphs.drawGlyph(p, g, 0, 0);
p.end();
setPixmap(0,pm);
setHeight(QMAX(pm.height()+1,height()));
}
void paintCell( QPainter *p, const QColorGroup &cg,
int column, int width, int alignment )
{
if ( isSelectable() ) {
QListViewItem::paintCell( p, cg, column, width, alignment );
} else {
QColorGroup disabled(
cg.foreground().light(),
cg.button().light(),
cg.light(), cg.dark(), cg.mid(),
gray, cg.base() );
QListViewItem::paintCell( p, disabled, column, width, alignment );
}
}
};
class NhPSListViewRole : public NhPSListViewItem {
public:
NhPSListViewRole( QListView* parent, int id ) :
NhPSListViewItem(parent,
#ifdef QT_CHOOSE_RACE_FIRST // Lowerize - looks better
QString(QChar(roles[id].name.m[0])).lower()+QString(roles[id].name.m+1)
#else
roles[id].name.m
#endif
)
{
setGlyph(monnum_to_glyph(roles[id].malenum));
}
};
class NhPSListViewRace : public NhPSListViewItem {
public:
NhPSListViewRace( QListView* parent, int id ) :
NhPSListViewItem(parent,
#ifdef QT_CHOOSE_RACE_FIRST // Capitalize - looks better
QString(QChar(races[id].noun[0])).upper()+QString(races[id].noun+1)
#else
QString(QChar(races[id].noun[0])+QString(races[id].noun+1))
#endif
)
{
setGlyph(monnum_to_glyph(races[id].malenum));
}
};
class NhPSListView : public QListView {
public:
NhPSListView( QWidget* parent ) :
QListView(parent)
{
setSorting(-1); // order is identity
header()->setClickEnabled(FALSE);
}
QSizePolicy sizePolicy() const
{
return QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
}
QSize minimumSizeHint() const
{
return sizeHint();
}
QSize sizeHint() const
{
QListView::sizeHint();
QSize sz = header()->sizeHint();
int h=0;
QListViewItem* c=firstChild();
while (c) h+=c->height(),c = c->nextSibling();
sz += QSize(frameWidth()*2, h+frameWidth()*2);
return sz;
}
int selectedItemNumber() const
{
int i=0;
QListViewItem* c = firstChild();
while (c) {
if (c == selectedItem()) {
return i;
}
i++;
c = c->nextSibling();
}
return -1;
}
void setSelectedItemNumber(int i)
{
QListViewItem* c=firstChild();
while (i--)
c = c->nextSibling();
c->setSelected(TRUE);
}
};
NetHackQtPlayerSelector::NetHackQtPlayerSelector(NetHackQtKeyBuffer& ks) :
QDialog(qApp->mainWidget(),"plsel",TRUE),
keysource(ks),
fully_specified_role(TRUE)
{
/*
0 1 2
+ Name ------------------------------------+
0 | |
+ ---- ------------------------------------+
+ Role ---+ + Race ---+ + Gender ------+
| | | | | * Male |
1 | | | | | * Female |
| | | | +--------------+
| | | |
| | | | + Alignment ---+
2 | | | | | * Male |
| | | | | * Female |
| | | | +--------------+
3 | | | | ...stretch...
| | | |
4 | | | | [ Play ]
5 | | | | [ Quit ]
+---------+ +---------+
*/
int marg=4;
QGridLayout *l = new QGridLayout(this,6,3,marg,marg);
QButtonGroup* namebox = new QButtonGroup(1,Horizontal,"Name",this);
QLineEdit* name = new QLineEdit(namebox);
name->setMaxLength(sizeof(plname)-1);
if ( strncmp(plname,"player",6) && strncmp(plname,"games",5) )
name->setText(plname);
connect(name, SIGNAL(textChanged(const QString&)),
this, SLOT(selectName(const QString&)) );
name->setFocus();
QButtonGroup* genderbox = new QButtonGroup("Sex",this);
QButtonGroup* alignbox = new QButtonGroup("Alignment",this);
QVBoxLayout* vbgb = new QVBoxLayout(genderbox,3,1);
vbgb->setAutoAdd(TRUE);
vbgb->addSpacing(fontMetrics().height()*3/4);
QVBoxLayout* vbab = new QVBoxLayout(alignbox,3,1);
vbab->setAutoAdd(TRUE);
vbab->addSpacing(fontMetrics().height());
QLabel* logo = new QLabel(nh_attribution, this);
l->addMultiCellWidget( namebox, 0,0,0,2 );
#ifdef QT_CHOOSE_RACE_FIRST
race = new NhPSListView(this);
role = new NhPSListView(this);
l->addMultiCellWidget( race, 1,5,0,0 );
l->addMultiCellWidget( role, 1,5,1,1 );
#else
role = new NhPSListView(this);
race = new NhPSListView(this);
l->addMultiCellWidget( role, 1,5,0,0 );
l->addMultiCellWidget( race, 1,5,1,1 );
#endif
role->addColumn("Role");
race->addColumn("Race");
l->addWidget( genderbox, 1, 2 );
l->addWidget( alignbox, 2, 2 );
l->addWidget( logo, 3, 2, AlignCenter );
l->setRowStretch( 3, 5 );
int i;
int nrole;
for (nrole=0; roles[nrole].name.m; nrole++)
;
for (i=nrole-1; i>=0; i--) { // XXX QListView unsorted goes in rev.
new NhPSListViewRole( role, i );
}
connect( role, SIGNAL(selectionChanged()), this, SLOT(selectRole()) );
int nrace;
for (nrace=0; races[nrace].noun; nrace++)
;
for (i=nrace-1; i>=0; i--) {
new NhPSListViewRace( race, i );
}
connect( race, SIGNAL(selectionChanged()), this, SLOT(selectRace()) );
gender = new QRadioButton*[ROLE_GENDERS];
for (i=0; i<ROLE_GENDERS; i++) {
gender[i] = new QRadioButton( genders[i].adj, genderbox );
}
connect( genderbox, SIGNAL(clicked(int)), this, SLOT(selectGender(int)) );
alignment = new QRadioButton*[ROLE_ALIGNS];
for (i=0; i<ROLE_ALIGNS; i++) {
alignment[i] = new QRadioButton( aligns[i].adj, alignbox );
}
connect( alignbox, SIGNAL(clicked(int)), this, SLOT(selectAlignment(int)) );
QPushButton* ok = new QPushButton("Play",this);
l->addWidget( ok, 4, 2 );
ok->setDefault(TRUE);
connect( ok, SIGNAL(clicked()), this, SLOT(accept()) );
QPushButton* cancel = new QPushButton("Quit",this);
l->addWidget( cancel, 5, 2 );
connect( cancel, SIGNAL(clicked()), this, SLOT(reject()) );
// Randomize race and role, unless specified in config
int ro = flags.initrole;
if (ro == ROLE_NONE || ro == ROLE_RANDOM) {
ro = rn2(nrole);
if (flags.initrole != ROLE_RANDOM) {
fully_specified_role = FALSE;
}
}
int ra = flags.initrace;
if (ra == ROLE_NONE || ra == ROLE_RANDOM) {
ra = rn2(nrace);
if (flags.initrace != ROLE_RANDOM) {
fully_specified_role = FALSE;
}
}
// make sure we have a valid combination, honoring
// the users request if possible.
bool choose_race_first;
#ifdef QT_CHOOSE_RACE_FIRST
choose_race_first = TRUE;
if (flags.initrole >= 0 && flags.initrace < 0) {
choose_race_first = FALSE;
}
#else
choose_race_first = FALSE;
if (flags.initrace >= 0 && flags.initrole < 0) {
choose_race_first = TRUE;
}
#endif
while (!validrace(ro,ra)) {
if (choose_race_first) {
ro = rn2(nrole);
if (flags.initrole != ROLE_RANDOM) {
fully_specified_role = FALSE;
}
} else {
ra = rn2(nrace);
if (flags.initrace != ROLE_RANDOM) {
fully_specified_role = FALSE;
}
}
}
int g = flags.initgend;
if (g == -1) {
g = rn2(ROLE_GENDERS);
fully_specified_role = FALSE;
}
while (!validgend(ro,ra,g)) {
g = rn2(ROLE_GENDERS);
}
gender[g]->setChecked(TRUE);
selectGender(g);
int a = flags.initalign;
if (a == -1) {
a = rn2(ROLE_ALIGNS);
fully_specified_role = FALSE;
}
while (!validalign(ro,ra,a)) {
a = rn2(ROLE_ALIGNS);
}
alignment[a]->setChecked(TRUE);
selectAlignment(a);
QListViewItem* li;
li = role->firstChild();
while (ro--) li=li->nextSibling();
role->setSelected(li,TRUE);
li = race->firstChild();
while (ra--) li=li->nextSibling();
race->setSelected(li,TRUE);
flags.initrace = race->selectedItemNumber();
flags.initrole = role->selectedItemNumber();
}
void NetHackQtPlayerSelector::selectName(const QString& n)
{
strncpy(plname,n.latin1(),sizeof(plname)-1);
}
void NetHackQtPlayerSelector::selectRole()
{
int ra = race->selectedItemNumber();
int ro = role->selectedItemNumber();
if (ra == -1 || ro == -1) return;
#ifdef QT_CHOOSE_RACE_FIRST
selectRace();
#else
QListViewItem* i=role->currentItem();
QListViewItem* valid=0;
int j;
NhPSListViewItem* item;
item = (NhPSListViewItem*)role->firstChild();
for (j=0; roles[j].name.m; j++) {
bool v = validrace(j,ra);
item->setSelectable(TRUE);
if ( !valid && v ) valid = item;
item=(NhPSListViewItem*)item->nextSibling();
}
if ( !validrace(role->selectedItemNumber(),ra) )
i = valid;
role->setSelected(i,TRUE);
item = (NhPSListViewItem*)role->firstChild();
for (j=0; roles[j].name.m; j++) {
bool v = validrace(j,ra);
item->setSelectable(v);
item->repaint();
item=(NhPSListViewItem*)item->nextSibling();
}
#endif
flags.initrole = role->selectedItemNumber();
setupOthers();
}
void NetHackQtPlayerSelector::selectRace()
{
int ra = race->selectedItemNumber();
int ro = role->selectedItemNumber();
if (ra == -1 || ro == -1) return;
#ifndef QT_CHOOSE_RACE_FIRST
selectRole();
#else
QListViewItem* i=race->currentItem();
QListViewItem* valid=0;
int j;
NhPSListViewItem* item;
item = (NhPSListViewItem*)race->firstChild();
for (j=0; races[j].noun; j++) {
bool v = validrace(ro,j);
item->setSelectable(TRUE);
if ( !valid && v ) valid = item;
item=(NhPSListViewItem*)item->nextSibling();
}
if ( !validrace(ro,race->selectedItemNumber()) )
i = valid;
race->setSelected(i,TRUE);
item = (NhPSListViewItem*)race->firstChild();
for (j=0; races[j].noun; j++) {
bool v = validrace(ro,j);
item->setSelectable(v);
item->repaint();
item=(NhPSListViewItem*)item->nextSibling();
}
#endif
flags.initrace = race->selectedItemNumber();
setupOthers();
}
void NetHackQtPlayerSelector::setupOthers()
{
int ro = role->selectedItemNumber();
int ra = race->selectedItemNumber();
int valid=-1;
int c=0;
int j;
for (j=0; j<ROLE_GENDERS; j++) {
bool v = validgend(ro,ra,j);
if ( gender[j]->isChecked() )
c = j;
gender[j]->setEnabled(v);
if ( valid<0 && v ) valid = j;
}
if ( !validgend(ro,ra,c) )
c = valid;
int k;
for (k=0; k<ROLE_GENDERS; k++) {
gender[k]->setChecked(c==k);
}
selectGender(c);
valid=-1;
for (j=0; j<ROLE_ALIGNS; j++) {
bool v = validalign(ro,ra,j);
if ( alignment[j]->isChecked() )
c = j;
alignment[j]->setEnabled(v);
if ( valid<0 && v ) valid = j;
}
if ( !validalign(ro,ra,c) )
c = valid;
for (k=0; k<ROLE_ALIGNS; k++) {
alignment[k]->setChecked(c==k);
}
selectAlignment(c);
}
void NetHackQtPlayerSelector::selectGender(int i)
{
flags.initgend = i;
}
void NetHackQtPlayerSelector::selectAlignment(int i)
{
flags.initalign = i;
}
void NetHackQtPlayerSelector::done(int i)
{
setResult(i);
qApp->exit_loop();
}
void NetHackQtPlayerSelector::Quit()
{
done(R_Quit);
qApp->exit_loop();
}
void NetHackQtPlayerSelector::Random()
{
done(R_Rand);
qApp->exit_loop();
}
bool NetHackQtPlayerSelector::Choose()
{
if (fully_specified_role) return TRUE;
#if defined(QWS) // probably safe with Qt 3, too (where show!=exec in QDialog).
if ( qt_compact_mode ) {
showMaximized();
} else
#endif
{
adjustSize();
centerOnMain(this);
}
if ( exec() ) {
return TRUE;
} else {
return FALSE;
}
}
NetHackQtStringRequestor::NetHackQtStringRequestor(NetHackQtKeyBuffer& ks, const char* p, const char* cancelstr) :
QDialog(qApp->mainWidget(),"string",FALSE),
prompt(p,this,"prompt"),
input(this,"input"),
keysource(ks)
{
cancel=new QPushButton(cancelstr,this);
connect(cancel,SIGNAL(clicked()),this,SLOT(reject()));
okay=new QPushButton("Okay",this);
connect(okay,SIGNAL(clicked()),this,SLOT(accept()));
connect(&input,SIGNAL(returnPressed()),this,SLOT(accept()));
okay->setDefault(TRUE);
setFocusPolicy(StrongFocus);
}
void NetHackQtStringRequestor::resizeEvent(QResizeEvent*)
{
const int margin=5;
const int gutter=5;
int h=(height()-margin*2-gutter);
if (strlen(prompt.text()) > 16) {
h/=3;
prompt.setGeometry(margin,margin,width()-margin*2,h);
input.setGeometry(width()*1/5,margin+h+gutter,
(width()-margin-2-gutter)*4/5,h);
} else {
h/=2;
prompt.setGeometry(margin,margin,(width()-margin*2-gutter)*2/5,h);
input.setGeometry(prompt.geometry().right()+gutter,margin,
(width()-margin-2-gutter)*3/5,h);
}
cancel->setGeometry(margin,input.geometry().bottom()+gutter,
(width()-margin*2-gutter)/2,h);
okay->setGeometry(cancel->geometry().right()+gutter,cancel->geometry().y(),
cancel->width(),h);
}
void NetHackQtStringRequestor::SetDefault(const char* d)
{
input.setText(d);
}
bool NetHackQtStringRequestor::Get(char* buffer, int maxchar)
{
input.setMaxLength(maxchar);
if (strlen(prompt.text()) > 16) {
resize(fontMetrics().width(prompt.text())+50,fontMetrics().height()*6);
} else {
resize(fontMetrics().width(prompt.text())*2+50,fontMetrics().height()*4);
}
centerOnMain(this);
show();
input.setFocus();
setResult(-1);
while (result()==-1) {
// Put keys in buffer (eg. from macros, from out-of-focus input)
if (!keysource.Empty()) {
while (!keysource.Empty()) {
int key=keysource.TopKey();
int ascii=keysource.TopAscii();
int state=keysource.GetState();
if (ascii=='\r' || ascii=='\n') {
// CR or LF in buffer causes confirmation
strcpy(buffer,input.text());
return TRUE;
} else if (ascii=='\033') {
return FALSE;
} else {
input.fakeEvent(key,ascii,state);
}
}
}
qApp->enter_loop();
}
// XXX Get rid of extra keys, since we couldn't get focus!
while (!keysource.Empty()) keysource.GetKey();
if (result()) {
strcpy(buffer,input.text());
return TRUE;
} else {
return FALSE;
}
}
void NetHackQtStringRequestor::done(int i)
{
setResult(i);
qApp->exit_loop();
}
NetHackQtWindow::NetHackQtWindow()
{
}
NetHackQtWindow::~NetHackQtWindow()
{
}
// XXX Use "expected ..." for now, abort or default later.
//
void NetHackQtWindow::Clear() { puts("unexpected Clear"); }
void NetHackQtWindow::Display(bool block) { puts("unexpected Display"); }
bool NetHackQtWindow::Destroy() { return TRUE; }
void NetHackQtWindow::CursorTo(int x,int y) { puts("unexpected CursorTo"); }
void NetHackQtWindow::PutStr(int attr, const char* text) { puts("unexpected PutStr"); }
void NetHackQtWindow::StartMenu() { puts("unexpected StartMenu"); }
void NetHackQtWindow::AddMenu(int glyph, const ANY_P* identifier, char ch, char gch, int attr,
const char* str, bool presel) { puts("unexpected AddMenu"); }
void NetHackQtWindow::EndMenu(const char* prompt) { puts("unexpected EndMenu"); }
int NetHackQtWindow::SelectMenu(int how, MENU_ITEM_P **menu_list) { puts("unexpected SelectMenu"); return 0; }
void NetHackQtWindow::ClipAround(int x,int y) { puts("unexpected ClipAround"); }
void NetHackQtWindow::PrintGlyph(int x,int y,int glyph) { puts("unexpected PrintGlyph"); }
//void NetHackQtWindow::PrintGlyphCompose(int x,int y,int,int) { puts("unexpected PrintGlyphCompose"); }
void NetHackQtWindow::UseRIP(int how) { puts("unexpected UseRIP"); }
// XXX Hmmm... crash after saving bones file if Map window is
// XXX deleted. Strange bug somewhere.
bool NetHackQtMapWindow::Destroy() { return FALSE; }
NetHackQtMapWindow::NetHackQtMapWindow(NetHackQtClickBuffer& click_sink) :
clicksink(click_sink),
change(10),
rogue_font(0)
{
viewport.addChild(this);
setBackgroundColor(black);
viewport.setBackgroundColor(black);
pet_annotation = QPixmap(qt_compact_mode ? pet_mark_small_xpm : pet_mark_xpm);
cursor.setX(0);
cursor.setY(0);
Clear();
connect(qt_settings,SIGNAL(tilesChanged()),this,SLOT(updateTiles()));
connect(&viewport, SIGNAL(contentsMoving(int,int)), this,
SLOT(moveMessages(int,int)));
updateTiles();
//setFocusPolicy(StrongFocus);
}
void NetHackQtMapWindow::moveMessages(int x, int y)
{
QRect u = messages_rect;
messages_rect.moveTopLeft(QPoint(x,y));
u |= messages_rect;
update(u);
}
void NetHackQtMapWindow::clearMessages()
{
messages = "";
update(messages_rect);
messages_rect = QRect();
}
void NetHackQtMapWindow::putMessage(int attr, const char* text)
{
if ( !messages.isEmpty() )
messages += "\n";
messages += text;
QFontMetrics fm = fontMetrics();
messages_rect = fm.boundingRect(viewport.contentsX(),viewport.contentsY(),viewport.width(),0, WordBreak|AlignTop|AlignLeft|DontClip, messages);
update(messages_rect);
}
void NetHackQtMapWindow::updateTiles()
{
NetHackQtGlyphs& glyphs = qt_settings->glyphs();
int gw = glyphs.width();
int gh = glyphs.height();
// Be exactly the size we want to be - full map...
resize(COLNO*gw,ROWNO*gh);
viewport.verticalScrollBar()->setSteps(gh,gh);
viewport.horizontalScrollBar()->setSteps(gw,gw);
/*
viewport.setMaximumSize(
gw*COLNO + viewport.verticalScrollBar()->width(),
gh*ROWNO + viewport.horizontalScrollBar()->height()
);
*/
viewport.updateScrollBars();
change.clear();
change.add(0,0,COLNO,ROWNO);
delete rogue_font; rogue_font = 0;
Display(FALSE);
emit resized();
}
NetHackQtMapWindow::~NetHackQtMapWindow()
{
// Remove from viewport porthole, since that is a destructible member.
viewport.removeChild(this);
recreate(0,0,QPoint(0,0));
}
QWidget* NetHackQtMapWindow::Widget()
{
return &viewport;
}
void NetHackQtMapWindow::Scroll(int dx, int dy)
{
if (viewport.horizontalScrollBar()->isVisible()) {
while (dx<0) { viewport.horizontalScrollBar()->subtractPage(); dx++; }
while (dx>0) { viewport.horizontalScrollBar()->addPage(); dx--; }
}
if (viewport.verticalScrollBar()->isVisible()) {
while (dy<0) { viewport.verticalScrollBar()->subtractPage(); dy++; }
while (dy>0) { viewport.verticalScrollBar()->addPage(); dy--; }
}
}
void NetHackQtMapWindow::Clear()
{
unsigned short stone=cmap_to_glyph(S_stone);
/*
* Tile set may have changed if tileset option changed via doset(). --ALI
*/
qt_settings->updateTiles();
for (int j=0; j<ROWNO; j++) {
for (int i=0; i<COLNO; i++) {
Glyph(i,j)=stone;
}
}
change.clear();
change.add(0,0,COLNO,ROWNO);
}
void NetHackQtMapWindow::clickCursor()
{
clicksink.Put(cursor.x(),cursor.y(),CLICK_1);
qApp->exit_loop();
}
void NetHackQtMapWindow::mousePressEvent(QMouseEvent* event)
{
clicksink.Put(
event->pos().x()/qt_settings->glyphs().width(),
event->pos().y()/qt_settings->glyphs().height(),
event->button()==LeftButton ? CLICK_1 : CLICK_2
);
qApp->exit_loop();
}
#ifdef TEXTCOLOR
static
const QPen& nhcolor_to_pen(int c)
{
static QPen* pen=0;
if ( !pen ) {
pen = new QPen[17];
pen[0] = Qt::black;
pen[1] = Qt::red;
pen[2] = QColor(0,191,0);
pen[3] = QColor(127,127,0);
pen[4] = Qt::blue;
pen[5] = Qt::magenta;
pen[6] = Qt::cyan;
pen[7] = Qt::gray;
pen[8] = Qt::white; // no color
pen[9] = QColor(255,127,0);
pen[10] = QColor(127,255,127);
pen[11] = Qt::yellow;
pen[12] = QColor(127,127,255);
pen[13] = QColor(255,127,255);
pen[14] = QColor(127,255,255);
pen[15] = Qt::white;
pen[16] = Qt::black;
}
return pen[c];
}
#endif
void NetHackQtMapWindow::paintEvent(QPaintEvent* event)
{
QRect area=event->rect();
QRect garea;
garea.setCoords(
QMAX(0,area.left()/qt_settings->glyphs().width()),
QMAX(0,area.top()/qt_settings->glyphs().height()),
QMIN(COLNO-1,area.right()/qt_settings->glyphs().width()),
QMIN(ROWNO-1,area.bottom()/qt_settings->glyphs().height())
);
QPainter painter;
painter.begin(this);
if (
#ifdef REINCARNATION
Is_rogue_level(&u.uz) ||
#endif
iflags.wc_ascii_map
)
{
// You enter a VERY primitive world!
painter.setClipRect( event->rect() ); // (normally we don't clip)
painter.fillRect( event->rect(), black );
if ( !rogue_font ) {
// Find font...
int pts = 5;
QString fontfamily = iflags.wc_font_map
? iflags.wc_font_map : "Courier";
bool bold = FALSE;
if ( fontfamily.right(5).lower() == "-bold" ) {
fontfamily.truncate(fontfamily.length()-5);
bold = TRUE;
}
while ( pts < 32 ) {
QFont f(fontfamily, pts, bold ? QFont::Bold : QFont::Normal);
painter.setFont(QFont(fontfamily, pts));
QFontMetrics fm = painter.fontMetrics();
if ( fm.width("M") > qt_settings->glyphs().width() )
break;
if ( fm.height() > qt_settings->glyphs().height() )
break;
pts++;
}
rogue_font = new QFont(fontfamily,pts-1);
}
painter.setFont(*rogue_font);
for (int j=garea.top(); j<=garea.bottom(); j++) {
for (int i=garea.left(); i<=garea.right(); i++) {
unsigned short g=Glyph(i,j);
uchar ch;
int color, och;
unsigned special;
painter.setPen( green );
/* map glyph to character and color */
mapglyph(g, &och, &color, &special, i, j);
ch = (uchar)och;
#ifdef TEXTCOLOR
painter.setPen( nhcolor_to_pen(color) );
#endif
painter.drawText(
i*qt_settings->glyphs().width(),
j*qt_settings->glyphs().height(),
qt_settings->glyphs().width(),
qt_settings->glyphs().height(),
AlignCenter,
(const char*)&ch, 1
);
if (glyph_is_pet(g)
#ifdef TEXTCOLOR
&& ::iflags.hilite_pet
#endif
) {
painter.drawPixmap(QPoint(i*qt_settings->glyphs().width(), j*qt_settings->glyphs().height()), pet_annotation);
}
}
}
painter.setFont(font());
} else {
for (int j=garea.top(); j<=garea.bottom(); j++) {
for (int i=garea.left(); i<=garea.right(); i++) {
unsigned short g=Glyph(i,j);
qt_settings->glyphs().drawCell(painter, g, i, j);
if (glyph_is_pet(g)
#ifdef TEXTCOLOR
&& ::iflags.hilite_pet
#endif
) {
painter.drawPixmap(QPoint(i*qt_settings->glyphs().width(), j*qt_settings->glyphs().height()), pet_annotation);
}
}
}
}
if (garea.contains(cursor)) {
#ifdef REINCARNATION
if (Is_rogue_level(&u.uz)) {
#ifdef TEXTCOLOR
painter.setPen( white );
#else
painter.setPen( green ); // REALLY primitive
#endif
} else
#endif
{
int hp100;
if (u.mtimedone) {
hp100=u.mhmax ? u.mh*100/u.mhmax : 100;
} else {
hp100=u.uhpmax ? u.uhp*100/u.uhpmax : 100;
}
if (hp100 > 75) painter.setPen(white);
else if (hp100 > 50) painter.setPen(yellow);
else if (hp100 > 25) painter.setPen(QColor(0xff,0xbf,0x00)); // orange
else if (hp100 > 10) painter.setPen(red);
else painter.setPen(magenta);
}
painter.drawRect(
cursor.x()*qt_settings->glyphs().width(),cursor.y()*qt_settings->glyphs().height(),
qt_settings->glyphs().width(),qt_settings->glyphs().height());
}
if (area.intersects(messages_rect)) {
painter.setPen(black);
painter.drawText(viewport.contentsX()+1,viewport.contentsY()+1,
viewport.width(),0, WordBreak|AlignTop|AlignLeft|DontClip, messages);
painter.setPen(white);
painter.drawText(viewport.contentsX(),viewport.contentsY(),
viewport.width(),0, WordBreak|AlignTop|AlignLeft|DontClip, messages);
}
painter.end();
}
void NetHackQtMapWindow::Display(bool block)
{
for (int i=0; i<change.clusters(); i++) {
const QRect& ch=change[i];
repaint(
ch.x()*qt_settings->glyphs().width(),
ch.y()*qt_settings->glyphs().height(),
ch.width()*qt_settings->glyphs().width(),
ch.height()*qt_settings->glyphs().height(),
FALSE
);
}
change.clear();
if (block) {
yn_function("Press a key when done viewing",0,'\0');
}
}
void NetHackQtMapWindow::CursorTo(int x,int y)
{
Changed(cursor.x(),cursor.y());
cursor.setX(x);
cursor.setY(y);
Changed(cursor.x(),cursor.y());
}
void NetHackQtMapWindow::PutStr(int attr, const char* text)
{
puts("unexpected PutStr in MapWindow");
}
void NetHackQtMapWindow::ClipAround(int x,int y)
{
// Convert to pixel of center of tile
x=x*qt_settings->glyphs().width()+qt_settings->glyphs().width()/2;
y=y*qt_settings->glyphs().height()+qt_settings->glyphs().height()/2;
// Then ensure that pixel is visible
viewport.center(x,y,0.45,0.45);
}
void NetHackQtMapWindow::PrintGlyph(int x,int y,int glyph)
{
Glyph(x,y)=glyph;
Changed(x,y);
}
//void NetHackQtMapWindow::PrintGlyphCompose(int x,int y,int glyph1, int glyph2)
//{
// TODO: composed graphics
//}
void NetHackQtMapWindow::Changed(int x, int y)
{
change.add(x,y);
}
class NetHackQtScrollText : public QTableView {
struct UData {
UData() : text(0), attr(0) { }
~UData() { if (text) free(text); }
char* text;
int attr;
};
public:
int uncleared;
NetHackQtScrollText(int maxlength) :
uncleared(0),
maxitems(maxlength),
first(0),
count(0),
item_cycle(maxlength)
{
setNumCols(1);
setCellWidth(200);
setCellHeight(fontMetrics().height());
setBackgroundColor(white);
setTableFlags(Tbl_vScrollBar
|Tbl_autoHScrollBar
|Tbl_clipCellPainting
|Tbl_smoothScrolling);
}
~NetHackQtScrollText()
{
}
void Scroll(int dx, int dy)
{
setXOffset(xOffset()+dx*viewWidth());
setYOffset(yOffset()+dy*viewHeight());
}
void insertItem(int attr, const char* text)
{
setTopCell(count);
setAutoUpdate(FALSE);
int i;
if (count<maxitems) {
i=count++;
setNumRows(count);
} else {
i=count-1;
first=(first+1)%maxitems;
}
item(i).attr=attr;
item(i).text=strdup(text);
int w=datumWidth(item(i));
if (w > cellWidth()) {
// Get wider.
setCellWidth(w);
}
setTopCell(count);
setAutoUpdate(TRUE);
if (viewHeight() >= totalHeight()-cellHeight()) {
repaint();
} else {
scroll(0,cellHeight());
}
}
virtual void setFont(const QFont& font)
{
QTableView::setFont(font);
setCellHeight(fontMetrics().height());
}
protected:
UData& item(int i)
{
return item_cycle[(first+i)%maxitems];
}
const int maxitems;
int first, count;
QArray<UData> item_cycle;
int datumWidth(const UData& uitem)
{
if (uitem.text) {
int width=fontMetrics().width(uitem.text)+3;
if (uitem.attr) {
// XXX Too expensive to do properly, because
// XXX we have to set the font of the widget
// XXX just to get the font metrics information!
// XXX Could hold a fake widget for that
// XXX purpose, but this hack is less ugly.
width+=width/10;
}
return width;
} else {
return 0;
}
}
virtual void setupPainter(QPainter *p)
{
// XXX This shouldn't be needed - we set the bg in the constructor.
p->setBackgroundColor(white);
}
virtual void paintCell(QPainter *p, int row, int col)
{
bool sel=FALSE;
UData& uitem=item(row);
if (!sel && row < count-uncleared) {
p->setPen(darkGray);
} else {
p->setPen(black);
}
if (uitem.attr) {
// XXX only bold
QFont bold(font().family(),font().pointSize(),QFont::Bold);
p->setFont(bold);
}
p->drawText(3, 0, cellWidth(), cellHeight(),
AlignLeft|AlignVCenter, uitem.text);
if (uitem.attr) {
p->setFont(font());
}
}
};
NetHackQtMessageWindow::NetHackQtMessageWindow() :
list(new NetHackQtScrollText(::iflags.msg_history))
{
::iflags.window_inited = 1;
map = 0;
connect(qt_settings,SIGNAL(fontChanged()),this,SLOT(updateFont()));
updateFont();
}
NetHackQtMessageWindow::~NetHackQtMessageWindow()
{
::iflags.window_inited = 0;
delete list;
}
QWidget* NetHackQtMessageWindow::Widget() { return list; }
void NetHackQtMessageWindow::setMap(NetHackQtMapWindow* m)
{
map = m;
updateFont();
}
void NetHackQtMessageWindow::updateFont()
{
list->setFont(qt_settings->normalFont());
if ( map )
map->setFont(qt_settings->normalFont());
}
void NetHackQtMessageWindow::Scroll(int dx, int dy)
{
list->Scroll(dx,dy);
}
void NetHackQtMessageWindow::Clear()
{
if ( map )
map->clearMessages();
if (list->uncleared) {
list->uncleared=0;
changed=TRUE;
Display(FALSE);
}
}
void NetHackQtMessageWindow::Display(bool block)
{
if (changed) {
list->repaint();
changed=FALSE;
}
}
void NetHackQtMessageWindow::PutStr(int attr, const char* text)
{
#ifdef USER_SOUNDS
play_sound_for_message(text);
#endif
changed=TRUE;
list->uncleared++;
list->insertItem(attr,text);
// Force scrollbar to bottom
// XXX list->setTopItem(list->count());
if ( map )
map->putMessage(attr, text);
}
NetHackQtLabelledIcon::NetHackQtLabelledIcon(QWidget* parent, const char* l) :
QWidget(parent),
low_is_good(FALSE),
prev_value(-123),
turn_count(-1),
label(new QLabel(l,this)),
icon(0)
{
initHighlight();
}
NetHackQtLabelledIcon::NetHackQtLabelledIcon(QWidget* parent, const char* l, const QPixmap& i) :
QWidget(parent),
low_is_good(FALSE),
prev_value(-123),
turn_count(-1),
label(new QLabel(l,this)),
icon(new QLabel(this))
{
setIcon(i);
initHighlight();
}
void NetHackQtLabelledIcon::initHighlight()
{
const QPalette& pal=palette();
const QColorGroup& pa=pal.normal();
//QColorGroup good(white,darkGreen,pa.light(),pa.dark(),pa.mid(),white,pa.base());
QColorGroup good(black,green,pa.light(),pa.dark(),pa.mid(),black,pa.base());
QColorGroup bad(white,red,pa.light(),pa.dark(),pa.mid(),white,pa.base());
hl_good=pal.copy();
hl_good.setNormal(good);
hl_good.setActive(good);
hl_bad=pal.copy();
hl_bad.setNormal(bad);
hl_bad.setActive(bad);
}
void NetHackQtLabelledIcon::setLabel(const char* t, bool lower)
{
if (!label) {
label=new QLabel(this);
label->setFont(font());
resizeEvent(0);
}
if (0!=strcmp(label->text(),t)) {
label->setText(t);
highlight(lower==low_is_good ? hl_good : hl_bad);
}
}
void NetHackQtLabelledIcon::setLabel(const char* t, long v, long cv, const char* tail)
{
char buf[BUFSZ];
if (v==NoNum) {
Sprintf(buf,"%s%s",t,tail);
} else {
Sprintf(buf,"%s%ld%s",t,v,tail);
}
setLabel(buf,cv<prev_value);
prev_value=cv;
}
void NetHackQtLabelledIcon::setLabel(const char* t, long v, const char* tail)
{
setLabel(t,v,v,tail);
}
void NetHackQtLabelledIcon::setIcon(const QPixmap& i)
{
if (icon) icon->setPixmap(i);
else { icon=new QLabel(this); icon->setPixmap(i); resizeEvent(0); }
icon->resize(i.width(),i.height());
}
void NetHackQtLabelledIcon::setFont(const QFont& f)
{
QWidget::setFont(f);
if (label) label->setFont(f);
}
void NetHackQtLabelledIcon::show()
{
#if QT_VERSION >= 300
if (isHidden())
#else
if (!isVisible())
#endif
highlight(hl_bad);
QWidget::show();
}
void NetHackQtLabelledIcon::highlightWhenChanging()
{
turn_count=0;
}
void NetHackQtLabelledIcon::lowIsGood()
{
low_is_good=TRUE;
}
void NetHackQtLabelledIcon::dissipateHighlight()
{
if (turn_count>0) {
turn_count--;
if (!turn_count)
unhighlight();
}
}
void NetHackQtLabelledIcon::highlight(const QPalette& hl)
{
if (label) { // Surely it is?!
if (turn_count>=0) {
label->setPalette(hl);
turn_count=4;
// `4' includes this turn, so dissipates after
// 3 more keypresses.
} else {
label->setPalette(palette());
}
}
}
void NetHackQtLabelledIcon::unhighlight()
{
if (label) { // Surely it is?!
label->setPalette(palette());
}
}
void NetHackQtLabelledIcon::resizeEvent(QResizeEvent*)
{
setAlignments();
//int labw=label ? label->fontMetrics().width(label->text()) : 0;
int labh=label ? label->fontMetrics().height() : 0;
int icoh=icon ? icon->height() : 0;
int h=icoh+labh;
int icoy=(h>height() ? height()-labh-icoh : height()/2-h/2);
int laby=icoy+icoh;
if (icon) {
icon->setGeometry(0,icoy,width(),icoh);
}
if (label) {
label->setGeometry(0,laby,width(),labh);
}
}
void NetHackQtLabelledIcon::setAlignments()
{
if (label) label->setAlignment(AlignHCenter|AlignVCenter);
if (icon) icon->setAlignment(AlignHCenter|AlignVCenter);
}
static void
tryload(QPixmap& pm, const char* fn)
{
#ifndef FILE_AREAS
const char *filename = fn;
#else
char *filename = make_file_name(FILE_AREA_SHARE, fn);
#endif
if (!pm.load(filename)) {
QString msg;
msg.sprintf("Cannot load \"%s\"", fn);
QMessageBox::warning(qApp->mainWidget(), "IO Error", msg);
}
#ifdef FILE_AREAS
free(filename);
#endif
}
NetHackQtStatusWindow::NetHackQtStatusWindow() :
// Notes:
// Alignment needs -2 init value, because -1 is an alignment.
// Armor Class is an schar, so 256 is out of range.
// Blank value is 0 and should never change.
name(this,"(name)"),
dlevel(this,"(dlevel)"),
str(this,"STR"),
dex(this,"DEX"),
con(this,"CON"),
intel(this,"INT"),
wis(this,"WIS"),
cha(this,"CHA"),
gold(this,"Gold"),
hp(this,"Hit Points"),
power(this,"Power"),
ac(this,"Armour Class"),
level(this,"Level"),
exp(this,"Experience"),
align(this,"Alignment"),
time(this,"Time"),
score(this,"Score"),
weight(this,"Weight"),
hunger(this,""),
confused(this,"Confused"),
sick_fp(this,"Sick"),
sick_il(this,"Ill"),
blind(this,"Blind"),
stunned(this,"Stunned"),
hallu(this,"Hallu"),
encumber(this,""),
hline1(this),
hline2(this),
hline3(this),
first_set(TRUE)
{
p_str = QPixmap(str_xpm);
p_str = QPixmap(str_xpm);
p_dex = QPixmap(dex_xpm);
p_con = QPixmap(cns_xpm);
p_int = QPixmap(int_xpm);
p_wis = QPixmap(wis_xpm);
p_cha = QPixmap(cha_xpm);
p_chaotic = QPixmap(chaotic_xpm);
p_neutral = QPixmap(neutral_xpm);
p_lawful = QPixmap(lawful_xpm);
p_satiated = QPixmap(satiated_xpm);
p_hungry = QPixmap(hungry_xpm);
p_confused = QPixmap(confused_xpm);
p_sick_fp = QPixmap(sick_fp_xpm);
p_sick_il = QPixmap(sick_il_xpm);
p_blind = QPixmap(blind_xpm);
p_stunned = QPixmap(stunned_xpm);
p_hallu = QPixmap(hallu_xpm);
p_encumber[0] = QPixmap(slt_enc_xpm);
p_encumber[1] = QPixmap(mod_enc_xpm);
p_encumber[2] = QPixmap(hvy_enc_xpm);
p_encumber[3] = QPixmap(ext_enc_xpm);
p_encumber[4] = QPixmap(ovr_enc_xpm);
str.setIcon(p_str);
dex.setIcon(p_dex);
con.setIcon(p_con);
intel.setIcon(p_int);
wis.setIcon(p_wis);
cha.setIcon(p_cha);
align.setIcon(p_neutral);
hunger.setIcon(p_hungry);
confused.setIcon(p_confused);
sick_fp.setIcon(p_sick_fp);
sick_il.setIcon(p_sick_il);
blind.setIcon(p_blind);
stunned.setIcon(p_stunned);
hallu.setIcon(p_hallu);
encumber.setIcon(p_encumber[0]);
hline1.setFrameStyle(QFrame::HLine|QFrame::Sunken);
hline2.setFrameStyle(QFrame::HLine|QFrame::Sunken);
hline3.setFrameStyle(QFrame::HLine|QFrame::Sunken);
hline1.setLineWidth(1);
hline2.setLineWidth(1);
hline3.setLineWidth(1);
connect(qt_settings,SIGNAL(fontChanged()),this,SLOT(doUpdate()));
doUpdate();
}
void NetHackQtStatusWindow::doUpdate()
{
const QFont& large=qt_settings->largeFont();
name.setFont(large);
dlevel.setFont(large);
const QFont& normal=qt_settings->normalFont();
str.setFont(normal);
dex.setFont(normal);
con.setFont(normal);
intel.setFont(normal);
wis.setFont(normal);
cha.setFont(normal);
gold.setFont(normal);
hp.setFont(normal);
power.setFont(normal);
ac.setFont(normal);
level.setFont(normal);
exp.setFont(normal);
align.setFont(normal);
time.setFont(normal);
score.setFont(normal);
weight.setFont(normal);
hunger.setFont(normal);
confused.setFont(normal);
sick_fp.setFont(normal);
sick_il.setFont(normal);
blind.setFont(normal);
stunned.setFont(normal);
hallu.setFont(normal);
encumber.setFont(normal);
updateStats();
}
QWidget* NetHackQtStatusWindow::Widget() { return this; }
void NetHackQtStatusWindow::Clear()
{
}
void NetHackQtStatusWindow::Display(bool block)
{
}
void NetHackQtStatusWindow::CursorTo(int,int y)
{
cursy=y;
}
void NetHackQtStatusWindow::PutStr(int attr, const char* text)
{
// do a complete update when line 0 is done (as per X11 fancy status)
if (cursy==0) updateStats();
}
void NetHackQtStatusWindow::resizeEvent(QResizeEvent*)
{
const float SP_name=0.13; // <Name> the <Class> (large)
const float SP_dlev=0.13; // Level 3 in The Dungeons of Doom (large)
const float SP_atr1=0.25; // STR DEX CON INT WIS CHA
const float SP_hln1=0.02; // ---
const float SP_atr2=0.09; // Au HP PW AC LVL EXP
const float SP_hln2=0.02; // ---
const float SP_time=0.09; // time score weight
const float SP_hln3=0.02; // ---
const float SP_stat=0.25; // Alignment, Poisoned, Hungry, Sick, etc.
int h=height();
int x=0,y=0;
int iw; // Width of an item across line
int lh; // Height of a line of values
lh=int(h*SP_name);
name.setGeometry(0,0,width(),lh); y+=lh;
lh=int(h*SP_dlev);
dlevel.setGeometry(0,y,width(),lh); y+=lh;
lh=int(h*SP_hln1);
hline1.setGeometry(0,y,width(),lh); y+=lh;
lh=int(h*SP_atr1);
iw=width()/6;
str.setGeometry(x,y,iw,lh); x+=iw;
dex.setGeometry(x,y,iw,lh); x+=iw;
con.setGeometry(x,y,iw,lh); x+=iw;
intel.setGeometry(x,y,iw,lh); x+=iw;
wis.setGeometry(x,y,iw,lh); x+=iw;
cha.setGeometry(x,y,iw,lh); x+=iw;
x=0; y+=lh;
lh=int(h*SP_hln2);
hline2.setGeometry(0,y,width(),lh); y+=lh;
lh=int(h*SP_atr2);
iw=width()/6;
gold.setGeometry(x,y,iw,lh); x+=iw;
hp.setGeometry(x,y,iw,lh); x+=iw;
power.setGeometry(x,y,iw,lh); x+=iw;
ac.setGeometry(x,y,iw,lh); x+=iw;
level.setGeometry(x,y,iw,lh); x+=iw;
exp.setGeometry(x,y,iw,lh); x+=iw;
x=0; y+=lh;
lh=int(h*SP_hln3);
hline3.setGeometry(0,y,width(),lh); y+=lh;
lh=int(h*SP_time);
iw=width()/4; x+=iw/2;
time.setGeometry(x,y,iw,lh); x+=iw;
score.setGeometry(x,y,iw,lh); x+=iw;
weight.setGeometry(x,y,iw,lh); x+=iw;
x=0; y+=lh;
lh=int(h*SP_stat);
iw=width()/9;
align.setGeometry(x,y,iw,lh); x+=iw;
hunger.setGeometry(x,y,iw,lh); x+=iw;
confused.setGeometry(x,y,iw,lh); x+=iw;
sick_fp.setGeometry(x,y,iw,lh); x+=iw;
sick_il.setGeometry(x,y,iw,lh); x+=iw;
blind.setGeometry(x,y,iw,lh); x+=iw;
stunned.setGeometry(x,y,iw,lh); x+=iw;
hallu.setGeometry(x,y,iw,lh); x+=iw;
encumber.setGeometry(x,y,iw,lh); x+=iw;
x=0; y+=lh;
}
/*
* Set all widget values to a null string. This is used after all spacings
* have been calculated so that when the window is popped up we don't get all
* kinds of funny values being displayed.
*/
void NetHackQtStatusWindow::nullOut()
{
}
void NetHackQtStatusWindow::fadeHighlighting()
{
name.dissipateHighlight();
dlevel.dissipateHighlight();
str.dissipateHighlight();
dex.dissipateHighlight();
con.dissipateHighlight();
intel.dissipateHighlight();
wis.dissipateHighlight();
cha.dissipateHighlight();
gold.dissipateHighlight();
hp.dissipateHighlight();
power.dissipateHighlight();
ac.dissipateHighlight();
level.dissipateHighlight();
exp.dissipateHighlight();
align.dissipateHighlight();
time.dissipateHighlight();
score.dissipateHighlight();
weight.dissipateHighlight();
hunger.dissipateHighlight();
confused.dissipateHighlight();
sick_fp.dissipateHighlight();
sick_il.dissipateHighlight();
blind.dissipateHighlight();
stunned.dissipateHighlight();
hallu.dissipateHighlight();
encumber.dissipateHighlight();
}
/*
* Update the displayed status. The current code in botl.c updates
* two lines of information. Both lines are always updated one after
* the other. So only do our update when we update the second line.
*
* Information on the first line:
* name, attributes, alignment, score
*
* Information on the second line:
* dlvl, gold, hp, power, ac, {level & exp or HD **}
* status (hunger, conf, halu, stun, sick, blind), time, weight, encumbrance
*
* [**] HD is shown instead of level and exp if mtimedone is non-zero.
*/
void NetHackQtStatusWindow::updateStats()
{
if (!parentWidget()) return;
char buf[BUFSZ];
if (cursy != 0) return; /* do a complete update when line 0 is done */
if (ACURR(A_STR) > 118) {
Sprintf(buf,"STR:%d",ACURR(A_STR)-100);
} else if (ACURR(A_STR)==118) {
Sprintf(buf,"STR:18/**");
} else if(ACURR(A_STR) > 18) {
Sprintf(buf,"STR:18/%02d",ACURR(A_STR)-18);
} else {
Sprintf(buf,"STR:%d",ACURR(A_STR));
}
str.setLabel(buf,NetHackQtLabelledIcon::NoNum,ACURR(A_STR));
dex.setLabel("DEX:",(long)ACURR(A_DEX));
con.setLabel("CON:",(long)ACURR(A_CON));
intel.setLabel("INT:",(long)ACURR(A_INT));
wis.setLabel("WIS:",(long)ACURR(A_WIS));
cha.setLabel("CHA:",(long)ACURR(A_CHA));
const char* hung=hu_stat[u.uhs];
if (hung[0]==' ') {
hunger.hide();
} else {
hunger.setIcon(u.uhs ? p_hungry : p_satiated);
hunger.setLabel(hung);
hunger.show();
}
if (Confusion) confused.show(); else confused.hide();
if (Sick) {
if (u.usick_type & SICK_VOMITABLE) {
sick_fp.show();
} else {
sick_fp.hide();
}
if (u.usick_type & SICK_NONVOMITABLE) {
sick_il.show();
} else {
sick_il.hide();
}
} else {
sick_fp.hide();
sick_il.hide();
}
if (Blind) blind.show(); else blind.hide();
if (Stunned) stunned.show(); else stunned.hide();
if (Hallucination) hallu.show(); else hallu.hide();
const char* enc=enc_stat[near_capacity()];
if (enc[0]==' ' || !enc[0]) {
encumber.hide();
} else {
encumber.setIcon(p_encumber[near_capacity()-1]);
encumber.setLabel(enc);
encumber.show();
}
Strcpy(buf, plname);
if ('a' <= buf[0] && buf[0] <= 'z') buf[0] += 'A'-'a';
Strcat(buf, " the ");
if (u.mtimedone) {
char mname[BUFSZ];
int k = 0;
Strcpy(mname, mons[u.umonnum].mname);
while(mname[k] != 0) {
if ((k == 0 || (k > 0 && mname[k-1] == ' '))
&& 'a' <= mname[k] && mname[k] <= 'z')
{
mname[k] += 'A' - 'a';
}
k++;
}
Strcat(buf, mname);
} else {
Strcat(buf, rank_of(u.ulevel, pl_character[0], ::flags.female));
}
name.setLabel(buf,NetHackQtLabelledIcon::NoNum,u.ulevel);
if (describe_level(buf, FALSE)) {
dlevel.setLabel(buf,(bool)TRUE);
} else {
Sprintf(buf, "%s, level ", dungeons[u.uz.dnum].dname);
dlevel.setLabel(buf,(long)depth(&u.uz));
}
#ifndef GOLDOBJ
gold.setLabel("Au:", u.ugold);
#else
gold.setLabel("Au:", money_cnt(invent));
#endif
if (u.mtimedone) {
// You're a monster!
Sprintf(buf, "/%d", u.mhmax);
hp.setLabel("HP:",u.mh > 0 ? u.mh : 0,buf);
level.setLabel("HD:",(long)mons[u.umonnum].mlevel);
} else {
// You're normal.
Sprintf(buf, "/%d", u.uhpmax);
hp.setLabel("HP:",u.uhp > 0 ? u.uhp : 0,buf);
level.setLabel("Level:",(long)u.ulevel);
}
Sprintf(buf, "/%d", u.uenmax);
power.setLabel("Pow:",u.uen,buf);
ac.setLabel("AC:",(long)u.uac);
#ifdef EXP_ON_BOTL
if (::flags.showexp) {
exp.setLabel("Exp:",(long)u.uexp);
} else
#endif
{
exp.setLabel("");
}
if (u.ualign.type==A_CHAOTIC) {
align.setIcon(p_chaotic);
align.setLabel("Chaotic");
} else if (u.ualign.type==A_NEUTRAL) {
align.setIcon(p_neutral);
align.setLabel("Neutral");
} else {
align.setIcon(p_lawful);
align.setLabel("Lawful");
}
if (::flags.time) time.setLabel("Time:",(long)moves);
else time.setLabel("");
#ifdef SCORE_ON_BOTL
if (::flags.showscore) {
score.setLabel("Score:",(long)botl_score());
} else
#endif
{
score.setLabel("");
}
#ifdef SHOW_WEIGHT
if (::flags.showweight) {
Sprintf(buf, "/%ld", (long)weight_cap());
weight.setLabel("Weight:",(long)(inv_weight()+weight_cap()),buf);
} else
#endif
{
weight.setLabel("");
}
if (first_set)
{
first_set=FALSE;
name.highlightWhenChanging();
dlevel.highlightWhenChanging();
str.highlightWhenChanging();
dex.highlightWhenChanging();
con.highlightWhenChanging();
intel.highlightWhenChanging();
wis.highlightWhenChanging();
cha.highlightWhenChanging();
gold.highlightWhenChanging();
hp.highlightWhenChanging();
power.highlightWhenChanging();
ac.highlightWhenChanging(); ac.lowIsGood();
level.highlightWhenChanging();
exp.highlightWhenChanging();
align.highlightWhenChanging();
//time.highlightWhenChanging();
score.highlightWhenChanging();
weight.highlightWhenChanging();
hunger.highlightWhenChanging();
confused.highlightWhenChanging();
sick_fp.highlightWhenChanging();
sick_il.highlightWhenChanging();
blind.highlightWhenChanging();
stunned.highlightWhenChanging();
hallu.highlightWhenChanging();
encumber.highlightWhenChanging();
}
}
/*
* Turn off hilighted status values after a certain amount of turns.
*/
void NetHackQtStatusWindow::checkTurnEvents()
{
}
NetHackQtMenuDialog::NetHackQtMenuDialog() :
QDialog(qApp->mainWidget(),0,FALSE)
{
}
void NetHackQtMenuDialog::resizeEvent(QResizeEvent*)
{
emit Resized();
}
void NetHackQtMenuDialog::Accept()
{
accept();
}
void NetHackQtMenuDialog::Reject()
{
reject();
}
void NetHackQtMenuDialog::SetResult(int r)
{
setResult(r);
}
void NetHackQtMenuDialog::done(int i)
{
setResult(i);
qApp->exit_loop();
}
// Table view columns:
//
// [pick-count] [accel] [glyph] [string]
//
// Maybe accel should be near string. We'll see.
// pick-count normally blank.
// double-clicking or click-on-count gives pop-up entry
// string is green when selected
//
NetHackQtMenuWindow::NetHackQtMenuWindow(NetHackQtKeyBuffer& ks) :
QTableView(),
keysource(ks),
dialog(new NetHackQtMenuDialog()),
prompt(0),
pressed(-1)
{
setNumCols(4);
setCellHeight(QMAX(qt_settings->glyphs().height()+1,fontMetrics().height()));
setBackgroundColor(lightGray);
setFrameStyle(Panel|Sunken);
setLineWidth(2);
ok=new QPushButton("Ok",dialog);
connect(ok,SIGNAL(clicked()),dialog,SLOT(accept()));
cancel=new QPushButton("Cancel",dialog);
connect(cancel,SIGNAL(clicked()),dialog,SLOT(reject()));
all=new QPushButton("All",dialog);
connect(all,SIGNAL(clicked()),this,SLOT(All()));
none=new QPushButton("None",dialog);
connect(none,SIGNAL(clicked()),this,SLOT(ChooseNone()));
invert=new QPushButton("Invert",dialog);
connect(invert,SIGNAL(clicked()),this,SLOT(Invert()));
search=new QPushButton("Search",dialog);
connect(search,SIGNAL(clicked()),this,SLOT(Search()));
QPoint pos(0,ok->height());
recreate(dialog,0,pos);
prompt.recreate(dialog,0,pos);
setBackgroundColor(lightGray);
connect(dialog,SIGNAL(Resized()),this,SLOT(Layout()));
setTableFlags(Tbl_autoHScrollBar|Tbl_autoVScrollBar
|Tbl_smoothScrolling|Tbl_clipCellPainting);
setFocusPolicy(StrongFocus);
}
NetHackQtMenuWindow::~NetHackQtMenuWindow()
{
// Remove from dialog before we destruct it
recreate(0,0,QPoint(0,0));
delete dialog;
}
void NetHackQtMenuWindow::focusInEvent(QFocusEvent *)
{
// Don't repaint at all, since nothing is using the focus colour
}
void NetHackQtMenuWindow::focusOutEvent(QFocusEvent *)
{
// Don't repaint at all, since nothing is using the focus colour
}
int NetHackQtMenuWindow::cellWidth(int col)
{
switch (col) {
case 0:
return fontMetrics().width("All ");
break; case 1:
return fontMetrics().width(" m ");
break; case 2:
return qt_settings->glyphs().width();
break; case 3:
return str_width;
}
impossible("Extra column (#%d) in MenuWindow",col);
return 0;
}
QWidget* NetHackQtMenuWindow::Widget() { return dialog; }
void NetHackQtMenuWindow::StartMenu()
{
setNumRows((itemcount=0));
str_width=200;
str_fixed=FALSE;
next_accel=0;
has_glyphs=FALSE;
}
NetHackQtMenuWindow::MenuItem::MenuItem() :
str(0)
{
}
NetHackQtMenuWindow::MenuItem::~MenuItem()
{
if (str) free((void*)str);
}
#define STR_MARGIN 4
void NetHackQtMenuWindow::AddMenu(int glyph, const ANY_P* identifier,
char ch, char gch, int attr, const char* str, bool presel)
{
if (!ch && identifier->a_void!=0) {
// Supply a keyboard accelerator. Limited supply.
static char accel[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
if (accel[next_accel]) {
ch=accel[next_accel++];
}
}
if ((int)item.size() < itemcount+1) {
item.resize(itemcount*4+10);
}
item[itemcount].glyph=glyph;
item[itemcount].identifier=*identifier;
item[itemcount].ch=ch;
item[itemcount].attr=attr;
item[itemcount].str=strdup(str);
item[itemcount].selected=presel;
item[itemcount].count=-1;
++itemcount;
str_fixed=str_fixed || strstr(str," ");
if (glyph!=NO_GLYPH) has_glyphs=TRUE;
}
void NetHackQtMenuWindow::EndMenu(const char* p)
{
prompt.setText(p ? p : "");
}
void NetHackQtMenuWindow::Layout()
{
int butw=totalWidth()/6; // 6 buttons
int buth=fontMetrics().height()+8; // 8 for spacing & mitres
int prompth=(prompt.text().isNull() ? 0 : buth);
prompt.setGeometry(6,buth,dialog->width()-6,prompth);
int h=dialog->height()-buth-prompth;
setGeometry(0,buth+prompth, dialog->width(), h);
// Below, we take care to use up full width
int x=0;
ok->setGeometry(x,0,butw,buth); x+=butw; butw=(dialog->width()-x)/5;
cancel->setGeometry(x,0,butw,buth); x+=butw; butw=(dialog->width()-x)/4;
all->setGeometry(x,0,butw,buth); x+=butw; butw=(dialog->width()-x)/3;
none->setGeometry(x,0,butw,buth); x+=butw; butw=(dialog->width()-x)/2;
invert->setGeometry(x,0,butw,buth); x+=butw; butw=(dialog->width()-x)/1;
search->setGeometry(x,0,butw,buth);
}
int NetHackQtMenuWindow::SelectMenu(int h, MENU_ITEM_P **menu_list)
{
setFont(str_fixed ?
qt_settings->normalFixedFont() : qt_settings->normalFont());
for (int i=0; i<itemcount; i++) {
str_width=QMAX(str_width,
STR_MARGIN+fontMetrics().width(item[i].str));
}
setCellHeight(QMAX(qt_settings->glyphs().height()+1,fontMetrics().height()));
setNumRows(itemcount);
int buth=fontMetrics().height()+8; // 8 for spacing & mitres
how=h;
ok->setEnabled(how!=PICK_ONE);ok->setDefault(how!=PICK_ONE);
cancel->setEnabled(how!=PICK_NONE);
all->setEnabled(how==PICK_ANY);
none->setEnabled(how==PICK_ANY);
invert->setEnabled(how==PICK_ANY);
search->setEnabled(how!=PICK_NONE);
dialog->SetResult(-1);
// 20 allows for scrollbar or spacing
// 4 for frame borders
int mh = QApplication::desktop()->height()*3/5;
if ( qt_compact_mode && totalHeight() > mh ) {
// big, so make it fill
dialog->showMaximized();
} else {
dialog->resize(totalWidth()+20,
QMIN(totalHeight(), mh)+buth+4+(prompt.text().isNull() ? 0 : buth));
if ( dialog->width() > QApplication::desktop()->width() )
dialog->resize(QApplication::desktop()->width(),dialog->height()+16);
centerOnMain(dialog);
dialog->show();
}
setFocus();
while (dialog->result()<0) {
// changed the defaults below to the values in wintype.h 000119 - azy
if (!keysource.Empty()) {
char k=keysource.GetAscii();
k=map_menu_cmd(k); /* added 000119 - azy */
if (k=='\033')
dialog->Reject();
else if (k=='\r' || k=='\n' || k==' ')
dialog->Accept();
else if (k==MENU_SEARCH)
Search();
else if (k==MENU_SELECT_ALL)
All();
else if (k==MENU_INVERT_ALL)
Invert();
else if (k==MENU_UNSELECT_ALL)
ChooseNone();
else {
for (int i=0; i<itemcount; i++) {
if (item[i].ch==k)
ToggleSelect(i);
}
}
}
if (dialog->result()<0)
qApp->enter_loop();
}
//if ( (nhid != WIN_INVEN || !flags.perm_invent) ) // doesn't work yet
{
dialog->hide();
}
int result=dialog->result();
// Consume ^M (which QDialog steals for default button)
while (!keysource.Empty() &&
(keysource.TopAscii()=='\n' || keysource.TopAscii()=='\r'))
keysource.GetAscii();
*menu_list=0;
if (result>0 && how!=PICK_NONE) {
if (how==PICK_ONE) {
int i;
for (i=0; i<itemcount && !item[i].selected; i++)
;
if (i<itemcount) {
*menu_list=(MENU_ITEM_P*)malloc(sizeof(MENU_ITEM_P));
(*menu_list)[0].item=item[i].identifier;
(*menu_list)[0].count=item[i].count;
return 1;
} else {
return 0;
}
} else {
int count=0;
for (int i=0; i<itemcount; i++)
if (item[i].selected) count++;
if (count) {
*menu_list=(MENU_ITEM_P*)malloc(count*sizeof(MENU_ITEM_P));
int j=0;
for (int i=0; i<itemcount; i++) {
if (item[i].selected) {
(*menu_list)[j].item=item[i].identifier;
(*menu_list)[j].count=item[i].count;
j++;
}
}
return count;
} else {
return 0;
}
}
} else {
return -1;
}
}
void NetHackQtMenuWindow::keyPressEvent(QKeyEvent* event)
{
if (viewHeight() < totalHeight() && !(event->state()&ShiftButton)) {
if (event->key()==Key_Prior) {
setYOffset(yOffset()-viewHeight());
} else if (event->key()==Key_Next) {
setYOffset(yOffset()+viewHeight());
} else {
event->ignore();
}
} else {
event->ignore();
}
}
void NetHackQtMenuWindow::All()
{
for (int i=0; i<itemcount; i++) {
if (!item[i].selected) ToggleSelect(i);
}
}
void NetHackQtMenuWindow::ChooseNone()
{
for (int i=0; i<itemcount; i++) {
if (item[i].selected) ToggleSelect(i);
}
}
void NetHackQtMenuWindow::Invert()
{
for (int i=0; i<itemcount; i++) {
ToggleSelect(i);
}
}
void NetHackQtMenuWindow::Search()
{
NetHackQtStringRequestor requestor(keysource,"Search for:");
char line[256];
if (requestor.Get(line)) {
for (int i=0; i<itemcount; i++) {
if (strstr(item[i].str,line))
ToggleSelect(i);
}
}
}
void NetHackQtMenuWindow::ToggleSelect(int i)
{
if (item[i].Selectable()) {
item[i].selected = !item[i].selected;
if ( !item[i].selected )
item[i].count=-1;
updateCell(i,3);
if (how==PICK_ONE) {
dialog->Accept();
}
}
}
void NetHackQtMenuWindow::paintCell(QPainter* painter, int row, int col)
{
// [pick-count] [accel] [glyph] [string]
MenuItem& i = item[row];
painter->setPen(black);
painter->setFont(font());
if (i.selected) {
painter->setPen(darkGreen);
}
switch (col) {
case 0:
if ( i.ch || i.attr!=ATR_INVERSE ) {
QString text;
if ( i.selected && i.count == -1 ) {
if ( i.str[0]>='0' && i.str[0]<='9' )
text = "All";
else
text = "*";
} else if ( i.count<0 ) {
text = "-";
} else {
text.sprintf("%d",i.count);
}
painter->drawText(0,0,cellWidth(col),cellHeight(),
AlignHCenter|AlignVCenter,text);
}
break; case 1:
if ((signed char)i.ch >= 0) {
char text[2]={i.ch,0};
painter->drawText(0,0,cellWidth(col),cellHeight(),
AlignHCenter|AlignVCenter,text);
}
break; case 2:
if (i.glyph!=NO_GLYPH) {
// Centered in height
int y=(cellHeight()-qt_settings->glyphs().height())/2;
if (y<0) y=0;
qt_settings->glyphs().drawGlyph(*painter, i.glyph, 0, y);
}
break; case 3:
// XXX should qt_settings have ALL the various fonts
QFont newfont=font();
if (i.attr) {
switch(i.attr) {
case ATR_ULINE:
newfont.setUnderline(TRUE);
break; case ATR_BOLD:
painter->setPen(red);
break; case ATR_BLINK:
newfont.setItalic(TRUE);
break; case ATR_INVERSE:
newfont=qt_settings->largeFont();
newfont.setWeight(QFont::Bold);
if (i.selected) {
painter->setPen(blue);
} else {
painter->setPen(darkBlue);
}
}
}
painter->setFont(newfont);
painter->drawText(STR_MARGIN,0,cellWidth(col),cellHeight(),
AlignLeft|AlignVCenter,i.str);
}
}
void NetHackQtMenuWindow::mousePressEvent(QMouseEvent* event)
{
int col=findCol(event->pos().x());
int row=findRow(event->pos().y());
if (col<0 || row<0 || !item[row].Selectable()) return;
if (how!=PICK_NONE) {
if (col==0) {
// Changing count.
NetHackQtStringRequestor requestor(keysource,"Count:");
char buf[BUFSZ];
if (item[row].count>0)
Sprintf(buf,"%d", item[row].count);
else
Strcpy(buf, "");
requestor.SetDefault(buf);
if (requestor.Get(buf)) {
item[row].count=atoi(buf);
if (item[row].count==0) {
item[row].count=-1;
if (item[row].selected) ToggleSelect(row);
} else {
if (!item[row].selected) ToggleSelect(row);
}
updateCell(row,0);
}
} else {
pressed=row;
was_sel=item[row].selected;
ToggleSelect(row);
updateCell(row,0);
}
}
}
void NetHackQtMenuWindow::mouseReleaseEvent(QMouseEvent* event)
{
if (pressed>=0) {
int p=pressed;
pressed=-1;
updateCell(p,3);
}
}
void NetHackQtMenuWindow::mouseMoveEvent(QMouseEvent* event)
{
if (pressed>=0) {
int col=findCol(event->pos().x());
int row=findRow(event->pos().y());
if (row>=0 && col>=0) {
if (pressed!=row) {
// reset to initial state
if (item[pressed].selected!=was_sel)
ToggleSelect(pressed);
} else {
// reset to new state
if (item[pressed].selected==was_sel)
ToggleSelect(pressed);
}
}
}
}
class NetHackQtTextListBox : public QListBox {
public:
NetHackQtTextListBox(QWidget* parent) : QListBox(parent) { }
int TotalWidth()
{
doLayout();
return contentsWidth();
}
int TotalHeight()
{
doLayout();
return contentsHeight();
}
virtual void setFont(const QFont &font)
{
QListBox::setFont(font);
}
void keyPressEvent(QKeyEvent* e)
{
QListBox::keyPressEvent(e);
}
};
QPixmap* NetHackQtRIP::pixmap=0;
NetHackQtRIP::NetHackQtRIP(QWidget* parent) :
QWidget(parent)
{
if (!pixmap) {
pixmap=new QPixmap;
tryload(*pixmap, "rip.xpm");
}
riplines=0;
resize(pixmap->width(),pixmap->height());
setFont(QFont("times",12)); // XXX may need to be configurable
}
void NetHackQtRIP::setLines(char** l, int n)
{
line=l;
riplines=n;
}
QSize NetHackQtRIP::sizeHint() const
{
return pixmap->size();
}
void NetHackQtRIP::paintEvent(QPaintEvent* event)
{
if ( riplines ) {
int pix_x=(width()-pixmap->width())/2;
int pix_y=(height()-pixmap->height())/2;
// XXX positions based on RIP image
int rip_text_x=pix_x+156;
int rip_text_y=pix_y+67;
int rip_text_h=94/riplines;
QPainter painter;
painter.begin(this);
painter.drawPixmap(pix_x,pix_y,*pixmap);
for (int i=0; i<riplines; i++) {
painter.drawText(rip_text_x-i/2,rip_text_y+i*rip_text_h,
1,1,DontClip|AlignHCenter,line[i]);
}
painter.end();
}
}
NetHackQtTextWindow::NetHackQtTextWindow(NetHackQtKeyBuffer& ks) :
QDialog(qApp->mainWidget(),0,FALSE),
keysource(ks),
use_rip(FALSE),
str_fixed(FALSE),
ok("Dismiss",this),
search("Search",this),
lines(new NetHackQtTextListBox(this)),
rip(this)
{
ok.setDefault(TRUE);
connect(&ok,SIGNAL(clicked()),this,SLOT(accept()));
connect(&search,SIGNAL(clicked()),this,SLOT(Search()));
connect(qt_settings,SIGNAL(fontChanged()),this,SLOT(doUpdate()));
QVBoxLayout* vb = new QVBoxLayout(this);
vb->addWidget(&rip);
QHBoxLayout* hb = new QHBoxLayout(vb);
hb->addWidget(&ok);
hb->addWidget(&search);
vb->addWidget(lines);
}
void NetHackQtTextWindow::doUpdate()
{
update();
}
NetHackQtTextWindow::~NetHackQtTextWindow()
{
}
QWidget* NetHackQtTextWindow::Widget()
{
return this;
}
bool NetHackQtTextWindow::Destroy()
{
return !isVisible();
}
void NetHackQtTextWindow::UseRIP(int how)
{
// Code from X11 windowport
#define STONE_LINE_LEN 16 /* # chars that fit on one line */
#define NAME_LINE 0 /* line # for player name */
#define GOLD_LINE 1 /* line # for amount of gold */
#define DEATH_LINE 2 /* line # for death description */
#define YEAR_LINE 6 /* line # for year */
static char** rip_line=0;
if (!rip_line) {
rip_line=new char*[YEAR_LINE+1];
for (int i=0; i<YEAR_LINE+1; i++) {
rip_line[i]=new char[STONE_LINE_LEN+1];
}
}
/* Follows same algorithm as genl_outrip() */
char buf[BUFSZ];
char *dpx;
int line;
/* Put name on stone */
Sprintf(rip_line[NAME_LINE], "%s", plname);
/* Put $ on stone */
#ifndef GOLDOBJ
Sprintf(rip_line[GOLD_LINE], "%ld Au", u.ugold);
#else
Sprintf(rip_line[GOLD_LINE], "%ld Au", done_money);
#endif
/* Put together death description */
switch (killer_format) {
default: impossible("bad killer format?");
case KILLED_BY_AN:
Strcpy(buf, killed_by_prefix[how]);
Strcat(buf, an(killer));
break;
case KILLED_BY:
Strcpy(buf, killed_by_prefix[how]);
Strcat(buf, killer);
break;
case NO_KILLER_PREFIX:
Strcpy(buf, killer);
break;
}
/* Put death type on stone */
for (line=DEATH_LINE, dpx = buf; line<YEAR_LINE; line++) {
register int i,i0;
char tmpchar;
if ( (i0=strlen(dpx)) > STONE_LINE_LEN) {
for(i = STONE_LINE_LEN;
((i0 > STONE_LINE_LEN) && i); i--)
if(dpx[i] == ' ') i0 = i;
if(!i) i0 = STONE_LINE_LEN;
}
tmpchar = dpx[i0];
dpx[i0] = 0;
strcpy(rip_line[line], dpx);
if (tmpchar != ' ') {
dpx[i0] = tmpchar;
dpx= &dpx[i0];
} else dpx= &dpx[i0+1];
}
/* Put year on stone */
Sprintf(rip_line[YEAR_LINE], "%4d", getyear());
rip.setLines(rip_line,YEAR_LINE+1);
use_rip=TRUE;
}
void NetHackQtTextWindow::Clear()
{
lines->clear();
use_rip=FALSE;
str_fixed=FALSE;
}
void NetHackQtTextWindow::Display(bool block)
{
if (str_fixed) {
lines->setFont(qt_settings->normalFixedFont());
} else {
lines->setFont(qt_settings->normalFont());
}
int h=0;
if (use_rip) {
h+=rip.height();
ok.hide();
search.hide();
rip.show();
} else {
h+=ok.height()*2;
ok.show();
search.show();
rip.hide();
}
int mh = QApplication::desktop()->height()*3/5;
if ( qt_compact_mode && lines->TotalHeight() > mh || use_rip ) {
// big, so make it fill
showMaximized();
} else {
resize(QMAX(use_rip ? rip.width() : 200,
lines->TotalWidth()+24),
QMIN(mh, lines->TotalHeight()+h));
centerOnMain(this);
show();
}
if (block) {
setResult(-1);
while (result()==-1) {
qApp->enter_loop();
if (result()==-1 && !keysource.Empty()) {
char k=keysource.GetAscii();
if (k=='\033' || k==' ' || k=='\r' || k=='\n') {
accept();
} else if (k=='/') {
Search();
}
}
}
}
}
void NetHackQtTextWindow::PutStr(int attr, const char* text)
{
str_fixed=str_fixed || strstr(text," ");
lines->insertItem(text);
}
void NetHackQtTextWindow::done(int i)
{
setResult(i+1000);
hide();
qApp->exit_loop();
}
void NetHackQtTextWindow::keyPressEvent(QKeyEvent* e)
{
if ( e->ascii() != '\r' && e->ascii() != '\n' && e->ascii() != '\033' )
lines->keyPressEvent(e);
else
QDialog::keyPressEvent(e);
}
void NetHackQtTextWindow::Search()
{
NetHackQtStringRequestor requestor(keysource,"Search for:");
static char line[256]="";
requestor.SetDefault(line);
if (requestor.Get(line)) {
int current=lines->currentItem();
for (uint i=1; i<lines->count(); i++) {
int lnum=(i+current)%lines->count();
QString str=lines->text(lnum);
if (str.contains(line)) {
lines->setCurrentItem(lnum);
lines->centerCurrentItem();
return;
}
}
lines->setCurrentItem(-1);
}
}
NetHackQtDelay::NetHackQtDelay(int ms) :
msec(ms)
{
}
void NetHackQtDelay::wait()
{
startTimer(msec);
qApp->enter_loop();
}
void NetHackQtDelay::timerEvent(QTimerEvent* timer)
{
qApp->exit_loop();
killTimers();
}
NetHackQtInvUsageWindow::NetHackQtInvUsageWindow(QWidget* parent) :
QWidget(parent)
{
}
void NetHackQtInvUsageWindow::drawWorn(QPainter& painter, obj* nhobj, int x, int y, bool canbe)
{
short int glyph;
if (nhobj)
glyph=obj_to_glyph(nhobj);
else if (canbe)
glyph=cmap_to_glyph(S_room);
else
glyph=cmap_to_glyph(S_stone);
qt_settings->glyphs().drawCell(painter,glyph,x,y);
}
void NetHackQtInvUsageWindow::paintEvent(QPaintEvent*)
{
// 012
//
//0 WhB
//1 s"w
//2 gCg
//3 =A=
//4 T
//5 S
QPainter painter;
painter.begin(this);
// Blanks
drawWorn(painter,0,0,4,FALSE);
drawWorn(painter,0,0,5,FALSE);
drawWorn(painter,0,2,4,FALSE);
drawWorn(painter,0,2,5,FALSE);
drawWorn(painter,uarm,1,3); // Armour
drawWorn(painter,uarmc,1,2); // Cloak
drawWorn(painter,uarmh,1,0); // Helmet
drawWorn(painter,uarms,0,1); // Shield
drawWorn(painter,uarmg,0,2); // Gloves - repeated
drawWorn(painter,uarmg,2,2); // Gloves - repeated
#ifdef TOURIST
drawWorn(painter,uarmf,1,5); // Shoes (feet)
drawWorn(painter,uarmu,1,4); // Undershirt
#else
drawWorn(painter,0 ,1,5,FALSE);
drawWorn(painter,uarmf,1,4); // Shoes (feet)
#endif
drawWorn(painter,uleft,0,3); // RingL
drawWorn(painter,uright,2,3); // RingR
drawWorn(painter,uwep,2,1); // Weapon
drawWorn(painter,uswapwep,0,0); // Secondary weapon
drawWorn(painter,uamul,1,1); // Amulet
drawWorn(painter,ublindf,2,0); // Blindfold
painter.end();
}
class SmallToolButton : public QToolButton {
public:
SmallToolButton(const QPixmap & pm, const QString &textLabel,
const QString& grouptext,
QObject * receiver, const char* slot,
QToolBar * parent) :
QToolButton(pm, textLabel,
#if QT_VERSION < 210
QString::null,
#else
grouptext,
#endif
receiver, slot, parent)
{
}
QSize sizeHint() const
{
// get just a couple more pixels for the map
return QToolButton::sizeHint()-QSize(0,2);
}
};
NetHackQtMainWindow::NetHackQtMainWindow(NetHackQtKeyBuffer& ks) :
message(0), map(0), status(0), invusage(0),
keysink(ks), dirkey(0)
{
QToolBar* toolbar = new QToolBar(this);
#if QT_VERSION >= 210
setToolBarsMovable(FALSE);
toolbar->setHorizontalStretchable(TRUE);
toolbar->setVerticalStretchable(TRUE);
#endif
addToolBar(toolbar);
menubar = menuBar();
setCaption("Qt " DEF_GAME_NAME);
if ( qt_compact_mode )
setIcon(QPixmap(nh_icon_small));
else
setIcon(QPixmap(nh_icon));
QPopupMenu* game=new QPopupMenu;
QPopupMenu* apparel=new QPopupMenu;
QPopupMenu* act1=new QPopupMenu;
QPopupMenu* act2 = qt_compact_mode ? new QPopupMenu : act1;
QPopupMenu* magic=new QPopupMenu;
QPopupMenu* info=new QPopupMenu;
QPopupMenu *help;
#ifdef KDE
help = kapp->getHelpMenu( TRUE, "" );
help->insertSeparator();
#else
help = qt_compact_mode ? info : new QPopupMenu;
#endif
enum { OnDesktop=1, OnHandhelds=2 };
struct Macro {
QPopupMenu* menu;
const char* name;
const char* action;
int flags;
} item[] = {
{ game, 0, 0, 3},
{ game, "Version\tv", "v", 3},
{ game, "Compilation\tAlt+V", "\366", 3},
{ game, "History\tShift+V", "V", 3},
{ game, "Redraw\tCtrl+R", "\022", 0}, // useless
{ game, "Options\tShift+O", "O", 3},
{ game, "Explore mode\tShift+X", "X", 3},
{ game, 0, 0, 3},
{ game, "Save\tSy", "Sy", 3},
{ game, "Quit\tAlt+Q", "\361", 3},
{ apparel, "Apparel off\tShift+A", "A", 2},
{ apparel, "Remove many\tShift+A", "A", 1},
{ apparel, 0, 0, 3},
{ apparel, "Wield weapon\tw", "w", 3},
{ apparel, "Exchange weapons\tx", "x", 3},
{ apparel, "Two weapon combat\t#two", "#tw", 3},
{ apparel, "Load quiver\tShift+Q", "Q", 3},
{ apparel, 0, 0, 3},
{ apparel, "Wear armour\tShift+W", "W", 3},
{ apparel, "Take off armour\tShift+T", "T", 3},
{ apparel, 0, 0, 3},
{ apparel, "Put on non-armour\tShift+P", "P", 3},
{ apparel, "Remove non-armour\tShift+R", "R", 3},
{ act1, "Again\tCtrl+A", "\001", 2},
{ act1, 0, 0, 3},
{ act1, "Apply\ta?", "a?", 3},
{ act1, "Chat\tAlt+C", "\343", 3},
{ act1, "Close door\tc", "c", 3},
{ act1, "Down\t>", ">", 3},
{ act1, "Drop many\tShift+D", "D", 2},
{ act1, "Drop\td?", "d?", 2},
{ act1, "Eat\te?", "e?", 2},
{ act1, "Engrave\tShift+E", "E", 3},
{ act1, "Fight\tShift+F", "F", 3},
{ act1, "Fire from quiver\tf", "f", 2},
{ act1, "Force\tAlt+F", "\346", 3},
{ act1, "Get\t,", ",", 2},
{ act1, "Jump\tAlt+J", "\352", 3},
{ act2, "Kick\tCtrl+D", "\004", 2},
{ act2, "Loot\tAlt+L", "\354", 3},
{ act2, "Open door\to", "o", 3},
{ act2, "Pay\tp", "p", 3},
{ act2, "Rest\t.", ".", 2},
{ act2, "Ride\t#ri", "#ri", 3},
{ act2, "Search\ts", "s", 3},
{ act2, "Sit\tAlt+S", "\363", 3},
{ act2, "Steal\tAlt+B", "\342", 3},
{ act2, "Throw\tt", "t", 2},
{ act2, "Untrap\t#u", "#u", 3},
{ act2, "Up\t<", "<", 3},
{ act2, "Wipe face\tAlt+W", "\367", 3},
{ magic, "Quaff potion\tq?", "q?", 3},
{ magic, "Read scroll/book\tr?", "r?", 3},
{ magic, "Zap wand\tz?", "z?", 3},
{ magic, "Zap spell\tShift+Z", "Z", 3},
{ magic, "Dip\tAlt+D", "\344", 3},
{ magic, "Rub\tAlt+R", "\362", 3},
{ magic, "Invoke\tAlt+I", "\351", 3},
{ magic, 0, 0, 3},
{ magic, "Offer\tAlt+O", "\357", 3},
{ magic, "Pray\tAlt+P", "\360", 3},
{ magic, 0, 0, 3},
{ magic, "Teleport\tCtrl+T", "\024", 3},
{ magic, "Poly self\tAlt+Y", "\371", 3},
{ magic, "Monster action\tAlt+M", "\355", 3},
{ magic, "Technique\tAlt+T", "\364", 3},
{ magic, "Turn undead\t#tu", "#tu", 3},
{ help, "Help\t?", "?", 3},
{ help, 0, 0, 3},
{ help, "What is here\t:", ":", 3},
{ help, "What is there\t;", ";", 3},
{ help, "What is...\t/y", "/y", 2},
{ help, 0, 0, 1},
{ info, "Inventory\ti", "i", 3},
#if 0
{ info, "Angbandish inventory\t*", "*", 3},
#endif
{ info, "Conduct\t#co", "#co", 3},
{ info, "Discoveries\t\\", "\\", 3},
{ info, "List/reorder spells\t+", "+", 3},
{ info, "Adjust letters\tAlt+A", "\341", 2},
{ info, 0, 0, 3},
{ info, "Name object\tAlt+N", "\356y?", 3},
{ info, "Name object type\tAlt+N", "\356n?", 3},
{ info, "Name creature\tShift+C", "C", 3},
{ info, 0, 0, 3},
{ info, "Qualifications\tAlt+E", "\345", 3},
{ 0, 0, 0, 0 }
};
int i;
int count=0;
for (i=0; item[i].menu; i++)
if (item[i].name) count++;
macro=new const char* [count];
game->insertItem("Qt settings...",1000);
help->insertItem("About Qt " DEF_GAME_NAME "...",2000);
//help->insertItem(DEF_GAME_NAME " Guidebook...",3000);
help->insertSeparator();
count=0;
for (i=0; item[i].menu; i++) {
if ( item[i].flags & (qt_compact_mode ? 1 : 2) ) {
if (item[i].name) {
QString name = item[i].name;
if ( qt_compact_mode ) // accelerators aren't
name.replace(QRegExp("\t.*"),"");
item[i].menu->insertItem(name,count);
macro[count++]=item[i].action;
} else {
item[i].menu->insertSeparator();
}
}
}
menubar->insertItem("Game",game);
menubar->insertItem("Gear",apparel);
if ( qt_compact_mode ) {
menubar->insertItem("A-J",act1);
menubar->insertItem("K-Z",act2);
menubar->insertItem("Magic",magic);
menubar->insertItem(QPixmap(info_xpm),info);
menubar->insertItem(QPixmap(map_xpm), this, SLOT(raiseMap()));
menubar->insertItem(QPixmap(msg_xpm), this, SLOT(raiseMessages()));
menubar->insertItem(QPixmap(stat_xpm), this, SLOT(raiseStatus()));
} else {
menubar->insertItem("Action",act1);
menubar->insertItem("Magic",magic);
menubar->insertItem("Info",info);
menubar->insertSeparator();
menubar->insertItem("Help",help);
}
QSignalMapper* sm = new QSignalMapper(this);
connect(sm, SIGNAL(mapped(const QString&)), this, SLOT(doKeys(const QString&)));
QToolButton* tb;
tb = new SmallToolButton( QPixmap(again_xpm),"Again","Action", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "\001" );
tb = new SmallToolButton( QPixmap(get_xpm),"Get","Action", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "," );
tb = new SmallToolButton( QPixmap(kick_xpm),"Kick","Action", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "\004" );
tb = new SmallToolButton( QPixmap(throw_xpm),"Throw","Action", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "t" );
tb = new SmallToolButton( QPixmap(fire_xpm),"Fire","Action", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "f" );
tb = new SmallToolButton( QPixmap(drop_xpm),"Drop","Action", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "D" );
tb = new SmallToolButton( QPixmap(eat_xpm),"Eat","Action", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "e" );
tb = new SmallToolButton( QPixmap(rest_xpm),"Rest","Action", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "." );
tb = new SmallToolButton( QPixmap(cast_a_xpm),"Cast A","Magic", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "Za" );
tb = new SmallToolButton( QPixmap(cast_b_xpm),"Cast B","Magic", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "Zb" );
tb = new SmallToolButton( QPixmap(cast_c_xpm),"Cast C","Magic", sm, SLOT(map()), toolbar );
sm->setMapping(tb, "Zc" );
if ( !qt_compact_mode ) {
QWidget* filler = new QWidget(toolbar);
filler->setBackgroundMode(PaletteButton);
toolbar->setStretchableWidget(filler);
}
connect(menubar,SIGNAL(activated(int)),this,SLOT(doMenuItem(int)));
#ifdef KDE
setMenu (menubar);
#endif
int x=0,y=0;
int w=QApplication::desktop()->width()-10; // XXX arbitrary extra space for frame
int h=QApplication::desktop()->height()-50;
int maxwn;
int maxhn;
if (qt_tilewidth != NULL) {
maxwn = atoi(qt_tilewidth) * COLNO + 10;
} else {
maxwn = 1400;
}
if (qt_tileheight != NULL) {
maxhn = atoi(qt_tileheight) * ROWNO * 6/4;
} else {
maxhn = 1024;
}
// Be exactly the size we want to be - full map...
if (w>maxwn) {
x+=(w-maxwn)/2;
w=maxwn; // Doesn't need to be any wider
}
if (h>maxhn) {
y+=(h-maxhn)/2;
h=maxhn; // Doesn't need to be any taller
}
setGeometry(x,y,w,h);
if ( qt_compact_mode ) {
stack = new QWidgetStack(this);
setCentralWidget(stack);
} else {
setCentralWidget(new QWidget(this));
invusage = new NetHackQtInvUsageWindow(centralWidget());
}
}
void NetHackQtMainWindow::zoomMap()
{
qt_settings->toggleGlyphSize();
}
void NetHackQtMainWindow::raiseMap()
{
if ( stack->id(stack->visibleWidget()) == 0 ) {
zoomMap();
} else {
stack->raiseWidget(0);
}
}
void NetHackQtMainWindow::raiseMessages()
{
stack->raiseWidget(1);
}
void NetHackQtMainWindow::raiseStatus()
{
stack->raiseWidget(2);
}
class NetHackMimeSourceFactory : public QMimeSourceFactory {
public:
const QMimeSource* data(const QString& abs_name) const
{
const QMimeSource* r = 0;
if ( (NetHackMimeSourceFactory*)this == QMimeSourceFactory::defaultFactory() )
r = QMimeSourceFactory::data(abs_name);
else
r = QMimeSourceFactory::defaultFactory()->data(abs_name);
if ( !r ) {
int sl = abs_name.length();
do {
sl = abs_name.findRev('/',sl-1);
QString name = sl>=0 ? abs_name.mid(sl+1) : abs_name;
int dot = name.findRev('.');
if ( dot >= 0 )
name = name.left(dot);
if ( name == "map" )
r = new QImageDrag(QImage(map_xpm));
else if ( name == "msg" )
r = new QImageDrag(QImage(msg_xpm));
else if ( name == "stat" )
r = new QImageDrag(QImage(stat_xpm));
} while (!r && sl>0);
}
return r;
}
};
void NetHackQtMainWindow::doMenuItem(int id)
{
switch (id) {
case 1000:
centerOnMain(qt_settings);
qt_settings->show();
break;
case 2000:
QMessageBox::about(this, "About Qt " DEF_GAME_NAME, aboutMsg());
break;
case 3000: {
QDialog dlg(this,0,TRUE);
(new QVBoxLayout(&dlg))->setAutoAdd(TRUE);
QTextBrowser browser(&dlg);
NetHackMimeSourceFactory ms;
browser.setMimeSourceFactory(&ms);
browser.setSource(QDir::currentDirPath()+"/Guidebook.html");
if ( qt_compact_mode )
dlg.showMaximized();
dlg.exec();
}
break;
default:
if ( id >= 0 )
doKeys(macro[id]);
}
}
void NetHackQtMainWindow::doKeys(const QString& k)
{
keysink.Put(k);
qApp->exit_loop();
}
void NetHackQtMainWindow::AddMessageWindow(NetHackQtMessageWindow* window)
{
message=window;
ShowIfReady();
}
void NetHackQtMainWindow::AddMapWindow(NetHackQtMapWindow* window)
{
map=window;
ShowIfReady();
connect(map,SIGNAL(resized()),this,SLOT(layout()));
}
void NetHackQtMainWindow::AddStatusWindow(NetHackQtStatusWindow* window)
{
status=window;
ShowIfReady();
}
void NetHackQtMainWindow::RemoveWindow(NetHackQtWindow* window)
{
if (window==status) {
status=0;
ShowIfReady();
} else if (window==map) {
map=0;
ShowIfReady();
} else if (window==message) {
message=0;
ShowIfReady();
}
}
void NetHackQtMainWindow::updateInventory()
{
if ( invusage )
invusage->repaint(FALSE);
}
void NetHackQtMainWindow::fadeHighlighting()
{
if (status) {
status->fadeHighlighting();
}
}
void NetHackQtMainWindow::layout()
{
if ( qt_compact_mode )
return;
if (message && map && status) {
QSize maxs=map->Widget()->maximumSize();
int maph=QMIN(height()*2/3,maxs.height());
QWidget* c = centralWidget();
int h=c->height();
int toph=h-maph;
int iuw=3*qt_settings->glyphs().width();
int topw=(c->width()-iuw)/2;
message->Widget()->setGeometry(0,0,topw,toph);
invusage->setGeometry(topw,0,iuw,toph);
status->Widget()->setGeometry(topw+iuw,0,topw,toph);
map->Widget()->setGeometry(QMAX(0,(c->width()-maxs.width())/2),
toph,c->width(),maph);
}
}
void NetHackQtMainWindow::resizeEvent(QResizeEvent*)
{
layout();
#ifdef KDE
updateRects();
#endif
}
void NetHackQtMainWindow::keyReleaseEvent(QKeyEvent* event)
{
if ( dirkey ) {
doKeys(QString(QChar(dirkey)));
if ( !event->isAutoRepeat() )
dirkey = 0;
}
}
void NetHackQtMainWindow::keyPressEvent(QKeyEvent* event)
{
// Global key controls
// For desktop, arrow keys scroll map, since we don't want players
// to think that's the way to move. For handhelds, the normal way is to
// click-to-travel, so we allow the cursor keys for fine movements.
// 321
// 4 0
// 567
if ( event->isAutoRepeat() &&
event->key() >= Key_Left && event->key() <= Key_Down )
return;
const char* d = iflags.num_pad ? ndir : sdir;
switch (event->key()) {
case Key_Up:
if ( dirkey == d[0] )
dirkey = d[1];
else if ( dirkey == d[4] )
dirkey = d[3];
else
dirkey = d[2];
break; case Key_Down:
if ( dirkey == d[0] )
dirkey = d[7];
else if ( dirkey == d[4] )
dirkey = d[5];
else
dirkey = d[6];
break; case Key_Left:
if ( dirkey == d[2] )
dirkey = d[1];
else if ( dirkey == d[6] )
dirkey = d[7];
else
dirkey = d[0];
break; case Key_Right:
if ( dirkey == d[2] )
dirkey = d[3];
else if ( dirkey == d[6] )
dirkey = d[5];
else
dirkey = d[4];
break; case Key_Prior:
dirkey = 0;
if (message) message->Scroll(0,-1);
break; case Key_Next:
dirkey = 0;
if (message) message->Scroll(0,+1);
break; case Key_Space:
if ( flags.rest_on_space ) {
event->ignore();
return;
}
case Key_Enter:
if ( map )
map->clickCursor();
break; default:
dirkey = 0;
event->ignore();
}
}
void NetHackQtMainWindow::closeEvent(QCloseEvent* e)
{
if ( program_state.something_worth_saving ) {
switch ( QMessageBox::information( this, DEF_GAME_NAME,
"This will end your " DEF_GAME_NAME " session",
"&Save", "&Cancel", 0, 1 ) )
{
case 0:
// See dosave() function
if (dosave0()) {
u.uhp = -1;
NetHackQtBind::qt_exit_nhwindows(0);
terminate(EXIT_SUCCESS);
}
break;
case 1:
break; // ignore the event
}
} else {
e->accept();
}
}
void NetHackQtMainWindow::ShowIfReady()
{
if (message && map && status) {
QPoint pos(0,0);
QWidget* p = qt_compact_mode ? stack : centralWidget();
message->Widget()->recreate(p,0,pos);
map->Widget()->recreate(p,0,pos);
status->Widget()->recreate(p,0,pos);
if ( qt_compact_mode ) {
message->setMap(map);
stack->addWidget(map->Widget(), 0);
stack->addWidget(message->Widget(), 1);
stack->addWidget(status->Widget(), 2);
raiseMap();
} else {
layout();
}
showMaximized();
} else if (isVisible()) {
hide();
}
}
NetHackQtYnDialog::NetHackQtYnDialog(NetHackQtKeyBuffer& keysrc,const char* q,const char* ch,char df) :
QDialog(qApp->mainWidget(),0,FALSE),
question(q), choices(ch), def(df),
keysource(keysrc)
{
setCaption(DEF_GAME_NAME ": Question");
}
char NetHackQtYnDialog::Exec()
{
QString ch(choices);
int ch_per_line=6;
QString qlabel;
QString enable;
if ( qt_compact_mode && !choices ) {
// expand choices from prompt
// ##### why isn't choices set properly???
const char* c=question;
while ( *c && *c != '[' )
c++;
qlabel = QString(question).left(c-question);
if ( *c ) {
c++;
if ( *c == '-' )
ch.append(*c++);
char from=0;
while ( *c && *c != ']' && *c != ' ' ) {
if ( *c == '-' ) {
from = c[-1];
} else if ( from ) {
for (char f=from+1; f<=*c; f++)
ch.append(f);
from = 0;
} else {
ch.append(*c);
from = 0;
}
c++;
}
if ( *c == ' ' ) {
while ( *c && *c != ']' ) {
if ( *c == '*' || *c == '?' )
ch.append(*c);
c++;
}
}
}
if ( strstr(question, "what direction") ) {
// We replace this regardless, since sometimes you get choices.
const char* d = iflags.num_pad ? ndir : sdir;
enable=ch;
ch="";
ch.append(d[1]);
ch.append(d[2]);
ch.append(d[3]);
ch.append(d[0]);
ch.append('.');
ch.append(d[4]);
ch.append(d[7]);
ch.append(d[6]);
ch.append(d[5]);
ch.append(d[8]);
ch.append(d[9]);
ch_per_line = 3;
def = ' ';
} else {
// Hmm... they'll have to use a virtual keyboard
}
} else {
qlabel = question;
}
if (!ch.isNull()) {
QVBoxLayout vb(this);
vb.setAutoAdd(TRUE);
bool bigq = qlabel.length()>40;
if ( bigq ) {
QLabel* q = new QLabel(qlabel,this);
q->setAlignment(AlignLeft|WordBreak);
q->setMargin(4);
}
QButtonGroup group(ch_per_line, Horizontal,
bigq ? QString::null : qlabel, this);
int nchoices=ch.length();
bool allow_count=ch.contains('#');
const int margin=8;
const int gutter=8;
const int extra=fontMetrics().height(); // Extra for group
int x=margin, y=extra+margin;
int butsize=fontMetrics().height()*2+5;
QPushButton* button;
for (int i=0; i<nchoices && ch[i]!='\033'; i++) {
button=new QPushButton(QString(ch[i]),&group);
if ( !enable.isNull() ) {
if ( !enable.contains(ch[i]) )
button->setEnabled(FALSE);
}
button->setFixedSize(butsize,butsize); // Square
if (ch[i]==def) button->setDefault(TRUE);
if (i%10==9) {
// last in row
x=margin;
y+=butsize+gutter;
} else {
x+=butsize+gutter;
}
}
connect(&group,SIGNAL(clicked(int)),this,SLOT(doneItem(int)));
QLabel* lb=0;
QLineEdit* le=0;
if (allow_count) {
QHBox *hb = new QHBox(this);
lb=new QLabel("Count: ",hb);
le=new QLineEdit(hb);
}
adjustSize();
centerOnMain(this);
show();
char choice=0;
char ch_esc=0;
for (uint i=0; i<ch.length(); i++) {
if (ch[i].latin1()=='q') ch_esc='q';
else if (!ch_esc && ch[i].latin1()=='n') ch_esc='n';
}
setResult(-1);
while (!choice) {
if (!keysource.Empty()) {
char k=keysource.GetAscii();
char ch_esc=0;
for (uint i=0; i<ch.length(); i++)
if (ch[i].latin1()==k)
choice=k;
if (!choice) {
if (k=='\033' && ch_esc)
choice=ch_esc;
else if (k==' ' || k=='\r' || k=='\n')
choice=def;
// else choice remains 0
}
} else if ( result() == 0 ) {
choice = ch_esc ? ch_esc : def ? def : ' ';
} else if ( result() == 1 ) {
choice = def ? def : ch_esc ? ch_esc : ' ';
} else if ( result() >= 1000 ) {
choice = ch[result() - 1000].latin1();
}
if ( !choice )
qApp->enter_loop();
}
hide();
if (allow_count && !le->text().isEmpty()) {
yn_number=atoi(le->text());
choice='#';
}
return choice;
} else {
QLabel label(qlabel,this);
QPushButton cancel("Dismiss",this);
label.setFrameStyle(QFrame::Box|QFrame::Sunken);
label.setAlignment(AlignCenter);
label.resize(fontMetrics().width(qlabel)+60,30+fontMetrics().height());
cancel.move(width()/2-cancel.width()/2,label.geometry().bottom()+8);
connect(&cancel,SIGNAL(clicked()),this,SLOT(reject()));
centerOnMain(this);
setResult(-1);
show();
while (result()<0 && keysource.Empty()) {
qApp->enter_loop();
}
hide();
if (keysource.Empty()) {
return '\033';
} else {
return keysource.GetAscii();
}
}
}
void NetHackQtYnDialog::keyPressEvent(QKeyEvent* event)
{
// Don't want QDialog's Return/Esc behaviour
event->ignore();
}
void NetHackQtYnDialog::doneItem(int i)
{
done(i+1000);
}
void NetHackQtYnDialog::done(int i)
{
setResult(i);
qApp->exit_loop();
}
int NetHackQtGlyphs::loadTiles(const char *file)
{
int retval;
#ifndef FILE_AREAS
const char *tile_file = file;
#else
char *tile_file = make_file_name(FILE_AREA_SHARE, file);
#endif
retval = img.load(tile_file);
#ifdef FILE_AREAS
free(tile_file);
#endif
if (!retval)
return 0;
if ( iflags.wc_tile_width )
tilefile_tile_W = iflags.wc_tile_width;
else
tilefile_tile_W = img.width() / tiles_per_row;
if ( iflags.wc_tile_height )
tilefile_tile_H = iflags.wc_tile_height;
else
tilefile_tile_H = img.height() / tiles_per_col;
setSize(tilefile_tile_W, tilefile_tile_H);
return 1;
}
NetHackQtGlyphs::NetHackQtGlyphs()
{
int i;
int tw, th;
const char* tile_file;
QString msg;
// Try user specified tile set first
if (tileset[0] == '\0')
pline("ASCII mode not supported in Qt port.");
else {
for(i = 0; i < no_tilesets; i++)
if (!strcmp(tileset, tilesets[i].name))
break;
tileset_index = i;
// We don't really support transparency, but such
// tile sets are still useable with the Qt port.
if ((tilesets[i].flags & ~TILESET_TRANSPARENT) != 0) {
msg.sprintf("Tile set %s has an unsupported flag set.", tileset);
QMessageBox::warning(0, "IO Error", msg);
}
else if (i >= no_tilesets)
impossible("Tile set %s not defined?", tileset);
else if (loadTiles(tilesets[i].file))
return;
else {
msg.sprintf("Failed to load tile set %s.", tileset);
QMessageBox::warning(0, "IO Error", msg);
}
}
// Else choose first valid tile set
for(i = 0; i < no_tilesets; i++) {
if ((tilesets[i].flags & ~TILESET_TRANSPARENT) != 0)
continue;
if (loadTiles(tilesets[i].file)) {
tileset_index = i;
strcpy(tileset, tilesets[i].name);
return;
}
}
msg.sprintf("Cannot find a valid tile set");
QMessageBox::warning(0, "IO Error", msg);
panic("No valid tiles found");
}
void NetHackQtGlyphs::drawGlyph(QPainter& painter, int glyph, int x, int y)
{
int tile = glyph2tile[glyph];
int px = (tile%tiles_per_row)*width();
int py = tile/tiles_per_row*height();
painter.drawPixmap(
x,
y,
pm,
px,py,
width(),height()
);
}
void NetHackQtGlyphs::drawCell(QPainter& painter, int glyph, int cellx, int celly)
{
drawGlyph(painter,glyph,cellx*width(),celly*height());
}
void NetHackQtGlyphs::setSize(int w, int h)
{
if ( size == QSize(w,h) )
return;
bool was1 = size == pm1.size();
size = QSize(w,h);
if (!w || !h)
return; // Still not decided
if ( size == pm1.size() ) {
pm = pm1;
return;
}
if ( size == pm2.size() ) {
pm = pm2;
return;
}
if (w==tilefile_tile_W && h==tilefile_tile_H) {
pm.convertFromImage(img);
} else {
QApplication::setOverrideCursor( Qt::waitCursor );
QImage scaled = img.smoothScale(
w*img.width()/tilefile_tile_W,
h*img.height()/tilefile_tile_H
);
pm.convertFromImage(scaled,Qt::ThresholdDither|Qt::PreferDither);
QApplication::restoreOverrideCursor();
}
(was1 ? pm2 : pm1) = pm;
}
//////////////////////////////////////////////////////////////
//
// The ugly C binding classes...
//
//////////////////////////////////////////////////////////////
NetHackQtMenuOrTextWindow::NetHackQtMenuOrTextWindow(NetHackQtKeyBuffer& ks) :
actual(0),
keysource(ks)
{
}
QWidget* NetHackQtMenuOrTextWindow::Widget()
{
if (!actual) impossible("Widget called before we know if Menu or Text");
return actual->Widget();
}
// Text
void NetHackQtMenuOrTextWindow::Clear()
{
if (!actual) impossible("Clear called before we know if Menu or Text");
actual->Clear();
}
void NetHackQtMenuOrTextWindow::Display(bool block)
{
if (!actual) impossible("Display called before we know if Menu or Text");
actual->Display(block);
}
bool NetHackQtMenuOrTextWindow::Destroy()
{
if (!actual) impossible("Destroy called before we know if Menu or Text");
return actual->Destroy();
}
void NetHackQtMenuOrTextWindow::PutStr(int attr, const char* text)
{
if (!actual) actual=new NetHackQtTextWindow(keysource);
actual->PutStr(attr,text);
}
// Menu
void NetHackQtMenuOrTextWindow::StartMenu()
{
if (!actual) actual=new NetHackQtMenuWindow(keysource);
actual->StartMenu();
}
void NetHackQtMenuOrTextWindow::AddMenu(int glyph, const ANY_P* identifier, char ch, char gch, int attr,
const char* str, bool presel)
{
if (!actual) impossible("AddMenu called before we know if Menu or Text");
actual->AddMenu(glyph,identifier,ch,gch,attr,str,presel);
}
void NetHackQtMenuOrTextWindow::EndMenu(const char* prompt)
{
if (!actual) impossible("EndMenu called before we know if Menu or Text");
actual->EndMenu(prompt);
}
int NetHackQtMenuOrTextWindow::SelectMenu(int how, MENU_ITEM_P **menu_list)
{
if (!actual) impossible("SelectMenu called before we know if Menu or Text");
return actual->SelectMenu(how,menu_list);
}
// XXX Should be from Options
//
// XXX Hmm. Tricky part is that perhaps some macros should only be active
// XXX when a key is about to be gotten. For example, the user could
// XXX define "-" to do "E-yyyyyyyy\r", but would still need "-" for
// XXX other purposes. Maybe just too bad.
//
struct {
int key;
int state;
const char* macro;
} key_macro[]={
{ Qt::Key_F1, 0, "n100." }, // Rest (x100)
{ Qt::Key_F2, 0, "n20s" }, // Search (x20)
{ Qt::Key_F3, 0, "o8o4o6o2o8o4o6o2o8o4o6o2" }, // Open all doors (x3)
{ Qt::Key_Tab, 0, "\001" },
{ 0, 0, 0 }
};
NetHackQtBind::NetHackQtBind(int& argc, char** argv) :
#ifdef KDE
KApplication(argc,argv)
#elif defined(QWS) // not quite the right condition
QPEApplication(argc,argv)
#else
QApplication(argc,argv)
#endif
{
QPixmap pm("nhsplash.xpm");
if ( iflags.wc_splash_screen && !pm.isNull() ) {
QVBox *vb = new QVBox(0,0,
WStyle_Customize | WStyle_NoBorder | nh_WX11BypassWM | WStyle_StaysOnTop );
splash = vb;
QLabel *lsplash = new QLabel(vb);
lsplash->setAlignment(AlignCenter);
lsplash->setPixmap(pm);
QLabel* capt = new QLabel("Loading...",vb);
capt->setAlignment(AlignCenter);
if ( pm.mask() ) {
lsplash->setFixedSize(pm.size());
lsplash->setMask(*pm.mask());
}
splash->move((QApplication::desktop()->width()-pm.width())/2,
(QApplication::desktop()->height()-pm.height())/2);
//splash->setGeometry(0,0,100,100);
if ( qt_compact_mode ) {
splash->showMaximized();
} else {
vb->setFrameStyle(QFrame::WinPanel|QFrame::Raised);
vb->setMargin(10);
splash->adjustSize();
splash->show();
}
// force content refresh outside event loop
splash->repaint(FALSE);
lsplash->repaint(FALSE);
capt->repaint(FALSE);
qApp->flushX();
} else {
splash = 0;
}
main = new NetHackQtMainWindow(keybuffer);
#if defined(QWS) // not quite the right condition
showMainWidget(main);
#else
setMainWidget(main);
#endif
qt_settings=new NetHackQtSettings(main->width(),main->height());
}
void NetHackQtBind::qt_init_nhwindows(int* argc, char** argv)
{
#ifdef UNIX
// Userid control
//
// Michael Hohmuth <hohmuth@inf.tu-dresden.de>...
//
// As the game runs setuid games, it must seteuid(getuid()) before
// calling XOpenDisplay(), and reset the euid afterwards.
// Otherwise, it can't read the $HOME/.Xauthority file and whines about
// not being able to open the X display (if a magic-cookie
// authorization mechanism is being used).
uid_t gamesuid=geteuid();
seteuid(getuid());
#endif
QApplication::setColorSpec(ManyColor);
instance=new NetHackQtBind(*argc,argv);
#ifdef UNIX
seteuid(gamesuid);
#endif
#ifdef _WS_WIN_
// This nethack engine feature should be moved into windowport API
nt_kbhit = NetHackQtBind::qt_kbhit;
#endif
}
int NetHackQtBind::qt_kbhit()
{
return !keybuffer.Empty();
}
static bool have_asked = FALSE;
void NetHackQtBind::qt_player_selection()
{
if ( !have_asked )
qt_askname();
}
NetHackQtSavedGameSelector::NetHackQtSavedGameSelector(const char** saved) :
QDialog(qApp->mainWidget(),"sgsel",TRUE)
{
QVBoxLayout *vbl = new QVBoxLayout(this,6);
QHBox* hb;
QLabel* logo = new QLabel(this); vbl->addWidget(logo);
logo->setAlignment(AlignCenter);
logo->setPixmap(QPixmap("nhsplash.xpm"));
QLabel* attr = new QLabel("by the NetHack DevTeam",this);
attr->setAlignment(AlignCenter);
vbl->addWidget(attr);
vbl->addStretch(2);
/*
QLabel* logo = new QLabel(hb);
hb = new QHBox(this);
vbl->addWidget(hb, AlignCenter);
logo->setPixmap(QPixmap(nh_icon));
logo->setAlignment(AlignRight|AlignVCenter);
new QLabel(nh_attribution,hb);
*/
hb = new QHBox(this);
vbl->addWidget(hb, AlignCenter);
QPushButton* q = new QPushButton("Quit",hb);
connect(q, SIGNAL(clicked()), this, SLOT(reject()));
QPushButton* c = new QPushButton("New Game",hb);
connect(c, SIGNAL(clicked()), this, SLOT(accept()));
c->setDefault(TRUE);
QButtonGroup* bg = new QButtonGroup(3, Horizontal, "Saved Characters",this);
vbl->addWidget(bg);
connect(bg, SIGNAL(clicked(int)), this, SLOT(done(int)));
for (int i=0; saved[i]; i++) {
QPushButton* b = new QPushButton(saved[i],bg);
bg->insert(b, i+2);
}
}
int NetHackQtSavedGameSelector::choose()
{
#if defined(QWS) // probably safe with Qt 3, too (where show!=exec in QDialog).
if ( qt_compact_mode )
showMaximized();
#endif
return exec()-2;
}
void NetHackQtBind::qt_askname()
{
have_asked = TRUE;
// We do it all here, and nothing in askname
char** saved = get_saved_games();
int ch = -1;
if ( saved && *saved ) {
if ( splash ) splash->hide();
NetHackQtSavedGameSelector sgsel((const char**)saved);
ch = sgsel.choose();
if ( ch >= 0 )
strcpy(plname,saved[ch]);
}
free_saved_games(saved);
switch (ch) {
case -1:
if ( splash ) splash->hide();
if (NetHackQtPlayerSelector(keybuffer).Choose())
return;
case -2:
break;
default:
return;
}
// Quit
clearlocks();
qt_exit_nhwindows(0);
terminate(0);
}
void NetHackQtBind::qt_get_nh_event()
{
}
#if defined(QWS)
// Kludge to access lastWindowClosed() signal.
class TApp : public QApplication {
public:
TApp(int& c, char**v) : QApplication(c,v) {}
void lwc() { emit lastWindowClosed(); }
};
#endif
void NetHackQtBind::qt_exit_nhwindows(const char *)
{
#if defined(QWS)
// Avoids bug in SHARP SL5500
((TApp*)qApp)->lwc();
qApp->quit();
#endif
delete instance; // ie. qApp
}
void NetHackQtBind::qt_suspend_nhwindows(const char *)
{
}
void NetHackQtBind::qt_resume_nhwindows()
{
}
static QArray<NetHackQtWindow*> id_to_window;
winid NetHackQtBind::qt_create_nhwindow(int type)
{
winid id;
for (id = 0; id < (winid) id_to_window.size(); id++) {
if ( !id_to_window[id] )
break;
}
if ( id == (winid) id_to_window.size() )
id_to_window.resize(id+1);
NetHackQtWindow* window=0;
switch (type) {
case NHW_MAP: {
NetHackQtMapWindow* w=new NetHackQtMapWindow(clickbuffer);
main->AddMapWindow(w);
window=w;
} break; case NHW_MESSAGE: {
NetHackQtMessageWindow* w=new NetHackQtMessageWindow;
main->AddMessageWindow(w);
window=w;
} break; case NHW_STATUS: {
NetHackQtStatusWindow* w=new NetHackQtStatusWindow;
main->AddStatusWindow(w);
window=w;
} break; case NHW_MENU:
window=new NetHackQtMenuOrTextWindow(keybuffer);
break; case NHW_TEXT:
window=new NetHackQtTextWindow(keybuffer);
}
window->nhid = id;
// Note: use of isHidden does not work with Qt 2.1
if ( splash
#if QT_VERSION >= 300
&& !main->isHidden()
#else
&& main->isVisible()
#endif
)
{
delete splash;
splash = 0;
}
id_to_window[id] = window;
return id;
}
void NetHackQtBind::qt_clear_nhwindow(winid wid)
{
NetHackQtWindow* window=id_to_window[wid];
window->Clear();
}
void NetHackQtBind::qt_display_nhwindow(winid wid, BOOLEAN_P block)
{
NetHackQtWindow* window=id_to_window[wid];
window->Display(block);
}
void NetHackQtBind::qt_destroy_nhwindow(winid wid)
{
NetHackQtWindow* window=id_to_window[wid];
main->RemoveWindow(window);
if (window->Destroy())
delete window;
id_to_window[wid] = 0;
}
void NetHackQtBind::qt_curs(winid wid, int x, int y)
{
NetHackQtWindow* window=id_to_window[wid];
window->CursorTo(x,y);
}
void NetHackQtBind::qt_putstr(winid wid, int attr, const char *text)
{
NetHackQtWindow* window=id_to_window[wid];
window->PutStr(attr,text);
}
#ifdef FILE_AREAS
void NetHackQtBind::qt_display_file(const char *filearea, const char *filename, BOOLEAN_P must_exist)
#else
void NetHackQtBind::qt_display_file(const char *filename, BOOLEAN_P must_exist)
#endif
{
NetHackQtTextWindow* window=new NetHackQtTextWindow(keybuffer);
bool complain = FALSE;
#ifdef DLB
{
dlb *f;
char buf[BUFSZ];
char *cr;
window->Clear();
#ifdef FILE_AREAS
f = dlb_fopen_area(filearea, filename, "r");
#else
f = dlb_fopen(filename, "r");
#endif
if (!f) {
complain = must_exist;
} else {
while (dlb_fgets(buf, BUFSZ, f)) {
if ((cr = index(buf, '\n')) != 0) *cr = 0;
#ifdef MSDOS
if ((cr = index(buf, '\r')) != 0) *cr = 0;
#endif
if (index(buf, '\t') != 0) (void) tabexpand(buf);
window->PutStr(ATR_NONE, buf);
}
window->Display(FALSE);
(void) dlb_fclose(f);
}
}
#else
#ifndef FILE_AREAS
QFile file(filename);
#else
char *path;
path = make_file_name(filearea, filename);
QFile file(path);
#endif
if (file.open(IO_ReadOnly)) {
char line[128];
while (file.readLine(line,127) >= 0) {
line[strlen(line)-1]=0;// remove newline
window->PutStr(ATR_NONE,line);
}
window->Display(FALSE);
} else {
complain = must_exist;
}
#ifdef FILE_AREAS
free(path);
#endif
#endif
if (complain) {
QString message;
message.sprintf("File not found: %s\n",filename);
QMessageBox::message("File Error", (const char*)message, "Ignore");
}
}
void NetHackQtBind::qt_start_menu(winid wid)
{
NetHackQtWindow* window=id_to_window[wid];
window->StartMenu();
}
void NetHackQtBind::qt_add_menu(winid wid, int glyph,
const ANY_P * identifier, CHAR_P ch, CHAR_P gch, int attr,
const char *str, BOOLEAN_P presel)
{
NetHackQtWindow* window=id_to_window[wid];
window->AddMenu(glyph, identifier, ch, gch, attr, str, presel);
}
void NetHackQtBind::qt_end_menu(winid wid, const char *prompt)
{
NetHackQtWindow* window=id_to_window[wid];
window->EndMenu(prompt);
}
int NetHackQtBind::qt_select_menu(winid wid, int how, MENU_ITEM_P **menu_list)
{
NetHackQtWindow* window=id_to_window[wid];
return window->SelectMenu(how,menu_list);
}
void NetHackQtBind::qt_update_inventory()
{
if (main)
main->updateInventory();
/* doesn't work yet
if (program_state.something_worth_saving && flags.perm_invent)
display_inventory(NULL, FALSE);
*/
}
void NetHackQtBind::qt_mark_synch()
{
}
void NetHackQtBind::qt_wait_synch()
{
}
void NetHackQtBind::qt_cliparound(int x, int y)
{
// XXXNH - winid should be a parameter!
qt_cliparound_window(WIN_MAP,x,y);
}
void NetHackQtBind::qt_cliparound_window(winid wid, int x, int y)
{
NetHackQtWindow* window=id_to_window[wid];
window->ClipAround(x,y);
}
void NetHackQtBind::qt_print_glyph(winid wid,XCHAR_P x,XCHAR_P y,int glyph)
{
NetHackQtWindow* window=id_to_window[wid];
window->PrintGlyph(x,y,glyph);
}
//void NetHackQtBind::qt_print_glyph_compose(winid wid,XCHAR_P x,XCHAR_P y,int glyph1, int glyph2)
//{
//NetHackQtWindow* window=id_to_window[wid];
//window->PrintGlyphCompose(x,y,glyph1,glyph2);
//}
void NetHackQtBind::qt_raw_print(const char *str)
{
puts(str);
}
void NetHackQtBind::qt_raw_print_bold(const char *str)
{
puts(str);
}
int NetHackQtBind::qt_nhgetch()
{
if (main)
main->fadeHighlighting();
// Process events until a key arrives.
//
while (keybuffer.Empty()) {
qApp->enter_loop();
}
return keybuffer.GetAscii();
}
int NetHackQtBind::qt_nh_poskey(int *x, int *y, int *mod)
{
if (main)
main->fadeHighlighting();
// Process events until a key or map-click arrives.
//
while (keybuffer.Empty() && clickbuffer.Empty()) {
qApp->enter_loop();
}
if (!keybuffer.Empty()) {
return keybuffer.GetAscii();
} else {
*x=clickbuffer.NextX();
*y=clickbuffer.NextY();
*mod=clickbuffer.NextMod();
clickbuffer.Get();
return 0;
}
}
void NetHackQtBind::qt_nhbell()
{
QApplication::beep();
}
int NetHackQtBind::qt_doprev_message()
{
// Don't need it - uses scrollbar
// XXX but could make this a shortcut
return 0;
}
char NetHackQtBind::qt_yn_function(const char *question, const char *choices, CHAR_P def)
{
if (qt_settings->ynInMessages() && WIN_MESSAGE!=WIN_ERR) {
// Similar to X11 windowport `slow' feature.
char message[BUFSZ];
char yn_esc_map='\033';
if (choices) {
char *cb, choicebuf[QBUFSZ];
Strcpy(choicebuf, choices);
if ((cb = index(choicebuf, '\033')) != 0) {
// anything beyond <esc> is hidden
*cb = '\0';
}
Sprintf(message, "%s [%s] ", question, choicebuf);
if (def) Sprintf(eos(message), "(%c) ", def);
// escape maps to 'q' or 'n' or default, in that order
yn_esc_map = (index(choices, 'q') ? 'q' :
(index(choices, 'n') ? 'n' : def));
} else {
Strcpy(message, question);
}
#ifdef USE_POPUPS
// Improve some special-cases (DIRKS 08/02/23)
if (strcmp (choices,"ynq") == 0) {
switch (QMessageBox::information (qApp->mainWidget(),DEF_GAME_NAME,
question,"&Yes","&No","&Quit",0,2))
{
case 0: return 'y';
case 1: return 'n';
case 2: return 'q';
}
}
if (strcmp (choices,"yn") == 0) {
switch (QMessageBox::information(qApp->mainWidget(),DEF_GAME_NAME,
question,"&Yes", "&No",0,1))
{
case 0: return 'y';
case 1: return 'n';
}
}
#endif
NetHackQtBind::qt_putstr(WIN_MESSAGE, ATR_BOLD, message);
int result=-1;
while (result<0) {
char ch=NetHackQtBind::qt_nhgetch();
if (ch=='\033') {
result=yn_esc_map;
} else if (choices && !index(choices,ch)) {
if (def && (ch==' ' || ch=='\r' || ch=='\n')) {
result=def;
} else {
NetHackQtBind::qt_nhbell();
// and try again...
}
} else {
result=ch;
}
}
NetHackQtBind::qt_clear_nhwindow(WIN_MESSAGE);
return result;
} else {
NetHackQtYnDialog dialog(keybuffer,question,choices,def);
return dialog.Exec();
}
}
void NetHackQtBind::qt_getlin(const char *prompt, char *line)
{
NetHackQtStringRequestor requestor(keybuffer,prompt);
if (!requestor.Get(line)) {
line[0]=0;
}
}
NetHackQtExtCmdRequestor::NetHackQtExtCmdRequestor(NetHackQtKeyBuffer& ks) :
QDialog(qApp->mainWidget(), "ext-cmd", FALSE),
keysource(ks)
{
int marg=4;
QVBoxLayout *l = new QVBoxLayout(this,marg,marg);
QPushButton* can = new QPushButton("Cancel", this);
can->setDefault(TRUE);
can->setMinimumSize(can->sizeHint());
l->addWidget(can);
QButtonGroup *group=new QButtonGroup("",0);
QGroupBox *grid=new QGroupBox("Extended commands",this);
l->addWidget(grid);
int i;
int butw=50;
QFontMetrics fm = fontMetrics();
for (i=0; extcmdlist[i].ef_txt; i++) {
butw = QMAX(butw,30+fm.width(extcmdlist[i].ef_txt));
}
int ncols=4;
int nrows=(i+ncols-1)/ncols;
QVBoxLayout* bl = new QVBoxLayout(grid,marg);
bl->addSpacing(fm.height());
QGridLayout* gl = new QGridLayout(nrows,ncols,marg);
bl->addLayout(gl);
for (i=0; extcmdlist[i].ef_txt; i++) {
QPushButton* pb=new QPushButton(extcmdlist[i].ef_txt, grid);
pb->setMinimumSize(butw,pb->sizeHint().height());
group->insert(pb);
gl->addWidget(pb,i/ncols,i%ncols);
}
connect(group,SIGNAL(clicked(int)),this,SLOT(done(int)));
bl->activate();
l->activate();
resize(1,1);
connect(can,SIGNAL(clicked()),this,SLOT(cancel()));
}
void NetHackQtExtCmdRequestor::cancel()
{
setResult(-1);
qApp->exit_loop();
}
void NetHackQtExtCmdRequestor::done(int i)
{
setResult(i);
qApp->exit_loop();
}
int NetHackQtExtCmdRequestor::get()
{
const int none = -10;
char str[32];
int cursor=0;
resize(1,1); // pack
centerOnMain(this);
show();
setResult(none);
while (result()==none) {
while (result()==none && !keysource.Empty()) {
char k=keysource.GetAscii();
if (k=='\r' || k=='\n' || k==' ' || k=='\033') {
setResult(-1);
} else {
str[cursor++] = k;
int r=-1;
for (int i=0; extcmdlist[i].ef_txt; i++) {
if (qstrnicmp(str, extcmdlist[i].ef_txt, cursor)==0) {
if ( r == -1 )
r = i;
else
r = -2;
}
}
if ( r == -1 ) { // no match!
QApplication::beep();
cursor=0;
} else if ( r != -2 ) { // only one match
setResult(r);
}
}
}
if (result()==none)
qApp->enter_loop();
}
hide();
return result();
}
int NetHackQtBind::qt_get_ext_cmd()
{
NetHackQtExtCmdRequestor requestor(keybuffer);
return requestor.get();
}
void NetHackQtBind::qt_number_pad(int)
{
// Ignore.
}
void NetHackQtBind::qt_delay_output()
{
NetHackQtDelay delay(15);
delay.wait();
}
void NetHackQtBind::qt_start_screen()
{
// Ignore.
}
void NetHackQtBind::qt_end_screen()
{
// Ignore.
}
void NetHackQtBind::qt_outrip(winid wid, int how)
{
NetHackQtWindow* window=id_to_window[wid];
window->UseRIP(how);
}
bool NetHackQtBind::notify(QObject *receiver, QEvent *event)
{
// Ignore Alt-key navigation to menubar, it's annoying when you
// use Alt-Direction to move around.
if ( main && event->type()==QEvent::KeyRelease && main==receiver
&& ((QKeyEvent*)event)->key() == Key_Alt )
return TRUE;
bool result=QApplication::notify(receiver,event);
if (event->type()==QEvent::KeyPress) {
QKeyEvent* key_event=(QKeyEvent*)event;
if (!key_event->isAccepted()) {
const int k=key_event->key();
bool macro=FALSE;
for (int i=0; !macro && key_macro[i].key; i++) {
if (key_macro[i].key==k
&& ((key_macro[i].state&key_event->state())==key_macro[i].state))
{
keybuffer.Put(key_macro[i].macro);
macro=TRUE;
}
}
char ch=key_event->ascii();
if ( !ch && (key_event->state() & Qt::ControlButton) ) {
// On Mac, ascii control codes are not sent, force them.
if ( k>=Qt::Key_A && k<=Qt::Key_Z )
ch = k - Qt::Key_A + 1;
}
if (!macro && ch) {
bool alt = (key_event->state()&AltButton) ||
(k >= Key_0 && k <= Key_9 && (key_event->state()&ControlButton));
keybuffer.Put(key_event->key(),ch + (alt ? 128 : 0),
key_event->state());
key_event->accept();
result=TRUE;
}
if (ch || macro) {
qApp->exit_loop();
}
}
}
return result;
}
NetHackQtBind* NetHackQtBind::instance=0;
NetHackQtKeyBuffer NetHackQtBind::keybuffer;
NetHackQtClickBuffer NetHackQtBind::clickbuffer;
NetHackQtMainWindow* NetHackQtBind::main=0;
QWidget* NetHackQtBind::splash=0;
extern "C" struct window_procs Qt_procs;
struct window_procs Qt_procs = {
"Qt",
WC_COLOR|WC_HILITE_PET|
WC_ASCII_MAP|WC_TILED_MAP|
WC_FONT_MAP|WC_TILE_FILE|WC_TILE_WIDTH|WC_TILE_HEIGHT|
WC_PLAYER_SELECTION|WC_SPLASH_SCREEN,
0L,
NetHackQtBind::qt_init_nhwindows,
NetHackQtBind::qt_player_selection,
NetHackQtBind::qt_askname,
NetHackQtBind::qt_get_nh_event,
NetHackQtBind::qt_exit_nhwindows,
NetHackQtBind::qt_suspend_nhwindows,
NetHackQtBind::qt_resume_nhwindows,
NetHackQtBind::qt_create_nhwindow,
NetHackQtBind::qt_clear_nhwindow,
NetHackQtBind::qt_display_nhwindow,
NetHackQtBind::qt_destroy_nhwindow,
NetHackQtBind::qt_curs,
NetHackQtBind::qt_putstr,
NetHackQtBind::qt_display_file,
NetHackQtBind::qt_start_menu,
NetHackQtBind::qt_add_menu,
NetHackQtBind::qt_end_menu,
NetHackQtBind::qt_select_menu,
genl_message_menu, /* no need for X-specific handling */
NetHackQtBind::qt_update_inventory,
NetHackQtBind::qt_mark_synch,
NetHackQtBind::qt_wait_synch,
#ifdef CLIPPING
NetHackQtBind::qt_cliparound,
#endif
#ifdef POSITIONBAR
donull,
#endif
NetHackQtBind::qt_print_glyph,
//NetHackQtBind::qt_print_glyph_compose,
NetHackQtBind::qt_raw_print,
NetHackQtBind::qt_raw_print_bold,
NetHackQtBind::qt_nhgetch,
NetHackQtBind::qt_nh_poskey,
NetHackQtBind::qt_nhbell,
NetHackQtBind::qt_doprev_message,
NetHackQtBind::qt_yn_function,
NetHackQtBind::qt_getlin,
NetHackQtBind::qt_get_ext_cmd,
NetHackQtBind::qt_number_pad,
NetHackQtBind::qt_delay_output,
#ifdef CHANGE_COLOR /* only a Mac option currently */
donull,
donull,
#endif
/* other defs that really should go away (they're tty specific) */
NetHackQtBind::qt_start_screen,
NetHackQtBind::qt_end_screen,
#ifdef GRAPHIC_TOMBSTONE
NetHackQtBind::qt_outrip,
#else
genl_outrip,
#endif
genl_preference_update,
};
extern "C" void play_usersound(const char* filename, int volume)
{
#ifdef USER_SOUNDS
#ifndef QT_NO_SOUND
QSound::play(filename);
#endif
#endif
}
#include "qt_win.moc"
#ifndef KDE
#include "qt_kde0.moc"
#endif
#if QT_VERSION >= 300
#include "qttableview.moc"
#endif
|