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
|
// Scintilla source code edit control
/** @file PlatWin.cxx
** Implementation of platform facilities on Windows.
**/
// Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <limits.h>
#include <vector>
#include <map>
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#undef WINVER
#define WINVER 0x0500
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include <windowsx.h>
#if defined(NTDDI_WIN7) && !defined(DISABLE_D2D)
#define USE_D2D 1
#endif
#if defined(USE_D2D)
#include <d2d1.h>
#include <dwrite.h>
#endif
#include "Platform.h"
#include "StringCopy.h"
#include "XPM.h"
#include "UniConversion.h"
#include "FontQuality.h"
#ifndef SPI_GETFONTSMOOTHINGCONTRAST
#define SPI_GETFONTSMOOTHINGCONTRAST 0x200C
#endif
#ifndef LOAD_LIBRARY_SEARCH_SYSTEM32
#define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
#endif
static void *PointerFromWindow(HWND hWnd) {
return reinterpret_cast<void *>(::GetWindowLongPtr(hWnd, 0));
}
static void SetWindowPointer(HWND hWnd, void *ptr) {
::SetWindowLongPtr(hWnd, 0, reinterpret_cast<LONG_PTR>(ptr));
}
extern UINT CodePageFromCharSet(DWORD characterSet, UINT documentCodePage);
static CRITICAL_SECTION crPlatformLock;
static HINSTANCE hinstPlatformRes = 0;
static HCURSOR reverseArrowCursor = NULL;
#ifdef SCI_NAMESPACE
namespace Scintilla {
#endif
Point Point::FromLong(long lpoint) {
return Point(static_cast<short>(LOWORD(lpoint)), static_cast<short>(HIWORD(lpoint)));
}
static RECT RectFromPRectangle(PRectangle prc) {
RECT rc = {static_cast<LONG>(prc.left), static_cast<LONG>(prc.top),
static_cast<LONG>(prc.right), static_cast<LONG>(prc.bottom)};
return rc;
}
#if defined(USE_D2D)
IDWriteFactory *pIDWriteFactory = 0;
ID2D1Factory *pD2DFactory = 0;
IDWriteRenderingParams *defaultRenderingParams = 0;
IDWriteRenderingParams *customClearTypeRenderingParams = 0;
static HMODULE hDLLD2D = NULL;
static HMODULE hDLLDWrite = NULL;
bool LoadD2D() {
static bool triedLoadingD2D = false;
if (!triedLoadingD2D) {
DWORD loadLibraryFlags = 0;
HMODULE kernel32 = ::GetModuleHandle(TEXT("kernel32.dll"));
if (kernel32) {
if (::GetProcAddress(kernel32, "SetDefaultDllDirectories")) {
// Availability of SetDefaultDllDirectories implies Windows 8+ or
// that KB2533623 has been installed so LoadLibraryEx can be called
// with LOAD_LIBRARY_SEARCH_SYSTEM32.
loadLibraryFlags = LOAD_LIBRARY_SEARCH_SYSTEM32;
}
}
typedef HRESULT (WINAPI *D2D1CFSig)(D2D1_FACTORY_TYPE factoryType, REFIID riid,
CONST D2D1_FACTORY_OPTIONS *pFactoryOptions, IUnknown **factory);
typedef HRESULT (WINAPI *DWriteCFSig)(DWRITE_FACTORY_TYPE factoryType, REFIID iid,
IUnknown **factory);
hDLLD2D = ::LoadLibraryEx(TEXT("D2D1.DLL"), 0, loadLibraryFlags);
if (hDLLD2D) {
D2D1CFSig fnD2DCF = (D2D1CFSig)::GetProcAddress(hDLLD2D, "D2D1CreateFactory");
if (fnD2DCF) {
// A single threaded factory as Scintilla always draw on the GUI thread
fnD2DCF(D2D1_FACTORY_TYPE_SINGLE_THREADED,
__uuidof(ID2D1Factory),
0,
reinterpret_cast<IUnknown**>(&pD2DFactory));
}
}
hDLLDWrite = ::LoadLibraryEx(TEXT("DWRITE.DLL"), 0, loadLibraryFlags);
if (hDLLDWrite) {
DWriteCFSig fnDWCF = (DWriteCFSig)::GetProcAddress(hDLLDWrite, "DWriteCreateFactory");
if (fnDWCF) {
fnDWCF(DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&pIDWriteFactory));
}
}
if (pIDWriteFactory) {
HRESULT hr = pIDWriteFactory->CreateRenderingParams(&defaultRenderingParams);
if (SUCCEEDED(hr)) {
unsigned int clearTypeContrast;
if (::SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &clearTypeContrast, 0)) {
FLOAT gamma;
if (clearTypeContrast >= 1000 && clearTypeContrast <= 2200)
gamma = static_cast<FLOAT>(clearTypeContrast) / 1000.0f;
else
gamma = defaultRenderingParams->GetGamma();
pIDWriteFactory->CreateCustomRenderingParams(gamma, defaultRenderingParams->GetEnhancedContrast(), defaultRenderingParams->GetClearTypeLevel(),
defaultRenderingParams->GetPixelGeometry(), defaultRenderingParams->GetRenderingMode(), &customClearTypeRenderingParams);
}
}
}
}
triedLoadingD2D = true;
return pIDWriteFactory && pD2DFactory;
}
#endif
struct FormatAndMetrics {
int technology;
HFONT hfont;
#if defined(USE_D2D)
IDWriteTextFormat *pTextFormat;
#endif
int extraFontFlag;
int characterSet;
FLOAT yAscent;
FLOAT yDescent;
FLOAT yInternalLeading;
FormatAndMetrics(HFONT hfont_, int extraFontFlag_, int characterSet_) :
technology(SCWIN_TECH_GDI), hfont(hfont_),
#if defined(USE_D2D)
pTextFormat(0),
#endif
extraFontFlag(extraFontFlag_), characterSet(characterSet_), yAscent(2), yDescent(1), yInternalLeading(0) {
}
#if defined(USE_D2D)
FormatAndMetrics(IDWriteTextFormat *pTextFormat_,
int extraFontFlag_,
int characterSet_,
FLOAT yAscent_,
FLOAT yDescent_,
FLOAT yInternalLeading_) :
technology(SCWIN_TECH_DIRECTWRITE),
hfont(0),
pTextFormat(pTextFormat_),
extraFontFlag(extraFontFlag_),
characterSet(characterSet_),
yAscent(yAscent_),
yDescent(yDescent_),
yInternalLeading(yInternalLeading_) {
}
#endif
~FormatAndMetrics() {
if (hfont)
::DeleteObject(hfont);
#if defined(USE_D2D)
if (pTextFormat)
pTextFormat->Release();
pTextFormat = 0;
#endif
extraFontFlag = 0;
characterSet = 0;
yAscent = 2;
yDescent = 1;
yInternalLeading = 0;
}
HFONT HFont();
};
HFONT FormatAndMetrics::HFont() {
LOGFONTW lf = {};
#if defined(USE_D2D)
if (technology == SCWIN_TECH_GDI) {
if (0 == ::GetObjectW(hfont, sizeof(lf), &lf)) {
return 0;
}
} else {
HRESULT hr = pTextFormat->GetFontFamilyName(lf.lfFaceName, LF_FACESIZE);
if (!SUCCEEDED(hr)) {
return 0;
}
lf.lfWeight = pTextFormat->GetFontWeight();
lf.lfItalic = pTextFormat->GetFontStyle() == DWRITE_FONT_STYLE_ITALIC;
lf.lfHeight = -static_cast<int>(pTextFormat->GetFontSize());
}
#else
if (0 == ::GetObjectW(hfont, sizeof(lf), &lf)) {
return 0;
}
#endif
return ::CreateFontIndirectW(&lf);
}
#ifndef CLEARTYPE_QUALITY
#define CLEARTYPE_QUALITY 5
#endif
static BYTE Win32MapFontQuality(int extraFontFlag) {
switch (extraFontFlag & SC_EFF_QUALITY_MASK) {
case SC_EFF_QUALITY_NON_ANTIALIASED:
return NONANTIALIASED_QUALITY;
case SC_EFF_QUALITY_ANTIALIASED:
return ANTIALIASED_QUALITY;
case SC_EFF_QUALITY_LCD_OPTIMIZED:
return CLEARTYPE_QUALITY;
default:
return SC_EFF_QUALITY_DEFAULT;
}
}
#if defined(USE_D2D)
static D2D1_TEXT_ANTIALIAS_MODE DWriteMapFontQuality(int extraFontFlag) {
switch (extraFontFlag & SC_EFF_QUALITY_MASK) {
case SC_EFF_QUALITY_NON_ANTIALIASED:
return D2D1_TEXT_ANTIALIAS_MODE_ALIASED;
case SC_EFF_QUALITY_ANTIALIASED:
return D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE;
case SC_EFF_QUALITY_LCD_OPTIMIZED:
return D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE;
default:
return D2D1_TEXT_ANTIALIAS_MODE_DEFAULT;
}
}
#endif
static void SetLogFont(LOGFONTW &lf, const char *faceName, int characterSet, float size, int weight, bool italic, int extraFontFlag) {
lf = LOGFONTW();
// The negative is to allow for leading
lf.lfHeight = -(abs(static_cast<int>(size + 0.5)));
lf.lfWeight = weight;
lf.lfItalic = static_cast<BYTE>(italic ? 1 : 0);
lf.lfCharSet = static_cast<BYTE>(characterSet);
lf.lfQuality = Win32MapFontQuality(extraFontFlag);
UTF16FromUTF8(faceName, strlen(faceName)+1, lf.lfFaceName, LF_FACESIZE);
}
/**
* Create a hash from the parameters for a font to allow easy checking for identity.
* If one font is the same as another, its hash will be the same, but if the hash is the
* same then they may still be different.
*/
static int HashFont(const FontParameters &fp) {
return
static_cast<int>(fp.size) ^
(fp.characterSet << 10) ^
((fp.extraFontFlag & SC_EFF_QUALITY_MASK) << 9) ^
((fp.weight/100) << 12) ^
(fp.italic ? 0x20000000 : 0) ^
(fp.technology << 15) ^
fp.faceName[0];
}
class FontCached : Font {
FontCached *next;
int usage;
float size;
LOGFONTW lf;
int technology;
int hash;
explicit FontCached(const FontParameters &fp);
~FontCached() override {}
bool SameAs(const FontParameters &fp);
void Release() override;
static FontCached *first;
public:
static FontID FindOrCreate(const FontParameters &fp);
static void ReleaseId(FontID fid_);
};
FontCached *FontCached::first = 0;
FontCached::FontCached(const FontParameters &fp) :
next(0), usage(0), size(1.0), hash(0) {
SetLogFont(lf, fp.faceName, fp.characterSet, fp.size, fp.weight, fp.italic, fp.extraFontFlag);
technology = fp.technology;
hash = HashFont(fp);
fid = 0;
if (technology == SCWIN_TECH_GDI) {
HFONT hfont = ::CreateFontIndirectW(&lf);
fid = static_cast<void *>(new FormatAndMetrics(hfont, fp.extraFontFlag, fp.characterSet));
} else {
#if defined(USE_D2D)
IDWriteTextFormat *pTextFormat;
const int faceSize = 200;
WCHAR wszFace[faceSize];
UTF16FromUTF8(fp.faceName, strlen(fp.faceName)+1, wszFace, faceSize);
FLOAT fHeight = fp.size;
DWRITE_FONT_STYLE style = fp.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;
HRESULT hr = pIDWriteFactory->CreateTextFormat(wszFace, NULL,
static_cast<DWRITE_FONT_WEIGHT>(fp.weight),
style,
DWRITE_FONT_STRETCH_NORMAL, fHeight, L"en-us", &pTextFormat);
if (SUCCEEDED(hr)) {
pTextFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
const int maxLines = 2;
DWRITE_LINE_METRICS lineMetrics[maxLines];
UINT32 lineCount = 0;
FLOAT yAscent = 1.0f;
FLOAT yDescent = 1.0f;
FLOAT yInternalLeading = 0.0f;
IDWriteTextLayout *pTextLayout = 0;
hr = pIDWriteFactory->CreateTextLayout(L"X", 1, pTextFormat,
100.0f, 100.0f, &pTextLayout);
if (SUCCEEDED(hr)) {
hr = pTextLayout->GetLineMetrics(lineMetrics, maxLines, &lineCount);
if (SUCCEEDED(hr)) {
yAscent = lineMetrics[0].baseline;
yDescent = lineMetrics[0].height - lineMetrics[0].baseline;
FLOAT emHeight;
hr = pTextLayout->GetFontSize(0, &emHeight);
if (SUCCEEDED(hr)) {
yInternalLeading = lineMetrics[0].height - emHeight;
}
}
pTextLayout->Release();
pTextFormat->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_UNIFORM, lineMetrics[0].height, lineMetrics[0].baseline);
}
fid = static_cast<void *>(new FormatAndMetrics(pTextFormat, fp.extraFontFlag, fp.characterSet, yAscent, yDescent, yInternalLeading));
}
#endif
}
usage = 1;
}
bool FontCached::SameAs(const FontParameters &fp) {
if (
(size == fp.size) &&
(lf.lfWeight == fp.weight) &&
(lf.lfItalic == static_cast<BYTE>(fp.italic ? 1 : 0)) &&
(lf.lfCharSet == fp.characterSet) &&
(lf.lfQuality == Win32MapFontQuality(fp.extraFontFlag)) &&
(technology == fp.technology)) {
wchar_t wszFace[LF_FACESIZE];
UTF16FromUTF8(fp.faceName, strlen(fp.faceName)+1, wszFace, LF_FACESIZE);
return 0 == wcscmp(lf.lfFaceName,wszFace);
}
return false;
}
void FontCached::Release() {
delete static_cast<FormatAndMetrics *>(fid);
fid = 0;
}
FontID FontCached::FindOrCreate(const FontParameters &fp) {
FontID ret = 0;
::EnterCriticalSection(&crPlatformLock);
int hashFind = HashFont(fp);
for (FontCached *cur=first; cur; cur=cur->next) {
if ((cur->hash == hashFind) &&
cur->SameAs(fp)) {
cur->usage++;
ret = cur->fid;
}
}
if (ret == 0) {
FontCached *fc = new FontCached(fp);
fc->next = first;
first = fc;
ret = fc->fid;
}
::LeaveCriticalSection(&crPlatformLock);
return ret;
}
void FontCached::ReleaseId(FontID fid_) {
::EnterCriticalSection(&crPlatformLock);
FontCached **pcur=&first;
for (FontCached *cur=first; cur; cur=cur->next) {
if (cur->fid == fid_) {
cur->usage--;
if (cur->usage == 0) {
*pcur = cur->next;
cur->Release();
cur->next = 0;
delete cur;
}
break;
}
pcur=&cur->next;
}
::LeaveCriticalSection(&crPlatformLock);
}
Font::Font() {
fid = 0;
}
Font::~Font() {
}
#define FONTS_CACHED
void Font::Create(const FontParameters &fp) {
Release();
if (fp.faceName)
fid = FontCached::FindOrCreate(fp);
}
void Font::Release() {
if (fid)
FontCached::ReleaseId(fid);
fid = 0;
}
// Buffer to hold strings and string position arrays without always allocating on heap.
// May sometimes have string too long to allocate on stack. So use a fixed stack-allocated buffer
// when less than safe size otherwise allocate on heap and free automatically.
template<typename T, int lengthStandard>
class VarBuffer {
T bufferStandard[lengthStandard];
// Private so VarBuffer objects can not be copied
VarBuffer(const VarBuffer &);
VarBuffer &operator=(const VarBuffer &);
public:
T *buffer;
explicit VarBuffer(size_t length) : buffer(0) {
if (length > lengthStandard) {
buffer = new T[length];
} else {
buffer = bufferStandard;
}
}
~VarBuffer() {
if (buffer != bufferStandard) {
delete []buffer;
buffer = 0;
}
}
};
const int stackBufferLength = 1000;
class TextWide : public VarBuffer<wchar_t, stackBufferLength> {
public:
int tlen;
TextWide(const char *s, int len, bool unicodeMode, int codePage=0) :
VarBuffer<wchar_t, stackBufferLength>(len) {
if (unicodeMode) {
tlen = static_cast<int>(UTF16FromUTF8(s, len, buffer, len));
} else {
// Support Asian string display in 9x English
tlen = ::MultiByteToWideChar(codePage, 0, s, len, buffer, len);
}
}
};
typedef VarBuffer<XYPOSITION, stackBufferLength> TextPositions;
class SurfaceGDI : public Surface {
bool unicodeMode;
HDC hdc;
bool hdcOwned;
HPEN pen;
HPEN penOld;
HBRUSH brush;
HBRUSH brushOld;
HFONT font;
HFONT fontOld;
HBITMAP bitmap;
HBITMAP bitmapOld;
int maxWidthMeasure;
int maxLenText;
int codePage;
void BrushColor(ColourDesired back);
void SetFont(Font &font_);
// Private so SurfaceGDI objects can not be copied
SurfaceGDI(const SurfaceGDI &);
SurfaceGDI &operator=(const SurfaceGDI &);
public:
SurfaceGDI();
~SurfaceGDI() override;
void Init(WindowID wid) override;
void Init(SurfaceID sid, WindowID wid) override;
void InitPixMap(int width, int height, Surface *surface_, WindowID wid) override;
void Release() override;
bool Initialised() override;
void PenColour(ColourDesired fore) override;
int LogPixelsY() override;
int DeviceHeightFont(int points) override;
void MoveTo(int x_, int y_) override;
void LineTo(int x_, int y_) override;
void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back) override;
void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back) override;
void FillRectangle(PRectangle rc, ColourDesired back) override;
void FillRectangle(PRectangle rc, Surface &surfacePattern) override;
void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back) override;
void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
ColourDesired outline, int alphaOutline, int flags) override;
void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) override;
void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) override;
void Copy(PRectangle rc, Point from, Surface &surfaceSource) override;
void DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, UINT fuOptions);
void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back) override;
void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back) override;
void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore) override;
void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) override;
XYPOSITION WidthText(Font &font_, const char *s, int len) override;
XYPOSITION WidthChar(Font &font_, char ch) override;
XYPOSITION Ascent(Font &font_) override;
XYPOSITION Descent(Font &font_) override;
XYPOSITION InternalLeading(Font &font_) override;
XYPOSITION ExternalLeading(Font &font_) override;
XYPOSITION Height(Font &font_) override;
XYPOSITION AverageCharWidth(Font &font_) override;
void SetClip(PRectangle rc) override;
void FlushCachedState() override;
void SetUnicodeMode(bool unicodeMode_) override;
void SetDBCSMode(int codePage_) override;
};
SurfaceGDI::SurfaceGDI() :
unicodeMode(false),
hdc(0), hdcOwned(false),
pen(0), penOld(0),
brush(0), brushOld(0),
font(0), fontOld(0),
bitmap(0), bitmapOld(0) {
maxWidthMeasure = INT_MAX;
// There appears to be a 16 bit string length limit in GDI on NT.
maxLenText = 65535;
codePage = 0;
}
SurfaceGDI::~SurfaceGDI() {
Release();
}
void SurfaceGDI::Release() {
if (penOld) {
::SelectObject(hdc, penOld);
::DeleteObject(pen);
penOld = 0;
}
pen = 0;
if (brushOld) {
::SelectObject(hdc, brushOld);
::DeleteObject(brush);
brushOld = 0;
}
brush = 0;
if (fontOld) {
// Fonts are not deleted as they are owned by a Font object
::SelectObject(hdc, fontOld);
fontOld = 0;
}
font = 0;
if (bitmapOld) {
::SelectObject(hdc, bitmapOld);
::DeleteObject(bitmap);
bitmapOld = 0;
}
bitmap = 0;
if (hdcOwned) {
::DeleteDC(hdc);
hdc = 0;
hdcOwned = false;
}
}
bool SurfaceGDI::Initialised() {
return hdc != 0;
}
void SurfaceGDI::Init(WindowID) {
Release();
hdc = ::CreateCompatibleDC(NULL);
hdcOwned = true;
::SetTextAlign(hdc, TA_BASELINE);
}
void SurfaceGDI::Init(SurfaceID sid, WindowID) {
Release();
hdc = static_cast<HDC>(sid);
::SetTextAlign(hdc, TA_BASELINE);
}
void SurfaceGDI::InitPixMap(int width, int height, Surface *surface_, WindowID) {
Release();
SurfaceGDI *psurfOther = static_cast<SurfaceGDI *>(surface_);
hdc = ::CreateCompatibleDC(psurfOther->hdc);
hdcOwned = true;
bitmap = ::CreateCompatibleBitmap(psurfOther->hdc, width, height);
bitmapOld = static_cast<HBITMAP>(::SelectObject(hdc, bitmap));
::SetTextAlign(hdc, TA_BASELINE);
SetUnicodeMode(psurfOther->unicodeMode);
SetDBCSMode(psurfOther->codePage);
}
void SurfaceGDI::PenColour(ColourDesired fore) {
if (pen) {
::SelectObject(hdc, penOld);
::DeleteObject(pen);
pen = 0;
penOld = 0;
}
pen = ::CreatePen(0,1,fore.AsLong());
penOld = static_cast<HPEN>(::SelectObject(hdc, pen));
}
void SurfaceGDI::BrushColor(ColourDesired back) {
if (brush) {
::SelectObject(hdc, brushOld);
::DeleteObject(brush);
brush = 0;
brushOld = 0;
}
// Only ever want pure, non-dithered brushes
ColourDesired colourNearest = ::GetNearestColor(hdc, back.AsLong());
brush = ::CreateSolidBrush(colourNearest.AsLong());
brushOld = static_cast<HBRUSH>(::SelectObject(hdc, brush));
}
void SurfaceGDI::SetFont(Font &font_) {
if (font_.GetID() != font) {
FormatAndMetrics *pfm = static_cast<FormatAndMetrics *>(font_.GetID());
PLATFORM_ASSERT(pfm->technology == SCWIN_TECH_GDI);
if (fontOld) {
::SelectObject(hdc, pfm->hfont);
} else {
fontOld = static_cast<HFONT>(::SelectObject(hdc, pfm->hfont));
}
font = pfm->hfont;
}
}
int SurfaceGDI::LogPixelsY() {
return ::GetDeviceCaps(hdc, LOGPIXELSY);
}
int SurfaceGDI::DeviceHeightFont(int points) {
return ::MulDiv(points, LogPixelsY(), 72);
}
void SurfaceGDI::MoveTo(int x_, int y_) {
::MoveToEx(hdc, x_, y_, 0);
}
void SurfaceGDI::LineTo(int x_, int y_) {
::LineTo(hdc, x_, y_);
}
void SurfaceGDI::Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back) {
PenColour(fore);
BrushColor(back);
std::vector<POINT> outline;
for (int i=0; i<npts; i++) {
POINT pt = {static_cast<LONG>(pts[i].x), static_cast<LONG>(pts[i].y)};
outline.push_back(pt);
}
::Polygon(hdc, &outline[0], npts);
}
void SurfaceGDI::RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back) {
PenColour(fore);
BrushColor(back);
const RECT rcw = RectFromPRectangle(rc);
::Rectangle(hdc, rcw.left, rcw.top, rcw.right, rcw.bottom);
}
void SurfaceGDI::FillRectangle(PRectangle rc, ColourDesired back) {
// Using ExtTextOut rather than a FillRect ensures that no dithering occurs.
// There is no need to allocate a brush either.
RECT rcw = RectFromPRectangle(rc);
::SetBkColor(hdc, back.AsLong());
::ExtTextOut(hdc, rcw.left, rcw.top, ETO_OPAQUE, &rcw, TEXT(""), 0, NULL);
}
void SurfaceGDI::FillRectangle(PRectangle rc, Surface &surfacePattern) {
HBRUSH br;
if (static_cast<SurfaceGDI &>(surfacePattern).bitmap)
br = ::CreatePatternBrush(static_cast<SurfaceGDI &>(surfacePattern).bitmap);
else // Something is wrong so display in red
br = ::CreateSolidBrush(RGB(0xff, 0, 0));
RECT rcw = RectFromPRectangle(rc);
::FillRect(hdc, &rcw, br);
::DeleteObject(br);
}
void SurfaceGDI::RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back) {
PenColour(fore);
BrushColor(back);
const RECT rcw = RectFromPRectangle(rc);
::RoundRect(hdc,
rcw.left + 1, rcw.top,
rcw.right - 1, rcw.bottom,
8, 8);
}
// Plot a point into a DWORD buffer symmetrically to all 4 quadrants
static void AllFour(DWORD *pixels, int width, int height, int x, int y, DWORD val) {
pixels[y*width+x] = val;
pixels[y*width+width-1-x] = val;
pixels[(height-1-y)*width+x] = val;
pixels[(height-1-y)*width+width-1-x] = val;
}
static DWORD dwordFromBGRA(byte b, byte g, byte r, byte a) {
union {
byte pixVal[4];
DWORD val;
} converter;
converter.pixVal[0] = b;
converter.pixVal[1] = g;
converter.pixVal[2] = r;
converter.pixVal[3] = a;
return converter.val;
}
void SurfaceGDI::AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
ColourDesired outline, int alphaOutline, int /* flags*/ ) {
const RECT rcw = RectFromPRectangle(rc);
if (rc.Width() > 0) {
HDC hMemDC = ::CreateCompatibleDC(hdc);
int width = static_cast<int>(rc.Width());
int height = static_cast<int>(rc.Height());
// Ensure not distorted too much by corners when small
cornerSize = Platform::Minimum(cornerSize, (Platform::Minimum(width, height) / 2) - 2);
BITMAPINFO bpih = {{sizeof(BITMAPINFOHEADER), width, height, 1, 32, BI_RGB, 0, 0, 0, 0, 0}};
void *image = 0;
HBITMAP hbmMem = CreateDIBSection(hMemDC, &bpih,
DIB_RGB_COLORS, &image, NULL, 0);
if (hbmMem) {
HBITMAP hbmOld = SelectBitmap(hMemDC, hbmMem);
DWORD valEmpty = dwordFromBGRA(0,0,0,0);
DWORD valFill = dwordFromBGRA(
static_cast<byte>(GetBValue(fill.AsLong()) * alphaFill / 255),
static_cast<byte>(GetGValue(fill.AsLong()) * alphaFill / 255),
static_cast<byte>(GetRValue(fill.AsLong()) * alphaFill / 255),
static_cast<byte>(alphaFill));
DWORD valOutline = dwordFromBGRA(
static_cast<byte>(GetBValue(outline.AsLong()) * alphaOutline / 255),
static_cast<byte>(GetGValue(outline.AsLong()) * alphaOutline / 255),
static_cast<byte>(GetRValue(outline.AsLong()) * alphaOutline / 255),
static_cast<byte>(alphaOutline));
DWORD *pixels = static_cast<DWORD *>(image);
for (int y=0; y<height; y++) {
for (int x=0; x<width; x++) {
if ((x==0) || (x==width-1) || (y == 0) || (y == height-1)) {
pixels[y*width+x] = valOutline;
} else {
pixels[y*width+x] = valFill;
}
}
}
for (int c=0; c<cornerSize; c++) {
for (int x=0; x<c+1; x++) {
AllFour(pixels, width, height, x, c-x, valEmpty);
}
}
for (int x=1; x<cornerSize; x++) {
AllFour(pixels, width, height, x, cornerSize-x, valOutline);
}
BLENDFUNCTION merge = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
AlphaBlend(hdc, rcw.left, rcw.top, width, height, hMemDC, 0, 0, width, height, merge);
SelectBitmap(hMemDC, hbmOld);
::DeleteObject(hbmMem);
}
::DeleteDC(hMemDC);
} else {
BrushColor(outline);
FrameRect(hdc, &rcw, brush);
}
}
void SurfaceGDI::DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) {
if (rc.Width() > 0) {
HDC hMemDC = ::CreateCompatibleDC(hdc);
if (rc.Width() > width)
rc.left += static_cast<int>((rc.Width() - width) / 2);
rc.right = rc.left + width;
if (rc.Height() > height)
rc.top += static_cast<int>((rc.Height() - height) / 2);
rc.bottom = rc.top + height;
BITMAPINFO bpih = {{sizeof(BITMAPINFOHEADER), width, height, 1, 32, BI_RGB, 0, 0, 0, 0, 0}};
unsigned char *image = 0;
HBITMAP hbmMem = CreateDIBSection(hMemDC, &bpih,
DIB_RGB_COLORS, reinterpret_cast<void **>(&image), NULL, 0);
if (hbmMem) {
HBITMAP hbmOld = SelectBitmap(hMemDC, hbmMem);
for (int y=height-1; y>=0; y--) {
for (int x=0; x<width; x++) {
unsigned char *pixel = image + (y*width+x) * 4;
unsigned char alpha = pixelsImage[3];
// Input is RGBA, output is BGRA with premultiplied alpha
pixel[2] = static_cast<unsigned char>((*pixelsImage++) * alpha / 255);
pixel[1] = static_cast<unsigned char>((*pixelsImage++) * alpha / 255);
pixel[0] = static_cast<unsigned char>((*pixelsImage++) * alpha / 255);
pixel[3] = static_cast<unsigned char>(*pixelsImage++);
}
}
BLENDFUNCTION merge = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
AlphaBlend(hdc, static_cast<int>(rc.left), static_cast<int>(rc.top),
static_cast<int>(rc.Width()), static_cast<int>(rc.Height()), hMemDC, 0, 0, width, height, merge);
SelectBitmap(hMemDC, hbmOld);
::DeleteObject(hbmMem);
}
::DeleteDC(hMemDC);
}
}
void SurfaceGDI::Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) {
PenColour(fore);
BrushColor(back);
const RECT rcw = RectFromPRectangle(rc);
::Ellipse(hdc, rcw.left, rcw.top, rcw.right, rcw.bottom);
}
void SurfaceGDI::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
::BitBlt(hdc,
static_cast<int>(rc.left), static_cast<int>(rc.top),
static_cast<int>(rc.Width()), static_cast<int>(rc.Height()),
static_cast<SurfaceGDI &>(surfaceSource).hdc,
static_cast<int>(from.x), static_cast<int>(from.y), SRCCOPY);
}
typedef VarBuffer<int, stackBufferLength> TextPositionsI;
void SurfaceGDI::DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, UINT fuOptions) {
SetFont(font_);
const RECT rcw = RectFromPRectangle(rc);
const int x = static_cast<int>(rc.left);
const int yBaseInt = static_cast<int>(ybase);
if (unicodeMode) {
const TextWide tbuf(s, len, unicodeMode, codePage);
::ExtTextOutW(hdc, x, yBaseInt, fuOptions, &rcw, tbuf.buffer, tbuf.tlen, NULL);
} else {
::ExtTextOutA(hdc, x, yBaseInt, fuOptions, &rcw, s, len, NULL);
}
}
void SurfaceGDI::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
ColourDesired fore, ColourDesired back) {
::SetTextColor(hdc, fore.AsLong());
::SetBkColor(hdc, back.AsLong());
DrawTextCommon(rc, font_, ybase, s, len, ETO_OPAQUE);
}
void SurfaceGDI::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
ColourDesired fore, ColourDesired back) {
::SetTextColor(hdc, fore.AsLong());
::SetBkColor(hdc, back.AsLong());
DrawTextCommon(rc, font_, ybase, s, len, ETO_OPAQUE | ETO_CLIPPED);
}
void SurfaceGDI::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
ColourDesired fore) {
// Avoid drawing spaces in transparent mode
for (int i=0; i<len; i++) {
if (s[i] != ' ') {
::SetTextColor(hdc, fore.AsLong());
::SetBkMode(hdc, TRANSPARENT);
DrawTextCommon(rc, font_, ybase, s, len, 0);
::SetBkMode(hdc, OPAQUE);
return;
}
}
}
XYPOSITION SurfaceGDI::WidthText(Font &font_, const char *s, int len) {
SetFont(font_);
SIZE sz={0,0};
if (!unicodeMode) {
::GetTextExtentPoint32A(hdc, s, Platform::Minimum(len, maxLenText), &sz);
} else {
const TextWide tbuf(s, len, unicodeMode, codePage);
::GetTextExtentPoint32W(hdc, tbuf.buffer, tbuf.tlen, &sz);
}
return static_cast<XYPOSITION>(sz.cx);
}
void SurfaceGDI::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) {
// Zero positions to avoid random behaviour on failure.
std::fill(positions, positions + len, 0.0f);
SetFont(font_);
SIZE sz={0,0};
int fit = 0;
int i = 0;
if (unicodeMode) {
const TextWide tbuf(s, len, unicodeMode, codePage);
TextPositionsI poses(tbuf.tlen);
if (!::GetTextExtentExPointW(hdc, tbuf.buffer, tbuf.tlen, maxWidthMeasure, &fit, poses.buffer, &sz)) {
// Failure
return;
}
// Map the widths given for UTF-16 characters back onto the UTF-8 input string
for (int ui = 0; ui < fit; ui++) {
unsigned int lenChar = UTF8BytesOfLead[static_cast<unsigned char>(s[i])];
if (lenChar == 4) { // Non-BMP
ui++;
}
for (unsigned int bytePos=0; (bytePos<lenChar) && (i<len); bytePos++) {
positions[i++] = static_cast<XYPOSITION>(poses.buffer[ui]);
}
}
} else {
TextPositionsI poses(len);
if (!::GetTextExtentExPointA(hdc, s, len, maxWidthMeasure, &fit, poses.buffer, &sz)) {
// Eeek - a NULL DC or other foolishness could cause this.
return;
}
while (i<fit) {
positions[i] = static_cast<XYPOSITION>(poses.buffer[i]);
i++;
}
}
// If any positions not filled in then use the last position for them
const XYPOSITION lastPos = (fit > 0) ? positions[fit - 1] : 0.0f;
std::fill(positions+i, positions + len, lastPos);
}
XYPOSITION SurfaceGDI::WidthChar(Font &font_, char ch) {
SetFont(font_);
SIZE sz;
::GetTextExtentPoint32A(hdc, &ch, 1, &sz);
return static_cast<XYPOSITION>(sz.cx);
}
XYPOSITION SurfaceGDI::Ascent(Font &font_) {
SetFont(font_);
TEXTMETRIC tm;
::GetTextMetrics(hdc, &tm);
return static_cast<XYPOSITION>(tm.tmAscent);
}
XYPOSITION SurfaceGDI::Descent(Font &font_) {
SetFont(font_);
TEXTMETRIC tm;
::GetTextMetrics(hdc, &tm);
return static_cast<XYPOSITION>(tm.tmDescent);
}
XYPOSITION SurfaceGDI::InternalLeading(Font &font_) {
SetFont(font_);
TEXTMETRIC tm;
::GetTextMetrics(hdc, &tm);
return static_cast<XYPOSITION>(tm.tmInternalLeading);
}
XYPOSITION SurfaceGDI::ExternalLeading(Font &font_) {
SetFont(font_);
TEXTMETRIC tm;
::GetTextMetrics(hdc, &tm);
return static_cast<XYPOSITION>(tm.tmExternalLeading);
}
XYPOSITION SurfaceGDI::Height(Font &font_) {
SetFont(font_);
TEXTMETRIC tm;
::GetTextMetrics(hdc, &tm);
return static_cast<XYPOSITION>(tm.tmHeight);
}
XYPOSITION SurfaceGDI::AverageCharWidth(Font &font_) {
SetFont(font_);
TEXTMETRIC tm;
::GetTextMetrics(hdc, &tm);
return static_cast<XYPOSITION>(tm.tmAveCharWidth);
}
void SurfaceGDI::SetClip(PRectangle rc) {
::IntersectClipRect(hdc, static_cast<int>(rc.left), static_cast<int>(rc.top),
static_cast<int>(rc.right), static_cast<int>(rc.bottom));
}
void SurfaceGDI::FlushCachedState() {
pen = 0;
brush = 0;
font = 0;
}
void SurfaceGDI::SetUnicodeMode(bool unicodeMode_) {
unicodeMode=unicodeMode_;
}
void SurfaceGDI::SetDBCSMode(int codePage_) {
// No action on window as automatically handled by system.
codePage = codePage_;
}
#if defined(USE_D2D)
class SurfaceD2D : public Surface {
bool unicodeMode;
int x, y;
int codePage;
int codePageText;
ID2D1RenderTarget *pRenderTarget;
bool ownRenderTarget;
int clipsActive;
IDWriteTextFormat *pTextFormat;
FLOAT yAscent;
FLOAT yDescent;
FLOAT yInternalLeading;
ID2D1SolidColorBrush *pBrush;
int logPixelsY;
float dpiScaleX;
float dpiScaleY;
void SetFont(Font &font_);
// Private so SurfaceD2D objects can not be copied
SurfaceD2D(const SurfaceD2D &);
SurfaceD2D &operator=(const SurfaceD2D &);
public:
SurfaceD2D();
virtual ~SurfaceD2D() override;
void SetScale();
void Init(WindowID wid) override;
void Init(SurfaceID sid, WindowID wid) override;
void InitPixMap(int width, int height, Surface *surface_, WindowID wid) override;
void Release() override;
bool Initialised() override;
HRESULT FlushDrawing();
void PenColour(ColourDesired fore) override;
void D2DPenColour(ColourDesired fore, int alpha=255);
int LogPixelsY() override;
int DeviceHeightFont(int points) override;
void MoveTo(int x_, int y_) override;
void LineTo(int x_, int y_) override;
void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back) override;
void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back) override;
void FillRectangle(PRectangle rc, ColourDesired back) override;
void FillRectangle(PRectangle rc, Surface &surfacePattern) override;
void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back) override;
void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
ColourDesired outline, int alphaOutline, int flags) override;
void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) override;
void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) override;
void Copy(PRectangle rc, Point from, Surface &surfaceSource) override;
void DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, UINT fuOptions);
void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back) override;
void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back) override;
void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore) override;
void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) override;
XYPOSITION WidthText(Font &font_, const char *s, int len) override;
XYPOSITION WidthChar(Font &font_, char ch) override;
XYPOSITION Ascent(Font &font_) override;
XYPOSITION Descent(Font &font_) override;
XYPOSITION InternalLeading(Font &font_) override;
XYPOSITION ExternalLeading(Font &font_) override;
XYPOSITION Height(Font &font_) override;
XYPOSITION AverageCharWidth(Font &font_) override;
void SetClip(PRectangle rc) override;
void FlushCachedState() override;
void SetUnicodeMode(bool unicodeMode_) override;
void SetDBCSMode(int codePage_) override;
};
SurfaceD2D::SurfaceD2D() :
unicodeMode(false),
x(0), y(0) {
codePage = 0;
codePageText = 0;
pRenderTarget = NULL;
ownRenderTarget = false;
clipsActive = 0;
// From selected font
pTextFormat = NULL;
yAscent = 2;
yDescent = 1;
yInternalLeading = 0;
pBrush = NULL;
logPixelsY = 72;
dpiScaleX = 1.0;
dpiScaleY = 1.0;
}
SurfaceD2D::~SurfaceD2D() {
Release();
}
void SurfaceD2D::Release() {
if (pBrush) {
pBrush->Release();
pBrush = 0;
}
if (pRenderTarget) {
while (clipsActive) {
pRenderTarget->PopAxisAlignedClip();
clipsActive--;
}
if (ownRenderTarget) {
pRenderTarget->Release();
}
pRenderTarget = 0;
}
}
void SurfaceD2D::SetScale() {
HDC hdcMeasure = ::CreateCompatibleDC(NULL);
logPixelsY = ::GetDeviceCaps(hdcMeasure, LOGPIXELSY);
dpiScaleX = ::GetDeviceCaps(hdcMeasure, LOGPIXELSX) / 96.0f;
dpiScaleY = logPixelsY / 96.0f;
::DeleteDC(hdcMeasure);
}
bool SurfaceD2D::Initialised() {
return pRenderTarget != 0;
}
HRESULT SurfaceD2D::FlushDrawing() {
return pRenderTarget->Flush();
}
void SurfaceD2D::Init(WindowID /* wid */) {
Release();
SetScale();
}
void SurfaceD2D::Init(SurfaceID sid, WindowID) {
Release();
SetScale();
pRenderTarget = static_cast<ID2D1RenderTarget *>(sid);
}
void SurfaceD2D::InitPixMap(int width, int height, Surface *surface_, WindowID) {
Release();
SetScale();
SurfaceD2D *psurfOther = static_cast<SurfaceD2D *>(surface_);
ID2D1BitmapRenderTarget *pCompatibleRenderTarget = NULL;
D2D1_SIZE_F desiredSize = D2D1::SizeF(static_cast<float>(width), static_cast<float>(height));
D2D1_PIXEL_FORMAT desiredFormat;
#ifdef __MINGW32__
desiredFormat.format = DXGI_FORMAT_UNKNOWN;
#else
desiredFormat = psurfOther->pRenderTarget->GetPixelFormat();
#endif
desiredFormat.alphaMode = D2D1_ALPHA_MODE_IGNORE;
HRESULT hr = psurfOther->pRenderTarget->CreateCompatibleRenderTarget(
&desiredSize, NULL, &desiredFormat, D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE, &pCompatibleRenderTarget);
if (SUCCEEDED(hr)) {
pRenderTarget = pCompatibleRenderTarget;
pRenderTarget->BeginDraw();
ownRenderTarget = true;
}
SetUnicodeMode(psurfOther->unicodeMode);
SetDBCSMode(psurfOther->codePage);
}
void SurfaceD2D::PenColour(ColourDesired fore) {
D2DPenColour(fore);
}
void SurfaceD2D::D2DPenColour(ColourDesired fore, int alpha) {
if (pRenderTarget) {
D2D_COLOR_F col;
col.r = (fore.AsLong() & 0xff) / 255.0f;
col.g = ((fore.AsLong() & 0xff00) >> 8) / 255.0f;
col.b = (fore.AsLong() >> 16) / 255.0f;
col.a = alpha / 255.0f;
if (pBrush) {
pBrush->SetColor(col);
} else {
HRESULT hr = pRenderTarget->CreateSolidColorBrush(col, &pBrush);
if (!SUCCEEDED(hr) && pBrush) {
pBrush->Release();
pBrush = 0;
}
}
}
}
void SurfaceD2D::SetFont(Font &font_) {
FormatAndMetrics *pfm = static_cast<FormatAndMetrics *>(font_.GetID());
PLATFORM_ASSERT(pfm->technology == SCWIN_TECH_DIRECTWRITE);
pTextFormat = pfm->pTextFormat;
yAscent = pfm->yAscent;
yDescent = pfm->yDescent;
yInternalLeading = pfm->yInternalLeading;
codePageText = codePage;
if (pfm->characterSet) {
codePageText = CodePageFromCharSet(pfm->characterSet, codePage);
}
if (pRenderTarget) {
D2D1_TEXT_ANTIALIAS_MODE aaMode;
aaMode = DWriteMapFontQuality(pfm->extraFontFlag);
if (aaMode == D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE && customClearTypeRenderingParams)
pRenderTarget->SetTextRenderingParams(customClearTypeRenderingParams);
else if (defaultRenderingParams)
pRenderTarget->SetTextRenderingParams(defaultRenderingParams);
pRenderTarget->SetTextAntialiasMode(aaMode);
}
}
int SurfaceD2D::LogPixelsY() {
return logPixelsY;
}
int SurfaceD2D::DeviceHeightFont(int points) {
return ::MulDiv(points, LogPixelsY(), 72);
}
void SurfaceD2D::MoveTo(int x_, int y_) {
x = x_;
y = y_;
}
static int Delta(int difference) {
if (difference < 0)
return -1;
else if (difference > 0)
return 1;
else
return 0;
}
static float RoundFloat(float f) {
return float(int(f+0.5f));
}
void SurfaceD2D::LineTo(int x_, int y_) {
if (pRenderTarget) {
int xDiff = x_ - x;
int xDelta = Delta(xDiff);
int yDiff = y_ - y;
int yDelta = Delta(yDiff);
if ((xDiff == 0) || (yDiff == 0)) {
// Horizontal or vertical lines can be more precisely drawn as a filled rectangle
int xEnd = x_ - xDelta;
int left = Platform::Minimum(x, xEnd);
int width = abs(x - xEnd) + 1;
int yEnd = y_ - yDelta;
int top = Platform::Minimum(y, yEnd);
int height = abs(y - yEnd) + 1;
D2D1_RECT_F rectangle1 = D2D1::RectF(static_cast<float>(left), static_cast<float>(top),
static_cast<float>(left+width), static_cast<float>(top+height));
pRenderTarget->FillRectangle(&rectangle1, pBrush);
} else if ((abs(xDiff) == abs(yDiff))) {
// 45 degree slope
pRenderTarget->DrawLine(D2D1::Point2F(x + 0.5f, y + 0.5f),
D2D1::Point2F(x_ + 0.5f - xDelta, y_ + 0.5f - yDelta), pBrush);
} else {
// Line has a different slope so difficult to avoid last pixel
pRenderTarget->DrawLine(D2D1::Point2F(x + 0.5f, y + 0.5f),
D2D1::Point2F(x_ + 0.5f, y_ + 0.5f), pBrush);
}
x = x_;
y = y_;
}
}
void SurfaceD2D::Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back) {
if (pRenderTarget) {
ID2D1Factory *pFactory = 0;
pRenderTarget->GetFactory(&pFactory);
ID2D1PathGeometry *geometry=0;
HRESULT hr = pFactory->CreatePathGeometry(&geometry);
if (SUCCEEDED(hr)) {
ID2D1GeometrySink *sink = 0;
hr = geometry->Open(&sink);
if (SUCCEEDED(hr)) {
sink->BeginFigure(D2D1::Point2F(pts[0].x + 0.5f, pts[0].y + 0.5f), D2D1_FIGURE_BEGIN_FILLED);
for (size_t i=1; i<static_cast<size_t>(npts); i++) {
sink->AddLine(D2D1::Point2F(pts[i].x + 0.5f, pts[i].y + 0.5f));
}
sink->EndFigure(D2D1_FIGURE_END_CLOSED);
sink->Close();
sink->Release();
D2DPenColour(back);
pRenderTarget->FillGeometry(geometry,pBrush);
D2DPenColour(fore);
pRenderTarget->DrawGeometry(geometry,pBrush);
}
geometry->Release();
}
}
}
void SurfaceD2D::RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back) {
if (pRenderTarget) {
D2D1_RECT_F rectangle1 = D2D1::RectF(RoundFloat(rc.left) + 0.5f, rc.top+0.5f, RoundFloat(rc.right) - 0.5f, rc.bottom-0.5f);
D2DPenColour(back);
pRenderTarget->FillRectangle(&rectangle1, pBrush);
D2DPenColour(fore);
pRenderTarget->DrawRectangle(&rectangle1, pBrush);
}
}
void SurfaceD2D::FillRectangle(PRectangle rc, ColourDesired back) {
if (pRenderTarget) {
D2DPenColour(back);
D2D1_RECT_F rectangle1 = D2D1::RectF(RoundFloat(rc.left), rc.top, RoundFloat(rc.right), rc.bottom);
pRenderTarget->FillRectangle(&rectangle1, pBrush);
}
}
void SurfaceD2D::FillRectangle(PRectangle rc, Surface &surfacePattern) {
SurfaceD2D &surfOther = static_cast<SurfaceD2D &>(surfacePattern);
surfOther.FlushDrawing();
ID2D1Bitmap *pBitmap = NULL;
ID2D1BitmapRenderTarget *pCompatibleRenderTarget = reinterpret_cast<ID2D1BitmapRenderTarget *>(
surfOther.pRenderTarget);
HRESULT hr = pCompatibleRenderTarget->GetBitmap(&pBitmap);
if (SUCCEEDED(hr)) {
ID2D1BitmapBrush *pBitmapBrush = NULL;
D2D1_BITMAP_BRUSH_PROPERTIES brushProperties =
D2D1::BitmapBrushProperties(D2D1_EXTEND_MODE_WRAP, D2D1_EXTEND_MODE_WRAP,
D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR);
// Create the bitmap brush.
hr = pRenderTarget->CreateBitmapBrush(pBitmap, brushProperties, &pBitmapBrush);
pBitmap->Release();
if (SUCCEEDED(hr)) {
pRenderTarget->FillRectangle(
D2D1::RectF(rc.left, rc.top, rc.right, rc.bottom),
pBitmapBrush);
pBitmapBrush->Release();
}
}
}
void SurfaceD2D::RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back) {
if (pRenderTarget) {
D2D1_ROUNDED_RECT roundedRectFill = {
D2D1::RectF(rc.left+1.0f, rc.top+1.0f, rc.right-1.0f, rc.bottom-1.0f),
4, 4};
D2DPenColour(back);
pRenderTarget->FillRoundedRectangle(roundedRectFill, pBrush);
D2D1_ROUNDED_RECT roundedRect = {
D2D1::RectF(rc.left + 0.5f, rc.top+0.5f, rc.right - 0.5f, rc.bottom-0.5f),
4, 4};
D2DPenColour(fore);
pRenderTarget->DrawRoundedRectangle(roundedRect, pBrush);
}
}
void SurfaceD2D::AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
ColourDesired outline, int alphaOutline, int /* flags*/ ) {
if (pRenderTarget) {
if (cornerSize == 0) {
// When corner size is zero, draw square rectangle to prevent blurry pixels at corners
D2D1_RECT_F rectFill = D2D1::RectF(RoundFloat(rc.left) + 1.0f, rc.top + 1.0f, RoundFloat(rc.right) - 1.0f, rc.bottom - 1.0f);
D2DPenColour(fill, alphaFill);
pRenderTarget->FillRectangle(rectFill, pBrush);
D2D1_RECT_F rectOutline = D2D1::RectF(RoundFloat(rc.left) + 0.5f, rc.top + 0.5f, RoundFloat(rc.right) - 0.5f, rc.bottom - 0.5f);
D2DPenColour(outline, alphaOutline);
pRenderTarget->DrawRectangle(rectOutline, pBrush);
} else {
const float cornerSizeF = static_cast<float>(cornerSize);
D2D1_ROUNDED_RECT roundedRectFill = {
D2D1::RectF(RoundFloat(rc.left) + 1.0f, rc.top + 1.0f, RoundFloat(rc.right) - 1.0f, rc.bottom - 1.0f),
cornerSizeF, cornerSizeF};
D2DPenColour(fill, alphaFill);
pRenderTarget->FillRoundedRectangle(roundedRectFill, pBrush);
D2D1_ROUNDED_RECT roundedRect = {
D2D1::RectF(RoundFloat(rc.left) + 0.5f, rc.top + 0.5f, RoundFloat(rc.right) - 0.5f, rc.bottom - 0.5f),
cornerSizeF, cornerSizeF};
D2DPenColour(outline, alphaOutline);
pRenderTarget->DrawRoundedRectangle(roundedRect, pBrush);
}
}
}
void SurfaceD2D::DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) {
if (pRenderTarget) {
if (rc.Width() > width)
rc.left += static_cast<int>((rc.Width() - width) / 2);
rc.right = rc.left + width;
if (rc.Height() > height)
rc.top += static_cast<int>((rc.Height() - height) / 2);
rc.bottom = rc.top + height;
std::vector<unsigned char> image(height * width * 4);
for (int yPixel=0; yPixel<height; yPixel++) {
for (int xPixel = 0; xPixel<width; xPixel++) {
unsigned char *pixel = &image[0] + (yPixel*width + xPixel) * 4;
unsigned char alpha = pixelsImage[3];
// Input is RGBA, output is BGRA with premultiplied alpha
pixel[2] = (*pixelsImage++) * alpha / 255;
pixel[1] = (*pixelsImage++) * alpha / 255;
pixel[0] = (*pixelsImage++) * alpha / 255;
pixel[3] = *pixelsImage++;
}
}
ID2D1Bitmap *bitmap = 0;
D2D1_SIZE_U size = D2D1::SizeU(width, height);
D2D1_BITMAP_PROPERTIES props = {{DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED}, 72.0, 72.0};
HRESULT hr = pRenderTarget->CreateBitmap(size, &image[0],
width * 4, &props, &bitmap);
if (SUCCEEDED(hr)) {
D2D1_RECT_F rcDestination = {rc.left, rc.top, rc.right, rc.bottom};
pRenderTarget->DrawBitmap(bitmap, rcDestination);
bitmap->Release();
}
}
}
void SurfaceD2D::Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) {
if (pRenderTarget) {
FLOAT radius = rc.Width() / 2.0f;
D2D1_ELLIPSE ellipse = {
D2D1::Point2F((rc.left + rc.right) / 2.0f, (rc.top + rc.bottom) / 2.0f),
radius,radius};
PenColour(back);
pRenderTarget->FillEllipse(ellipse, pBrush);
PenColour(fore);
pRenderTarget->DrawEllipse(ellipse, pBrush);
}
}
void SurfaceD2D::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
SurfaceD2D &surfOther = static_cast<SurfaceD2D &>(surfaceSource);
surfOther.FlushDrawing();
ID2D1BitmapRenderTarget *pCompatibleRenderTarget = reinterpret_cast<ID2D1BitmapRenderTarget *>(
surfOther.pRenderTarget);
ID2D1Bitmap *pBitmap = NULL;
HRESULT hr = pCompatibleRenderTarget->GetBitmap(&pBitmap);
if (SUCCEEDED(hr)) {
D2D1_RECT_F rcDestination = {rc.left, rc.top, rc.right, rc.bottom};
D2D1_RECT_F rcSource = {from.x, from.y, from.x + rc.Width(), from.y + rc.Height()};
pRenderTarget->DrawBitmap(pBitmap, rcDestination, 1.0f,
D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, rcSource);
hr = pRenderTarget->Flush();
if (FAILED(hr)) {
Platform::DebugPrintf("Failed Flush 0x%x\n", hr);
}
pBitmap->Release();
}
}
void SurfaceD2D::DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, UINT fuOptions) {
SetFont(font_);
// Use Unicode calls
const TextWide tbuf(s, len, unicodeMode, codePageText);
if (pRenderTarget && pTextFormat && pBrush) {
if (fuOptions & ETO_CLIPPED) {
D2D1_RECT_F rcClip = {rc.left, rc.top, rc.right, rc.bottom};
pRenderTarget->PushAxisAlignedClip(rcClip, D2D1_ANTIALIAS_MODE_ALIASED);
}
// Explicitly creating a text layout appears a little faster
IDWriteTextLayout *pTextLayout;
HRESULT hr = pIDWriteFactory->CreateTextLayout(tbuf.buffer, tbuf.tlen, pTextFormat,
rc.Width(), rc.Height(), &pTextLayout);
if (SUCCEEDED(hr)) {
D2D1_POINT_2F origin = {rc.left, ybase-yAscent};
pRenderTarget->DrawTextLayout(origin, pTextLayout, pBrush, D2D1_DRAW_TEXT_OPTIONS_NONE);
pTextLayout->Release();
}
if (fuOptions & ETO_CLIPPED) {
pRenderTarget->PopAxisAlignedClip();
}
}
}
void SurfaceD2D::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
ColourDesired fore, ColourDesired back) {
if (pRenderTarget) {
FillRectangle(rc, back);
D2DPenColour(fore);
DrawTextCommon(rc, font_, ybase, s, len, ETO_OPAQUE);
}
}
void SurfaceD2D::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
ColourDesired fore, ColourDesired back) {
if (pRenderTarget) {
FillRectangle(rc, back);
D2DPenColour(fore);
DrawTextCommon(rc, font_, ybase, s, len, ETO_OPAQUE | ETO_CLIPPED);
}
}
void SurfaceD2D::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
ColourDesired fore) {
// Avoid drawing spaces in transparent mode
for (int i=0; i<len; i++) {
if (s[i] != ' ') {
if (pRenderTarget) {
D2DPenColour(fore);
DrawTextCommon(rc, font_, ybase, s, len, 0);
}
return;
}
}
}
XYPOSITION SurfaceD2D::WidthText(Font &font_, const char *s, int len) {
FLOAT width = 1.0;
SetFont(font_);
const TextWide tbuf(s, len, unicodeMode, codePageText);
if (pIDWriteFactory && pTextFormat) {
// Create a layout
IDWriteTextLayout *pTextLayout = 0;
HRESULT hr = pIDWriteFactory->CreateTextLayout(tbuf.buffer, tbuf.tlen, pTextFormat, 1000.0, 1000.0, &pTextLayout);
if (SUCCEEDED(hr)) {
DWRITE_TEXT_METRICS textMetrics;
if (SUCCEEDED(pTextLayout->GetMetrics(&textMetrics)))
width = textMetrics.widthIncludingTrailingWhitespace;
pTextLayout->Release();
}
}
return width;
}
void SurfaceD2D::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) {
SetFont(font_);
int fit = 0;
const TextWide tbuf(s, len, unicodeMode, codePageText);
TextPositions poses(tbuf.tlen);
fit = tbuf.tlen;
const int clusters = 1000;
DWRITE_CLUSTER_METRICS clusterMetrics[clusters];
UINT32 count = 0;
if (pIDWriteFactory && pTextFormat) {
SetFont(font_);
// Create a layout
IDWriteTextLayout *pTextLayout = 0;
HRESULT hr = pIDWriteFactory->CreateTextLayout(tbuf.buffer, tbuf.tlen, pTextFormat, 10000.0, 1000.0, &pTextLayout);
if (!SUCCEEDED(hr))
return;
if (!SUCCEEDED(pTextLayout->GetClusterMetrics(clusterMetrics, clusters, &count)))
return;
// A cluster may be more than one WCHAR, such as for "ffi" which is a ligature in the Candara font
FLOAT position = 0.0f;
size_t ti=0;
for (size_t ci=0; ci<count; ci++) {
for (size_t inCluster=0; inCluster<clusterMetrics[ci].length; inCluster++) {
poses.buffer[ti++] = position + clusterMetrics[ci].width * (inCluster + 1) / clusterMetrics[ci].length;
}
position += clusterMetrics[ci].width;
}
PLATFORM_ASSERT(ti == static_cast<size_t>(tbuf.tlen));
pTextLayout->Release();
}
if (unicodeMode) {
// Map the widths given for UTF-16 characters back onto the UTF-8 input string
int ui=0;
const unsigned char *us = reinterpret_cast<const unsigned char *>(s);
int i=0;
while (ui<fit) {
unsigned char uch = us[i];
unsigned int lenChar = 1;
if (uch >= (0x80 + 0x40 + 0x20 + 0x10)) {
lenChar = 4;
ui++;
} else if (uch >= (0x80 + 0x40 + 0x20)) {
lenChar = 3;
} else if (uch >= (0x80)) {
lenChar = 2;
}
for (unsigned int bytePos=0; (bytePos<lenChar) && (i<len); bytePos++) {
positions[i++] = poses.buffer[ui];
}
ui++;
}
XYPOSITION lastPos = 0.0f;
if (i > 0)
lastPos = positions[i-1];
while (i<len) {
positions[i++] = lastPos;
}
} else if (codePageText == 0) {
// One character per position
PLATFORM_ASSERT(len == tbuf.tlen);
for (size_t kk=0; kk<static_cast<size_t>(len); kk++) {
positions[kk] = poses.buffer[kk];
}
} else {
// May be more than one byte per position
unsigned int ui = 0;
FLOAT position = 0.0f;
for (int i=0; i<len;) {
if (ui < count)
position = poses.buffer[ui];
if (Platform::IsDBCSLeadByte(codePageText, s[i])) {
positions[i] = position;
positions[i+1] = position;
i += 2;
} else {
positions[i] = position;
i++;
}
ui++;
}
}
}
XYPOSITION SurfaceD2D::WidthChar(Font &font_, char ch) {
FLOAT width = 1.0;
SetFont(font_);
if (pIDWriteFactory && pTextFormat) {
// Create a layout
IDWriteTextLayout *pTextLayout = 0;
const WCHAR wch = ch;
HRESULT hr = pIDWriteFactory->CreateTextLayout(&wch, 1, pTextFormat, 1000.0, 1000.0, &pTextLayout);
if (SUCCEEDED(hr)) {
DWRITE_TEXT_METRICS textMetrics;
if (SUCCEEDED(pTextLayout->GetMetrics(&textMetrics)))
width = textMetrics.widthIncludingTrailingWhitespace;
pTextLayout->Release();
}
}
return width;
}
XYPOSITION SurfaceD2D::Ascent(Font &font_) {
SetFont(font_);
return ceil(yAscent);
}
XYPOSITION SurfaceD2D::Descent(Font &font_) {
SetFont(font_);
return ceil(yDescent);
}
XYPOSITION SurfaceD2D::InternalLeading(Font &font_) {
SetFont(font_);
return floor(yInternalLeading);
}
XYPOSITION SurfaceD2D::ExternalLeading(Font &) {
// Not implemented, always return one
return 1;
}
XYPOSITION SurfaceD2D::Height(Font &font_) {
return Ascent(font_) + Descent(font_);
}
XYPOSITION SurfaceD2D::AverageCharWidth(Font &font_) {
FLOAT width = 1.0;
SetFont(font_);
if (pIDWriteFactory && pTextFormat) {
// Create a layout
IDWriteTextLayout *pTextLayout = 0;
const WCHAR wszAllAlpha[] = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const size_t lenAllAlpha = wcslen(wszAllAlpha);
HRESULT hr = pIDWriteFactory->CreateTextLayout(wszAllAlpha, static_cast<UINT32>(lenAllAlpha),
pTextFormat, 1000.0, 1000.0, &pTextLayout);
if (SUCCEEDED(hr)) {
DWRITE_TEXT_METRICS textMetrics;
if (SUCCEEDED(pTextLayout->GetMetrics(&textMetrics)))
width = textMetrics.width / lenAllAlpha;
pTextLayout->Release();
}
}
return width;
}
void SurfaceD2D::SetClip(PRectangle rc) {
if (pRenderTarget) {
D2D1_RECT_F rcClip = {rc.left, rc.top, rc.right, rc.bottom};
pRenderTarget->PushAxisAlignedClip(rcClip, D2D1_ANTIALIAS_MODE_ALIASED);
clipsActive++;
}
}
void SurfaceD2D::FlushCachedState() {
}
void SurfaceD2D::SetUnicodeMode(bool unicodeMode_) {
unicodeMode=unicodeMode_;
}
void SurfaceD2D::SetDBCSMode(int codePage_) {
// No action on window as automatically handled by system.
codePage = codePage_;
}
#endif
Surface *Surface::Allocate(int technology) {
#if defined(USE_D2D)
if (technology == SCWIN_TECH_GDI)
return new SurfaceGDI;
else
return new SurfaceD2D;
#else
return new SurfaceGDI;
#endif
}
Window::~Window() {
}
void Window::Destroy() {
if (wid)
::DestroyWindow(static_cast<HWND>(wid));
wid = 0;
}
bool Window::HasFocus() {
return ::GetFocus() == wid;
}
PRectangle Window::GetPosition() {
RECT rc;
::GetWindowRect(static_cast<HWND>(wid), &rc);
return PRectangle::FromInts(rc.left, rc.top, rc.right, rc.bottom);
}
void Window::SetPosition(PRectangle rc) {
::SetWindowPos(static_cast<HWND>(wid),
0, static_cast<int>(rc.left), static_cast<int>(rc.top),
static_cast<int>(rc.Width()), static_cast<int>(rc.Height()), SWP_NOZORDER | SWP_NOACTIVATE);
}
static RECT RectFromMonitor(HMONITOR hMonitor) {
MONITORINFO mi = {0};
mi.cbSize = sizeof(mi);
if (GetMonitorInfo(hMonitor, &mi)) {
return mi.rcWork;
}
RECT rc = {0, 0, 0, 0};
if (::SystemParametersInfoA(SPI_GETWORKAREA, 0, &rc, 0) == 0) {
rc.left = 0;
rc.top = 0;
rc.right = 0;
rc.bottom = 0;
}
return rc;
}
void Window::SetPositionRelative(PRectangle rc, Window w) {
LONG style = ::GetWindowLong(static_cast<HWND>(wid), GWL_STYLE);
if (style & WS_POPUP) {
POINT ptOther = {0, 0};
::ClientToScreen(static_cast<HWND>(w.GetID()), &ptOther);
rc.Move(static_cast<XYPOSITION>(ptOther.x), static_cast<XYPOSITION>(ptOther.y));
RECT rcMonitor = RectFromPRectangle(rc);
HMONITOR hMonitor = MonitorFromRect(&rcMonitor, MONITOR_DEFAULTTONEAREST);
// If hMonitor is NULL, that's just the main screen anyways.
RECT rcWork = RectFromMonitor(hMonitor);
if (rcWork.left < rcWork.right) {
// Now clamp our desired rectangle to fit inside the work area
// This way, the menu will fit wholly on one screen. An improvement even
// if you don't have a second monitor on the left... Menu's appears half on
// one screen and half on the other are just U.G.L.Y.!
if (rc.right > rcWork.right)
rc.Move(rcWork.right - rc.right, 0);
if (rc.bottom > rcWork.bottom)
rc.Move(0, rcWork.bottom - rc.bottom);
if (rc.left < rcWork.left)
rc.Move(rcWork.left - rc.left, 0);
if (rc.top < rcWork.top)
rc.Move(0, rcWork.top - rc.top);
}
}
SetPosition(rc);
}
PRectangle Window::GetClientPosition() {
RECT rc={0,0,0,0};
if (wid)
::GetClientRect(static_cast<HWND>(wid), &rc);
return PRectangle::FromInts(rc.left, rc.top, rc.right, rc.bottom);
}
void Window::Show(bool show) {
if (show)
::ShowWindow(static_cast<HWND>(wid), SW_SHOWNOACTIVATE);
else
::ShowWindow(static_cast<HWND>(wid), SW_HIDE);
}
void Window::InvalidateAll() {
::InvalidateRect(static_cast<HWND>(wid), NULL, FALSE);
}
void Window::InvalidateRectangle(PRectangle rc) {
RECT rcw = RectFromPRectangle(rc);
::InvalidateRect(static_cast<HWND>(wid), &rcw, FALSE);
}
static LRESULT Window_SendMessage(Window *w, UINT msg, WPARAM wParam=0, LPARAM lParam=0) {
return ::SendMessage(static_cast<HWND>(w->GetID()), msg, wParam, lParam);
}
void Window::SetFont(Font &font) {
Window_SendMessage(this, WM_SETFONT,
reinterpret_cast<WPARAM>(font.GetID()), 0);
}
static void FlipBitmap(HBITMAP bitmap, int width, int height) {
HDC hdc = ::CreateCompatibleDC(NULL);
if (hdc != NULL) {
HGDIOBJ prevBmp = ::SelectObject(hdc, bitmap);
::StretchBlt(hdc, width - 1, 0, -width, height, hdc, 0, 0, width, height, SRCCOPY);
::SelectObject(hdc, prevBmp);
::DeleteDC(hdc);
}
}
static HCURSOR GetReverseArrowCursor() {
if (reverseArrowCursor != NULL)
return reverseArrowCursor;
::EnterCriticalSection(&crPlatformLock);
HCURSOR cursor = reverseArrowCursor;
if (cursor == NULL) {
cursor = ::LoadCursor(NULL, IDC_ARROW);
ICONINFO info;
if (::GetIconInfo(cursor, &info)) {
BITMAP bmp;
if (::GetObject(info.hbmMask, sizeof(bmp), &bmp)) {
FlipBitmap(info.hbmMask, bmp.bmWidth, bmp.bmHeight);
if (info.hbmColor != NULL)
FlipBitmap(info.hbmColor, bmp.bmWidth, bmp.bmHeight);
info.xHotspot = (DWORD)bmp.bmWidth - 1 - info.xHotspot;
reverseArrowCursor = ::CreateIconIndirect(&info);
if (reverseArrowCursor != NULL)
cursor = reverseArrowCursor;
}
::DeleteObject(info.hbmMask);
if (info.hbmColor != NULL)
::DeleteObject(info.hbmColor);
}
}
::LeaveCriticalSection(&crPlatformLock);
return cursor;
}
void Window::SetCursor(Cursor curs) {
switch (curs) {
case cursorText:
::SetCursor(::LoadCursor(NULL,IDC_IBEAM));
break;
case cursorUp:
::SetCursor(::LoadCursor(NULL,IDC_UPARROW));
break;
case cursorWait:
::SetCursor(::LoadCursor(NULL,IDC_WAIT));
break;
case cursorHoriz:
::SetCursor(::LoadCursor(NULL,IDC_SIZEWE));
break;
case cursorVert:
::SetCursor(::LoadCursor(NULL,IDC_SIZENS));
break;
case cursorHand:
::SetCursor(::LoadCursor(NULL,IDC_HAND));
break;
case cursorReverseArrow:
::SetCursor(GetReverseArrowCursor());
break;
case cursorArrow:
case cursorInvalid: // Should not occur, but just in case.
::SetCursor(::LoadCursor(NULL,IDC_ARROW));
break;
}
}
void Window::SetTitle(const char *s) {
::SetWindowTextA(static_cast<HWND>(wid), s);
}
/* Returns rectangle of monitor pt is on, both rect and pt are in Window's
coordinates */
PRectangle Window::GetMonitorRect(Point pt) {
PRectangle rcPosition = GetPosition();
POINT ptDesktop = {static_cast<LONG>(pt.x + rcPosition.left),
static_cast<LONG>(pt.y + rcPosition.top)};
HMONITOR hMonitor = MonitorFromPoint(ptDesktop, MONITOR_DEFAULTTONEAREST);
RECT rcWork = RectFromMonitor(hMonitor);
if (rcWork.left < rcWork.right) {
PRectangle rcMonitor(
rcWork.left - rcPosition.left,
rcWork.top - rcPosition.top,
rcWork.right - rcPosition.left,
rcWork.bottom - rcPosition.top);
return rcMonitor;
} else {
return PRectangle();
}
}
struct ListItemData {
const char *text;
int pixId;
};
class LineToItem {
std::vector<char> words;
std::vector<ListItemData> data;
public:
LineToItem() {
}
~LineToItem() {
Clear();
}
void Clear() {
words.clear();
data.clear();
}
ListItemData Get(int index) const {
if (index >= 0 && index < static_cast<int>(data.size())) {
return data[index];
} else {
ListItemData missing = {"", -1};
return missing;
}
}
int Count() const {
return static_cast<int>(data.size());
}
void AllocItem(const char *text, int pixId) {
ListItemData lid = { text, pixId };
data.push_back(lid);
}
char *SetWords(const char *s) {
words = std::vector<char>(s, s+strlen(s)+1);
return &words[0];
}
};
const TCHAR ListBoxX_ClassName[] = TEXT("ListBoxX");
ListBox::ListBox() {
}
ListBox::~ListBox() {
}
class ListBoxX : public ListBox {
int lineHeight;
FontID fontCopy;
int technology;
RGBAImageSet images;
LineToItem lti;
HWND lb;
bool unicodeMode;
int desiredVisibleRows;
unsigned int maxItemCharacters;
unsigned int aveCharWidth;
Window *parent;
int ctrlID;
CallBackAction doubleClickAction;
void *doubleClickActionData;
const char *widestItem;
unsigned int maxCharWidth;
int resizeHit;
PRectangle rcPreSize;
Point dragOffset;
Point location; // Caret location at which the list is opened
int wheelDelta; // mouse wheel residue
HWND GetHWND() const;
void AppendListItem(const char *text, const char *numword);
static void AdjustWindowRect(PRectangle *rc);
int ItemHeight() const;
int MinClientWidth() const;
int TextOffset() const;
POINT GetClientExtent() const;
POINT MinTrackSize() const;
POINT MaxTrackSize() const;
void SetRedraw(bool on);
void OnDoubleClick();
void ResizeToCursor();
void StartResize(WPARAM);
LRESULT NcHitTest(WPARAM, LPARAM) const;
void CentreItem(int n);
void Paint(HDC);
static LRESULT PASCAL ControlWndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam);
static const Point ItemInset; // Padding around whole item
static const Point TextInset; // Padding around text
static const Point ImageInset; // Padding around image
public:
ListBoxX() : lineHeight(10), fontCopy(0), technology(0), lb(0), unicodeMode(false),
desiredVisibleRows(5), maxItemCharacters(0), aveCharWidth(8),
parent(NULL), ctrlID(0), doubleClickAction(NULL), doubleClickActionData(NULL),
widestItem(NULL), maxCharWidth(1), resizeHit(0), wheelDelta(0) {
}
~ListBoxX() override {
if (fontCopy) {
::DeleteObject(fontCopy);
fontCopy = 0;
}
}
virtual void SetFont(Font &font) override;
virtual void Create(Window &parent_, int ctrlID_, Point location_, int lineHeight_, bool unicodeMode_, int technology_) override;
virtual void SetAverageCharWidth(int width) override;
virtual void SetVisibleRows(int rows) override;
virtual int GetVisibleRows() const override;
virtual PRectangle GetDesiredRect() override;
virtual int CaretFromEdge() override;
virtual void Clear() override;
virtual void Append(char *s, int type = -1) override;
virtual int Length() override;
virtual void Select(int n) override;
virtual int GetSelection() override;
virtual int Find(const char *prefix) override;
virtual void GetValue(int n, char *value, int len) override;
virtual void RegisterImage(int type, const char *xpm_data) override;
virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) override;
virtual void ClearRegisteredImages() override;
virtual void SetDoubleClickAction(CallBackAction action, void *data) override {
doubleClickAction = action;
doubleClickActionData = data;
}
virtual void SetList(const char *list, char separator, char typesep) override;
void Draw(DRAWITEMSTRUCT *pDrawItem);
LRESULT WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam);
static LRESULT PASCAL StaticWndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam);
};
const Point ListBoxX::ItemInset(0, 0);
const Point ListBoxX::TextInset(2, 0);
const Point ListBoxX::ImageInset(1, 0);
ListBox *ListBox::Allocate() {
ListBoxX *lb = new ListBoxX();
return lb;
}
void ListBoxX::Create(Window &parent_, int ctrlID_, Point location_, int lineHeight_, bool unicodeMode_, int technology_) {
parent = &parent_;
ctrlID = ctrlID_;
location = location_;
lineHeight = lineHeight_;
unicodeMode = unicodeMode_;
technology = technology_;
HWND hwndParent = static_cast<HWND>(parent->GetID());
HINSTANCE hinstanceParent = GetWindowInstance(hwndParent);
// Window created as popup so not clipped within parent client area
wid = ::CreateWindowEx(
WS_EX_WINDOWEDGE, ListBoxX_ClassName, TEXT(""),
WS_POPUP | WS_THICKFRAME,
100,100, 150,80, hwndParent,
NULL,
hinstanceParent,
this);
POINT locationw = {static_cast<LONG>(location.x), static_cast<LONG>(location.y)};
::MapWindowPoints(hwndParent, NULL, &locationw, 1);
location = Point::FromInts(locationw.x, locationw.y);
}
void ListBoxX::SetFont(Font &font) {
if (font.GetID()) {
if (fontCopy) {
::DeleteObject(fontCopy);
fontCopy = 0;
}
FormatAndMetrics *pfm = static_cast<FormatAndMetrics *>(font.GetID());
fontCopy = pfm->HFont();
::SendMessage(lb, WM_SETFONT, reinterpret_cast<WPARAM>(fontCopy), 0);
}
}
void ListBoxX::SetAverageCharWidth(int width) {
aveCharWidth = width;
}
void ListBoxX::SetVisibleRows(int rows) {
desiredVisibleRows = rows;
}
int ListBoxX::GetVisibleRows() const {
return desiredVisibleRows;
}
HWND ListBoxX::GetHWND() const {
return static_cast<HWND>(GetID());
}
PRectangle ListBoxX::GetDesiredRect() {
PRectangle rcDesired = GetPosition();
int rows = Length();
if ((rows == 0) || (rows > desiredVisibleRows))
rows = desiredVisibleRows;
rcDesired.bottom = rcDesired.top + ItemHeight() * rows;
int width = MinClientWidth();
HDC hdc = ::GetDC(lb);
HFONT oldFont = SelectFont(hdc, fontCopy);
SIZE textSize = {0, 0};
int len = 0;
if (widestItem) {
len = static_cast<int>(strlen(widestItem));
if (unicodeMode) {
const TextWide tbuf(widestItem, len, unicodeMode);
::GetTextExtentPoint32W(hdc, tbuf.buffer, tbuf.tlen, &textSize);
} else {
::GetTextExtentPoint32A(hdc, widestItem, len, &textSize);
}
}
TEXTMETRIC tm;
::GetTextMetrics(hdc, &tm);
maxCharWidth = tm.tmMaxCharWidth;
SelectFont(hdc, oldFont);
::ReleaseDC(lb, hdc);
int widthDesired = Platform::Maximum(textSize.cx, (len + 1) * tm.tmAveCharWidth);
if (width < widthDesired)
width = widthDesired;
rcDesired.right = rcDesired.left + TextOffset() + width + (TextInset.x * 2);
if (Length() > rows)
rcDesired.right += ::GetSystemMetrics(SM_CXVSCROLL);
AdjustWindowRect(&rcDesired);
return rcDesired;
}
int ListBoxX::TextOffset() const {
int pixWidth = images.GetWidth();
return static_cast<int>(pixWidth == 0 ? ItemInset.x : ItemInset.x + pixWidth + (ImageInset.x * 2));
}
int ListBoxX::CaretFromEdge() {
PRectangle rc;
AdjustWindowRect(&rc);
return TextOffset() + static_cast<int>(TextInset.x + (0 - rc.left) - 1);
}
void ListBoxX::Clear() {
::SendMessage(lb, LB_RESETCONTENT, 0, 0);
maxItemCharacters = 0;
widestItem = NULL;
lti.Clear();
}
void ListBoxX::Append(char *, int) {
// This method is no longer called in Scintilla
PLATFORM_ASSERT(false);
}
int ListBoxX::Length() {
return lti.Count();
}
void ListBoxX::Select(int n) {
// We are going to scroll to centre on the new selection and then select it, so disable
// redraw to avoid flicker caused by a painting new selection twice in unselected and then
// selected states
SetRedraw(false);
CentreItem(n);
::SendMessage(lb, LB_SETCURSEL, n, 0);
SetRedraw(true);
}
int ListBoxX::GetSelection() {
return static_cast<int>(::SendMessage(lb, LB_GETCURSEL, 0, 0));
}
// This is not actually called at present
int ListBoxX::Find(const char *) {
return LB_ERR;
}
void ListBoxX::GetValue(int n, char *value, int len) {
ListItemData item = lti.Get(n);
strncpy(value, item.text, len);
value[len-1] = '\0';
}
void ListBoxX::RegisterImage(int type, const char *xpm_data) {
XPM xpmImage(xpm_data);
images.Add(type, new RGBAImage(xpmImage));
}
void ListBoxX::RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) {
images.Add(type, new RGBAImage(width, height, 1.0, pixelsImage));
}
void ListBoxX::ClearRegisteredImages() {
images.Clear();
}
void ListBoxX::Draw(DRAWITEMSTRUCT *pDrawItem) {
if ((pDrawItem->itemAction == ODA_SELECT) || (pDrawItem->itemAction == ODA_DRAWENTIRE)) {
RECT rcBox = pDrawItem->rcItem;
rcBox.left += TextOffset();
if (pDrawItem->itemState & ODS_SELECTED) {
RECT rcImage = pDrawItem->rcItem;
rcImage.right = rcBox.left;
// The image is not highlighted
::FillRect(pDrawItem->hDC, &rcImage, reinterpret_cast<HBRUSH>(COLOR_WINDOW+1));
::FillRect(pDrawItem->hDC, &rcBox, reinterpret_cast<HBRUSH>(COLOR_HIGHLIGHT+1));
::SetBkColor(pDrawItem->hDC, ::GetSysColor(COLOR_HIGHLIGHT));
::SetTextColor(pDrawItem->hDC, ::GetSysColor(COLOR_HIGHLIGHTTEXT));
} else {
::FillRect(pDrawItem->hDC, &pDrawItem->rcItem, reinterpret_cast<HBRUSH>(COLOR_WINDOW+1));
::SetBkColor(pDrawItem->hDC, ::GetSysColor(COLOR_WINDOW));
::SetTextColor(pDrawItem->hDC, ::GetSysColor(COLOR_WINDOWTEXT));
}
ListItemData item = lti.Get(pDrawItem->itemID);
int pixId = item.pixId;
const char *text = item.text;
int len = static_cast<int>(strlen(text));
RECT rcText = rcBox;
::InsetRect(&rcText, static_cast<int>(TextInset.x), static_cast<int>(TextInset.y));
if (unicodeMode) {
const TextWide tbuf(text, len, unicodeMode);
::DrawTextW(pDrawItem->hDC, tbuf.buffer, tbuf.tlen, &rcText, DT_NOPREFIX|DT_END_ELLIPSIS|DT_SINGLELINE|DT_NOCLIP);
} else {
::DrawTextA(pDrawItem->hDC, text, len, &rcText, DT_NOPREFIX|DT_END_ELLIPSIS|DT_SINGLELINE|DT_NOCLIP);
}
if (pDrawItem->itemState & ODS_SELECTED) {
::DrawFocusRect(pDrawItem->hDC, &rcBox);
}
// Draw the image, if any
RGBAImage *pimage = images.Get(pixId);
if (pimage) {
Surface *surfaceItem = Surface::Allocate(technology);
if (surfaceItem) {
if (technology == SCWIN_TECH_GDI) {
surfaceItem->Init(pDrawItem->hDC, pDrawItem->hwndItem);
long left = pDrawItem->rcItem.left + static_cast<int>(ItemInset.x + ImageInset.x);
PRectangle rcImage = PRectangle::FromInts(left, pDrawItem->rcItem.top,
left + images.GetWidth(), pDrawItem->rcItem.bottom);
surfaceItem->DrawRGBAImage(rcImage,
pimage->GetWidth(), pimage->GetHeight(), pimage->Pixels());
delete surfaceItem;
::SetTextAlign(pDrawItem->hDC, TA_TOP);
} else {
#if defined(USE_D2D)
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_IGNORE),
0,
0,
D2D1_RENDER_TARGET_USAGE_NONE,
D2D1_FEATURE_LEVEL_DEFAULT
);
ID2D1DCRenderTarget *pDCRT = 0;
HRESULT hr = pD2DFactory->CreateDCRenderTarget(&props, &pDCRT);
if (SUCCEEDED(hr)) {
RECT rcWindow;
GetClientRect(pDrawItem->hwndItem, &rcWindow);
hr = pDCRT->BindDC(pDrawItem->hDC, &rcWindow);
if (SUCCEEDED(hr)) {
surfaceItem->Init(pDCRT, pDrawItem->hwndItem);
pDCRT->BeginDraw();
long left = pDrawItem->rcItem.left + static_cast<long>(ItemInset.x + ImageInset.x);
PRectangle rcImage = PRectangle::FromInts(left, pDrawItem->rcItem.top,
left + images.GetWidth(), pDrawItem->rcItem.bottom);
surfaceItem->DrawRGBAImage(rcImage,
pimage->GetWidth(), pimage->GetHeight(), pimage->Pixels());
delete surfaceItem;
pDCRT->EndDraw();
pDCRT->Release();
} else {
delete surfaceItem;
}
} else {
delete surfaceItem;
}
#endif
}
}
}
}
}
void ListBoxX::AppendListItem(const char *text, const char *numword) {
int pixId = -1;
if (numword) {
pixId = 0;
char ch;
while ((ch = *++numword) != '\0') {
pixId = 10 * pixId + (ch - '0');
}
}
lti.AllocItem(text, pixId);
unsigned int len = static_cast<unsigned int>(strlen(text));
if (maxItemCharacters < len) {
maxItemCharacters = len;
widestItem = text;
}
}
void ListBoxX::SetList(const char *list, char separator, char typesep) {
// Turn off redraw while populating the list - this has a significant effect, even if
// the listbox is not visible.
SetRedraw(false);
Clear();
size_t size = strlen(list);
char *words = lti.SetWords(list);
char *startword = words;
char *numword = NULL;
for (size_t i=0; i < size; i++) {
if (words[i] == separator) {
words[i] = '\0';
if (numword)
*numword = '\0';
AppendListItem(startword, numword);
startword = words + i + 1;
numword = NULL;
} else if (words[i] == typesep) {
numword = words + i;
}
}
if (startword) {
if (numword)
*numword = '\0';
AppendListItem(startword, numword);
}
// Finally populate the listbox itself with the correct number of items
int count = lti.Count();
::SendMessage(lb, LB_INITSTORAGE, count, 0);
for (int j=0; j<count; j++) {
::SendMessage(lb, LB_ADDSTRING, 0, j+1);
}
SetRedraw(true);
}
void ListBoxX::AdjustWindowRect(PRectangle *rc) {
RECT rcw = RectFromPRectangle(*rc);
::AdjustWindowRectEx(&rcw, WS_THICKFRAME, false, WS_EX_WINDOWEDGE);
*rc = PRectangle::FromInts(rcw.left, rcw.top, rcw.right, rcw.bottom);
}
int ListBoxX::ItemHeight() const {
int itemHeight = lineHeight + (static_cast<int>(TextInset.y) * 2);
int pixHeight = images.GetHeight() + (static_cast<int>(ImageInset.y) * 2);
if (itemHeight < pixHeight) {
itemHeight = pixHeight;
}
return itemHeight;
}
int ListBoxX::MinClientWidth() const {
return 12 * (aveCharWidth+aveCharWidth/3);
}
POINT ListBoxX::MinTrackSize() const {
PRectangle rc = PRectangle::FromInts(0, 0, MinClientWidth(), ItemHeight());
AdjustWindowRect(&rc);
POINT ret = {static_cast<LONG>(rc.Width()), static_cast<LONG>(rc.Height())};
return ret;
}
POINT ListBoxX::MaxTrackSize() const {
PRectangle rc = PRectangle::FromInts(0, 0,
Platform::Maximum(MinClientWidth(),
maxCharWidth * maxItemCharacters + static_cast<int>(TextInset.x) * 2 +
TextOffset() + ::GetSystemMetrics(SM_CXVSCROLL)),
ItemHeight() * lti.Count());
AdjustWindowRect(&rc);
POINT ret = {static_cast<LONG>(rc.Width()), static_cast<LONG>(rc.Height())};
return ret;
}
void ListBoxX::SetRedraw(bool on) {
::SendMessage(lb, WM_SETREDRAW, static_cast<BOOL>(on), 0);
if (on)
::InvalidateRect(lb, NULL, TRUE);
}
static XYPOSITION XYMinimum(XYPOSITION a, XYPOSITION b) {
if (a < b)
return a;
else
return b;
}
static XYPOSITION XYMaximum(XYPOSITION a, XYPOSITION b) {
if (a > b)
return a;
else
return b;
}
void ListBoxX::ResizeToCursor() {
PRectangle rc = GetPosition();
POINT ptw;
::GetCursorPos(&ptw);
Point pt = Point::FromInts(ptw.x, ptw.y);
pt.x += dragOffset.x;
pt.y += dragOffset.y;
switch (resizeHit) {
case HTLEFT:
rc.left = pt.x;
break;
case HTRIGHT:
rc.right = pt.x;
break;
case HTTOP:
rc.top = pt.y;
break;
case HTTOPLEFT:
rc.top = pt.y;
rc.left = pt.x;
break;
case HTTOPRIGHT:
rc.top = pt.y;
rc.right = pt.x;
break;
case HTBOTTOM:
rc.bottom = pt.y;
break;
case HTBOTTOMLEFT:
rc.bottom = pt.y;
rc.left = pt.x;
break;
case HTBOTTOMRIGHT:
rc.bottom = pt.y;
rc.right = pt.x;
break;
}
POINT ptMin = MinTrackSize();
POINT ptMax = MaxTrackSize();
// We don't allow the left edge to move at present, but just in case
rc.left = XYMaximum(XYMinimum(rc.left, rcPreSize.right - ptMin.x), rcPreSize.right - ptMax.x);
rc.top = XYMaximum(XYMinimum(rc.top, rcPreSize.bottom - ptMin.y), rcPreSize.bottom - ptMax.y);
rc.right = XYMaximum(XYMinimum(rc.right, rcPreSize.left + ptMax.x), rcPreSize.left + ptMin.x);
rc.bottom = XYMaximum(XYMinimum(rc.bottom, rcPreSize.top + ptMax.y), rcPreSize.top + ptMin.y);
SetPosition(rc);
}
void ListBoxX::StartResize(WPARAM hitCode) {
rcPreSize = GetPosition();
POINT cursorPos;
::GetCursorPos(&cursorPos);
switch (hitCode) {
case HTRIGHT:
case HTBOTTOM:
case HTBOTTOMRIGHT:
dragOffset.x = rcPreSize.right - cursorPos.x;
dragOffset.y = rcPreSize.bottom - cursorPos.y;
break;
case HTTOPRIGHT:
dragOffset.x = rcPreSize.right - cursorPos.x;
dragOffset.y = rcPreSize.top - cursorPos.y;
break;
// Note that the current hit test code prevents the left edge cases ever firing
// as we don't want the left edge to be moveable
case HTLEFT:
case HTTOP:
case HTTOPLEFT:
dragOffset.x = rcPreSize.left - cursorPos.x;
dragOffset.y = rcPreSize.top - cursorPos.y;
break;
case HTBOTTOMLEFT:
dragOffset.x = rcPreSize.left - cursorPos.x;
dragOffset.y = rcPreSize.bottom - cursorPos.y;
break;
default:
return;
}
::SetCapture(GetHWND());
resizeHit = static_cast<int>(hitCode);
}
LRESULT ListBoxX::NcHitTest(WPARAM wParam, LPARAM lParam) const {
Window win = *this; // Copy HWND to avoid const problems
const PRectangle rc = win.GetPosition();
LRESULT hit = ::DefWindowProc(GetHWND(), WM_NCHITTEST, wParam, lParam);
// There is an apparent bug in the DefWindowProc hit test code whereby it will
// return HTTOPXXX if the window in question is shorter than the default
// window caption height + frame, even if one is hovering over the bottom edge of
// the frame, so workaround that here
if (hit >= HTTOP && hit <= HTTOPRIGHT) {
int minHeight = GetSystemMetrics(SM_CYMINTRACK);
int yPos = GET_Y_LPARAM(lParam);
if ((rc.Height() < minHeight) && (yPos > ((rc.top + rc.bottom)/2))) {
hit += HTBOTTOM - HTTOP;
}
}
// Nerver permit resizing that moves the left edge. Allow movement of top or bottom edge
// depending on whether the list is above or below the caret
switch (hit) {
case HTLEFT:
case HTTOPLEFT:
case HTBOTTOMLEFT:
hit = HTERROR;
break;
case HTTOP:
case HTTOPRIGHT: {
// Valid only if caret below list
if (location.y < rc.top)
hit = HTERROR;
}
break;
case HTBOTTOM:
case HTBOTTOMRIGHT: {
// Valid only if caret above list
if (rc.bottom <= location.y)
hit = HTERROR;
}
break;
}
return hit;
}
void ListBoxX::OnDoubleClick() {
if (doubleClickAction != NULL) {
doubleClickAction(doubleClickActionData);
}
}
POINT ListBoxX::GetClientExtent() const {
Window win = *this; // Copy HWND to avoid const problems
const PRectangle rc = win.GetPosition();
POINT ret;
ret.x = static_cast<LONG>(rc.Width());
ret.y = static_cast<LONG>(rc.Height());
return ret;
}
void ListBoxX::CentreItem(int n) {
// If below mid point, scroll up to centre, but with more items below if uneven
if (n >= 0) {
POINT extent = GetClientExtent();
int visible = extent.y/ItemHeight();
if (visible < Length()) {
LRESULT top = ::SendMessage(lb, LB_GETTOPINDEX, 0, 0);
int half = (visible - 1) / 2;
if (n > (top + half))
::SendMessage(lb, LB_SETTOPINDEX, n - half , 0);
}
}
}
// Performs a double-buffered paint operation to avoid flicker
void ListBoxX::Paint(HDC hDC) {
POINT extent = GetClientExtent();
HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, extent.x, extent.y);
HDC bitmapDC = ::CreateCompatibleDC(hDC);
HBITMAP hBitmapOld = SelectBitmap(bitmapDC, hBitmap);
// The list background is mainly erased during painting, but can be a small
// unpainted area when at the end of a non-integrally sized list with a
// vertical scroll bar
RECT rc = { 0, 0, extent.x, extent.y };
::FillRect(bitmapDC, &rc, reinterpret_cast<HBRUSH>(COLOR_WINDOW+1));
// Paint the entire client area and vertical scrollbar
::SendMessage(lb, WM_PRINT, reinterpret_cast<WPARAM>(bitmapDC), PRF_CLIENT|PRF_NONCLIENT);
::BitBlt(hDC, 0, 0, extent.x, extent.y, bitmapDC, 0, 0, SRCCOPY);
// Select a stock brush to prevent warnings from BoundsChecker
::SelectObject(bitmapDC, GetStockFont(WHITE_BRUSH));
SelectBitmap(bitmapDC, hBitmapOld);
::DeleteDC(bitmapDC);
::DeleteObject(hBitmap);
}
LRESULT PASCAL ListBoxX::ControlWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
try {
switch (uMsg) {
case WM_ERASEBKGND:
return TRUE;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hDC = ::BeginPaint(hWnd, &ps);
ListBoxX *lbx = static_cast<ListBoxX *>(PointerFromWindow(::GetParent(hWnd)));
if (lbx)
lbx->Paint(hDC);
::EndPaint(hWnd, &ps);
}
return 0;
case WM_MOUSEACTIVATE:
// This prevents the view activating when the scrollbar is clicked
return MA_NOACTIVATE;
case WM_LBUTTONDOWN: {
// We must take control of selection to prevent the ListBox activating
// the popup
LRESULT lResult = ::SendMessage(hWnd, LB_ITEMFROMPOINT, 0, lParam);
int item = LOWORD(lResult);
if (HIWORD(lResult) == 0 && item >= 0) {
::SendMessage(hWnd, LB_SETCURSEL, item, 0);
}
}
return 0;
case WM_LBUTTONUP:
return 0;
case WM_LBUTTONDBLCLK: {
ListBoxX *lbx = static_cast<ListBoxX *>(PointerFromWindow(::GetParent(hWnd)));
if (lbx) {
lbx->OnDoubleClick();
}
}
return 0;
case WM_MBUTTONDOWN:
// disable the scroll wheel button click action
return 0;
}
WNDPROC prevWndProc = reinterpret_cast<WNDPROC>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
if (prevWndProc) {
return ::CallWindowProc(prevWndProc, hWnd, uMsg, wParam, lParam);
} else {
return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
}
} catch (...) {
}
return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
}
LRESULT ListBoxX::WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) {
switch (iMessage) {
case WM_CREATE: {
HINSTANCE hinstanceParent = GetWindowInstance(static_cast<HWND>(parent->GetID()));
// Note that LBS_NOINTEGRALHEIGHT is specified to fix cosmetic issue when resizing the list
// but has useful side effect of speeding up list population significantly
lb = ::CreateWindowEx(
0, TEXT("listbox"), TEXT(""),
WS_CHILD | WS_VSCROLL | WS_VISIBLE |
LBS_OWNERDRAWFIXED | LBS_NODATA | LBS_NOINTEGRALHEIGHT,
0, 0, 150,80, hWnd,
reinterpret_cast<HMENU>(static_cast<ptrdiff_t>(ctrlID)),
hinstanceParent,
0);
WNDPROC prevWndProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(lb, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(ControlWndProc)));
::SetWindowLongPtr(lb, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(prevWndProc));
}
break;
case WM_SIZE:
if (lb) {
SetRedraw(false);
::SetWindowPos(lb, 0, 0,0, LOWORD(lParam), HIWORD(lParam), SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE);
// Ensure the selection remains visible
CentreItem(GetSelection());
SetRedraw(true);
}
break;
case WM_PAINT: {
PAINTSTRUCT ps;
::BeginPaint(hWnd, &ps);
::EndPaint(hWnd, &ps);
}
break;
case WM_COMMAND:
// This is not actually needed now - the registered double click action is used
// directly to action a choice from the list.
::SendMessage(static_cast<HWND>(parent->GetID()), iMessage, wParam, lParam);
break;
case WM_MEASUREITEM: {
MEASUREITEMSTRUCT *pMeasureItem = reinterpret_cast<MEASUREITEMSTRUCT *>(lParam);
pMeasureItem->itemHeight = static_cast<unsigned int>(ItemHeight());
}
break;
case WM_DRAWITEM:
Draw(reinterpret_cast<DRAWITEMSTRUCT *>(lParam));
break;
case WM_DESTROY:
lb = 0;
::SetWindowLong(hWnd, 0, 0);
return ::DefWindowProc(hWnd, iMessage, wParam, lParam);
case WM_ERASEBKGND:
// To reduce flicker we can elide background erasure since this window is
// completely covered by its child.
return TRUE;
case WM_GETMINMAXINFO: {
MINMAXINFO *minMax = reinterpret_cast<MINMAXINFO*>(lParam);
minMax->ptMaxTrackSize = MaxTrackSize();
minMax->ptMinTrackSize = MinTrackSize();
}
break;
case WM_MOUSEACTIVATE:
return MA_NOACTIVATE;
case WM_NCHITTEST:
return NcHitTest(wParam, lParam);
case WM_NCLBUTTONDOWN:
// We have to implement our own window resizing because the DefWindowProc
// implementation insists on activating the resized window
StartResize(wParam);
return 0;
case WM_MOUSEMOVE: {
if (resizeHit == 0) {
return ::DefWindowProc(hWnd, iMessage, wParam, lParam);
} else {
ResizeToCursor();
}
}
break;
case WM_LBUTTONUP:
case WM_CANCELMODE:
if (resizeHit != 0) {
resizeHit = 0;
::ReleaseCapture();
}
return ::DefWindowProc(hWnd, iMessage, wParam, lParam);
case WM_MOUSEWHEEL:
wheelDelta -= static_cast<short>(HIWORD(wParam));
if (abs(wheelDelta) >= WHEEL_DELTA) {
int nRows = GetVisibleRows();
int linesToScroll = 1;
if (nRows > 1) {
linesToScroll = nRows - 1;
}
if (linesToScroll > 3) {
linesToScroll = 3;
}
linesToScroll *= (wheelDelta / WHEEL_DELTA);
LRESULT top = ::SendMessage(lb, LB_GETTOPINDEX, 0, 0) + linesToScroll;
if (top < 0) {
top = 0;
}
::SendMessage(lb, LB_SETTOPINDEX, top, 0);
// update wheel delta residue
if (wheelDelta >= 0)
wheelDelta = wheelDelta % WHEEL_DELTA;
else
wheelDelta = - (-wheelDelta % WHEEL_DELTA);
}
break;
default:
return ::DefWindowProc(hWnd, iMessage, wParam, lParam);
}
return 0;
}
LRESULT PASCAL ListBoxX::StaticWndProc(
HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) {
if (iMessage == WM_CREATE) {
CREATESTRUCT *pCreate = reinterpret_cast<CREATESTRUCT *>(lParam);
SetWindowPointer(hWnd, pCreate->lpCreateParams);
}
// Find C++ object associated with window.
ListBoxX *lbx = static_cast<ListBoxX *>(PointerFromWindow(hWnd));
if (lbx) {
return lbx->WndProc(hWnd, iMessage, wParam, lParam);
} else {
return ::DefWindowProc(hWnd, iMessage, wParam, lParam);
}
}
static bool ListBoxX_Register() {
WNDCLASSEX wndclassc;
wndclassc.cbSize = sizeof(wndclassc);
// We need CS_HREDRAW and CS_VREDRAW because of the ellipsis that might be drawn for
// truncated items in the list and the appearance/disappearance of the vertical scroll bar.
// The list repaint is double-buffered to avoid the flicker this would otherwise cause.
wndclassc.style = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW;
wndclassc.cbClsExtra = 0;
wndclassc.cbWndExtra = sizeof(ListBoxX *);
wndclassc.hInstance = hinstPlatformRes;
wndclassc.hIcon = NULL;
wndclassc.hbrBackground = NULL;
wndclassc.lpszMenuName = NULL;
wndclassc.lpfnWndProc = ListBoxX::StaticWndProc;
wndclassc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wndclassc.lpszClassName = ListBoxX_ClassName;
wndclassc.hIconSm = 0;
return ::RegisterClassEx(&wndclassc) != 0;
}
bool ListBoxX_Unregister() {
return ::UnregisterClass(ListBoxX_ClassName, hinstPlatformRes) != 0;
}
Menu::Menu() : mid(0) {
}
void Menu::CreatePopUp() {
Destroy();
mid = ::CreatePopupMenu();
}
void Menu::Destroy() {
if (mid)
::DestroyMenu(static_cast<HMENU>(mid));
mid = 0;
}
void Menu::Show(Point pt, Window &w) {
::TrackPopupMenu(static_cast<HMENU>(mid),
TPM_RIGHTBUTTON, static_cast<int>(pt.x - 4), static_cast<int>(pt.y), 0,
static_cast<HWND>(w.GetID()), NULL);
Destroy();
}
static bool initialisedET = false;
static bool usePerformanceCounter = false;
static LARGE_INTEGER frequency;
ElapsedTime::ElapsedTime() {
if (!initialisedET) {
usePerformanceCounter = ::QueryPerformanceFrequency(&frequency) != 0;
initialisedET = true;
}
if (usePerformanceCounter) {
LARGE_INTEGER timeVal;
::QueryPerformanceCounter(&timeVal);
bigBit = timeVal.HighPart;
littleBit = timeVal.LowPart;
} else {
bigBit = clock();
littleBit = 0;
}
}
double ElapsedTime::Duration(bool reset) {
double result;
long endBigBit;
long endLittleBit;
if (usePerformanceCounter) {
LARGE_INTEGER lEnd;
::QueryPerformanceCounter(&lEnd);
endBigBit = lEnd.HighPart;
endLittleBit = lEnd.LowPart;
LARGE_INTEGER lBegin;
lBegin.HighPart = bigBit;
lBegin.LowPart = littleBit;
double elapsed = static_cast<double>(lEnd.QuadPart - lBegin.QuadPart);
result = elapsed / static_cast<double>(frequency.QuadPart);
} else {
endBigBit = clock();
endLittleBit = 0;
double elapsed = endBigBit - bigBit;
result = elapsed / CLOCKS_PER_SEC;
}
if (reset) {
bigBit = endBigBit;
littleBit = endLittleBit;
}
return result;
}
class DynamicLibraryImpl : public DynamicLibrary {
protected:
HMODULE h;
public:
explicit DynamicLibraryImpl(const char *modulePath) {
h = ::LoadLibraryA(modulePath);
}
~DynamicLibraryImpl() override {
if (h != NULL)
::FreeLibrary(h);
}
// Use GetProcAddress to get a pointer to the relevant function.
Function FindFunction(const char *name) override {
if (h != NULL) {
// C++ standard doesn't like casts between function pointers and void pointers so use a union
union {
FARPROC fp;
Function f;
} fnConv;
fnConv.fp = ::GetProcAddress(h, name);
return fnConv.f;
} else {
return NULL;
}
}
bool IsValid() override {
return h != NULL;
}
};
DynamicLibrary *DynamicLibrary::Load(const char *modulePath) {
return static_cast<DynamicLibrary *>(new DynamicLibraryImpl(modulePath));
}
ColourDesired Platform::Chrome() {
return ::GetSysColor(COLOR_3DFACE);
}
ColourDesired Platform::ChromeHighlight() {
return ::GetSysColor(COLOR_3DHIGHLIGHT);
}
const char *Platform::DefaultFont() {
return "Verdana";
}
int Platform::DefaultFontSize() {
return 8;
}
unsigned int Platform::DoubleClickTime() {
return ::GetDoubleClickTime();
}
bool Platform::MouseButtonBounce() {
return false;
}
void Platform::DebugDisplay(const char *s) {
::OutputDebugStringA(s);
}
bool Platform::IsKeyDown(int key) {
return (::GetKeyState(key) & 0x80000000) != 0;
}
long Platform::SendScintilla(WindowID w, unsigned int msg, unsigned long wParam, long lParam) {
// This should never be called - its here to satisfy an old interface
return static_cast<long>(::SendMessage(static_cast<HWND>(w), msg, wParam, lParam));
}
long Platform::SendScintillaPointer(WindowID w, unsigned int msg, unsigned long wParam, void *lParam) {
// This should never be called - its here to satisfy an old interface
return static_cast<long>(::SendMessage(static_cast<HWND>(w), msg, wParam,
reinterpret_cast<LPARAM>(lParam)));
}
bool Platform::IsDBCSLeadByte(int codePage, char ch) {
// Byte ranges found in Wikipedia articles with relevant search strings in each case
unsigned char uch = static_cast<unsigned char>(ch);
switch (codePage) {
case 932:
// Shift_jis
return ((uch >= 0x81) && (uch <= 0x9F)) ||
((uch >= 0xE0) && (uch <= 0xEF));
case 936:
// GBK
return (uch >= 0x81) && (uch <= 0xFE);
case 949:
// Korean Wansung KS C-5601-1987
return (uch >= 0x81) && (uch <= 0xFE);
case 950:
// Big5
return (uch >= 0x81) && (uch <= 0xFE);
case 1361:
// Korean Johab KS C-5601-1992
return
((uch >= 0x84) && (uch <= 0xD3)) ||
((uch >= 0xD8) && (uch <= 0xDE)) ||
((uch >= 0xE0) && (uch <= 0xF9));
}
return false;
}
int Platform::DBCSCharLength(int codePage, const char *s) {
if (codePage == 932 || codePage == 936 || codePage == 949 ||
codePage == 950 || codePage == 1361) {
return Platform::IsDBCSLeadByte(codePage, s[0]) ? 2 : 1;
} else {
return 1;
}
}
int Platform::DBCSCharMaxLength() {
return 2;
}
// These are utility functions not really tied to a platform
int Platform::Minimum(int a, int b) {
if (a < b)
return a;
else
return b;
}
int Platform::Maximum(int a, int b) {
if (a > b)
return a;
else
return b;
}
//#define TRACE
#ifdef TRACE
void Platform::DebugPrintf(const char *format, ...) {
char buffer[2000];
va_list pArguments;
va_start(pArguments, format);
vsprintf(buffer,format,pArguments);
va_end(pArguments);
Platform::DebugDisplay(buffer);
}
#else
void Platform::DebugPrintf(const char *, ...) {
}
#endif
static bool assertionPopUps = true;
bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
bool ret = assertionPopUps;
assertionPopUps = assertionPopUps_;
return ret;
}
void Platform::Assert(const char *c, const char *file, int line) {
char buffer[2000];
sprintf(buffer, "Assertion [%s] failed at %s %d%s", c, file, line, assertionPopUps ? "" : "\r\n");
if (assertionPopUps) {
int idButton = ::MessageBoxA(0, buffer, "Assertion failure",
MB_ABORTRETRYIGNORE|MB_ICONHAND|MB_SETFOREGROUND|MB_TASKMODAL);
if (idButton == IDRETRY) {
::DebugBreak();
} else if (idButton == IDIGNORE) {
// all OK
} else {
abort();
}
} else {
Platform::DebugDisplay(buffer);
::DebugBreak();
abort();
}
}
int Platform::Clamp(int val, int minVal, int maxVal) {
if (val > maxVal)
val = maxVal;
if (val < minVal)
val = minVal;
return val;
}
void Platform_Initialise(void *hInstance) {
::InitializeCriticalSection(&crPlatformLock);
hinstPlatformRes = static_cast<HINSTANCE>(hInstance);
ListBoxX_Register();
}
void Platform_Finalise(bool fromDllMain) {
#if defined(USE_D2D)
if (!fromDllMain) {
if (defaultRenderingParams) {
defaultRenderingParams->Release();
defaultRenderingParams = 0;
}
if (customClearTypeRenderingParams) {
customClearTypeRenderingParams->Release();
customClearTypeRenderingParams = 0;
}
if (pIDWriteFactory) {
pIDWriteFactory->Release();
pIDWriteFactory = 0;
}
if (pD2DFactory) {
pD2DFactory->Release();
pD2DFactory = 0;
}
if (hDLLDWrite) {
FreeLibrary(hDLLDWrite);
hDLLDWrite = NULL;
}
if (hDLLD2D) {
FreeLibrary(hDLLD2D);
hDLLD2D = NULL;
}
}
#endif
if (reverseArrowCursor != NULL)
::DestroyCursor(reverseArrowCursor);
ListBoxX_Unregister();
::DeleteCriticalSection(&crPlatformLock);
}
#ifdef SCI_NAMESPACE
}
#endif
|