kernel32/tests: Also test wrong architecture with matching 32/64 bitness.
[wine.git] / dlls / imm32 / imm.c
blob3f8ad1868ce93705d94ede58e90458f3bf893c4d
1 /*
2 * IMM32 library
4 * Copyright 1998 Patrik Stridvall
5 * Copyright 2002, 2003, 2007 CodeWeavers, Aric Stewart
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <stdio.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winerror.h"
30 #include "wine/debug.h"
31 #include "imm.h"
32 #include "ddk/imm.h"
33 #include "winnls.h"
34 #include "winreg.h"
35 #include "wine/list.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(imm);
40 #define IMM_INIT_MAGIC 0x19650412
41 BOOL WINAPI User32InitializeImmEntryTable(DWORD);
43 #define MAKE_FUNCPTR(f) typeof(f) * p##f
44 typedef struct _tagImmHkl{
45 struct list entry;
46 HKL hkl;
47 HMODULE hIME;
48 IMEINFO imeInfo;
49 WCHAR imeClassName[17]; /* 16 character max */
50 ULONG uSelected;
51 HWND UIWnd;
53 /* Function Pointers */
54 MAKE_FUNCPTR(ImeInquire);
55 MAKE_FUNCPTR(ImeConfigure);
56 MAKE_FUNCPTR(ImeDestroy);
57 MAKE_FUNCPTR(ImeEscape);
58 MAKE_FUNCPTR(ImeSelect);
59 MAKE_FUNCPTR(ImeSetActiveContext);
60 MAKE_FUNCPTR(ImeToAsciiEx);
61 MAKE_FUNCPTR(NotifyIME);
62 MAKE_FUNCPTR(ImeRegisterWord);
63 MAKE_FUNCPTR(ImeUnregisterWord);
64 MAKE_FUNCPTR(ImeEnumRegisterWord);
65 MAKE_FUNCPTR(ImeSetCompositionString);
66 MAKE_FUNCPTR(ImeConversionList);
67 MAKE_FUNCPTR(ImeProcessKey);
68 MAKE_FUNCPTR(ImeGetRegisterWordStyle);
69 MAKE_FUNCPTR(ImeGetImeMenuItems);
70 } ImmHkl;
71 #undef MAKE_FUNCPTR
73 typedef struct tagInputContextData
75 DWORD dwLock;
76 INPUTCONTEXT IMC;
77 DWORD threadID;
79 ImmHkl *immKbd;
80 UINT lastVK;
81 BOOL threadDefault;
82 DWORD magic;
83 } InputContextData;
85 #define WINE_IMC_VALID_MAGIC 0x56434D49
87 typedef struct _tagTRANSMSG {
88 UINT message;
89 WPARAM wParam;
90 LPARAM lParam;
91 } TRANSMSG, *LPTRANSMSG;
93 typedef struct _tagIMMThreadData {
94 struct list entry;
95 DWORD threadID;
96 HIMC defaultContext;
97 HWND hwndDefault;
98 BOOL disableIME;
99 DWORD windowRefs;
100 } IMMThreadData;
102 static struct list ImmHklList = LIST_INIT(ImmHklList);
103 static struct list ImmThreadDataList = LIST_INIT(ImmThreadDataList);
105 static const WCHAR szwWineIMCProperty[] = {'W','i','n','e','I','m','m','H','I','M','C','P','r','o','p','e','r','t','y',0};
107 static const WCHAR szImeFileW[] = {'I','m','e',' ','F','i','l','e',0};
108 static const WCHAR szLayoutTextW[] = {'L','a','y','o','u','t',' ','T','e','x','t',0};
109 static const WCHAR szImeRegFmt[] = {'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\','C','o','n','t','r','o','l','\\','K','e','y','b','o','a','r','d',' ','L','a','y','o','u','t','s','\\','%','0','8','l','x',0};
111 static const WCHAR szwIME[] = {'I','M','E',0};
112 static const WCHAR szwDefaultIME[] = {'D','e','f','a','u','l','t',' ','I','M','E',0};
114 static CRITICAL_SECTION threaddata_cs;
115 static CRITICAL_SECTION_DEBUG critsect_debug =
117 0, 0, &threaddata_cs,
118 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
119 0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
121 static CRITICAL_SECTION threaddata_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
122 static BOOL disable_ime;
124 #define is_himc_ime_unicode(p) (p->immKbd->imeInfo.fdwProperty & IME_PROP_UNICODE)
125 #define is_kbd_ime_unicode(p) (p->imeInfo.fdwProperty & IME_PROP_UNICODE)
127 static BOOL IMM_DestroyContext(HIMC hIMC);
128 static InputContextData* get_imc_data(HIMC hIMC);
130 static inline WCHAR *strdupAtoW( const char *str )
132 WCHAR *ret = NULL;
133 if (str)
135 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
136 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
137 MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
139 return ret;
142 static inline CHAR *strdupWtoA( const WCHAR *str )
144 CHAR *ret = NULL;
145 if (str)
147 DWORD len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
148 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
149 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
151 return ret;
154 static DWORD convert_candidatelist_WtoA(
155 LPCANDIDATELIST lpSrc, LPCANDIDATELIST lpDst, DWORD dwBufLen)
157 DWORD ret, i, len;
159 ret = FIELD_OFFSET( CANDIDATELIST, dwOffset[lpSrc->dwCount] );
160 if ( lpDst && dwBufLen > 0 )
162 *lpDst = *lpSrc;
163 lpDst->dwOffset[0] = ret;
166 for ( i = 0; i < lpSrc->dwCount; i++)
168 LPBYTE src = (LPBYTE)lpSrc + lpSrc->dwOffset[i];
170 if ( lpDst && dwBufLen > 0 )
172 LPBYTE dest = (LPBYTE)lpDst + lpDst->dwOffset[i];
174 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1,
175 (LPSTR)dest, dwBufLen, NULL, NULL);
177 if ( i + 1 < lpSrc->dwCount )
178 lpDst->dwOffset[i+1] = lpDst->dwOffset[i] + len * sizeof(char);
179 dwBufLen -= len * sizeof(char);
181 else
182 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, NULL, 0, NULL, NULL);
184 ret += len * sizeof(char);
187 if ( lpDst )
188 lpDst->dwSize = ret;
190 return ret;
193 static DWORD convert_candidatelist_AtoW(
194 LPCANDIDATELIST lpSrc, LPCANDIDATELIST lpDst, DWORD dwBufLen)
196 DWORD ret, i, len;
198 ret = FIELD_OFFSET( CANDIDATELIST, dwOffset[lpSrc->dwCount] );
199 if ( lpDst && dwBufLen > 0 )
201 *lpDst = *lpSrc;
202 lpDst->dwOffset[0] = ret;
205 for ( i = 0; i < lpSrc->dwCount; i++)
207 LPBYTE src = (LPBYTE)lpSrc + lpSrc->dwOffset[i];
209 if ( lpDst && dwBufLen > 0 )
211 LPBYTE dest = (LPBYTE)lpDst + lpDst->dwOffset[i];
213 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1,
214 (LPWSTR)dest, dwBufLen);
216 if ( i + 1 < lpSrc->dwCount )
217 lpDst->dwOffset[i+1] = lpDst->dwOffset[i] + len * sizeof(WCHAR);
218 dwBufLen -= len * sizeof(WCHAR);
220 else
221 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, NULL, 0);
223 ret += len * sizeof(WCHAR);
226 if ( lpDst )
227 lpDst->dwSize = ret;
229 return ret;
232 static IMMThreadData *IMM_GetThreadData(HWND hwnd, DWORD thread)
234 IMMThreadData *data;
235 DWORD process;
237 if (hwnd)
239 if (!(thread = GetWindowThreadProcessId(hwnd, &process))) return NULL;
240 if (process != GetCurrentProcessId()) return NULL;
242 else if (thread)
244 HANDLE h = OpenThread(THREAD_QUERY_INFORMATION, FALSE, thread);
245 if (!h) return NULL;
246 process = GetProcessIdOfThread(h);
247 CloseHandle(h);
248 if (process != GetCurrentProcessId()) return NULL;
250 else
251 thread = GetCurrentThreadId();
253 EnterCriticalSection(&threaddata_cs);
254 LIST_FOR_EACH_ENTRY(data, &ImmThreadDataList, IMMThreadData, entry)
255 if (data->threadID == thread) return data;
257 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
258 data->threadID = thread;
259 list_add_head(&ImmThreadDataList,&data->entry);
260 TRACE("Thread Data Created (%x)\n",thread);
261 return data;
264 static BOOL IMM_IsDefaultContext(HIMC imc)
266 InputContextData *data = get_imc_data(imc);
268 if (!data)
269 return FALSE;
271 return data->threadDefault;
274 static void IMM_FreeThreadData(void)
276 IMMThreadData *data;
278 EnterCriticalSection(&threaddata_cs);
279 LIST_FOR_EACH_ENTRY(data, &ImmThreadDataList, IMMThreadData, entry)
281 if (data->threadID == GetCurrentThreadId())
283 list_remove(&data->entry);
284 LeaveCriticalSection(&threaddata_cs);
285 IMM_DestroyContext(data->defaultContext);
286 HeapFree(GetProcessHeap(),0,data);
287 TRACE("Thread Data Destroyed\n");
288 return;
291 LeaveCriticalSection(&threaddata_cs);
294 static HMODULE load_graphics_driver(void)
296 static const WCHAR display_device_guid_propW[] = {
297 '_','_','w','i','n','e','_','d','i','s','p','l','a','y','_',
298 'd','e','v','i','c','e','_','g','u','i','d',0 };
299 static const WCHAR key_pathW[] = {
300 'S','y','s','t','e','m','\\',
301 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
302 'C','o','n','t','r','o','l','\\',
303 'V','i','d','e','o','\\','{',0};
304 static const WCHAR displayW[] = {'}','\\','0','0','0','0',0};
305 static const WCHAR driverW[] = {'G','r','a','p','h','i','c','s','D','r','i','v','e','r',0};
307 HMODULE ret = 0;
308 HKEY hkey;
309 DWORD size;
310 WCHAR path[MAX_PATH];
311 WCHAR key[(sizeof(key_pathW) + sizeof(displayW)) / sizeof(WCHAR) + 40];
312 UINT guid_atom = HandleToULong( GetPropW( GetDesktopWindow(), display_device_guid_propW ));
314 if (!guid_atom) return 0;
315 memcpy( key, key_pathW, sizeof(key_pathW) );
316 if (!GlobalGetAtomNameW( guid_atom, key + strlenW(key), 40 )) return 0;
317 strcatW( key, displayW );
318 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, key, &hkey )) return 0;
319 size = sizeof(path);
320 if (!RegQueryValueExW( hkey, driverW, NULL, NULL, (BYTE *)path, &size )) ret = LoadLibraryW( path );
321 RegCloseKey( hkey );
322 TRACE( "%s %p\n", debugstr_w(path), ret );
323 return ret;
326 /* ImmHkl loading and freeing */
327 #define LOAD_FUNCPTR(f) if((ptr->p##f = (LPVOID)GetProcAddress(ptr->hIME, #f)) == NULL){WARN("Can't find function %s in ime\n", #f);}
328 static ImmHkl *IMM_GetImmHkl(HKL hkl)
330 ImmHkl *ptr;
331 WCHAR filename[MAX_PATH];
333 TRACE("Seeking ime for keyboard %p\n",hkl);
335 LIST_FOR_EACH_ENTRY(ptr, &ImmHklList, ImmHkl, entry)
337 if (ptr->hkl == hkl)
338 return ptr;
340 /* not found... create it */
342 ptr = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ImmHkl));
344 ptr->hkl = hkl;
345 if (ImmGetIMEFileNameW(hkl, filename, MAX_PATH)) ptr->hIME = LoadLibraryW(filename);
346 if (!ptr->hIME) ptr->hIME = load_graphics_driver();
347 if (ptr->hIME)
349 LOAD_FUNCPTR(ImeInquire);
350 if (!ptr->pImeInquire || !ptr->pImeInquire(&ptr->imeInfo, ptr->imeClassName, NULL))
352 FreeLibrary(ptr->hIME);
353 ptr->hIME = NULL;
355 else
357 LOAD_FUNCPTR(ImeDestroy);
358 LOAD_FUNCPTR(ImeSelect);
359 if (!ptr->pImeSelect || !ptr->pImeDestroy)
361 FreeLibrary(ptr->hIME);
362 ptr->hIME = NULL;
364 else
366 LOAD_FUNCPTR(ImeConfigure);
367 LOAD_FUNCPTR(ImeEscape);
368 LOAD_FUNCPTR(ImeSetActiveContext);
369 LOAD_FUNCPTR(ImeToAsciiEx);
370 LOAD_FUNCPTR(NotifyIME);
371 LOAD_FUNCPTR(ImeRegisterWord);
372 LOAD_FUNCPTR(ImeUnregisterWord);
373 LOAD_FUNCPTR(ImeEnumRegisterWord);
374 LOAD_FUNCPTR(ImeSetCompositionString);
375 LOAD_FUNCPTR(ImeConversionList);
376 LOAD_FUNCPTR(ImeProcessKey);
377 LOAD_FUNCPTR(ImeGetRegisterWordStyle);
378 LOAD_FUNCPTR(ImeGetImeMenuItems);
379 /* make sure our classname is WCHAR */
380 if (!is_kbd_ime_unicode(ptr))
382 WCHAR bufW[17];
383 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ptr->imeClassName,
384 -1, bufW, 17);
385 lstrcpyW(ptr->imeClassName, bufW);
390 list_add_head(&ImmHklList,&ptr->entry);
392 return ptr;
394 #undef LOAD_FUNCPTR
396 HWND WINAPI __wine_get_ui_window(HKL hkl)
398 ImmHkl *immHkl = IMM_GetImmHkl(hkl);
399 return immHkl->UIWnd;
402 static void IMM_FreeAllImmHkl(void)
404 ImmHkl *ptr,*cursor2;
406 LIST_FOR_EACH_ENTRY_SAFE(ptr, cursor2, &ImmHklList, ImmHkl, entry)
408 list_remove(&ptr->entry);
409 if (ptr->hIME)
411 ptr->pImeDestroy(1);
412 FreeLibrary(ptr->hIME);
414 if (ptr->UIWnd)
415 DestroyWindow(ptr->UIWnd);
416 HeapFree(GetProcessHeap(),0,ptr);
420 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved)
422 TRACE("%p, %x, %p\n",hInstDLL,fdwReason,lpReserved);
423 switch (fdwReason)
425 case DLL_PROCESS_ATTACH:
426 if (!User32InitializeImmEntryTable(IMM_INIT_MAGIC))
428 return FALSE;
430 break;
431 case DLL_THREAD_ATTACH:
432 break;
433 case DLL_THREAD_DETACH:
434 IMM_FreeThreadData();
435 break;
436 case DLL_PROCESS_DETACH:
437 if (lpReserved) break;
438 IMM_FreeThreadData();
439 IMM_FreeAllImmHkl();
440 break;
442 return TRUE;
445 /* for posting messages as the IME */
446 static void ImmInternalPostIMEMessage(InputContextData *data, UINT msg, WPARAM wParam, LPARAM lParam)
448 HWND target = GetFocus();
449 if (!target)
450 PostMessageW(data->IMC.hWnd,msg,wParam,lParam);
451 else
452 PostMessageW(target, msg, wParam, lParam);
455 /* for sending messages as the IME */
456 static void ImmInternalSendIMEMessage(InputContextData *data, UINT msg, WPARAM wParam, LPARAM lParam)
458 HWND target = GetFocus();
459 if (!target)
460 SendMessageW(data->IMC.hWnd,msg,wParam,lParam);
461 else
462 SendMessageW(target, msg, wParam, lParam);
465 static LRESULT ImmInternalSendIMENotify(InputContextData *data, WPARAM notify, LPARAM lParam)
467 HWND target;
469 target = data->IMC.hWnd;
470 if (!target) target = GetFocus();
472 if (target)
473 return SendMessageW(target, WM_IME_NOTIFY, notify, lParam);
475 return 0;
478 static HIMCC ImmCreateBlankCompStr(void)
480 HIMCC rc;
481 LPCOMPOSITIONSTRING ptr;
482 rc = ImmCreateIMCC(sizeof(COMPOSITIONSTRING));
483 ptr = ImmLockIMCC(rc);
484 memset(ptr,0,sizeof(COMPOSITIONSTRING));
485 ptr->dwSize = sizeof(COMPOSITIONSTRING);
486 ImmUnlockIMCC(rc);
487 return rc;
490 static InputContextData* get_imc_data(HIMC hIMC)
492 InputContextData *data = hIMC;
494 if (hIMC == NULL)
495 return NULL;
497 if(IsBadReadPtr(data, sizeof(InputContextData)) || data->magic != WINE_IMC_VALID_MAGIC)
499 SetLastError(ERROR_INVALID_HANDLE);
500 return NULL;
502 return data;
505 static HIMC get_default_context( HWND hwnd )
507 HIMC ret;
508 IMMThreadData* thread_data = IMM_GetThreadData( hwnd, 0 );
510 if (!thread_data) return 0;
512 if (thread_data->defaultContext)
514 ret = thread_data->defaultContext;
515 LeaveCriticalSection(&threaddata_cs);
516 return ret;
519 /* can't create a default context in another thread */
520 if (thread_data->threadID != GetCurrentThreadId())
522 LeaveCriticalSection(&threaddata_cs);
523 return 0;
526 LeaveCriticalSection(&threaddata_cs);
528 ret = ImmCreateContext();
529 if (!ret) return 0;
530 ((InputContextData*)ret)->threadDefault = TRUE;
532 /* thread_data is in the current thread so we can assume it's still valid */
533 EnterCriticalSection(&threaddata_cs);
535 if (thread_data->defaultContext) /* someone beat us */
537 IMM_DestroyContext( ret );
538 ret = thread_data->defaultContext;
540 else thread_data->defaultContext = ret;
542 LeaveCriticalSection(&threaddata_cs);
543 return ret;
546 static BOOL IMM_IsCrossThreadAccess(HWND hWnd, HIMC hIMC)
548 InputContextData *data;
550 if (hWnd)
552 DWORD thread = GetWindowThreadProcessId(hWnd, NULL);
553 if (thread != GetCurrentThreadId()) return TRUE;
555 data = get_imc_data(hIMC);
556 if (data && data->threadID != GetCurrentThreadId())
557 return TRUE;
559 return FALSE;
562 /***********************************************************************
563 * ImmAssociateContext (IMM32.@)
565 HIMC WINAPI ImmAssociateContext(HWND hWnd, HIMC hIMC)
567 HIMC old = NULL;
568 InputContextData *data = get_imc_data(hIMC);
570 TRACE("(%p, %p):\n", hWnd, hIMC);
572 if(hIMC && !data)
573 return NULL;
576 * If already associated just return
578 if (hIMC && data->IMC.hWnd == hWnd)
579 return hIMC;
581 if (hIMC && IMM_IsCrossThreadAccess(hWnd, hIMC))
582 return NULL;
584 if (hWnd)
586 HIMC defaultContext = get_default_context( hWnd );
587 old = RemovePropW(hWnd,szwWineIMCProperty);
589 if (old == NULL)
590 old = defaultContext;
591 else if (old == (HIMC)-1)
592 old = NULL;
594 if (hIMC != defaultContext)
596 if (hIMC == NULL) /* Meaning disable imm for that window*/
597 SetPropW(hWnd,szwWineIMCProperty,(HANDLE)-1);
598 else
599 SetPropW(hWnd,szwWineIMCProperty,hIMC);
602 if (old)
604 InputContextData *old_data = old;
605 if (old_data->IMC.hWnd == hWnd)
606 old_data->IMC.hWnd = NULL;
610 if (!hIMC)
611 return old;
613 SendMessageW(data->IMC.hWnd, WM_IME_SETCONTEXT, FALSE, ISC_SHOWUIALL);
614 data->IMC.hWnd = hWnd;
615 SendMessageW(data->IMC.hWnd, WM_IME_SETCONTEXT, TRUE, ISC_SHOWUIALL);
617 return old;
622 * Helper function for ImmAssociateContextEx
624 static BOOL CALLBACK _ImmAssociateContextExEnumProc(HWND hwnd, LPARAM lParam)
626 HIMC hImc = (HIMC)lParam;
627 ImmAssociateContext(hwnd,hImc);
628 return TRUE;
631 /***********************************************************************
632 * ImmAssociateContextEx (IMM32.@)
634 BOOL WINAPI ImmAssociateContextEx(HWND hWnd, HIMC hIMC, DWORD dwFlags)
636 TRACE("(%p, %p, 0x%x):\n", hWnd, hIMC, dwFlags);
638 if (!hWnd)
639 return FALSE;
641 switch (dwFlags)
643 case 0:
644 ImmAssociateContext(hWnd,hIMC);
645 return TRUE;
646 case IACE_DEFAULT:
648 HIMC defaultContext = get_default_context( hWnd );
649 if (!defaultContext) return FALSE;
650 ImmAssociateContext(hWnd,defaultContext);
651 return TRUE;
653 case IACE_IGNORENOCONTEXT:
654 if (GetPropW(hWnd,szwWineIMCProperty))
655 ImmAssociateContext(hWnd,hIMC);
656 return TRUE;
657 case IACE_CHILDREN:
658 EnumChildWindows(hWnd,_ImmAssociateContextExEnumProc,(LPARAM)hIMC);
659 return TRUE;
660 default:
661 FIXME("Unknown dwFlags 0x%x\n",dwFlags);
662 return FALSE;
666 /***********************************************************************
667 * ImmConfigureIMEA (IMM32.@)
669 BOOL WINAPI ImmConfigureIMEA(
670 HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
672 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
674 TRACE("(%p, %p, %d, %p):\n", hKL, hWnd, dwMode, lpData);
676 if (dwMode == IME_CONFIG_REGISTERWORD && !lpData)
677 return FALSE;
679 if (immHkl->hIME && immHkl->pImeConfigure)
681 if (dwMode != IME_CONFIG_REGISTERWORD || !is_kbd_ime_unicode(immHkl))
682 return immHkl->pImeConfigure(hKL,hWnd,dwMode,lpData);
683 else
685 REGISTERWORDW rww;
686 REGISTERWORDA *rwa = lpData;
687 BOOL rc;
689 rww.lpReading = strdupAtoW(rwa->lpReading);
690 rww.lpWord = strdupAtoW(rwa->lpWord);
691 rc = immHkl->pImeConfigure(hKL,hWnd,dwMode,&rww);
692 HeapFree(GetProcessHeap(),0,rww.lpReading);
693 HeapFree(GetProcessHeap(),0,rww.lpWord);
694 return rc;
697 else
698 return FALSE;
701 /***********************************************************************
702 * ImmConfigureIMEW (IMM32.@)
704 BOOL WINAPI ImmConfigureIMEW(
705 HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
707 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
709 TRACE("(%p, %p, %d, %p):\n", hKL, hWnd, dwMode, lpData);
711 if (dwMode == IME_CONFIG_REGISTERWORD && !lpData)
712 return FALSE;
714 if (immHkl->hIME && immHkl->pImeConfigure)
716 if (dwMode != IME_CONFIG_REGISTERWORD || is_kbd_ime_unicode(immHkl))
717 return immHkl->pImeConfigure(hKL,hWnd,dwMode,lpData);
718 else
720 REGISTERWORDW *rww = lpData;
721 REGISTERWORDA rwa;
722 BOOL rc;
724 rwa.lpReading = strdupWtoA(rww->lpReading);
725 rwa.lpWord = strdupWtoA(rww->lpWord);
726 rc = immHkl->pImeConfigure(hKL,hWnd,dwMode,&rwa);
727 HeapFree(GetProcessHeap(),0,rwa.lpReading);
728 HeapFree(GetProcessHeap(),0,rwa.lpWord);
729 return rc;
732 else
733 return FALSE;
736 /***********************************************************************
737 * ImmCreateContext (IMM32.@)
739 HIMC WINAPI ImmCreateContext(void)
741 InputContextData *new_context;
742 LPGUIDELINE gl;
743 LPCANDIDATEINFO ci;
744 int i;
746 new_context = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(InputContextData));
748 /* Load the IME */
749 new_context->immKbd = IMM_GetImmHkl(GetKeyboardLayout(0));
751 if (!new_context->immKbd->hIME)
753 TRACE("IME dll could not be loaded\n");
754 HeapFree(GetProcessHeap(),0,new_context);
755 return 0;
758 /* the HIMCCs are never NULL */
759 new_context->IMC.hCompStr = ImmCreateBlankCompStr();
760 new_context->IMC.hMsgBuf = ImmCreateIMCC(0);
761 new_context->IMC.hCandInfo = ImmCreateIMCC(sizeof(CANDIDATEINFO));
762 ci = ImmLockIMCC(new_context->IMC.hCandInfo);
763 memset(ci,0,sizeof(CANDIDATEINFO));
764 ci->dwSize = sizeof(CANDIDATEINFO);
765 ImmUnlockIMCC(new_context->IMC.hCandInfo);
766 new_context->IMC.hGuideLine = ImmCreateIMCC(sizeof(GUIDELINE));
767 gl = ImmLockIMCC(new_context->IMC.hGuideLine);
768 memset(gl,0,sizeof(GUIDELINE));
769 gl->dwSize = sizeof(GUIDELINE);
770 ImmUnlockIMCC(new_context->IMC.hGuideLine);
772 for (i = 0; i < sizeof(new_context->IMC.cfCandForm) / sizeof(CANDIDATEFORM); i++)
773 new_context->IMC.cfCandForm[i].dwIndex = ~0u;
775 /* Initialize the IME Private */
776 new_context->IMC.hPrivate = ImmCreateIMCC(new_context->immKbd->imeInfo.dwPrivateDataSize);
778 new_context->IMC.fdwConversion = new_context->immKbd->imeInfo.fdwConversionCaps;
779 new_context->IMC.fdwSentence = new_context->immKbd->imeInfo.fdwSentenceCaps;
781 if (!new_context->immKbd->pImeSelect(new_context, TRUE))
783 TRACE("Selection of IME failed\n");
784 IMM_DestroyContext(new_context);
785 return 0;
787 new_context->threadID = GetCurrentThreadId();
788 SendMessageW(GetFocus(), WM_IME_SELECT, TRUE, (LPARAM)new_context->immKbd);
790 new_context->immKbd->uSelected++;
791 TRACE("Created context %p\n",new_context);
793 new_context->magic = WINE_IMC_VALID_MAGIC;
794 return new_context;
797 static BOOL IMM_DestroyContext(HIMC hIMC)
799 InputContextData *data = get_imc_data(hIMC);
801 TRACE("Destroying %p\n",hIMC);
803 if (!data)
804 return FALSE;
806 data->immKbd->uSelected --;
807 data->immKbd->pImeSelect(hIMC, FALSE);
808 SendMessageW(data->IMC.hWnd, WM_IME_SELECT, FALSE, (LPARAM)data->immKbd);
810 ImmDestroyIMCC(data->IMC.hCompStr);
811 ImmDestroyIMCC(data->IMC.hCandInfo);
812 ImmDestroyIMCC(data->IMC.hGuideLine);
813 ImmDestroyIMCC(data->IMC.hPrivate);
814 ImmDestroyIMCC(data->IMC.hMsgBuf);
816 data->magic = 0;
817 HeapFree(GetProcessHeap(),0,data);
819 return TRUE;
822 /***********************************************************************
823 * ImmDestroyContext (IMM32.@)
825 BOOL WINAPI ImmDestroyContext(HIMC hIMC)
827 if (!IMM_IsDefaultContext(hIMC) && !IMM_IsCrossThreadAccess(NULL, hIMC))
828 return IMM_DestroyContext(hIMC);
829 else
830 return FALSE;
833 /***********************************************************************
834 * ImmDisableIME (IMM32.@)
836 BOOL WINAPI ImmDisableIME(DWORD idThread)
838 if (idThread == (DWORD)-1)
839 disable_ime = TRUE;
840 else {
841 IMMThreadData *thread_data = IMM_GetThreadData(NULL, idThread);
842 if (!thread_data) return FALSE;
843 thread_data->disableIME = TRUE;
844 LeaveCriticalSection(&threaddata_cs);
846 return TRUE;
849 /***********************************************************************
850 * ImmEnumRegisterWordA (IMM32.@)
852 UINT WINAPI ImmEnumRegisterWordA(
853 HKL hKL, REGISTERWORDENUMPROCA lpfnEnumProc,
854 LPCSTR lpszReading, DWORD dwStyle,
855 LPCSTR lpszRegister, LPVOID lpData)
857 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
858 TRACE("(%p, %p, %s, %d, %s, %p):\n", hKL, lpfnEnumProc,
859 debugstr_a(lpszReading), dwStyle, debugstr_a(lpszRegister), lpData);
860 if (immHkl->hIME && immHkl->pImeEnumRegisterWord)
862 if (!is_kbd_ime_unicode(immHkl))
863 return immHkl->pImeEnumRegisterWord((REGISTERWORDENUMPROCW)lpfnEnumProc,
864 (LPCWSTR)lpszReading, dwStyle, (LPCWSTR)lpszRegister, lpData);
865 else
867 LPWSTR lpszwReading = strdupAtoW(lpszReading);
868 LPWSTR lpszwRegister = strdupAtoW(lpszRegister);
869 BOOL rc;
871 rc = immHkl->pImeEnumRegisterWord((REGISTERWORDENUMPROCW)lpfnEnumProc,
872 lpszwReading, dwStyle, lpszwRegister,
873 lpData);
875 HeapFree(GetProcessHeap(),0,lpszwReading);
876 HeapFree(GetProcessHeap(),0,lpszwRegister);
877 return rc;
880 else
881 return 0;
884 /***********************************************************************
885 * ImmEnumRegisterWordW (IMM32.@)
887 UINT WINAPI ImmEnumRegisterWordW(
888 HKL hKL, REGISTERWORDENUMPROCW lpfnEnumProc,
889 LPCWSTR lpszReading, DWORD dwStyle,
890 LPCWSTR lpszRegister, LPVOID lpData)
892 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
893 TRACE("(%p, %p, %s, %d, %s, %p):\n", hKL, lpfnEnumProc,
894 debugstr_w(lpszReading), dwStyle, debugstr_w(lpszRegister), lpData);
895 if (immHkl->hIME && immHkl->pImeEnumRegisterWord)
897 if (is_kbd_ime_unicode(immHkl))
898 return immHkl->pImeEnumRegisterWord(lpfnEnumProc, lpszReading, dwStyle,
899 lpszRegister, lpData);
900 else
902 LPSTR lpszaReading = strdupWtoA(lpszReading);
903 LPSTR lpszaRegister = strdupWtoA(lpszRegister);
904 BOOL rc;
906 rc = immHkl->pImeEnumRegisterWord(lpfnEnumProc, (LPCWSTR)lpszaReading,
907 dwStyle, (LPCWSTR)lpszaRegister, lpData);
909 HeapFree(GetProcessHeap(),0,lpszaReading);
910 HeapFree(GetProcessHeap(),0,lpszaRegister);
911 return rc;
914 else
915 return 0;
918 static inline BOOL EscapeRequiresWA(UINT uEscape)
920 if (uEscape == IME_ESC_GET_EUDC_DICTIONARY ||
921 uEscape == IME_ESC_SET_EUDC_DICTIONARY ||
922 uEscape == IME_ESC_IME_NAME ||
923 uEscape == IME_ESC_GETHELPFILENAME)
924 return TRUE;
925 return FALSE;
928 /***********************************************************************
929 * ImmEscapeA (IMM32.@)
931 LRESULT WINAPI ImmEscapeA(
932 HKL hKL, HIMC hIMC,
933 UINT uEscape, LPVOID lpData)
935 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
936 TRACE("(%p, %p, %d, %p):\n", hKL, hIMC, uEscape, lpData);
938 if (immHkl->hIME && immHkl->pImeEscape)
940 if (!EscapeRequiresWA(uEscape) || !is_kbd_ime_unicode(immHkl))
941 return immHkl->pImeEscape(hIMC,uEscape,lpData);
942 else
944 WCHAR buffer[81]; /* largest required buffer should be 80 */
945 LRESULT rc;
946 if (uEscape == IME_ESC_SET_EUDC_DICTIONARY)
948 MultiByteToWideChar(CP_ACP,0,lpData,-1,buffer,81);
949 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
951 else
953 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
954 WideCharToMultiByte(CP_ACP,0,buffer,-1,lpData,80, NULL, NULL);
956 return rc;
959 else
960 return 0;
963 /***********************************************************************
964 * ImmEscapeW (IMM32.@)
966 LRESULT WINAPI ImmEscapeW(
967 HKL hKL, HIMC hIMC,
968 UINT uEscape, LPVOID lpData)
970 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
971 TRACE("(%p, %p, %d, %p):\n", hKL, hIMC, uEscape, lpData);
973 if (immHkl->hIME && immHkl->pImeEscape)
975 if (!EscapeRequiresWA(uEscape) || is_kbd_ime_unicode(immHkl))
976 return immHkl->pImeEscape(hIMC,uEscape,lpData);
977 else
979 CHAR buffer[81]; /* largest required buffer should be 80 */
980 LRESULT rc;
981 if (uEscape == IME_ESC_SET_EUDC_DICTIONARY)
983 WideCharToMultiByte(CP_ACP,0,lpData,-1,buffer,81, NULL, NULL);
984 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
986 else
988 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
989 MultiByteToWideChar(CP_ACP,0,buffer,-1,lpData,80);
991 return rc;
994 else
995 return 0;
998 /***********************************************************************
999 * ImmGetCandidateListA (IMM32.@)
1001 DWORD WINAPI ImmGetCandidateListA(
1002 HIMC hIMC, DWORD dwIndex,
1003 LPCANDIDATELIST lpCandList, DWORD dwBufLen)
1005 InputContextData *data = get_imc_data(hIMC);
1006 LPCANDIDATEINFO candinfo;
1007 LPCANDIDATELIST candlist;
1008 DWORD ret = 0;
1010 TRACE("%p, %d, %p, %d\n", hIMC, dwIndex, lpCandList, dwBufLen);
1012 if (!data || !data->IMC.hCandInfo)
1013 return 0;
1015 candinfo = ImmLockIMCC(data->IMC.hCandInfo);
1016 if ( dwIndex >= candinfo->dwCount ||
1017 dwIndex >= (sizeof(candinfo->dwOffset) / sizeof(DWORD)) )
1018 goto done;
1020 candlist = (LPCANDIDATELIST)((LPBYTE)candinfo + candinfo->dwOffset[dwIndex]);
1021 if ( !candlist->dwSize || !candlist->dwCount )
1022 goto done;
1024 if ( !is_himc_ime_unicode(data) )
1026 ret = candlist->dwSize;
1027 if ( lpCandList && dwBufLen >= ret )
1028 memcpy(lpCandList, candlist, ret);
1030 else
1031 ret = convert_candidatelist_WtoA( candlist, lpCandList, dwBufLen);
1033 done:
1034 ImmUnlockIMCC(data->IMC.hCandInfo);
1035 return ret;
1038 /***********************************************************************
1039 * ImmGetCandidateListCountA (IMM32.@)
1041 DWORD WINAPI ImmGetCandidateListCountA(
1042 HIMC hIMC, LPDWORD lpdwListCount)
1044 InputContextData *data = get_imc_data(hIMC);
1045 LPCANDIDATEINFO candinfo;
1046 DWORD ret, count;
1048 TRACE("%p, %p\n", hIMC, lpdwListCount);
1050 if (!data || !lpdwListCount || !data->IMC.hCandInfo)
1051 return 0;
1053 candinfo = ImmLockIMCC(data->IMC.hCandInfo);
1055 *lpdwListCount = count = candinfo->dwCount;
1057 if ( !is_himc_ime_unicode(data) )
1058 ret = candinfo->dwSize;
1059 else
1061 ret = sizeof(CANDIDATEINFO);
1062 while ( count-- )
1063 ret += ImmGetCandidateListA(hIMC, count, NULL, 0);
1066 ImmUnlockIMCC(data->IMC.hCandInfo);
1067 return ret;
1070 /***********************************************************************
1071 * ImmGetCandidateListCountW (IMM32.@)
1073 DWORD WINAPI ImmGetCandidateListCountW(
1074 HIMC hIMC, LPDWORD lpdwListCount)
1076 InputContextData *data = get_imc_data(hIMC);
1077 LPCANDIDATEINFO candinfo;
1078 DWORD ret, count;
1080 TRACE("%p, %p\n", hIMC, lpdwListCount);
1082 if (!data || !lpdwListCount || !data->IMC.hCandInfo)
1083 return 0;
1085 candinfo = ImmLockIMCC(data->IMC.hCandInfo);
1087 *lpdwListCount = count = candinfo->dwCount;
1089 if ( is_himc_ime_unicode(data) )
1090 ret = candinfo->dwSize;
1091 else
1093 ret = sizeof(CANDIDATEINFO);
1094 while ( count-- )
1095 ret += ImmGetCandidateListW(hIMC, count, NULL, 0);
1098 ImmUnlockIMCC(data->IMC.hCandInfo);
1099 return ret;
1102 /***********************************************************************
1103 * ImmGetCandidateListW (IMM32.@)
1105 DWORD WINAPI ImmGetCandidateListW(
1106 HIMC hIMC, DWORD dwIndex,
1107 LPCANDIDATELIST lpCandList, DWORD dwBufLen)
1109 InputContextData *data = get_imc_data(hIMC);
1110 LPCANDIDATEINFO candinfo;
1111 LPCANDIDATELIST candlist;
1112 DWORD ret = 0;
1114 TRACE("%p, %d, %p, %d\n", hIMC, dwIndex, lpCandList, dwBufLen);
1116 if (!data || !data->IMC.hCandInfo)
1117 return 0;
1119 candinfo = ImmLockIMCC(data->IMC.hCandInfo);
1120 if ( dwIndex >= candinfo->dwCount ||
1121 dwIndex >= (sizeof(candinfo->dwOffset) / sizeof(DWORD)) )
1122 goto done;
1124 candlist = (LPCANDIDATELIST)((LPBYTE)candinfo + candinfo->dwOffset[dwIndex]);
1125 if ( !candlist->dwSize || !candlist->dwCount )
1126 goto done;
1128 if ( is_himc_ime_unicode(data) )
1130 ret = candlist->dwSize;
1131 if ( lpCandList && dwBufLen >= ret )
1132 memcpy(lpCandList, candlist, ret);
1134 else
1135 ret = convert_candidatelist_AtoW( candlist, lpCandList, dwBufLen);
1137 done:
1138 ImmUnlockIMCC(data->IMC.hCandInfo);
1139 return ret;
1142 /***********************************************************************
1143 * ImmGetCandidateWindow (IMM32.@)
1145 BOOL WINAPI ImmGetCandidateWindow(
1146 HIMC hIMC, DWORD dwIndex, LPCANDIDATEFORM lpCandidate)
1148 InputContextData *data = get_imc_data(hIMC);
1150 TRACE("%p, %d, %p\n", hIMC, dwIndex, lpCandidate);
1152 if (!data || !lpCandidate)
1153 return FALSE;
1155 if ( dwIndex >= (sizeof(data->IMC.cfCandForm) / sizeof(CANDIDATEFORM)) )
1156 return FALSE;
1158 if (data->IMC.cfCandForm[dwIndex].dwIndex != dwIndex)
1159 return FALSE;
1161 *lpCandidate = data->IMC.cfCandForm[dwIndex];
1163 return TRUE;
1166 /***********************************************************************
1167 * ImmGetCompositionFontA (IMM32.@)
1169 BOOL WINAPI ImmGetCompositionFontA(HIMC hIMC, LPLOGFONTA lplf)
1171 LOGFONTW lfW;
1172 BOOL rc;
1174 TRACE("(%p, %p):\n", hIMC, lplf);
1176 rc = ImmGetCompositionFontW(hIMC,&lfW);
1177 if (!rc || !lplf)
1178 return FALSE;
1180 memcpy(lplf,&lfW,sizeof(LOGFONTA));
1181 WideCharToMultiByte(CP_ACP, 0, lfW.lfFaceName, -1, lplf->lfFaceName,
1182 LF_FACESIZE, NULL, NULL);
1183 return TRUE;
1186 /***********************************************************************
1187 * ImmGetCompositionFontW (IMM32.@)
1189 BOOL WINAPI ImmGetCompositionFontW(HIMC hIMC, LPLOGFONTW lplf)
1191 InputContextData *data = get_imc_data(hIMC);
1193 TRACE("(%p, %p):\n", hIMC, lplf);
1195 if (!data || !lplf)
1196 return FALSE;
1198 *lplf = data->IMC.lfFont.W;
1200 return TRUE;
1204 /* Helpers for the GetCompositionString functions */
1206 static INT CopyCompStringIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE target, INT tlen,
1207 BOOL unicode )
1209 INT rc;
1211 if (is_himc_ime_unicode(data) && !unicode)
1212 rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)source, slen, (LPSTR)target, tlen, NULL, NULL);
1213 else if (!is_himc_ime_unicode(data) && unicode)
1214 rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)source, slen, (LPWSTR)target, tlen) * sizeof(WCHAR);
1215 else
1217 int dlen = (unicode)?sizeof(WCHAR):sizeof(CHAR);
1218 memcpy( target, source, min(slen,tlen)*dlen);
1219 rc = slen*dlen;
1222 return rc;
1225 static INT CopyCompAttrIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE ssource, INT sslen,
1226 LPBYTE target, INT tlen, BOOL unicode )
1228 INT rc;
1230 if (is_himc_ime_unicode(data) && !unicode)
1232 rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource, sslen, NULL, 0, NULL, NULL);
1233 if (tlen)
1235 const BYTE *src = source;
1236 LPBYTE dst = target;
1237 int i, j = 0, k = 0;
1239 if (rc < tlen)
1240 tlen = rc;
1241 for (i = 0; i < sslen; ++i)
1243 int len;
1245 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)ssource + i, 1,
1246 NULL, 0, NULL, NULL);
1247 for (; len > 0; --len)
1249 dst[j++] = src[k];
1251 if (j >= tlen)
1252 goto end;
1254 ++k;
1256 end:
1257 rc = j;
1260 else if (!is_himc_ime_unicode(data) && unicode)
1262 rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource, sslen, NULL, 0);
1263 if (tlen)
1265 const BYTE *src = source;
1266 LPBYTE dst = target;
1267 int i, j = 0;
1269 if (rc < tlen)
1270 tlen = rc;
1271 for (i = 0; i < sslen; ++i)
1273 if (IsDBCSLeadByte(((LPSTR)ssource)[i]))
1274 continue;
1276 dst[j++] = src[i];
1278 if (j >= tlen)
1279 break;
1281 rc = j;
1284 else
1286 memcpy( target, source, min(slen,tlen));
1287 rc = slen;
1290 return rc;
1293 static INT CopyCompClauseIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE ssource,
1294 LPBYTE target, INT tlen, BOOL unicode )
1296 INT rc;
1298 if (is_himc_ime_unicode(data) && !unicode)
1300 if (tlen)
1302 int i;
1304 if (slen < tlen)
1305 tlen = slen;
1306 tlen /= sizeof (DWORD);
1307 for (i = 0; i < tlen; ++i)
1309 ((DWORD *)target)[i] = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource,
1310 ((DWORD *)source)[i],
1311 NULL, 0,
1312 NULL, NULL);
1314 rc = sizeof (DWORD) * i;
1316 else
1317 rc = slen;
1319 else if (!is_himc_ime_unicode(data) && unicode)
1321 if (tlen)
1323 int i;
1325 if (slen < tlen)
1326 tlen = slen;
1327 tlen /= sizeof (DWORD);
1328 for (i = 0; i < tlen; ++i)
1330 ((DWORD *)target)[i] = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource,
1331 ((DWORD *)source)[i],
1332 NULL, 0);
1334 rc = sizeof (DWORD) * i;
1336 else
1337 rc = slen;
1339 else
1341 memcpy( target, source, min(slen,tlen));
1342 rc = slen;
1345 return rc;
1348 static INT CopyCompOffsetIMEtoClient(InputContextData *data, DWORD offset, LPBYTE ssource, BOOL unicode)
1350 int rc;
1352 if (is_himc_ime_unicode(data) && !unicode)
1354 rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource, offset, NULL, 0, NULL, NULL);
1356 else if (!is_himc_ime_unicode(data) && unicode)
1358 rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource, offset, NULL, 0);
1360 else
1361 rc = offset;
1363 return rc;
1366 static LONG ImmGetCompositionStringT( HIMC hIMC, DWORD dwIndex, LPVOID lpBuf,
1367 DWORD dwBufLen, BOOL unicode)
1369 LONG rc = 0;
1370 InputContextData *data = get_imc_data(hIMC);
1371 LPCOMPOSITIONSTRING compstr;
1372 LPBYTE compdata;
1374 TRACE("(%p, 0x%x, %p, %d)\n", hIMC, dwIndex, lpBuf, dwBufLen);
1376 if (!data)
1377 return FALSE;
1379 if (!data->IMC.hCompStr)
1380 return FALSE;
1382 compdata = ImmLockIMCC(data->IMC.hCompStr);
1383 compstr = (LPCOMPOSITIONSTRING)compdata;
1385 switch (dwIndex)
1387 case GCS_RESULTSTR:
1388 TRACE("GCS_RESULTSTR\n");
1389 rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwResultStrOffset, compstr->dwResultStrLen, lpBuf, dwBufLen, unicode);
1390 break;
1391 case GCS_COMPSTR:
1392 TRACE("GCS_COMPSTR\n");
1393 rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen, lpBuf, dwBufLen, unicode);
1394 break;
1395 case GCS_COMPATTR:
1396 TRACE("GCS_COMPATTR\n");
1397 rc = CopyCompAttrIMEtoClient(data, compdata + compstr->dwCompAttrOffset, compstr->dwCompAttrLen,
1398 compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen,
1399 lpBuf, dwBufLen, unicode);
1400 break;
1401 case GCS_COMPCLAUSE:
1402 TRACE("GCS_COMPCLAUSE\n");
1403 rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwCompClauseOffset,compstr->dwCompClauseLen,
1404 compdata + compstr->dwCompStrOffset,
1405 lpBuf, dwBufLen, unicode);
1406 break;
1407 case GCS_RESULTCLAUSE:
1408 TRACE("GCS_RESULTCLAUSE\n");
1409 rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwResultClauseOffset,compstr->dwResultClauseLen,
1410 compdata + compstr->dwResultStrOffset,
1411 lpBuf, dwBufLen, unicode);
1412 break;
1413 case GCS_RESULTREADSTR:
1414 TRACE("GCS_RESULTREADSTR\n");
1415 rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwResultReadStrOffset, compstr->dwResultReadStrLen, lpBuf, dwBufLen, unicode);
1416 break;
1417 case GCS_RESULTREADCLAUSE:
1418 TRACE("GCS_RESULTREADCLAUSE\n");
1419 rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwResultReadClauseOffset,compstr->dwResultReadClauseLen,
1420 compdata + compstr->dwResultStrOffset,
1421 lpBuf, dwBufLen, unicode);
1422 break;
1423 case GCS_COMPREADSTR:
1424 TRACE("GCS_COMPREADSTR\n");
1425 rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwCompReadStrOffset, compstr->dwCompReadStrLen, lpBuf, dwBufLen, unicode);
1426 break;
1427 case GCS_COMPREADATTR:
1428 TRACE("GCS_COMPREADATTR\n");
1429 rc = CopyCompAttrIMEtoClient(data, compdata + compstr->dwCompReadAttrOffset, compstr->dwCompReadAttrLen,
1430 compdata + compstr->dwCompReadStrOffset, compstr->dwCompReadStrLen,
1431 lpBuf, dwBufLen, unicode);
1432 break;
1433 case GCS_COMPREADCLAUSE:
1434 TRACE("GCS_COMPREADCLAUSE\n");
1435 rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwCompReadClauseOffset,compstr->dwCompReadClauseLen,
1436 compdata + compstr->dwCompStrOffset,
1437 lpBuf, dwBufLen, unicode);
1438 break;
1439 case GCS_CURSORPOS:
1440 TRACE("GCS_CURSORPOS\n");
1441 rc = CopyCompOffsetIMEtoClient(data, compstr->dwCursorPos, compdata + compstr->dwCompStrOffset, unicode);
1442 break;
1443 case GCS_DELTASTART:
1444 TRACE("GCS_DELTASTART\n");
1445 rc = CopyCompOffsetIMEtoClient(data, compstr->dwDeltaStart, compdata + compstr->dwCompStrOffset, unicode);
1446 break;
1447 default:
1448 FIXME("Unhandled index 0x%x\n",dwIndex);
1449 break;
1452 ImmUnlockIMCC(data->IMC.hCompStr);
1454 return rc;
1457 /***********************************************************************
1458 * ImmGetCompositionStringA (IMM32.@)
1460 LONG WINAPI ImmGetCompositionStringA(
1461 HIMC hIMC, DWORD dwIndex, LPVOID lpBuf, DWORD dwBufLen)
1463 return ImmGetCompositionStringT(hIMC, dwIndex, lpBuf, dwBufLen, FALSE);
1467 /***********************************************************************
1468 * ImmGetCompositionStringW (IMM32.@)
1470 LONG WINAPI ImmGetCompositionStringW(
1471 HIMC hIMC, DWORD dwIndex,
1472 LPVOID lpBuf, DWORD dwBufLen)
1474 return ImmGetCompositionStringT(hIMC, dwIndex, lpBuf, dwBufLen, TRUE);
1477 /***********************************************************************
1478 * ImmGetCompositionWindow (IMM32.@)
1480 BOOL WINAPI ImmGetCompositionWindow(HIMC hIMC, LPCOMPOSITIONFORM lpCompForm)
1482 InputContextData *data = get_imc_data(hIMC);
1484 TRACE("(%p, %p)\n", hIMC, lpCompForm);
1486 if (!data)
1487 return FALSE;
1489 *lpCompForm = data->IMC.cfCompForm;
1490 return TRUE;
1493 /***********************************************************************
1494 * ImmGetContext (IMM32.@)
1497 HIMC WINAPI ImmGetContext(HWND hWnd)
1499 HIMC rc;
1501 TRACE("%p\n", hWnd);
1503 if (!IsWindow(hWnd))
1505 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
1506 return NULL;
1509 rc = GetPropW(hWnd,szwWineIMCProperty);
1510 if (rc == (HIMC)-1)
1511 rc = NULL;
1512 else if (rc == NULL)
1513 rc = get_default_context( hWnd );
1515 if (rc)
1517 InputContextData *data = rc;
1518 data->IMC.hWnd = hWnd;
1521 TRACE("returning %p\n", rc);
1523 return rc;
1526 /***********************************************************************
1527 * ImmGetConversionListA (IMM32.@)
1529 DWORD WINAPI ImmGetConversionListA(
1530 HKL hKL, HIMC hIMC,
1531 LPCSTR pSrc, LPCANDIDATELIST lpDst,
1532 DWORD dwBufLen, UINT uFlag)
1534 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1535 TRACE("(%p, %p, %s, %p, %d, %d):\n", hKL, hIMC, debugstr_a(pSrc), lpDst,
1536 dwBufLen, uFlag);
1537 if (immHkl->hIME && immHkl->pImeConversionList)
1539 if (!is_kbd_ime_unicode(immHkl))
1540 return immHkl->pImeConversionList(hIMC,(LPCWSTR)pSrc,lpDst,dwBufLen,uFlag);
1541 else
1543 LPCANDIDATELIST lpwDst;
1544 DWORD ret = 0, len;
1545 LPWSTR pwSrc = strdupAtoW(pSrc);
1547 len = immHkl->pImeConversionList(hIMC, pwSrc, NULL, 0, uFlag);
1548 lpwDst = HeapAlloc(GetProcessHeap(), 0, len);
1549 if ( lpwDst )
1551 immHkl->pImeConversionList(hIMC, pwSrc, lpwDst, len, uFlag);
1552 ret = convert_candidatelist_WtoA( lpwDst, lpDst, dwBufLen);
1553 HeapFree(GetProcessHeap(), 0, lpwDst);
1555 HeapFree(GetProcessHeap(), 0, pwSrc);
1557 return ret;
1560 else
1561 return 0;
1564 /***********************************************************************
1565 * ImmGetConversionListW (IMM32.@)
1567 DWORD WINAPI ImmGetConversionListW(
1568 HKL hKL, HIMC hIMC,
1569 LPCWSTR pSrc, LPCANDIDATELIST lpDst,
1570 DWORD dwBufLen, UINT uFlag)
1572 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1573 TRACE("(%p, %p, %s, %p, %d, %d):\n", hKL, hIMC, debugstr_w(pSrc), lpDst,
1574 dwBufLen, uFlag);
1575 if (immHkl->hIME && immHkl->pImeConversionList)
1577 if (is_kbd_ime_unicode(immHkl))
1578 return immHkl->pImeConversionList(hIMC,pSrc,lpDst,dwBufLen,uFlag);
1579 else
1581 LPCANDIDATELIST lpaDst;
1582 DWORD ret = 0, len;
1583 LPSTR paSrc = strdupWtoA(pSrc);
1585 len = immHkl->pImeConversionList(hIMC, (LPCWSTR)paSrc, NULL, 0, uFlag);
1586 lpaDst = HeapAlloc(GetProcessHeap(), 0, len);
1587 if ( lpaDst )
1589 immHkl->pImeConversionList(hIMC, (LPCWSTR)paSrc, lpaDst, len, uFlag);
1590 ret = convert_candidatelist_AtoW( lpaDst, lpDst, dwBufLen);
1591 HeapFree(GetProcessHeap(), 0, lpaDst);
1593 HeapFree(GetProcessHeap(), 0, paSrc);
1595 return ret;
1598 else
1599 return 0;
1602 /***********************************************************************
1603 * ImmGetConversionStatus (IMM32.@)
1605 BOOL WINAPI ImmGetConversionStatus(
1606 HIMC hIMC, LPDWORD lpfdwConversion, LPDWORD lpfdwSentence)
1608 InputContextData *data = get_imc_data(hIMC);
1610 TRACE("%p %p %p\n", hIMC, lpfdwConversion, lpfdwSentence);
1612 if (!data)
1613 return FALSE;
1615 if (lpfdwConversion)
1616 *lpfdwConversion = data->IMC.fdwConversion;
1617 if (lpfdwSentence)
1618 *lpfdwSentence = data->IMC.fdwSentence;
1620 return TRUE;
1623 static BOOL needs_ime_window(HWND hwnd)
1625 WCHAR classW[8];
1627 if (GetClassNameW(hwnd, classW, sizeof(classW)/sizeof(classW[0])) &&
1628 !strcmpW(classW, szwIME))
1629 return FALSE;
1630 if (GetClassLongPtrW(hwnd, GCL_STYLE) & CS_IME) return FALSE;
1632 return TRUE;
1635 /***********************************************************************
1636 * __wine_register_window (IMM32.@)
1638 BOOL WINAPI __wine_register_window(HWND hwnd)
1640 HWND new = NULL;
1641 IMMThreadData *thread_data;
1642 TRACE("(%p)\n", hwnd);
1644 if (!needs_ime_window(hwnd))
1645 return FALSE;
1647 thread_data = IMM_GetThreadData(hwnd, 0);
1648 if (!thread_data)
1649 return FALSE;
1651 if (thread_data->disableIME || disable_ime)
1653 TRACE("IME for this thread is disabled\n");
1654 LeaveCriticalSection(&threaddata_cs);
1655 return FALSE;
1657 thread_data->windowRefs++;
1658 TRACE("windowRefs=%u, hwndDefault=%p\n",
1659 thread_data->windowRefs, thread_data->hwndDefault);
1661 /* Create default IME window */
1662 if (thread_data->windowRefs == 1)
1664 /* Do not create the window inside of a critical section */
1665 LeaveCriticalSection(&threaddata_cs);
1666 new = CreateWindowExW( 0, szwIME, szwDefaultIME,
1667 WS_POPUP | WS_DISABLED | WS_CLIPSIBLINGS,
1668 0, 0, 1, 1, 0, 0, 0, 0);
1669 /* thread_data is in the current thread so we can assume it's still valid */
1670 EnterCriticalSection(&threaddata_cs);
1671 /* See if anyone beat us */
1672 if (thread_data->hwndDefault == NULL)
1674 thread_data->hwndDefault = new;
1675 new = NULL;
1676 TRACE("Default is %p\n", thread_data->hwndDefault);
1680 LeaveCriticalSection(&threaddata_cs);
1682 /* Clean up an unused new window outside of the critical section */
1683 if (new != NULL)
1684 DestroyWindow(new);
1685 return TRUE;
1688 /***********************************************************************
1689 * __wine_unregister_window (IMM32.@)
1691 void WINAPI __wine_unregister_window(HWND hwnd)
1693 HWND to_destroy = 0;
1694 IMMThreadData *thread_data;
1695 TRACE("(%p)\n", hwnd);
1697 thread_data = IMM_GetThreadData(hwnd, 0);
1698 if (!thread_data) return;
1700 thread_data->windowRefs--;
1701 TRACE("windowRefs=%u, hwndDefault=%p\n",
1702 thread_data->windowRefs, thread_data->hwndDefault);
1704 /* Destroy default IME window */
1705 if (thread_data->windowRefs == 0 && thread_data->hwndDefault)
1707 to_destroy = thread_data->hwndDefault;
1708 thread_data->hwndDefault = NULL;
1710 LeaveCriticalSection(&threaddata_cs);
1712 if (to_destroy) DestroyWindow( to_destroy );
1715 /***********************************************************************
1716 * ImmGetDefaultIMEWnd (IMM32.@)
1718 HWND WINAPI ImmGetDefaultIMEWnd(HWND hWnd)
1720 HWND ret;
1721 IMMThreadData* thread_data = IMM_GetThreadData(hWnd, 0);
1722 if (!thread_data)
1723 return NULL;
1724 ret = thread_data->hwndDefault;
1725 LeaveCriticalSection(&threaddata_cs);
1726 TRACE("Default is %p\n",ret);
1727 return ret;
1730 /***********************************************************************
1731 * ImmGetDescriptionA (IMM32.@)
1733 UINT WINAPI ImmGetDescriptionA(
1734 HKL hKL, LPSTR lpszDescription, UINT uBufLen)
1736 WCHAR *buf;
1737 DWORD len;
1739 TRACE("%p %p %d\n", hKL, lpszDescription, uBufLen);
1741 /* find out how many characters in the unicode buffer */
1742 len = ImmGetDescriptionW( hKL, NULL, 0 );
1743 if (!len)
1744 return 0;
1746 /* allocate a buffer of that size */
1747 buf = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof (WCHAR) );
1748 if( !buf )
1749 return 0;
1751 /* fetch the unicode buffer */
1752 len = ImmGetDescriptionW( hKL, buf, len + 1 );
1754 /* convert it back to ASCII */
1755 len = WideCharToMultiByte( CP_ACP, 0, buf, len + 1,
1756 lpszDescription, uBufLen, NULL, NULL );
1758 HeapFree( GetProcessHeap(), 0, buf );
1760 if (len == 0)
1761 return 0;
1763 return len - 1;
1766 /***********************************************************************
1767 * ImmGetDescriptionW (IMM32.@)
1769 UINT WINAPI ImmGetDescriptionW(HKL hKL, LPWSTR lpszDescription, UINT uBufLen)
1771 static const WCHAR name[] = { 'W','i','n','e',' ','X','I','M',0 };
1773 FIXME("(%p, %p, %d): semi stub\n", hKL, lpszDescription, uBufLen);
1775 if (!hKL) return 0;
1776 if (!uBufLen) return lstrlenW( name );
1777 lstrcpynW( lpszDescription, name, uBufLen );
1778 return lstrlenW( lpszDescription );
1781 /***********************************************************************
1782 * ImmGetGuideLineA (IMM32.@)
1784 DWORD WINAPI ImmGetGuideLineA(
1785 HIMC hIMC, DWORD dwIndex, LPSTR lpBuf, DWORD dwBufLen)
1787 FIXME("(%p, %d, %s, %d): stub\n",
1788 hIMC, dwIndex, debugstr_a(lpBuf), dwBufLen
1790 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1791 return 0;
1794 /***********************************************************************
1795 * ImmGetGuideLineW (IMM32.@)
1797 DWORD WINAPI ImmGetGuideLineW(HIMC hIMC, DWORD dwIndex, LPWSTR lpBuf, DWORD dwBufLen)
1799 FIXME("(%p, %d, %s, %d): stub\n",
1800 hIMC, dwIndex, debugstr_w(lpBuf), dwBufLen
1802 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1803 return 0;
1806 /***********************************************************************
1807 * ImmGetIMEFileNameA (IMM32.@)
1809 UINT WINAPI ImmGetIMEFileNameA( HKL hKL, LPSTR lpszFileName, UINT uBufLen)
1811 LPWSTR bufW = NULL;
1812 UINT wBufLen = uBufLen;
1813 UINT rc;
1815 if (uBufLen && lpszFileName)
1816 bufW = HeapAlloc(GetProcessHeap(),0,uBufLen * sizeof(WCHAR));
1817 else /* We need this to get the number of byte required */
1819 bufW = HeapAlloc(GetProcessHeap(),0,MAX_PATH * sizeof(WCHAR));
1820 wBufLen = MAX_PATH;
1823 rc = ImmGetIMEFileNameW(hKL,bufW,wBufLen);
1825 if (rc > 0)
1827 if (uBufLen && lpszFileName)
1828 rc = WideCharToMultiByte(CP_ACP, 0, bufW, -1, lpszFileName,
1829 uBufLen, NULL, NULL);
1830 else /* get the length */
1831 rc = WideCharToMultiByte(CP_ACP, 0, bufW, -1, NULL, 0, NULL,
1832 NULL);
1835 HeapFree(GetProcessHeap(),0,bufW);
1836 return rc;
1839 /***********************************************************************
1840 * ImmGetIMEFileNameW (IMM32.@)
1842 UINT WINAPI ImmGetIMEFileNameW(HKL hKL, LPWSTR lpszFileName, UINT uBufLen)
1844 HKEY hkey;
1845 DWORD length;
1846 DWORD rc;
1847 WCHAR regKey[sizeof(szImeRegFmt)/sizeof(WCHAR)+8];
1849 wsprintfW( regKey, szImeRegFmt, (ULONG_PTR)hKL );
1850 rc = RegOpenKeyW( HKEY_LOCAL_MACHINE, regKey, &hkey);
1851 if (rc != ERROR_SUCCESS)
1853 SetLastError(rc);
1854 return 0;
1857 length = 0;
1858 rc = RegGetValueW(hkey, NULL, szImeFileW, RRF_RT_REG_SZ, NULL, NULL, &length);
1860 if (rc != ERROR_SUCCESS)
1862 RegCloseKey(hkey);
1863 SetLastError(rc);
1864 return 0;
1866 if (length > uBufLen * sizeof(WCHAR) || !lpszFileName)
1868 RegCloseKey(hkey);
1869 if (lpszFileName)
1871 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1872 return 0;
1874 else
1875 return length / sizeof(WCHAR);
1878 RegGetValueW(hkey, NULL, szImeFileW, RRF_RT_REG_SZ, NULL, lpszFileName, &length);
1880 RegCloseKey(hkey);
1882 return length / sizeof(WCHAR);
1885 /***********************************************************************
1886 * ImmGetOpenStatus (IMM32.@)
1888 BOOL WINAPI ImmGetOpenStatus(HIMC hIMC)
1890 InputContextData *data = get_imc_data(hIMC);
1891 static int i;
1893 if (!data)
1894 return FALSE;
1896 TRACE("(%p): semi-stub\n", hIMC);
1898 if (!i++)
1899 FIXME("(%p): semi-stub\n", hIMC);
1901 return data->IMC.fOpen;
1904 /***********************************************************************
1905 * ImmGetProperty (IMM32.@)
1907 DWORD WINAPI ImmGetProperty(HKL hKL, DWORD fdwIndex)
1909 DWORD rc = 0;
1910 ImmHkl *kbd;
1912 TRACE("(%p, %d)\n", hKL, fdwIndex);
1913 kbd = IMM_GetImmHkl(hKL);
1915 if (kbd && kbd->hIME)
1917 switch (fdwIndex)
1919 case IGP_PROPERTY: rc = kbd->imeInfo.fdwProperty; break;
1920 case IGP_CONVERSION: rc = kbd->imeInfo.fdwConversionCaps; break;
1921 case IGP_SENTENCE: rc = kbd->imeInfo.fdwSentenceCaps; break;
1922 case IGP_SETCOMPSTR: rc = kbd->imeInfo.fdwSCSCaps; break;
1923 case IGP_SELECT: rc = kbd->imeInfo.fdwSelectCaps; break;
1924 case IGP_GETIMEVERSION: rc = IMEVER_0400; break;
1925 case IGP_UI: rc = 0; break;
1926 default: rc = 0;
1929 return rc;
1932 /***********************************************************************
1933 * ImmGetRegisterWordStyleA (IMM32.@)
1935 UINT WINAPI ImmGetRegisterWordStyleA(
1936 HKL hKL, UINT nItem, LPSTYLEBUFA lpStyleBuf)
1938 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1939 TRACE("(%p, %d, %p):\n", hKL, nItem, lpStyleBuf);
1940 if (immHkl->hIME && immHkl->pImeGetRegisterWordStyle)
1942 if (!is_kbd_ime_unicode(immHkl))
1943 return immHkl->pImeGetRegisterWordStyle(nItem,(LPSTYLEBUFW)lpStyleBuf);
1944 else
1946 STYLEBUFW sbw;
1947 UINT rc;
1949 rc = immHkl->pImeGetRegisterWordStyle(nItem,&sbw);
1950 WideCharToMultiByte(CP_ACP, 0, sbw.szDescription, -1,
1951 lpStyleBuf->szDescription, 32, NULL, NULL);
1952 lpStyleBuf->dwStyle = sbw.dwStyle;
1953 return rc;
1956 else
1957 return 0;
1960 /***********************************************************************
1961 * ImmGetRegisterWordStyleW (IMM32.@)
1963 UINT WINAPI ImmGetRegisterWordStyleW(
1964 HKL hKL, UINT nItem, LPSTYLEBUFW lpStyleBuf)
1966 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1967 TRACE("(%p, %d, %p):\n", hKL, nItem, lpStyleBuf);
1968 if (immHkl->hIME && immHkl->pImeGetRegisterWordStyle)
1970 if (is_kbd_ime_unicode(immHkl))
1971 return immHkl->pImeGetRegisterWordStyle(nItem,lpStyleBuf);
1972 else
1974 STYLEBUFA sba;
1975 UINT rc;
1977 rc = immHkl->pImeGetRegisterWordStyle(nItem,(LPSTYLEBUFW)&sba);
1978 MultiByteToWideChar(CP_ACP, 0, sba.szDescription, -1,
1979 lpStyleBuf->szDescription, 32);
1980 lpStyleBuf->dwStyle = sba.dwStyle;
1981 return rc;
1984 else
1985 return 0;
1988 /***********************************************************************
1989 * ImmGetStatusWindowPos (IMM32.@)
1991 BOOL WINAPI ImmGetStatusWindowPos(HIMC hIMC, LPPOINT lpptPos)
1993 InputContextData *data = get_imc_data(hIMC);
1995 TRACE("(%p, %p)\n", hIMC, lpptPos);
1997 if (!data || !lpptPos)
1998 return FALSE;
2000 *lpptPos = data->IMC.ptStatusWndPos;
2002 return TRUE;
2005 /***********************************************************************
2006 * ImmGetVirtualKey (IMM32.@)
2008 UINT WINAPI ImmGetVirtualKey(HWND hWnd)
2010 OSVERSIONINFOA version;
2011 InputContextData *data = ImmGetContext( hWnd );
2012 TRACE("%p\n", hWnd);
2014 if ( data )
2015 return data->lastVK;
2017 version.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
2018 GetVersionExA( &version );
2019 switch(version.dwPlatformId)
2021 case VER_PLATFORM_WIN32_WINDOWS:
2022 return VK_PROCESSKEY;
2023 case VER_PLATFORM_WIN32_NT:
2024 return 0;
2025 default:
2026 FIXME("%d not supported\n",version.dwPlatformId);
2027 return VK_PROCESSKEY;
2031 /***********************************************************************
2032 * ImmInstallIMEA (IMM32.@)
2034 HKL WINAPI ImmInstallIMEA(
2035 LPCSTR lpszIMEFileName, LPCSTR lpszLayoutText)
2037 LPWSTR lpszwIMEFileName;
2038 LPWSTR lpszwLayoutText;
2039 HKL hkl;
2041 TRACE ("(%s, %s)\n", debugstr_a(lpszIMEFileName),
2042 debugstr_a(lpszLayoutText));
2044 lpszwIMEFileName = strdupAtoW(lpszIMEFileName);
2045 lpszwLayoutText = strdupAtoW(lpszLayoutText);
2047 hkl = ImmInstallIMEW(lpszwIMEFileName, lpszwLayoutText);
2049 HeapFree(GetProcessHeap(),0,lpszwIMEFileName);
2050 HeapFree(GetProcessHeap(),0,lpszwLayoutText);
2051 return hkl;
2054 /***********************************************************************
2055 * ImmInstallIMEW (IMM32.@)
2057 HKL WINAPI ImmInstallIMEW(
2058 LPCWSTR lpszIMEFileName, LPCWSTR lpszLayoutText)
2060 INT lcid = GetUserDefaultLCID();
2061 INT count;
2062 HKL hkl;
2063 DWORD rc;
2064 HKEY hkey;
2065 WCHAR regKey[sizeof(szImeRegFmt)/sizeof(WCHAR)+8];
2067 TRACE ("(%s, %s):\n", debugstr_w(lpszIMEFileName),
2068 debugstr_w(lpszLayoutText));
2070 /* Start with 2. e001 will be blank and so default to the wine internal IME */
2071 count = 2;
2073 while (count < 0xfff)
2075 DWORD disposition = 0;
2077 hkl = (HKL)MAKELPARAM( lcid, 0xe000 | count );
2078 wsprintfW( regKey, szImeRegFmt, (ULONG_PTR)hkl);
2080 rc = RegCreateKeyExW(HKEY_LOCAL_MACHINE, regKey, 0, NULL, 0, KEY_WRITE, NULL, &hkey, &disposition);
2081 if (rc == ERROR_SUCCESS && disposition == REG_CREATED_NEW_KEY)
2082 break;
2083 else if (rc == ERROR_SUCCESS)
2084 RegCloseKey(hkey);
2086 count++;
2089 if (count == 0xfff)
2091 WARN("Unable to find slot to install IME\n");
2092 return 0;
2095 if (rc == ERROR_SUCCESS)
2097 rc = RegSetValueExW(hkey, szImeFileW, 0, REG_SZ, (const BYTE*)lpszIMEFileName,
2098 (lstrlenW(lpszIMEFileName) + 1) * sizeof(WCHAR));
2099 if (rc == ERROR_SUCCESS)
2100 rc = RegSetValueExW(hkey, szLayoutTextW, 0, REG_SZ, (const BYTE*)lpszLayoutText,
2101 (lstrlenW(lpszLayoutText) + 1) * sizeof(WCHAR));
2102 RegCloseKey(hkey);
2103 return hkl;
2105 else
2107 WARN("Unable to set IME registry values\n");
2108 return 0;
2112 /***********************************************************************
2113 * ImmIsIME (IMM32.@)
2115 BOOL WINAPI ImmIsIME(HKL hKL)
2117 ImmHkl *ptr;
2118 TRACE("(%p):\n", hKL);
2119 ptr = IMM_GetImmHkl(hKL);
2120 return (ptr && ptr->hIME);
2123 /***********************************************************************
2124 * ImmIsUIMessageA (IMM32.@)
2126 BOOL WINAPI ImmIsUIMessageA(
2127 HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam)
2129 TRACE("(%p, %x, %ld, %ld)\n", hWndIME, msg, wParam, lParam);
2130 if ((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
2131 (msg == WM_IME_SETCONTEXT) ||
2132 (msg == WM_IME_NOTIFY) ||
2133 (msg == WM_IME_COMPOSITIONFULL) ||
2134 (msg == WM_IME_SELECT) ||
2135 (msg == 0x287 /* FIXME: WM_IME_SYSTEM */))
2137 if (hWndIME)
2138 SendMessageA(hWndIME, msg, wParam, lParam);
2140 return TRUE;
2142 return FALSE;
2145 /***********************************************************************
2146 * ImmIsUIMessageW (IMM32.@)
2148 BOOL WINAPI ImmIsUIMessageW(
2149 HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam)
2151 TRACE("(%p, %x, %ld, %ld)\n", hWndIME, msg, wParam, lParam);
2152 if ((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
2153 (msg == WM_IME_SETCONTEXT) ||
2154 (msg == WM_IME_NOTIFY) ||
2155 (msg == WM_IME_COMPOSITIONFULL) ||
2156 (msg == WM_IME_SELECT) ||
2157 (msg == 0x287 /* FIXME: WM_IME_SYSTEM */))
2159 if (hWndIME)
2160 SendMessageW(hWndIME, msg, wParam, lParam);
2162 return TRUE;
2164 return FALSE;
2167 /***********************************************************************
2168 * ImmNotifyIME (IMM32.@)
2170 BOOL WINAPI ImmNotifyIME(
2171 HIMC hIMC, DWORD dwAction, DWORD dwIndex, DWORD dwValue)
2173 InputContextData *data = get_imc_data(hIMC);
2175 TRACE("(%p, %d, %d, %d)\n",
2176 hIMC, dwAction, dwIndex, dwValue);
2178 if (hIMC == NULL)
2180 SetLastError(ERROR_SUCCESS);
2181 return FALSE;
2184 if (!data || ! data->immKbd->pNotifyIME)
2186 return FALSE;
2189 return data->immKbd->pNotifyIME(hIMC,dwAction,dwIndex,dwValue);
2192 /***********************************************************************
2193 * ImmRegisterWordA (IMM32.@)
2195 BOOL WINAPI ImmRegisterWordA(
2196 HKL hKL, LPCSTR lpszReading, DWORD dwStyle, LPCSTR lpszRegister)
2198 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
2199 TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_a(lpszReading), dwStyle,
2200 debugstr_a(lpszRegister));
2201 if (immHkl->hIME && immHkl->pImeRegisterWord)
2203 if (!is_kbd_ime_unicode(immHkl))
2204 return immHkl->pImeRegisterWord((LPCWSTR)lpszReading,dwStyle,
2205 (LPCWSTR)lpszRegister);
2206 else
2208 LPWSTR lpszwReading = strdupAtoW(lpszReading);
2209 LPWSTR lpszwRegister = strdupAtoW(lpszRegister);
2210 BOOL rc;
2212 rc = immHkl->pImeRegisterWord(lpszwReading,dwStyle,lpszwRegister);
2213 HeapFree(GetProcessHeap(),0,lpszwReading);
2214 HeapFree(GetProcessHeap(),0,lpszwRegister);
2215 return rc;
2218 else
2219 return FALSE;
2222 /***********************************************************************
2223 * ImmRegisterWordW (IMM32.@)
2225 BOOL WINAPI ImmRegisterWordW(
2226 HKL hKL, LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszRegister)
2228 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
2229 TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_w(lpszReading), dwStyle,
2230 debugstr_w(lpszRegister));
2231 if (immHkl->hIME && immHkl->pImeRegisterWord)
2233 if (is_kbd_ime_unicode(immHkl))
2234 return immHkl->pImeRegisterWord(lpszReading,dwStyle,lpszRegister);
2235 else
2237 LPSTR lpszaReading = strdupWtoA(lpszReading);
2238 LPSTR lpszaRegister = strdupWtoA(lpszRegister);
2239 BOOL rc;
2241 rc = immHkl->pImeRegisterWord((LPCWSTR)lpszaReading,dwStyle,
2242 (LPCWSTR)lpszaRegister);
2243 HeapFree(GetProcessHeap(),0,lpszaReading);
2244 HeapFree(GetProcessHeap(),0,lpszaRegister);
2245 return rc;
2248 else
2249 return FALSE;
2252 /***********************************************************************
2253 * ImmReleaseContext (IMM32.@)
2255 BOOL WINAPI ImmReleaseContext(HWND hWnd, HIMC hIMC)
2257 static BOOL shown = FALSE;
2259 if (!shown) {
2260 FIXME("(%p, %p): stub\n", hWnd, hIMC);
2261 shown = TRUE;
2263 return TRUE;
2266 /***********************************************************************
2267 * ImmRequestMessageA(IMM32.@)
2269 LRESULT WINAPI ImmRequestMessageA(HIMC hIMC, WPARAM wParam, LPARAM lParam)
2271 InputContextData *data = get_imc_data(hIMC);
2273 TRACE("%p %ld %ld\n", hIMC, wParam, wParam);
2275 if (data) return SendMessageA(data->IMC.hWnd, WM_IME_REQUEST, wParam, lParam);
2277 SetLastError(ERROR_INVALID_HANDLE);
2278 return 0;
2281 /***********************************************************************
2282 * ImmRequestMessageW(IMM32.@)
2284 LRESULT WINAPI ImmRequestMessageW(HIMC hIMC, WPARAM wParam, LPARAM lParam)
2286 InputContextData *data = get_imc_data(hIMC);
2288 TRACE("%p %ld %ld\n", hIMC, wParam, wParam);
2290 if (data) return SendMessageW(data->IMC.hWnd, WM_IME_REQUEST, wParam, lParam);
2292 SetLastError(ERROR_INVALID_HANDLE);
2293 return 0;
2296 /***********************************************************************
2297 * ImmSetCandidateWindow (IMM32.@)
2299 BOOL WINAPI ImmSetCandidateWindow(
2300 HIMC hIMC, LPCANDIDATEFORM lpCandidate)
2302 InputContextData *data = get_imc_data(hIMC);
2304 TRACE("(%p, %p)\n", hIMC, lpCandidate);
2306 if (!data || !lpCandidate)
2307 return FALSE;
2309 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2310 return FALSE;
2312 TRACE("\t%x, %x, %s, %s\n",
2313 lpCandidate->dwIndex, lpCandidate->dwStyle,
2314 wine_dbgstr_point(&lpCandidate->ptCurrentPos),
2315 wine_dbgstr_rect(&lpCandidate->rcArea));
2317 if ( lpCandidate->dwIndex >= (sizeof(data->IMC.cfCandForm) / sizeof(CANDIDATEFORM)) )
2318 return FALSE;
2320 data->IMC.cfCandForm[lpCandidate->dwIndex] = *lpCandidate;
2321 ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCANDIDATEPOS);
2322 ImmInternalSendIMENotify(data, IMN_SETCANDIDATEPOS, 1 << lpCandidate->dwIndex);
2324 return TRUE;
2327 /***********************************************************************
2328 * ImmSetCompositionFontA (IMM32.@)
2330 BOOL WINAPI ImmSetCompositionFontA(HIMC hIMC, LPLOGFONTA lplf)
2332 InputContextData *data = get_imc_data(hIMC);
2333 TRACE("(%p, %p)\n", hIMC, lplf);
2335 if (!data || !lplf)
2337 SetLastError(ERROR_INVALID_HANDLE);
2338 return FALSE;
2341 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2342 return FALSE;
2344 memcpy(&data->IMC.lfFont.W,lplf,sizeof(LOGFONTA));
2345 MultiByteToWideChar(CP_ACP, 0, lplf->lfFaceName, -1, data->IMC.lfFont.W.lfFaceName,
2346 LF_FACESIZE);
2347 ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCOMPOSITIONFONT);
2348 ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONFONT, 0);
2350 return TRUE;
2353 /***********************************************************************
2354 * ImmSetCompositionFontW (IMM32.@)
2356 BOOL WINAPI ImmSetCompositionFontW(HIMC hIMC, LPLOGFONTW lplf)
2358 InputContextData *data = get_imc_data(hIMC);
2359 TRACE("(%p, %p)\n", hIMC, lplf);
2361 if (!data || !lplf)
2363 SetLastError(ERROR_INVALID_HANDLE);
2364 return FALSE;
2367 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2368 return FALSE;
2370 data->IMC.lfFont.W = *lplf;
2371 ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCOMPOSITIONFONT);
2372 ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONFONT, 0);
2374 return TRUE;
2377 /***********************************************************************
2378 * ImmSetCompositionStringA (IMM32.@)
2380 BOOL WINAPI ImmSetCompositionStringA(
2381 HIMC hIMC, DWORD dwIndex,
2382 LPCVOID lpComp, DWORD dwCompLen,
2383 LPCVOID lpRead, DWORD dwReadLen)
2385 DWORD comp_len;
2386 DWORD read_len;
2387 WCHAR *CompBuffer = NULL;
2388 WCHAR *ReadBuffer = NULL;
2389 BOOL rc;
2390 InputContextData *data = get_imc_data(hIMC);
2392 TRACE("(%p, %d, %p, %d, %p, %d):\n",
2393 hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
2395 if (!data)
2396 return FALSE;
2398 if (!(dwIndex == SCS_SETSTR ||
2399 dwIndex == SCS_CHANGEATTR ||
2400 dwIndex == SCS_CHANGECLAUSE ||
2401 dwIndex == SCS_SETRECONVERTSTRING ||
2402 dwIndex == SCS_QUERYRECONVERTSTRING))
2403 return FALSE;
2405 if (!is_himc_ime_unicode(data))
2406 return data->immKbd->pImeSetCompositionString(hIMC, dwIndex, lpComp,
2407 dwCompLen, lpRead, dwReadLen);
2409 comp_len = MultiByteToWideChar(CP_ACP, 0, lpComp, dwCompLen, NULL, 0);
2410 if (comp_len)
2412 CompBuffer = HeapAlloc(GetProcessHeap(),0,comp_len * sizeof(WCHAR));
2413 MultiByteToWideChar(CP_ACP, 0, lpComp, dwCompLen, CompBuffer, comp_len);
2416 read_len = MultiByteToWideChar(CP_ACP, 0, lpRead, dwReadLen, NULL, 0);
2417 if (read_len)
2419 ReadBuffer = HeapAlloc(GetProcessHeap(),0,read_len * sizeof(WCHAR));
2420 MultiByteToWideChar(CP_ACP, 0, lpRead, dwReadLen, ReadBuffer, read_len);
2423 rc = ImmSetCompositionStringW(hIMC, dwIndex, CompBuffer, comp_len,
2424 ReadBuffer, read_len);
2426 HeapFree(GetProcessHeap(), 0, CompBuffer);
2427 HeapFree(GetProcessHeap(), 0, ReadBuffer);
2429 return rc;
2432 /***********************************************************************
2433 * ImmSetCompositionStringW (IMM32.@)
2435 BOOL WINAPI ImmSetCompositionStringW(
2436 HIMC hIMC, DWORD dwIndex,
2437 LPCVOID lpComp, DWORD dwCompLen,
2438 LPCVOID lpRead, DWORD dwReadLen)
2440 DWORD comp_len;
2441 DWORD read_len;
2442 CHAR *CompBuffer = NULL;
2443 CHAR *ReadBuffer = NULL;
2444 BOOL rc;
2445 InputContextData *data = get_imc_data(hIMC);
2447 TRACE("(%p, %d, %p, %d, %p, %d):\n",
2448 hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
2450 if (!data)
2451 return FALSE;
2453 if (!(dwIndex == SCS_SETSTR ||
2454 dwIndex == SCS_CHANGEATTR ||
2455 dwIndex == SCS_CHANGECLAUSE ||
2456 dwIndex == SCS_SETRECONVERTSTRING ||
2457 dwIndex == SCS_QUERYRECONVERTSTRING))
2458 return FALSE;
2460 if (is_himc_ime_unicode(data))
2461 return data->immKbd->pImeSetCompositionString(hIMC, dwIndex, lpComp,
2462 dwCompLen, lpRead, dwReadLen);
2464 comp_len = WideCharToMultiByte(CP_ACP, 0, lpComp, dwCompLen, NULL, 0, NULL,
2465 NULL);
2466 if (comp_len)
2468 CompBuffer = HeapAlloc(GetProcessHeap(),0,comp_len);
2469 WideCharToMultiByte(CP_ACP, 0, lpComp, dwCompLen, CompBuffer, comp_len,
2470 NULL, NULL);
2473 read_len = WideCharToMultiByte(CP_ACP, 0, lpRead, dwReadLen, NULL, 0, NULL,
2474 NULL);
2475 if (read_len)
2477 ReadBuffer = HeapAlloc(GetProcessHeap(),0,read_len);
2478 WideCharToMultiByte(CP_ACP, 0, lpRead, dwReadLen, ReadBuffer, read_len,
2479 NULL, NULL);
2482 rc = ImmSetCompositionStringA(hIMC, dwIndex, CompBuffer, comp_len,
2483 ReadBuffer, read_len);
2485 HeapFree(GetProcessHeap(), 0, CompBuffer);
2486 HeapFree(GetProcessHeap(), 0, ReadBuffer);
2488 return rc;
2491 /***********************************************************************
2492 * ImmSetCompositionWindow (IMM32.@)
2494 BOOL WINAPI ImmSetCompositionWindow(
2495 HIMC hIMC, LPCOMPOSITIONFORM lpCompForm)
2497 BOOL reshow = FALSE;
2498 InputContextData *data = get_imc_data(hIMC);
2500 TRACE("(%p, %p)\n", hIMC, lpCompForm);
2501 if (lpCompForm)
2502 TRACE("\t%x, %s, %s\n", lpCompForm->dwStyle,
2503 wine_dbgstr_point(&lpCompForm->ptCurrentPos),
2504 wine_dbgstr_rect(&lpCompForm->rcArea));
2506 if (!data)
2508 SetLastError(ERROR_INVALID_HANDLE);
2509 return FALSE;
2512 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2513 return FALSE;
2515 data->IMC.cfCompForm = *lpCompForm;
2517 if (IsWindowVisible(data->immKbd->UIWnd))
2519 reshow = TRUE;
2520 ShowWindow(data->immKbd->UIWnd,SW_HIDE);
2523 /* FIXME: this is a partial stub */
2525 if (reshow)
2526 ShowWindow(data->immKbd->UIWnd,SW_SHOWNOACTIVATE);
2528 ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONWINDOW, 0);
2529 return TRUE;
2532 /***********************************************************************
2533 * ImmSetConversionStatus (IMM32.@)
2535 BOOL WINAPI ImmSetConversionStatus(
2536 HIMC hIMC, DWORD fdwConversion, DWORD fdwSentence)
2538 DWORD oldConversion, oldSentence;
2539 InputContextData *data = get_imc_data(hIMC);
2541 TRACE("%p %d %d\n", hIMC, fdwConversion, fdwSentence);
2543 if (!data)
2545 SetLastError(ERROR_INVALID_HANDLE);
2546 return FALSE;
2549 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2550 return FALSE;
2552 if ( fdwConversion != data->IMC.fdwConversion )
2554 oldConversion = data->IMC.fdwConversion;
2555 data->IMC.fdwConversion = fdwConversion;
2556 ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, oldConversion, IMC_SETCONVERSIONMODE);
2557 ImmInternalSendIMENotify(data, IMN_SETCONVERSIONMODE, 0);
2559 if ( fdwSentence != data->IMC.fdwSentence )
2561 oldSentence = data->IMC.fdwSentence;
2562 data->IMC.fdwSentence = fdwSentence;
2563 ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, oldSentence, IMC_SETSENTENCEMODE);
2564 ImmInternalSendIMENotify(data, IMN_SETSENTENCEMODE, 0);
2567 return TRUE;
2570 /***********************************************************************
2571 * ImmSetOpenStatus (IMM32.@)
2573 BOOL WINAPI ImmSetOpenStatus(HIMC hIMC, BOOL fOpen)
2575 InputContextData *data = get_imc_data(hIMC);
2577 TRACE("%p %d\n", hIMC, fOpen);
2579 if (!data)
2581 SetLastError(ERROR_INVALID_HANDLE);
2582 return FALSE;
2585 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2586 return FALSE;
2588 if (data->immKbd->UIWnd == NULL)
2590 /* create the ime window */
2591 data->immKbd->UIWnd = CreateWindowExW( WS_EX_TOOLWINDOW,
2592 data->immKbd->imeClassName, NULL, WS_POPUP, 0, 0, 1, 1, 0,
2593 0, data->immKbd->hIME, 0);
2594 SetWindowLongPtrW(data->immKbd->UIWnd, IMMGWL_IMC, (LONG_PTR)data);
2596 else if (fOpen)
2597 SetWindowLongPtrW(data->immKbd->UIWnd, IMMGWL_IMC, (LONG_PTR)data);
2599 if (!fOpen != !data->IMC.fOpen)
2601 data->IMC.fOpen = fOpen;
2602 ImmNotifyIME( hIMC, NI_CONTEXTUPDATED, 0, IMC_SETOPENSTATUS);
2603 ImmInternalSendIMENotify(data, IMN_SETOPENSTATUS, 0);
2606 return TRUE;
2609 /***********************************************************************
2610 * ImmSetStatusWindowPos (IMM32.@)
2612 BOOL WINAPI ImmSetStatusWindowPos(HIMC hIMC, LPPOINT lpptPos)
2614 InputContextData *data = get_imc_data(hIMC);
2616 TRACE("(%p, %p)\n", hIMC, lpptPos);
2618 if (!data || !lpptPos)
2620 SetLastError(ERROR_INVALID_HANDLE);
2621 return FALSE;
2624 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2625 return FALSE;
2627 TRACE("\t%s\n", wine_dbgstr_point(lpptPos));
2629 data->IMC.ptStatusWndPos = *lpptPos;
2630 ImmNotifyIME( hIMC, NI_CONTEXTUPDATED, 0, IMC_SETSTATUSWINDOWPOS);
2631 ImmInternalSendIMENotify(data, IMN_SETSTATUSWINDOWPOS, 0);
2633 return TRUE;
2636 /***********************************************************************
2637 * ImmCreateSoftKeyboard(IMM32.@)
2639 HWND WINAPI ImmCreateSoftKeyboard(UINT uType, UINT hOwner, int x, int y)
2641 FIXME("(%d, %d, %d, %d): stub\n", uType, hOwner, x, y);
2642 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2643 return 0;
2646 /***********************************************************************
2647 * ImmDestroySoftKeyboard(IMM32.@)
2649 BOOL WINAPI ImmDestroySoftKeyboard(HWND hSoftWnd)
2651 FIXME("(%p): stub\n", hSoftWnd);
2652 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2653 return FALSE;
2656 /***********************************************************************
2657 * ImmShowSoftKeyboard(IMM32.@)
2659 BOOL WINAPI ImmShowSoftKeyboard(HWND hSoftWnd, int nCmdShow)
2661 FIXME("(%p, %d): stub\n", hSoftWnd, nCmdShow);
2662 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2663 return FALSE;
2666 /***********************************************************************
2667 * ImmSimulateHotKey (IMM32.@)
2669 BOOL WINAPI ImmSimulateHotKey(HWND hWnd, DWORD dwHotKeyID)
2671 FIXME("(%p, %d): stub\n", hWnd, dwHotKeyID);
2672 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2673 return FALSE;
2676 /***********************************************************************
2677 * ImmUnregisterWordA (IMM32.@)
2679 BOOL WINAPI ImmUnregisterWordA(
2680 HKL hKL, LPCSTR lpszReading, DWORD dwStyle, LPCSTR lpszUnregister)
2682 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
2683 TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_a(lpszReading), dwStyle,
2684 debugstr_a(lpszUnregister));
2685 if (immHkl->hIME && immHkl->pImeUnregisterWord)
2687 if (!is_kbd_ime_unicode(immHkl))
2688 return immHkl->pImeUnregisterWord((LPCWSTR)lpszReading,dwStyle,
2689 (LPCWSTR)lpszUnregister);
2690 else
2692 LPWSTR lpszwReading = strdupAtoW(lpszReading);
2693 LPWSTR lpszwUnregister = strdupAtoW(lpszUnregister);
2694 BOOL rc;
2696 rc = immHkl->pImeUnregisterWord(lpszwReading,dwStyle,lpszwUnregister);
2697 HeapFree(GetProcessHeap(),0,lpszwReading);
2698 HeapFree(GetProcessHeap(),0,lpszwUnregister);
2699 return rc;
2702 else
2703 return FALSE;
2706 /***********************************************************************
2707 * ImmUnregisterWordW (IMM32.@)
2709 BOOL WINAPI ImmUnregisterWordW(
2710 HKL hKL, LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszUnregister)
2712 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
2713 TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_w(lpszReading), dwStyle,
2714 debugstr_w(lpszUnregister));
2715 if (immHkl->hIME && immHkl->pImeUnregisterWord)
2717 if (is_kbd_ime_unicode(immHkl))
2718 return immHkl->pImeUnregisterWord(lpszReading,dwStyle,lpszUnregister);
2719 else
2721 LPSTR lpszaReading = strdupWtoA(lpszReading);
2722 LPSTR lpszaUnregister = strdupWtoA(lpszUnregister);
2723 BOOL rc;
2725 rc = immHkl->pImeUnregisterWord((LPCWSTR)lpszaReading,dwStyle,
2726 (LPCWSTR)lpszaUnregister);
2727 HeapFree(GetProcessHeap(),0,lpszaReading);
2728 HeapFree(GetProcessHeap(),0,lpszaUnregister);
2729 return rc;
2732 else
2733 return FALSE;
2736 /***********************************************************************
2737 * ImmGetImeMenuItemsA (IMM32.@)
2739 DWORD WINAPI ImmGetImeMenuItemsA( HIMC hIMC, DWORD dwFlags, DWORD dwType,
2740 LPIMEMENUITEMINFOA lpImeParentMenu, LPIMEMENUITEMINFOA lpImeMenu,
2741 DWORD dwSize)
2743 InputContextData *data = get_imc_data(hIMC);
2744 TRACE("(%p, %i, %i, %p, %p, %i):\n", hIMC, dwFlags, dwType,
2745 lpImeParentMenu, lpImeMenu, dwSize);
2747 if (!data)
2749 SetLastError(ERROR_INVALID_HANDLE);
2750 return 0;
2753 if (data->immKbd->hIME && data->immKbd->pImeGetImeMenuItems)
2755 if (!is_himc_ime_unicode(data) || (!lpImeParentMenu && !lpImeMenu))
2756 return data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2757 (IMEMENUITEMINFOW*)lpImeParentMenu,
2758 (IMEMENUITEMINFOW*)lpImeMenu, dwSize);
2759 else
2761 IMEMENUITEMINFOW lpImeParentMenuW;
2762 IMEMENUITEMINFOW *lpImeMenuW, *parent = NULL;
2763 DWORD rc;
2765 if (lpImeParentMenu)
2766 parent = &lpImeParentMenuW;
2767 if (lpImeMenu)
2769 int count = dwSize / sizeof(LPIMEMENUITEMINFOA);
2770 dwSize = count * sizeof(IMEMENUITEMINFOW);
2771 lpImeMenuW = HeapAlloc(GetProcessHeap(), 0, dwSize);
2773 else
2774 lpImeMenuW = NULL;
2776 rc = data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2777 parent, lpImeMenuW, dwSize);
2779 if (lpImeParentMenu)
2781 memcpy(lpImeParentMenu,&lpImeParentMenuW,sizeof(IMEMENUITEMINFOA));
2782 lpImeParentMenu->hbmpItem = lpImeParentMenuW.hbmpItem;
2783 WideCharToMultiByte(CP_ACP, 0, lpImeParentMenuW.szString,
2784 -1, lpImeParentMenu->szString, IMEMENUITEM_STRING_SIZE,
2785 NULL, NULL);
2787 if (lpImeMenu && rc)
2789 unsigned int i;
2790 for (i = 0; i < rc; i++)
2792 memcpy(&lpImeMenu[i],&lpImeMenuW[1],sizeof(IMEMENUITEMINFOA));
2793 lpImeMenu[i].hbmpItem = lpImeMenuW[i].hbmpItem;
2794 WideCharToMultiByte(CP_ACP, 0, lpImeMenuW[i].szString,
2795 -1, lpImeMenu[i].szString, IMEMENUITEM_STRING_SIZE,
2796 NULL, NULL);
2799 HeapFree(GetProcessHeap(),0,lpImeMenuW);
2800 return rc;
2803 else
2804 return 0;
2807 /***********************************************************************
2808 * ImmGetImeMenuItemsW (IMM32.@)
2810 DWORD WINAPI ImmGetImeMenuItemsW( HIMC hIMC, DWORD dwFlags, DWORD dwType,
2811 LPIMEMENUITEMINFOW lpImeParentMenu, LPIMEMENUITEMINFOW lpImeMenu,
2812 DWORD dwSize)
2814 InputContextData *data = get_imc_data(hIMC);
2815 TRACE("(%p, %i, %i, %p, %p, %i):\n", hIMC, dwFlags, dwType,
2816 lpImeParentMenu, lpImeMenu, dwSize);
2818 if (!data)
2820 SetLastError(ERROR_INVALID_HANDLE);
2821 return 0;
2824 if (data->immKbd->hIME && data->immKbd->pImeGetImeMenuItems)
2826 if (is_himc_ime_unicode(data) || (!lpImeParentMenu && !lpImeMenu))
2827 return data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2828 lpImeParentMenu, lpImeMenu, dwSize);
2829 else
2831 IMEMENUITEMINFOA lpImeParentMenuA;
2832 IMEMENUITEMINFOA *lpImeMenuA, *parent = NULL;
2833 DWORD rc;
2835 if (lpImeParentMenu)
2836 parent = &lpImeParentMenuA;
2837 if (lpImeMenu)
2839 int count = dwSize / sizeof(LPIMEMENUITEMINFOW);
2840 dwSize = count * sizeof(IMEMENUITEMINFOA);
2841 lpImeMenuA = HeapAlloc(GetProcessHeap(), 0, dwSize);
2843 else
2844 lpImeMenuA = NULL;
2846 rc = data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2847 (IMEMENUITEMINFOW*)parent,
2848 (IMEMENUITEMINFOW*)lpImeMenuA, dwSize);
2850 if (lpImeParentMenu)
2852 memcpy(lpImeParentMenu,&lpImeParentMenuA,sizeof(IMEMENUITEMINFOA));
2853 lpImeParentMenu->hbmpItem = lpImeParentMenuA.hbmpItem;
2854 MultiByteToWideChar(CP_ACP, 0, lpImeParentMenuA.szString,
2855 -1, lpImeParentMenu->szString, IMEMENUITEM_STRING_SIZE);
2857 if (lpImeMenu && rc)
2859 unsigned int i;
2860 for (i = 0; i < rc; i++)
2862 memcpy(&lpImeMenu[i],&lpImeMenuA[1],sizeof(IMEMENUITEMINFOA));
2863 lpImeMenu[i].hbmpItem = lpImeMenuA[i].hbmpItem;
2864 MultiByteToWideChar(CP_ACP, 0, lpImeMenuA[i].szString,
2865 -1, lpImeMenu[i].szString, IMEMENUITEM_STRING_SIZE);
2868 HeapFree(GetProcessHeap(),0,lpImeMenuA);
2869 return rc;
2872 else
2873 return 0;
2876 /***********************************************************************
2877 * ImmLockIMC(IMM32.@)
2879 LPINPUTCONTEXT WINAPI ImmLockIMC(HIMC hIMC)
2881 InputContextData *data = get_imc_data(hIMC);
2883 if (!data)
2884 return NULL;
2885 data->dwLock++;
2886 return &data->IMC;
2889 /***********************************************************************
2890 * ImmUnlockIMC(IMM32.@)
2892 BOOL WINAPI ImmUnlockIMC(HIMC hIMC)
2894 InputContextData *data = get_imc_data(hIMC);
2896 if (!data)
2897 return FALSE;
2898 if (data->dwLock)
2899 data->dwLock--;
2900 return TRUE;
2903 /***********************************************************************
2904 * ImmGetIMCLockCount(IMM32.@)
2906 DWORD WINAPI ImmGetIMCLockCount(HIMC hIMC)
2908 InputContextData *data = get_imc_data(hIMC);
2909 if (!data)
2910 return 0;
2911 return data->dwLock;
2914 /***********************************************************************
2915 * ImmCreateIMCC(IMM32.@)
2917 HIMCC WINAPI ImmCreateIMCC(DWORD size)
2919 return GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE, size);
2922 /***********************************************************************
2923 * ImmDestroyIMCC(IMM32.@)
2925 HIMCC WINAPI ImmDestroyIMCC(HIMCC block)
2927 return GlobalFree(block);
2930 /***********************************************************************
2931 * ImmLockIMCC(IMM32.@)
2933 LPVOID WINAPI ImmLockIMCC(HIMCC imcc)
2935 return GlobalLock(imcc);
2938 /***********************************************************************
2939 * ImmUnlockIMCC(IMM32.@)
2941 BOOL WINAPI ImmUnlockIMCC(HIMCC imcc)
2943 return GlobalUnlock(imcc);
2946 /***********************************************************************
2947 * ImmGetIMCCLockCount(IMM32.@)
2949 DWORD WINAPI ImmGetIMCCLockCount(HIMCC imcc)
2951 return GlobalFlags(imcc) & GMEM_LOCKCOUNT;
2954 /***********************************************************************
2955 * ImmReSizeIMCC(IMM32.@)
2957 HIMCC WINAPI ImmReSizeIMCC(HIMCC imcc, DWORD size)
2959 return GlobalReAlloc(imcc, size, GMEM_ZEROINIT | GMEM_MOVEABLE);
2962 /***********************************************************************
2963 * ImmGetIMCCSize(IMM32.@)
2965 DWORD WINAPI ImmGetIMCCSize(HIMCC imcc)
2967 return GlobalSize(imcc);
2970 /***********************************************************************
2971 * ImmGenerateMessage(IMM32.@)
2973 BOOL WINAPI ImmGenerateMessage(HIMC hIMC)
2975 InputContextData *data = get_imc_data(hIMC);
2977 if (!data)
2979 SetLastError(ERROR_INVALID_HANDLE);
2980 return FALSE;
2983 TRACE("%i messages queued\n",data->IMC.dwNumMsgBuf);
2984 if (data->IMC.dwNumMsgBuf > 0)
2986 LPTRANSMSG lpTransMsg;
2987 HIMCC hMsgBuf;
2988 DWORD i, dwNumMsgBuf;
2990 /* We are going to detach our hMsgBuff so that if processing messages
2991 generates new messages they go into a new buffer */
2992 hMsgBuf = data->IMC.hMsgBuf;
2993 dwNumMsgBuf = data->IMC.dwNumMsgBuf;
2995 data->IMC.hMsgBuf = ImmCreateIMCC(0);
2996 data->IMC.dwNumMsgBuf = 0;
2998 lpTransMsg = ImmLockIMCC(hMsgBuf);
2999 for (i = 0; i < dwNumMsgBuf; i++)
3000 ImmInternalSendIMEMessage(data, lpTransMsg[i].message, lpTransMsg[i].wParam, lpTransMsg[i].lParam);
3002 ImmUnlockIMCC(hMsgBuf);
3003 ImmDestroyIMCC(hMsgBuf);
3006 return TRUE;
3009 /***********************************************************************
3010 * ImmTranslateMessage(IMM32.@)
3011 * ( Undocumented, call internally and from user32.dll )
3013 BOOL WINAPI ImmTranslateMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lKeyData)
3015 InputContextData *data;
3016 HIMC imc = ImmGetContext(hwnd);
3017 BYTE state[256];
3018 UINT scancode;
3019 LPVOID list = 0;
3020 UINT msg_count;
3021 UINT uVirtKey;
3022 static const DWORD list_count = 10;
3024 TRACE("%p %x %x %x\n",hwnd, msg, (UINT)wParam, (UINT)lKeyData);
3026 if (imc)
3027 data = imc;
3028 else
3029 return FALSE;
3031 if (!data->immKbd->hIME || !data->immKbd->pImeToAsciiEx)
3032 return FALSE;
3034 GetKeyboardState(state);
3035 scancode = lKeyData >> 0x10 & 0xff;
3037 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list_count * sizeof(TRANSMSG) + sizeof(DWORD));
3038 ((DWORD*)list)[0] = list_count;
3040 if (data->immKbd->imeInfo.fdwProperty & IME_PROP_KBD_CHAR_FIRST)
3042 WCHAR chr;
3044 if (!is_himc_ime_unicode(data))
3045 ToAscii(data->lastVK, scancode, state, &chr, 0);
3046 else
3047 ToUnicodeEx(data->lastVK, scancode, state, &chr, 1, 0, GetKeyboardLayout(0));
3048 uVirtKey = MAKELONG(data->lastVK,chr);
3050 else
3051 uVirtKey = data->lastVK;
3053 msg_count = data->immKbd->pImeToAsciiEx(uVirtKey, scancode, state, list, 0, imc);
3054 TRACE("%i messages generated\n",msg_count);
3055 if (msg_count && msg_count <= list_count)
3057 UINT i;
3058 LPTRANSMSG msgs = (LPTRANSMSG)((LPBYTE)list + sizeof(DWORD));
3060 for (i = 0; i < msg_count; i++)
3061 ImmInternalPostIMEMessage(data, msgs[i].message, msgs[i].wParam, msgs[i].lParam);
3063 else if (msg_count > list_count)
3064 ImmGenerateMessage(imc);
3066 HeapFree(GetProcessHeap(),0,list);
3068 data->lastVK = VK_PROCESSKEY;
3070 return (msg_count > 0);
3073 /***********************************************************************
3074 * ImmProcessKey(IMM32.@)
3075 * ( Undocumented, called from user32.dll )
3077 BOOL WINAPI ImmProcessKey(HWND hwnd, HKL hKL, UINT vKey, LPARAM lKeyData, DWORD unknown)
3079 InputContextData *data;
3080 HIMC imc = ImmGetContext(hwnd);
3081 BYTE state[256];
3083 TRACE("%p %p %x %x %x\n",hwnd, hKL, vKey, (UINT)lKeyData, unknown);
3085 if (imc)
3086 data = imc;
3087 else
3088 return FALSE;
3090 /* Make sure we are inputting to the correct keyboard */
3091 if (data->immKbd->hkl != hKL)
3093 ImmHkl *new_hkl = IMM_GetImmHkl(hKL);
3094 if (new_hkl)
3096 data->immKbd->pImeSelect(imc, FALSE);
3097 data->immKbd->uSelected--;
3098 data->immKbd = new_hkl;
3099 data->immKbd->pImeSelect(imc, TRUE);
3100 data->immKbd->uSelected++;
3102 else
3103 return FALSE;
3106 if (!data->immKbd->hIME || !data->immKbd->pImeProcessKey)
3107 return FALSE;
3109 GetKeyboardState(state);
3110 if (data->immKbd->pImeProcessKey(imc, vKey, lKeyData, state))
3112 data->lastVK = vKey;
3113 return TRUE;
3116 data->lastVK = VK_PROCESSKEY;
3117 return FALSE;
3120 /***********************************************************************
3121 * ImmDisableTextFrameService(IMM32.@)
3123 BOOL WINAPI ImmDisableTextFrameService(DWORD idThread)
3125 FIXME("Stub\n");
3126 return FALSE;
3129 /***********************************************************************
3130 * ImmEnumInputContext(IMM32.@)
3133 BOOL WINAPI ImmEnumInputContext(DWORD idThread, IMCENUMPROC lpfn, LPARAM lParam)
3135 FIXME("Stub\n");
3136 return FALSE;
3139 /***********************************************************************
3140 * ImmGetHotKey(IMM32.@)
3143 BOOL WINAPI ImmGetHotKey(DWORD hotkey, UINT *modifiers, UINT *key, HKL hkl)
3145 FIXME("%x, %p, %p, %p: stub\n", hotkey, modifiers, key, hkl);
3146 return FALSE;