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
|
/*
* controls.c: routines to self-manage the controls in a dialog
* box.
*/
/*
* Possible TODO in new cross-platform config box stuff:
*
* - When lining up two controls alongside each other, I wonder if
* we could conveniently arrange to centre them vertically?
* Particularly ugly in the current setup is the `Add new
* forwarded port:' static next to the rather taller `Remove'
* button.
*/
#include <assert.h>
#include <ctype.h>
#include "putty.h"
#include "misc.h"
#include "dialog.h"
#include <commctrl.h>
#define GAPBETWEEN 3
#define GAPWITHIN 1
#define GAPXBOX 7
#define GAPYBOX 4
#define DLGWIDTH 168
#define STATICHEIGHT 8
#define TITLEHEIGHT 12
#define CHECKBOXHEIGHT 8
#define RADIOHEIGHT 8
#define EDITHEIGHT 12
#define LISTHEIGHT 11
#define LISTINCREMENT 8
#define COMBOHEIGHT 12
#define PUSHBTNHEIGHT 14
#define PROGBARHEIGHT 14
DECL_WINDOWS_FUNCTION(static, void, InitCommonControls, (void));
DECL_WINDOWS_FUNCTION(static, BOOL, MakeDragList, (HWND));
DECL_WINDOWS_FUNCTION(static, int, LBItemFromPt, (HWND, POINT, BOOL));
DECL_WINDOWS_FUNCTION(static, void, DrawInsert, (HWND, HWND, int));
void init_common_controls(void)
{
HMODULE comctl32_module = load_system32_dll("comctl32.dll");
GET_WINDOWS_FUNCTION(comctl32_module, InitCommonControls);
GET_WINDOWS_FUNCTION(comctl32_module, MakeDragList);
GET_WINDOWS_FUNCTION(comctl32_module, LBItemFromPt);
GET_WINDOWS_FUNCTION(comctl32_module, DrawInsert);
p_InitCommonControls();
}
void ctlposinit(struct ctlpos *cp, HWND hwnd,
int leftborder, int rightborder, int topborder)
{
RECT r, r2;
cp->hwnd = hwnd;
cp->font = SendMessage(hwnd, WM_GETFONT, 0, 0);
cp->ypos = topborder;
GetClientRect(hwnd, &r);
r2.left = r2.top = 0;
r2.right = 4;
r2.bottom = 8;
MapDialogRect(hwnd, &r2);
cp->dlu4inpix = r2.right;
cp->width = (r.right * 4) / (r2.right) - 2 * GAPBETWEEN;
cp->xoff = leftborder;
cp->width -= leftborder + rightborder;
}
HWND doctl(struct ctlpos *cp, RECT r, const char *wclass, int wstyle,
int exstyle, const char *wtext, int wid)
{
HWND ctl;
/*
* Note nonstandard use of RECT. This is deliberate: by
* transforming the width and height directly we arrange to
* have all supposedly same-sized controls really same-sized.
*/
r.left += cp->xoff;
MapDialogRect(cp->hwnd, &r);
/*
* We can pass in cp->hwnd == NULL, to indicate a dry run
* without creating any actual controls.
*/
if (cp->hwnd) {
ctl = CreateWindowEx(exstyle, wclass, wtext, wstyle,
r.left, r.top, r.right, r.bottom,
cp->hwnd, (HMENU)(ULONG_PTR)wid, hinst, NULL);
SendMessage(ctl, WM_SETFONT, cp->font, MAKELPARAM(true, 0));
if (!strcmp(wclass, "LISTBOX")) {
/*
* Bizarre Windows bug: the list box calculates its
* number of lines based on the font it has at creation
* time, but sending it WM_SETFONT doesn't cause it to
* recalculate. So now, _after_ we've sent it
* WM_SETFONT, we explicitly resize it (to the same
* size it was already!) to force it to reconsider.
*/
SetWindowPos(ctl, NULL, 0, 0, r.right, r.bottom,
SWP_NOACTIVATE | SWP_NOCOPYBITS |
SWP_NOMOVE | SWP_NOZORDER);
}
} else
ctl = NULL;
return ctl;
}
/*
* A title bar across the top of a sub-dialog.
*/
void bartitle(struct ctlpos *cp, const char *name, int id)
{
RECT r;
r.left = GAPBETWEEN;
r.right = cp->width;
r.top = cp->ypos;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, name, id);
}
/*
* Begin a grouping box, with or without a group title.
*/
void beginbox(struct ctlpos *cp, const char *name, int idbox)
{
cp->boxystart = cp->ypos;
if (!name)
cp->boxystart -= STATICHEIGHT / 2;
if (name)
cp->ypos += STATICHEIGHT;
cp->ypos += GAPYBOX;
cp->width -= 2 * GAPXBOX;
cp->xoff += GAPXBOX;
cp->boxid = idbox;
cp->boxtext = name;
}
/*
* End a grouping box.
*/
void endbox(struct ctlpos *cp)
{
RECT r;
cp->xoff -= GAPXBOX;
cp->width += 2 * GAPXBOX;
cp->ypos += GAPYBOX - GAPBETWEEN;
r.left = GAPBETWEEN;
r.right = cp->width;
r.top = cp->boxystart;
r.bottom = cp->ypos - cp->boxystart;
doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0,
cp->boxtext ? cp->boxtext : "", cp->boxid);
cp->ypos += GAPYBOX;
}
/*
* A static line, followed by a full-width edit box.
*/
void editboxfw(struct ctlpos *cp, bool password, bool readonly,
const char *text, int staticid, int editid)
{
RECT r;
r.left = GAPBETWEEN;
r.right = cp->width;
if (text) {
r.top = cp->ypos;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
cp->ypos += STATICHEIGHT + GAPWITHIN;
}
r.top = cp->ypos;
r.bottom = EDITHEIGHT;
doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL |
(password ? ES_PASSWORD : 0) |
(readonly ? ES_READONLY : 0),
WS_EX_CLIENTEDGE, "", editid);
cp->ypos += EDITHEIGHT + GAPBETWEEN;
}
/*
* A static line, followed by a full-width combo box.
*/
void combobox(struct ctlpos *cp, const char *text, int staticid, int listid)
{
RECT r;
r.left = GAPBETWEEN;
r.right = cp->width;
if (text) {
r.top = cp->ypos;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
cp->ypos += STATICHEIGHT + GAPWITHIN;
}
r.top = cp->ypos;
r.bottom = COMBOHEIGHT * 10;
doctl(cp, r, "COMBOBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
cp->ypos += COMBOHEIGHT + GAPBETWEEN;
}
struct radio { const char *text; int id; };
static void radioline_common(struct ctlpos *cp, const char *text, int id,
int nacross, struct radio *buttons, int nbuttons)
{
RECT r;
int group;
int i;
int j;
r.left = GAPBETWEEN;
r.top = cp->ypos;
if (text) {
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
} else {
r.right = r.bottom = 0;
}
group = WS_GROUP;
i = 0;
for (j = 0; j < nbuttons; j++) {
const char *btext = buttons[j].text;
int bid = buttons[j].id;
if (i == nacross) {
cp->ypos += r.bottom + (nacross > 1 ? GAPBETWEEN : GAPWITHIN);
i = 0;
}
r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
if (j < nbuttons-1)
r.right =
(i + 1) * (cp->width + GAPBETWEEN) / nacross - r.left;
else
r.right = cp->width - r.left;
r.top = cp->ypos;
r.bottom = RADIOHEIGHT;
doctl(cp, r, "BUTTON",
BS_NOTIFY | BS_AUTORADIOBUTTON | WS_CHILD |
WS_VISIBLE | WS_TABSTOP | group, 0, btext, bid);
group = 0;
i++;
}
cp->ypos += r.bottom + GAPBETWEEN;
}
/*
* A set of radio buttons on the same line, with a static above
* them. `nacross' dictates how many parts the line is divided into
* (you might want this not to equal the number of buttons if you
* needed to line up some 2s and some 3s to look good in the same
* panel).
*
* There's a bit of a hack in here to ensure that if nacross
* exceeds the actual number of buttons, the rightmost button
* really does get all the space right to the edge of the line, so
* you can do things like
*
* (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
*/
void radioline(struct ctlpos *cp, const char *text, int id, int nacross, ...)
{
va_list ap;
struct radio *buttons;
int i, nbuttons;
va_start(ap, nacross);
nbuttons = 0;
while (1) {
const char *btext = va_arg(ap, const char *);
if (!btext)
break;
(void) va_arg(ap, int); /* id */
nbuttons++;
}
va_end(ap);
buttons = snewn(nbuttons, struct radio);
va_start(ap, nacross);
for (i = 0; i < nbuttons; i++) {
buttons[i].text = va_arg(ap, const char *);
buttons[i].id = va_arg(ap, int);
}
va_end(ap);
radioline_common(cp, text, id, nacross, buttons, nbuttons);
sfree(buttons);
}
/*
* A set of radio buttons on the same line, without a static above
* them. Otherwise just like radioline.
*/
void bareradioline(struct ctlpos *cp, int nacross, ...)
{
va_list ap;
struct radio *buttons;
int i, nbuttons;
va_start(ap, nacross);
nbuttons = 0;
while (1) {
const char *btext = va_arg(ap, const char *);
if (!btext)
break;
(void) va_arg(ap, int); /* id */
nbuttons++;
}
va_end(ap);
buttons = snewn(nbuttons, struct radio);
va_start(ap, nacross);
for (i = 0; i < nbuttons; i++) {
buttons[i].text = va_arg(ap, const char *);
buttons[i].id = va_arg(ap, int);
}
va_end(ap);
radioline_common(cp, NULL, 0, nacross, buttons, nbuttons);
sfree(buttons);
}
/*
* A set of radio buttons on multiple lines, with a static above
* them.
*/
void radiobig(struct ctlpos *cp, const char *text, int id, ...)
{
va_list ap;
struct radio *buttons;
int i, nbuttons;
va_start(ap, id);
nbuttons = 0;
while (1) {
const char *btext = va_arg(ap, const char *);
if (!btext)
break;
(void) va_arg(ap, int); /* id */
nbuttons++;
}
va_end(ap);
buttons = snewn(nbuttons, struct radio);
va_start(ap, id);
for (i = 0; i < nbuttons; i++) {
buttons[i].text = va_arg(ap, const char *);
buttons[i].id = va_arg(ap, int);
}
va_end(ap);
radioline_common(cp, text, id, 1, buttons, nbuttons);
sfree(buttons);
}
/*
* A single standalone checkbox.
*/
void checkbox(struct ctlpos *cp, const char *text, int id)
{
RECT r;
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = CHECKBOXHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "BUTTON",
BS_NOTIFY | BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
text, id);
}
/*
* Wrap a piece of text for a static text control. Returns the
* wrapped text (a malloc'ed string containing \ns), and also
* returns the number of lines required.
*/
char *staticwrap(struct ctlpos *cp, HWND hwnd, const char *text, int *lines)
{
HDC hdc = GetDC(hwnd);
int width, nlines, j;
INT *pwidths, nfit;
SIZE size;
const char *p;
RECT r;
HFONT oldfont, newfont;
strbuf *sb = strbuf_new();
p = text;
pwidths = snewn(1+strlen(text), INT);
/*
* Work out the width the text will need to fit in, by doing
* the same adjustment that the `statictext' function itself
* will perform.
*/
SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
r.left = r.top = r.bottom = 0;
r.right = cp->width;
MapDialogRect(hwnd, &r);
width = r.right;
nlines = 1;
/*
* We must select the correct font into the HDC before calling
* GetTextExtent*, or silly things will happen.
*/
newfont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
oldfont = SelectObject(hdc, newfont);
while (*p) {
if (!GetTextExtentExPoint(hdc, p, strlen(p), width,
&nfit, pwidths, &size) ||
(size_t)nfit >= strlen(p)) {
/*
* Either GetTextExtentExPoint returned failure, or the
* whole of the rest of the text fits on this line.
* Either way, we stop wrapping, copy the remainder of
* the input string unchanged to the output, and leave.
*/
put_datapl(sb, ptrlen_from_asciz(p));
break;
}
/*
* Now we search backwards along the string from `nfit',
* looking for a space at which to break the line. If we
* don't find one at all, that's fine - we'll just break
* the line at `nfit'.
*/
for (j = nfit; j > 0; j--) {
if (isspace((unsigned char)p[j])) {
nfit = j;
break;
}
}
put_data(sb, p, nfit);
put_byte(sb, '\n');
p += nfit;
while (*p && isspace((unsigned char)*p))
p++;
nlines++;
}
SelectObject(hdc, oldfont);
ReleaseDC(cp->hwnd, hdc);
if (lines) *lines = nlines;
sfree(pwidths);
return strbuf_to_str(sb);
}
/*
* A single standalone static text control.
*/
void statictext(struct ctlpos *cp, const char *text, int lines, int id)
{
RECT r;
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT * lines;
cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "STATIC",
WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP,
0, text, id);
}
/*
* An owner-drawn static text control for a panel title.
*/
void paneltitle(struct ctlpos *cp, int id)
{
RECT r;
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = TITLEHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
0, NULL, id);
}
/*
* A button on the right hand side, with a static to its left.
*/
void staticbtn(struct ctlpos *cp, const char *stext, int sid,
const char *btext, int bid)
{
const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
PUSHBTNHEIGHT : STATICHEIGHT);
RECT r;
int lwid, rwid, rpos;
rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN;
r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos;
r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
r.right = rwid;
r.bottom = PUSHBTNHEIGHT;
doctl(cp, r, "BUTTON",
BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
0, btext, bid);
cp->ypos += height + GAPBETWEEN;
}
/*
* A simple push button.
*/
void button(struct ctlpos *cp, const char *btext, int bid, bool defbtn)
{
RECT r;
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = PUSHBTNHEIGHT;
/* Q67655: the _dialog box_ must know which button is default
* as well as the button itself knowing */
if (defbtn && cp->hwnd)
SendMessage(cp->hwnd, DM_SETDEFID, bid, 0);
doctl(cp, r, "BUTTON",
BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
(defbtn ? BS_DEFPUSHBUTTON : 0) | BS_PUSHBUTTON,
0, btext, bid);
cp->ypos += PUSHBTNHEIGHT + GAPBETWEEN;
}
/*
* Like staticbtn, but two buttons.
*/
void static2btn(struct ctlpos *cp, const char *stext, int sid,
const char *btext1, int bid1, const char *btext2, int bid2)
{
const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
PUSHBTNHEIGHT : STATICHEIGHT);
RECT r;
int lwid, rwid1, rwid2, rpos1, rpos2;
rpos1 = GAPBETWEEN + (cp->width + GAPBETWEEN) / 2;
rpos2 = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
lwid = rpos1 - 2 * GAPBETWEEN;
rwid1 = rpos2 - rpos1 - GAPBETWEEN;
rwid2 = cp->width + GAPBETWEEN - rpos2;
r.left = GAPBETWEEN;
r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos1;
r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
r.right = rwid1;
r.bottom = PUSHBTNHEIGHT;
doctl(cp, r, "BUTTON",
BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
0, btext1, bid1);
r.left = rpos2;
r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
r.right = rwid2;
r.bottom = PUSHBTNHEIGHT;
doctl(cp, r, "BUTTON",
BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
0, btext2, bid2);
cp->ypos += height + GAPBETWEEN;
}
/*
* An edit control on the right hand side, with a static to its left.
*/
static void staticedit_internal(struct ctlpos *cp, const char *stext,
int sid, int eid, int percentedit,
int style)
{
const int height = (EDITHEIGHT > STATICHEIGHT ?
EDITHEIGHT : STATICHEIGHT);
RECT r;
int lwid, rwid, rpos;
rpos =
GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN;
r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos;
r.top = cp->ypos + (height - EDITHEIGHT) / 2;
r.right = rwid;
r.bottom = EDITHEIGHT;
doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
WS_EX_CLIENTEDGE, "", eid);
cp->ypos += height + GAPBETWEEN;
}
void staticedit(struct ctlpos *cp, const char *stext,
int sid, int eid, int percentedit)
{
staticedit_internal(cp, stext, sid, eid, percentedit, 0);
}
void staticpassedit(struct ctlpos *cp, const char *stext,
int sid, int eid, int percentedit)
{
staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
}
/*
* A drop-down list box on the right hand side, with a static to
* its left.
*/
void staticddl(struct ctlpos *cp, const char *stext,
int sid, int lid, int percentlist)
{
const int height = (COMBOHEIGHT > STATICHEIGHT ?
COMBOHEIGHT : STATICHEIGHT);
RECT r;
int lwid, rwid, rpos;
rpos =
GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN;
r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos;
r.top = cp->ypos + (height - EDITHEIGHT) / 2;
r.right = rwid;
r.bottom = COMBOHEIGHT*4;
doctl(cp, r, "COMBOBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
cp->ypos += height + GAPBETWEEN;
}
/*
* A combo box on the right hand side, with a static to its left.
*/
void staticcombo(struct ctlpos *cp, const char *stext,
int sid, int lid, int percentlist)
{
const int height = (COMBOHEIGHT > STATICHEIGHT ?
COMBOHEIGHT : STATICHEIGHT);
RECT r;
int lwid, rwid, rpos;
rpos =
GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN;
r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos;
r.top = cp->ypos + (height - EDITHEIGHT) / 2;
r.right = rwid;
r.bottom = COMBOHEIGHT*10;
doctl(cp, r, "COMBOBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
cp->ypos += height + GAPBETWEEN;
}
/*
* A static, with a full-width drop-down list box below it.
*/
void staticddlbig(struct ctlpos *cp, const char *stext,
int sid, int lid)
{
RECT r;
if (stext) {
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
cp->ypos += STATICHEIGHT;
}
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = COMBOHEIGHT*4;
doctl(cp, r, "COMBOBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
cp->ypos += COMBOHEIGHT + GAPBETWEEN;
}
/*
* A big multiline edit control with a static labelling it.
*/
void bigeditctrl(struct ctlpos *cp, const char *stext,
int sid, int eid, int lines)
{
RECT r;
if (stext) {
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
}
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = EDITHEIGHT + (lines - 1) * STATICHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
WS_EX_CLIENTEDGE, "", eid);
}
/*
* A list box with a static labelling it.
*/
void listbox(struct ctlpos *cp, const char *stext,
int sid, int lid, int lines, bool multi)
{
RECT r;
if (stext != NULL) {
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
}
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "LISTBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
LBS_NOTIFY | LBS_HASSTRINGS | LBS_USETABSTOPS |
(multi ? LBS_MULTIPLESEL : 0),
WS_EX_CLIENTEDGE, "", lid);
}
/*
* A tab-control substitute when a real tab control is unavailable.
*/
void ersatztab(struct ctlpos *cp, const char *stext, int sid, int lid,
int s2id)
{
const int height = (COMBOHEIGHT > STATICHEIGHT ?
COMBOHEIGHT : STATICHEIGHT);
RECT r;
int bigwid, lwid, rwid, rpos;
static const int BIGGAP = 15;
static const int MEDGAP = 3;
bigwid = cp->width + 2 * GAPBETWEEN - 2 * BIGGAP;
cp->ypos += MEDGAP;
rpos = BIGGAP + (bigwid + BIGGAP) / 2;
lwid = rpos - 2 * BIGGAP;
rwid = bigwid + BIGGAP - rpos;
r.left = BIGGAP;
r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos;
r.top = cp->ypos + (height - COMBOHEIGHT) / 2;
r.right = rwid;
r.bottom = COMBOHEIGHT * 10;
doctl(cp, r, "COMBOBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP |
CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
cp->ypos += height + MEDGAP + GAPBETWEEN;
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = 2;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
0, "", s2id);
}
/*
* A static line, followed by an edit control on the left hand side
* and a button on the right.
*/
void editbutton(struct ctlpos *cp, const char *stext, int sid,
int eid, const char *btext, int bid)
{
const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
EDITHEIGHT : PUSHBTNHEIGHT);
RECT r;
int lwid, rwid, rpos;
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN;
r.top = cp->ypos + (height - EDITHEIGHT) / 2;
r.right = lwid;
r.bottom = EDITHEIGHT;
doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
WS_EX_CLIENTEDGE, "", eid);
r.left = rpos;
r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
r.right = rwid;
r.bottom = PUSHBTNHEIGHT;
doctl(cp, r, "BUTTON",
BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
0, btext, bid);
cp->ypos += height + GAPBETWEEN;
}
/*
* A special control for manipulating an ordered preference list
* (eg. for cipher selection).
* XXX: this is a rough hack and could be improved.
*/
void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
const char *stext, int sid, int listid, int upbid, int dnbid)
{
const static int percents[] = { 5, 75, 20 };
RECT r;
int xpos, percent = 0, i;
int listheight = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
const int BTNSHEIGHT = 2*PUSHBTNHEIGHT + GAPBETWEEN;
int totalheight, buttonpos;
/* Squirrel away IDs. */
hdl->listid = listid;
hdl->upbid = upbid;
hdl->dnbid = dnbid;
/* The static label. */
if (stext != NULL) {
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
}
if (listheight > BTNSHEIGHT) {
totalheight = listheight;
buttonpos = (listheight - BTNSHEIGHT) / 2;
} else {
totalheight = BTNSHEIGHT;
buttonpos = 0;
}
for (i=0; i<3; i++) {
int left, wid;
xpos = (cp->width + GAPBETWEEN) * percent / 100;
left = xpos + GAPBETWEEN;
percent += percents[i];
xpos = (cp->width + GAPBETWEEN) * percent / 100;
wid = xpos - left;
switch (i) {
case 1: {
/* The drag list box. */
r.left = left; r.right = wid;
r.top = cp->ypos; r.bottom = listheight;
HWND ctl = doctl(cp, r, "LISTBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP |
WS_VSCROLL | LBS_HASSTRINGS | LBS_USETABSTOPS,
WS_EX_CLIENTEDGE,
"", listid);
p_MakeDragList(ctl);
break;
}
case 2:
/* The "Up" and "Down" buttons. */
/* XXX worry about accelerators if we have more than one
* prefslist on a panel */
r.left = left; r.right = wid;
r.top = cp->ypos + buttonpos; r.bottom = PUSHBTNHEIGHT;
doctl(cp, r, "BUTTON",
BS_NOTIFY | WS_CHILD | WS_VISIBLE |
WS_TABSTOP | BS_PUSHBUTTON,
0, "&Up", upbid);
r.left = left; r.right = wid;
r.top = cp->ypos + buttonpos + PUSHBTNHEIGHT + GAPBETWEEN;
r.bottom = PUSHBTNHEIGHT;
doctl(cp, r, "BUTTON",
BS_NOTIFY | WS_CHILD | WS_VISIBLE |
WS_TABSTOP | BS_PUSHBUTTON,
0, "&Down", dnbid);
break;
}
}
cp->ypos += totalheight + GAPBETWEEN;
}
/*
* Helper function for prefslist: move item in list box.
*/
static void pl_moveitem(HWND hwnd, int listid, int src, int dst)
{
int tlen, val;
char *txt;
/* Get the item's data. */
tlen = SendDlgItemMessage (hwnd, listid, LB_GETTEXTLEN, src, 0);
txt = snewn(tlen+1, char);
SendDlgItemMessage (hwnd, listid, LB_GETTEXT, src, (LPARAM) txt);
val = SendDlgItemMessage (hwnd, listid, LB_GETITEMDATA, src, 0);
/* Deselect old location. */
SendDlgItemMessage (hwnd, listid, LB_SETSEL, false, src);
/* Delete it at the old location. */
SendDlgItemMessage (hwnd, listid, LB_DELETESTRING, src, 0);
/* Insert it at new location. */
SendDlgItemMessage (hwnd, listid, LB_INSERTSTRING, dst,
(LPARAM) txt);
SendDlgItemMessage (hwnd, listid, LB_SETITEMDATA, dst,
(LPARAM) val);
/* Set selection. */
SendDlgItemMessage (hwnd, listid, LB_SETCURSEL, dst, 0);
sfree (txt);
}
int pl_itemfrompt(HWND hwnd, POINT cursor, bool scroll)
{
int ret;
POINT uppoint, downpoint;
int updist, downdist, upitem, downitem, i;
/*
* Ghastly hackery to try to figure out not which
* _item_, but which _gap between items_, the user
* is pointing at. We do this by first working out
* which list item is under the cursor, and then
* working out how far the cursor would have to
* move up or down before the answer was different.
* Then we put the insertion point _above_ the
* current item if the upper edge is closer than
* the lower edge, or _below_ it if vice versa.
*/
ret = p_LBItemFromPt(hwnd, cursor, scroll);
if (ret == -1)
return ret;
ret = p_LBItemFromPt(hwnd, cursor, false);
updist = downdist = 0;
for (i = 1; i < 4096 && (!updist || !downdist); i++) {
uppoint = downpoint = cursor;
uppoint.y -= i;
downpoint.y += i;
upitem = p_LBItemFromPt(hwnd, uppoint, false);
downitem = p_LBItemFromPt(hwnd, downpoint, false);
if (!updist && upitem != ret)
updist = i;
if (!downdist && downitem != ret)
downdist = i;
}
if (downdist < updist)
ret++;
return ret;
}
/*
* Handler for prefslist above.
*
* Return value has bit 0 set if the dialog box procedure needs to
* return true from handling this message; it has bit 1 set if a
* change may have been made in the contents of the list.
*/
int handle_prefslist(struct prefslist *hdl,
int *array, int maxmemb,
bool is_dlmsg, HWND hwnd,
WPARAM wParam, LPARAM lParam)
{
int i;
int ret = 0;
if (is_dlmsg) {
if ((int)wParam == hdl->listid) {
DRAGLISTINFO *dlm = (DRAGLISTINFO *)lParam;
int dest = 0; /* initialise to placate gcc */
switch (dlm->uNotification) {
case DL_BEGINDRAG:
/* Add a dummy item to make pl_itemfrompt() work
* better.
* FIXME: this causes scrollbar glitches if the count of
* listbox contains >= its height. */
hdl->dummyitem =
SendDlgItemMessage(hwnd, hdl->listid,
LB_ADDSTRING, 0, (LPARAM) "");
hdl->srcitem = p_LBItemFromPt(dlm->hWnd, dlm->ptCursor, true);
hdl->dragging = false;
/* XXX hack Q183115 */
SetWindowLongPtr(hwnd, DWLP_MSGRESULT, true);
ret |= 1; break;
case DL_CANCELDRAG:
p_DrawInsert(hwnd, dlm->hWnd, -1); /* Clear arrow */
SendDlgItemMessage(hwnd, hdl->listid,
LB_DELETESTRING, hdl->dummyitem, 0);
hdl->dragging = false;
ret |= 1; break;
case DL_DRAGGING:
hdl->dragging = true;
dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, true);
if (dest > hdl->dummyitem) dest = hdl->dummyitem;
p_DrawInsert (hwnd, dlm->hWnd, dest);
if (dest >= 0)
SetWindowLongPtr(hwnd, DWLP_MSGRESULT, DL_MOVECURSOR);
else
SetWindowLongPtr(hwnd, DWLP_MSGRESULT, DL_STOPCURSOR);
ret |= 1; break;
case DL_DROPPED:
if (hdl->dragging) {
dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, true);
if (dest > hdl->dummyitem) dest = hdl->dummyitem;
p_DrawInsert (hwnd, dlm->hWnd, -1);
}
SendDlgItemMessage(hwnd, hdl->listid,
LB_DELETESTRING, hdl->dummyitem, 0);
if (hdl->dragging) {
hdl->dragging = false;
if (dest >= 0) {
/* Correct for "missing" item. */
if (dest > hdl->srcitem) dest--;
pl_moveitem(hwnd, hdl->listid, hdl->srcitem, dest);
}
ret |= 2;
}
ret |= 1; break;
}
}
} else {
if (((LOWORD(wParam) == hdl->upbid) ||
(LOWORD(wParam) == hdl->dnbid)) &&
((HIWORD(wParam) == BN_CLICKED) ||
(HIWORD(wParam) == BN_DOUBLECLICKED))) {
/* Move an item up or down the list. */
/* Get the current selection, if any. */
int selection = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCURSEL, 0, 0);
if (selection == LB_ERR) {
MessageBeep(0);
} else {
int nitems;
/* Get the total number of items. */
nitems = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCOUNT, 0, 0);
/* Should we do anything? */
if (LOWORD(wParam) == hdl->upbid && (selection > 0))
pl_moveitem(hwnd, hdl->listid, selection, selection - 1);
else if (LOWORD(wParam) == hdl->dnbid && (selection < nitems - 1))
pl_moveitem(hwnd, hdl->listid, selection, selection + 1);
ret |= 2;
}
}
}
if (array) {
/* Update array to match the list box. */
for (i=0; i < maxmemb; i++)
array[i] = SendDlgItemMessage (hwnd, hdl->listid, LB_GETITEMDATA,
i, 0);
}
return ret;
}
/*
* A progress bar (from Common Controls). We like our progress bars
* to be smooth and unbroken, without those ugly divisions; some
* older compilers may not support that, but that's life.
*/
void progressbar(struct ctlpos *cp, int id)
{
RECT r;
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = PROGBARHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, PROGRESS_CLASS, WS_CHILD | WS_VISIBLE
#ifdef PBS_SMOOTH
| PBS_SMOOTH
#endif
, WS_EX_CLIENTEDGE, "", id);
}
/* ----------------------------------------------------------------------
* Platform-specific side of portable dialog-box mechanism.
*/
/*
* This function takes a string, escapes all the ampersands, and
* places a single (unescaped) ampersand in front of the first
* occurrence of the given shortcut character (which may be
* NO_SHORTCUT).
*
* Return value is a malloc'ed copy of the processed version of the
* string.
*/
static char *shortcut_escape(const char *text, char shortcut)
{
if (!text)
return NULL; /* sfree won't choke on this */
strbuf *sb = strbuf_new();
shortcut = tolower((unsigned char)shortcut);
const char *p = text;
while (*p) {
if (shortcut != NO_SHORTCUT &&
tolower((unsigned char)*p) == shortcut) {
put_byte(sb, '&');
shortcut = NO_SHORTCUT; /* stop it happening twice */
} else if (*p == '&') {
put_byte(sb, '&');
}
put_byte(sb, *p++);
}
return strbuf_to_str(sb);
}
void winctrl_add_shortcuts(struct dlgparam *dp, struct winctrl *c)
{
int i;
for (i = 0; i < lenof(c->shortcuts); i++)
if (c->shortcuts[i] != NO_SHORTCUT) {
unsigned char s = tolower((unsigned char)c->shortcuts[i]);
assert(!dp->shortcuts[s]);
dp->shortcuts[s] = true;
}
}
void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c)
{
int i;
for (i = 0; i < lenof(c->shortcuts); i++)
if (c->shortcuts[i] != NO_SHORTCUT) {
unsigned char s = tolower((unsigned char)c->shortcuts[i]);
assert(dp->shortcuts[s]);
dp->shortcuts[s] = false;
}
}
static int winctrl_cmp_byctrl(void *av, void *bv)
{
struct winctrl *a = (struct winctrl *)av;
struct winctrl *b = (struct winctrl *)bv;
if (a->ctrl < b->ctrl)
return -1;
else if (a->ctrl > b->ctrl)
return +1;
else
return 0;
}
static int winctrl_cmp_byid(void *av, void *bv)
{
struct winctrl *a = (struct winctrl *)av;
struct winctrl *b = (struct winctrl *)bv;
if (a->base_id < b->base_id)
return -1;
else if (a->base_id > b->base_id)
return +1;
else
return 0;
}
static int winctrl_cmp_byctrl_find(void *av, void *bv)
{
dlgcontrol *a = (dlgcontrol *)av;
struct winctrl *b = (struct winctrl *)bv;
if (a < b->ctrl)
return -1;
else if (a > b->ctrl)
return +1;
else
return 0;
}
static int winctrl_cmp_byid_find(void *av, void *bv)
{
int *a = (int *)av;
struct winctrl *b = (struct winctrl *)bv;
if (*a < b->base_id)
return -1;
else if (*a >= b->base_id + b->num_ids)
return +1;
else
return 0;
}
void winctrl_init(struct winctrls *wc)
{
wc->byctrl = newtree234(winctrl_cmp_byctrl);
wc->byid = newtree234(winctrl_cmp_byid);
}
void winctrl_cleanup(struct winctrls *wc)
{
struct winctrl *c;
while ((c = index234(wc->byid, 0)) != NULL) {
winctrl_remove(wc, c);
sfree(c->data);
sfree(c);
}
freetree234(wc->byctrl);
freetree234(wc->byid);
wc->byctrl = wc->byid = NULL;
}
void winctrl_add(struct winctrls *wc, struct winctrl *c)
{
struct winctrl *ret;
if (c->ctrl) {
ret = add234(wc->byctrl, c);
assert(ret == c);
}
ret = add234(wc->byid, c);
assert(ret == c);
}
void winctrl_remove(struct winctrls *wc, struct winctrl *c)
{
struct winctrl *ret;
ret = del234(wc->byctrl, c);
ret = del234(wc->byid, c);
assert(ret == c);
}
struct winctrl *winctrl_findbyctrl(struct winctrls *wc, dlgcontrol *ctrl)
{
return find234(wc->byctrl, ctrl, winctrl_cmp_byctrl_find);
}
struct winctrl *winctrl_findbyid(struct winctrls *wc, int id)
{
return find234(wc->byid, &id, winctrl_cmp_byid_find);
}
struct winctrl *winctrl_findbyindex(struct winctrls *wc, int index)
{
return index234(wc->byid, index);
}
static void move_windows(HWND hwnd, int base_id, int num_ids, LONG dy)
{
if (!dy)
return;
for (int i = 0; i < num_ids; i++) {
HWND win = GetDlgItem(hwnd, base_id + i);
RECT rect;
if (!GetWindowRect(win, &rect))
continue;
POINT p;
p.x = rect.left;
p.y = rect.top + dy;
if (!ScreenToClient(hwnd, &p))
continue;
SetWindowPos(win, NULL, p.x, p.y, 0, 0,
SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
}
}
void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
struct ctlpos *cp, struct controlset *s, int *id)
{
struct ctlpos columns[16];
int ncols, colstart, colspan;
struct ctlpos tabdelays[16];
dlgcontrol *tabdelayed[16];
int ntabdelays;
struct ctlpos pos;
char shortcuts[MAX_SHORTCUTS_PER_CTRL];
int nshortcuts;
char *escaped;
int i, actual_base_id, base_id, num_ids, align_id_relative;
void *data;
base_id = *id;
ctrlset_normalise_aligns(s);
/* Start a containing box, if we have a boxname. */
if (s->boxname && *s->boxname) {
struct winctrl *c = snew(struct winctrl);
c->ctrl = NULL;
c->base_id = c->align_id = base_id;
c->num_ids = 1;
c->data = NULL;
memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
winctrl_add(wc, c);
beginbox(cp, s->boxtitle, base_id);
base_id++;
}
/* Draw a title, if we have one. */
if (!s->boxname && s->boxtitle) {
struct winctrl *c = snew(struct winctrl);
c->ctrl = NULL;
c->base_id = c->align_id = base_id;
c->num_ids = 1;
c->data = dupstr(s->boxtitle);
memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
winctrl_add(wc, c);
paneltitle(cp, base_id);
base_id++;
}
/* Initially we have just one column. */
ncols = 1;
columns[0] = *cp; /* structure copy */
/* And initially, there are no pending tab-delayed controls. */
ntabdelays = 0;
/* Loop over each control in the controlset. */
for (i = 0; i < s->ncontrols; i++) {
dlgcontrol *ctrl = s->ctrls[i];
/*
* Generic processing that pertains to all control types.
* At the end of this if statement, we'll have produced
* `ctrl' (a pointer to the control we have to create, or
* think about creating, in this iteration of the loop),
* `pos' (a suitable ctlpos with which to position it), and
* `c' (a winctrl structure to receive details of the
* dialog IDs). Or we'll have done a `continue', if it was
* CTRL_COLUMNS and doesn't require any control creation at
* all.
*/
if (ctrl->type == CTRL_COLUMNS) {
assert((ctrl->columns.ncols == 1) ^ (ncols == 1));
if (ncols == 1) {
/*
* We're splitting into multiple columns.
*/
int lpercent, rpercent, lx, rx, i;
ncols = ctrl->columns.ncols;
assert(ncols <= lenof(columns));
for (i = 1; i < ncols; i++)
columns[i] = columns[0]; /* structure copy */
lpercent = 0;
for (i = 0; i < ncols; i++) {
rpercent = lpercent + ctrl->columns.percentages[i];
lx = columns[i].xoff + lpercent *
(columns[i].width + GAPBETWEEN) / 100;
rx = columns[i].xoff + rpercent *
(columns[i].width + GAPBETWEEN) / 100;
columns[i].xoff = lx;
columns[i].width = rx - lx - GAPBETWEEN;
lpercent = rpercent;
}
} else {
/*
* We're recombining the various columns into one.
*/
int maxy = columns[0].ypos;
int i;
for (i = 1; i < ncols; i++)
if (maxy < columns[i].ypos)
maxy = columns[i].ypos;
ncols = 1;
columns[0] = *cp; /* structure copy */
columns[0].ypos = maxy;
}
continue;
} else if (ctrl->type == CTRL_TABDELAY) {
int i;
assert(!ctrl->delay_taborder);
ctrl = ctrl->tabdelay.ctrl;
for (i = 0; i < ntabdelays; i++)
if (tabdelayed[i] == ctrl)
break;
assert(i < ntabdelays); /* we have to have found it */
pos = tabdelays[i]; /* structure copy */
colstart = colspan = -1; /* indicate this was tab-delayed */
} else {
/*
* If it wasn't one of those, it's a genuine control;
* so we'll have to compute a position for it now, by
* checking its column span.
*/
int col;
colstart = COLUMN_START(ctrl->column);
colspan = COLUMN_SPAN(ctrl->column);
pos = columns[colstart]; /* structure copy */
pos.width = columns[colstart+colspan-1].width +
(columns[colstart+colspan-1].xoff - columns[colstart].xoff);
for (col = colstart; col < colstart+colspan; col++)
if (pos.ypos < columns[col].ypos)
pos.ypos = columns[col].ypos;
/*
* If this control is to be tabdelayed, add it to the
* tabdelay list, and unset pos.hwnd to inhibit actual
* control creation.
*/
if (ctrl->delay_taborder) {
assert(ntabdelays < lenof(tabdelays));
tabdelays[ntabdelays] = pos; /* structure copy */
tabdelayed[ntabdelays] = ctrl;
ntabdelays++;
pos.hwnd = NULL;
}
}
/* Most controls don't need anything in c->data. */
data = NULL;
/* And they all start off with no shortcuts registered. */
memset(shortcuts, NO_SHORTCUT, lenof(shortcuts));
nshortcuts = 0;
/* Almost all controls start at base_id. */
actual_base_id = base_id;
/* For vertical alignment purposes, the most relevant control
* in a group is usually the last one. But that can be
* overridden occasionally. */
align_id_relative = -1;
/*
* Now we're ready to actually create the control, by
* switching on its type.
*/
switch (ctrl->type) {
case CTRL_TEXT:
if (ctrl->text.wrap) {
char *wrapped, *escaped;
int lines;
num_ids = 1;
wrapped = staticwrap(&pos, cp->hwnd,
ctrl->label, &lines);
escaped = shortcut_escape(wrapped, NO_SHORTCUT);
statictext(&pos, escaped, lines, base_id);
sfree(escaped);
sfree(wrapped);
} else {
num_ids = 1;
editboxfw(&pos, false, true, NULL, 0, base_id);
SetDlgItemText(pos.hwnd, base_id, ctrl->label);
MakeDlgItemBorderless(pos.hwnd, base_id);
}
break;
case CTRL_EDITBOX:
num_ids = 2; /* static, edit */
escaped = shortcut_escape(ctrl->label,
ctrl->editbox.shortcut);
shortcuts[nshortcuts++] = ctrl->editbox.shortcut;
if (ctrl->editbox.percentwidth == 100) {
if (ctrl->editbox.has_list)
combobox(&pos, escaped,
base_id, base_id+1);
else
editboxfw(&pos, ctrl->editbox.password, false, escaped,
base_id, base_id+1);
} else {
if (ctrl->editbox.has_list) {
staticcombo(&pos, escaped, base_id, base_id+1,
ctrl->editbox.percentwidth);
} else {
(ctrl->editbox.password ? staticpassedit : staticedit)
(&pos, escaped, base_id, base_id+1,
ctrl->editbox.percentwidth);
}
}
sfree(escaped);
break;
case CTRL_RADIO: {
num_ids = ctrl->radio.nbuttons + 1; /* label as well */
struct radio *buttons;
int i;
escaped = shortcut_escape(ctrl->label,
ctrl->radio.shortcut);
shortcuts[nshortcuts++] = ctrl->radio.shortcut;
buttons = snewn(ctrl->radio.nbuttons, struct radio);
for (i = 0; i < ctrl->radio.nbuttons; i++) {
buttons[i].text =
shortcut_escape(ctrl->radio.buttons[i],
(char)(ctrl->radio.shortcuts ?
ctrl->radio.shortcuts[i] :
NO_SHORTCUT));
buttons[i].id = base_id + 1 + i;
if (ctrl->radio.shortcuts) {
assert(nshortcuts < MAX_SHORTCUTS_PER_CTRL);
shortcuts[nshortcuts++] = ctrl->radio.shortcuts[i];
}
}
radioline_common(&pos, escaped, base_id,
ctrl->radio.ncolumns,
buttons, ctrl->radio.nbuttons);
for (i = 0; i < ctrl->radio.nbuttons; i++) {
sfree((char *)buttons[i].text);
}
sfree(buttons);
sfree(escaped);
break;
}
case CTRL_CHECKBOX:
num_ids = 1;
escaped = shortcut_escape(ctrl->label,
ctrl->checkbox.shortcut);
shortcuts[nshortcuts++] = ctrl->checkbox.shortcut;
checkbox(&pos, escaped, base_id);
sfree(escaped);
break;
case CTRL_BUTTON:
escaped = shortcut_escape(ctrl->label,
ctrl->button.shortcut);
shortcuts[nshortcuts++] = ctrl->button.shortcut;
if (ctrl->button.iscancel)
actual_base_id = IDCANCEL;
num_ids = 1;
button(&pos, escaped, actual_base_id, ctrl->button.isdefault);
sfree(escaped);
break;
case CTRL_LISTBOX:
num_ids = 2;
escaped = shortcut_escape(ctrl->label,
ctrl->listbox.shortcut);
shortcuts[nshortcuts++] = ctrl->listbox.shortcut;
if (ctrl->listbox.draglist) {
data = snew(struct prefslist);
num_ids = 4;
prefslist(data, &pos, ctrl->listbox.height, escaped,
base_id, base_id+1, base_id+2, base_id+3);
shortcuts[nshortcuts++] = 'u'; /* Up */
shortcuts[nshortcuts++] = 'd'; /* Down */
} else if (ctrl->listbox.height == 0) {
/* Drop-down list. */
if (ctrl->listbox.percentwidth == 100) {
staticddlbig(&pos, escaped,
base_id, base_id+1);
} else {
staticddl(&pos, escaped, base_id,
base_id+1, ctrl->listbox.percentwidth);
}
} else {
/* Ordinary list. */
listbox(&pos, escaped, base_id, base_id+1,
ctrl->listbox.height, ctrl->listbox.multisel);
}
if (ctrl->listbox.ncols) {
/*
* This method of getting the box width is a bit of
* a hack; we'd do better to try to retrieve the
* actual width in dialog units from doctl() just
* before MapDialogRect. But that's going to be no
* fun, and this should be good enough accuracy.
*/
int width = cp->width * ctrl->listbox.percentwidth;
int *tabarray;
int i, percent;
tabarray = snewn(ctrl->listbox.ncols-1, int);
percent = 0;
for (i = 0; i < ctrl->listbox.ncols-1; i++) {
percent += ctrl->listbox.percentages[i];
tabarray[i] = width * percent / 10000;
}
SendDlgItemMessage(cp->hwnd, base_id+1, LB_SETTABSTOPS,
ctrl->listbox.ncols-1, (LPARAM)tabarray);
sfree(tabarray);
}
sfree(escaped);
break;
case CTRL_FILESELECT:
escaped = shortcut_escape(ctrl->label, ctrl->fileselect.shortcut);
shortcuts[nshortcuts++] = ctrl->fileselect.shortcut;
num_ids = 3;
if (!ctrl->fileselect.just_button) {
editbutton(&pos, escaped, base_id, base_id+1,
"Browse...", base_id+2);
} else {
button(&pos, escaped, base_id+2, false);
}
sfree(escaped);
break;
case CTRL_FONTSELECT:
num_ids = 3;
escaped = shortcut_escape(ctrl->label,
ctrl->fontselect.shortcut);
shortcuts[nshortcuts++] = ctrl->fontselect.shortcut;
statictext(&pos, escaped, 1, base_id);
staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
data = fontspec_new_default();
sfree(escaped);
break;
default:
unreachable("bad control type in winctrl_layout");
}
/* Translate the original align_id_relative of -1 into n-1 */
if (align_id_relative < 0)
align_id_relative += num_ids;
/*
* Create a `struct winctrl' for this control, and advance
* the dialog ID counter, if it's actually been created
* (and isn't tabdelayed).
*/
if (pos.hwnd) {
struct winctrl *c = snew(struct winctrl);
c->ctrl = ctrl;
c->base_id = actual_base_id;
c->align_id = c->base_id + align_id_relative;
c->num_ids = num_ids;
c->data = data;
memcpy(c->shortcuts, shortcuts, sizeof(shortcuts));
winctrl_add(wc, c);
winctrl_add_shortcuts(dp, c);
if (actual_base_id == base_id)
base_id += num_ids;
if (ctrl->align_next_to) {
/*
* Implement align_next_to by looking at the y extents
* of the two controls now that both are created, and
* moving one or the other downwards so that they're
* centred on a common horizontal line.
*/
LONG mid2 = 0;
for (dlgcontrol *thisctrl = ctrl; thisctrl;
thisctrl = thisctrl->align_next_to) {
struct winctrl *thisc = winctrl_findbyctrl(wc, thisctrl);
assert(thisc);
HWND win = GetDlgItem(pos.hwnd, thisc->align_id);
assert(win);
RECT rect;
if (!GetWindowRect(win, &rect))
continue;
if (mid2 < rect.top + rect.bottom)
mid2 = rect.top + rect.bottom;
}
for (dlgcontrol *thisctrl = ctrl; thisctrl;
thisctrl = thisctrl->align_next_to) {
struct winctrl *thisc = winctrl_findbyctrl(wc, thisctrl);
assert(thisc);
HWND win = GetDlgItem(pos.hwnd, thisc->align_id);
assert(win);
RECT rect;
if (!GetWindowRect(win, &rect))
continue;
LONG dy = (mid2 - (rect.top + rect.bottom)) / 2;
move_windows(pos.hwnd, thisc->base_id, thisc->num_ids, dy);
}
}
} else {
sfree(data);
}
if (colstart >= 0) {
/*
* Update the ypos in all columns crossed by this
* control.
*/
int i;
for (i = colstart; i < colstart+colspan; i++)
columns[i].ypos = pos.ypos;
}
}
/*
* We've now finished laying out the controls; so now update
* the ctlpos and control ID that were passed in, terminate
* any containing box, and return.
*/
for (i = 0; i < ncols; i++)
if (cp->ypos < columns[i].ypos)
cp->ypos = columns[i].ypos;
*id = base_id;
if (s->boxname && *s->boxname)
endbox(cp);
}
static void winctrl_set_focus(dlgcontrol *ctrl, struct dlgparam *dp,
bool has_focus)
{
if (has_focus) {
if (dp->focused)
dp->lastfocused = dp->focused;
dp->focused = ctrl;
} else if (!has_focus && dp->focused == ctrl) {
dp->lastfocused = dp->focused;
dp->focused = NULL;
}
}
dlgcontrol *dlg_last_focused(dlgcontrol *ctrl, dlgparam *dp)
{
return dp->focused == ctrl ? dp->lastfocused : dp->focused;
}
/*
* The dialog-box procedure calls this function to handle Windows
* messages on a control we manage.
*/
bool winctrl_handle_command(struct dlgparam *dp, UINT msg,
WPARAM wParam, LPARAM lParam)
{
struct winctrl *c;
dlgcontrol *ctrl;
int i, id;
bool ret;
static UINT draglistmsg = WM_NULL;
/*
* Filter out pointless window messages. Our interest is in
* WM_COMMAND and the drag list message, and nothing else.
*/
if (draglistmsg == WM_NULL)
draglistmsg = RegisterWindowMessage (DRAGLISTMSGSTRING);
if (msg != draglistmsg && msg != WM_COMMAND && msg != WM_DRAWITEM)
return false;
/*
* Look up the control ID in our data.
*/
c = NULL;
for (i = 0; i < dp->nctrltrees; i++) {
c = winctrl_findbyid(dp->controltrees[i], LOWORD(wParam));
if (c)
break;
}
if (!c)
return false; /* we have nothing to do */
if (msg == WM_DRAWITEM) {
/*
* Owner-draw request for a panel title.
*/
LPDRAWITEMSTRUCT di = (LPDRAWITEMSTRUCT) lParam;
HDC hdc = di->hDC;
RECT r = di->rcItem;
SIZE s;
SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
GetTextExtentPoint32(hdc, (char *)c->data,
strlen((char *)c->data), &s);
DrawEdge(hdc, &r, EDGE_ETCHED, BF_ADJUST | BF_RECT);
TextOut(hdc,
r.left + (r.right-r.left-s.cx)/2,
r.top + (r.bottom-r.top-s.cy)/2,
(char *)c->data, strlen((char *)c->data));
return true;
}
ctrl = c->ctrl;
id = LOWORD(wParam) - c->base_id;
if (!ctrl || !ctrl->handler)
return false; /* nothing we can do here */
/*
* From here on we do not issue `return' statements until the
* very end of the dialog box: any event handler is entitled to
* ask for a colour selector, so we _must_ always allow control
* to reach the end of this switch statement so that the
* subsequent code can test dp->coloursel_wanted().
*/
ret = false;
dp->coloursel_wanted = false;
/*
* Now switch on the control type and the message.
*/
switch (ctrl->type) {
case CTRL_EDITBOX:
if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
(HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
if (msg == WM_COMMAND && ctrl->editbox.has_list &&
(HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
HIWORD(wParam) == EN_CHANGE)
ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
if (msg == WM_COMMAND &&
ctrl->editbox.has_list) {
if (HIWORD(wParam) == CBN_SELCHANGE) {
int index, len;
char *text;
index = SendDlgItemMessage(dp->hwnd, c->base_id+1,
CB_GETCURSEL, 0, 0);
len = SendDlgItemMessage(dp->hwnd, c->base_id+1,
CB_GETLBTEXTLEN, index, 0);
text = snewn(len+1, char);
SendDlgItemMessage(dp->hwnd, c->base_id+1, CB_GETLBTEXT,
index, (LPARAM)text);
SetDlgItemText(dp->hwnd, c->base_id+1, text);
sfree(text);
ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
} else if (HIWORD(wParam) == CBN_EDITCHANGE) {
ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
} else if (HIWORD(wParam) == CBN_KILLFOCUS) {
ctrl->handler(ctrl, dp, dp->data, EVENT_REFRESH);
}
}
break;
case CTRL_RADIO:
if (msg == WM_COMMAND &&
(HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
/*
* We sometimes get spurious BN_CLICKED messages for the
* radio button that is just about to _lose_ selection, if
* we're switching using the arrow keys. Therefore we
* double-check that the button in wParam is actually
* checked before generating an event.
*/
if (msg == WM_COMMAND &&
(HIWORD(wParam) == BN_CLICKED ||
HIWORD(wParam) == BN_DOUBLECLICKED) &&
IsDlgButtonChecked(dp->hwnd, LOWORD(wParam))) {
ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
}
break;
case CTRL_CHECKBOX:
if (msg == WM_COMMAND &&
(HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
if (msg == WM_COMMAND &&
(HIWORD(wParam) == BN_CLICKED ||
HIWORD(wParam) == BN_DOUBLECLICKED)) {
ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
}
break;
case CTRL_BUTTON:
if (msg == WM_COMMAND &&
(HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
if (msg == WM_COMMAND &&
(HIWORD(wParam) == BN_CLICKED ||
HIWORD(wParam) == BN_DOUBLECLICKED)) {
ctrl->handler(ctrl, dp, dp->data, EVENT_ACTION);
}
break;
case CTRL_LISTBOX:
if (msg == WM_COMMAND && ctrl->listbox.height != 0 &&
(HIWORD(wParam)==LBN_SETFOCUS || HIWORD(wParam)==LBN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == LBN_SETFOCUS);
if (msg == WM_COMMAND && ctrl->listbox.height == 0 &&
(HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
if (msg == WM_COMMAND && id >= 2 &&
(HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
if (ctrl->listbox.draglist) {
int pret;
pret = handle_prefslist(c->data, NULL, 0, (msg != WM_COMMAND),
dp->hwnd, wParam, lParam);
if (pret & 2)
ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
ret = pret & 1;
} else {
if (msg == WM_COMMAND && HIWORD(wParam) == LBN_DBLCLK) {
SetCapture(dp->hwnd);
ctrl->handler(ctrl, dp, dp->data, EVENT_ACTION);
} else if (msg == WM_COMMAND && HIWORD(wParam) == LBN_SELCHANGE) {
ctrl->handler(ctrl, dp, dp->data, EVENT_SELCHANGE);
}
}
break;
case CTRL_FILESELECT:
if (msg == WM_COMMAND && id == 1 &&
(HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
if (msg == WM_COMMAND && id == 2 &&
(HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
if (msg == WM_COMMAND && id == 1 && HIWORD(wParam) == EN_CHANGE)
ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
if (id == 2 &&
(msg == WM_COMMAND &&
(HIWORD(wParam) == BN_CLICKED ||
HIWORD(wParam) == BN_DOUBLECLICKED))) {
Filename *fn_prev = NULL;
if (!ctrl->fileselect.just_button) {
wchar_t *text = GetDlgItemTextW_alloc(dp->hwnd, c->base_id+1);
if (*text)
fn_prev = filename_from_wstr(text);
sfree(text);
}
Filename *fn = request_file(
dp->hwnd, ctrl->fileselect.title, fn_prev,
ctrl->fileselect.for_writing, NULL, false,
ctrl->fileselect.filter);
if (fn_prev)
filename_free(fn_prev);
if (fn) {
if (!ctrl->fileselect.just_button) {
SetDlgItemTextW(dp->hwnd, c->base_id + 1,
filename_to_wstr(fn));
ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
} else {
assert(!c->data);
c->data = fn;
ctrl->handler(ctrl, dp, dp->data, EVENT_ACTION);
c->data = NULL;
}
filename_free(fn);
}
}
break;
case CTRL_FONTSELECT:
if (msg == WM_COMMAND && id == 2 &&
(HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
if (id == 2 &&
(msg == WM_COMMAND &&
(HIWORD(wParam) == BN_CLICKED ||
HIWORD(wParam) == BN_DOUBLECLICKED))) {
CHOOSEFONT cf;
LOGFONT lf;
HDC hdc;
FontSpec *fs = (FontSpec *)c->data;
hdc = GetDC(0);
lf.lfHeight = -MulDiv(fs->height,
GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC(0, hdc);
lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
lf.lfWeight = (fs->isbold ? FW_BOLD : 0);
lf.lfCharSet = fs->charset;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
strncpy(lf.lfFaceName, fs->name,
sizeof(lf.lfFaceName) - 1);
lf.lfFaceName[sizeof(lf.lfFaceName) - 1] = '\0';
cf.lStructSize = sizeof(cf);
cf.hwndOwner = dp->hwnd;
cf.lpLogFont = &lf;
cf.Flags = (dp->fixed_pitch_fonts ? CF_FIXEDPITCHONLY : 0) |
CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
if (ChooseFont(&cf)) {
fs = fontspec_new(lf.lfFaceName, (lf.lfWeight == FW_BOLD),
cf.iPointSize / 10, lf.lfCharSet);
dlg_fontsel_set(ctrl, dp, fs);
fontspec_free(fs);
ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
}
}
break;
}
/*
* If the above event handler has asked for a colour selector,
* now is the time to generate one.
*/
if (dp->coloursel_wanted) {
static CHOOSECOLOR cc;
static DWORD custom[16] = { 0 }; /* zero initialisers */
cc.lStructSize = sizeof(cc);
cc.hwndOwner = dp->hwnd;
cc.hInstance = (HWND) hinst;
cc.lpCustColors = custom;
cc.rgbResult = RGB(dp->coloursel_result.r,
dp->coloursel_result.g,
dp->coloursel_result.b);
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColor(&cc)) {
dp->coloursel_result.r =
(unsigned char) (cc.rgbResult & 0xFF);
dp->coloursel_result.g =
(unsigned char) (cc.rgbResult >> 8) & 0xFF;
dp->coloursel_result.b =
(unsigned char) (cc.rgbResult >> 16) & 0xFF;
dp->coloursel_result.ok = true;
} else
dp->coloursel_result.ok = false;
ctrl->handler(ctrl, dp, dp->data, EVENT_CALLBACK);
}
return ret;
}
/*
* This function can be called to produce context help on a
* control. Returns true if it has actually launched some help.
*/
bool winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id)
{
int i;
struct winctrl *c;
/*
* Look up the control ID in our data.
*/
c = NULL;
for (i = 0; i < dp->nctrltrees; i++) {
c = winctrl_findbyid(dp->controltrees[i], id);
if (c)
break;
}
if (!c)
return false; /* we have nothing to do */
/*
* This is the Windows front end, so we're allowed to assume
* `helpctx' is a context string.
*/
if (!c->ctrl || !c->ctrl->helpctx)
return false; /* no help available for this ctrl */
launch_help(hwnd, c->ctrl->helpctx);
return true;
}
/*
* Now the various functions that the platform-independent
* mechanism can call to access the dialog box entries.
*/
static struct winctrl *dlg_findbyctrl(struct dlgparam *dp, dlgcontrol *ctrl)
{
int i;
for (i = 0; i < dp->nctrltrees; i++) {
struct winctrl *c = winctrl_findbyctrl(dp->controltrees[i], ctrl);
if (c)
return c;
}
return NULL;
}
bool dlg_is_visible(dlgcontrol *ctrl, dlgparam *dp)
{
/*
* In this implementation of the dialog box, we physically
* uncreate controls that aren't in a visible panel of the config
* box. So we can tell if a control is visible just by checking if
* it _exists_.
*/
return dlg_findbyctrl(dp, ctrl) != NULL;
}
void dlg_radiobutton_set(dlgcontrol *ctrl, dlgparam *dp, int whichbutton)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_RADIO);
CheckRadioButton(dp->hwnd,
c->base_id + 1,
c->base_id + c->ctrl->radio.nbuttons,
c->base_id + 1 + whichbutton);
}
int dlg_radiobutton_get(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
int i;
assert(c && c->ctrl->type == CTRL_RADIO);
for (i = 0; i < c->ctrl->radio.nbuttons; i++)
if (IsDlgButtonChecked(dp->hwnd, c->base_id + 1 + i))
return i;
unreachable("no radio button was checked");
}
void dlg_checkbox_set(dlgcontrol *ctrl, dlgparam *dp, bool checked)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_CHECKBOX);
CheckDlgButton(dp->hwnd, c->base_id, checked);
}
bool dlg_checkbox_get(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_CHECKBOX);
return 0 != IsDlgButtonChecked(dp->hwnd, c->base_id);
}
void dlg_editbox_set(dlgcontrol *ctrl, dlgparam *dp, char const *text)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_EDITBOX);
SetDlgItemText(dp->hwnd, c->base_id+1, text);
}
void dlg_editbox_set_utf8(dlgcontrol *ctrl, dlgparam *dp, char const *text)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_EDITBOX);
wchar_t *wtext = dup_mb_to_wc(CP_UTF8, text);
SetDlgItemTextW(dp->hwnd, c->base_id+1, wtext);
sfree(wtext);
}
char *dlg_editbox_get(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_EDITBOX);
return GetDlgItemText_alloc(dp->hwnd, c->base_id+1);
}
char *dlg_editbox_get_utf8(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_EDITBOX);
wchar_t *wtext = GetDlgItemTextW_alloc(dp->hwnd, c->base_id+1);
char *text = dup_wc_to_mb(CP_UTF8, wtext, "");
sfree(wtext);
return text;
}
void dlg_editbox_select_range(dlgcontrol *ctrl, dlgparam *dp,
size_t start, size_t len)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_EDITBOX);
SendDlgItemMessage(dp->hwnd, c->base_id+1, EM_SETSEL, start, start+len);
}
/* The `listbox' functions can also apply to combo boxes. */
void dlg_listbox_clear(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
int msg;
assert(c &&
(c->ctrl->type == CTRL_LISTBOX ||
(c->ctrl->type == CTRL_EDITBOX &&
c->ctrl->editbox.has_list)));
msg = (c->ctrl->type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
LB_RESETCONTENT : CB_RESETCONTENT);
SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
}
void dlg_listbox_del(dlgcontrol *ctrl, dlgparam *dp, int index)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
int msg;
assert(c &&
(c->ctrl->type == CTRL_LISTBOX ||
(c->ctrl->type == CTRL_EDITBOX &&
c->ctrl->editbox.has_list)));
msg = (c->ctrl->type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
LB_DELETESTRING : CB_DELETESTRING);
SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
}
void dlg_listbox_add(dlgcontrol *ctrl, dlgparam *dp, char const *text)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
int msg;
assert(c &&
(c->ctrl->type == CTRL_LISTBOX ||
(c->ctrl->type == CTRL_EDITBOX &&
c->ctrl->editbox.has_list)));
msg = (c->ctrl->type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
LB_ADDSTRING : CB_ADDSTRING);
SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
}
/*
* Each listbox entry may have a numeric id associated with it.
* Note that some front ends only permit a string to be stored at
* each position, which means that _if_ you put two identical
* strings in any listbox then you MUST not assign them different
* IDs and expect to get meaningful results back.
*/
void dlg_listbox_addwithid(dlgcontrol *ctrl, dlgparam *dp,
char const *text, int id)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
int msg, msg2, index;
assert(c &&
(c->ctrl->type == CTRL_LISTBOX ||
(c->ctrl->type == CTRL_EDITBOX &&
c->ctrl->editbox.has_list)));
msg = (c->ctrl->type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
LB_ADDSTRING : CB_ADDSTRING);
msg2 = (c->ctrl->type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
LB_SETITEMDATA : CB_SETITEMDATA);
index = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
SendDlgItemMessage(dp->hwnd, c->base_id+1, msg2, index, (LPARAM)id);
}
int dlg_listbox_getid(dlgcontrol *ctrl, dlgparam *dp, int index)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
int msg;
assert(c && c->ctrl->type == CTRL_LISTBOX);
msg = (c->ctrl->listbox.height != 0 ? LB_GETITEMDATA : CB_GETITEMDATA);
return
SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
}
/* dlg_listbox_index returns <0 if no single element is selected. */
int dlg_listbox_index(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
int msg, ret;
assert(c && c->ctrl->type == CTRL_LISTBOX);
if (c->ctrl->listbox.multisel) {
assert(c->ctrl->listbox.height != 0); /* not combo box */
ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSELCOUNT, 0, 0);
if (ret == LB_ERR || ret > 1)
return -1;
}
msg = (c->ctrl->listbox.height != 0 ? LB_GETCURSEL : CB_GETCURSEL);
ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
if (ret == LB_ERR)
return -1;
else
return ret;
}
bool dlg_listbox_issel(dlgcontrol *ctrl, dlgparam *dp, int index)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_LISTBOX &&
c->ctrl->listbox.multisel &&
c->ctrl->listbox.height != 0);
return
SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSEL, index, 0);
}
void dlg_listbox_select(dlgcontrol *ctrl, dlgparam *dp, int index)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
int msg;
assert(c && c->ctrl->type == CTRL_LISTBOX &&
!c->ctrl->listbox.multisel);
msg = (c->ctrl->listbox.height != 0 ? LB_SETCURSEL : CB_SETCURSEL);
SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
}
void dlg_text_set(dlgcontrol *ctrl, dlgparam *dp, char const *text)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_TEXT);
SetDlgItemText(dp->hwnd, c->base_id, text);
}
void dlg_label_change(dlgcontrol *ctrl, dlgparam *dp, char const *text)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
char *escaped = NULL;
int id = -1;
assert(c);
switch (c->ctrl->type) {
case CTRL_EDITBOX:
escaped = shortcut_escape(text, c->ctrl->editbox.shortcut);
id = c->base_id;
break;
case CTRL_RADIO:
escaped = shortcut_escape(text, c->ctrl->radio.shortcut);
id = c->base_id;
break;
case CTRL_CHECKBOX:
escaped = shortcut_escape(text, ctrl->checkbox.shortcut);
id = c->base_id;
break;
case CTRL_BUTTON:
escaped = shortcut_escape(text, ctrl->button.shortcut);
id = c->base_id;
break;
case CTRL_LISTBOX:
escaped = shortcut_escape(text, ctrl->listbox.shortcut);
id = c->base_id;
break;
case CTRL_FILESELECT:
escaped = shortcut_escape(text, ctrl->fileselect.shortcut);
if (ctrl->fileselect.just_button)
id = c->base_id + 2; /* the button */
else
id = c->base_id; /* the label */
break;
case CTRL_FONTSELECT:
escaped = shortcut_escape(text, ctrl->fontselect.shortcut);
id = c->base_id;
break;
default:
unreachable("bad control type in label_change");
}
if (escaped) {
SetDlgItemText(dp->hwnd, id, escaped);
sfree(escaped);
}
}
void dlg_filesel_set(dlgcontrol *ctrl, dlgparam *dp, Filename *fn)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c);
assert(c->ctrl->type == CTRL_FILESELECT);
assert(!c->ctrl->fileselect.just_button);
SetDlgItemTextW(dp->hwnd, c->base_id+1, fn->wpath);
}
Filename *dlg_filesel_get(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c);
assert(c->ctrl->type == CTRL_FILESELECT);
if (!c->ctrl->fileselect.just_button) {
wchar_t *tmp = GetDlgItemTextW_alloc(dp->hwnd, c->base_id+1);
Filename *ret = filename_from_wstr(tmp);
sfree(tmp);
return ret;
} else {
return filename_copy(c->data);
}
}
void dlg_fontsel_set(dlgcontrol *ctrl, dlgparam *dp, FontSpec *fs)
{
char *buf, *boldstr;
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_FONTSELECT);
fontspec_free((FontSpec *)c->data);
c->data = fontspec_copy(fs);
boldstr = (fs->isbold ? "bold, " : "");
if (fs->height == 0)
buf = dupprintf("Font: %s, %sdefault height", fs->name, boldstr);
else
buf = dupprintf("Font: %s, %s%d-%s", fs->name, boldstr,
(fs->height < 0 ? -fs->height : fs->height),
(fs->height < 0 ? "pixel" : "point"));
SetDlgItemText(dp->hwnd, c->base_id+1, buf);
sfree(buf);
dlg_auto_set_fixed_pitch_flag(dp);
}
FontSpec *dlg_fontsel_get(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->type == CTRL_FONTSELECT);
return fontspec_copy((FontSpec *)c->data);
}
/*
* Bracketing a large set of updates in these two functions will
* cause the front end (if possible) to delay updating the screen
* until it's all complete, thus avoiding flicker.
*/
void dlg_update_start(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
if (c && c->ctrl->type == CTRL_LISTBOX) {
SendDlgItemMessage(dp->hwnd, c->base_id+1, WM_SETREDRAW, false, 0);
}
}
void dlg_update_done(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
if (c && c->ctrl->type == CTRL_LISTBOX) {
HWND hw = GetDlgItem(dp->hwnd, c->base_id+1);
SendMessage(hw, WM_SETREDRAW, true, 0);
InvalidateRect(hw, NULL, true);
}
}
void dlg_set_focus(dlgcontrol *ctrl, dlgparam *dp)
{
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
int id;
HWND ctl;
if (!c)
return;
switch (ctrl->type) {
case CTRL_EDITBOX: id = c->base_id + 1; break;
case CTRL_RADIO:
for (id = c->base_id + ctrl->radio.nbuttons; id > 1; id--)
if (IsDlgButtonChecked(dp->hwnd, id))
break;
/*
* In the theoretically-unlikely case that no button was
* selected, id should come out of this as 1, which is a
* reasonable enough choice.
*/
break;
case CTRL_CHECKBOX: id = c->base_id; break;
case CTRL_BUTTON: id = c->base_id; break;
case CTRL_LISTBOX: id = c->base_id + 1; break;
case CTRL_FILESELECT: id = c->base_id + 1; break;
case CTRL_FONTSELECT: id = c->base_id + 2; break;
default: id = c->base_id; break;
}
ctl = GetDlgItem(dp->hwnd, id);
SetFocus(ctl);
}
/*
* During event processing, you might well want to give an error
* indication to the user. dlg_beep() is a quick and easy generic
* error; dlg_error() puts up a message-box or equivalent.
*/
void dlg_beep(dlgparam *dp)
{
MessageBeep(0);
}
void dlg_error_msg(dlgparam *dp, const char *msg)
{
MessageBox(dp->hwnd, msg,
dp->errtitle ? dp->errtitle : NULL,
MB_OK | MB_ICONERROR);
}
/*
* This function signals to the front end that the dialog's
* processing is completed, and passes an integer value (typically
* a success status).
*/
void dlg_end(dlgparam *dp, int value)
{
dp->ended = true;
dp->endresult = value;
}
void dlg_refresh(dlgcontrol *ctrl, dlgparam *dp)
{
int i, j;
struct winctrl *c;
if (!ctrl) {
/*
* Send EVENT_REFRESH to absolutely everything.
*/
for (j = 0; j < dp->nctrltrees; j++) {
for (i = 0;
(c = winctrl_findbyindex(dp->controltrees[j], i)) != NULL;
i++) {
if (c->ctrl && c->ctrl->handler != NULL)
c->ctrl->handler(c->ctrl, dp,
dp->data, EVENT_REFRESH);
}
}
} else {
/*
* Send EVENT_REFRESH to a specific control.
*/
if (ctrl->handler != NULL)
ctrl->handler(ctrl, dp, dp->data, EVENT_REFRESH);
}
}
void dlg_coloursel_start(dlgcontrol *ctrl, dlgparam *dp, int r, int g, int b)
{
dp->coloursel_wanted = true;
dp->coloursel_result.r = r;
dp->coloursel_result.g = g;
dp->coloursel_result.b = b;
}
bool dlg_coloursel_results(dlgcontrol *ctrl, dlgparam *dp,
int *r, int *g, int *b)
{
if (dp->coloursel_result.ok) {
*r = dp->coloursel_result.r;
*g = dp->coloursel_result.g;
*b = dp->coloursel_result.b;
return true;
} else
return false;
}
void dlg_auto_set_fixed_pitch_flag(dlgparam *dp)
{
Conf *conf = (Conf *)dp->data;
FontSpec *fs;
int quality;
HFONT hfont;
HDC hdc;
TEXTMETRIC tm;
bool is_var;
/*
* Attempt to load the current font, and see if it's
* variable-pitch. If so, start off the fixed-pitch flag for the
* dialog box as false.
*
* We assume here that any client of the dlg_* mechanism which is
* using font selectors at all is also using a normal 'Conf *'
* as dp->data.
*/
quality = conf_get_int(conf, CONF_font_quality);
fs = conf_get_fontspec(conf, CONF_font);
hfont = CreateFont(0, 0, 0, 0, FW_DONTCARE, false, false, false,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality),
FIXED_PITCH | FF_DONTCARE, fs->name);
hdc = GetDC(NULL);
if (hdc && SelectObject(hdc, hfont) && GetTextMetrics(hdc, &tm)) {
/* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
is_var = (tm.tmPitchAndFamily & TMPF_FIXED_PITCH);
} else {
is_var = false; /* assume it's basically normal */
}
if (hdc)
ReleaseDC(NULL, hdc);
if (hfont)
DeleteObject(hfont);
if (is_var)
dp->fixed_pitch_fonts = false;
}
bool dlg_get_fixed_pitch_flag(dlgparam *dp)
{
return dp->fixed_pitch_fonts;
}
void dlg_set_fixed_pitch_flag(dlgparam *dp, bool flag)
{
dp->fixed_pitch_fonts = flag;
}
void dp_init(struct dlgparam *dp)
{
dp->nctrltrees = 0;
dp->data = NULL;
dp->ended = false;
dp->focused = dp->lastfocused = NULL;
memset(dp->shortcuts, 0, sizeof(dp->shortcuts));
dp->hwnd = NULL;
dp->wintitle = dp->errtitle = NULL;
dp->fixed_pitch_fonts = true;
}
void dp_add_tree(struct dlgparam *dp, struct winctrls *wc)
{
assert(dp->nctrltrees < lenof(dp->controltrees));
dp->controltrees[dp->nctrltrees++] = wc;
}
void dp_cleanup(struct dlgparam *dp)
{
sfree(dp->wintitle);
sfree(dp->errtitle);
}
|