1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
|
//--------------------------------------------------------------------------------------
// File: ImeUi.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkId=320437
//--------------------------------------------------------------------------------------
#include "dxut.h"
#include "ImeUi.h"
#include <math.h>
#include <msctf.h>
#include <malloc.h>
// Ignore typecast warnings
#pragma warning( disable : 4312 )
#pragma warning( disable : 4244 )
#pragma warning( disable : 4311 )
#pragma prefast( disable : 28159, "GetTickCount() is fine for a blinking cursor" )
#define MAX_CANDIDATE_LENGTH 256
#define COUNTOF(a) ( sizeof( a ) / sizeof( ( a )[0] ) )
#define POSITION_UNINITIALIZED ((DWORD)-1)
#define LANG_CHT MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL)
#define LANG_CHS MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED)
#define MAKEIMEVERSION(major,minor) ( (DWORD)( ( (BYTE)( major ) << 24 ) | ( (BYTE)( minor ) << 16 ) ) )
#define IMEID_VER(dwId) ( ( dwId ) & 0xffff0000 )
#define IMEID_LANG(dwId) ( ( dwId ) & 0x0000ffff )
#define _CHT_HKL_DAYI ( (HKL)0xE0060404 ) // DaYi
#define _CHT_HKL_NEW_PHONETIC ( (HKL)0xE0080404 ) // New Phonetic
#define _CHT_HKL_NEW_CHANG_JIE ( (HKL)0xE0090404 ) // New Chang Jie
#define _CHT_HKL_NEW_QUICK ( (HKL)0xE00A0404 ) // New Quick
#define _CHT_HKL_HK_CANTONESE ( (HKL)0xE00B0404 ) // Hong Kong Cantonese
#define _CHT_IMEFILENAME "TINTLGNT.IME" // New Phonetic
#define _CHT_IMEFILENAME2 "CINTLGNT.IME" // New Chang Jie
#define _CHT_IMEFILENAME3 "MSTCIPHA.IME" // Phonetic 5.1
#define IMEID_CHT_VER42 ( LANG_CHT | MAKEIMEVERSION( 4, 2 ) ) // New(Phonetic/ChanJie)IME98 : 4.2.x.x // Win98
#define IMEID_CHT_VER43 ( LANG_CHT | MAKEIMEVERSION( 4, 3 ) ) // New(Phonetic/ChanJie)IME98a : 4.3.x.x // Win2k
#define IMEID_CHT_VER44 ( LANG_CHT | MAKEIMEVERSION( 4, 4 ) ) // New ChanJie IME98b : 4.4.x.x // WinXP
#define IMEID_CHT_VER50 ( LANG_CHT | MAKEIMEVERSION( 5, 0 ) ) // New(Phonetic/ChanJie)IME5.0 : 5.0.x.x // WinME
#define IMEID_CHT_VER51 ( LANG_CHT | MAKEIMEVERSION( 5, 1 ) ) // New(Phonetic/ChanJie)IME5.1 : 5.1.x.x // IME2002(w/OfficeXP)
#define IMEID_CHT_VER52 ( LANG_CHT | MAKEIMEVERSION( 5, 2 ) ) // New(Phonetic/ChanJie)IME5.2 : 5.2.x.x // IME2002a(w/WinXP)
#define IMEID_CHT_VER60 ( LANG_CHT | MAKEIMEVERSION( 6, 0 ) ) // New(Phonetic/ChanJie)IME6.0 : 6.0.x.x // New IME 6.0(web download)
#define IMEID_CHT_VER_VISTA ( LANG_CHT | MAKEIMEVERSION( 7, 0 ) ) // All TSF TIP under Cicero UI-less mode: a hack to make GetImeId() return non-zero value
#define _CHS_HKL ( (HKL)0xE00E0804 ) // MSPY
#define _CHS_IMEFILENAME "PINTLGNT.IME" // MSPY1.5/2/3
#define _CHS_IMEFILENAME2 "MSSCIPYA.IME" // MSPY3 for OfficeXP
#define IMEID_CHS_VER41 ( LANG_CHS | MAKEIMEVERSION( 4, 1 ) ) // MSPY1.5 // SCIME97 or MSPY1.5 (w/Win98, Office97)
#define IMEID_CHS_VER42 ( LANG_CHS | MAKEIMEVERSION( 4, 2 ) ) // MSPY2 // Win2k/WinME
#define IMEID_CHS_VER53 ( LANG_CHS | MAKEIMEVERSION( 5, 3 ) ) // MSPY3 // WinXP
static CHAR signature[] = "%%%IMEUILIB:070111%%%";
static IMEUI_APPEARANCE gSkinIME =
{
0, // symbolColor;
0x404040, // symbolColorOff;
0xff000000, // symbolColorText;
24, // symbolHeight;
0xa0, // symbolTranslucence;
0, // symbolPlacement;
nullptr, // symbolFont;
0xffffffff, // candColorBase;
0xff000000, // candColorBorder;
0, // candColorText;
0x00ffff00, // compColorInput;
0x000000ff, // compColorTargetConv;
0x0000ff00, // compColorConverted;
0x00ff0000, // compColorTargetNotConv;
0x00ff0000, // compColorInputErr;
0x80, // compTranslucence;
0, // compColorText;
2, // caretWidth;
1, // caretYMargin;
};
struct _SkinCompStr
{
DWORD colorInput;
DWORD colorTargetConv;
DWORD colorConverted;
DWORD colorTargetNotConv;
DWORD colorInputErr;
};
_SkinCompStr gSkinCompStr;
// Definition from Win98DDK version of IMM.H
typedef struct
tagINPUTCONTEXT2
{
HWND hWnd;
BOOL fOpen;
POINT ptStatusWndPos;
POINT ptSoftKbdPos;
DWORD fdwConversion;
DWORD fdwSentence;
union
{
LOGFONTA A;
LOGFONTW W;
} lfFont;
COMPOSITIONFORM cfCompForm;
CANDIDATEFORM cfCandForm[4];
HIMCC hCompStr;
HIMCC hCandInfo;
HIMCC hGuideLine;
HIMCC hPrivate;
DWORD dwNumMsgBuf;
HIMCC hMsgBuf;
DWORD fdwInit;
DWORD dwReserve[3];
}
INPUTCONTEXT2, *PINPUTCONTEXT2, NEAR *NPINPUTCONTEXT2,
FAR* LPINPUTCONTEXT2;
// Class to disable Cicero in case ImmDisableTextFrameService() doesn't disable it completely
class CDisableCicero
{
public:
CDisableCicero() : m_ptim( nullptr ),
m_bComInit( false )
{
}
~CDisableCicero()
{
Uninitialize();
}
void Initialize()
{
if( m_bComInit )
{
return;
}
HRESULT hr;
hr = CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED );
if( SUCCEEDED( hr ) )
{
m_bComInit = true;
hr = CoCreateInstance( CLSID_TF_ThreadMgr,
nullptr,
CLSCTX_INPROC_SERVER,
__uuidof( ITfThreadMgr ),
( void** )&m_ptim );
}
}
void Uninitialize()
{
if( m_ptim )
{
m_ptim->Release();
m_ptim = nullptr;
}
if( m_bComInit )
CoUninitialize();
m_bComInit = false;
}
void DisableCiceroOnThisWnd( HWND hwnd )
{
if( !m_ptim )
return;
ITfDocumentMgr* pdimPrev; // the dim that is associated previously.
// Associate nullptr dim to the window.
// When this window gets the focus, Cicero does not work and IMM32 IME
// will be activated.
if( SUCCEEDED( m_ptim->AssociateFocus( hwnd, nullptr, &pdimPrev ) ) )
{
if( pdimPrev )
pdimPrev->Release();
}
}
private:
ITfThreadMgr* m_ptim;
bool m_bComInit;
};
static CDisableCicero g_disableCicero;
#define _IsLeadByte(x) ( LeadByteTable[(BYTE)( x )] )
static void _PumpMessage();
static BYTE LeadByteTable[256];
#define _ImmGetContext ImmGetContext
#define _ImmReleaseContext ImmReleaseContext
#define _ImmAssociateContext ImmAssociateContext
static LONG ( WINAPI* _ImmGetCompositionString )( HIMC himc, DWORD dwIndex, LPVOID lpBuf, DWORD dwBufLen );
#define _ImmGetOpenStatus ImmGetOpenStatus
#define _ImmSetOpenStatus ImmSetOpenStatus
#define _ImmGetConversionStatus ImmGetConversionStatus
static DWORD ( WINAPI* _ImmGetCandidateList )( HIMC himc, DWORD deIndex, LPCANDIDATELIST lpCandList, DWORD dwBufLen );
static LPINPUTCONTEXT2 ( WINAPI* _ImmLockIMC )( HIMC hIMC );
static BOOL ( WINAPI* _ImmUnlockIMC )( HIMC hIMC );
static LPVOID ( WINAPI* _ImmLockIMCC )( HIMCC hIMCC );
static BOOL ( WINAPI* _ImmUnlockIMCC )( HIMCC hIMCC );
#define _ImmGetDefaultIMEWnd ImmGetDefaultIMEWnd
#define _ImmGetIMEFileNameA ImmGetIMEFileNameA
#define _ImmGetVirtualKey ImmGetVirtualKey
#define _ImmNotifyIME ImmNotifyIME
#define _ImmSetConversionStatus ImmSetConversionStatus
#define _ImmSimulateHotKey ImmSimulateHotKey
#define _ImmIsIME ImmIsIME
// private API provided by CHT IME. Available on version 6.0 or later.
UINT ( WINAPI*_GetReadingString )( HIMC himc, UINT uReadingBufLen, LPWSTR lpwReadingBuf, PINT pnErrorIndex,
BOOL* pfIsVertical, PUINT puMaxReadingLen );
BOOL ( WINAPI*_ShowReadingWindow )( HIMC himc, BOOL bShow );
// Callbacks
void ( CALLBACK*ImeUiCallback_DrawRect )( int x1, int y1, int x2, int y2, DWORD color );
void ( CALLBACK*ImeUiCallback_DrawFans )( const IMEUI_VERTEX* paVertex, UINT uNum );
void* ( __cdecl*ImeUiCallback_Malloc )( size_t bytes );
void ( __cdecl*ImeUiCallback_Free )( void* ptr );
void ( CALLBACK*ImeUiCallback_OnChar )( WCHAR wc );
static void (*_SendCompString )();
static LRESULT ( WINAPI* _SendMessage )( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp ) = SendMessageA;
static DWORD (* _GetCandidateList )( HIMC himc, DWORD dwIndex, LPCANDIDATELIST* ppCandList );
static HWND g_hwndMain;
static HWND g_hwndCurr;
static HIMC g_himcOrg;
static bool g_bImeEnabled = false;
static TCHAR g_szCompositionString[256];
static BYTE g_szCompAttrString[256];
static DWORD g_IMECursorBytes = 0;
static DWORD g_IMECursorChars = 0;
static TCHAR g_szCandidate[MAX_CANDLIST][MAX_CANDIDATE_LENGTH];
static DWORD g_dwSelection, g_dwCount;
static UINT g_uCandPageSize;
static DWORD g_bDisableImeCompletely = false;
static DWORD g_dwIMELevel;
static DWORD g_dwIMELevelSaved;
static TCHAR g_szMultiLineCompString[ 256 *( 3 - sizeof( TCHAR ) ) ];
static bool g_bReadingWindow = false;
static bool g_bHorizontalReading = false;
static bool g_bVerticalCand = true;
static UINT g_uCaretBlinkTime = 0;
static UINT g_uCaretBlinkLast = 0;
static bool g_bCaretDraw = false;
static bool g_bChineseIME;
static bool g_bInsertMode = true;
static TCHAR g_szReadingString[32]; // Used only in case of horizontal reading window
static int g_iReadingError; // Used only in case of horizontal reading window
static UINT g_screenWidth, g_screenHeight;
static DWORD g_dwPrevFloat;
static bool bIsSendingKeyMessage = false;
static OSVERSIONINFOA g_osi;
static bool g_bInitialized = false;
static bool g_bCandList = false;
static DWORD g_dwCandX, g_dwCandY;
static DWORD g_dwCaretX, g_dwCaretY;
static DWORD g_hCompChar;
static int g_iCandListIndexBase;
static DWORD g_dwImeUiFlags = IMEUI_FLAG_SUPPORT_CARET;
static bool g_bUILessMode = false;
static HMODULE g_hImmDll = nullptr;
#define IsNT() (g_osi.dwPlatformId == VER_PLATFORM_WIN32_NT)
struct CompStringAttribute
{
UINT caretX;
UINT caretY;
CImeUiFont_Base* pFont;
DWORD colorComp;
DWORD colorCand;
RECT margins;
};
static CompStringAttribute g_CaretInfo;
static DWORD g_dwState = IMEUI_STATE_OFF;
static DWORD swirl = 0;
static double lastSwirl;
#define INDICATOR_NON_IME 0
#define INDICATOR_CHS 1
#define INDICATOR_CHT 2
#define INDICATOR_KOREAN 3
#define INDICATOR_JAPANESE 4
#define GETLANG() LOWORD(g_hklCurrent)
#define GETPRIMLANG() ((WORD)PRIMARYLANGID(GETLANG()))
#define GETSUBLANG() SUBLANGID(GETLANG())
#define LANG_CHS MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED)
#define LANG_CHT MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL)
static HKL g_hklCurrent = 0;
static UINT g_uCodePage = 0;
static LPCTSTR g_aszIndicator[] =
{
TEXT( "A" ),
L"\x7B80",
L"\x7E41",
L"\xac00",
L"\x3042",
};
static LPCTSTR g_pszIndicatior = g_aszIndicator[0];
static void GetReadingString( _In_ HWND hWnd );
static DWORD GetImeId( _In_ UINT uIndex = 0 );
static void CheckToggleState();
static void DrawImeIndicator();
static void DrawCandidateList();
static void DrawCompositionString( _In_ bool bDrawCompAttr );
static void GetReadingWindowOrientation( _In_ DWORD dwId );
static void OnInputLangChangeWorker();
static void OnInputLangChange();
static void SetImeApi();
static void CheckInputLocale();
static void SetSupportLevel( _In_ DWORD dwImeLevel );
void ImeUi_SetSupportLevel( _In_ DWORD dwImeLevel );
//
// local helper functions
//
inline LRESULT SendKeyMsg( HWND hwnd, UINT msg, WPARAM wp )
{
bIsSendingKeyMessage = true;
LRESULT lRc = _SendMessage( hwnd, msg, wp, 1 );
bIsSendingKeyMessage = false;
return lRc;
}
#define SendKeyMsg_DOWN(hwnd,vk) SendKeyMsg(hwnd, WM_KEYDOWN, vk)
#define SendKeyMsg_UP(hwnd,vk) SendKeyMsg(hwnd, WM_KEYUP, vk)
///////////////////////////////////////////////////////////////////////////////
//
// CTsfUiLessMode
// Handles IME events using Text Service Framework (TSF). Before Vista,
// IMM (Input Method Manager) API has been used to handle IME events and
// inqueries. Some IMM functions lose backward compatibility due to design
// of TSF, so we have to use new TSF interfaces.
//
///////////////////////////////////////////////////////////////////////////////
class CTsfUiLessMode
{
protected:
// Sink receives event notifications
class CUIElementSink : public ITfUIElementSink,
public ITfInputProcessorProfileActivationSink,
public ITfCompartmentEventSink
{
public:
CUIElementSink();
virtual ~CUIElementSink();
// IUnknown
STDMETHODIMP QueryInterface( _In_ REFIID riid, _COM_Outptr_ void** ppvObj );
STDMETHODIMP_( ULONG )
AddRef();
STDMETHODIMP_( ULONG )
Release();
// ITfUIElementSink
// Notifications for Reading Window events. We could process candidate as well, but we'll use IMM for simplicity sake.
STDMETHODIMP BeginUIElement( DWORD dwUIElementId, BOOL* pbShow );
STDMETHODIMP UpdateUIElement( DWORD dwUIElementId );
STDMETHODIMP EndUIElement( DWORD dwUIElementId );
// ITfInputProcessorProfileActivationSink
// Notification for keyboard input locale change
STDMETHODIMP OnActivated( DWORD dwProfileType, LANGID langid, _In_ REFCLSID clsid, _In_ REFGUID catid,
_In_ REFGUID guidProfile, HKL hkl, DWORD dwFlags );
// ITfCompartmentEventSink
// Notification for open mode (toggle state) change
STDMETHODIMP OnChange( _In_ REFGUID rguid );
private:
LONG _cRef;
};
static void MakeReadingInformationString( ITfReadingInformationUIElement* preading );
static void MakeCandidateStrings( ITfCandidateListUIElement* pcandidate );
static ITfUIElement* GetUIElement( DWORD dwUIElementId );
static BOOL GetCompartments( ITfCompartmentMgr** ppcm, ITfCompartment** ppTfOpenMode,
ITfCompartment** ppTfConvMode );
static BOOL SetupCompartmentSinks( BOOL bResetOnly = FALSE, ITfCompartment* pTfOpenMode = nullptr,
ITfCompartment* ppTfConvMode = nullptr );
static ITfThreadMgrEx* m_tm;
static DWORD m_dwUIElementSinkCookie;
static DWORD m_dwAlpnSinkCookie;
static DWORD m_dwOpenModeSinkCookie;
static DWORD m_dwConvModeSinkCookie;
static CUIElementSink* m_TsfSink;
static int m_nCandidateRefCount; // Some IME shows multiple candidate lists but the Library doesn't support multiple candidate list.
// So track open / close events to make sure the candidate list opened last is shown.
CTsfUiLessMode()
{
} // this class can't be instanciated
public:
static BOOL SetupSinks();
static void ReleaseSinks();
static BOOL CurrentInputLocaleIsIme();
static void UpdateImeState( BOOL bResetCompartmentEventSink = FALSE );
static void EnableUiUpdates( bool bEnable );
};
ITfThreadMgrEx* CTsfUiLessMode::m_tm;
DWORD CTsfUiLessMode::m_dwUIElementSinkCookie = TF_INVALID_COOKIE;
DWORD CTsfUiLessMode::m_dwAlpnSinkCookie = TF_INVALID_COOKIE;
DWORD CTsfUiLessMode::m_dwOpenModeSinkCookie = TF_INVALID_COOKIE;
DWORD CTsfUiLessMode::m_dwConvModeSinkCookie = TF_INVALID_COOKIE;
CTsfUiLessMode::CUIElementSink* CTsfUiLessMode::m_TsfSink = nullptr;
int CTsfUiLessMode::m_nCandidateRefCount = 0;
static unsigned long _strtoul( LPCSTR psz, LPTSTR*, int )
{
if( !psz )
return 0;
ULONG ulRet = 0;
if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
{
psz += 2;
ULONG ul = 0;
while( *psz )
{
if( '0' <= *psz && *psz <= '9' )
ul = *psz - '0';
else if( 'A' <= *psz && *psz <= 'F' )
ul = *psz - 'A' + 10;
else if( 'a' <= *psz && *psz <= 'f' )
ul = *psz - 'a' + 10;
else
break;
ulRet = ulRet * 16 + ul;
psz++;
}
}
else
{
while( *psz && ( '0' <= *psz && *psz <= '9' ) )
{
ulRet = ulRet * 10 + ( *psz - '0' );
psz++;
}
}
return ulRet;
}
#define GetCharCount(psz) (int)wcslen(psz)
#define GetCharCountFromBytes(psz,iBytes) (iBytes)
static void ComposeCandidateLine( int index, LPCTSTR pszCandidate )
{
LPTSTR psz = g_szCandidate[index];
*psz++ = ( TCHAR )( TEXT( '0' ) + ( ( index + g_iCandListIndexBase ) % 10 ) );
if( g_bVerticalCand )
{
*psz++ = TEXT( ' ' );
}
while( *pszCandidate && ( COUNTOF(g_szCandidate[index]) > ( psz - g_szCandidate[index] ) ) )
{
*psz++ = *pszCandidate++;
}
*psz = 0;
}
static void SendCompString()
{
int i, iLen = (int)wcslen( g_szCompositionString );
if( ImeUiCallback_OnChar )
{
LPCWSTR pwz;
pwz = g_szCompositionString;
for( i = 0; i < iLen; i++ )
{
ImeUiCallback_OnChar( pwz[i] );
}
return;
}
for( i = 0; i < iLen; i++ )
{
SendKeyMsg( g_hwndCurr, WM_CHAR,
(WPARAM)g_szCompositionString[i]
);
}
}
static DWORD GetCandidateList( HIMC himc, DWORD dwIndex, LPCANDIDATELIST* ppCandList )
{
DWORD dwBufLen = _ImmGetCandidateList( himc, dwIndex, nullptr, 0 );
if( dwBufLen )
{
*ppCandList = ( LPCANDIDATELIST )ImeUiCallback_Malloc( dwBufLen );
dwBufLen = _ImmGetCandidateList( himc, dwIndex, *ppCandList, dwBufLen );
}
return dwBufLen;
}
static void SendControlKeys( UINT vk, UINT num )
{
if( num == 0 )
return;
for( UINT i = 0; i < num; i++ )
{
SendKeyMsg_DOWN(g_hwndCurr, vk);
}
SendKeyMsg_UP(g_hwndCurr, vk);
}
// send key messages to erase composition string.
static void CancelCompString( HWND hwnd, bool bUseBackSpace = true, int iNewStrLen = 0 )
{
if( g_dwIMELevel != 3 )
return;
int cc = GetCharCount( g_szCompositionString );
int i;
// move caret to the end of composition string
SendControlKeys( VK_RIGHT, cc - g_IMECursorChars );
if( bUseBackSpace || g_bInsertMode )
iNewStrLen = 0;
// The caller sets bUseBackSpace to false if there's possibility of sending
// new composition string to the app right after this function call.
//
// If the app is in overwriting mode and new comp string is
// shorter than current one, delete previous comp string
// till it's same long as the new one. Then move caret to the beginning of comp string.
// New comp string will overwrite old one.
if( iNewStrLen < cc )
{
for( i = 0; i < cc - iNewStrLen; i++ )
{
SendKeyMsg_DOWN(hwnd, VK_BACK);
SendKeyMsg( hwnd, WM_CHAR, 8 ); //Backspace character
}
SendKeyMsg_UP(hwnd, VK_BACK);
}
else
iNewStrLen = cc;
SendControlKeys( VK_LEFT, iNewStrLen );
}
// initialize composition string data.
static void InitCompStringData()
{
g_IMECursorBytes = 0;
g_IMECursorChars = 0;
memset( &g_szCompositionString, 0, sizeof( g_szCompositionString ) );
memset( &g_szCompAttrString, 0, sizeof( g_szCompAttrString ) );
}
static void DrawCaret( DWORD x, DWORD y, DWORD height )
{
if( g_bCaretDraw && ImeUiCallback_DrawRect )
ImeUiCallback_DrawRect( x, y + gSkinIME.caretYMargin, x + gSkinIME.caretWidth,
y + height - gSkinIME.caretYMargin, g_CaretInfo.colorComp );
}
//
// Apps that draw the composition string on top of composition string attribute
// in level 3 support should call this function twice in rendering a frame.
// // Draw edit box UI;
// ImeUi_RenderUI(true, false); // paint composition string attribute;
// // Draw text in the edit box;
// ImeUi_RenderUi(false, true); // paint the rest of IME UI;
//
void ImeUi_RenderUI( _In_ bool bDrawCompAttr, _In_ bool bDrawOtherUi )
{
if( !g_bInitialized || !g_bImeEnabled || !g_CaretInfo.pFont )
return;
if( !bDrawCompAttr && !bDrawOtherUi )
return; // error case
if( g_dwIMELevel == 2 )
{
if( !bDrawOtherUi )
return; // 1st call for level 3 support
}
if( bDrawOtherUi )
DrawImeIndicator();
DrawCompositionString( bDrawCompAttr );
if( bDrawOtherUi )
DrawCandidateList();
}
static void DrawImeIndicator()
{
bool bOn = g_dwState != IMEUI_STATE_OFF;
IMEUI_VERTEX PieData[17];
float SizeOfPie = ( float )gSkinIME.symbolHeight;
memset( PieData, 0, sizeof( PieData ) );
switch( gSkinIME.symbolPlacement )
{
case 0: // vertical centering IME indicator
{
if( SizeOfPie + g_CaretInfo.margins.right + 4 > g_screenWidth )
{
PieData[0].sx = ( -SizeOfPie / 2 ) + g_CaretInfo.margins.left - 4;
PieData[0].sy = ( float )g_CaretInfo.margins.top + ( g_CaretInfo.margins.bottom -
g_CaretInfo.margins.top ) / 2;
}
else
{
PieData[0].sx = -( SizeOfPie / 2 ) + g_CaretInfo.margins.right + gSkinIME.symbolHeight + 4;
PieData[0].sy = ( float )g_CaretInfo.margins.top + ( g_CaretInfo.margins.bottom -
g_CaretInfo.margins.top ) / 2;
}
break;
}
case 1: // upperleft
PieData[0].sx = 4 + ( SizeOfPie / 2 );
PieData[0].sy = 4 + ( SizeOfPie / 2 );
break;
case 2: // upperright
PieData[0].sx = g_screenWidth - ( 4 + ( SizeOfPie / 2 ) );
PieData[0].sy = 4 + ( SizeOfPie / 2 );
break;
case 3: // lowerright
PieData[0].sx = g_screenWidth - ( 4 + ( SizeOfPie / 2 ) );
PieData[0].sy = g_screenHeight - ( 4 + ( SizeOfPie / 2 ) );
break;
case 4: // lowerleft
PieData[0].sx = 4 + ( SizeOfPie / 2 );
PieData[0].sy = g_screenHeight - ( 4 + ( SizeOfPie / 2 ) );
break;
}
PieData[0].rhw = 1.0f;
if( bOn )
{
if( GetTickCount() - lastSwirl > 250 )
{
swirl++;
lastSwirl = GetTickCount();
if( swirl > 13 )
swirl = 0;
}
}
else
swirl = 0;
for( int t1 = 1; t1 < 16; t1++ )
{
float radian = 2.0f * 3.1415926f * ( t1 - 1 + ( DWORD(bOn) * swirl ) ) / 14.0f;
PieData[t1].sx = ( float )( PieData[0].sx + SizeOfPie / 2 * cos( radian ) );
PieData[t1].sy = ( float )( PieData[0].sy + SizeOfPie / 2 * sin( radian ) );
PieData[t1].rhw = 1.0f;
}
PieData[0].color = 0xffffff + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
if( !gSkinIME.symbolColor && bOn )
{
{
PieData[1].color = 0xff0000 + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[2].color = 0xff3000 + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[3].color = 0xff6000 + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[4].color = 0xff9000 + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[5].color = 0xffC000 + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[6].color = 0xffff00 + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[7].color = 0xC0ff00 + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[8].color = 0x90ff00 + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[9].color = 0x60ff00 + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[10].color = 0x30c0ff + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[11].color = 0x00a0ff + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[12].color = 0x3090ff + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[13].color = 0x6060ff + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[14].color = 0x9030ff + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
PieData[15].color = 0xc000ff + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
}
}
else
{
DWORD dwColor = bOn ? gSkinIME.symbolColor : gSkinIME.symbolColorOff;
for( int t1 = 1; t1 < 16; t1++ )
{
PieData[t1].color = dwColor + ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 );
}
}
PieData[16] = PieData[1];
if( ImeUiCallback_DrawFans )
ImeUiCallback_DrawFans( PieData, 17 );
float fHeight = gSkinIME.symbolHeight * 0.625f;
// fix for Ent Gen #120 - reduce the height of character when Korean IME is on
if( GETPRIMLANG() == LANG_KOREAN && bOn )
{
fHeight *= 0.8f;
}
if( gSkinIME.symbolFont )
{
#ifdef DS2
// save the font height here since DS2 shares editbox font and indicator font
DWORD _w, _h;
g_CaretInfo.pFont->GetTextExtent( TEXT(" "), &_w, &_h );
#endif //DS2
// GOS deals height in points that is 1/72nd inch and assumes display device is 96dpi.
fHeight = fHeight * 96 / 72;
gSkinIME.symbolFont->SetHeight( ( UINT )fHeight );
gSkinIME.symbolFont->SetColor( ( ( ( DWORD )gSkinIME.symbolTranslucence ) << 24 ) | gSkinIME.symbolColorText );
//
// draw the proper symbol over the fan
//
DWORD w, h;
LPCTSTR cszSymbol = ( g_dwState == IMEUI_STATE_ON ) ? g_pszIndicatior : g_aszIndicator[0];
gSkinIME.symbolFont->GetTextExtent( cszSymbol, &w, &h );
gSkinIME.symbolFont->SetPosition( ( int )( PieData[0].sx ) - w / 2, ( int )( PieData[0].sy ) - h / 2 );
gSkinIME.symbolFont->DrawText( cszSymbol );
#ifdef DS2
// revert the height.
g_CaretInfo.pFont->SetHeight( _h );
// Double-check: Confirm match by testing a range of font heights to find best fit
DWORD _h2;
g_CaretInfo.pFont->GetTextExtent( TEXT(" "), &_w, &_h2 );
if ( _h2 < _h )
{
for ( int i=1; _h2<_h && i<10; i++ )
{
g_CaretInfo.pFont->SetHeight( _h+i );
g_CaretInfo.pFont->GetTextExtent( TEXT(" "), &_w, &_h2 );
}
}
else if ( _h2 > _h )
{
for ( int i=1; _h2>_h && i<10; i++ )
{
g_CaretInfo.pFont->SetHeight( _h-i );
g_CaretInfo.pFont->GetTextExtent( TEXT(" "), &_w, &_h2 );
}
}
#endif //DS2
}
}
static void DrawCompositionString( _In_ bool bDrawCompAttr )
{
// Process timer for caret blink
UINT uCurrentTime = GetTickCount();
if( uCurrentTime - g_uCaretBlinkLast > g_uCaretBlinkTime )
{
g_uCaretBlinkLast = uCurrentTime;
g_bCaretDraw = !g_bCaretDraw;
}
int i = 0;
g_CaretInfo.pFont->SetColor( g_CaretInfo.colorComp );
DWORD uDummy;
int len = (int)wcslen( g_szCompositionString );
DWORD bgX = g_CaretInfo.caretX;
DWORD bgY = g_CaretInfo.caretY;
g_dwCaretX = POSITION_UNINITIALIZED;
g_dwCaretY = POSITION_UNINITIALIZED;
DWORD candX = POSITION_UNINITIALIZED;
DWORD candY = 0;
LPTSTR pszMlcs = g_szMultiLineCompString;
DWORD wCompChar = 0;
DWORD hCompChar = 0;
g_CaretInfo.pFont->GetTextExtent( TEXT( " " ), &uDummy, &hCompChar );
if( g_dwIMELevel == 3 && g_IMECursorBytes && g_szCompositionString[0] )
{
// shift starting point of drawing composition string according to the current caret position.
TCHAR temp = g_szCompositionString[g_IMECursorBytes];
g_szCompositionString[g_IMECursorBytes] = 0;
g_CaretInfo.pFont->GetTextExtent( g_szCompositionString, &wCompChar, &hCompChar );
g_szCompositionString[g_IMECursorBytes] = temp;
bgX -= wCompChar;
}
//
// Draw the background colors for IME text nuggets
//
bool saveCandPos = false;
DWORD cType = 1;
LPTSTR pszCurrentCompLine = g_szCompositionString;
DWORD dwCompLineStart = bgX;
DWORD bgXnext = bgX;
if( GETPRIMLANG() != LANG_KOREAN || g_bCaretDraw ) // Korean uses composition attribute as blinking block caret
for( i = 0; i < len; i += cType )
{
DWORD bgColor = 0x00000000;
TCHAR szChar[3];
szChar[0] = g_szCompositionString[i];
szChar[1] = szChar[2] = 0;
bgX = bgXnext;
TCHAR cSave = g_szCompositionString[i + cType];
g_szCompositionString[i + cType] = 0;
g_CaretInfo.pFont->GetTextExtent( pszCurrentCompLine, &bgXnext, &hCompChar );
g_szCompositionString[i + cType] = cSave;
bgXnext += dwCompLineStart;
wCompChar = bgXnext - bgX;
switch( g_szCompAttrString[i] )
{
case ATTR_INPUT:
bgColor = gSkinCompStr.colorInput;
break;
case ATTR_TARGET_CONVERTED:
bgColor = gSkinCompStr.colorTargetConv;
if( IMEID_LANG( GetImeId() ) != LANG_CHS )
saveCandPos = true;
break;
case ATTR_CONVERTED:
bgColor = gSkinCompStr.colorConverted;
break;
case ATTR_TARGET_NOTCONVERTED:
//
// This is the one the user is working with currently
//
bgColor = gSkinCompStr.colorTargetNotConv;
break;
case ATTR_INPUT_ERROR:
bgColor = gSkinCompStr.colorInputErr;
break;
default:
// STOP( TEXT( "Attributes on IME characters are wrong" ) );
break;
}
if( g_dwIMELevel == 3 && bDrawCompAttr )
{
if( ( LONG )bgX >= g_CaretInfo.margins.left && ( LONG )bgX <= g_CaretInfo.margins.right )
{
if( g_dwImeUiFlags & IMEUI_FLAG_SUPPORT_CARET )
{
if( ImeUiCallback_DrawRect )
ImeUiCallback_DrawRect( bgX, bgY, bgX + wCompChar, bgY + hCompChar, bgColor );
}
else
{
if( ImeUiCallback_DrawRect )
ImeUiCallback_DrawRect( bgX - wCompChar, bgY, bgX, bgY + hCompChar, bgColor );
}
}
}
else if( g_dwIMELevel == 2 )
{
// make sure enough buffer space (possible space, NUL for current line, possible DBCS, 2 more NUL)
// are available in multiline composition string buffer
bool bWrite = ( pszMlcs - g_szMultiLineCompString <
COUNTOF( g_szMultiLineCompString ) - 5 * ( 3 - sizeof( TCHAR ) ) );
if( ( LONG )( bgX + wCompChar ) >= g_CaretInfo.margins.right )
{
bgX = dwCompLineStart = bgXnext = g_CaretInfo.margins.left;
bgY = bgY + hCompChar;
pszCurrentCompLine = g_szCompositionString + i;
if( bWrite )
{
if( pszMlcs == g_szMultiLineCompString || pszMlcs[-1] == 0 )
*pszMlcs++ = ' '; // to avoid zero length line
*pszMlcs++ = 0;
}
}
if( ImeUiCallback_DrawRect )
ImeUiCallback_DrawRect( bgX, bgY, bgX + wCompChar, bgY + hCompChar, bgColor );
if( bWrite )
{
*pszMlcs++ = g_szCompositionString[i];
}
if( ( DWORD )i == g_IMECursorBytes )
{
g_dwCaretX = bgX;
g_dwCaretY = bgY;
}
}
if( ( saveCandPos && candX == POSITION_UNINITIALIZED ) ||
( IMEID_LANG( GetImeId() ) == LANG_CHS && i / ( 3 - sizeof( TCHAR ) ) == ( int )g_IMECursorChars ) )
{
candX = bgX;
candY = bgY;
}
saveCandPos = false;
}
bgX = bgXnext;
if( g_dwIMELevel == 2 )
{
// in case the caret in composition string is at the end of it, draw it here
if( len != 0 && ( DWORD )i == g_IMECursorBytes )
{
g_dwCaretX = bgX;
g_dwCaretY = bgY;
}
// Draw composition string.
//assert(pszMlcs - g_szMultiLineCompString <=
// sizeof(g_szMultiLineCompString) / sizeof(g_szMultiLineCompString[0]) - 2);
*pszMlcs++ = 0;
*pszMlcs++ = 0;
DWORD x, y;
x = g_CaretInfo.caretX;
y = g_CaretInfo.caretY;
pszMlcs = g_szMultiLineCompString;
while( *pszMlcs &&
pszMlcs - g_szMultiLineCompString < sizeof( g_szMultiLineCompString ) / sizeof
( g_szMultiLineCompString[0] ) )
{
g_CaretInfo.pFont->SetPosition( x, y );
g_CaretInfo.pFont->DrawText( pszMlcs );
pszMlcs += wcslen( pszMlcs ) + 1;
x = g_CaretInfo.margins.left;
y += hCompChar;
}
}
// for changing z-order of caret
if( g_dwCaretX != POSITION_UNINITIALIZED && g_dwCaretY != POSITION_UNINITIALIZED )
{
DrawCaret( g_dwCaretX, g_dwCaretY, hCompChar );
}
g_dwCandX = candX;
g_dwCandY = candY;
g_hCompChar = hCompChar;
}
static void DrawCandidateList()
{
assert( g_CaretInfo.pFont != nullptr );
_Analysis_assume_( g_CaretInfo.pFont != nullptr );
DWORD candX = g_dwCandX;
DWORD candY = g_dwCandY;
DWORD hCompChar = g_hCompChar;
int i;
// draw candidate list / reading window
if( !g_dwCount || g_szCandidate[0][0] == 0 )
{
return;
}
// If position of candidate list is not initialized yet, set it here.
if( candX == POSITION_UNINITIALIZED )
{
// CHT IME in Vista doesn't have ATTR_TARGET_CONVERTED attribute while typing,
// so display the candidate list near the caret in the composition string
if( GETLANG() == LANG_CHT && GetImeId() != 0 && g_dwCaretX != POSITION_UNINITIALIZED )
{
candX = g_dwCaretX;
candY = g_dwCaretY;
}
else
{
candX = g_CaretInfo.caretX;
candY = g_CaretInfo.caretY;
}
}
SIZE largest =
{
0,0
};
static DWORD uDigitWidth = 0;
DWORD uSpaceWidth = 0;
static DWORD uDigitWidthList[10];
static CImeUiFont_Base* pPrevFont = nullptr;
// find out the widest width of the digits
if( pPrevFont != g_CaretInfo.pFont )
{
pPrevFont = g_CaretInfo.pFont;
for( int cnt = 0; cnt <= 9; cnt++ )
{
DWORD uDW = 0;
DWORD uDH = 0;
TCHAR ss[8];
swprintf_s( ss, COUNTOF(ss), TEXT( "%d" ), cnt );
g_CaretInfo.pFont->GetTextExtent( ss, &uDW, &uDH );
uDigitWidthList[cnt] = uDW;
if( uDW > uDigitWidth )
uDigitWidth = uDW;
if( ( signed )uDH > largest.cy )
largest.cy = uDH;
}
}
uSpaceWidth = uDigitWidth;
DWORD dwMarginX = ( uSpaceWidth + 1 ) / 2;
DWORD adwCandWidth[ MAX_CANDLIST ];
// Find out the widest width of the candidate strings
DWORD dwCandWidth = 0;
if( g_bReadingWindow && g_bHorizontalReading )
g_CaretInfo.pFont->GetTextExtent( g_szReadingString, ( DWORD* )&largest.cx, ( DWORD* )&largest.cy );
else
{
for( i = 0; g_szCandidate[i][0] && i < ( int )g_uCandPageSize; i++ )
{
DWORD tx = 0;
DWORD ty = 0;
if( g_bReadingWindow )
g_CaretInfo.pFont->GetTextExtent( g_szCandidate[i], &tx, &ty );
else
{
if( g_bVerticalCand )
g_CaretInfo.pFont->GetTextExtent( g_szCandidate[i] + 2, &tx, &ty );
else
g_CaretInfo.pFont->GetTextExtent( g_szCandidate[i] + 1, &tx, &ty );
tx = tx + uDigitWidth + uSpaceWidth;
}
if( ( signed )tx > largest.cx )
largest.cx = tx;
if( ( signed )ty > largest.cy )
largest.cy = ty;
adwCandWidth[ i ] = tx;
dwCandWidth += tx;
}
}
DWORD slotsUsed;
if( g_bReadingWindow && g_dwCount < g_uCandPageSize )
slotsUsed = g_dwCount;
else
slotsUsed = g_uCandPageSize;
// Show candidate list above composition string if there isn't enough room below.
DWORD dwCandHeight;
if( g_bVerticalCand && !( g_bReadingWindow && g_bHorizontalReading ) )
dwCandHeight = slotsUsed * largest.cy + 2;
else
dwCandHeight = largest.cy + 2;
if( candY + hCompChar + dwCandHeight > g_screenHeight )
candY -= dwCandHeight;
else
candY += hCompChar;
if( ( int )candY < 0 )
candY = 0;
// Move candidate list horizontally to keep it inside of screen
if( !g_bReadingWindow && IMEID_LANG( GetImeId() ) == LANG_CHS )
dwCandWidth += dwMarginX * ( slotsUsed - 1 );
else if( g_bReadingWindow && g_bHorizontalReading )
dwCandWidth = largest.cx + 2 + dwMarginX * 2;
else if( g_bVerticalCand || g_bReadingWindow )
dwCandWidth = largest.cx + 2 + dwMarginX * 2;
else
dwCandWidth = slotsUsed * ( largest.cx + 1 ) + 1;
if( candX + dwCandWidth > g_screenWidth )
candX = g_screenWidth - dwCandWidth;
if( ( int )candX < 0 )
candX = 0;
// Draw frame and background of candidate list / reading window
int seperateLineX = 0;
int left = candX;
int top = candY;
int right = candX + dwCandWidth;
int bottom = candY + dwCandHeight;
if( ImeUiCallback_DrawRect )
ImeUiCallback_DrawRect( left, top, right, bottom, gSkinIME.candColorBorder );
left++;
top++;
right--;
bottom--;
if( g_bReadingWindow || IMEID_LANG( GetImeId() ) == LANG_CHS )
{
if( ImeUiCallback_DrawRect )
ImeUiCallback_DrawRect( left, top, right, bottom, gSkinIME.candColorBase );
}
else if( g_bVerticalCand )
{
// uDigitWidth is the max width of all digits.
if( !g_bReadingWindow )
{
seperateLineX = left + dwMarginX + uDigitWidth + uSpaceWidth / 2;
if( ImeUiCallback_DrawRect )
{
ImeUiCallback_DrawRect( left, top, seperateLineX - 1, bottom, gSkinIME.candColorBase );
ImeUiCallback_DrawRect( seperateLineX, top, right, bottom, gSkinIME.candColorBase );
}
}
}
else
{
for( i = 0; ( DWORD )i < slotsUsed; i++ )
{
if( ImeUiCallback_DrawRect )
ImeUiCallback_DrawRect( left, top, left + largest.cx, bottom, gSkinIME.candColorBase );
left += largest.cx + 1;
}
}
// Draw candidates / reading strings
candX++;
candY++;
if( g_bReadingWindow && g_bHorizontalReading )
{
int iStart = -1, iEnd = -1, iDummy;
candX += dwMarginX;
// draw background of error character if it exists
TCHAR szTemp[COUNTOF( g_szReadingString ) ];
if( g_iReadingError >= 0 )
{
wcscpy_s( szTemp, COUNTOF(szTemp), g_szReadingString );
LPTSTR psz = szTemp + g_iReadingError;
psz++;
*psz = 0;
g_CaretInfo.pFont->GetTextExtent( szTemp, ( DWORD* )&iEnd, ( DWORD* )&iDummy );
TCHAR cSave = szTemp[ g_iReadingError ];
szTemp[g_iReadingError] = 0;
g_CaretInfo.pFont->GetTextExtent( szTemp, ( DWORD* )&iStart, ( DWORD* )&iDummy );
szTemp[g_iReadingError] = cSave;
if( ImeUiCallback_DrawRect )
ImeUiCallback_DrawRect( candX + iStart, candY, candX + iEnd, candY + largest.cy,
gSkinIME.candColorBorder );
}
g_CaretInfo.pFont->SetPosition( candX, candY );
g_CaretInfo.pFont->SetColor( g_CaretInfo.colorCand );
g_CaretInfo.pFont->DrawText( g_szReadingString );
// draw error character if it exists
if( iStart >= 0 )
{
g_CaretInfo.pFont->SetPosition( candX + iStart, candY );
if( gSkinIME.candColorBase != 0xffffffff || gSkinIME.candColorBorder != 0xff000000 )
g_CaretInfo.pFont->SetColor( g_CaretInfo.colorCand );
else
g_CaretInfo.pFont->SetColor( 0xff000000 + ( ~( ( 0x00ffffff ) & g_CaretInfo.colorCand ) ) );
g_CaretInfo.pFont->DrawText( szTemp + g_iReadingError );
}
}
else
{
for( i = 0; i < ( int )g_uCandPageSize && ( DWORD )i < g_dwCount; i++ )
{
if( g_dwSelection == ( DWORD )i )
{
if( gSkinIME.candColorBase != 0xffffffff || gSkinIME.candColorBorder != 0xff000000 )
g_CaretInfo.pFont->SetColor( g_CaretInfo.colorCand );
else
g_CaretInfo.pFont->SetColor( 0xff000000 + ( ~( ( 0x00ffffff ) & g_CaretInfo.colorCand ) ) );
if( ImeUiCallback_DrawRect )
{
if( g_bReadingWindow || g_bVerticalCand )
ImeUiCallback_DrawRect( candX, candY + i * largest.cy,
candX - 1 + dwCandWidth, candY + ( i + 1 ) * largest.cy,
gSkinIME.candColorBorder );
else
ImeUiCallback_DrawRect( candX, candY,
candX + adwCandWidth[i], candY + largest.cy,
gSkinIME.candColorBorder );
}
}
else
g_CaretInfo.pFont->SetColor( g_CaretInfo.colorCand );
if( g_szCandidate[i][0] != 0 )
{
if( !g_bReadingWindow && g_bVerticalCand )
{
TCHAR szOneDigit[2] =
{
g_szCandidate[i][0], 0
};
int nOneDigit = g_szCandidate[i][0] - TEXT( '0' );
TCHAR* szCandidateBody = g_szCandidate[i] + 2;
int dx = candX + ( seperateLineX - candX - uDigitWidthList[nOneDigit] ) / 2;
int dy = candY + largest.cy * i;
g_CaretInfo.pFont->SetPosition( dx, dy );
g_CaretInfo.pFont->DrawText( szOneDigit );
g_CaretInfo.pFont->SetPosition( seperateLineX + dwMarginX, dy );
g_CaretInfo.pFont->DrawText( szCandidateBody );
}
else if( g_bReadingWindow )
{
g_CaretInfo.pFont->SetPosition( dwMarginX + candX, candY + i * largest.cy );
g_CaretInfo.pFont->DrawText( g_szCandidate[i] );
}
else
{
g_CaretInfo.pFont->SetPosition( uSpaceWidth / 2 + candX, candY );
g_CaretInfo.pFont->DrawText( g_szCandidate[i] );
}
}
if( !g_bReadingWindow && !g_bVerticalCand )
{
if( IMEID_LANG( GetImeId() ) == LANG_CHS )
candX += adwCandWidth[i] + dwMarginX;
else
candX += largest.cx + 1;
}
}
}
}
static void CloseCandidateList()
{
g_bCandList = false;
if( !g_bReadingWindow ) // fix for Ent Gen #120.
{
g_dwCount = 0;
memset( &g_szCandidate, 0, sizeof( g_szCandidate ) );
}
}
//
// ProcessIMEMessages()
// Processes IME related messages and acquire information
//
#pragma warning(push)
#pragma warning( disable : 4616 6305 )
_Use_decl_annotations_
LPARAM ImeUi_ProcessMessage( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM& lParam, bool* trapped )
{
HIMC himc;
int len;
static LPARAM lAlt = 0x80000000, lCtrl = 0x80000000, lShift = 0x80000000;
*trapped = false;
if( !g_bInitialized || g_bDisableImeCompletely )
{
return 0;
}
switch( uMsg )
{
//
// IME Handling
//
case WM_INPUTLANGCHANGE:
OnInputLangChange();
break;
case WM_IME_SETCONTEXT:
//
// We don't want anything to display, so we have to clear lParam and pass it to DefWindowProc().
// Expecially important in Vista to receive IMN_CHANGECANDIDATE correctly.
//
lParam = 0;
break;
case WM_IME_STARTCOMPOSITION:
InitCompStringData();
*trapped = true;
break;
case WM_IME_COMPOSITION:
{
LONG lRet;
TCHAR szCompStr[COUNTOF(g_szCompositionString)];
*trapped = true;
himc = ImmGetContext( hWnd );
if( !himc )
{
break;
}
// ResultStr must be processed before composition string.
if( lParam & GCS_RESULTSTR )
{
lRet = ( LONG )_ImmGetCompositionString( himc, GCS_RESULTSTR, szCompStr,
COUNTOF( szCompStr ) ) / sizeof( TCHAR );
szCompStr[lRet] = 0;
CancelCompString( g_hwndCurr, false, GetCharCount( szCompStr ) );
wcscpy_s( g_szCompositionString, COUNTOF(g_szCompositionString), szCompStr );
_SendCompString();
InitCompStringData();
}
//
// Reads in the composition string.
//
if( lParam & GCS_COMPSTR )
{
//////////////////////////////////////////////////////
// Retrieve the latest user-selected IME candidates
lRet = ( LONG )_ImmGetCompositionString( himc, GCS_COMPSTR, szCompStr,
COUNTOF( szCompStr ) ) / sizeof( TCHAR );
szCompStr[lRet] = 0;
//
// Remove the whole of the string
//
CancelCompString( g_hwndCurr, false, GetCharCount( szCompStr ) );
wcscpy_s( g_szCompositionString, COUNTOF(g_szCompositionString), szCompStr );
lRet = _ImmGetCompositionString( himc, GCS_COMPATTR, g_szCompAttrString,
COUNTOF( g_szCompAttrString ) );
g_szCompAttrString[lRet] = 0;
// Older CHT IME uses composition string for reading string
if( GETLANG() == LANG_CHT && !GetImeId() )
{
int i, chars = (int)wcslen( g_szCompositionString ) / ( 3 - sizeof( TCHAR ) );
if( chars )
{
g_dwCount = 4;
g_dwSelection = ( DWORD )-1; // don't select any candidate
for( i = 3; i >= 0; i-- )
{
if( i > chars - 1 )
g_szCandidate[i][0] = 0;
else
{
g_szCandidate[i][0] = g_szCompositionString[i];
g_szCandidate[i][1] = 0;
}
}
g_uCandPageSize = MAX_CANDLIST;
memset( g_szCompositionString, 0, 8 );
g_bReadingWindow = true;
GetReadingWindowOrientation( 0 );
if( g_bHorizontalReading )
{
g_iReadingError = -1;
g_szReadingString[0] = 0;
for( i = 0; i < ( int )g_dwCount; i++ )
{
if( g_dwSelection == ( DWORD )i )
g_iReadingError = (int)wcslen( g_szReadingString );
LPCTSTR pszTmp = g_szCandidate[i];
wcscat_s( g_szReadingString, COUNTOF(g_szReadingString), pszTmp );
}
}
}
else
g_dwCount = 0;
}
// get caret position in composition string
g_IMECursorBytes = _ImmGetCompositionString( himc, GCS_CURSORPOS, nullptr, 0 );
g_IMECursorChars = GetCharCountFromBytes( g_szCompositionString, g_IMECursorBytes );
if( g_dwIMELevel == 3 )
{
// send composition string via WM_CHAR
_SendCompString();
// move caret to appropreate location
len = GetCharCount( g_szCompositionString + g_IMECursorBytes );
SendControlKeys( VK_LEFT, len );
}
}
_ImmReleaseContext( hWnd, himc );
}
break;
case WM_IME_ENDCOMPOSITION:
CancelCompString( g_hwndCurr );
InitCompStringData();
break;
case WM_IME_NOTIFY:
switch( wParam )
{
case IMN_SETCONVERSIONMODE:
{
// Disable CHT IME software keyboard.
static bool bNoReentrance = false;
if( LANG_CHT == GETLANG() && !bNoReentrance )
{
bNoReentrance = true;
DWORD dwConvMode, dwSentMode;
_ImmGetConversionStatus( g_himcOrg, &dwConvMode, &dwSentMode );
const DWORD dwFlag = IME_CMODE_SOFTKBD | IME_CMODE_SYMBOL;
if( dwConvMode & dwFlag )
_ImmSetConversionStatus( g_himcOrg, dwConvMode & ~dwFlag, dwSentMode );
}
bNoReentrance = false;
}
// fall through
case IMN_SETOPENSTATUS:
if( g_bUILessMode )
break;
CheckToggleState();
break;
case IMN_OPENCANDIDATE:
case IMN_CHANGECANDIDATE:
if( g_bUILessMode )
{
break;
}
{
g_bCandList = true;
*trapped = true;
himc = _ImmGetContext( hWnd );
if( !himc )
break;
LPCANDIDATELIST lpCandList;
DWORD dwIndex, dwBufLen;
g_bReadingWindow = false;
dwIndex = 0;
dwBufLen = _GetCandidateList( himc, dwIndex, &lpCandList );
if( dwBufLen )
{
g_dwSelection = lpCandList->dwSelection;
g_dwCount = lpCandList->dwCount;
int startOfPage = 0;
if( GETLANG() == LANG_CHS && GetImeId() )
{
// MSPY (CHS IME) has variable number of candidates in candidate window
// find where current page starts, and the size of current page
const int maxCandChar = 18 * ( 3 - sizeof( TCHAR ) );
UINT cChars = 0;
UINT i;
for( i = 0; i < g_dwCount; i++ )
{
UINT uLen = (int)wcslen(
( LPTSTR )( (UINT_PTR)lpCandList + lpCandList->dwOffset[i] ) ) +
( 3 - sizeof( TCHAR ) );
if( uLen + cChars > maxCandChar )
{
if( i > g_dwSelection )
{
break;
}
startOfPage = i;
cChars = uLen;
}
else
{
cChars += uLen;
}
}
g_uCandPageSize = i - startOfPage;
}
else
{
g_uCandPageSize = std::min<UINT>( lpCandList->dwPageSize, MAX_CANDLIST );
startOfPage = g_bUILessMode ? lpCandList->dwPageStart :
( g_dwSelection / g_uCandPageSize ) * g_uCandPageSize;
}
g_dwSelection = ( GETLANG() == LANG_CHS && !GetImeId() ) ? ( DWORD )-1
: g_dwSelection - startOfPage;
memset( &g_szCandidate, 0, sizeof( g_szCandidate ) );
for( UINT i = startOfPage, j = 0;
( DWORD )i < lpCandList->dwCount && j < g_uCandPageSize;
i++, j++ )
{
ComposeCandidateLine( j,
( LPTSTR )( (UINT_PTR)lpCandList + lpCandList->dwOffset[i] ) );
}
ImeUiCallback_Free( ( HANDLE )lpCandList );
_ImmReleaseContext( hWnd, himc );
// don't display selection in candidate list in case of Korean and old Chinese IME.
if( GETPRIMLANG() == LANG_KOREAN ||
GETLANG() == LANG_CHT && !GetImeId() )
g_dwSelection = ( DWORD )-1;
}
break;
}
case IMN_CLOSECANDIDATE:
if( g_bUILessMode )
{
break;
}
CloseCandidateList();
*trapped = true;
break;
// Jun.16,2000 05:21 by yutaka.
case IMN_PRIVATE:
{
if( !g_bCandList )
{
GetReadingString( hWnd );
}
// Trap some messages to hide reading window
DWORD dwId = GetImeId();
switch( dwId )
{
case IMEID_CHT_VER42:
case IMEID_CHT_VER43:
case IMEID_CHT_VER44:
case IMEID_CHS_VER41:
case IMEID_CHS_VER42:
if( ( lParam == 1 ) || ( lParam == 2 ) )
{
*trapped = true;
}
break;
case IMEID_CHT_VER50:
case IMEID_CHT_VER51:
case IMEID_CHT_VER52:
case IMEID_CHT_VER60:
case IMEID_CHS_VER53:
if( ( lParam == 16 ) || ( lParam == 17 ) || ( lParam == 26 ) || ( lParam == 27 ) ||
( lParam == 28 ) )
{
*trapped = true;
}
break;
}
}
break;
default:
*trapped = true;
break;
}
break;
// fix for #15386 - When Text Service Framework is installed in Win2K, Alt+Shift and Ctrl+Shift combination (to switch
// input locale / keyboard layout) doesn't send WM_KEYUP message for the key that is released first. We need to check
// if these keys are actually up whenever we receive key up message for other keys.
case WM_KEYUP:
case WM_SYSKEYUP:
if( !( lAlt & 0x80000000 ) && wParam != VK_MENU && ( GetAsyncKeyState( VK_MENU ) & 0x8000 ) == 0 )
{
PostMessageA( GetFocus(), WM_KEYUP, ( WPARAM )VK_MENU, ( lAlt & 0x01ff0000 ) | 0xC0000001 );
}
else if( !( lCtrl & 0x80000000 ) && wParam != VK_CONTROL &&
( GetAsyncKeyState( VK_CONTROL ) & 0x8000 ) == 0 )
{
PostMessageA( GetFocus(), WM_KEYUP, ( WPARAM )VK_CONTROL, ( lCtrl & 0x01ff0000 ) | 0xC0000001 );
}
else if( !( lShift & 0x80000000 ) && wParam != VK_SHIFT && ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) == 0 )
{
PostMessageA( GetFocus(), WM_KEYUP, ( WPARAM )VK_SHIFT, ( lShift & 0x01ff0000 ) | 0xC0000001 );
}
// fall through WM_KEYDOWN / WM_SYSKEYDOWN
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
{
switch( wParam )
{
case VK_MENU:
lAlt = lParam;
break;
case VK_SHIFT:
lShift = lParam;
break;
case VK_CONTROL:
lCtrl = lParam;
break;
}
}
break;
}
return 0;
}
#pragma warning(pop)
_Use_decl_annotations_
void ImeUi_SetCaretPosition( UINT x, UINT y )
{
if( !g_bInitialized )
return;
g_CaretInfo.caretX = x;
g_CaretInfo.caretY = y;
}
_Use_decl_annotations_
void ImeUi_SetCompStringAppearance( CImeUiFont_Base* pFont, DWORD color, const RECT* prc )
{
if( !g_bInitialized )
return;
g_CaretInfo.pFont = pFont;
g_CaretInfo.margins = *prc;
if( 0 == gSkinIME.candColorText )
g_CaretInfo.colorCand = color;
else
g_CaretInfo.colorCand = gSkinIME.candColorText;
if( 0 == gSkinIME.compColorText )
g_CaretInfo.colorComp = color;
else
g_CaretInfo.colorComp = gSkinIME.compColorText;
}
void ImeUi_SetState( _In_ DWORD dwState )
{
if( !g_bInitialized )
return;
HIMC himc;
if( dwState == IMEUI_STATE_ON )
{
ImeUi_EnableIme( true );
}
himc = _ImmGetContext( g_hwndCurr );
if( himc )
{
if( g_bDisableImeCompletely )
dwState = IMEUI_STATE_OFF;
bool bOn = dwState == IMEUI_STATE_ON; // for non-Chinese IME
switch( GETPRIMLANG() )
{
case LANG_CHINESE:
{
// toggle Chinese IME
DWORD dwId;
DWORD dwConvMode = 0, dwSentMode = 0;
if( ( g_bChineseIME && dwState == IMEUI_STATE_OFF ) ||
( !g_bChineseIME && dwState != IMEUI_STATE_OFF ) )
{
_ImmSimulateHotKey( g_hwndCurr, IME_THOTKEY_IME_NONIME_TOGGLE );
_PumpMessage();
}
if( dwState != IMEUI_STATE_OFF )
{
dwId = GetImeId();
if( dwId )
{
_ImmGetConversionStatus( himc, &dwConvMode, &dwSentMode );
dwConvMode = ( dwState == IMEUI_STATE_ON )
? ( dwConvMode | IME_CMODE_NATIVE )
: ( dwConvMode & ~IME_CMODE_NATIVE );
_ImmSetConversionStatus( himc, dwConvMode, dwSentMode );
}
}
break;
}
case LANG_KOREAN:
// toggle Korean IME
if( ( bOn && g_dwState != IMEUI_STATE_ON ) || ( !bOn && g_dwState == IMEUI_STATE_ON ) )
{
_ImmSimulateHotKey( g_hwndCurr, IME_KHOTKEY_ENGLISH );
}
break;
case LANG_JAPANESE:
_ImmSetOpenStatus( himc, bOn );
break;
}
_ImmReleaseContext( g_hwndCurr, himc );
CheckToggleState();
}
}
DWORD ImeUi_GetState()
{
if( !g_bInitialized )
return IMEUI_STATE_OFF;
CheckToggleState();
return g_dwState;
}
void ImeUi_EnableIme( _In_ bool bEnable )
{
if( !g_bInitialized || !g_hwndCurr )
return;
if( g_bDisableImeCompletely )
bEnable = false;
if( g_hwndCurr == g_hwndMain )
{
HIMC himcDbg;
himcDbg = _ImmAssociateContext( g_hwndCurr, bEnable? g_himcOrg : nullptr );
}
g_bImeEnabled = bEnable;
if( bEnable )
{
CheckToggleState();
}
CTsfUiLessMode::EnableUiUpdates( bEnable );
}
bool ImeUi_IsEnabled()
{
return g_bImeEnabled;
}
bool ImeUi_Initialize(_In_ HWND hwnd, _In_ bool bDisable )
{
if( g_bInitialized )
{
return true;
}
g_hwndMain = hwnd;
g_disableCicero.Initialize();
g_hImmDll = LoadLibraryEx( L"imm32.dll", nullptr, 0x00000800 /* LOAD_LIBRARY_SEARCH_SYSTEM32 */ );
g_bDisableImeCompletely = false;
if( g_hImmDll )
{
_ImmLockIMC = reinterpret_cast<LPINPUTCONTEXT2 ( WINAPI* )( HIMC hIMC )>( reinterpret_cast<void*>( GetProcAddress( g_hImmDll, "ImmLockIMC" ) ) );
_ImmUnlockIMC = reinterpret_cast<BOOL ( WINAPI* )( HIMC hIMC )>( reinterpret_cast<void*>( GetProcAddress( g_hImmDll, "ImmUnlockIMC" ) ) );
_ImmLockIMCC = reinterpret_cast<LPVOID ( WINAPI* )( HIMCC hIMCC )>( reinterpret_cast<void*>( GetProcAddress( g_hImmDll, "ImmLockIMCC" ) ) );
_ImmUnlockIMCC = reinterpret_cast<BOOL ( WINAPI* )( HIMCC hIMCC )>( reinterpret_cast<void*>( GetProcAddress( g_hImmDll, "ImmUnlockIMCC" ) ) );
BOOL ( WINAPI* _ImmDisableTextFrameService )( DWORD ) = reinterpret_cast<BOOL ( WINAPI* )( DWORD )>( reinterpret_cast<void*>( GetProcAddress( g_hImmDll,
"ImmDisableTextFrameService" ) ) );
if( _ImmDisableTextFrameService )
{
_ImmDisableTextFrameService( ( DWORD )-1 );
}
}
else
{
g_bDisableImeCompletely = true;
return false;
}
_ImmGetCompositionString = ImmGetCompositionStringW;
_ImmGetCandidateList = ImmGetCandidateListW;
_GetCandidateList = GetCandidateList;
_SendCompString = SendCompString;
_SendMessage = SendMessageW;
// turn init flag on so that subsequent calls to ImeUi functions work.
g_bInitialized = true;
ImeUi_SetWindow( g_hwndMain );
g_himcOrg = _ImmGetContext( g_hwndMain );
_ImmReleaseContext( g_hwndMain, g_himcOrg );
if( !g_himcOrg )
{
bDisable = true;
}
// the following pointers to function has to be initialized before this function is called.
if( bDisable ||
!ImeUiCallback_Malloc ||
!ImeUiCallback_Free
)
{
g_bDisableImeCompletely = true;
ImeUi_EnableIme( false );
g_bInitialized = bDisable;
return false;
}
g_uCaretBlinkTime = GetCaretBlinkTime();
g_CaretInfo.caretX = 0;
g_CaretInfo.caretY = 0;
g_CaretInfo.pFont = 0;
g_CaretInfo.colorComp = 0;
g_CaretInfo.colorCand = 0;
g_CaretInfo.margins.left = 0;
g_CaretInfo.margins.right = 640;
g_CaretInfo.margins.top = 0;
g_CaretInfo.margins.bottom = 480;
CheckInputLocale();
OnInputLangChangeWorker();
ImeUi_SetSupportLevel( 2 );
// SetupTSFSinks has to be called before CheckToggleState to make it work correctly.
g_bUILessMode = CTsfUiLessMode::SetupSinks() != FALSE;
CheckToggleState();
if( g_bUILessMode )
{
g_bChineseIME = ( GETPRIMLANG() == LANG_CHINESE ) && CTsfUiLessMode::CurrentInputLocaleIsIme();
CTsfUiLessMode::UpdateImeState();
}
ImeUi_EnableIme( false );
return true;
}
void ImeUi_Uninitialize()
{
if( !g_bInitialized )
{
return;
}
CTsfUiLessMode::ReleaseSinks();
if( g_hwndMain )
{
ImmAssociateContext( g_hwndMain, g_himcOrg );
}
g_hwndMain = nullptr;
g_himcOrg = nullptr;
if( g_hImmDll )
{
FreeLibrary( g_hImmDll );
g_hImmDll = nullptr;
}
g_disableCicero.Uninitialize();
g_bInitialized = false;
}
//
// GetImeId( UINT uIndex )
// returns
// returned value:
// 0: In the following cases
// - Non Chinese IME input locale
// - Older Chinese IME
// - Other error cases
//
// Othewise:
// When uIndex is 0 (default)
// bit 31-24: Major version
// bit 23-16: Minor version
// bit 15-0: Language ID
// When uIndex is 1
// pVerFixedInfo->dwFileVersionLS
//
// Use IMEID_VER and IMEID_LANG macro to extract version and language information.
//
static DWORD GetImeId( _In_ UINT uIndex )
{
static HKL hklPrev = 0;
static DWORD dwRet[2] =
{
0, 0
};
DWORD dwVerSize;
DWORD dwVerHandle;
LPVOID lpVerBuffer;
LPVOID lpVerData;
UINT cbVerData;
char szTmp[1024];
if( uIndex >= sizeof( dwRet ) / sizeof( dwRet[0] ) )
return 0;
HKL kl = g_hklCurrent;
if( hklPrev == kl )
{
return dwRet[uIndex];
}
hklPrev = kl;
DWORD dwLang = ( static_cast<DWORD>(reinterpret_cast<UINT_PTR>(kl)) & 0xffff );
if( g_bUILessMode && GETLANG() == LANG_CHT )
{
// In case of Vista, artifitial value is returned so that it's not considered as older IME.
dwRet[0] = IMEID_CHT_VER_VISTA;
dwRet[1] = 0;
return dwRet[0];
}
if( kl != _CHT_HKL_NEW_PHONETIC && kl != _CHT_HKL_NEW_CHANG_JIE
&& kl != _CHT_HKL_NEW_QUICK && kl != _CHT_HKL_HK_CANTONESE && kl != _CHS_HKL )
{
goto error;
}
if( _ImmGetIMEFileNameA( kl, szTmp, sizeof( szTmp ) - 1 ) <= 0 )
{
goto error;
}
if( !_GetReadingString ) // IME that doesn't implement private API
{
#define LCID_INVARIANT MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT)
if( ( CompareStringA( LCID_INVARIANT, NORM_IGNORECASE, szTmp, -1, _CHT_IMEFILENAME, -1 ) != 2 )
&& ( CompareStringA( LCID_INVARIANT, NORM_IGNORECASE, szTmp, -1, _CHT_IMEFILENAME2, -1 ) != 2 )
&& ( CompareStringA( LCID_INVARIANT, NORM_IGNORECASE, szTmp, -1, _CHT_IMEFILENAME3, -1 ) != 2 )
&& ( CompareStringA( LCID_INVARIANT, NORM_IGNORECASE, szTmp, -1, _CHS_IMEFILENAME, -1 ) != 2 )
&& ( CompareStringA( LCID_INVARIANT, NORM_IGNORECASE, szTmp, -1, _CHS_IMEFILENAME2, -1 ) != 2 )
)
{
goto error;
}
}
dwVerSize = GetFileVersionInfoSizeA( szTmp, &dwVerHandle );
if( dwVerSize )
{
lpVerBuffer = ( LPVOID )ImeUiCallback_Malloc( dwVerSize );
if( lpVerBuffer )
{
if( GetFileVersionInfoA( szTmp, 0, dwVerSize, lpVerBuffer ) )
{
if( VerQueryValueA( lpVerBuffer, "\\", &lpVerData, &cbVerData ) )
{
#define pVerFixedInfo ((VS_FIXEDFILEINFO FAR*)lpVerData)
DWORD dwVer = pVerFixedInfo->dwFileVersionMS;
dwVer = ( dwVer & 0x00ff0000 ) << 8 | ( dwVer & 0x000000ff ) << 16;
if( _GetReadingString ||
dwLang == LANG_CHT && (
dwVer == MAKEIMEVERSION(4, 2) ||
dwVer == MAKEIMEVERSION(4, 3) ||
dwVer == MAKEIMEVERSION(4, 4) ||
dwVer == MAKEIMEVERSION(5, 0) ||
dwVer == MAKEIMEVERSION(5, 1) ||
dwVer == MAKEIMEVERSION(5, 2) ||
dwVer == MAKEIMEVERSION(6, 0) )
||
dwLang == LANG_CHS && (
dwVer == MAKEIMEVERSION(4, 1) ||
dwVer == MAKEIMEVERSION(4, 2) ||
dwVer == MAKEIMEVERSION(5, 3) ) )
{
dwRet[0] = dwVer | dwLang;
dwRet[1] = pVerFixedInfo->dwFileVersionLS;
ImeUiCallback_Free( lpVerBuffer );
return dwRet[0];
}
#undef pVerFixedInfo
}
}
ImeUiCallback_Free( lpVerBuffer );
}
}
// The flow comes here in the following conditions
// - Non Chinese IME input locale
// - Older Chinese IME
// - Other error cases
error:
dwRet[0] = dwRet[1] = 0;
return dwRet[uIndex];
}
static void GetReadingString( _In_ HWND hWnd )
{
if( g_bUILessMode )
{
return;
}
DWORD dwId = GetImeId();
if( !dwId )
{
return;
}
HIMC himc;
himc = _ImmGetContext( hWnd );
if( !himc )
return;
DWORD dwlen = 0;
DWORD dwerr = 0;
WCHAR wzBuf[16]; // We believe 16 wchars are big enough to hold reading string after having discussion with CHT IME team.
WCHAR* wstr = wzBuf;
bool unicode = FALSE;
LPINPUTCONTEXT2 lpIMC = nullptr;
if( _GetReadingString )
{
BOOL bVertical;
UINT uMaxUiLen;
dwlen = _GetReadingString( himc, 0, nullptr, ( PINT )&dwerr, &bVertical, &uMaxUiLen );
if( dwlen )
{
if( dwlen > COUNTOF(wzBuf) )
{
dwlen = COUNTOF(wzBuf);
}
dwlen = _GetReadingString( himc, dwlen, wstr, ( PINT )&dwerr, &bVertical, &uMaxUiLen );
}
g_bHorizontalReading = bVertical == 0;
unicode = true;
}
else // IMEs that doesn't implement Reading String API
{
lpIMC = _ImmLockIMC( himc );
// *** hacking code from Michael Yang ***
LPBYTE p = 0;
switch( dwId )
{
case IMEID_CHT_VER42: // New(Phonetic/ChanJie)IME98 : 4.2.x.x // Win98
case IMEID_CHT_VER43: // New(Phonetic/ChanJie)IME98a : 4.3.x.x // WinMe, Win2k
case IMEID_CHT_VER44: // New ChanJie IME98b : 4.4.x.x // WinXP
p = *( LPBYTE* )( ( LPBYTE )_ImmLockIMCC( lpIMC->hPrivate ) + 24 );
if( !p ) break;
dwlen = *( DWORD* )( p + 7 * 4 + 32 * 4 ); //m_dwInputReadStrLen
dwerr = *( DWORD* )( p + 8 * 4 + 32 * 4 ); //m_dwErrorReadStrStart
wstr = ( WCHAR* )( p + 56 );
unicode = TRUE;
break;
case IMEID_CHT_VER50: // 5.0.x.x // WinME
p = *( LPBYTE* )( ( LPBYTE )_ImmLockIMCC( lpIMC->hPrivate ) + 3 * 4 ); // PCKeyCtrlManager
if( !p ) break;
p = *( LPBYTE* )( ( LPBYTE )p + 1 * 4 + 5 * 4 + 4 * 2 ); // = PCReading = &STypingInfo
if( !p ) break;
dwlen = *( DWORD* )( p + 1 * 4 + ( 16 * 2 + 2 * 4 ) + 5 * 4 + 16 ); //m_dwDisplayStringLength;
dwerr = *( DWORD* )( p + 1 * 4 + ( 16 * 2 + 2 * 4 ) + 5 * 4 + 16 + 1 * 4 ); //m_dwDisplayErrorStart;
wstr = ( WCHAR* )( p + 1 * 4 + ( 16 * 2 + 2 * 4 ) + 5 * 4 );
unicode = FALSE;
break;
case IMEID_CHT_VER51: // 5.1.x.x // IME2002(w/OfficeXP)
case IMEID_CHT_VER52: // 5.2.x.x // (w/whistler)
case IMEID_CHS_VER53: // 5.3.x.x // SCIME2k or MSPY3 (w/OfficeXP and Whistler)
p = *( LPBYTE* )( ( LPBYTE )_ImmLockIMCC( lpIMC->hPrivate ) + 4 ); // PCKeyCtrlManager
if( !p ) break;
p = *( LPBYTE* )( ( LPBYTE )p + 1 * 4 + 5 * 4 ); // = PCReading = &STypingInfo
if( !p ) break;
dwlen = *( DWORD* )( p + 1 * 4 + ( 16 * 2 + 2 * 4 ) + 5 * 4 + 16 * 2 ); //m_dwDisplayStringLength;
dwerr = *( DWORD* )( p + 1 * 4 + ( 16 * 2 + 2 * 4 ) + 5 * 4 + 16 * 2 + 1 * 4 ); //m_dwDisplayErrorStart;
wstr = ( WCHAR* )( p + 1 * 4 + ( 16 * 2 + 2 * 4 ) + 5 * 4 );
unicode = TRUE;
break;
// the code tested only with Win 98 SE (MSPY 1.5/ ver 4.1.0.21)
case IMEID_CHS_VER41:
{
int offset;
offset = ( GetImeId( 1 ) >= 0x00000002 ) ? 8 : 7;
p = *( LPBYTE* )( ( LPBYTE )_ImmLockIMCC( lpIMC->hPrivate ) + offset * 4 );
if( !p ) break;
dwlen = *( DWORD* )( p + 7 * 4 + 16 * 2 * 4 );
dwerr = *( DWORD* )( p + 8 * 4 + 16 * 2 * 4 );
dwerr = std::min( dwerr, dwlen );
wstr = ( WCHAR* )( p + 6 * 4 + 16 * 2 * 1 );
unicode = TRUE;
break;
}
case IMEID_CHS_VER42: // 4.2.x.x // SCIME98 or MSPY2 (w/Office2k, Win2k, WinME, etc)
{
int nTcharSize = IsNT() ? sizeof( WCHAR ) : sizeof( char );
p = *( LPBYTE* )( ( LPBYTE )_ImmLockIMCC( lpIMC->hPrivate ) + 1 * 4 + 1 * 4 + 6 * 4 ); // = PCReading = &STypintInfo
if( !p ) break;
dwlen = *( DWORD* )( p + 1 * 4 + ( 16 * 2 + 2 * 4 ) + 5 * 4 + 16 * nTcharSize ); //m_dwDisplayStringLength;
dwerr = *( DWORD* )( p + 1 * 4 + ( 16 * 2 + 2 * 4 ) + 5 * 4 + 16 * nTcharSize + 1 * 4 ); //m_dwDisplayErrorStart;
wstr = ( WCHAR* )( p + 1 * 4 + ( 16 * 2 + 2 * 4 ) + 5 * 4 ); //m_tszDisplayString
unicode = IsNT() ? TRUE : FALSE;
}
} // switch
g_szCandidate[0][0] = 0;
g_szCandidate[1][0] = 0;
g_szCandidate[2][0] = 0;
g_szCandidate[3][0] = 0;
}
g_dwCount = dwlen;
g_dwSelection = ( DWORD )-1; // do not select any char
if( unicode )
{
int i;
for( i = 0; ( DWORD )i < dwlen; i++ ) // dwlen > 0, if known IME : yutakah
{
if( dwerr <= ( DWORD )i && g_dwSelection == ( DWORD )-1 )
{ // select error char
g_dwSelection = i;
}
g_szCandidate[i][0] = wstr[i];
g_szCandidate[i][1] = 0;
}
g_szCandidate[i][0] = 0;
}
else
{
char* p = ( char* )wstr;
int i, j;
for( i = 0, j = 0; ( DWORD )i < dwlen; i++, j++ ) // dwlen > 0, if known IME : yutakah
{
if( dwerr <= ( DWORD )i && g_dwSelection == ( DWORD )-1 )
{
g_dwSelection = ( DWORD )j;
}
MultiByteToWideChar( g_uCodePage, 0, p + i, 1 + ( _IsLeadByte( p[i] ) ? 1 : 0 ),
g_szCandidate[j], 1 );
if ( _IsLeadByte( p[i] ) )
{
i++;
}
}
g_szCandidate[j][0] = 0;
g_dwCount = j;
}
if( !_GetReadingString )
{
_ImmUnlockIMCC( lpIMC->hPrivate );
_ImmUnlockIMC( himc );
GetReadingWindowOrientation( dwId );
}
_ImmReleaseContext( hWnd, himc );
g_bReadingWindow = true;
if( g_bHorizontalReading )
{
g_iReadingError = -1;
g_szReadingString[0] = 0;
for( UINT i = 0; i < g_dwCount; i++ )
{
if( g_dwSelection == ( DWORD )i )
g_iReadingError = (int)wcslen( g_szReadingString );
LPCTSTR pszTmp = g_szCandidate[i];
wcscat_s( g_szReadingString, COUNTOF(g_szReadingString), pszTmp );
}
}
g_uCandPageSize = MAX_CANDLIST;
}
static struct
{
bool m_bCtrl;
bool m_bShift;
bool m_bAlt;
UINT m_uVk;
}
aHotKeys[] =
{
false, false, false, VK_APPS,
true, false, false, '8',
true, false, false, 'Y',
true, false, false, VK_DELETE,
true, false, false, VK_F7,
true, false, false, VK_F9,
true, false, false, VK_F10,
true, false, false, VK_F11,
true, false, false, VK_F12,
false, false, false, VK_F2,
false, false, false, VK_F3,
false, false, false, VK_F4,
false, false, false, VK_F5,
false, false, false, VK_F10,
false, true, false, VK_F6,
false, true, false, VK_F7,
false, true, false, VK_F8,
true, true, false, VK_F10,
true, true, false, VK_F11,
true, false, false, VK_CONVERT,
true, false, false, VK_SPACE,
true, false, true, 0xbc, // Alt + Ctrl + ',': SW keyboard for Trad. Chinese IME
true, false, false, VK_TAB, // ATOK2005's Ctrl+TAB
};
//
// Ignores specific keys when IME is on. Returns true if the message is a hot key to ignore.
// - Caller doesn't have to check whether IME is on.
// - This function must be called before TranslateMessage() is called.
//
bool ImeUi_IgnoreHotKey( _In_ const MSG* pmsg )
{
if( !g_bInitialized || !pmsg )
return false;
if( pmsg->wParam == VK_PROCESSKEY && ( pmsg->message == WM_KEYDOWN || pmsg->message == WM_SYSKEYDOWN ) )
{
bool bCtrl, bShift, bAlt;
UINT uVkReal = _ImmGetVirtualKey( pmsg->hwnd );
// special case #1 - VK_JUNJA toggles half/full width input mode in Korean IME.
// This VK (sent by Alt+'=' combo) is ignored regardless of the modifier state.
if( uVkReal == VK_JUNJA )
{
return true;
}
// special case #2 - disable right arrow key that switches the candidate list to expanded mode in CHT IME.
if( uVkReal == VK_RIGHT && g_bCandList && GETLANG() == LANG_CHT )
{
return true;
}
#ifndef ENABLE_HANJA_KEY
// special case #3 - we disable VK_HANJA key because 1. some Korean fonts don't Hanja and 2. to reduce testing cost.
if( uVkReal == VK_HANJA && GETPRIMLANG() == LANG_KOREAN )
{
return true;
}
#endif
bCtrl = ( GetKeyState( VK_CONTROL ) & 0x8000 ) ? true : false;
bShift = ( GetKeyState( VK_SHIFT ) & 0x8000 ) ? true : false;
bAlt = ( GetKeyState( VK_MENU ) & 0x8000 ) ? true : false;
for( int i = 0; i < COUNTOF(aHotKeys); i++ )
{
if( aHotKeys[i].m_bCtrl == bCtrl &&
aHotKeys[i].m_bShift == bShift &&
aHotKeys[i].m_bAlt == bAlt &&
aHotKeys[i].m_uVk == uVkReal )
return true;
}
}
return false;
}
void ImeUi_FinalizeString( _In_ bool bSend )
{
HIMC himc;
static bool bProcessing = false; // to avoid infinite recursion
if( !g_bInitialized || bProcessing )
return;
himc = _ImmGetContext( g_hwndCurr );
if ( !himc )
return;
bProcessing = true;
if( g_dwIMELevel == 2 && bSend )
{
// Send composition string to app.
LONG lRet = (int)wcslen( g_szCompositionString );
assert( lRet >= 2 );
// In case of CHT IME, don't send the trailing double byte space, if it exists.
if ( GETLANG() == LANG_CHT && (lRet >= 1)
&& g_szCompositionString[lRet - 1] == 0x3000 )
{
lRet--;
}
_SendCompString();
}
InitCompStringData();
// clear composition string in IME
_ImmNotifyIME( himc, NI_COMPOSITIONSTR, CPS_CANCEL, 0 );
if( g_bUILessMode )
{
// For some reason ImmNotifyIME doesn't work on DaYi and Array CHT IMEs. Cancel composition string by setting zero-length string.
ImmSetCompositionString( himc, SCS_SETSTR, const_cast<wchar_t*>(L""), sizeof(wchar_t), const_cast<char*>(""), sizeof(wchar_t) );
}
// the following line is necessary as Korean IME doesn't close cand list when comp string is cancelled.
_ImmNotifyIME( himc, NI_CLOSECANDIDATE, 0, 0 );
_ImmReleaseContext( g_hwndCurr, himc );
// Zooty2 RAID #4759: Sometimes application doesn't receive IMN_CLOSECANDIDATE on Alt+Tab
// So the same code for IMN_CLOSECANDIDATE is replicated here.
CloseCandidateList();
bProcessing = false;
return;
}
static void SetCompStringColor()
{
// change color setting according to current IME level.
DWORD dwTranslucency = ( g_dwIMELevel == 2 ) ? 0xff000000 : ( ( DWORD )gSkinIME.compTranslucence << 24 );
gSkinCompStr.colorInput = dwTranslucency | gSkinIME.compColorInput;
gSkinCompStr.colorTargetConv = dwTranslucency | gSkinIME.compColorTargetConv;
gSkinCompStr.colorConverted = dwTranslucency | gSkinIME.compColorConverted;
gSkinCompStr.colorTargetNotConv = dwTranslucency | gSkinIME.compColorTargetNotConv;
gSkinCompStr.colorInputErr = dwTranslucency | gSkinIME.compColorInputErr;
}
static void SetSupportLevel( _In_ DWORD dwImeLevel )
{
if( dwImeLevel < 2 || 3 < dwImeLevel )
return;
if( GETPRIMLANG() == LANG_KOREAN )
{
dwImeLevel = 3;
}
g_dwIMELevel = dwImeLevel;
// cancel current composition string.
ImeUi_FinalizeString();
SetCompStringColor();
}
void ImeUi_SetSupportLevel( _In_ DWORD dwImeLevel )
{
if( !g_bInitialized )
return;
g_dwIMELevelSaved = dwImeLevel;
SetSupportLevel( dwImeLevel );
}
void ImeUi_SetAppearance( _In_opt_ const IMEUI_APPEARANCE* pia )
{
if( !g_bInitialized || !pia )
return;
gSkinIME = *pia;
gSkinIME.symbolColor &= 0xffffff; // mask translucency
gSkinIME.symbolColorOff &= 0xffffff; // mask translucency
gSkinIME.symbolColorText &= 0xffffff; // mask translucency
gSkinIME.compColorInput &= 0xffffff; // mask translucency
gSkinIME.compColorTargetConv &= 0xffffff; // mask translucency
gSkinIME.compColorConverted &= 0xffffff; // mask translucency
gSkinIME.compColorTargetNotConv &= 0xffffff; // mask translucency
gSkinIME.compColorInputErr &= 0xffffff; // mask translucency
SetCompStringColor();
}
void ImeUi_GetAppearance( _Out_opt_ IMEUI_APPEARANCE* pia )
{
if ( pia )
{
if ( g_bInitialized )
{
*pia = gSkinIME;
}
else
{
memset( pia, 0, sizeof(IMEUI_APPEARANCE) );
}
}
}
static void CheckToggleState()
{
CheckInputLocale();
// In Vista, we have to use TSF since few IMM functions don't work as expected.
// WARNING: Because of timing, g_dwState and g_bChineseIME may not be updated
// immediately after the change on IME states by user.
if( g_bUILessMode )
{
return;
}
bool bIme = _ImmIsIME( g_hklCurrent ) != 0
&& ( ( 0xF0000000 & static_cast<DWORD>( reinterpret_cast<UINT_PTR>( g_hklCurrent ) ) ) == 0xE0000000 ); // Hack to detect IME correctly. When IME is running as TIP, ImmIsIME() returns true for CHT US keyboard.
g_bChineseIME = ( GETPRIMLANG() == LANG_CHINESE ) && bIme;
HIMC himc = _ImmGetContext( g_hwndCurr );
if( himc )
{
if( g_bChineseIME )
{
DWORD dwConvMode, dwSentMode;
_ImmGetConversionStatus( himc, &dwConvMode, &dwSentMode );
g_dwState = ( dwConvMode & IME_CMODE_NATIVE ) ? IMEUI_STATE_ON : IMEUI_STATE_ENGLISH;
}
else
{
g_dwState = ( bIme && _ImmGetOpenStatus( himc ) != 0 ) ? IMEUI_STATE_ON : IMEUI_STATE_OFF;
}
_ImmReleaseContext( g_hwndCurr, himc );
}
else
g_dwState = IMEUI_STATE_OFF;
}
void ImeUi_SetInsertMode( _In_ bool bInsert )
{
if( !g_bInitialized )
return;
g_bInsertMode = bInsert;
}
bool ImeUi_GetCaretStatus()
{
return !g_bInitialized || !g_szCompositionString[0];
}
void ImeUi_SetScreenDimension( _In_ UINT width, _In_ UINT height )
{
if( !g_bInitialized )
return;
g_screenWidth = width;
g_screenHeight = height;
}
// this function is used only in brief time in CHT IME handling, so accelerator isn't processed.
static void _PumpMessage()
{
MSG msg;
while( PeekMessageA( &msg, nullptr, 0, 0, PM_NOREMOVE ) )
{
if( !GetMessageA( &msg, nullptr, 0, 0 ) )
{
PostQuitMessage( msg.wParam );
return;
}
// if (0 == TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
TranslateMessage( &msg );
DispatchMessageA( &msg );
// }
}
}
static void GetReadingWindowOrientation( _In_ DWORD dwId )
{
g_bHorizontalReading = ( g_hklCurrent == _CHS_HKL ) || ( g_hklCurrent == _CHT_HKL_NEW_CHANG_JIE ) || ( dwId == 0 );
if( !g_bHorizontalReading && IMEID_LANG( dwId ) == LANG_CHT )
{
char szRegPath[MAX_PATH];
HKEY hkey;
DWORD dwVer = IMEID_VER( dwId );
strcpy_s( szRegPath, COUNTOF(szRegPath), "software\\microsoft\\windows\\currentversion\\" );
strcat_s( szRegPath, COUNTOF(szRegPath), ( dwVer >= MAKEIMEVERSION(5, 1) ) ? "MSTCIPH" : "TINTLGNT" );
LONG lRc = RegOpenKeyExA( HKEY_CURRENT_USER, szRegPath, 0, KEY_READ, &hkey );
if( lRc == ERROR_SUCCESS )
{
DWORD dwSize = sizeof( DWORD ), dwMapping, dwType;
lRc = RegQueryValueExA( hkey, "keyboard mapping", nullptr, &dwType, ( PBYTE )&dwMapping, &dwSize );
if( lRc == ERROR_SUCCESS )
{
if(
( dwVer <= MAKEIMEVERSION( 5, 0 ) &&
( ( BYTE )dwMapping == 0x22 || ( BYTE )dwMapping == 0x23 )
) ||
( ( dwVer == MAKEIMEVERSION( 5, 1 ) || dwVer == MAKEIMEVERSION( 5, 2 ) ) &&
( ( BYTE )dwMapping >= 0x22 && ( BYTE )dwMapping <= 0x24 )
)
)
{
g_bHorizontalReading = true;
}
}
RegCloseKey( hkey );
}
}
}
void ImeUi_ToggleLanguageBar( _In_ BOOL bRestore )
{
static BOOL prevRestore = TRUE;
bool bCheck = ( prevRestore == TRUE || bRestore == TRUE );
prevRestore = bRestore;
if( !bCheck )
return;
static int iShowStatusWindow = -1;
if( iShowStatusWindow == -1 )
{
iShowStatusWindow = IsNT() && g_osi.dwMajorVersion >= 5 &&
( g_osi.dwMinorVersion > 1 || ( g_osi.dwMinorVersion == 1 && strlen( g_osi.szCSDVersion ) ) ) ? 1 : 0;
}
HWND hwndImeDef = _ImmGetDefaultIMEWnd( g_hwndCurr );
if( hwndImeDef && bRestore && iShowStatusWindow )
SendMessageA( hwndImeDef, WM_IME_CONTROL, IMC_OPENSTATUSWINDOW, 0 );
HRESULT hr;
hr = CoInitialize( nullptr );
if( SUCCEEDED( hr ) )
{
ITfLangBarMgr* plbm = nullptr;
hr = CoCreateInstance( CLSID_TF_LangBarMgr, nullptr, CLSCTX_INPROC_SERVER, __uuidof( ITfLangBarMgr ),
( void** )&plbm );
if( SUCCEEDED( hr ) && plbm )
{
DWORD dwCur;
ULONG uRc;
if( SUCCEEDED( hr ) )
{
if( bRestore )
{
if( g_dwPrevFloat )
hr = plbm->ShowFloating( g_dwPrevFloat );
}
else
{
hr = plbm->GetShowFloatingStatus( &dwCur );
if( SUCCEEDED( hr ) )
g_dwPrevFloat = dwCur;
if( !( g_dwPrevFloat & TF_SFT_DESKBAND ) )
{
hr = plbm->ShowFloating( TF_SFT_HIDDEN );
}
}
}
uRc = plbm->Release();
}
CoUninitialize();
}
if( hwndImeDef && !bRestore )
{
// The following OPENSTATUSWINDOW is required to hide ATOK16 toolbar (FS9:#7546)
SendMessageA( hwndImeDef, WM_IME_CONTROL, IMC_OPENSTATUSWINDOW, 0 );
SendMessageA( hwndImeDef, WM_IME_CONTROL, IMC_CLOSESTATUSWINDOW, 0 );
}
}
bool ImeUi_IsSendingKeyMessage()
{
return bIsSendingKeyMessage;
}
static void OnInputLangChangeWorker()
{
if( !g_bUILessMode )
{
g_iCandListIndexBase = ( g_hklCurrent == _CHT_HKL_DAYI ) ? 0 : 1;
}
SetImeApi();
}
static void OnInputLangChange()
{
UINT uLang = GETPRIMLANG();
CheckToggleState();
OnInputLangChangeWorker();
if( uLang != GETPRIMLANG() )
{
// Korean IME always uses level 3 support.
// Other languages use the level that is specified by ImeUi_SetSupportLevel()
SetSupportLevel( ( GETPRIMLANG() == LANG_KOREAN ) ? 3 : g_dwIMELevelSaved );
}
HWND hwndImeDef = _ImmGetDefaultIMEWnd( g_hwndCurr );
if( hwndImeDef )
{
// Fix for Zooty #3995: prevent CHT IME toobar from showing up
SendMessageA( hwndImeDef, WM_IME_CONTROL, IMC_OPENSTATUSWINDOW, 0 );
SendMessageA( hwndImeDef, WM_IME_CONTROL, IMC_CLOSESTATUSWINDOW, 0 );
}
}
static void SetImeApi()
{
_GetReadingString = nullptr;
_ShowReadingWindow = nullptr;
if( g_bUILessMode )
return;
char szImeFile[MAX_PATH + 1];
HKL kl = g_hklCurrent;
if( _ImmGetIMEFileNameA( kl, szImeFile, sizeof( szImeFile ) - 1 ) <= 0 )
return;
HMODULE hIme = LoadLibraryExA( szImeFile, nullptr, 0x00000800 /* LOAD_LIBRARY_SEARCH_SYSTEM32 */ );
if( !hIme )
return;
_GetReadingString = reinterpret_cast<UINT ( WINAPI* )( HIMC, UINT, LPWSTR, PINT, BOOL*, PUINT )>( reinterpret_cast<void*>( GetProcAddress( hIme, "GetReadingString" ) ) );
_ShowReadingWindow = reinterpret_cast<BOOL ( WINAPI* )( HIMC himc, BOOL )>( reinterpret_cast<void*>( GetProcAddress( hIme, "ShowReadingWindow" ) ) );
if( _ShowReadingWindow )
{
HIMC himc = _ImmGetContext( g_hwndCurr );
if( himc )
{
_ShowReadingWindow( himc, false );
_ImmReleaseContext( g_hwndCurr, himc );
}
}
}
static void CheckInputLocale()
{
static HKL hklPrev = 0;
g_hklCurrent = GetKeyboardLayout( 0 );
if( hklPrev == g_hklCurrent )
{
return;
}
hklPrev = g_hklCurrent;
switch( GETPRIMLANG() )
{
// Simplified Chinese
case LANG_CHINESE:
g_bVerticalCand = true;
switch( GETSUBLANG() )
{
case SUBLANG_CHINESE_SIMPLIFIED:
g_pszIndicatior = g_aszIndicator[INDICATOR_CHS];
//g_bVerticalCand = GetImeId() == 0;
g_bVerticalCand = false;
break;
case SUBLANG_CHINESE_TRADITIONAL:
g_pszIndicatior = g_aszIndicator[INDICATOR_CHT];
break;
default: // unsupported sub-language
g_pszIndicatior = g_aszIndicator[INDICATOR_NON_IME];
break;
}
break;
// Korean
case LANG_KOREAN:
g_pszIndicatior = g_aszIndicator[INDICATOR_KOREAN];
g_bVerticalCand = false;
break;
// Japanese
case LANG_JAPANESE:
g_pszIndicatior = g_aszIndicator[INDICATOR_JAPANESE];
g_bVerticalCand = true;
break;
default:
g_pszIndicatior = g_aszIndicator[INDICATOR_NON_IME];
}
char szCodePage[8];
(void)GetLocaleInfoA( MAKELCID( GETLANG(), SORT_DEFAULT ), LOCALE_IDEFAULTANSICODEPAGE, szCodePage,
COUNTOF( szCodePage ) );
g_uCodePage = _strtoul( szCodePage, nullptr, 0 );
for( int i = 0; i < 256; i++ )
{
LeadByteTable[i] = ( BYTE )IsDBCSLeadByteEx( g_uCodePage, ( BYTE )i );
}
}
void ImeUi_SetWindow( _In_ HWND hwnd )
{
g_hwndCurr = hwnd;
g_disableCicero.DisableCiceroOnThisWnd( hwnd );
}
UINT ImeUi_GetInputCodePage()
{
return g_uCodePage;
}
DWORD ImeUi_GetFlags()
{
return g_dwImeUiFlags;
}
void ImeUi_SetFlags( _In_ DWORD dwFlags, _In_ bool bSet )
{
if( bSet )
{
g_dwImeUiFlags |= dwFlags;
}
else
{
g_dwImeUiFlags &= ~dwFlags;
}
}
///////////////////////////////////////////////////////////////////////////////
//
// CTsfUiLessMode methods
//
///////////////////////////////////////////////////////////////////////////////
//
// SetupSinks()
// Set up sinks. A sink is used to receive a Text Service Framework event.
// CUIElementSink implements multiple sink interfaces to receive few different TSF events.
//
BOOL CTsfUiLessMode::SetupSinks()
{
// ITfThreadMgrEx is available on Vista or later.
HRESULT hr;
hr = CoCreateInstance( CLSID_TF_ThreadMgr,
nullptr,
CLSCTX_INPROC_SERVER,
__uuidof( ITfThreadMgrEx ),
( void** )&m_tm );
if( hr != S_OK )
{
return FALSE;
}
// ready to start interacting
TfClientId cid; // not used
if( FAILED( m_tm->ActivateEx( &cid, TF_TMAE_UIELEMENTENABLEDONLY ) ) )
{
return FALSE;
}
// Setup sinks
BOOL bRc = FALSE;
m_TsfSink = new (std::nothrow) CUIElementSink();
if( m_TsfSink )
{
ITfSource* srcTm;
if( SUCCEEDED( hr = m_tm->QueryInterface( __uuidof( ITfSource ), ( void** )&srcTm ) ) )
{
// Sink for reading window change
if( SUCCEEDED( hr = srcTm->AdviseSink( __uuidof( ITfUIElementSink ), ( ITfUIElementSink* )m_TsfSink,
&m_dwUIElementSinkCookie ) ) )
{
// Sink for input locale change
if( SUCCEEDED( hr = srcTm->AdviseSink( __uuidof( ITfInputProcessorProfileActivationSink ),
( ITfInputProcessorProfileActivationSink* )m_TsfSink,
&m_dwAlpnSinkCookie ) ) )
{
if( SetupCompartmentSinks() ) // Setup compartment sinks for the first time
{
bRc = TRUE;
}
}
}
srcTm->Release();
}
}
return bRc;
}
void CTsfUiLessMode::ReleaseSinks()
{
HRESULT hr;
ITfSource* source;
// Remove all sinks
if( m_tm && SUCCEEDED( m_tm->QueryInterface( __uuidof( ITfSource ), ( void** )&source ) ) )
{
hr = source->UnadviseSink( m_dwUIElementSinkCookie );
hr = source->UnadviseSink( m_dwAlpnSinkCookie );
source->Release();
SetupCompartmentSinks( TRUE ); // Remove all compartment sinks
m_tm->Deactivate();
SAFE_RELEASE( m_tm );
SAFE_RELEASE( m_TsfSink );
}
}
CTsfUiLessMode::CUIElementSink::CUIElementSink()
{
_cRef = 1;
}
CTsfUiLessMode::CUIElementSink::~CUIElementSink()
{
}
STDAPI CTsfUiLessMode::CUIElementSink::QueryInterface( _In_ REFIID riid, _COM_Outptr_ void** ppvObj )
{
if( !ppvObj )
return E_INVALIDARG;
*ppvObj = nullptr;
if( IsEqualIID( riid, IID_IUnknown ) )
{
*ppvObj = static_cast<IUnknown*>( static_cast<ITfUIElementSink*>( this ) );
}
else if( IsEqualIID( riid, __uuidof( ITfUIElementSink ) ) )
{
*ppvObj = ( ITfUIElementSink* )this;
}
else if( IsEqualIID( riid, __uuidof( ITfInputProcessorProfileActivationSink ) ) )
{
*ppvObj = ( ITfInputProcessorProfileActivationSink* )this;
}
else if( IsEqualIID( riid, __uuidof( ITfCompartmentEventSink ) ) )
{
*ppvObj = ( ITfCompartmentEventSink* )this;
}
if( *ppvObj )
{
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
STDAPI_( ULONG )
CTsfUiLessMode::CUIElementSink::AddRef()
{
return ++_cRef;
}
STDAPI_( ULONG )
CTsfUiLessMode::CUIElementSink::Release()
{
LONG cr = --_cRef;
if( _cRef == 0 )
{
delete this;
}
return cr;
}
STDAPI CTsfUiLessMode::CUIElementSink::BeginUIElement( DWORD dwUIElementId, BOOL* pbShow )
{
auto pElement = GetUIElement( dwUIElementId );
if( !pElement )
return E_INVALIDARG;
ITfReadingInformationUIElement* preading = nullptr;
ITfCandidateListUIElement* pcandidate = nullptr;
*pbShow = FALSE;
if( !g_bCandList && SUCCEEDED( pElement->QueryInterface( __uuidof( ITfReadingInformationUIElement ),
( void** )&preading ) ) )
{
MakeReadingInformationString( preading );
preading->Release();
}
else if( SUCCEEDED( pElement->QueryInterface( __uuidof( ITfCandidateListUIElement ),
( void** )&pcandidate ) ) )
{
m_nCandidateRefCount++;
MakeCandidateStrings( pcandidate );
pcandidate->Release();
}
pElement->Release();
return S_OK;
}
STDAPI CTsfUiLessMode::CUIElementSink::UpdateUIElement( DWORD dwUIElementId )
{
auto pElement = GetUIElement( dwUIElementId );
if( !pElement )
return E_INVALIDARG;
ITfReadingInformationUIElement* preading = nullptr;
ITfCandidateListUIElement* pcandidate = nullptr;
if( !g_bCandList && SUCCEEDED( pElement->QueryInterface( __uuidof( ITfReadingInformationUIElement ),
( void** )&preading ) ) )
{
MakeReadingInformationString( preading );
preading->Release();
}
else if( SUCCEEDED( pElement->QueryInterface( __uuidof( ITfCandidateListUIElement ),
( void** )&pcandidate ) ) )
{
MakeCandidateStrings( pcandidate );
pcandidate->Release();
}
pElement->Release();
return S_OK;
}
STDAPI CTsfUiLessMode::CUIElementSink::EndUIElement( DWORD dwUIElementId )
{
auto pElement = GetUIElement( dwUIElementId );
if( !pElement )
return E_INVALIDARG;
ITfReadingInformationUIElement* preading = nullptr;
if( !g_bCandList && SUCCEEDED( pElement->QueryInterface( __uuidof( ITfReadingInformationUIElement ),
( void** )&preading ) ) )
{
g_dwCount = 0;
preading->Release();
}
ITfCandidateListUIElement* pcandidate = nullptr;
if( SUCCEEDED( pElement->QueryInterface( __uuidof( ITfCandidateListUIElement ),
( void** )&pcandidate ) ) )
{
m_nCandidateRefCount--;
if( m_nCandidateRefCount == 0 )
CloseCandidateList();
pcandidate->Release();
}
pElement->Release();
return S_OK;
}
void CTsfUiLessMode::UpdateImeState( BOOL bResetCompartmentEventSink )
{
ITfCompartmentMgr* pcm;
ITfCompartment* pTfOpenMode = nullptr;
ITfCompartment* pTfConvMode = nullptr;
if( GetCompartments( &pcm, &pTfOpenMode, &pTfConvMode ) )
{
VARIANT valOpenMode;
if ( SUCCEEDED(pTfOpenMode->GetValue(&valOpenMode)) )
{
VARIANT valConvMode;
if (SUCCEEDED(pTfConvMode->GetValue(&valConvMode)))
{
if (valOpenMode.vt == VT_I4)
{
if (g_bChineseIME)
{
g_dwState = valOpenMode.lVal != 0 && valConvMode.lVal != 0 ? IMEUI_STATE_ON : IMEUI_STATE_ENGLISH;
}
else
{
g_dwState = valOpenMode.lVal != 0 ? IMEUI_STATE_ON : IMEUI_STATE_OFF;
}
}
VariantClear(&valConvMode);
}
VariantClear(&valOpenMode);
}
if( bResetCompartmentEventSink )
{
SetupCompartmentSinks( FALSE, pTfOpenMode, pTfConvMode ); // Reset compartment sinks
}
pTfOpenMode->Release();
pTfConvMode->Release();
pcm->Release();
}
}
STDAPI CTsfUiLessMode::CUIElementSink::OnActivated( DWORD dwProfileType, LANGID langid, _In_ REFCLSID clsid, _In_ REFGUID catid,
_In_ REFGUID guidProfile, HKL hkl, DWORD dwFlags )
{
UNREFERENCED_PARAMETER(clsid);
UNREFERENCED_PARAMETER(hkl);
static GUID s_TF_PROFILE_DAYI =
{
0x037B2C25, 0x480C, 0x4D7F, 0xB0, 0x27, 0xD6, 0xCA, 0x6B, 0x69, 0x78, 0x8A
};
g_iCandListIndexBase = IsEqualGUID( s_TF_PROFILE_DAYI, guidProfile ) ? 0 : 1;
if( IsEqualIID( catid, GUID_TFCAT_TIP_KEYBOARD ) && ( dwFlags & TF_IPSINK_FLAG_ACTIVE ) )
{
g_bChineseIME = ( dwProfileType & TF_PROFILETYPE_INPUTPROCESSOR ) && langid == LANG_CHT;
if( dwProfileType & TF_PROFILETYPE_INPUTPROCESSOR )
{
UpdateImeState( TRUE );
}
else
g_dwState = IMEUI_STATE_OFF;
OnInputLangChange();
}
return S_OK;
}
STDAPI CTsfUiLessMode::CUIElementSink::OnChange( _In_ REFGUID rguid )
{
UNREFERENCED_PARAMETER(rguid);
UpdateImeState();
return S_OK;
}
void CTsfUiLessMode::MakeReadingInformationString( ITfReadingInformationUIElement* preading )
{
UINT cchMax;
UINT uErrorIndex = 0;
BOOL fVertical;
DWORD dwFlags;
preading->GetUpdatedFlags( &dwFlags );
preading->GetMaxReadingStringLength( &cchMax );
preading->GetErrorIndex( &uErrorIndex ); // errorIndex is zero-based
preading->IsVerticalOrderPreferred( &fVertical );
g_iReadingError = ( int )uErrorIndex;
g_bHorizontalReading = !fVertical;
g_bReadingWindow = true;
g_uCandPageSize = MAX_CANDLIST;
g_dwSelection = g_iReadingError ? g_iReadingError - 1 : ( DWORD )-1;
g_iReadingError--; // g_iReadingError is used only in horizontal window, and has to be -1 if there's no error.
BSTR bstr;
if( SUCCEEDED( preading->GetString( &bstr ) ) )
{
if( bstr )
{
wcscpy_s( g_szReadingString, COUNTOF(g_szReadingString), bstr );
g_dwCount = cchMax;
LPCTSTR pszSource = g_szReadingString;
if( fVertical )
{
// for vertical reading window, copy each character to g_szCandidate array.
for( UINT i = 0; i < cchMax; i++ )
{
LPTSTR pszDest = g_szCandidate[i];
if( *pszSource )
{
LPTSTR pszNextSrc = CharNext( pszSource );
SIZE_T size = ( LPSTR )pszNextSrc - ( LPSTR )pszSource;
memcpy( pszDest, pszSource, size );
pszSource = pszNextSrc;
pszDest += size;
}
*pszDest = 0;
}
}
else
{
g_szCandidate[0][0] = TEXT( ' ' ); // hack to make rendering happen
}
SysFreeString( bstr );
}
}
}
void CTsfUiLessMode::MakeCandidateStrings( ITfCandidateListUIElement* pcandidate )
{
UINT uIndex = 0;
UINT uCount = 0;
UINT uCurrentPage = 0;
UINT* IndexList = nullptr;
UINT uPageCnt = 0;
DWORD dwPageStart = 0;
DWORD dwPageSize = 0;
BSTR bstr;
pcandidate->GetSelection( &uIndex );
pcandidate->GetCount( &uCount );
pcandidate->GetCurrentPage( &uCurrentPage );
g_dwSelection = ( DWORD )uIndex;
g_dwCount = ( DWORD )uCount;
g_bCandList = true;
g_bReadingWindow = false;
pcandidate->GetPageIndex( nullptr, 0, &uPageCnt );
if( uPageCnt > 0 )
{
IndexList = ( UINT* )ImeUiCallback_Malloc( sizeof( UINT ) * uPageCnt );
if( IndexList )
{
pcandidate->GetPageIndex( IndexList, uPageCnt, &uPageCnt );
dwPageStart = IndexList[uCurrentPage];
dwPageSize = ( uCurrentPage < uPageCnt - 1 ) ?
std::min( uCount, IndexList[uCurrentPage + 1] ) - dwPageStart:
uCount - dwPageStart;
}
}
g_uCandPageSize = std::min<UINT>( dwPageSize, MAX_CANDLIST );
g_dwSelection = g_dwSelection - dwPageStart;
memset( &g_szCandidate, 0, sizeof( g_szCandidate ) );
for( UINT i = dwPageStart, j = 0; ( DWORD )i < g_dwCount && j < g_uCandPageSize; i++, j++ )
{
if( SUCCEEDED( pcandidate->GetString( i, &bstr ) ) )
{
if( bstr )
{
ComposeCandidateLine( j, bstr );
SysFreeString( bstr );
}
}
}
if( GETPRIMLANG() == LANG_KOREAN )
{
g_dwSelection = ( DWORD )-1;
}
if( IndexList )
{
ImeUiCallback_Free( IndexList );
}
}
ITfUIElement* CTsfUiLessMode::GetUIElement( DWORD dwUIElementId )
{
ITfUIElementMgr* puiem;
ITfUIElement* pElement = nullptr;
if( SUCCEEDED( m_tm->QueryInterface( __uuidof( ITfUIElementMgr ), ( void** )&puiem ) ) )
{
puiem->GetUIElement( dwUIElementId, &pElement );
puiem->Release();
}
return pElement;
}
BOOL CTsfUiLessMode::CurrentInputLocaleIsIme()
{
BOOL ret = FALSE;
HRESULT hr;
ITfInputProcessorProfiles* pProfiles;
hr = CoCreateInstance( CLSID_TF_InputProcessorProfiles, nullptr, CLSCTX_INPROC_SERVER,
__uuidof( ITfInputProcessorProfiles ), ( LPVOID* )&pProfiles );
if( SUCCEEDED( hr ) )
{
ITfInputProcessorProfileMgr* pProfileMgr;
hr = pProfiles->QueryInterface( __uuidof( ITfInputProcessorProfileMgr ), ( LPVOID* )&pProfileMgr );
if( SUCCEEDED( hr ) )
{
TF_INPUTPROCESSORPROFILE tip;
hr = pProfileMgr->GetActiveProfile( GUID_TFCAT_TIP_KEYBOARD, &tip );
if( SUCCEEDED( hr ) )
{
ret = ( tip.dwProfileType & TF_PROFILETYPE_INPUTPROCESSOR ) != 0;
}
pProfileMgr->Release();
}
pProfiles->Release();
}
return ret;
}
// Sets up or removes sink for UI element.
// UI element sink should be removed when IME is disabled,
// otherwise the sink can be triggered when a game has multiple instances of IME UI library.
void CTsfUiLessMode::EnableUiUpdates( bool bEnable )
{
if( !m_tm ||
( bEnable && m_dwUIElementSinkCookie != TF_INVALID_COOKIE ) ||
( !bEnable && m_dwUIElementSinkCookie == TF_INVALID_COOKIE ) )
{
return;
}
ITfSource* srcTm = nullptr;
HRESULT hr = E_FAIL;
if( SUCCEEDED( hr = m_tm->QueryInterface( __uuidof( ITfSource ), ( void** )&srcTm ) ) )
{
if( bEnable )
{
hr = srcTm->AdviseSink( __uuidof( ITfUIElementSink ), ( ITfUIElementSink* )m_TsfSink,
&m_dwUIElementSinkCookie );
}
else
{
hr = srcTm->UnadviseSink( m_dwUIElementSinkCookie );
m_dwUIElementSinkCookie = TF_INVALID_COOKIE;
}
srcTm->Release();
}
}
// Returns open mode compartments and compartment manager.
// Function fails if it fails to acquire any of the objects to be returned.
BOOL CTsfUiLessMode::GetCompartments( ITfCompartmentMgr** ppcm, ITfCompartment** ppTfOpenMode,
ITfCompartment** ppTfConvMode )
{
ITfCompartmentMgr* pcm = nullptr;
ITfCompartment* pTfOpenMode = nullptr;
ITfCompartment* pTfConvMode = nullptr;
static GUID _GUID_COMPARTMENT_KEYBOARD_INPUTMODE_CONVERSION =
{
0xCCF05DD8, 0x4A87, 0x11D7, 0xA6, 0xE2, 0x00, 0x06, 0x5B, 0x84, 0x43, 0x5C
};
HRESULT hr;
if( SUCCEEDED( hr = m_tm->QueryInterface( IID_ITfCompartmentMgr, ( void** )&pcm ) ) )
{
if( SUCCEEDED( hr = pcm->GetCompartment( GUID_COMPARTMENT_KEYBOARD_OPENCLOSE, &pTfOpenMode ) ) )
{
if( SUCCEEDED( hr = pcm->GetCompartment( _GUID_COMPARTMENT_KEYBOARD_INPUTMODE_CONVERSION,
&pTfConvMode ) ) )
{
*ppcm = pcm;
*ppTfOpenMode = pTfOpenMode;
*ppTfConvMode = pTfConvMode;
return TRUE;
}
pTfOpenMode->Release();
}
pcm->Release();
}
return FALSE;
}
// There are three ways to call this function:
// SetupCompartmentSinks() : initialization
// SetupCompartmentSinks(FALSE, openmode, convmode) : Resetting sinks. This is necessary as DaYi and Array IME resets compartment on switching input locale
// SetupCompartmentSinks(TRUE) : clean up sinks
BOOL CTsfUiLessMode::SetupCompartmentSinks( BOOL bRemoveOnly, ITfCompartment* pTfOpenMode,
ITfCompartment* pTfConvMode )
{
bool bLocalCompartments = false;
ITfCompartmentMgr* pcm = nullptr;
BOOL bRc = FALSE;
HRESULT hr = E_FAIL;
if( !pTfOpenMode && !pTfConvMode )
{
bLocalCompartments = true;
GetCompartments( &pcm, &pTfOpenMode, &pTfConvMode );
}
if( !( pTfOpenMode && pTfConvMode ) )
{
// Invalid parameters or GetCompartments() has failed.
return FALSE;
}
ITfSource* srcOpenMode = nullptr;
if( SUCCEEDED( hr = pTfOpenMode->QueryInterface( IID_ITfSource, ( void** )&srcOpenMode ) ) )
{
// Remove existing sink for open mode
if( m_dwOpenModeSinkCookie != TF_INVALID_COOKIE )
{
srcOpenMode->UnadviseSink( m_dwOpenModeSinkCookie );
m_dwOpenModeSinkCookie = TF_INVALID_COOKIE;
}
// Setup sink for open mode (toggle state) change
if( bRemoveOnly || SUCCEEDED( hr = srcOpenMode->AdviseSink( IID_ITfCompartmentEventSink,
( ITfCompartmentEventSink* )m_TsfSink,
&m_dwOpenModeSinkCookie ) ) )
{
ITfSource* srcConvMode = nullptr;
if( SUCCEEDED( hr = pTfConvMode->QueryInterface( IID_ITfSource, ( void** )&srcConvMode ) ) )
{
// Remove existing sink for open mode
if( m_dwConvModeSinkCookie != TF_INVALID_COOKIE )
{
srcConvMode->UnadviseSink( m_dwConvModeSinkCookie );
m_dwConvModeSinkCookie = TF_INVALID_COOKIE;
}
// Setup sink for open mode (toggle state) change
if( bRemoveOnly || SUCCEEDED( hr = srcConvMode->AdviseSink( IID_ITfCompartmentEventSink,
( ITfCompartmentEventSink* )m_TsfSink,
&m_dwConvModeSinkCookie ) ) )
{
bRc = TRUE;
}
srcConvMode->Release();
}
}
srcOpenMode->Release();
}
if( bLocalCompartments )
{
pTfOpenMode->Release();
pTfConvMode->Release();
pcm->Release();
}
return bRc;
}
WORD ImeUi_GetPrimaryLanguage()
{
return GETPRIMLANG();
};
DWORD ImeUi_GetImeId( _In_ UINT uIndex )
{
return GetImeId( uIndex );
};
WORD ImeUi_GetLanguage()
{
return GETLANG();
};
PCTSTR ImeUi_GetIndicatior()
{
return g_pszIndicatior;
};
bool ImeUi_IsShowReadingWindow()
{
return g_bReadingWindow;
};
bool ImeUi_IsShowCandListWindow()
{
return g_bCandList;
};
bool ImeUi_IsVerticalCand()
{
return g_bVerticalCand;
};
bool ImeUi_IsHorizontalReading()
{
return g_bHorizontalReading;
};
TCHAR* ImeUi_GetCandidate( _In_ UINT idx )
{
if( idx < MAX_CANDLIST )
return g_szCandidate[idx];
else
return g_szCandidate[0];
}
DWORD ImeUi_GetCandidateSelection()
{
return g_dwSelection;
}
DWORD ImeUi_GetCandidateCount()
{
return g_dwCount;
}
TCHAR* ImeUi_GetCompositionString()
{
return g_szCompositionString;
}
BYTE* ImeUi_GetCompStringAttr()
{
return g_szCompAttrString;
}
DWORD ImeUi_GetImeCursorChars()
{
return g_IMECursorChars;
}
|