Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
I think this also fixes the bug relating to non-latin characters in filenames, since UNICODE wasn't defined in SDL_rwops.c
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2010 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
25 #define SDL_DISABLE_WINDOWS_IME
28 #include "SDL_windowsvideo.h"
30 #include "../../events/SDL_keyboard_c.h"
31 #include "../../events/scancodes_windows.h"
36 #ifndef SDL_DISABLE_WINDOWS_IME
37 static void IME_Init(SDL_VideoData *videodata, HWND hwnd);
38 static void IME_Enable(SDL_VideoData *videodata, HWND hwnd);
39 static void IME_Disable(SDL_VideoData *videodata, HWND hwnd);
40 static void IME_Quit(SDL_VideoData *videodata);
41 #endif /* !SDL_DISABLE_WINDOWS_IME */
43 #ifndef MAPVK_VK_TO_VSC
44 #define MAPVK_VK_TO_VSC 0
46 #ifndef MAPVK_VSC_TO_VK
47 #define MAPVK_VSC_TO_VK 1
49 #ifndef MAPVK_VK_TO_CHAR
50 #define MAPVK_VK_TO_CHAR 2
53 /* Alphabetic scancodes for PC keyboards */
54 BYTE alpha_scancodes[26] = {
55 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24,
56 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44
59 BYTE keypad_scancodes[10] = {
60 82, 79, 80, 81, 75, 76, 77, 71, 72, 73
64 WIN_InitKeyboard(_THIS)
66 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
69 /* Make sure the alpha scancodes are correct. T isn't usually remapped */
70 if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T' - 'A']) {
73 ("Fixing alpha scancode map, assuming US QWERTY layout!\nPlease send the following 26 lines of output to the SDL mailing list <sdl@libsdl.org>, including a description of your keyboard hardware.\n");
75 for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
76 alpha_scancodes[i] = MapVirtualKey('A' + i, MAPVK_VK_TO_VSC);
78 printf("%d = %d\n", i, alpha_scancodes[i]);
82 if (MapVirtualKey(VK_NUMPAD0, MAPVK_VK_TO_VSC) != keypad_scancodes[0]) {
85 ("Fixing keypad scancode map!\nPlease send the following 10 lines of output to the SDL mailing list <sdl@libsdl.org>, including a description of your keyboard hardware.\n");
87 for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
89 MapVirtualKey(VK_NUMPAD0 + i, MAPVK_VK_TO_VSC);
91 printf("%d = %d\n", i, keypad_scancodes[i]);
96 data->key_layout = windows_scancode_table;
98 data->ime_com_initialized = SDL_FALSE;
99 data->ime_threadmgr = 0;
100 data->ime_initialized = SDL_FALSE;
101 data->ime_enabled = SDL_FALSE;
102 data->ime_available = SDL_FALSE;
103 data->ime_hwnd_main = 0;
104 data->ime_hwnd_current = 0;
106 data->ime_composition[0] = 0;
107 data->ime_readingstring[0] = 0;
108 data->ime_cursor = 0;
110 data->ime_candlist = SDL_FALSE;
111 SDL_memset(data->ime_candidates, 0, sizeof(data->ime_candidates));
112 data->ime_candcount = 0;
113 data->ime_candref = 0;
114 data->ime_candsel = 0;
115 data->ime_candpgsize = 0;
116 data->ime_candlistindexbase = 0;
117 data->ime_candvertical = SDL_TRUE;
119 data->ime_candtex = NULL;
120 data->ime_dirty = SDL_FALSE;
121 SDL_memset(&data->ime_rect, 0, sizeof(data->ime_rect));
122 SDL_memset(&data->ime_candlistrect, 0, sizeof(data->ime_candlistrect));
123 data->ime_winwidth = 0;
124 data->ime_winheight = 0;
127 data->ime_himm32 = 0;
128 data->GetReadingString = 0;
129 data->ShowReadingWindow = 0;
130 data->ImmLockIMC = 0;
131 data->ImmUnlockIMC = 0;
132 data->ImmLockIMCC = 0;
133 data->ImmUnlockIMCC = 0;
134 data->ime_uiless = SDL_FALSE;
135 data->ime_threadmgrex = 0;
136 data->ime_uielemsinkcookie = TF_INVALID_COOKIE;
137 data->ime_alpnsinkcookie = TF_INVALID_COOKIE;
138 data->ime_openmodesinkcookie = TF_INVALID_COOKIE;
139 data->ime_convmodesinkcookie = TF_INVALID_COOKIE;
140 data->ime_uielemsink = 0;
141 data->ime_ippasink = 0;
145 SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
146 SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
147 SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");
154 SDL_ScanCode scancode;
155 SDLKey keymap[SDL_NUM_SCANCODES];
157 SDL_GetDefaultKeymap(keymap);
159 for (i = 0; i < SDL_arraysize(windows_scancode_table); i++) {
161 /* Make sure this scancode is a valid character scancode */
162 scancode = windows_scancode_table[i];
163 if (scancode == SDL_SCANCODE_UNKNOWN || keymap[scancode] >= 127) {
167 /* Alphabetic keys are handled specially, since Windows remaps them */
168 if (i >= 'A' && i <= 'Z') {
169 BYTE vsc = alpha_scancodes[i - 'A'];
170 keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
172 keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
175 SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
179 WIN_QuitKeyboard(_THIS)
181 #ifndef SDL_DISABLE_WINDOWS_IME
182 IME_Quit((SDL_VideoData *)_this->driverdata);
187 WIN_StartTextInput(_THIS)
189 #ifndef SDL_DISABLE_WINDOWS_IME
190 SDL_Window *window = SDL_GetKeyboardFocus();
192 HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
193 SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
194 SDL_GetWindowSize(window, &videodata->ime_winwidth, &videodata->ime_winheight);
195 IME_Init(videodata, hwnd);
196 IME_Enable(videodata, hwnd);
198 #endif /* !SDL_DISABLE_WINDOWS_IME */
202 WIN_StopTextInput(_THIS)
204 #ifndef SDL_DISABLE_WINDOWS_IME
205 SDL_Window *window = SDL_GetKeyboardFocus();
207 HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
208 SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
209 IME_Init(videodata, hwnd);
210 IME_Disable(videodata, hwnd);
212 #endif /* !SDL_DISABLE_WINDOWS_IME */
216 WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
218 SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
219 videodata->ime_rect = *rect;
222 #ifdef SDL_DISABLE_WINDOWS_IME
226 IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
231 void IME_Present(SDL_VideoData *videodata)
239 #define DEFINE_GUID(n,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) static const GUID n = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}
240 DEFINE_GUID(IID_ITfInputProcessorProfileActivationSink, 0x71C6E74E,0x0F28,0x11D8,0xA8,0x2A,0x00,0x06,0x5B,0x84,0x43,0x5C);
241 DEFINE_GUID(IID_ITfUIElementSink, 0xEA1EA136,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
242 DEFINE_GUID(GUID_TFCAT_TIP_KEYBOARD, 0x34745C63,0xB2F0,0x4784,0x8B,0x67,0x5E,0x12,0xC8,0x70,0x1A,0x31);
243 DEFINE_GUID(IID_ITfSource, 0x4EA48A35,0x60AE,0x446F,0x8F,0xD6,0xE6,0xA8,0xD8,0x24,0x59,0xF7);
244 DEFINE_GUID(IID_ITfUIElementMgr, 0xEA1EA135,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
245 DEFINE_GUID(IID_ITfCandidateListUIElement, 0xEA1EA138,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
246 DEFINE_GUID(IID_ITfReadingInformationUIElement, 0xEA1EA139,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
247 DEFINE_GUID(IID_ITfThreadMgr, 0xAA80E801,0x2021,0x11D2,0x93,0xE0,0x00,0x60,0xB0,0x67,0xB8,0x6E);
248 DEFINE_GUID(CLSID_TF_ThreadMgr, 0x529A9E6B,0x6587,0x4F23,0xAB,0x9E,0x9C,0x7D,0x68,0x3E,0x3C,0x50);
249 DEFINE_GUID(IID_ITfThreadMgrEx, 0x3E90ADE3,0x7594,0x4CB0,0xBB,0x58,0x69,0x62,0x8F,0x5F,0x45,0x8C);
252 #define LANG_CHT MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL)
253 #define LANG_CHS MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED)
255 #define MAKEIMEVERSION(major,minor) ((DWORD) (((BYTE)(major) << 24) | ((BYTE)(minor) << 16) ))
256 #define IMEID_VER(id) ((id) & 0xffff0000)
257 #define IMEID_LANG(id) ((id) & 0x0000ffff)
259 #define CHT_HKL_DAYI ((HKL)0xE0060404)
260 #define CHT_HKL_NEW_PHONETIC ((HKL)0xE0080404)
261 #define CHT_HKL_NEW_CHANG_JIE ((HKL)0xE0090404)
262 #define CHT_HKL_NEW_QUICK ((HKL)0xE00A0404)
263 #define CHT_HKL_HK_CANTONESE ((HKL)0xE00B0404)
264 #define CHT_IMEFILENAME1 "TINTLGNT.IME"
265 #define CHT_IMEFILENAME2 "CINTLGNT.IME"
266 #define CHT_IMEFILENAME3 "MSTCIPHA.IME"
267 #define IMEID_CHT_VER42 (LANG_CHT | MAKEIMEVERSION(4, 2))
268 #define IMEID_CHT_VER43 (LANG_CHT | MAKEIMEVERSION(4, 3))
269 #define IMEID_CHT_VER44 (LANG_CHT | MAKEIMEVERSION(4, 4))
270 #define IMEID_CHT_VER50 (LANG_CHT | MAKEIMEVERSION(5, 0))
271 #define IMEID_CHT_VER51 (LANG_CHT | MAKEIMEVERSION(5, 1))
272 #define IMEID_CHT_VER52 (LANG_CHT | MAKEIMEVERSION(5, 2))
273 #define IMEID_CHT_VER60 (LANG_CHT | MAKEIMEVERSION(6, 0))
274 #define IMEID_CHT_VER_VISTA (LANG_CHT | MAKEIMEVERSION(7, 0))
276 #define CHS_HKL ((HKL)0xE00E0804)
277 #define CHS_IMEFILENAME1 "PINTLGNT.IME"
278 #define CHS_IMEFILENAME2 "MSSCIPYA.IME"
279 #define IMEID_CHS_VER41 (LANG_CHS | MAKEIMEVERSION(4, 1))
280 #define IMEID_CHS_VER42 (LANG_CHS | MAKEIMEVERSION(4, 2))
281 #define IMEID_CHS_VER53 (LANG_CHS | MAKEIMEVERSION(5, 3))
283 #define LANG() LOWORD((videodata->ime_hkl))
284 #define PRIMLANG() ((WORD)PRIMARYLANGID(LANG()))
285 #define SUBLANG() SUBLANGID(LANG())
287 static void IME_UpdateInputLocale(SDL_VideoData *videodata);
288 static void IME_ClearComposition(SDL_VideoData *videodata);
289 static void IME_SetWindow(SDL_VideoData* videodata, HWND hwnd);
290 static void IME_SetupAPI(SDL_VideoData *videodata);
291 static DWORD IME_GetId(SDL_VideoData *videodata, UINT uIndex);
292 static void IME_SendEditingEvent(SDL_VideoData *videodata);
293 static void IME_DestroyTextures(SDL_VideoData *videodata);
295 #define SDL_IsEqualIID(riid1, riid2) SDL_IsEqualGUID(riid1, riid2)
296 #define SDL_IsEqualGUID(rguid1, rguid2) (!SDL_memcmp(rguid1, rguid2, sizeof(GUID)))
298 static SDL_bool UILess_SetupSinks(SDL_VideoData *videodata);
299 static void UILess_ReleaseSinks(SDL_VideoData *videodata);
300 static void UILess_EnableUIUpdates(SDL_VideoData *videodata);
301 static void UILess_DisableUIUpdates(SDL_VideoData *videodata);
304 IME_Init(SDL_VideoData *videodata, HWND hwnd)
306 if (videodata->ime_initialized)
309 videodata->ime_hwnd_main = hwnd;
310 if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
311 videodata->ime_com_initialized = SDL_TRUE;
312 CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, (LPVOID *)&videodata->ime_threadmgr);
314 videodata->ime_initialized = SDL_TRUE;
315 videodata->ime_himm32 = SDL_LoadObject("imm32.dll");
316 if (!videodata->ime_himm32) {
317 videodata->ime_available = SDL_FALSE;
320 videodata->ImmLockIMC = (LPINPUTCONTEXT2 (WINAPI *)(HIMC))SDL_LoadFunction(videodata->ime_himm32, "ImmLockIMC");
321 videodata->ImmUnlockIMC = (BOOL (WINAPI *)(HIMC))SDL_LoadFunction(videodata->ime_himm32, "ImmUnlockIMC");
322 videodata->ImmLockIMCC = (LPVOID (WINAPI *)(HIMCC))SDL_LoadFunction(videodata->ime_himm32, "ImmLockIMCC");
323 videodata->ImmUnlockIMCC = (BOOL (WINAPI *)(HIMCC))SDL_LoadFunction(videodata->ime_himm32, "ImmUnlockIMCC");
325 IME_SetWindow(videodata, hwnd);
326 videodata->ime_himc = ImmGetContext(hwnd);
327 ImmReleaseContext(hwnd, videodata->ime_himc);
328 if (!videodata->ime_himc) {
329 videodata->ime_available = SDL_FALSE;
330 IME_Disable(videodata, hwnd);
333 videodata->ime_available = SDL_TRUE;
334 IME_UpdateInputLocale(videodata);
335 IME_SetupAPI(videodata);
336 videodata->ime_uiless = UILess_SetupSinks(videodata);
337 IME_UpdateInputLocale(videodata);
338 IME_Disable(videodata, hwnd);
342 IME_Enable(SDL_VideoData *videodata, HWND hwnd)
344 if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
347 if (!videodata->ime_available) {
348 IME_Disable(videodata, hwnd);
351 if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
352 ImmAssociateContext(videodata->ime_hwnd_current, videodata->ime_himc);
354 videodata->ime_enabled = SDL_TRUE;
355 IME_UpdateInputLocale(videodata);
356 UILess_EnableUIUpdates(videodata);
360 IME_Disable(SDL_VideoData *videodata, HWND hwnd)
362 if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
365 IME_ClearComposition(videodata);
366 if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
367 ImmAssociateContext(videodata->ime_hwnd_current, (HIMC)0);
369 videodata->ime_enabled = SDL_FALSE;
370 UILess_DisableUIUpdates(videodata);
374 IME_Quit(SDL_VideoData *videodata)
376 if (!videodata->ime_initialized)
379 UILess_ReleaseSinks(videodata);
380 if (videodata->ime_hwnd_main)
381 ImmAssociateContext(videodata->ime_hwnd_main, videodata->ime_himc);
383 videodata->ime_hwnd_main = 0;
384 videodata->ime_himc = 0;
385 if (videodata->ime_himm32) {
386 SDL_UnloadObject(videodata->ime_himm32);
387 videodata->ime_himm32 = 0;
389 if (videodata->ime_threadmgr) {
390 videodata->ime_threadmgr->lpVtbl->Release(videodata->ime_threadmgr);
391 videodata->ime_threadmgr = 0;
393 if (videodata->ime_com_initialized) {
395 videodata->ime_com_initialized = SDL_FALSE;
397 IME_DestroyTextures(videodata);
398 videodata->ime_initialized = SDL_FALSE;
402 IME_GetReadingString(SDL_VideoData *videodata, HWND hwnd)
410 BOOL vertical = FALSE;
412 static OSVERSIONINFOA osversion = {0};
413 if (videodata->ime_uiless)
416 videodata->ime_readingstring[0] = 0;
417 if (!osversion.dwOSVersionInfoSize) {
418 osversion.dwOSVersionInfoSize = sizeof(osversion);
419 GetVersionExA(&osversion);
421 id = IME_GetId(videodata, 0);
425 himc = ImmGetContext(hwnd);
429 if (videodata->GetReadingString) {
430 len = videodata->GetReadingString(himc, 0, 0, &err, &vertical, &maxuilen);
432 if (len > SDL_arraysize(buffer))
433 len = SDL_arraysize(buffer);
435 len = videodata->GetReadingString(himc, len, s, &err, &vertical, &maxuilen);
437 SDL_wcslcpy(videodata->ime_readingstring, s, len);
440 LPINPUTCONTEXT2 lpimc = videodata->ImmLockIMC(himc);
445 case IMEID_CHT_VER42:
446 case IMEID_CHT_VER43:
447 case IMEID_CHT_VER44:
448 p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 24);
452 len = *(DWORD *)(p + 7*4 + 32*4);
453 s = (WCHAR *)(p + 56);
455 case IMEID_CHT_VER51:
456 case IMEID_CHT_VER52:
457 case IMEID_CHS_VER53:
458 p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 4);
462 p = *(LPBYTE *)((LPBYTE)p + 1*4 + 5*4);
466 len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2);
467 s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4);
469 case IMEID_CHS_VER41:
471 int offset = (IME_GetId(videodata, 1) >= 0x00000002) ? 8 : 7;
472 p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + offset * 4);
476 len = *(DWORD *)(p + 7*4 + 16*2*4);
477 s = (WCHAR *)(p + 6*4 + 16*2*1);
480 case IMEID_CHS_VER42:
481 if (osversion.dwPlatformId != VER_PLATFORM_WIN32_NT)
484 p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 1*4 + 1*4 + 6*4);
488 len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2);
489 s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4);
493 SDL_wcslcpy(videodata->ime_readingstring, s, len + 1);
495 videodata->ImmUnlockIMCC(lpimc->hPrivate);
496 videodata->ImmUnlockIMC(himc);
498 ImmReleaseContext(hwnd, himc);
499 IME_SendEditingEvent(videodata);
503 IME_InputLangChanged(SDL_VideoData *videodata)
505 UINT lang = PRIMLANG();
506 IME_UpdateInputLocale(videodata);
507 if (!videodata->ime_uiless)
508 videodata->ime_candlistindexbase = (videodata->ime_hkl == CHT_HKL_DAYI) ? 0 : 1;
510 IME_SetupAPI(videodata);
511 if (lang != PRIMLANG()) {
512 IME_ClearComposition(videodata);
517 IME_GetId(SDL_VideoData *videodata, UINT uIndex)
519 static HKL hklprev = 0;
520 static DWORD dwRet[2] = {0};
522 DWORD dwVerHandle = 0;
523 LPVOID lpVerBuffer = 0;
524 LPVOID lpVerData = 0;
529 if (uIndex >= sizeof(dwRet) / sizeof(dwRet[0]))
532 hkl = videodata->ime_hkl;
534 return dwRet[uIndex];
537 dwLang = ((DWORD)hkl & 0xffff);
538 if (videodata->ime_uiless && LANG() == LANG_CHT) {
539 dwRet[0] = IMEID_CHT_VER_VISTA;
543 if (hkl != CHT_HKL_NEW_PHONETIC
544 && hkl != CHT_HKL_NEW_CHANG_JIE
545 && hkl != CHT_HKL_NEW_QUICK
546 && hkl != CHT_HKL_HK_CANTONESE
548 dwRet[0] = dwRet[1] = 0;
549 return dwRet[uIndex];
551 if (ImmGetIMEFileNameA(hkl, szTemp, sizeof(szTemp) - 1) <= 0) {
552 dwRet[0] = dwRet[1] = 0;
553 return dwRet[uIndex];
555 if (!videodata->GetReadingString) {
556 #define LCID_INVARIANT MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT)
557 if (CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME1, -1) != 2
558 && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME2, -1) != 2
559 && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME3, -1) != 2
560 && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME1, -1) != 2
561 && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME2, -1) != 2) {
562 dwRet[0] = dwRet[1] = 0;
563 return dwRet[uIndex];
565 #undef LCID_INVARIANT
566 dwVerSize = GetFileVersionInfoSizeA(szTemp, &dwVerHandle);
568 lpVerBuffer = SDL_malloc(dwVerSize);
570 if (GetFileVersionInfoA(szTemp, dwVerHandle, dwVerSize, lpVerBuffer)) {
571 if (VerQueryValueA(lpVerBuffer, "\\", &lpVerData, &cbVerData)) {
572 #define pVerFixedInfo ((VS_FIXEDFILEINFO FAR*)lpVerData)
573 DWORD dwVer = pVerFixedInfo->dwFileVersionMS;
574 dwVer = (dwVer & 0x00ff0000) << 8 | (dwVer & 0x000000ff) << 16;
575 if (videodata->GetReadingString ||
576 dwLang == LANG_CHT && (
577 dwVer == MAKEIMEVERSION(4, 2) ||
578 dwVer == MAKEIMEVERSION(4, 3) ||
579 dwVer == MAKEIMEVERSION(4, 4) ||
580 dwVer == MAKEIMEVERSION(5, 0) ||
581 dwVer == MAKEIMEVERSION(5, 1) ||
582 dwVer == MAKEIMEVERSION(5, 2) ||
583 dwVer == MAKEIMEVERSION(6, 0))
585 dwLang == LANG_CHS && (
586 dwVer == MAKEIMEVERSION(4, 1) ||
587 dwVer == MAKEIMEVERSION(4, 2) ||
588 dwVer == MAKEIMEVERSION(5, 3))) {
589 dwRet[0] = dwVer | dwLang;
590 dwRet[1] = pVerFixedInfo->dwFileVersionLS;
591 SDL_free(lpVerBuffer);
598 SDL_free(lpVerBuffer);
601 dwRet[0] = dwRet[1] = 0;
602 return dwRet[uIndex];
606 IME_SetupAPI(SDL_VideoData *videodata)
608 char ime_file[MAX_PATH + 1];
611 videodata->GetReadingString = 0;
612 videodata->ShowReadingWindow = 0;
613 if (videodata->ime_uiless)
616 hkl = videodata->ime_hkl;
617 if (ImmGetIMEFileNameA(hkl, ime_file, sizeof(ime_file) - 1) <= 0)
620 hime = SDL_LoadObject(ime_file);
624 videodata->GetReadingString = (UINT (WINAPI *)(HIMC, UINT, LPWSTR, PINT, BOOL*, PUINT))
625 SDL_LoadFunction(hime, "GetReadingString");
626 videodata->ShowReadingWindow = (BOOL (WINAPI *)(HIMC, BOOL))
627 SDL_LoadFunction(hime, "ShowReadingWindow");
629 if (videodata->ShowReadingWindow) {
630 HIMC himc = ImmGetContext(videodata->ime_hwnd_current);
632 videodata->ShowReadingWindow(himc, FALSE);
633 ImmReleaseContext(videodata->ime_hwnd_current, himc);
639 IME_SetWindow(SDL_VideoData* videodata, HWND hwnd)
641 videodata->ime_hwnd_current = hwnd;
642 if (videodata->ime_threadmgr) {
643 struct ITfDocumentMgr *document_mgr = 0;
644 if (SUCCEEDED(videodata->ime_threadmgr->lpVtbl->AssociateFocus(videodata->ime_threadmgr, hwnd, NULL, &document_mgr))) {
646 document_mgr->lpVtbl->Release(document_mgr);
652 IME_UpdateInputLocale(SDL_VideoData *videodata)
654 static HKL hklprev = 0;
655 videodata->ime_hkl = GetKeyboardLayout(0);
656 if (hklprev == videodata->ime_hkl)
659 hklprev = videodata->ime_hkl;
660 switch (PRIMLANG()) {
662 videodata->ime_candvertical = SDL_TRUE;
663 if (SUBLANG() == SUBLANG_CHINESE_SIMPLIFIED)
664 videodata->ime_candvertical = SDL_FALSE;
668 videodata->ime_candvertical = SDL_TRUE;
671 videodata->ime_candvertical = SDL_FALSE;
677 IME_ClearComposition(SDL_VideoData *videodata)
680 if (!videodata->ime_initialized)
683 himc = ImmGetContext(videodata->ime_hwnd_current);
687 ImmNotifyIME(himc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
688 if (videodata->ime_uiless)
689 ImmSetCompositionString(himc, SCS_SETSTR, TEXT(""), sizeof(TCHAR), TEXT(""), sizeof(TCHAR));
691 ImmNotifyIME(himc, NI_CLOSECANDIDATE, 0, 0);
692 ImmReleaseContext(videodata->ime_hwnd_current, himc);
693 SDL_SendEditingText("", 0, 0);
697 IME_GetCompositionString(SDL_VideoData *videodata, HIMC himc, DWORD string)
699 LONG length = ImmGetCompositionStringW(himc, string, videodata->ime_composition, sizeof(videodata->ime_composition));
703 length /= sizeof(videodata->ime_composition[0]);
704 videodata->ime_cursor = LOWORD(ImmGetCompositionStringW(himc, GCS_CURSORPOS, 0, 0));
705 if (videodata->ime_composition[videodata->ime_cursor] == 0x3000) {
707 for (i = videodata->ime_cursor + 1; i < length; ++i)
708 videodata->ime_composition[i - 1] = videodata->ime_composition[i];
712 videodata->ime_composition[length] = 0;
716 IME_SendInputEvent(SDL_VideoData *videodata)
719 s = WIN_StringToUTF8(videodata->ime_composition);
720 SDL_SendKeyboardText(s);
723 videodata->ime_composition[0] = 0;
724 videodata->ime_readingstring[0] = 0;
725 videodata->ime_cursor = 0;
729 IME_SendEditingEvent(SDL_VideoData *videodata)
732 WCHAR buffer[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
734 if (videodata->ime_readingstring[0]) {
735 size_t len = SDL_min(SDL_wcslen(videodata->ime_composition), (size_t)videodata->ime_cursor);
736 SDL_wcslcpy(buffer, videodata->ime_composition, len + 1);
737 SDL_wcslcat(buffer, videodata->ime_readingstring, sizeof(buffer));
738 SDL_wcslcat(buffer, &videodata->ime_composition[len], sizeof(buffer) - len);
741 SDL_wcslcpy(buffer, videodata->ime_composition, sizeof(videodata->ime_composition));
743 s = WIN_StringToUTF8(buffer);
744 SDL_SendEditingText(s, videodata->ime_cursor + SDL_wcslen(videodata->ime_readingstring), 0);
749 IME_AddCandidate(SDL_VideoData *videodata, UINT i, LPCWSTR candidate)
751 LPWSTR dst = videodata->ime_candidates[i];
752 *dst++ = (WCHAR)(TEXT('0') + ((i + videodata->ime_candlistindexbase) % 10));
753 if (videodata->ime_candvertical)
756 while (*candidate && (SDL_arraysize(videodata->ime_candidates[i]) > (dst - videodata->ime_candidates[i])))
757 *dst++ = *candidate++;
763 IME_GetCandidateList(HIMC himc, SDL_VideoData *videodata)
765 LPCANDIDATELIST cand_list = 0;
766 DWORD size = ImmGetCandidateListW(himc, 0, 0, 0);
768 cand_list = (LPCANDIDATELIST)SDL_malloc(size);
770 size = ImmGetCandidateListW(himc, 0, cand_list, size);
775 videodata->ime_candsel = cand_list->dwSelection;
776 videodata->ime_candcount = cand_list->dwCount;
778 if (LANG() == LANG_CHS && IME_GetId(videodata, 0)) {
779 const UINT maxcandchar = 18;
783 for (; i < videodata->ime_candcount; ++i) {
784 UINT len = SDL_wcslen((LPWSTR)((DWORD)cand_list + cand_list->dwOffset[i])) + 1;
785 if (len + cchars > maxcandchar) {
786 if (i > cand_list->dwSelection)
796 videodata->ime_candpgsize = i - page_start;
799 videodata->ime_candpgsize = SDL_min(cand_list->dwPageSize, MAX_CANDLIST);
800 page_start = (cand_list->dwSelection / videodata->ime_candpgsize) * videodata->ime_candpgsize;
802 SDL_memset(&videodata->ime_candidates, 0, sizeof(videodata->ime_candidates));
803 for (i = page_start, j = 0; (DWORD)i < cand_list->dwCount && j < (int)videodata->ime_candpgsize; i++, j++) {
804 LPCWSTR candidate = (LPCWSTR)((DWORD)cand_list + cand_list->dwOffset[i]);
805 IME_AddCandidate(videodata, j, candidate);
807 if (PRIMLANG() == LANG_KOREAN || (PRIMLANG() == LANG_CHT && !IME_GetId(videodata, 0)))
808 videodata->ime_candsel = -1;
817 IME_ShowCandidateList(SDL_VideoData *videodata)
819 videodata->ime_dirty = SDL_TRUE;
820 videodata->ime_candlist = SDL_TRUE;
821 IME_DestroyTextures(videodata);
822 IME_SendEditingEvent(videodata);
826 IME_HideCandidateList(SDL_VideoData *videodata)
828 videodata->ime_dirty = SDL_FALSE;
829 videodata->ime_candlist = SDL_FALSE;
830 IME_DestroyTextures(videodata);
831 IME_SendEditingEvent(videodata);
835 IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
837 SDL_bool trap = SDL_FALSE;
839 if (!videodata->ime_initialized || !videodata->ime_available || !videodata->ime_enabled)
843 case WM_INPUTLANGCHANGE:
844 IME_InputLangChanged(videodata);
846 case WM_IME_SETCONTEXT:
849 case WM_IME_STARTCOMPOSITION:
852 case WM_IME_COMPOSITION:
854 himc = ImmGetContext(hwnd);
855 if (*lParam & GCS_RESULTSTR) {
856 IME_GetCompositionString(videodata, himc, GCS_RESULTSTR);
857 IME_SendInputEvent(videodata);
859 if (*lParam & GCS_COMPSTR) {
860 if (!videodata->ime_uiless)
861 videodata->ime_readingstring[0] = 0;
863 IME_GetCompositionString(videodata, himc, GCS_COMPSTR);
864 IME_SendEditingEvent(videodata);
866 ImmReleaseContext(hwnd, himc);
868 case WM_IME_ENDCOMPOSITION:
869 videodata->ime_composition[0] = 0;
870 videodata->ime_readingstring[0] = 0;
871 videodata->ime_cursor = 0;
872 SDL_SendEditingText("", 0, 0);
876 case IMN_SETCONVERSIONMODE:
877 case IMN_SETOPENSTATUS:
878 IME_UpdateInputLocale(videodata);
880 case IMN_OPENCANDIDATE:
881 case IMN_CHANGECANDIDATE:
882 if (videodata->ime_uiless)
886 IME_ShowCandidateList(videodata);
887 himc = ImmGetContext(hwnd);
891 IME_GetCandidateList(himc, videodata);
892 ImmReleaseContext(hwnd, himc);
894 case IMN_CLOSECANDIDATE:
896 IME_HideCandidateList(videodata);
900 DWORD dwId = IME_GetId(videodata, 0);
901 IME_GetReadingString(videodata, hwnd);
904 case IMEID_CHT_VER42:
905 case IMEID_CHT_VER43:
906 case IMEID_CHT_VER44:
907 case IMEID_CHS_VER41:
908 case IMEID_CHS_VER42:
909 if (*lParam == 1 || *lParam == 2)
913 case IMEID_CHT_VER50:
914 case IMEID_CHT_VER51:
915 case IMEID_CHT_VER52:
916 case IMEID_CHT_VER60:
917 case IMEID_CHS_VER53:
938 IME_CloseCandidateList(SDL_VideoData *videodata)
940 IME_HideCandidateList(videodata);
941 videodata->ime_candcount = 0;
942 SDL_memset(videodata->ime_candidates, 0, sizeof(videodata->ime_candidates));
946 UILess_GetCandidateList(SDL_VideoData *videodata, ITfCandidateListUIElement *pcandlist)
955 pcandlist->lpVtbl->GetSelection(pcandlist, &selection);
956 pcandlist->lpVtbl->GetCount(pcandlist, &count);
957 pcandlist->lpVtbl->GetCurrentPage(pcandlist, &page);
959 videodata->ime_candsel = selection;
960 videodata->ime_candcount = count;
961 IME_ShowCandidateList(videodata);
963 pcandlist->lpVtbl->GetPageIndex(pcandlist, 0, 0, &pgcount);
965 UINT *idxlist = SDL_malloc(sizeof(UINT) * pgcount);
967 pcandlist->lpVtbl->GetPageIndex(pcandlist, idxlist, pgcount, &pgcount);
968 pgstart = idxlist[page];
969 if (page < pgcount - 1)
970 pgsize = SDL_min(count, idxlist[page + 1]) - pgstart;
972 pgsize = count - pgstart;
977 videodata->ime_candpgsize = SDL_min(pgsize, MAX_CANDLIST);
978 videodata->ime_candsel = videodata->ime_candsel - pgstart;
980 SDL_memset(videodata->ime_candidates, 0, sizeof(videodata->ime_candidates));
981 for (i = pgstart, j = 0; (DWORD)i < count && j < videodata->ime_candpgsize; i++, j++) {
983 if (SUCCEEDED(pcandlist->lpVtbl->GetString(pcandlist, i, &bstr))) {
985 IME_AddCandidate(videodata, j, bstr);
990 if (PRIMLANG() == LANG_KOREAN)
991 videodata->ime_candsel = -1;
994 STDMETHODIMP_(ULONG) TSFSink_AddRef(TSFSink *sink)
996 return ++sink->refcount;
999 STDMETHODIMP_(ULONG)TSFSink_Release(TSFSink *sink)
1002 if (sink->refcount == 0) {
1006 return sink->refcount;
1009 STDMETHODIMP UIElementSink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv)
1012 return E_INVALIDARG;
1015 if (SDL_IsEqualIID(riid, &IID_IUnknown))
1016 *ppv = (IUnknown *)sink;
1017 else if (SDL_IsEqualIID(riid, &IID_ITfUIElementSink))
1018 *ppv = (ITfUIElementSink *)sink;
1021 TSFSink_AddRef(sink);
1024 return E_NOINTERFACE;
1027 ITfUIElement *UILess_GetUIElement(SDL_VideoData *videodata, DWORD dwUIElementId)
1029 ITfUIElementMgr *puiem = 0;
1030 ITfUIElement *pelem = 0;
1031 ITfThreadMgrEx *threadmgrex = videodata->ime_threadmgrex;
1033 if (SUCCEEDED(threadmgrex->lpVtbl->QueryInterface(threadmgrex, &IID_ITfUIElementMgr, (LPVOID *)&puiem))) {
1034 puiem->lpVtbl->GetUIElement(puiem, dwUIElementId, &pelem);
1035 puiem->lpVtbl->Release(puiem);
1040 STDMETHODIMP UIElementSink_BeginUIElement(TSFSink *sink, DWORD dwUIElementId, BOOL *pbShow)
1042 ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
1043 ITfReadingInformationUIElement *preading = 0;
1044 ITfCandidateListUIElement *pcandlist = 0;
1045 SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
1047 return E_INVALIDARG;
1050 if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
1052 if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr) {
1053 WCHAR *s = (WCHAR *)bstr;
1054 SysFreeString(bstr);
1056 preading->lpVtbl->Release(preading);
1058 else if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfCandidateListUIElement, (LPVOID *)&pcandlist))) {
1059 videodata->ime_candref++;
1060 UILess_GetCandidateList(videodata, pcandlist);
1061 pcandlist->lpVtbl->Release(pcandlist);
1066 STDMETHODIMP UIElementSink_UpdateUIElement(TSFSink *sink, DWORD dwUIElementId)
1068 ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
1069 ITfReadingInformationUIElement *preading = 0;
1070 ITfCandidateListUIElement *pcandlist = 0;
1071 SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
1073 return E_INVALIDARG;
1075 if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
1077 if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr) {
1078 WCHAR *s = (WCHAR *)bstr;
1079 SDL_wcslcpy(videodata->ime_readingstring, s, sizeof(videodata->ime_readingstring));
1080 IME_SendEditingEvent(videodata);
1081 SysFreeString(bstr);
1083 preading->lpVtbl->Release(preading);
1085 else if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfCandidateListUIElement, (LPVOID *)&pcandlist))) {
1086 UILess_GetCandidateList(videodata, pcandlist);
1087 pcandlist->lpVtbl->Release(pcandlist);
1092 STDMETHODIMP UIElementSink_EndUIElement(TSFSink *sink, DWORD dwUIElementId)
1094 ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
1095 ITfReadingInformationUIElement *preading = 0;
1096 ITfCandidateListUIElement *pcandlist = 0;
1097 SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
1099 return E_INVALIDARG;
1101 if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
1102 videodata->ime_readingstring[0] = 0;
1103 IME_SendEditingEvent(videodata);
1104 preading->lpVtbl->Release(preading);
1106 if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfCandidateListUIElement, (LPVOID *)&pcandlist))) {
1107 videodata->ime_candref--;
1108 if (videodata->ime_candref == 0)
1109 IME_CloseCandidateList(videodata);
1111 pcandlist->lpVtbl->Release(pcandlist);
1116 STDMETHODIMP IPPASink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv)
1119 return E_INVALIDARG;
1122 if (SDL_IsEqualIID(riid, &IID_IUnknown))
1123 *ppv = (IUnknown *)sink;
1124 else if (SDL_IsEqualIID(riid, &IID_ITfInputProcessorProfileActivationSink))
1125 *ppv = (ITfInputProcessorProfileActivationSink *)sink;
1128 TSFSink_AddRef(sink);
1131 return E_NOINTERFACE;
1134 STDMETHODIMP IPPASink_OnActivated(TSFSink *sink, DWORD dwProfileType, LANGID langid, REFCLSID clsid, REFGUID catid, REFGUID guidProfile, HKL hkl, DWORD dwFlags)
1136 static GUID TF_PROFILE_DAYI = {0x037B2C25, 0x480C, 0x4D7F, 0xB0, 0x27, 0xD6, 0xCA, 0x6B, 0x69, 0x78, 0x8A};
1137 SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
1138 videodata->ime_candlistindexbase = SDL_IsEqualGUID(&TF_PROFILE_DAYI, guidProfile) ? 0 : 1;
1139 if (SDL_IsEqualIID(catid, &GUID_TFCAT_TIP_KEYBOARD) && (dwFlags & TF_IPSINK_FLAG_ACTIVE))
1140 IME_InputLangChanged((SDL_VideoData *)sink->data);
1142 IME_HideCandidateList(videodata);
1146 static void *vtUIElementSink[] = {
1147 (void *)(UIElementSink_QueryInterface),
1148 (void *)(TSFSink_AddRef),
1149 (void *)(TSFSink_Release),
1150 (void *)(UIElementSink_BeginUIElement),
1151 (void *)(UIElementSink_UpdateUIElement),
1152 (void *)(UIElementSink_EndUIElement)
1155 static void *vtIPPASink[] = {
1156 (void *)(IPPASink_QueryInterface),
1157 (void *)(TSFSink_AddRef),
1158 (void *)(TSFSink_Release),
1159 (void *)(IPPASink_OnActivated)
1163 UILess_EnableUIUpdates(SDL_VideoData *videodata)
1165 ITfSource *source = 0;
1166 if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie != TF_INVALID_COOKIE)
1169 if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
1170 source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie);
1171 source->lpVtbl->Release(source);
1176 UILess_DisableUIUpdates(SDL_VideoData *videodata)
1178 ITfSource *source = 0;
1179 if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie == TF_INVALID_COOKIE)
1182 if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
1183 source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie);
1184 videodata->ime_uielemsinkcookie = TF_INVALID_COOKIE;
1185 source->lpVtbl->Release(source);
1190 UILess_SetupSinks(SDL_VideoData *videodata)
1192 TfClientId clientid = 0;
1193 SDL_bool result = SDL_FALSE;
1194 ITfSource *source = 0;
1195 if (FAILED(CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgrEx, (LPVOID *)&videodata->ime_threadmgrex)))
1198 if (FAILED(videodata->ime_threadmgrex->lpVtbl->ActivateEx(videodata->ime_threadmgrex, &clientid, TF_TMAE_UIELEMENTENABLEDONLY)))
1201 videodata->ime_uielemsink = SDL_malloc(sizeof(TSFSink));
1202 videodata->ime_ippasink = SDL_malloc(sizeof(TSFSink));
1204 videodata->ime_uielemsink->lpVtbl = vtUIElementSink;
1205 videodata->ime_uielemsink->refcount = 1;
1206 videodata->ime_uielemsink->data = videodata;
1208 videodata->ime_ippasink->lpVtbl = vtIPPASink;
1209 videodata->ime_ippasink->refcount = 1;
1210 videodata->ime_ippasink->data = videodata;
1212 if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
1213 if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie))) {
1214 if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfInputProcessorProfileActivationSink, (IUnknown *)videodata->ime_ippasink, &videodata->ime_alpnsinkcookie))) {
1218 source->lpVtbl->Release(source);
1223 #define SAFE_RELEASE(p) \
1226 (p)->lpVtbl->Release((p)); \
1232 UILess_ReleaseSinks(SDL_VideoData *videodata)
1234 ITfSource *source = 0;
1235 if (videodata->ime_threadmgrex && SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
1236 source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie);
1237 source->lpVtbl->UnadviseSink(source, videodata->ime_alpnsinkcookie);
1238 SAFE_RELEASE(source);
1239 videodata->ime_threadmgrex->lpVtbl->Deactivate(videodata->ime_threadmgrex);
1240 SAFE_RELEASE(videodata->ime_threadmgrex);
1241 TSFSink_Release(videodata->ime_uielemsink);
1242 videodata->ime_uielemsink = 0;
1243 TSFSink_Release(videodata->ime_ippasink);
1244 videodata->ime_ippasink = 0;
1249 StartDrawToBitmap(HDC hdc, HBITMAP *hhbm, int width, int height)
1251 BITMAPINFO info = {0};
1252 BITMAPINFOHEADER *infoHeader = &info.bmiHeader;
1255 infoHeader->biSize = sizeof(BITMAPINFOHEADER);
1256 infoHeader->biWidth = width;
1257 infoHeader->biHeight = -1 * SDL_abs(height);
1258 infoHeader->biPlanes = 1;
1259 infoHeader->biBitCount = 32;
1260 infoHeader->biCompression = BI_RGB;
1261 *hhbm = CreateDIBSection(hdc, &info, DIB_RGB_COLORS, (void **)&bits, 0, 0);
1263 SelectObject(hdc, *hhbm);
1269 StopDrawToBitmap(HDC hdc, HBITMAP *hhbm)
1271 if (hhbm && *hhbm) {
1272 DeleteObject(*hhbm);
1278 BitmapToTexture(HBITMAP hbm, BYTE *bits, int width, int height, SDL_Texture **texture)
1280 SDL_Surface *surface = NULL;
1283 if (GetObject(hbm, sizeof(bm), &bm) == 0)
1286 if (bits && texture) {
1290 const Uint8 alpha = 130;
1291 unsigned long *p = (unsigned long *)bits;
1292 unsigned long *end = (unsigned long *)(bits + (bm.bmWidthBytes * bm.bmHeight));
1294 *p = RGB(GetRValue(*p), GetGValue(*p), GetBValue(*p)) | (alpha << 24);
1297 surface = SDL_CreateRGBSurfaceFrom(bits, width, height, bm.bmBitsPixel, bm.bmWidthBytes, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
1299 surface = SDL_CreateRGBSurfaceFrom(bits, width, height, bm.bmBitsPixel, bm.bmWidthBytes, 0x00ff0000, 0x0000ff00, 0x000000ff, 0);
1301 *texture = SDL_CreateTextureFromSurface(0, surface);
1302 SDL_FreeSurface(surface);
1307 /* This draws only within the specified area and fills the entire region. */
1309 DrawRect(HDC hdc, int left, int top, int right, int bottom, int pensize)
1311 /* The case of no pen (PenSize = 0) is automatically taken care of. */
1312 const int penadjust = (int)SDL_floor(pensize / 2.0f - 0.5f);
1313 left += pensize / 2;
1316 bottom -= penadjust;
1317 Rectangle(hdc, left, top, right, bottom);
1321 DestroyTexture(SDL_Texture **texture)
1323 if (texture && *texture) {
1324 SDL_DestroyTexture(*texture);
1330 IME_DestroyTextures(SDL_VideoData *videodata)
1332 DestroyTexture(&videodata->ime_candtex);
1335 #define SDL_swap(a,b) { \
1342 IME_PositionCandidateList(SDL_VideoData *videodata, SIZE size)
1344 int left, top, right, bottom;
1345 SDL_bool ok = SDL_FALSE;
1346 int winw = videodata->ime_winwidth;
1347 int winh = videodata->ime_winheight;
1350 left = videodata->ime_rect.x;
1351 top = videodata->ime_rect.y + videodata->ime_rect.h;
1352 right = left + size.cx;
1353 bottom = top + size.cy;
1354 if (right >= winw) {
1355 left -= right - winw;
1363 left = videodata->ime_rect.x;
1364 top = videodata->ime_rect.y - size.cy;
1365 right = left + size.cx;
1366 bottom = videodata->ime_rect.y;
1367 if (right >= winw) {
1368 left -= right - winw;
1377 left = videodata->ime_rect.x + size.cx;
1379 right = left + size.cx;
1387 left = videodata->ime_rect.x - size.cx;
1389 right = videodata->ime_rect.x;
1395 /* Window too small, show at (0,0) */
1403 videodata->ime_candlistrect.x = left;
1404 videodata->ime_candlistrect.y = top;
1405 videodata->ime_candlistrect.w = right - left;
1406 videodata->ime_candlistrect.h = bottom - top;
1410 IME_RenderCandidateList(SDL_VideoData *videodata, HDC hdc)
1414 SIZE candsizes[MAX_CANDLIST];
1415 SIZE maxcandsize = {0};
1418 const int candcount = SDL_min(SDL_min(MAX_CANDLIST, videodata->ime_candcount), videodata->ime_candpgsize);
1419 SDL_bool vertical = videodata->ime_candvertical;
1421 const int listborder = 1;
1422 const int listpadding = 0;
1423 const int listbordercolor = RGB(0xB4, 0xC7, 0xAA);
1424 const int listfillcolor = RGB(255, 255, 255);
1426 const int candborder = 1;
1427 const int candpadding = 0;
1428 const int candmargin = 1;
1429 const COLORREF candbordercolor = RGB(255, 255, 255);
1430 const COLORREF candfillcolor = RGB(255, 255, 255);
1431 const COLORREF candtextcolor = RGB(0, 0, 0);
1432 const COLORREF selbordercolor = RGB(0x84, 0xAC, 0xDD);
1433 const COLORREF selfillcolor = RGB(0xD2, 0xE6, 0xFF);
1434 const COLORREF seltextcolor = RGB(0, 0, 0);
1435 const int horzcandspacing = 5;
1437 HPEN listpen = listborder != 0 ? CreatePen(PS_SOLID, listborder, listbordercolor) : (HPEN)GetStockObject(NULL_PEN);
1438 HBRUSH listbrush = CreateSolidBrush(listfillcolor);
1439 HPEN candpen = candborder != 0 ? CreatePen(PS_SOLID, candborder, candbordercolor) : (HPEN)GetStockObject(NULL_PEN);
1440 HBRUSH candbrush = CreateSolidBrush(candfillcolor);
1441 HPEN selpen = candborder != 0 ? CreatePen(PS_DOT, candborder, selbordercolor) : (HPEN)GetStockObject(NULL_PEN);
1442 HBRUSH selbrush = CreateSolidBrush(selfillcolor);
1443 HFONT font = CreateFont((int)(1 + videodata->ime_rect.h * 0.75f), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH | FF_SWISS, TEXT("Microsoft Sans Serif"));
1445 SetBkMode(hdc, TRANSPARENT);
1446 SelectObject(hdc, font);
1448 for (i = 0; i < candcount; ++i) {
1449 const WCHAR *s = videodata->ime_candidates[i];
1453 GetTextExtentPoint32W(hdc, s, SDL_wcslen(s), &candsizes[i]);
1454 maxcandsize.cx = SDL_max(maxcandsize.cx, candsizes[i].cx);
1455 maxcandsize.cy = SDL_max(maxcandsize.cy, candsizes[i].cy);
1470 ((candcount + 1) * candmargin) +
1471 (candcount * candborder * 2) +
1472 (candcount * candpadding * 2) +
1473 (candcount * maxcandsize.cy)
1480 ((candcount + 1) * candmargin) +
1481 (candcount * candborder * 2) +
1482 (candcount * candpadding * 2) +
1483 ((candcount - 1) * horzcandspacing);
1486 for (i = 0; i < candcount; ++i)
1487 size.cx += candsizes[i].cx;
1499 bits = StartDrawToBitmap(hdc, &hbm, size.cx, size.cy);
1501 SelectObject(hdc, listpen);
1502 SelectObject(hdc, listbrush);
1503 DrawRect(hdc, 0, 0, size.cx, size.cy, listborder);
1505 SelectObject(hdc, candpen);
1506 SelectObject(hdc, candbrush);
1507 SetTextColor(hdc, candtextcolor);
1508 SetBkMode(hdc, TRANSPARENT);
1510 for (i = 0; i < candcount; ++i) {
1511 const WCHAR *s = videodata->ime_candidates[i];
1512 int left, top, right, bottom;
1517 left = listborder + listpadding + candmargin;
1518 top = listborder + listpadding + (i * candborder * 2) + (i * candpadding * 2) + ((i + 1) * candmargin) + (i * maxcandsize.cy);
1519 right = size.cx - listborder - listpadding - candmargin;
1520 bottom = top + maxcandsize.cy + (candpadding * 2) + (candborder * 2);
1523 left = listborder + listpadding + (i * candborder * 2) + (i * candpadding * 2) + ((i + 1) * candmargin) + (i * horzcandspacing);
1525 for (j = 0; j < i; ++j)
1526 left += candsizes[j].cx;
1528 top = listborder + listpadding + candmargin;
1529 right = left + candsizes[i].cx + (candpadding * 2) + (candborder * 2);
1530 bottom = size.cy - listborder - listpadding - candmargin;
1533 if (i == videodata->ime_candsel) {
1534 SelectObject(hdc, selpen);
1535 SelectObject(hdc, selbrush);
1536 SetTextColor(hdc, seltextcolor);
1539 SelectObject(hdc, candpen);
1540 SelectObject(hdc, candbrush);
1541 SetTextColor(hdc, candtextcolor);
1544 DrawRect(hdc, left, top, right, bottom, candborder);
1545 ExtTextOutW(hdc, left + candborder + candpadding, top + candborder + candpadding, 0, NULL, s, SDL_wcslen(s), NULL);
1547 BitmapToTexture(hbm, bits, size.cx, size.cy, &videodata->ime_candtex);
1548 StopDrawToBitmap(hdc, &hbm);
1550 DeleteObject(listpen);
1551 DeleteObject(listbrush);
1552 DeleteObject(candpen);
1553 DeleteObject(candbrush);
1554 DeleteObject(selpen);
1555 DeleteObject(selbrush);
1558 IME_PositionCandidateList(videodata, size);
1562 IME_Render(SDL_VideoData *videodata)
1564 HDC hdc = CreateCompatibleDC(NULL);
1566 if (videodata->ime_candlist)
1567 IME_RenderCandidateList(videodata, hdc);
1571 videodata->ime_dirty = SDL_FALSE;
1574 void IME_Present(SDL_VideoData *videodata)
1576 if (videodata->ime_dirty)
1577 IME_Render(videodata);
1579 SDL_RenderCopy(videodata->ime_candtex, NULL, &videodata->ime_candlistrect);
1582 #endif /* SDL_DISABLE_WINDOWS_IME */
1584 /* vi: set ts=4 sw=4 expandtab: */