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
|
/*
* This file is part of the XForms library package.
*
* XForms is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1, or
* (at your option) any later version.
*
* XForms is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with XForms. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "include/forms.h"
#include "flinternal.h"
#include "private/flvasprintf.h"
#include <ctype.h>
/***************************************
* Local global variables
***************************************/
/* Head of linked list of popups */
static FL_POPUP *popups = NULL;
/* Default policy (i.e. does popup get closed when the user releases
the mouse button when not on an active entry or not?) */
static int popup_policy;
/* Default font styles and sizes for title and entries */
static int popup_entry_font_style;
static int popup_title_font_style;
static int popup_entry_font_size;
static int popup_title_font_size;
/* Default color of popup */
static FL_COLOR popup_bg_color;
/* Default color of entry the mouse is over (unless disabled) */
static FL_COLOR popup_on_color;
/* Default color of title text */
static FL_COLOR popup_title_color;
/* Default color of normal entrys text */
static FL_COLOR popup_text_color;
/* Default color of entrys text when mouse is on the entry (unless disabled) */
static FL_COLOR popup_text_on_color;
/* Default color of disabled entrys text */
static FL_COLOR popup_text_off_color;
/* Default color of button for radio entries */
static FL_COLOR popup_radio_color;
/* Default popup border width */
static int popup_bw;
/* Default cursor type to be used */
static int popup_cursor;
/***************************************
* Local functions *
***************************************/
static FL_POPUP_ENTRY *parse_entries( FL_POPUP *, char *, va_list,
const char *, int );
static FL_POPUP_ENTRY * failed_add( FL_POPUP_ENTRY * );
static int check_sub( FL_POPUP_ENTRY * );
static void radio_check( FL_POPUP_ENTRY * );
static void convert_shortcut( const char *, FL_POPUP_ENTRY * );
static void recalc_popup( FL_POPUP * );
static void title_dimensions( FL_POPUP *, unsigned int *, unsigned int * );
static void entry_text_dimensions( FL_POPUP_ENTRY *, unsigned int *,
unsigned int * );
static void draw_popup( FL_POPUP * );
static void draw_title( FL_POPUP * );
static void draw_entry( FL_POPUP_ENTRY * );
static void calculate_window_position( FL_POPUP * );
static void create_popup_window( FL_POPUP * );
static void grab( FL_POPUP * );
static void close_popup( FL_POPUP *, int );
static FL_POPUP_RETURN * popup_interaction( FL_POPUP * );
static int is_on_popups( FL_POPUP * pooup, int x, int y );
static FL_POPUP_RETURN * handle_selection( FL_POPUP_ENTRY * );
static FL_POPUP * handle_motion( FL_POPUP *, int, int );
static void motion_shift_window( FL_POPUP *, int *, int * );
static FL_POPUP * handle_key( FL_POPUP *, XKeyEvent *, FL_POPUP_ENTRY ** );
static void key_shift_window( FL_POPUP *, FL_POPUP_ENTRY * );
static FL_POPUP * open_subpopup( FL_POPUP_ENTRY * );
static FL_POPUP_ENTRY * handle_shortcut( FL_POPUP *, long, unsigned int );
static void enter_leave( FL_POPUP_ENTRY *, int );
static FL_POPUP * find_popup( int, int );
static FL_POPUP_ENTRY * find_entry( FL_POPUP *, int, int );
static void setup_subpopups( FL_POPUP * );
static char * cleanup_string( char * );
static void set_need_recalc( FL_POPUP * );
/***************************************
* #defines
***************************************/
/* Inner padding is used within the title box of popups only */
#define INNER_PADDING_LEFT 3
#define INNER_PADDING_RIGHT 3
#define INNER_PADDING_TOP 3
#define INNER_PADDING_BOTTOM 3
/* Spacing around title and entries of a popup */
#define OUTER_PADDING_LEFT 3
#define OUTER_PADDING_RIGHT 3
#define OUTER_PADDING_TOP 0
#define OUTER_PADDING_BOTTOM 0
/* Vert. spacing between symbols (box, hook, triangle) and text in an entry */
#define SYMBOL_PADDING 0
/* Height of the box for separation line in popup, must be at least 3 */
#define LINE_HEIGHT 4
/* Extra offsets added by fl_drw_text() that need to be taken into account
(see variables 'xoff' and 'yoff' of 'fli_draw_text_inside()' in xtext.c) */
#define STR_OFFSET_X 5
#define STR_OFFSET_Y 4
/* Amount window is shifted in horizontal direction if the popup doesn't fit on
the screen in that direction and delay (in usec) between shifts (in both
directions) when the user is pushing the mouse at the screen borders */
#define WINDOW_SHIFT ( fl_scrw / 10 )
#define WINDOW_SHIFT_DELAY 100000
/* Macro for testing if a popup entry can be made "active" (i.e. if it
gets highlighted when under the mouse) */
#define IS_ACTIVATABLE( e ) \
( ( e )->type != FL_POPUP_LINE \
&& ! ( ( e ) ->state & ( FL_POPUP_HIDDEN | FL_POPUP_DISABLED ) ) )
/***************************************
* Create a new popup
***************************************/
FL_POPUP *
fl_popup_add( Window win,
const char * title )
{
return fli_popup_add( win, title, "fl_popup_add" );
}
/***************************************
* Internal function to create a new popup
***************************************/
FL_POPUP *
fli_popup_add( Window win,
const char * title,
const char * caller )
{
FL_POPUP *p;
/* Try to get memory for the popup itself and the optional title string */
if ( ( p = fl_malloc( sizeof *p ) ) == NULL )
{
M_err( caller, "Running out of memory" );
return NULL;
}
if ( ! title || ! *title )
p->title = NULL;
else if ( ( p->title = fl_strdup( title ) ) == NULL )
{
fl_free( p );
M_err( caller, "Running out of memory" );
return NULL;
}
/* Link the new popup into the list of popups */
p->next = NULL;
if ( popups == NULL )
{
popups = p;
p->prev = NULL;
}
else
{
p->prev = popups;
while ( p->prev->next != NULL )
p->prev = p->prev->next;
p->prev->next = p;
}
p->parent = NULL;
p->top_parent = p; /* points at itself except for sub-popups */
p->win = None;
p->parent_win = win != None ? win : fl_root;
p->cursor = fli_get_cursor_byname( popup_cursor );
p->entries = NULL;
p->callback = NULL;
p->use_req_pos = 0;
p->need_recalc = 1;
p->min_width = 0;
p->has_subs = 0;
p->has_boxes = 0;
p->counter = 0;
p->policy = popup_policy;
fl_popup_set_title_font( p, popup_title_font_style, popup_title_font_size );
fl_popup_entry_set_font( p, popup_entry_font_style, popup_entry_font_size );
p->bw = popup_bw;
p->on_color = popup_on_color;
p->bg_color = popup_bg_color;
p->title_color = popup_title_color;
p->text_color = popup_text_color;
p->text_on_color = popup_text_on_color;
p->text_off_color = popup_text_off_color;
p->radio_color = popup_radio_color;
return p;
}
/***************************************
* Add (append) entries to a popup
***************************************/
FL_POPUP_ENTRY *
fl_popup_add_entries( FL_POPUP * popup,
const char * entries,
... )
{
FL_POPUP_ENTRY *new_entries;
va_list ap;
va_start( ap, entries );
new_entries = fli_popup_add_entries( popup, entries, ap,
"fl_popup_add_entries", 0 );
va_end( ap );
return new_entries;
}
/***************************************
* Internal function to add entries
***************************************/
FL_POPUP_ENTRY *
fli_popup_add_entries( FL_POPUP * popup,
const char * entries,
va_list ap,
const char * caller,
int simple )
{
FL_POPUP_ENTRY *new_entries,
*e;
char *str;
/* Calling this function with no string for the entries doesn't make
sense */
if ( entries == NULL )
{
M_err( caller, "NULL entries argument" );
return NULL;
}
/* Check if the popup we're supposed to add to does exist at all */
if ( fli_check_popup_exists( popup ) )
{
M_err( caller, "Popup does not exist" );
return NULL;
}
/* Now analyze the string with the information about the entries */
if ( ( str = fl_strdup( entries ) ) == NULL )
{
M_err( caller, "Running out of memory" );
return NULL;
}
new_entries = parse_entries( popup, str, ap, caller, simple );
fl_free( str );
/* A return value of NULL says something went wrong (warning was altready
output) */
if ( new_entries == NULL )
return NULL;
/* Now all left to do is append the list of new entries to the list of
already existing ones (if there are any) */
if ( popup->entries == NULL )
popup->entries = new_entries;
else
{
for ( e = popup->entries; e->next != NULL; e = e->next )
/* empty */ ;
e->next = new_entries;
new_entries->prev = e;
}
/* Make sure all sub-popus are set up correctly */
setup_subpopups( popup );
/* Set flag that indicates that the dimension of the popup have to be
recalculated */
set_need_recalc( popup );
/* Return a pointer to the first of the newly added entries */
return new_entries;
}
/***************************************
* Insert new entries after an entry (use NULL for inserting at the start)
***************************************/
FL_POPUP_ENTRY *
fl_popup_insert_entries( FL_POPUP * popup,
FL_POPUP_ENTRY * after,
const char * entries,
... )
{
FL_POPUP_ENTRY *new_entries;
va_list ap;
va_start( ap, entries );
new_entries = fli_popup_insert_entries( popup, after, entries, ap,
"fl_popup_insert_entries", 0 );
va_end( ap );
return new_entries;
}
/***************************************
* Internal function to insert new entries after an entry
***************************************/
FL_POPUP_ENTRY *
fli_popup_insert_entries( FL_POPUP * popup,
FL_POPUP_ENTRY * after,
const char * entries,
va_list ap,
const char * caller,
int simple)
{
FL_POPUP_ENTRY *new_entries,
*new_last,
*e;
char *str;
if ( after != NULL )
{
for ( e = popup->entries; e != NULL; e = e->next )
if ( e == after )
break;
if ( e == NULL )
{
M_err( caller, "Invalid 'after' argument" );
return NULL;
}
}
/* Calling this function with no string for the entries doesn't make
sense */
if ( entries == NULL )
{
M_err( caller, "NULL entries argument" );
return NULL;
}
/* Check if the popup we're supposed to add to does exist at all */
if ( fli_check_popup_exists( popup ) )
{
M_err( caller, "Popup does not exist" );
return NULL;
}
/* Now analyze the string with the information about the entries */
if ( ( str = fl_strdup( entries ) ) == NULL )
{
M_err( caller, "Running out of memory" );
return NULL;
}
new_entries = parse_entries( popup, str, ap, "fl_popup_insert_entries",
simple );
fl_free( str );
/* A return value of NULL says something went wrong (warning was already
output) */
if ( new_entries == NULL )
return NULL;
/* Now all left to do is insert the list of new entries into the list of
already existing ones. 'after' being NULL means at the start of the
list. */
for ( new_last = new_entries;
new_last->next != NULL; new_last = new_last->next )
/* empty */ ;
if ( after == NULL )
{
if ( popup->entries != NULL )
{
new_last->next = popup->entries;
popup->entries->prev = new_last;
}
popup->entries = new_entries;
}
else
{
if ( after->next )
after->next->prev = new_last;
new_last->next = after->next;
new_entries->prev = after;
after->next = new_entries;
}
/* Make sure all sub-popus are set up correctly */
setup_subpopups( popup );
/* Set flag that indicates that the dimension of the popup have to be
recalculated */
set_need_recalc( popup );
/* Return a pointer to the first of the newly added entries */
return new_entries;
}
/***************************************
* Internal function for inserting entries into a popup after entry
* 'after' (NULL stands for "insert at start") from a list of FL_POPUP_ITEM
* structures - they are converted into a string that then gets passed
* with the required additional arguments to fl_popup_insert_entries()
***************************************/
FL_POPUP_ENTRY *
fli_popup_insert_items( FL_POPUP * popup,
FL_POPUP_ENTRY * after,
FL_POPUP_ITEM * entries,
const char * caller)
{
FL_POPUP_ITEM *e;
const char *c;
char *txt;
size_t cnt;
static long val = 0;
int level = 0;
int need_line = 0;
int is_sub = 0;
FL_POPUP_ENTRY *entry = NULL;
int first = 1;
/* Return if the array of items is NULL */
if ( entries == NULL )
return NULL;
/* Check if the popup we're supposed to add to does exist at all */
if ( fli_check_popup_exists( popup ) )
{
M_err( caller, "Popup does not exist" );
return NULL;
}
/* If 'after' isn't NULL (indicating that we have to insert at the
start) check that this popup entry exists */
if ( after != NULL )
{
for ( entry = popup->entries; entry != NULL; entry = entry->next )
if ( entry == after )
break;
if ( entry == NULL )
{
M_err( caller, "Invalid 'after' argument" );
return NULL;
}
}
/* Iterate over all items, inserting each individually */
for ( e = entries; e->text != NULL; e++ )
{
/* Check that the type is ok */
if ( e->type != FL_POPUP_NORMAL
&& e->type != FL_POPUP_TOGGLE
&& e->type != FL_POPUP_RADIO )
{
M_err( caller, "Invalid entry type" );
return NULL;
}
c = e->text;
/* Check for '/' and '_' at the very start of the text */
if ( c[ 0 ] == '_' || ( c[ 0 ] == '/' && c[ 1 ] == '_' ) )
need_line = 1;
if ( c[ 0 ] == '/' || ( c[ 0 ] == '_' && c[ 1 ] == '/' ) )
{
if ( e->type != FL_POPUP_NORMAL )
{
M_err( caller, "Entry can't be for a sub-popup "
"and toggle or radio entry at the same time" );
return NULL;
}
is_sub = 1;
}
if ( ( *c == '/' && *++c == '_' )
|| ( *c == '_' && *++c == '/' ) )
c++;
/* Count the number of '%' in the string (without a directly following
'S') since all of them need to be escaped */
cnt = 0;
for ( txt = strchr( c, '%' ); txt != NULL; txt = strchr( ++txt, '%' ) )
if ( txt[ 1 ] != 'S' )
cnt++;
/* Get enough memory for the string to pass to
fl_popup_insert_entries() */
if ( ( txt = fl_malloc( strlen( c ) + cnt + 13 ) ) == NULL )
{
M_err( caller, "Running out of memory" );
return NULL;
}
/* Copy the original text, doubling all '%'s (except those in
front of 'S') */
for ( cnt = 0; *c != '\0'; c++, cnt++ )
{
txt[ cnt ] = *c;
if ( c[ 0 ] == '%' && c[ 1 ] != 'S' )
txt[ ++cnt ] = '%';
}
/* Add special sequences passed in every case */
memcpy( txt + cnt, "%x%f%s", 6 );
cnt += 6;
/* Optionally add those for for disabling or hiding the entry */
if ( e->state & FL_POPUP_DISABLED )
{
memcpy( txt + cnt, "%d", 2 );
cnt += 2;
}
if ( e->state & FL_POPUP_HIDDEN )
{
memcpy( txt + cnt, "%h", 2 );
cnt += 2;
}
txt[ cnt ] = '\0';
/* Now we can start creating the entry. To make sure that the
value assigned to the entry is correct even when sub-popus
are to be created we need to know the level of recursion */
level++;
if ( need_line )
{
if ( ( after = fl_popup_insert_entries( popup, after, "%l" ) )
== NULL )
{
if ( --level == 0 )
val = 0;
return NULL;
}
need_line = 0;
}
if ( e->type == FL_POPUP_NORMAL && ! is_sub )
{
if ( ( after = fl_popup_insert_entries( popup, after, txt, val++,
e->callback, e->shortcut ) )
== NULL )
{
fl_free( txt );
if ( --level == 0 )
val = 0;
return NULL;
}
}
else if ( e->type == FL_POPUP_TOGGLE )
{
strcat( txt, e->state & FL_POPUP_CHECKED ? "%T" : "%t" );
if ( ( after = fl_popup_insert_entries( popup, after, txt, val++,
e->callback, e->shortcut ) )
== NULL )
{
fl_free( txt );
if ( --level == 0 )
val = 0;
return NULL;
}
}
else if ( e->type == FL_POPUP_RADIO )
{
strcat( txt, e->state & FL_POPUP_CHECKED ? "%R" : "%r" );
if ( ( after = fl_popup_insert_entries( popup, after, txt, val++,
e->callback, e->shortcut,
INT_MIN ) ) == NULL )
{
fl_free( txt );
if ( --level == 0 )
val = 0;
return NULL;
}
}
else if ( is_sub )
{
FL_POPUP *sub;
long pval = val++;
strcat( txt, "%m" );
if ( ( sub = fl_popup_create( popup->win, NULL, e + 1 ) ) == NULL
|| ( after = fl_popup_insert_entries( popup, after,txt, pval,
e->callback, e->shortcut,
sub ) ) == NULL )
{
fl_free( txt );
if ( ! fli_check_popup_exists( sub ) )
fl_popup_delete( sub );
if ( --level == 0 )
val = 0;
return NULL;
}
}
fl_free( txt );
/* Set the entries text member to exactly what the user gave us */
fli_safe_free( after->text );
if ( ( after->text = fl_strdup( e->text ) ) == NULL )
{
fl_popup_delete( popup );
if ( --level == 0 )
val = 0;
return NULL;
}
/* If this was a sub-popup entry skip items that were for the sub- or
sub-sub-popus etc. */
if ( is_sub )
{
cnt = 1;
while ( cnt > 0 )
{
e++;
if ( e->text == NULL )
cnt--;
else if ( e->text[ 0 ] == '/'
|| ( e->text[ 0 ] == '_' && e->text[ 1 ] == '/' ) )
cnt++;
}
is_sub = 0;
}
if ( first )
{
entry = after;
first = 0;
}
}
val++;
if ( --level == 0 )
val = 0;
return entry;
}
/***************************************
* Create a popup and populate it from a list of FL_POPUP_ITEM structures
***************************************/
FL_POPUP *
fl_popup_create( Window win,
const char * title,
FL_POPUP_ITEM * entries )
{
FL_POPUP *popup;
if ( ( popup = fl_popup_add( win, title ) ) == NULL )
return NULL;
if ( fli_popup_insert_items( popup, NULL, entries,
"fl_popup_create" ) == NULL )
{
fl_popup_delete( popup );
return NULL;
}
return popup;
}
/***************************************
* Appends entries to a popup from a list of FL_POPUP_ITEM structures
***************************************/
FL_POPUP_ENTRY *
fl_popup_add_items( FL_POPUP * popup,
FL_POPUP_ITEM * entries )
{
FL_POPUP_ENTRY *after;
/* Return if the array of items is NULL */
if ( entries == NULL )
return NULL;
/* Check if the popup we're supposed to add to does exist at all */
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_add_items", "Popup does not exist" );
return NULL;
}
/* Determine the last existing entry in the popup */
after = popup->entries;
while ( after != NULL && after->next != NULL )
after = after->next;
return fli_popup_insert_items( popup, after, entries,
"fl_popup_add_items" );
}
/***************************************
* Insert entries into a popup from a list of FL_POPUP_ITEM structures
* after an entry
***************************************/
FL_POPUP_ENTRY *
fl_popup_insert_items( FL_POPUP * popup,
FL_POPUP_ENTRY * after,
FL_POPUP_ITEM * entries )
{
return fli_popup_insert_items( popup, after, entries,
"fl_popup_insert_items" );
}
/***************************************
* Removes a popup, returns 0 on success and -1 on failure.
***************************************/
int
fl_popup_delete( FL_POPUP * popup )
{
/* Check if the popup we're asked to delete does exist */
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_delete", "Popup does not exist" );
return -1;
}
/* Don't delete a popup that's currently shown */
if ( popup->win != None )
{
M_err( "fl_popup_delete", "Can't free popup that is still shown" );
return -1;
}
/* Remove all entries (including sub-popups refered to there) */
while ( popup->entries )
fl_popup_entry_delete( popup->entries );
/* Get rid of the title string (if it exists) */
fli_safe_free( popup->title );
/* Finally unlink it from the list */
if ( popup->prev != NULL )
popup->prev->next = popup->next;
else
popups = popup->next;
if ( popup->next != NULL )
popup->next->prev = popup->prev;
fl_free( popup );
return 0;
}
/***************************************
* Delete a single popup entry (if it's a sub-popup entry also delete
* the popup it refers to)
***************************************/
int
fl_popup_entry_delete( FL_POPUP_ENTRY * entry )
{
if ( entry == NULL )
{
M_err( "fl_popup_entry_delete", "Invalid argument" );
return -1;
}
/* We don't remove entries that are shown at the moment */
if ( entry->popup->win != None )
{
M_err( "fl_popup_entry_delete", "Can't free entry of a popup that is "
"shown" );
return -1;
}
/* Remove the entry from the entry list of the popup it belongs to */
if ( entry->prev == NULL )
entry->popup->entries = entry->next;
else
entry->prev->next = entry->next;
if ( entry->next != NULL )
entry->next->prev = entry->prev;
entry->popup->need_recalc = 1;
/* Free all remaining memory used by entry */
fli_safe_free( entry->text );
fli_safe_free( entry->label );
fli_safe_free( entry->accel );
fli_safe_free( entry->shortcut );
/* For entries that refer to sub-popups delete the sub-popup */
if ( entry->type == FL_POPUP_SUB )
fl_popup_delete( entry->sub );
return 0;
}
/***************************************
* Do interaction with a popup
***************************************/
FL_POPUP_RETURN *
fl_popup_do( FL_POPUP * popup )
{
/* Check that the popup exists */
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_do", "Invalid popup" );
return NULL;
}
/* We don't interact directly with sub-popups */
if ( popup->parent != NULL )
{
M_err( "fl_popup_do", "Can't do direct interaction with sub-popup" );
return NULL;
}
draw_popup( popup );
return popup_interaction( popup );
}
/***************************************
* Set position where the popup is supposed to appear (if
* never called the popup appears at the mouse position)
***************************************/
void
fl_popup_set_position( FL_POPUP * popup,
int x,
int y )
{
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_set_position", "Invalid popup" );
return;
}
popup->use_req_pos = 1;
popup->req_x = x;
popup->req_y = y;
}
/***************************************
* Returns the policy set for a popup or the default policy if called
* with a NULL pointer
***************************************/
int
fl_popup_get_policy( FL_POPUP * popup )
{
if ( popup == NULL )
return popup_policy;
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_get_title_font", "Invalid popup" );
return -1;
}
return popup->top_parent->policy;
}
/***************************************
* Set policy of handling the popup (i.e. does it get closed when the
* user releases the mouse button outside an active entry or not?)
***************************************/
int
fl_popup_set_policy( FL_POPUP * popup,
int policy )
{
int old_policy;
if ( policy < FL_POPUP_NORMAL_SELECT || policy > FL_POPUP_DRAG_SELECT )
{
M_err( "fl_popup_set_policy", "Invalid policy argument" );
return -1;
}
if ( popup == NULL )
{
old_policy = popup_policy;
popup_policy = policy;
return old_policy;
}
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_set_policy", "Invalid popup" );
return -1;
}
old_policy = popup->policy;
popup->policy = policy;
return old_policy;
}
/***************************************
* Set new text for an entry. Returns 0 on succes and -1 on failure.
***************************************/
int
fl_popup_entry_set_text( FL_POPUP_ENTRY * entry,
const char * text )
{
char *t,
*label,
*accel,
sc_str[ 2 ];
int ret = -1;
long *old_sc;
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_set_text", "Invalid entry argument" );
return -1;
}
/* If the new text is NULL (not very useful;-) we're already done */
if ( text == NULL )
{
M_err( "fl_popup_entry_set_text", "Invalid text argument" );
return -1;
}
/* Get rid of the old text, label and accelerator strings */
fli_safe_free( entry->text );
fli_safe_free( entry->label );
fli_safe_free( entry->accel );
/* Make two copies of the text, the first one for storing in the entry,
the second one for creating the label the accelerator string */
if ( ( t = fl_strdup( text ) ) == NULL )
goto REPLACE_DONE;
if ( ( entry->text = fl_strdup( text ) ) == NULL )
goto REPLACE_DONE;
/* Split up the text at the first '%S' */
label = t;
if ( ( accel = strstr( t, "%S" ) ) != NULL )
{
*accel = '\0';
accel += 2;
}
/* Remove all backspace characters and replace tabs by spaces in both the
label and the accelerator string */
cleanup_string( label );
cleanup_string( accel );
/* Finally set up the label and accel members of the structure */
if ( ! *label )
entry->label = NULL;
else if ( ( entry->label = fl_strdup( label ) ) == NULL )
goto REPLACE_DONE;
if ( ! accel || ! *accel )
entry->accel = NULL;
else if ( ( entry->accel = fl_strdup( accel ) ) == NULL )
goto REPLACE_DONE;
ret = 0;
REPLACE_DONE:
fli_safe_free( t );
if ( ret == -1 )
{
fli_safe_free( entry->text );
fli_safe_free( entry->label );
fli_safe_free( entry->accel );
M_err( "fl_popup_entry_set_text", "Running out of memory" );
}
for ( old_sc = entry->shortcut; *old_sc != 0; old_sc++ )
if ( ( *old_sc & ~ ( FL_CONTROL_MASK | FL_ALT_MASK ) ) > 0
&& ( *old_sc & ~ ( FL_CONTROL_MASK | FL_ALT_MASK ) ) <= 0xFF )
{
sc_str[ 0 ] = *old_sc & ~ ( FL_CONTROL_MASK | FL_ALT_MASK );
sc_str[ 1 ] = '\0';
convert_shortcut( sc_str, entry );
break;
}
entry->popup->need_recalc = 1;
return 0;
}
/***************************************
* Set new shortcuts for an entry
***************************************/
void
fl_popup_entry_set_shortcut( FL_POPUP_ENTRY * entry,
const char * sc )
{
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_set_shortcut", "Invalid entry argument" );
return;
}
fli_safe_free( entry->shortcut );
if ( sc == NULL )
entry->ulpos = -1;
else
convert_shortcut( sc, entry );
}
/***************************************
* Set callback function for a popup
***************************************/
FL_POPUP_CB
fl_popup_set_callback( FL_POPUP * popup,
FL_POPUP_CB callback )
{
FL_POPUP_CB old_cb;
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_set_callback", "Invalid popup" );
return NULL;
}
old_cb = popup->callback;
popup->callback = callback;
return old_cb;
}
/***************************************
* Set a new title for a popup
***************************************/
const char *
fl_popup_get_title( FL_POPUP * popup )
{
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_set_title", "Invalid popup" );
return NULL;
}
return popup->title;
}
/***************************************
* Set a new title for a popup
***************************************/
FL_POPUP *
fl_popup_set_title( FL_POPUP * popup,
const char * title )
{
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_set_title", "Invalid popup" );
return NULL;
}
fli_safe_free( popup->title );
if ( title && *title )
{
popup->title = fl_strdup( title );
if ( popup->title == NULL )
{
M_err( "fl_popup_set_title", "Running out of memory" );
return NULL;
}
}
popup->need_recalc = 1;
return popup;
}
/***************************************
* Set a new title for a popup
***************************************/
FL_POPUP *
fl_popup_set_title_f( FL_POPUP * popup,
const char * fmt,
... )
{
char *buf;
FL_POPUP *p;
EXPAND_FORMAT_STRING( buf, fmt );
p = fl_popup_set_title( popup, buf );
fl_free( buf );
return p;
}
/***************************************
* Return title font for a popup
***************************************/
void
fl_popup_get_title_font( FL_POPUP * popup,
int * style,
int * size )
{
if ( popup == NULL )
{
if ( style != NULL )
*style = popup_title_font_style;
if ( size != NULL )
*size = popup_title_font_size;
return;
}
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_get_title_font", "Invalid popup" );
return;
}
if ( style != NULL )
*style = popup->top_parent->title_font_style;
if ( size != NULL )
*size = popup->top_parent->title_font_size ;
}
/***************************************
* Set title font for a popup (or change the default if called with NULL)
***************************************/
void
fl_popup_set_title_font( FL_POPUP * popup,
int style,
int size )
{
if ( popup == NULL )
{
popup_title_font_style = style;
popup_title_font_size = size;
return;
}
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_set_title_font", "Invalid popup" );
return;
}
popup->title_font_style = style;
popup->title_font_size = size;
if ( popup->parent == NULL )
set_need_recalc( popup );
}
/***************************************
* Return entry font for a popup
***************************************/
void
fl_popup_entry_get_font( FL_POPUP * popup,
int * style,
int * size )
{
if ( popup == NULL )
{
if ( style != NULL )
*style = popup_entry_font_style;
if ( size != NULL )
*size = popup_entry_font_size;
return;
}
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_entry_get_font", "Invalid popup" );
return;
}
if ( style != NULL )
*style = popup->top_parent->entry_font_style;
if ( size != NULL )
*size = popup->top_parent->entry_font_size ;
}
/***************************************
* Set entry font for a popup (or change the default if called with NULL)
***************************************/
void
fl_popup_entry_set_font( FL_POPUP * popup,
int style,
int size )
{
if ( popup == NULL )
{
popup_entry_font_style = style;
popup_entry_font_size = size;
return;
}
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_entry_set_font", "Invalid popup" );
return;
}
popup->entry_font_style = style;
popup->entry_font_size = size;
if ( popup->parent == NULL )
set_need_recalc( popup );
}
/***************************************
* Return the border width of a popup (or the default border width if
* called with NULL or an invalid argument)
***************************************/
int
fl_popup_get_bw( FL_POPUP * popup )
{
if ( popup == NULL )
return popup_bw;
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_get_bw", "Invalid argument" );
return popup_bw;
}
return popup->top_parent->bw;
}
/***************************************
* Sets the border width of a popup or the default border width (if called
* with NULL)
***************************************/
int
fl_popup_set_bw( FL_POPUP * popup,
int bw )
{
int old_bw;
/* Clamp border width to a reasonable range */
if ( bw == 0 || FL_abs( bw ) > FL_MAX_BW )
{
bw = bw == 0 ? -1 : ( bw > 0 ? FL_MAX_BW : - FL_MAX_BW );
M_warn( "fl_popup_set_bw", "Adjusting invalid border width to %d",
bw );
}
if ( popup == NULL )
{
old_bw = popup_bw;
popup_bw = bw;
return old_bw;
}
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_set_bw", "Invalid popup argument" );
return INT_MIN;
}
old_bw = bw;
popup->bw = bw;
if ( popup->parent == NULL )
set_need_recalc( popup );
return old_bw;
}
/***************************************
* Get one of the colors of the popup (NULL or invalid popup returns the
* default settings)
***************************************/
FL_COLOR
fl_popup_get_color( FL_POPUP * popup,
int color_type )
{
/* Check if the popup exists */
if ( popup != NULL && fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_get_color", "Invalid popup argument" );
popup = NULL;
}
/* For sub-popups always return the color of the controlling popup */
if ( popup != NULL )
popup = popup->top_parent;
switch ( color_type )
{
case FL_POPUP_BACKGROUND_COLOR :
return popup ? popup->bg_color : popup_bg_color;
case FL_POPUP_HIGHLIGHT_COLOR :
return popup ? popup->on_color : popup_on_color;
case FL_POPUP_TITLE_COLOR :
return popup ? popup->title_color : popup_title_color;
case FL_POPUP_TEXT_COLOR :
return popup ? popup->text_color : popup_text_color;
case FL_POPUP_HIGHLIGHT_TEXT_COLOR :
return popup ? popup->text_on_color : popup_text_on_color;
case FL_POPUP_DISABLED_TEXT_COLOR :
return popup ? popup->text_off_color : popup_text_off_color;
case FL_POPUP_RADIO_COLOR :
return popup ? popup->radio_color : popup_radio_color;
}
M_err( "fl_popup_get_color", "Invalid color type argument" );
return FL_BLACK;
}
/***************************************
* Set one of the colors of the popup (NULL sets the default)
***************************************/
FL_COLOR
fl_popup_set_color( FL_POPUP * popup,
int color_type,
FL_COLOR color )
{
FL_COLOR old_color;
/* Check if the popup exists */
if ( popup != NULL && fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_set_color", "Invalid popup argument" );
return FL_MAX_COLORS;
}
if ( color >= FL_MAX_COLORS )
{
M_err( "fl_popup_set_color", "Invalid color argument" );
return FL_MAX_COLORS;
}
switch ( color_type )
{
case FL_POPUP_BACKGROUND_COLOR :
if ( popup != NULL )
{
old_color = popup->bg_color;
popup->bg_color = color;
}
else
{
old_color = popup_bg_color;
popup_bg_color = color;
}
break;
case FL_POPUP_HIGHLIGHT_COLOR :
if ( popup != NULL )
{
old_color = popup->on_color;
popup->on_color = color;
}
else
{
old_color = popup_on_color;
popup_on_color = color;
}
break;
case FL_POPUP_TITLE_COLOR :
if ( popup != NULL )
{
old_color = popup->title_color;
popup->title_color = color;
}
else
{
old_color = popup_title_color;
popup_title_color = color;
}
break;
case FL_POPUP_TEXT_COLOR :
if ( popup != NULL )
{
old_color = popup->text_color;
popup->text_color = color;
}
else
{
old_color = popup_text_color;
popup_text_color = color;
}
break;
case FL_POPUP_HIGHLIGHT_TEXT_COLOR :
if ( popup != NULL )
{
old_color = popup->text_on_color;
popup->text_on_color = color;
}
else
{
old_color = popup_text_on_color;
popup_text_on_color = color;
}
break;
case FL_POPUP_DISABLED_TEXT_COLOR :
if ( popup != NULL )
{
old_color = popup->text_off_color;
popup->text_off_color = color;
}
else
{
old_color = popup_text_off_color;
popup_text_off_color = color;
}
break;
case FL_POPUP_RADIO_COLOR :
if ( popup != NULL )
{
old_color = popup->radio_color;
popup->radio_color = color;
}
else
{
old_color = popup_radio_color;
popup_radio_color = color;
}
break;
default :
M_err( "fl_popup_set_color", "Invalid color type argument" );
return FL_MAX_COLORS;
}
return old_color;
}
/***************************************
* Sets the cursor of a popup (or change the
* default cursor if called with NULL)
***************************************/
void
fl_popup_set_cursor( FL_POPUP * popup,
int cursor )
{
if ( popup == NULL )
{
popup_cursor = fli_get_cursor_byname( cursor );
return;
}
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_set_cursor", "Invalid popup argument" );
return;
}
popup->cursor = fli_get_cursor_byname( cursor );
if ( popup->win )
XDefineCursor( flx->display, popup->win, popup->cursor );
}
/***************************************
* Set selection callback function for a popup entry
***************************************/
FL_POPUP_CB
fl_popup_entry_set_callback( FL_POPUP_ENTRY * entry,
FL_POPUP_CB callback )
{
FL_POPUP_CB old_cb;
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_set_enter_callback", "Invalid entry argument" );
return NULL;
}
old_cb = entry->callback;
entry->callback = callback;
return old_cb;
}
/***************************************
* Set enter callback function for a popup entry
***************************************/
FL_POPUP_CB
fl_popup_entry_set_enter_callback( FL_POPUP_ENTRY * entry,
FL_POPUP_CB callback )
{
FL_POPUP_CB old_cb;
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_set_enter_callback", "Invalid entry argument" );
return NULL;
}
old_cb = entry->enter_callback;
entry->enter_callback = callback;
return old_cb;
}
/***************************************
* Set leave callback function for a popup entry
***************************************/
FL_POPUP_CB
fl_popup_entry_set_leave_callback( FL_POPUP_ENTRY * entry,
FL_POPUP_CB callback )
{
FL_POPUP_CB old_cb;
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_set_leave_callback", "Invalid entry argument" );
return NULL;
}
old_cb = entry->leave_callback;
entry->leave_callback = callback;
return old_cb;
}
/***************************************
* Get state (disabled, hidden, checked) of a popup entry
***************************************/
unsigned int
fl_popup_entry_get_state( FL_POPUP_ENTRY * entry )
{
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_get_state", "Invalid entry argument" );
return UINT_MAX;
}
return entry->state;
}
/***************************************
* Set state (disabled, hidden, checked) of a popup entry
***************************************/
unsigned int
fl_popup_entry_set_state( FL_POPUP_ENTRY * entry,
unsigned int state )
{
unsigned int old_state;
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_set_state", "Invalid entry argument" );
return UINT_MAX;
}
/* Nothing to be done if new and old state are identical */
if ( entry->state == state )
return state;
old_state = entry->state;
entry->state = state;
/* If the entry gets disabled or hidden we may have to switch off it's
"activated" state - don't call leave callback in this case */
if ( entry->state & ( FL_POPUP_DISABLED | FL_POPUP_HIDDEN ) )
entry->is_act = 0;
/* If a radio entry gets checked uncheck all radio other entries belonging
to the same group in the popup */
if ( entry->type == FL_POPUP_RADIO && state & FL_POPUP_CHECKED )
{
FL_POPUP_ENTRY *e;
for ( e = entry->popup->entries; e != NULL; e = e->next )
if ( e->type == FL_POPUP_RADIO
&& entry->group == e->group
&& entry != e )
e->state &= ~ FL_POPUP_CHECKED;
}
/* If the entry was hidden or made visible again the dimensions of the
popup need to be recalculated */
if ( ( old_state & FL_POPUP_HIDDEN ) ^ ( state & FL_POPUP_HIDDEN ) )
entry->popup->need_recalc = 1;
/* If the popup the entry belongs to is visible redraw the popup */
if ( entry->popup->win != None )
draw_popup( entry->popup );
return old_state;
}
/***************************************
* Clear certain bits of an entries state
***************************************/
unsigned int
fl_popup_entry_clear_state( FL_POPUP_ENTRY * entry,
unsigned int what )
{
unsigned int old_state;
size_t i;
unsigned int flags[ ] = { FL_POPUP_DISABLED,
FL_POPUP_HIDDEN,
FL_POPUP_CHECKED };
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_clear_state", "Invalid entry argument" );
return UINT_MAX;
}
old_state = entry->state;
for ( i = 0; i < sizeof flags / sizeof *flags; i++ )
if ( what & flags[ i ] )
fl_popup_entry_set_state( entry, entry->state & ~ flags[ i ] );
return old_state;
}
/***************************************
* Set certain bits of an entrys state
***************************************/
unsigned int
fl_popup_entry_raise_state( FL_POPUP_ENTRY * entry,
unsigned int what )
{
unsigned int old_state;
size_t i;
unsigned int flags[ ] = { FL_POPUP_DISABLED,
FL_POPUP_HIDDEN,
FL_POPUP_CHECKED };
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_raise_state", "Invalid entry argument" );
return UINT_MAX;
}
old_state = entry->state;
for ( i = 0; i < sizeof flags / sizeof *flags; i++ )
if ( what & flags[ i ] )
fl_popup_entry_set_state( entry, entry->state | flags[ i ] );
return old_state;
}
/***************************************
* Toggle certain bits of an entries state
***************************************/
unsigned int
fl_popup_entry_toggle_state( FL_POPUP_ENTRY * entry,
unsigned int what )
{
unsigned int old_state;
size_t i;
unsigned int flags[ ] = { FL_POPUP_DISABLED,
FL_POPUP_HIDDEN,
FL_POPUP_CHECKED };
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_toggle_state", "Invalid entry argument" );
return UINT_MAX;
}
old_state = entry->state;
for ( i = 0; i < sizeof flags / sizeof *flags; i++ )
if ( what & flags[ i ] )
fl_popup_entry_set_state( entry, entry->state ^ flags[ i ] );
return old_state;
}
/***************************************
* Set the value associated with an entry
***************************************/
long
fl_popup_entry_set_value( FL_POPUP_ENTRY * entry,
long value )
{
long old_val;
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_set_value", "Invalid entry argument" );
return INT_MIN;
}
old_val = entry->val;
entry->val = value;
return old_val;
}
/***************************************
* Set the user data pointer associated with an entry
***************************************/
void *
fl_popup_entry_set_user_data( FL_POPUP_ENTRY * entry,
void * user_data )
{
void *old_user_data;
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_set_user_data", "Invalid entry argument" );
return NULL;
}
old_user_data = entry->user_data;
entry->user_data = user_data;
return old_user_data;
}
/***************************************
* Returns the group a radio entry belongs to.
* Returns group number on success and INT_MAX on failure.
***************************************/
int
fl_popup_entry_get_group( FL_POPUP_ENTRY * entry )
{
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_get_group", "Invalid entry argument" );
return INT_MAX;
}
return entry->group;
}
/***************************************
* Set a new group a radio entry
* Returns old group number on success and INT_MAX on failure.
***************************************/
int
fl_popup_entry_set_group( FL_POPUP_ENTRY * entry,
int group )
{
int old_group;
FL_POPUP_ENTRY *e;
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_set_group", "Invalid entry argument" );
return INT_MAX;
}
old_group = entry->group;
if ( entry->type != FL_POPUP_RADIO )
{
entry->group = group;
return old_group;
}
if ( old_group == group )
return entry->group;
for ( e = entry; e != NULL; e = e->next )
if ( e->type == FL_POPUP_RADIO
&& e->group == group
&& e->state & FL_POPUP_CHECKED )
entry->state &= ~ FL_POPUP_CHECKED;
entry->group = group;
return old_group;
}
/***************************************
* Returns the sub-popup for a sub-popup entry.
* Returns the sub-popups address on success and NULL on failure.
***************************************/
FL_POPUP *
fl_popup_entry_get_subpopup( FL_POPUP_ENTRY * entry )
{
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_get_subpopup", "Invalid entry argument" );
return NULL;
}
if ( entry->type != FL_POPUP_SUB )
{
M_err( "fl_popup_entry_get_subpopup", "Entry isn't a subpopup entry" );
return NULL;
}
return entry->sub;
}
/***************************************
* Set a new sub-popup for an entry, requires that this a sub-popup entry.
* Returns the new sub-popups address on success and NULL on failure.
***************************************/
FL_POPUP *
fl_popup_entry_set_subpopup( FL_POPUP_ENTRY * entry,
FL_POPUP * subpopup )
{
FL_POPUP *old_sub;
if ( fli_check_popup_entry_exists( entry ) )
{
M_err( "fl_popup_entry_set_subpopup", "Invalid entry argument" );
return NULL;
}
if ( entry->type != FL_POPUP_SUB )
{
M_err( "fl_popup_entry_set_subpopup", "Entry isn't a subpopup entry" );
return NULL;
}
if ( entry->sub == subpopup )
return entry->sub;
if ( entry->sub->win != None || subpopup->win != None )
{
M_err( "fl_popup_entry_set_subpopup", "Can't change sub-popup while "
"entries sub-popup is shown.");
return NULL;
}
old_sub = entry->sub;
entry->sub = subpopup;
if ( check_sub( entry ) )
{
entry->sub = old_sub;
M_err( "fl_popup_entry_set_subpopup", "Invalid sub-popup argument" );
return NULL;
}
fl_popup_delete( entry->sub );
return entry->sub = subpopup;
}
/***************************************
* Find a popup entry by its position in the popup, starting at 1.
* (Line entries aren't counted, but hidden entries are.)
***************************************/
FL_POPUP_ENTRY *
fl_popup_entry_get_by_position( FL_POPUP * popup,
int position )
{
FL_POPUP_ENTRY *e;
int i = 0;
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_entry_get_by_position", "Invalid popup argument" );
return NULL;
}
for ( e = popup->entries; e != NULL; e = e->next )
{
if ( e->type == FL_POPUP_LINE )
continue;
if ( i++ == position )
return e;
}
return NULL;
}
/***************************************
* Find a popup entry by its value
***************************************/
FL_POPUP_ENTRY *
fl_popup_entry_get_by_value( FL_POPUP * popup,
long val )
{
FL_POPUP_ENTRY *e,
*r;
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_entry_get_by_value", "Invalid popup argument" );
return NULL;
}
for ( e = popup->entries; e != NULL; e = e->next )
{
if ( e->type == FL_POPUP_LINE )
continue;
if ( e->val == val )
return e;
if ( e->type == FL_POPUP_SUB
&& ( r = fl_popup_entry_get_by_value( e->sub, val ) ) )
return r;
}
return NULL;
}
/***************************************
* Find a popup entry by its user data
***************************************/
FL_POPUP_ENTRY *
fl_popup_entry_get_by_user_data( FL_POPUP * popup,
void * user_data )
{
FL_POPUP_ENTRY *e,
*r;
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_entry_get_by_value", "Invalid popup argument" );
return NULL;
}
for ( e = popup->entries; e != NULL; e = e->next )
{
if ( e->type == FL_POPUP_LINE )
continue;
if ( e->user_data == user_data )
return e;
if ( e->type == FL_POPUP_SUB
&& ( r = fl_popup_entry_get_by_user_data( e->sub, user_data ) ) )
return r;
}
return NULL;
}
/***************************************
* Find a popup entry by its text
***************************************/
FL_POPUP_ENTRY *
fl_popup_entry_get_by_text( FL_POPUP * popup,
const char * text )
{
FL_POPUP_ENTRY *e,
*r;
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_entry_get_by_text", "Invalid popup argument" );
return NULL;
}
for ( e = popup->entries; e != NULL; e = e->next )
{
if ( e->type == FL_POPUP_LINE )
continue;
if ( ! strcmp( e->text, text ) )
return e;
if ( e->type == FL_POPUP_SUB
&& ( r = fl_popup_entry_get_by_text( e->sub, text ) ) )
return r;
}
return NULL;
}
/***************************************
* Find a popup entry by its text
***************************************/
FL_POPUP_ENTRY *
fl_popup_entry_get_by_text_f( FL_POPUP * popup,
const char * fmt,
... )
{
FL_POPUP_ENTRY *e;
char *buf;
EXPAND_FORMAT_STRING( buf, fmt );
e = fl_popup_entry_get_by_text( popup, buf );
fl_free( buf );
return e;
}
/***************************************
* Find a popup entry by its label
***************************************/
FL_POPUP_ENTRY *
fl_popup_entry_get_by_label( FL_POPUP * popup,
const char * label )
{
FL_POPUP_ENTRY *e,
*r;
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_entry_get_by_label", "Invalid popup argument" );
return NULL;
}
for ( e = popup->entries; e != NULL; e = e->next )
{
if ( e->type == FL_POPUP_LINE || e->label == NULL )
continue;
if ( ! strcmp( e->label, label ) )
return e;
if ( e->type == FL_POPUP_SUB
&& ( r = fl_popup_entry_get_by_label( e->sub, label ) ) )
return r;
}
return NULL;
}
/***************************************
* Find a popup entry by its label
***************************************/
FL_POPUP_ENTRY *
fl_popup_entry_get_by_label_f( FL_POPUP * popup,
const char * fmt,
... )
{
FL_POPUP_ENTRY *e;
char *buf;
EXPAND_FORMAT_STRING( buf, fmt );
e = fl_popup_entry_get_by_label( popup, buf );
fl_free( buf );
return e;
}
/***************************************
* Get size of a popup, returns 0 on success and -1 on error
***************************************/
int
fl_popup_get_size( FL_POPUP * popup,
unsigned int * w,
unsigned int * h )
{
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_get_size", "Invalid popup argument" );
return -1;
}
if ( popup->need_recalc )
recalc_popup( popup );
*w = popup->w;
*h = popup->h;
return 0;
}
/***************************************
* Get minimum width of a popup
***************************************/
int
fl_popup_get_min_width( FL_POPUP * popup )
{
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_get_size", "Invalid popup argument" );
return -1;
}
if ( popup->need_recalc )
recalc_popup( popup );
return popup->min_width;
}
/***************************************
* Set minimum width of a popup
***************************************/
int
fl_popup_set_min_width( FL_POPUP * popup,
int min_width )
{
int old_min_width;
if ( fli_check_popup_exists( popup ) )
{
M_err( "fl_popup_get_size", "Invalid popup argument" );
return -1;
}
old_min_width = popup->min_width;
if ( min_width < 0 )
min_width = 0;
popup->min_width = min_width;
popup->need_recalc = 1;
return old_min_width;
}
/***************************************
* Set up a linked list of entries for a popup according to a string
* and optional arguments. The resulting list then can be merged into
* an already existing list of entries of the popup. The 'simple'
* argument, when set, says that the new entry can't be a sub-entry,
* a toggle, line or radio entry.
***************************************/
static FL_POPUP_ENTRY *
parse_entries( FL_POPUP * popup,
char * str,
va_list ap,
const char * caller,
int simple )
{
FL_POPUP_ENTRY *entry,
*entry_first = NULL;
char *c,
*s,
*sc = NULL,
*acc = NULL;
/* Split the string at '|' and create a new entry for each part */
for ( c = strtok( str, "|" ); c != NULL; c = strtok( NULL, "|" ) )
{
/* Allocate a new entry and append it to the list of new entries */
if ( ( entry = fl_malloc( sizeof *entry ) ) == NULL )
{
M_err( caller, "Running out of memory" );
return failed_add( entry_first );
}
if ( entry_first == NULL )
{
entry_first = entry;
entry->prev = NULL;
}
else
{
FL_POPUP_ENTRY *e;
for ( e = entry_first; e->next != NULL; e = e->next )
/* empty */;
e->next = entry;
entry->prev = e;
}
entry->next = NULL;
entry->label = NULL;
entry->accel = NULL;
/* The text field is exactly what the user gave us */
if ( ( entry->text = fl_strdup( c ) ) == NULL )
{
M_err( caller, "Running out of memory" );
return failed_add( entry_first );
}
/* Set some default values */
entry->user_data = NULL;
entry->val = popup->counter++;
entry->is_act = 0;
entry->type = FL_POPUP_NORMAL;
entry->state = 0;
entry->shortcut = NULL;
entry->ulpos = -1;
entry->callback = NULL;
entry->enter_callback = NULL;
entry->leave_callback = NULL;
entry->sub = NULL;
entry->popup = popup;
/* Now analyze the string for the entry */
s = c;
while ( ( s = strchr( s, '%' ) ) != NULL )
{
switch ( s[ 1 ] )
{
case '%' :
memmove( s, s + 1, strlen( s ) );
s++;
break;
case 'x' : /* set a value */
entry->val = va_arg( ap, long );
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'u' : /* set pointer to user data */
entry->user_data = va_arg( ap, void * );
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'f' : /* set callback function */
entry->callback = va_arg( ap, FL_POPUP_CB );
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'E' : /* set enter callback function */
entry->enter_callback = va_arg( ap, FL_POPUP_CB );
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'L' : /* set leave callback function */
entry->leave_callback = va_arg( ap, FL_POPUP_CB );
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'm' : /* set a submenu */
if ( entry->type != FL_POPUP_NORMAL )
{
M_err( caller, "Entry can't be submenu and something "
"else" );
return failed_add( entry_first );
}
entry->sub = va_arg( ap, FL_POPUP * );
if ( ! simple )
{
if ( check_sub( entry ) )
{
M_err( caller, "Invalid submenu popup" );
return failed_add( entry_first );
}
entry->type = FL_POPUP_SUB;
entry->sub->parent = popup;
}
else
entry->sub = NULL;
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'l' : /* set up entry as a line entry */
if ( entry->type != FL_POPUP_NORMAL )
{
M_err( caller, "Entry can't be a line marker and "
"something else" );
return failed_add( entry_first );
}
entry->type = FL_POPUP_LINE;
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'T' : /* set up as toggle entry */
case 't' :
if ( entry->type != FL_POPUP_NORMAL )
{
M_err( caller, "Entry can't be a toggle entry and "
"something else" );
return failed_add( entry_first );
}
if ( ! simple )
{
entry->type = FL_POPUP_TOGGLE;
if ( s[ 1 ] == 'T' )
entry->state |= FL_POPUP_CHECKED;
}
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'R' : /* set up as radio entry */
case 'r' : /* set up as radio entry */
if ( entry->type != FL_POPUP_NORMAL )
{
M_err( caller, "Entry can't be radio entry and "
"something else" );
return failed_add( entry_first );
}
entry->group = va_arg( ap, int );
if ( ! simple )
{
entry->type = FL_POPUP_RADIO;
if ( s[ 1 ] == 'R' )
entry->state |= FL_POPUP_CHECKED;
}
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'd' : /* mark entry as disabled */
entry->state |= FL_POPUP_DISABLED;
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'h' : /* mark entry as hidden */
entry->state |= FL_POPUP_HIDDEN;
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 's' : /* set a shortcut */
sc = va_arg( ap, char * );
memmove( s, s + 2, strlen( s + 1 ) );
break;
case 'S' :
if ( acc != NULL )
{
M_err( caller, "'%%S' sequence found more than once in "
"entry definition" );
return failed_add( entry_first );
}
*s++ = '\0';
acc = ++s;
break;
default :
M_err( caller, "Invalid flag '%%%c'", s[ 1 ] );
return failed_add( entry_first );
}
}
/* If we're asked to create simple popup (for a FL_SELECT object)
remove the entry again if it's a line entry */
if ( simple && entry->type == FL_POPUP_LINE )
{
if ( entry->prev != NULL )
entry->prev->next = NULL;
else
entry_first = NULL;
fl_free( entry );
popup->counter--;
continue;
}
/* Now all flags should be removed from text string, so we can set
the entries label and accelerator key text after also removing
backspace characters and replacing tabs by spaces. */
cleanup_string ( c );
if ( ! *c )
entry->label = NULL;
else if ( ( entry->label = fl_strdup( c ) ) == NULL )
{
M_err( caller, "Running out of memory" );
return failed_add( entry_first );
}
acc = cleanup_string( acc );
if ( ! acc || ! *acc )
entry->accel = NULL;
else if ( ( entry->accel = fl_strdup( acc ) ) == NULL )
{
M_err( caller, "Running out of memory" );
return failed_add( entry_first );
}
acc = NULL;
/* Having the text we can set up the shortcuts */
if ( sc )
{
convert_shortcut( sc, entry );
sc = NULL;
}
}
/* Make sure the settings for radio entries are consistent, i.e. only
a single one of a group (taking both the already existing as well
as the new ones into account) is set (the last created wins) */
for ( entry = entry_first; entry != NULL; entry = entry->next )
if ( entry->type == FL_POPUP_RADIO && entry->state * FL_POPUP_CHECKED )
radio_check( entry );
/* List is complete and the caller can link it into its list */
return entry_first;
}
/***************************************
* Called when adding popup entries fails due to whatever reasons
* to get rid of all already created ones
***************************************/
static FL_POPUP_ENTRY *
failed_add( FL_POPUP_ENTRY * first )
{
FL_POPUP_ENTRY *e;
while ( first )
{
e = first->next;
fl_popup_entry_delete( first );
first = e;
}
return NULL;
}
/***************************************
* Checks if a popup is suitable for use as a sub-popup
***************************************/
static int
check_sub( FL_POPUP_ENTRY * entry )
{
/* Sub-popup can't be NULL */
if ( entry->sub == NULL )
return 1;
/* Check if the sub-popup exists */
if ( fli_check_popup_exists( entry->sub ) )
return 1;
/* The sub-popup can't be the entries popup itself */
if ( entry->popup == entry->sub )
return 1;
/* The sub-popup can not already be a sub-popup for some other entry */
if ( entry->sub->parent )
return 1;
return 0;
}
/***************************************
* Go through all entries of a popup and resets the checked state of
* all other entries belonging to the same group before the one we
* were called with.
***************************************/
static void
radio_check( FL_POPUP_ENTRY * entry )
{
FL_POPUP_ENTRY *e;
/* First reset (if necessary) the old ones */
for ( e = entry->popup->entries; e != NULL; e = e->next )
if ( e->type == FL_POPUP_RADIO
&& e != entry
&& entry->group == e->group )
e->state &= ~ FL_POPUP_CHECKED;
/* Also reset all the new ones (except the one we were called for) */
for ( e = entry->prev; e != NULL; e = e->prev )
if ( e->type == FL_POPUP_RADIO
&& e != entry
&& entry->group == e->group )
e->state &= ~ FL_POPUP_CHECKED;
}
/***************************************
* Makes a shortcut out of an entries text and determines where to underline
***************************************/
static void
convert_shortcut( const char * shortcut,
FL_POPUP_ENTRY * entry )
{
long sc[ MAX_SHORTCUTS + 1 ];
int cnt;
if ( entry->label && *entry->label
&& ( ! entry->accel || ! *entry->accel ) )
entry->ulpos = fli_get_underline_pos( entry->label, shortcut ) - 1;
else
entry->ulpos = -1;
cnt = fli_convert_shortcut( shortcut, sc );
fli_safe_free( entry->shortcut );
entry->shortcut = fl_malloc( ( cnt + 1 ) * sizeof *entry->shortcut );
memcpy( entry->shortcut, sc, ( cnt + 1 ) * sizeof *entry->shortcut );
}
/***************************************
* Recalculate the dimensions of a popup and the positions of the entries
***************************************/
static void
recalc_popup( FL_POPUP * popup )
{
FL_POPUP_ENTRY *e;
int offset = FL_abs( popup->top_parent->bw )
+ ( popup->top_parent->bw > 0 ? 1 : 0 );
unsigned int cur_w = 0,
cur_h = offset,
w,
h;
title_dimensions( popup, &w, &h );
if ( w > 0 )
{
/* Note: title box should always have a bit of spacing to the top
of the window */
popup->title_box_x = offset + OUTER_PADDING_LEFT;
popup->title_box_y = offset + FL_max( OUTER_PADDING_TOP, 3 );
cur_w = w + INNER_PADDING_LEFT + INNER_PADDING_RIGHT;
popup->title_box_h = h + INNER_PADDING_TOP + INNER_PADDING_BOTTOM;
cur_h += popup->title_box_h
+ FL_max( OUTER_PADDING_TOP, 3 ) + OUTER_PADDING_BOTTOM + 2;
}
popup->has_subs = 0;
popup->has_boxes = 0;
for ( e = popup->entries; e != NULL; e = e->next )
{
if ( e->state & FL_POPUP_HIDDEN )
continue;
e->box_x = offset + OUTER_PADDING_LEFT;
e->box_y = cur_h;
entry_text_dimensions( e, &w, &h );
cur_w = FL_max( cur_w, w );
cur_h += e->box_h = h + OUTER_PADDING_TOP + OUTER_PADDING_BOTTOM;
if ( e->type == FL_POPUP_TOGGLE || e->type == FL_POPUP_RADIO )
popup->has_boxes = 1;
else if ( e->type == FL_POPUP_SUB )
popup->has_subs = 1;
}
if ( popup->has_boxes )
cur_w += popup->top_parent->entry_font_size + SYMBOL_PADDING;
if ( popup->has_subs )
cur_w += SYMBOL_PADDING + popup->top_parent->entry_font_size;
popup->w = cur_w + 2 * offset + OUTER_PADDING_LEFT + OUTER_PADDING_RIGHT;
popup->h = cur_h + offset + 1;
popup->w = FL_max( popup->w, ( unsigned int ) popup->min_width );
popup->title_box_w = popup->w - 2 * offset
- OUTER_PADDING_LEFT - OUTER_PADDING_RIGHT;
popup->need_recalc = 0;
}
/***************************************
* Calculate the dimensions of the title
***************************************/
static void
title_dimensions( FL_POPUP * popup,
unsigned int * w,
unsigned int * h )
{
FL_POPUP *ptp = popup->top_parent;
char *s,
*c;
int dummy;
if ( popup->title == NULL )
{
*w = *h = 0;
return;
}
s = c = fl_strdup( popup->title );
/* Now calculate the dimensions of the string */
*w = 0;
*h = 0;
for ( c = strtok( s, "\n" ); c != NULL; c = strtok( NULL, "\n" ) )
{
*w = FL_max( *w, ( unsigned int )
fl_get_string_width( ptp->title_font_style,
ptp->title_font_size,
c, strlen( c ) ) );
*h += fl_get_string_height( ptp->title_font_style,
ptp->title_font_size,
c, strlen( c ), &dummy, &dummy );
}
fl_free( s );
/* Add offsets the string drawing function will add */
*w += 2 * STR_OFFSET_X;
*h += 2 * STR_OFFSET_Y;
}
/***************************************
* Calculate the (minimum) dimensions of an entry
***************************************/
static void
entry_text_dimensions( FL_POPUP_ENTRY * entry,
unsigned int * w,
unsigned int * h )
{
FL_POPUP *ptp = entry->popup->top_parent;
char *s,
*c;
int ulpos = entry->ulpos;
XRectangle *xr;
int asc;
int dummy;
*w = *h = 0;
if ( entry->type == FL_POPUP_LINE )
{
*h = LINE_HEIGHT;
return;
}
/* Determine length and height of label string */
if ( entry->label && *entry->label )
{
s = c = fl_strdup( entry->label );
/* Calculate the dimensions of the label (always use the font of the
top-most parent) */
for ( c = strtok( s, "\n" ); c != NULL; c = strtok( NULL, "\n" ) )
{
unsigned int old_h = *h;
*w = FL_max( *w, ( unsigned int )
fl_get_string_width( ptp->entry_font_style,
ptp->entry_font_size,
c, strlen( c ) ) );
*h += fl_get_string_height( ptp->entry_font_style,
ptp->entry_font_size,
c, strlen( c ), &asc, &dummy );
if ( c == s )
entry->sl_h = *h;
/* Not very nice hack to get the underline position */
if ( ulpos >= 0 )
{
if ( ulpos < ( int ) strlen( c ) )
{
xr = fli_get_underline_rect(
fl_get_font_struct( ptp->entry_font_style,
ptp->entry_font_size ),
0, asc, c, ulpos );
entry->ul_x = xr->x + STR_OFFSET_X;
entry->ul_y = old_h + xr->y + STR_OFFSET_Y;
entry->ul_w = xr->width;
entry->ul_h = xr->height;
}
ulpos -= strlen( c ) + 1;
}
}
fli_safe_free( s );
}
/* Repeat this for the accelerator key text (minimum spacing between this
and the label is 1.5 times the font size) */
if ( entry->accel && *entry->accel )
{
unsigned int aw = 0,
ah = 0;
*w += 1.5 * ptp->entry_font_size;
s = c = fl_strdup( entry->accel );
for ( c = strtok( s, "\n" ); c != NULL; c = strtok( NULL, "\n" ) )
{
aw = FL_max( aw, ( unsigned int )
fl_get_string_width( ptp->entry_font_style,
ptp->entry_font_size,
c, strlen( c ) ) );
ah += fl_get_string_height( ptp->entry_font_style,
ptp->entry_font_size,
c, strlen( c ), &dummy, &dummy );
}
fli_safe_free( s );
*w += aw;
*h = FL_max( *h, ah );
}
*w += 2 * STR_OFFSET_X;
*h += 2 * STR_OFFSET_Y;
}
/***************************************
* Draw a popup
***************************************/
static void
draw_popup( FL_POPUP * popup )
{
FL_POPUP_ENTRY *e;
/* If necessary recalculate the size of the popup window and,
if it's already shown, resize it */
if ( popup->need_recalc )
{
unsigned int old_w = popup->w,
old_h = popup->h;
recalc_popup( popup );
if ( popup->win != None
&& ( popup->w != old_w || popup->h != old_h ) )
XResizeWindow( flx->display, popup->win, popup->w, popup->h );
}
/* If necessary create and map the popup window, otherwise just draw it
(no drawing needed when the window gets opened, we will receive an
Expose event that will induce the drawing) */
if ( popup->win == None )
create_popup_window( popup );
else
{
/* Draw the popup box */
fl_drw_box( FL_UP_BOX, 0, 0, popup->w, popup->h, popup->bg_color,
popup->top_parent->bw );
/* Draw the title and all entries */
draw_title( popup );
for ( e = popup->entries; e != NULL; e = e->next )
draw_entry( e );
}
}
/***************************************
* Draws the title of a popup
***************************************/
static void
draw_title( FL_POPUP * popup )
{
FL_POPUP *ptp = popup->top_parent;
if ( popup->title == NULL )
return;
/* Draw a box with a frame around the title */
fl_drw_box( FL_FRAME_BOX, popup->title_box_x - 1, popup->title_box_y - 1,
popup->title_box_w + 2, popup->title_box_h + 2,
ptp->bg_color, 1 );
fl_drw_text( FL_ALIGN_CENTER, popup->title_box_x, popup->title_box_y,
popup->title_box_w, popup->title_box_h, ptp->title_color,
ptp->title_font_style, ptp->title_font_size, popup->title );
}
/***************************************
* Draws an entry of a popup
***************************************/
static void
draw_entry( FL_POPUP_ENTRY * entry )
{
FL_POPUP *p = entry->popup,
*ptp = p->top_parent;
FL_COLOR color;
int offset = FL_abs( ptp->bw ) + ( ptp->bw > 0 ? 1 : 0 );
int x;
unsigned int w;
/* Hidden entries need no further work */
if ( entry->state & FL_POPUP_HIDDEN )
return;
/* Calculate the width of the box we're going to be drawning in */
x = entry->box_x;
w = entry->box_w = p->w - 2 * offset
- OUTER_PADDING_LEFT - OUTER_PADDING_RIGHT;
/* For entries that just stand for separating lines draw that line
and be done with it */
if ( entry->type == FL_POPUP_LINE )
{
fl_drw_box( FL_DOWN_BOX, x, entry->box_y + OUTER_PADDING_TOP + 1,
w, LINE_HEIGHT - 1, ptp->bg_color, 1 );
return;
}
/* Draw the background of the entry */
fl_rectangle( 1, offset, entry->box_y, p->w - 2 * offset - 1, entry->box_h,
entry->is_act ? ptp->on_color : ptp->bg_color );
/* Find out what color the text is to be drawn in */
if ( entry->state & FL_POPUP_DISABLED )
color = ptp->text_off_color;
else
color = entry->is_act ? ptp->text_on_color : ptp->text_color;
/* If there are radio/toggle entries extra space at the start is needed.
For checked toggle items a check mark is drawn there and for radio
items a circle */
if ( p->has_boxes )
{
if ( entry->type == FL_POPUP_RADIO )
fl_drw_box( FL_ROUNDED3D_DOWNBOX, x + 0.2 * entry->sl_h,
entry->box_y + 0.25 * entry->sl_h + STR_OFFSET_Y,
0.5 * entry->sl_h, 0.5 * entry->sl_h,
entry->state & FL_POPUP_CHECKED ?
ptp->radio_color : ptp->bg_color, 1 );
else if ( entry->state & FL_POPUP_CHECKED )
{
FL_POINT xp[ 3 ];
xp[ 0 ].x = x + 0.25 * entry->sl_h;
xp[ 0 ].y = entry->box_y + 0.5 * entry->sl_h + STR_OFFSET_Y;
xp[ 1 ].x = xp[ 0 ].x + 0.2 * entry->sl_h;
xp[ 1 ].y = xp[ 0 ].y + 0.25 * entry->sl_h;
xp[ 2 ].x = xp[ 1 ].x + 0.2 * entry->sl_h;
xp[ 2 ].y = xp[ 1 ].y - 0.5 * entry->sl_h;
fl_lines( xp, 3, color );
xp[ 2 ].x += 1;
fl_lines( xp + 1, 2, color );
}
w -= ptp->entry_font_size + SYMBOL_PADDING;
x += ptp->entry_font_size + SYMBOL_PADDING;;
}
/* If there are sub-popups we need some extra space at the end of the
entries and for sub-popups entries a triangle at the end */
if ( p->has_subs )
{
if ( entry->type == FL_POPUP_SUB )
{
FL_POINT xp[ 4 ];
xp[ 0 ].x = x + w - 0.125 * entry->sl_h;
xp[ 0 ].y = entry->box_y + 0.5 * entry->box_h;
xp[ 1 ].x = xp[ 0 ].x - 0.35355 * entry->sl_h;
xp[ 1 ].y = xp[ 0 ].y - 0.25 * entry->sl_h;
xp[ 2 ].x = xp[ 1 ].x;
xp[ 2 ].y = xp[ 1 ].y + 0.5 * entry->sl_h;
fl_polygon( 1, xp, 3, color );
}
w -= ptp->entry_font_size + SYMBOL_PADDING;
}
/* Finally it's time to draw the label and accelerator. Underlining is not
done via the "normal" functions since they are too much of a mess...*/
if ( entry->label && *entry->label )
{
fl_drw_text( FL_ALIGN_LEFT_TOP, x, entry->box_y, w, entry->box_h,
color, ptp->entry_font_style, ptp->entry_font_size,
entry->label );
if ( entry->ulpos >= 0 )
fl_rectangle( 1, x + entry->ul_x, entry->box_y + entry->ul_y ,
entry->ul_w, entry->ul_h, color );
}
if ( entry->accel && *entry->accel )
fl_drw_text( FL_ALIGN_RIGHT_TOP, x, entry->box_y, w, entry->box_h,
color, ptp->entry_font_style, ptp->entry_font_size,
entry->accel );
}
/***************************************
* Figure out where to draw a popup window
***************************************/
static void
calculate_window_position( FL_POPUP * popup )
{
FL_POPUP_ENTRY *e;
int x, y;
unsigned int dummy;
/* If no position has been requested use the mouse position, otherwise
the requested coordinates. Negatve positions mean relative to right
edge or botton of the screen. */
fl_get_mouse( &x, &y, &dummy );
if ( ! popup->use_req_pos )
{
popup->x = x + 1;
popup->y = y + 1;
}
else
{
if ( popup->req_x >= 0 )
popup->x = popup->req_x;
else
popup->x = - popup->req_x - popup->w;
if ( popup->req_y >= 0 )
popup->y = popup->req_y;
else
popup->y = - popup->req_y - popup->h;
}
/* Try to make sure the popup is fully on the screen (for sub-popups
display to the left of the parent popup if necessary) */
if ( popup->y + ( int ) popup->h > fl_scrh && ! popup->use_req_pos )
popup->y = popup->y - ( int ) popup->h - 1;
if ( popup->y + ( int ) popup->h > fl_scrh )
popup->y = FL_max( fl_scrh - ( int ) popup->h, 0 );
if ( popup->y < 0 )
popup->y = 0;
if ( popup->x + ( int ) popup->w > fl_scrw )
{
if ( popup->parent != NULL )
popup->x = FL_max( popup->parent->x - ( int ) popup->w, 0 );
else
popup->x = FL_max( fl_scrw - ( int ) popup->w, 0 );
}
if ( ( e = find_entry( popup, x - popup->x, y - popup->y ) ) != NULL
&& ! ( e->state & FL_POPUP_DISABLED ) )
enter_leave( e, 1 );
}
/***************************************
* Create the popup's window
***************************************/
static void
create_popup_window( FL_POPUP * popup )
{
XSetWindowAttributes xswa;
unsigned long vmask;
/* Figure out where to open the window */
calculate_window_position( popup );
/* Create a new window */
popup->event_mask = ExposureMask
| ButtonPressMask
| ButtonReleaseMask
| OwnerGrabButtonMask
| PointerMotionMask
| PointerMotionHintMask
| KeyPressMask;
xswa.event_mask = popup->event_mask;
xswa.save_under = True;
xswa.backing_store = WhenMapped;
xswa.override_redirect = True;
xswa.cursor = popup->cursor;
xswa.border_pixel = 0;
xswa.colormap = fli_colormap( fl_vmode );
xswa.do_not_propagate_mask = ButtonPress | ButtonRelease | KeyPress;
vmask = CWEventMask | CWSaveUnder | CWBackingStore
| CWCursor | CWBorderPixel | CWColormap
| CWDontPropagate | CWOverrideRedirect;
popup->win = XCreateWindow( flx->display, fl_root,
popup->x, popup->y, popup->w, popup->h, 0,
fli_depth( fl_vmode ), InputOutput,
fli_visual( fl_vmode ), vmask, &xswa );
XSetTransientForHint( flx->display, popup->win, fl_root );
if ( popup->title )
XStoreName( flx->display, popup->win, popup->title );
/* Special hack for B&W */
if ( fli_dithered( fl_vmode ) )
{
XGCValues xgcv;
GC gc;
xgcv.stipple = FLI_INACTIVE_PATTERN;
vmask = GCForeground | GCFont | GCStipple;
xgcv.foreground = fl_get_flcolor( popup->text_off_color );
gc = XCreateGC( flx->display, popup->win, vmask, &xgcv );
XSetFillStyle( flx->display, gc, FillStippled );
}
XSetWMColormapWindows( flx->display, fl_root, &popup->win, 1 );
XMapRaised( flx->display, popup->win );
fl_winset( popup->win );
grab( popup );
}
/***************************************
* Grabs both the pointer and the keyboard
***************************************/
static void
grab( FL_POPUP * popup )
{
unsigned int evmask = popup->event_mask;
/* Set the window we're using */
fl_winset( popup->win );
/* Get rid of all non-pointer events in event_mask */
evmask &= ~ ( ExposureMask | KeyPressMask );
XSync( flx->display, False );
XChangeActivePointerGrab( flx->display, evmask,
popup->cursor, CurrentTime );
/* Do pointer and keyboard grab */
if ( XGrabPointer( flx->display, popup->win, False, evmask, GrabModeAsync,
GrabModeAsync, None, popup->cursor, CurrentTime )
!= GrabSuccess )
M_err( "grab", "Can't grab pointer" );
else if ( XGrabKeyboard( flx->display, popup->win, False, GrabModeAsync,
GrabModeAsync, CurrentTime ) != GrabSuccess )
{
M_err( "grab", "Can't grab keyboard" );
XUngrabPointer( flx->display, CurrentTime );
}
}
/***************************************
* Close a popup window
***************************************/
static void
close_popup( FL_POPUP * popup,
int do_callback )
{
FL_POPUP_ENTRY *e;
XEvent ev;
/* Change grab to parent popup window (if there's one), delete popup
window and drop all events for it. Sync before waiting for events
to make sure all events are already in the event queue. */
if ( popup->parent )
grab( popup->parent );
XDestroyWindow( flx->display, popup->win );
XSync( flx->display, False );
while ( XCheckWindowEvent( flx->display, popup->win, AllEventsMask, &ev ) )
/* empty */ ;
popup->win = None;
/* We have to redraw forms or popups that received a Expose event due to
the closing of a sub-popup (at least if the Xserver did not save the
content under the popups window). Not needed for top-level popups
since there the normal event loop takes care of this. */
if ( popup->parent != NULL
&& ! DoesSaveUnders( ScreenOfDisplay( flx->display, fl_screen ) ) )
{
FL_FORM *form;
FL_POPUP *p;
while ( XCheckMaskEvent( flx->display, ExposureMask, &ev ) != False )
if ( ( form = fl_win_to_form( ( ( XAnyEvent * ) &ev )->window ) )
!= NULL )
{
fl_winset( form->window );
fl_redraw_form( form );
}
else
for ( p = popups; p != NULL; p = p->next )
if ( ( ( XAnyEvent * ) &ev )->window == p->win )
{
fl_winset( p->win );
draw_popup( p );
}
fl_winset( popup->parent->win );
}
/* Run the leave callback for an active entry if we're asked to */
for ( e = popup->entries; e != NULL; e = e->next )
if ( e->is_act )
{
if ( do_callback )
enter_leave( e, 0 );
else
e->is_act = 0;
break;
}
}
/***************************************
* Do all the interaction with the popup, also dealing with other
* background tasks (taking over from the main event loop in forms.c
* while the popup is shown)
***************************************/
static FL_POPUP_RETURN *
popup_interaction( FL_POPUP * popup )
{
FL_POPUP *p;
FL_POPUP_ENTRY *e = NULL;
XEvent ev;
int timer_cnt = 0;
ev.xmotion.time = 0; /* for fli_handle_idling() */
while ( 1 )
{
long msec = fli_context->idle_delta;
if ( fli_context->timeout_rec )
fli_handle_timeouts( &msec );
/* Check for new event for the popup window, if there's none deal
with idle tasks */
if ( ! XCheckWindowEvent( flx->display, popup->win, popup->event_mask,
&ev ) )
{
if ( timer_cnt++ % 10 == 0 )
{
timer_cnt = 0;
fli_handle_idling( &ev, msec, 1 );
fl_winset( popup->win );
}
continue;
}
timer_cnt = 0;
fli_int.query_age++;
switch ( ev.type )
{
case Expose :
draw_popup( popup );
break;
case MotionNotify :
fli_int.mousex = ev.xmotion.x;
fli_int.mousey = ev.xmotion.y;
fli_int.keymask = ev.xmotion.state;
fli_int.query_age = 0;
fli_compress_event( &ev, PointerMotionMask );
popup = handle_motion( popup, ev.xmotion.x, ev.xmotion.y );
break;
case ButtonRelease :
case ButtonPress :
fli_int.mousex = ev.xbutton.x;
fli_int.mousey = ev.xbutton.y;
fli_int.keymask = ev.xbutton.state;
fli_int.query_age = 0;
/* Don't react to mouse wheel buttons */
if ( ev.xbutton.button == Button4
|| ev.xbutton.button == Button5 )
break;
/* Try to find "active" entry */
e = find_entry( popup, ev.xmotion.x, ev.xmotion.y );
/* Implement policy: in normal select mode don't react to
button release except when on a selectable item. React
to button press only when outside of popup(s). */
if ( popup->top_parent->policy == FL_POPUP_NORMAL_SELECT
&& ev.type == ButtonRelease
&& ( e == NULL || e->state & FL_POPUP_DISABLED ) )
break;
if ( ev.type == ButtonPress
&& is_on_popups( popup, ev.xmotion.x, ev.xmotion.y ) )
break;
/* Close all popups, invoking the leave callbacks if no
selection was made */
for ( p = popup; p != NULL; p = p->parent )
close_popup( p, e == NULL );
return handle_selection( e );
case KeyPress :
fli_int.mousex = ev.xkey.x;
fli_int.mousey = ev.xkey.y;
fli_int.keymask = ev.xkey.state;
fli_int.query_age = 0;
if ( ( p = handle_key( popup, ( XKeyEvent * ) &ev, &e ) ) )
popup = p;
else
return handle_selection( e );
}
}
return NULL;
}
/***************************************
* Returns if the mouse is within a popup window
***************************************/
static int
is_on_popups( FL_POPUP * popup,
int x,
int y )
{
do
{
if ( x >= 0 && x < ( int ) popup->w
&& y >= 0 && y < ( int ) popup->h )
return 1;
if ( popup->parent == NULL )
break;;
x += popup->x - popup->parent->x;
y += popup->y - popup->parent->y;
} while ( ( popup = popup->parent ) != NULL );
return 0;
}
/***************************************
* Deals with everything to be done once a selection has been made
***************************************/
static FL_POPUP_RETURN *
handle_selection( FL_POPUP_ENTRY * entry )
{
FL_POPUP *p;
int cb_result = 1;
/* If there wasn't a selection or the selected entry is disabled report
"failure" */
if ( entry == NULL || entry->state & FL_POPUP_DISABLED )
return NULL;
/* Toggle entries must change state */
if ( entry->type == FL_POPUP_TOGGLE )
{
if ( entry->state & FL_POPUP_CHECKED )
entry->state &= ~ FL_POPUP_CHECKED;
else
entry->state |= FL_POPUP_CHECKED;
}
/* For a radio entry that wasn't already set the other radio entries for
the same group must be unset before the selected one becomes set */
if ( entry->type == FL_POPUP_RADIO
&& ! ( entry->state & FL_POPUP_CHECKED ) )
{
FL_POPUP_ENTRY *e;
for ( e = entry->popup->entries; e != NULL; e = e->next )
if ( e->type == FL_POPUP_RADIO
&& e->group == entry->group )
e->state &= ~ FL_POPUP_CHECKED;
entry->state |= FL_POPUP_CHECKED;
}
/* Set up the structure to be returned and call the entries callback
function */
fli_set_popup_return( entry );
if ( entry->callback )
cb_result = entry->callback( &entry->popup->top_parent->ret );
/* Call all popup callback functions (if the selected entry is in a
sub-popup call that of the sub-popup first, then that of the parent,
grand-parent etc.). Interrupt chain of callbacks if one of them
returns FL_IGNORE. */
for ( p = entry->popup; p && cb_result != FL_IGNORE; p = p->parent )
if ( p->callback )
{
entry->popup->top_parent->ret.popup = p;
cb_result = p->callback( &entry->popup->top_parent->ret );
}
return ( cb_result != FL_IGNORE && entry->popup ) ?
&entry->popup->top_parent->ret : NULL;
}
/***************************************
* Deal with motion of the mouse
* Note: the coordinates received are relative to the current popup.
***************************************/
static FL_POPUP *
handle_motion( FL_POPUP * popup,
int x,
int y )
{
FL_POPUP_ENTRY *e,
*ce;
/* First deal with the situation where the mouse isn't on the popup */
if ( x < 0 || x >= ( int ) popup->w
|| y < 0 || y >= ( int ) popup->h )
{
FL_POPUP *p;
/* If there was an active entry make it inactive and call its
leave callback */
for ( e = popup->entries; e != NULL; e = e->next )
if ( e->is_act )
{
enter_leave( e, 0 );
break;
}
/* Check if we're on a different popup and return if we aren't.
Coordinates must be transformed to relative to the root window. */
if ( ( p = find_popup( x + popup->x, y + popup->y ) ) == NULL )
return popup;
/* Otherwise first check if we need to shift the window - coordinates
must now be relative to the new popups window and might be changed by
the routine */
x += popup->x - p->x;
y += popup->y - p->y;
motion_shift_window( p, &x, &y );
/* Also check if we're on the entry for the sub-entry we were on
before. In that case nothing needs to be done yet. Take care:
find_entry() needs to be called with the coordinates transformed to
those relative to the other popup. */
e = find_entry( p, x, y );
if ( e != NULL && e->type == FL_POPUP_SUB && e->sub == popup )
return popup;
/* Otherwise close the current sub-popup, invoking the leave callback
for an activate entry */
close_popup( popup, 1 );
/* We might not have ended up on the parent popup but a parent of
the parent, in which case also the parent popup must be closed.
Note that the coordinates must again be transformed so that they
are now relative to that of the parents window. */
return handle_motion( popup->parent, x + p->x - popup->parent->x,
y + p->y - popup->parent->y );
}
/* Check if we need to shift the window */
motion_shift_window( popup, &x, &y );
/* Test if the mouse is on a new entry*/
ce = find_entry( popup, x, y );
/* If we're still on the same entry as we were on the last call do nothing
(except for the case that we're on a entry for a sub-popup, in that case
we've got onto it via the keyboard and the sub-popup isn't open yet) */
if ( ce != NULL && ce->is_act )
return ce->type == FL_POPUP_SUB ? open_subpopup( ce ) : popup;
/* Redraw former active entry as inactive and call its leave callback*/
for ( e = popup->entries; e != NULL; e = e->next )
if ( e->is_act )
{
enter_leave( e, 0 );
break;
}
/* If we are on a new, non-disabled entry mark it as active and call its
enter callback. If the new entry is a sub-entry open the corresponding
sub-popup (per default at the same height as the entry and to the
right of it.*/
if ( ce != NULL && ! ( ce->state & FL_POPUP_DISABLED ) )
{
enter_leave( ce, 1 );
if ( ce->type == FL_POPUP_SUB )
return open_subpopup( ce );
}
return popup;
}
/***************************************
* Shift popup window if it doesn't fit completely on the screen and the
* user is trying to move the mouse in the direction of the non-visible
* parts of the popups window.
***************************************/
static void
motion_shift_window( FL_POPUP * popup,
int * x,
int * y )
{
FL_POPUP_ENTRY *e;
static long sec = 0,
usec = 0;
long now_sec,
now_usec;
int xr = *x + popup->x, /* coordinates relative to root window */
yr = *y + popup->y;
int old_x_pos,
old_y_pos;
/* First check if parts of the popup window are off-screen and the user
is tryng to move the mouse out of the screen into a direction where
non-visible parts are. If not we're done. */
if ( ( xr > 0 || popup->x >= 0 )
&& ( xr < fl_scrw - 1 || popup->x + ( int ) popup->w <= fl_scrw )
&& ( yr > 0 || popup->y >= 0 )
&& ( yr < fl_scrh - 1 || popup->y + ( int ) popup->h <= fl_scrh ) )
return;
/* Check if the minimum time delay since last shift is over */
fl_gettime( &now_sec, &now_usec );
if ( 1000000 * ( now_sec - sec ) + now_usec - usec < WINDOW_SHIFT_DELAY )
return;
/* Shift window left/right by a fixed amout of pixels*/
old_x_pos = popup->x;
if ( xr == fl_scrw - 1 && popup->x + ( int ) popup->w > fl_scrw )
popup->x = FL_max( popup->x - WINDOW_SHIFT,
fl_scrw - ( int ) popup->w );
else if ( xr == 0 && popup->x < 0 )
popup->x = FL_min( popup->x + WINDOW_SHIFT, 0 );
*x -= popup->x - old_x_pos;
/* Shift window up/down by one entry*/
old_y_pos = popup->y;
if ( yr == fl_scrh - 1 && popup->y + ( int ) popup->h > fl_scrh)
{
/* Find first entry that extends below the bottom of the screen
and set the windows y-position so that it is shown completely */
for ( e = popup->entries; e != NULL; e = e->next )
if ( e->type != FL_POPUP_LINE
&& ! ( e->state & FL_POPUP_HIDDEN )
&& e->box_y + ( int ) e->box_h - 1 > *y )
break;
if ( e != NULL )
popup->y = fl_scrh - e->box_y - ( int ) e->box_h;
}
else if ( yr == 0 && popup->y < 0 )
{
/* Find first entry that's at least one pixel below the upper screen
border */
for ( e = popup->entries; e != NULL; e = e->next )
if ( ! ( e->state & FL_POPUP_HIDDEN )
&& e->box_y >= *y )
break;
/* Go to the one before that which isn't a line or hidden and make it
the one at the very top of the screen */
if ( e != NULL )
do
e = e->prev;
while ( e != NULL
&& ( e->type == FL_POPUP_LINE
|| e->state & FL_POPUP_HIDDEN ) );
if ( e == NULL )
popup->y = 0;
else
popup->y = - e->box_y;
}
*y -= popup->y - old_y_pos;
/* Move the window to the new position */
if ( popup->x != old_x_pos || popup->y != old_y_pos )
{
XMoveWindow( flx->display, popup->win, popup->x, popup->y );
sec = now_sec;
usec = now_usec;
}
}
/***************************************
* Handle keyboard input. If NULL gets returned either a selection was
* made (in that case 'entry' points to the selected entry) or dealing
* with the popup is to be stopped. If non-NULL is returned it's the
* popup we now have to deal with.
***************************************/
static FL_POPUP *
handle_key( FL_POPUP * popup,
XKeyEvent * ev,
FL_POPUP_ENTRY ** entry )
{
KeySym keysym = NoSymbol;
char buf[ 16 ];
FL_POPUP_ENTRY *e,
*ce;
XLookupString( ev, buf, sizeof buf, &keysym, 0 );
/* Start of with checking for shortcut keys, they may have overridden some
of the keys normally used */
if ( ( e = handle_shortcut( popup, keysym, ev->state ) ) != NULL )
{
/* Special handling for sub-popup entries: shortcut key doesn't
result in a selection but in the corresponding sub-popup being
opened (if it isn't already shown) */
if ( e->type == FL_POPUP_SUB )
{
if ( e->sub->win != None )
return popup;
for ( ce = popup->entries; ce != NULL; ce = ce->next )
if ( ce->is_act )
{
if ( ce != e )
enter_leave( ce, 0 );
break;
}
if ( ce != e )
enter_leave( e, 1 );
return open_subpopup( e );
}
/* Close all popups. Leave callbacks are not invoked since a
selection was made */
while ( popup )
{
close_popup( popup, 0 );
popup = popup->parent;
}
*entry = e;
return NULL;
}
/* <Esc> and <Cancel> close the current popup, invoking leave callback */
if ( keysym == XK_Escape || keysym == XK_Cancel )
{
close_popup( popup, 1 );
return popup->parent;
}
/* Try to find the "active" entry (there may not exist one) */
for ( ce = popup->entries; ce != NULL; ce = ce->next )
if ( ce->is_act )
break;
/* <Return> does nothing (returning the original popup) if there isn't
an active entry. If we're on a sub-entry open the sup-popup. Otherwise
return indicating that the active entry was selected after closing all
popups. */
if ( keysym == XK_Return )
{
if ( ce == NULL )
return popup;
/* If we're on a sun-popup entry open the corresponding sub-pupop */
if ( ce->type == FL_POPUP_SUB )
return open_subpopup( ce );
/* Otherwise we got a selection, so close all popups, invoke no leave
callbacks (since selection was made) and return the selected entry */
while ( popup )
{
close_popup( popup, 0 );
popup = popup->parent;
}
*entry = ce;
return NULL;
}
/* The <Right> key only does something when we're on a sub-entry. In
this case the sub-popup is opened and the top-most usable entry is
activated. */
if ( IsRight( keysym ) )
{
if ( ce == NULL || ce->type != FL_POPUP_SUB )
return popup;
for ( e = ce->sub->entries; e != NULL; e = e->next )
if ( IS_ACTIVATABLE( e ) )
break;
if ( e != NULL )
enter_leave( e, 1 );
return open_subpopup( ce );
}
/* The <Left> key only has a meaning if we're in a sub-popup. In this
case the sub-popup gets closed (leave callbacks get invoked). */
if ( IsLeft( keysym ) )
{
if ( popup->parent == NULL )
return popup;
close_popup( popup, 1 );
return popup->parent;
}
/* The <Down> key moves down to the next "activatable" entry (with
wrap-around). If no entry was active yet the first in the popup
is activated. */
if ( IsDown( keysym ) )
{
e = NULL;
if ( ce != NULL )
for ( e = ce->next; e != NULL; e = e->next )
if ( IS_ACTIVATABLE( e ) )
break;
if ( e == NULL )
for ( e = popup->entries; e != ce; e = e->next )
if ( IS_ACTIVATABLE( e ) )
break;
if ( e != NULL )
key_shift_window( popup, e );
if ( e != ce )
{
if ( ce != NULL )
enter_leave( ce, 0 );
enter_leave( e, 1 );
}
return popup;
}
/* The <Up> key moves up to the previous "activatable" entry (with
wrap-around). If no entry was active yet the last in the popup
is activated. */
if ( IsUp( keysym ) )
{
FL_POPUP_ENTRY * ei;
for ( e = NULL, ei = popup->entries; ei != ce; ei = ei->next )
if ( IS_ACTIVATABLE( ei ) )
e = ei;
if ( e == NULL && ce != NULL )
for ( ei = ce->next; ei != NULL; ei = ei->next )
if ( IS_ACTIVATABLE( ei ) )
e = ei;
if ( ce != NULL && e != NULL )
{
key_shift_window( popup, e );
enter_leave( ce, 0 );
}
if ( e != NULL )
enter_leave( e, 1 );
return popup;
}
/* The <End> key moves to the last "activatable" in the popup */
if ( IsEnd( keysym ) )
{
FL_POPUP_ENTRY *ei;
for ( e = NULL, ei = ce != NULL ? ce->next : popup->entries; ei != NULL;
ei = ei->next )
if ( IS_ACTIVATABLE( ei ) )
e = ei;
if ( ce != NULL && e != NULL )
enter_leave( ce, 0 );
if ( e != NULL )
{
key_shift_window( popup, e );
enter_leave( e, 1 );
}
return popup;
}
/* The <Home> key moves to the first "activatable" in the popup */
if ( IsHome( keysym ) )
{
for ( e = popup->entries; e != ce; e = e->next )
if ( IS_ACTIVATABLE( e ) )
break;
if ( ce != NULL && e != ce )
enter_leave( ce, 0 );
if ( e != ce )
{
key_shift_window( popup, e );
enter_leave( e, 1 );
}
return popup;
}
/* All other keys do nothing, returning the original popup indicates that */
return popup;
}
/***************************************
* Shift popup window if it doesn't fit completely on the screen and the
* user pressed a key that takes us to a non-visible entry of the window.
***************************************/
static void
key_shift_window( FL_POPUP * popup,
FL_POPUP_ENTRY * entry )
{
/* Test if the window is only partially visible on the window (in vertical
direction) and the entry we're supposed to go to isn't in the visible
part of the window. If not we're done.*/
if ( ( popup->y >= 0 && popup->y + ( int ) popup->h < fl_scrh )
|| ( popup->y + entry->box_y >= 0
&& popup->y + entry->box_y + ( int ) entry->box_h < fl_scrh ) )
return;
/* Shift window up/down */
if ( popup->y + entry->box_y < 0 )
popup->y = - entry->box_y + 1;
else
popup->y = fl_scrh - entry->box_y - ( int ) entry->box_h - 1;
XMoveWindow( flx->display, popup->win, popup->x, popup->y );
}
/***************************************
* Open a sub-popup
***************************************/
static FL_POPUP *
open_subpopup( FL_POPUP_ENTRY * entry )
{
FL_POPUP *popup = entry->popup;
int offset = FL_abs( popup->top_parent->bw )
+ ( popup->top_parent->bw > 0 ? 1 : 0 ) + OUTER_PADDING_TOP;
/* Set the position of the new sub-popup. Normally show it to the right of
the parent popup, but if this is a sub-popup of a sub-popup and the
parent sub-pupop is to the left of its parent (because there wasn't
enough room on the right side) position it also to the left. Vertically
put it at the same height as the entry its opened up from. But the
function for opening the window for the sub-popup may overrule these
settings if there isn't enough room on the screen. */
if ( popup->parent == NULL
|| popup->x > popup->parent->x )
fl_popup_set_position( entry->sub, popup->x + popup->w,
popup->y + entry->box_y - offset );
else
{
if ( entry->sub->need_recalc )
recalc_popup( entry->sub );
fl_popup_set_position( entry->sub, popup->x - entry->sub->w,
popup->y + entry->box_y - offset );
}
draw_popup( entry->sub );
return entry->sub;
}
/***************************************
* Check popup entries for a shortcut identical to a pressed key
***************************************/
static FL_POPUP_ENTRY *
handle_shortcut( FL_POPUP * popup,
long keysym,
unsigned int keymask )
{
FL_POPUP_ENTRY *e;
long *sc;
if ( controlkey_down( keymask ) && keysym >= 'a' && keysym <= 'z' )
keysym = toupper( keysym );
keysym += ( controlkey_down( keymask ) ? FL_CONTROL_MASK : 0 )
+ ( metakey_down( keymask ) ? FL_ALT_MASK : 0 );
/* Look at the shortcuts for all of the entries of the current popup,
then those of the parent etc. */
while ( popup != NULL )
{
for ( e = popup->entries; e != NULL; e = e->next )
if ( IS_ACTIVATABLE( e ) && e->shortcut != NULL )
for ( sc = e->shortcut; *sc != 0; sc++ )
if ( *sc == keysym )
return e;
popup = popup->parent;
}
return NULL;
}
/***************************************
* Handle entering (act = 1) or leaving (act = 0) of an entry
***************************************/
static void
enter_leave( FL_POPUP_ENTRY * entry,
int act )
{
/* Mark entry as entered or left */
entry->is_act = act;
/* Redraw the entry (at least while popup is shown) */
if ( entry->popup->win != None )
draw_entry( entry );
/* If a callback for the situation exists invoke it */
if ( ( act && entry->enter_callback == NULL )
|| ( ! act && entry->leave_callback == NULL ) )
return;
fli_set_popup_return( entry );
if ( act )
entry->enter_callback( &entry->popup->top_parent->ret );
else
entry->leave_callback( &entry->popup->top_parent->ret );
}
/***************************************
* Try to find a (shown) popup from its position (coordinates must be
* relative to the root window)
***************************************/
static FL_POPUP *
find_popup( int x,
int y )
{
FL_POPUP *p;
for ( p = popups; p != NULL; p = p->next )
{
if ( p->win == None )
continue;
if ( x >= p->x && x < p->x + ( int ) p->w
&& y >= p->y && y < p->y + ( int ) p->h )
return p;
}
return NULL;
}
/***************************************
* Try to find an entry in a popup from its position (coordinates must be
* relative to the popup's window)
***************************************/
static FL_POPUP_ENTRY *
find_entry( FL_POPUP * popup,
int x,
int y )
{
FL_POPUP_ENTRY *e;
for ( e = popup->entries; e != NULL; e = e->next )
{
if ( e->type == FL_POPUP_LINE || e->state & FL_POPUP_HIDDEN )
continue;
if ( x >= 0 && x < ( int ) popup->w
&& y >= e->box_y && y < e->box_y + ( int ) e->box_h )
return e;
}
return NULL;
}
/***************************************
* Makes sure the top-parent pointers in sub-popups are set correctly
***************************************/
static void
setup_subpopups( FL_POPUP * popup )
{
FL_POPUP *p;
FL_POPUP_ENTRY *e;
/* The top-parent of sub-popups must be set to the top-most popup since
it gets all its drawing properties etc. from that one. In normal popups
the top_parent entry just points back to the popup itself. */
if ( ( p = popup->parent ) != NULL )
{
while ( p->parent != NULL )
p = p->parent;
popup->top_parent = p;
}
else
popup->top_parent = popup;
for ( e = popup->entries; e != NULL; e = e->next )
if ( e->type == FL_POPUP_SUB )
setup_subpopups( e->sub );
}
/***************************************
* Removes all backspace characters from a string
* and replaces all tabs by spaces.
***************************************/
static char *
cleanup_string( char *s )
{
char *c;
if ( ! s || ! *s )
return s;
/* Remove all backspace charscters */
c = s;
while ( ( c = strchr( c, '\b' ) ) )
memmove( c, c + 1, strlen( c ) );
/* Replace tabs by single blanks */
c = s;
while ( ( c = strchr( c, '\t' ) ) )
*c++ = ' ';
return s;
}
/***************************************
* Set need_recalc flag for a popup and all its sub-popups
***************************************/
static void
set_need_recalc( FL_POPUP * popup )
{
FL_POPUP_ENTRY *e;
popup->need_recalc = 1;
for ( e = popup->entries; e != NULL; e = e->next )
if ( e->type == FL_POPUP_SUB )
set_need_recalc( e->sub );
}
/***************************************
* Function called by fl_initialize() to set up defaults
***************************************/
void
fli_popup_init( void )
{
fli_popup_finish( ); /* just to make sure... */
popup_entry_font_style = FL_NORMAL_STYLE;
popup_title_font_style = FL_EMBOSSED_STYLE;
#ifdef __sgi
popup_entry_font_size = FL_SMALL_SIZE,
popup_title_font_size = FL_SMALL_SIZE;
#else
popup_entry_font_size = FL_NORMAL_SIZE;
popup_title_font_size = FL_NORMAL_SIZE;
#endif
popup_bg_color = FL_MCOL;
popup_on_color = FL_BOTTOM_BCOL;
popup_title_color = FL_BLACK;
popup_text_color = FL_BLACK;
popup_text_on_color = FL_WHITE;
popup_text_off_color = FL_INACTIVE_COL;
popup_radio_color = FL_BLUE;
popup_bw = ( fli_cntl.borderWidth
&& FL_abs( fli_cntl.borderWidth ) <= FL_MAX_BW ) ?
fli_cntl.borderWidth : FL_BOUND_WIDTH;
popup_cursor = XC_sb_right_arrow;
popup_policy = FL_POPUP_NORMAL_SELECT;
}
/***************************************
* Function called by fl_finish() to release all memory used by popups.
***************************************/
void
fli_popup_finish( void )
{
FL_POPUP *p;
/* Delete all top-level popups, sub-popus get taken care of automatically */
while ( popups )
for ( p = popups; p != NULL; p = p->next )
if ( p->parent == NULL )
{
fl_popup_delete( p );
break;
}
}
/***************************************
* Checks if a popup exists, returns 0 if it does, 1 otherwise.
***************************************/
int
fli_check_popup_exists( FL_POPUP * popup )
{
FL_POPUP *p;
for ( p = popups; p != NULL; p = p->next )
if ( popup == p )
return 0;
return 1;
}
/***************************************
* Checks if an entry exists, returns 0 if it does, 1 otherwise.
***************************************/
int
fli_check_popup_entry_exists( FL_POPUP_ENTRY * entry )
{
FL_POPUP_ENTRY *e;
if ( entry == NULL )
return 1;
if ( fli_check_popup_exists( entry->popup ) )
return 1;
for ( e = entry->popup->entries; e != NULL; e = e->next )
if ( entry == e )
return 0;
return 1;
}
/***************************************
* Set up the return structure of a popup for a certain entry
***************************************/
FL_POPUP_RETURN *
fli_set_popup_return( FL_POPUP_ENTRY * entry )
{
entry->popup->top_parent->ret.text = entry->text;
entry->popup->top_parent->ret.label = entry->label;
entry->popup->top_parent->ret.accel = entry->accel;
entry->popup->top_parent->ret.val = entry->val;
entry->popup->top_parent->ret.user_data = entry->user_data;
entry->popup->top_parent->ret.entry = entry;
entry->popup->top_parent->ret.popup = entry->popup;
return &entry->popup->top_parent->ret;
}
/***************************************
* Reset the popups counter to 0
***************************************/
void
fli_popup_reset_counter( FL_POPUP *popup )
{
popup->counter = 0;
}
/*
* Local variables:
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|