ole32: Don't use real synchronous locks for storage synchronization.
[wine.git] / dlls / imm32 / imm.c
blobc4b44e4cddd2a8af34a8797c9eb84922106266fb
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 } IMMThreadData;
100 static struct list ImmHklList = LIST_INIT(ImmHklList);
101 static struct list ImmThreadDataList = LIST_INIT(ImmThreadDataList);
103 static const WCHAR szwWineIMCProperty[] = {'W','i','n','e','I','m','m','H','I','M','C','P','r','o','p','e','r','t','y',0};
105 static const WCHAR szImeFileW[] = {'I','m','e',' ','F','i','l','e',0};
106 static const WCHAR szLayoutTextW[] = {'L','a','y','o','u','t',' ','T','e','x','t',0};
107 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};
109 static const WCHAR szwIME[] = {'I','M','E',0};
111 static CRITICAL_SECTION threaddata_cs;
112 static CRITICAL_SECTION_DEBUG critsect_debug =
114 0, 0, &threaddata_cs,
115 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
116 0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
118 static CRITICAL_SECTION threaddata_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
120 #define is_himc_ime_unicode(p) (p->immKbd->imeInfo.fdwProperty & IME_PROP_UNICODE)
121 #define is_kbd_ime_unicode(p) (p->imeInfo.fdwProperty & IME_PROP_UNICODE)
123 static BOOL IMM_DestroyContext(HIMC hIMC);
124 static InputContextData* get_imc_data(HIMC hIMC);
126 static inline WCHAR *strdupAtoW( const char *str )
128 WCHAR *ret = NULL;
129 if (str)
131 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
132 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
133 MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
135 return ret;
138 static inline CHAR *strdupWtoA( const WCHAR *str )
140 CHAR *ret = NULL;
141 if (str)
143 DWORD len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
144 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
145 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
147 return ret;
150 static DWORD convert_candidatelist_WtoA(
151 LPCANDIDATELIST lpSrc, LPCANDIDATELIST lpDst, DWORD dwBufLen)
153 DWORD ret, i, len;
155 ret = FIELD_OFFSET( CANDIDATELIST, dwOffset[lpSrc->dwCount] );
156 if ( lpDst && dwBufLen > 0 )
158 *lpDst = *lpSrc;
159 lpDst->dwOffset[0] = ret;
162 for ( i = 0; i < lpSrc->dwCount; i++)
164 LPBYTE src = (LPBYTE)lpSrc + lpSrc->dwOffset[i];
166 if ( lpDst && dwBufLen > 0 )
168 LPBYTE dest = (LPBYTE)lpDst + lpDst->dwOffset[i];
170 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1,
171 (LPSTR)dest, dwBufLen, NULL, NULL);
173 if ( i + 1 < lpSrc->dwCount )
174 lpDst->dwOffset[i+1] = lpDst->dwOffset[i] + len * sizeof(char);
175 dwBufLen -= len * sizeof(char);
177 else
178 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, NULL, 0, NULL, NULL);
180 ret += len * sizeof(char);
183 if ( lpDst )
184 lpDst->dwSize = ret;
186 return ret;
189 static DWORD convert_candidatelist_AtoW(
190 LPCANDIDATELIST lpSrc, LPCANDIDATELIST lpDst, DWORD dwBufLen)
192 DWORD ret, i, len;
194 ret = FIELD_OFFSET( CANDIDATELIST, dwOffset[lpSrc->dwCount] );
195 if ( lpDst && dwBufLen > 0 )
197 *lpDst = *lpSrc;
198 lpDst->dwOffset[0] = ret;
201 for ( i = 0; i < lpSrc->dwCount; i++)
203 LPBYTE src = (LPBYTE)lpSrc + lpSrc->dwOffset[i];
205 if ( lpDst && dwBufLen > 0 )
207 LPBYTE dest = (LPBYTE)lpDst + lpDst->dwOffset[i];
209 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1,
210 (LPWSTR)dest, dwBufLen);
212 if ( i + 1 < lpSrc->dwCount )
213 lpDst->dwOffset[i+1] = lpDst->dwOffset[i] + len * sizeof(WCHAR);
214 dwBufLen -= len * sizeof(WCHAR);
216 else
217 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, NULL, 0);
219 ret += len * sizeof(WCHAR);
222 if ( lpDst )
223 lpDst->dwSize = ret;
225 return ret;
228 static IMMThreadData *IMM_GetThreadData(HWND hwnd)
230 IMMThreadData *data;
231 DWORD process, thread;
233 if (hwnd)
235 if (!(thread = GetWindowThreadProcessId(hwnd, &process))) return NULL;
236 if (process != GetCurrentProcessId()) return NULL;
238 else thread = GetCurrentThreadId();
240 EnterCriticalSection(&threaddata_cs);
241 LIST_FOR_EACH_ENTRY(data, &ImmThreadDataList, IMMThreadData, entry)
242 if (data->threadID == thread) return data;
244 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
245 data->threadID = thread;
246 list_add_head(&ImmThreadDataList,&data->entry);
247 TRACE("Thread Data Created (%x)\n",thread);
248 return data;
251 static BOOL IMM_IsDefaultContext(HIMC imc)
253 InputContextData *data = get_imc_data(imc);
255 if (!data)
256 return FALSE;
258 return data->threadDefault;
261 static void IMM_FreeThreadData(void)
263 IMMThreadData *data;
265 EnterCriticalSection(&threaddata_cs);
266 LIST_FOR_EACH_ENTRY(data, &ImmThreadDataList, IMMThreadData, entry)
268 if (data->threadID == GetCurrentThreadId())
270 list_remove(&data->entry);
271 LeaveCriticalSection(&threaddata_cs);
272 IMM_DestroyContext(data->defaultContext);
273 DestroyWindow(data->hwndDefault);
274 HeapFree(GetProcessHeap(),0,data);
275 TRACE("Thread Data Destroyed\n");
276 return;
279 LeaveCriticalSection(&threaddata_cs);
282 static HMODULE load_graphics_driver(void)
284 static const WCHAR display_device_guid_propW[] = {
285 '_','_','w','i','n','e','_','d','i','s','p','l','a','y','_',
286 'd','e','v','i','c','e','_','g','u','i','d',0 };
287 static const WCHAR key_pathW[] = {
288 'S','y','s','t','e','m','\\',
289 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
290 'C','o','n','t','r','o','l','\\',
291 'V','i','d','e','o','\\','{',0};
292 static const WCHAR displayW[] = {'}','\\','0','0','0','0',0};
293 static const WCHAR driverW[] = {'G','r','a','p','h','i','c','s','D','r','i','v','e','r',0};
295 HMODULE ret = 0;
296 HKEY hkey;
297 DWORD size;
298 WCHAR path[MAX_PATH];
299 WCHAR key[(sizeof(key_pathW) + sizeof(displayW)) / sizeof(WCHAR) + 40];
300 UINT guid_atom = HandleToULong( GetPropW( GetDesktopWindow(), display_device_guid_propW ));
302 if (!guid_atom) return 0;
303 memcpy( key, key_pathW, sizeof(key_pathW) );
304 if (!GlobalGetAtomNameW( guid_atom, key + strlenW(key), 40 )) return 0;
305 strcatW( key, displayW );
306 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, key, &hkey )) return 0;
307 size = sizeof(path);
308 if (!RegQueryValueExW( hkey, driverW, NULL, NULL, (BYTE *)path, &size )) ret = LoadLibraryW( path );
309 RegCloseKey( hkey );
310 TRACE( "%s %p\n", debugstr_w(path), ret );
311 return ret;
314 /* ImmHkl loading and freeing */
315 #define LOAD_FUNCPTR(f) if((ptr->p##f = (LPVOID)GetProcAddress(ptr->hIME, #f)) == NULL){WARN("Can't find function %s in ime\n", #f);}
316 static ImmHkl *IMM_GetImmHkl(HKL hkl)
318 ImmHkl *ptr;
319 WCHAR filename[MAX_PATH];
321 TRACE("Seeking ime for keyboard %p\n",hkl);
323 LIST_FOR_EACH_ENTRY(ptr, &ImmHklList, ImmHkl, entry)
325 if (ptr->hkl == hkl)
326 return ptr;
328 /* not found... create it */
330 ptr = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ImmHkl));
332 ptr->hkl = hkl;
333 if (ImmGetIMEFileNameW(hkl, filename, MAX_PATH)) ptr->hIME = LoadLibraryW(filename);
334 if (!ptr->hIME) ptr->hIME = load_graphics_driver();
335 if (ptr->hIME)
337 LOAD_FUNCPTR(ImeInquire);
338 if (!ptr->pImeInquire || !ptr->pImeInquire(&ptr->imeInfo, ptr->imeClassName, NULL))
340 FreeLibrary(ptr->hIME);
341 ptr->hIME = NULL;
343 else
345 LOAD_FUNCPTR(ImeDestroy);
346 LOAD_FUNCPTR(ImeSelect);
347 if (!ptr->pImeSelect || !ptr->pImeDestroy)
349 FreeLibrary(ptr->hIME);
350 ptr->hIME = NULL;
352 else
354 LOAD_FUNCPTR(ImeConfigure);
355 LOAD_FUNCPTR(ImeEscape);
356 LOAD_FUNCPTR(ImeSetActiveContext);
357 LOAD_FUNCPTR(ImeToAsciiEx);
358 LOAD_FUNCPTR(NotifyIME);
359 LOAD_FUNCPTR(ImeRegisterWord);
360 LOAD_FUNCPTR(ImeUnregisterWord);
361 LOAD_FUNCPTR(ImeEnumRegisterWord);
362 LOAD_FUNCPTR(ImeSetCompositionString);
363 LOAD_FUNCPTR(ImeConversionList);
364 LOAD_FUNCPTR(ImeProcessKey);
365 LOAD_FUNCPTR(ImeGetRegisterWordStyle);
366 LOAD_FUNCPTR(ImeGetImeMenuItems);
367 /* make sure our classname is WCHAR */
368 if (!is_kbd_ime_unicode(ptr))
370 WCHAR bufW[17];
371 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ptr->imeClassName,
372 -1, bufW, 17);
373 lstrcpyW(ptr->imeClassName, bufW);
378 list_add_head(&ImmHklList,&ptr->entry);
380 return ptr;
382 #undef LOAD_FUNCPTR
384 HWND WINAPI __wine_get_ui_window(HKL hkl)
386 ImmHkl *immHkl = IMM_GetImmHkl(hkl);
387 return immHkl->UIWnd;
390 static void IMM_FreeAllImmHkl(void)
392 ImmHkl *ptr,*cursor2;
394 LIST_FOR_EACH_ENTRY_SAFE(ptr, cursor2, &ImmHklList, ImmHkl, entry)
396 list_remove(&ptr->entry);
397 if (ptr->hIME)
399 ptr->pImeDestroy(1);
400 FreeLibrary(ptr->hIME);
402 if (ptr->UIWnd)
403 DestroyWindow(ptr->UIWnd);
404 HeapFree(GetProcessHeap(),0,ptr);
408 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved)
410 TRACE("%p, %x, %p\n",hInstDLL,fdwReason,lpReserved);
411 switch (fdwReason)
413 case DLL_PROCESS_ATTACH:
414 if (!User32InitializeImmEntryTable(IMM_INIT_MAGIC))
416 return FALSE;
418 break;
419 case DLL_THREAD_ATTACH:
420 break;
421 case DLL_THREAD_DETACH:
422 IMM_FreeThreadData();
423 break;
424 case DLL_PROCESS_DETACH:
425 if (lpReserved) break;
426 IMM_FreeThreadData();
427 IMM_FreeAllImmHkl();
428 break;
430 return TRUE;
433 /* for posting messages as the IME */
434 static void ImmInternalPostIMEMessage(InputContextData *data, UINT msg, WPARAM wParam, LPARAM lParam)
436 HWND target = GetFocus();
437 if (!target)
438 PostMessageW(data->IMC.hWnd,msg,wParam,lParam);
439 else
440 PostMessageW(target, msg, wParam, lParam);
443 static LRESULT ImmInternalSendIMENotify(InputContextData *data, WPARAM notify, LPARAM lParam)
445 HWND target;
447 target = data->IMC.hWnd;
448 if (!target) target = GetFocus();
450 if (target)
451 return SendMessageW(target, WM_IME_NOTIFY, notify, lParam);
453 return 0;
456 static HIMCC ImmCreateBlankCompStr(void)
458 HIMCC rc;
459 LPCOMPOSITIONSTRING ptr;
460 rc = ImmCreateIMCC(sizeof(COMPOSITIONSTRING));
461 ptr = ImmLockIMCC(rc);
462 memset(ptr,0,sizeof(COMPOSITIONSTRING));
463 ptr->dwSize = sizeof(COMPOSITIONSTRING);
464 ImmUnlockIMCC(rc);
465 return rc;
468 static InputContextData* get_imc_data(HIMC hIMC)
470 InputContextData *data = hIMC;
472 if (hIMC == NULL)
473 return NULL;
475 if(IsBadReadPtr(data, sizeof(InputContextData)) || data->magic != WINE_IMC_VALID_MAGIC)
477 SetLastError(ERROR_INVALID_HANDLE);
478 return NULL;
480 return data;
483 static HIMC get_default_context( HWND hwnd )
485 HIMC ret;
486 IMMThreadData* thread_data = IMM_GetThreadData( hwnd );
488 if (!thread_data) return 0;
490 if (thread_data->defaultContext)
492 ret = thread_data->defaultContext;
493 LeaveCriticalSection(&threaddata_cs);
494 return ret;
497 /* can't create a default context in another thread */
498 if (thread_data->threadID != GetCurrentThreadId())
500 LeaveCriticalSection(&threaddata_cs);
501 return 0;
504 LeaveCriticalSection(&threaddata_cs);
506 ret = ImmCreateContext();
507 if (!ret) return 0;
508 ((InputContextData*)ret)->threadDefault = TRUE;
510 /* thread_data is in the current thread so we can assume it's still valid */
511 EnterCriticalSection(&threaddata_cs);
513 if (thread_data->defaultContext) /* someone beat us */
515 IMM_DestroyContext( ret );
516 ret = thread_data->defaultContext;
518 else thread_data->defaultContext = ret;
520 LeaveCriticalSection(&threaddata_cs);
521 return ret;
524 static BOOL IMM_IsCrossThreadAccess(HWND hWnd, HIMC hIMC)
526 InputContextData *data;
528 if (hWnd)
530 DWORD thread = GetWindowThreadProcessId(hWnd, NULL);
531 if (thread != GetCurrentThreadId()) return TRUE;
533 data = get_imc_data(hIMC);
534 if (data && data->threadID != GetCurrentThreadId())
535 return TRUE;
537 return FALSE;
540 /***********************************************************************
541 * ImmAssociateContext (IMM32.@)
543 HIMC WINAPI ImmAssociateContext(HWND hWnd, HIMC hIMC)
545 HIMC old = NULL;
546 InputContextData *data = get_imc_data(hIMC);
548 TRACE("(%p, %p):\n", hWnd, hIMC);
550 if(hIMC && !data)
551 return NULL;
554 * If already associated just return
556 if (hIMC && data->IMC.hWnd == hWnd)
557 return hIMC;
559 if (hIMC && IMM_IsCrossThreadAccess(hWnd, hIMC))
560 return NULL;
562 if (hWnd)
564 HIMC defaultContext = get_default_context( hWnd );
565 old = RemovePropW(hWnd,szwWineIMCProperty);
567 if (old == NULL)
568 old = defaultContext;
569 else if (old == (HIMC)-1)
570 old = NULL;
572 if (hIMC != defaultContext)
574 if (hIMC == NULL) /* Meaning disable imm for that window*/
575 SetPropW(hWnd,szwWineIMCProperty,(HANDLE)-1);
576 else
577 SetPropW(hWnd,szwWineIMCProperty,hIMC);
580 if (old)
582 InputContextData *old_data = old;
583 if (old_data->IMC.hWnd == hWnd)
584 old_data->IMC.hWnd = NULL;
588 if (!hIMC)
589 return old;
591 SendMessageW(data->IMC.hWnd, WM_IME_SETCONTEXT, FALSE, ISC_SHOWUIALL);
592 data->IMC.hWnd = hWnd;
593 SendMessageW(data->IMC.hWnd, WM_IME_SETCONTEXT, TRUE, ISC_SHOWUIALL);
595 return old;
600 * Helper function for ImmAssociateContextEx
602 static BOOL CALLBACK _ImmAssociateContextExEnumProc(HWND hwnd, LPARAM lParam)
604 HIMC hImc = (HIMC)lParam;
605 ImmAssociateContext(hwnd,hImc);
606 return TRUE;
609 /***********************************************************************
610 * ImmAssociateContextEx (IMM32.@)
612 BOOL WINAPI ImmAssociateContextEx(HWND hWnd, HIMC hIMC, DWORD dwFlags)
614 TRACE("(%p, %p, 0x%x):\n", hWnd, hIMC, dwFlags);
616 if (!hWnd)
617 return FALSE;
619 switch (dwFlags)
621 case 0:
622 ImmAssociateContext(hWnd,hIMC);
623 return TRUE;
624 case IACE_DEFAULT:
626 HIMC defaultContext = get_default_context( hWnd );
627 if (!defaultContext) return FALSE;
628 ImmAssociateContext(hWnd,defaultContext);
629 return TRUE;
631 case IACE_IGNORENOCONTEXT:
632 if (GetPropW(hWnd,szwWineIMCProperty))
633 ImmAssociateContext(hWnd,hIMC);
634 return TRUE;
635 case IACE_CHILDREN:
636 EnumChildWindows(hWnd,_ImmAssociateContextExEnumProc,(LPARAM)hIMC);
637 return TRUE;
638 default:
639 FIXME("Unknown dwFlags 0x%x\n",dwFlags);
640 return FALSE;
644 /***********************************************************************
645 * ImmConfigureIMEA (IMM32.@)
647 BOOL WINAPI ImmConfigureIMEA(
648 HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
650 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
652 TRACE("(%p, %p, %d, %p):\n", hKL, hWnd, dwMode, lpData);
654 if (dwMode == IME_CONFIG_REGISTERWORD && !lpData)
655 return FALSE;
657 if (immHkl->hIME && immHkl->pImeConfigure)
659 if (dwMode != IME_CONFIG_REGISTERWORD || !is_kbd_ime_unicode(immHkl))
660 return immHkl->pImeConfigure(hKL,hWnd,dwMode,lpData);
661 else
663 REGISTERWORDW rww;
664 REGISTERWORDA *rwa = lpData;
665 BOOL rc;
667 rww.lpReading = strdupAtoW(rwa->lpReading);
668 rww.lpWord = strdupAtoW(rwa->lpWord);
669 rc = immHkl->pImeConfigure(hKL,hWnd,dwMode,&rww);
670 HeapFree(GetProcessHeap(),0,rww.lpReading);
671 HeapFree(GetProcessHeap(),0,rww.lpWord);
672 return rc;
675 else
676 return FALSE;
679 /***********************************************************************
680 * ImmConfigureIMEW (IMM32.@)
682 BOOL WINAPI ImmConfigureIMEW(
683 HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
685 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
687 TRACE("(%p, %p, %d, %p):\n", hKL, hWnd, dwMode, lpData);
689 if (dwMode == IME_CONFIG_REGISTERWORD && !lpData)
690 return FALSE;
692 if (immHkl->hIME && immHkl->pImeConfigure)
694 if (dwMode != IME_CONFIG_REGISTERWORD || is_kbd_ime_unicode(immHkl))
695 return immHkl->pImeConfigure(hKL,hWnd,dwMode,lpData);
696 else
698 REGISTERWORDW *rww = lpData;
699 REGISTERWORDA rwa;
700 BOOL rc;
702 rwa.lpReading = strdupWtoA(rww->lpReading);
703 rwa.lpWord = strdupWtoA(rww->lpWord);
704 rc = immHkl->pImeConfigure(hKL,hWnd,dwMode,&rwa);
705 HeapFree(GetProcessHeap(),0,rwa.lpReading);
706 HeapFree(GetProcessHeap(),0,rwa.lpWord);
707 return rc;
710 else
711 return FALSE;
714 /***********************************************************************
715 * ImmCreateContext (IMM32.@)
717 HIMC WINAPI ImmCreateContext(void)
719 InputContextData *new_context;
720 LPGUIDELINE gl;
721 LPCANDIDATEINFO ci;
722 int i;
724 new_context = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(InputContextData));
726 /* Load the IME */
727 new_context->immKbd = IMM_GetImmHkl(GetKeyboardLayout(0));
729 if (!new_context->immKbd->hIME)
731 TRACE("IME dll could not be loaded\n");
732 HeapFree(GetProcessHeap(),0,new_context);
733 return 0;
736 /* the HIMCCs are never NULL */
737 new_context->IMC.hCompStr = ImmCreateBlankCompStr();
738 new_context->IMC.hMsgBuf = ImmCreateIMCC(0);
739 new_context->IMC.hCandInfo = ImmCreateIMCC(sizeof(CANDIDATEINFO));
740 ci = ImmLockIMCC(new_context->IMC.hCandInfo);
741 memset(ci,0,sizeof(CANDIDATEINFO));
742 ci->dwSize = sizeof(CANDIDATEINFO);
743 ImmUnlockIMCC(new_context->IMC.hCandInfo);
744 new_context->IMC.hGuideLine = ImmCreateIMCC(sizeof(GUIDELINE));
745 gl = ImmLockIMCC(new_context->IMC.hGuideLine);
746 memset(gl,0,sizeof(GUIDELINE));
747 gl->dwSize = sizeof(GUIDELINE);
748 ImmUnlockIMCC(new_context->IMC.hGuideLine);
750 for (i = 0; i < sizeof(new_context->IMC.cfCandForm) / sizeof(CANDIDATEFORM); i++)
751 new_context->IMC.cfCandForm[i].dwIndex = ~0u;
753 /* Initialize the IME Private */
754 new_context->IMC.hPrivate = ImmCreateIMCC(new_context->immKbd->imeInfo.dwPrivateDataSize);
756 new_context->IMC.fdwConversion = new_context->immKbd->imeInfo.fdwConversionCaps;
757 new_context->IMC.fdwSentence = new_context->immKbd->imeInfo.fdwSentenceCaps;
759 if (!new_context->immKbd->pImeSelect(new_context, TRUE))
761 TRACE("Selection of IME failed\n");
762 IMM_DestroyContext(new_context);
763 return 0;
765 new_context->threadID = GetCurrentThreadId();
766 SendMessageW(GetFocus(), WM_IME_SELECT, TRUE, (LPARAM)GetKeyboardLayout(0));
768 new_context->immKbd->uSelected++;
769 TRACE("Created context %p\n",new_context);
771 new_context->magic = WINE_IMC_VALID_MAGIC;
772 return new_context;
775 static BOOL IMM_DestroyContext(HIMC hIMC)
777 InputContextData *data = get_imc_data(hIMC);
779 TRACE("Destroying %p\n",hIMC);
781 if (!data)
782 return FALSE;
784 data->immKbd->uSelected --;
785 data->immKbd->pImeSelect(hIMC, FALSE);
786 SendMessageW(data->IMC.hWnd, WM_IME_SELECT, FALSE, (LPARAM)GetKeyboardLayout(0));
788 ImmDestroyIMCC(data->IMC.hCompStr);
789 ImmDestroyIMCC(data->IMC.hCandInfo);
790 ImmDestroyIMCC(data->IMC.hGuideLine);
791 ImmDestroyIMCC(data->IMC.hPrivate);
792 ImmDestroyIMCC(data->IMC.hMsgBuf);
794 data->magic = 0;
795 HeapFree(GetProcessHeap(),0,data);
797 return TRUE;
800 /***********************************************************************
801 * ImmDestroyContext (IMM32.@)
803 BOOL WINAPI ImmDestroyContext(HIMC hIMC)
805 if (!IMM_IsDefaultContext(hIMC) && !IMM_IsCrossThreadAccess(NULL, hIMC))
806 return IMM_DestroyContext(hIMC);
807 else
808 return FALSE;
811 /***********************************************************************
812 * ImmDisableIME (IMM32.@)
814 BOOL WINAPI ImmDisableIME(DWORD idThread)
816 FIXME("(%d): stub\n", idThread);
817 return TRUE;
820 /***********************************************************************
821 * ImmEnumRegisterWordA (IMM32.@)
823 UINT WINAPI ImmEnumRegisterWordA(
824 HKL hKL, REGISTERWORDENUMPROCA lpfnEnumProc,
825 LPCSTR lpszReading, DWORD dwStyle,
826 LPCSTR lpszRegister, LPVOID lpData)
828 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
829 TRACE("(%p, %p, %s, %d, %s, %p):\n", hKL, lpfnEnumProc,
830 debugstr_a(lpszReading), dwStyle, debugstr_a(lpszRegister), lpData);
831 if (immHkl->hIME && immHkl->pImeEnumRegisterWord)
833 if (!is_kbd_ime_unicode(immHkl))
834 return immHkl->pImeEnumRegisterWord((REGISTERWORDENUMPROCW)lpfnEnumProc,
835 (LPCWSTR)lpszReading, dwStyle, (LPCWSTR)lpszRegister, lpData);
836 else
838 LPWSTR lpszwReading = strdupAtoW(lpszReading);
839 LPWSTR lpszwRegister = strdupAtoW(lpszRegister);
840 BOOL rc;
842 rc = immHkl->pImeEnumRegisterWord((REGISTERWORDENUMPROCW)lpfnEnumProc,
843 lpszwReading, dwStyle, lpszwRegister,
844 lpData);
846 HeapFree(GetProcessHeap(),0,lpszwReading);
847 HeapFree(GetProcessHeap(),0,lpszwRegister);
848 return rc;
851 else
852 return 0;
855 /***********************************************************************
856 * ImmEnumRegisterWordW (IMM32.@)
858 UINT WINAPI ImmEnumRegisterWordW(
859 HKL hKL, REGISTERWORDENUMPROCW lpfnEnumProc,
860 LPCWSTR lpszReading, DWORD dwStyle,
861 LPCWSTR lpszRegister, LPVOID lpData)
863 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
864 TRACE("(%p, %p, %s, %d, %s, %p):\n", hKL, lpfnEnumProc,
865 debugstr_w(lpszReading), dwStyle, debugstr_w(lpszRegister), lpData);
866 if (immHkl->hIME && immHkl->pImeEnumRegisterWord)
868 if (is_kbd_ime_unicode(immHkl))
869 return immHkl->pImeEnumRegisterWord(lpfnEnumProc, lpszReading, dwStyle,
870 lpszRegister, lpData);
871 else
873 LPSTR lpszaReading = strdupWtoA(lpszReading);
874 LPSTR lpszaRegister = strdupWtoA(lpszRegister);
875 BOOL rc;
877 rc = immHkl->pImeEnumRegisterWord(lpfnEnumProc, (LPCWSTR)lpszaReading,
878 dwStyle, (LPCWSTR)lpszaRegister, lpData);
880 HeapFree(GetProcessHeap(),0,lpszaReading);
881 HeapFree(GetProcessHeap(),0,lpszaRegister);
882 return rc;
885 else
886 return 0;
889 static inline BOOL EscapeRequiresWA(UINT uEscape)
891 if (uEscape == IME_ESC_GET_EUDC_DICTIONARY ||
892 uEscape == IME_ESC_SET_EUDC_DICTIONARY ||
893 uEscape == IME_ESC_IME_NAME ||
894 uEscape == IME_ESC_GETHELPFILENAME)
895 return TRUE;
896 return FALSE;
899 /***********************************************************************
900 * ImmEscapeA (IMM32.@)
902 LRESULT WINAPI ImmEscapeA(
903 HKL hKL, HIMC hIMC,
904 UINT uEscape, LPVOID lpData)
906 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
907 TRACE("(%p, %p, %d, %p):\n", hKL, hIMC, uEscape, lpData);
909 if (immHkl->hIME && immHkl->pImeEscape)
911 if (!EscapeRequiresWA(uEscape) || !is_kbd_ime_unicode(immHkl))
912 return immHkl->pImeEscape(hIMC,uEscape,lpData);
913 else
915 WCHAR buffer[81]; /* largest required buffer should be 80 */
916 LRESULT rc;
917 if (uEscape == IME_ESC_SET_EUDC_DICTIONARY)
919 MultiByteToWideChar(CP_ACP,0,lpData,-1,buffer,81);
920 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
922 else
924 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
925 WideCharToMultiByte(CP_ACP,0,buffer,-1,lpData,80, NULL, NULL);
927 return rc;
930 else
931 return 0;
934 /***********************************************************************
935 * ImmEscapeW (IMM32.@)
937 LRESULT WINAPI ImmEscapeW(
938 HKL hKL, HIMC hIMC,
939 UINT uEscape, LPVOID lpData)
941 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
942 TRACE("(%p, %p, %d, %p):\n", hKL, hIMC, uEscape, lpData);
944 if (immHkl->hIME && immHkl->pImeEscape)
946 if (!EscapeRequiresWA(uEscape) || is_kbd_ime_unicode(immHkl))
947 return immHkl->pImeEscape(hIMC,uEscape,lpData);
948 else
950 CHAR buffer[81]; /* largest required buffer should be 80 */
951 LRESULT rc;
952 if (uEscape == IME_ESC_SET_EUDC_DICTIONARY)
954 WideCharToMultiByte(CP_ACP,0,lpData,-1,buffer,81, NULL, NULL);
955 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
957 else
959 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
960 MultiByteToWideChar(CP_ACP,0,buffer,-1,lpData,80);
962 return rc;
965 else
966 return 0;
969 /***********************************************************************
970 * ImmGetCandidateListA (IMM32.@)
972 DWORD WINAPI ImmGetCandidateListA(
973 HIMC hIMC, DWORD dwIndex,
974 LPCANDIDATELIST lpCandList, DWORD dwBufLen)
976 InputContextData *data = get_imc_data(hIMC);
977 LPCANDIDATEINFO candinfo;
978 LPCANDIDATELIST candlist;
979 DWORD ret = 0;
981 TRACE("%p, %d, %p, %d\n", hIMC, dwIndex, lpCandList, dwBufLen);
983 if (!data || !data->IMC.hCandInfo)
984 return 0;
986 candinfo = ImmLockIMCC(data->IMC.hCandInfo);
987 if ( dwIndex >= candinfo->dwCount ||
988 dwIndex >= (sizeof(candinfo->dwOffset) / sizeof(DWORD)) )
989 goto done;
991 candlist = (LPCANDIDATELIST)((LPBYTE)candinfo + candinfo->dwOffset[dwIndex]);
992 if ( !candlist->dwSize || !candlist->dwCount )
993 goto done;
995 if ( !is_himc_ime_unicode(data) )
997 ret = candlist->dwSize;
998 if ( lpCandList && dwBufLen >= ret )
999 memcpy(lpCandList, candlist, ret);
1001 else
1002 ret = convert_candidatelist_WtoA( candlist, lpCandList, dwBufLen);
1004 done:
1005 ImmUnlockIMCC(data->IMC.hCandInfo);
1006 return ret;
1009 /***********************************************************************
1010 * ImmGetCandidateListCountA (IMM32.@)
1012 DWORD WINAPI ImmGetCandidateListCountA(
1013 HIMC hIMC, LPDWORD lpdwListCount)
1015 InputContextData *data = get_imc_data(hIMC);
1016 LPCANDIDATEINFO candinfo;
1017 DWORD ret, count;
1019 TRACE("%p, %p\n", hIMC, lpdwListCount);
1021 if (!data || !lpdwListCount || !data->IMC.hCandInfo)
1022 return 0;
1024 candinfo = ImmLockIMCC(data->IMC.hCandInfo);
1026 *lpdwListCount = count = candinfo->dwCount;
1028 if ( !is_himc_ime_unicode(data) )
1029 ret = candinfo->dwSize;
1030 else
1032 ret = sizeof(CANDIDATEINFO);
1033 while ( count-- )
1034 ret += ImmGetCandidateListA(hIMC, count, NULL, 0);
1037 ImmUnlockIMCC(data->IMC.hCandInfo);
1038 return ret;
1041 /***********************************************************************
1042 * ImmGetCandidateListCountW (IMM32.@)
1044 DWORD WINAPI ImmGetCandidateListCountW(
1045 HIMC hIMC, LPDWORD lpdwListCount)
1047 InputContextData *data = get_imc_data(hIMC);
1048 LPCANDIDATEINFO candinfo;
1049 DWORD ret, count;
1051 TRACE("%p, %p\n", hIMC, lpdwListCount);
1053 if (!data || !lpdwListCount || !data->IMC.hCandInfo)
1054 return 0;
1056 candinfo = ImmLockIMCC(data->IMC.hCandInfo);
1058 *lpdwListCount = count = candinfo->dwCount;
1060 if ( is_himc_ime_unicode(data) )
1061 ret = candinfo->dwSize;
1062 else
1064 ret = sizeof(CANDIDATEINFO);
1065 while ( count-- )
1066 ret += ImmGetCandidateListW(hIMC, count, NULL, 0);
1069 ImmUnlockIMCC(data->IMC.hCandInfo);
1070 return ret;
1073 /***********************************************************************
1074 * ImmGetCandidateListW (IMM32.@)
1076 DWORD WINAPI ImmGetCandidateListW(
1077 HIMC hIMC, DWORD dwIndex,
1078 LPCANDIDATELIST lpCandList, DWORD dwBufLen)
1080 InputContextData *data = get_imc_data(hIMC);
1081 LPCANDIDATEINFO candinfo;
1082 LPCANDIDATELIST candlist;
1083 DWORD ret = 0;
1085 TRACE("%p, %d, %p, %d\n", hIMC, dwIndex, lpCandList, dwBufLen);
1087 if (!data || !data->IMC.hCandInfo)
1088 return 0;
1090 candinfo = ImmLockIMCC(data->IMC.hCandInfo);
1091 if ( dwIndex >= candinfo->dwCount ||
1092 dwIndex >= (sizeof(candinfo->dwOffset) / sizeof(DWORD)) )
1093 goto done;
1095 candlist = (LPCANDIDATELIST)((LPBYTE)candinfo + candinfo->dwOffset[dwIndex]);
1096 if ( !candlist->dwSize || !candlist->dwCount )
1097 goto done;
1099 if ( is_himc_ime_unicode(data) )
1101 ret = candlist->dwSize;
1102 if ( lpCandList && dwBufLen >= ret )
1103 memcpy(lpCandList, candlist, ret);
1105 else
1106 ret = convert_candidatelist_AtoW( candlist, lpCandList, dwBufLen);
1108 done:
1109 ImmUnlockIMCC(data->IMC.hCandInfo);
1110 return ret;
1113 /***********************************************************************
1114 * ImmGetCandidateWindow (IMM32.@)
1116 BOOL WINAPI ImmGetCandidateWindow(
1117 HIMC hIMC, DWORD dwIndex, LPCANDIDATEFORM lpCandidate)
1119 InputContextData *data = get_imc_data(hIMC);
1121 TRACE("%p, %d, %p\n", hIMC, dwIndex, lpCandidate);
1123 if (!data || !lpCandidate)
1124 return FALSE;
1126 if ( dwIndex >= (sizeof(data->IMC.cfCandForm) / sizeof(CANDIDATEFORM)) )
1127 return FALSE;
1129 if (data->IMC.cfCandForm[dwIndex].dwIndex != dwIndex)
1130 return FALSE;
1132 *lpCandidate = data->IMC.cfCandForm[dwIndex];
1134 return TRUE;
1137 /***********************************************************************
1138 * ImmGetCompositionFontA (IMM32.@)
1140 BOOL WINAPI ImmGetCompositionFontA(HIMC hIMC, LPLOGFONTA lplf)
1142 LOGFONTW lfW;
1143 BOOL rc;
1145 TRACE("(%p, %p):\n", hIMC, lplf);
1147 rc = ImmGetCompositionFontW(hIMC,&lfW);
1148 if (!rc || !lplf)
1149 return FALSE;
1151 memcpy(lplf,&lfW,sizeof(LOGFONTA));
1152 WideCharToMultiByte(CP_ACP, 0, lfW.lfFaceName, -1, lplf->lfFaceName,
1153 LF_FACESIZE, NULL, NULL);
1154 return TRUE;
1157 /***********************************************************************
1158 * ImmGetCompositionFontW (IMM32.@)
1160 BOOL WINAPI ImmGetCompositionFontW(HIMC hIMC, LPLOGFONTW lplf)
1162 InputContextData *data = get_imc_data(hIMC);
1164 TRACE("(%p, %p):\n", hIMC, lplf);
1166 if (!data || !lplf)
1167 return FALSE;
1169 *lplf = data->IMC.lfFont.W;
1171 return TRUE;
1175 /* Helpers for the GetCompositionString functions */
1177 static INT CopyCompStringIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE target, INT tlen,
1178 BOOL unicode )
1180 INT rc;
1182 if (is_himc_ime_unicode(data) && !unicode)
1183 rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)source, slen, (LPSTR)target, tlen, NULL, NULL);
1184 else if (!is_himc_ime_unicode(data) && unicode)
1185 rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)source, slen, (LPWSTR)target, tlen) * sizeof(WCHAR);
1186 else
1188 int dlen = (unicode)?sizeof(WCHAR):sizeof(CHAR);
1189 memcpy( target, source, min(slen,tlen)*dlen);
1190 rc = slen*dlen;
1193 return rc;
1196 static INT CopyCompAttrIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE ssource, INT sslen,
1197 LPBYTE target, INT tlen, BOOL unicode )
1199 INT rc;
1201 if (is_himc_ime_unicode(data) && !unicode)
1203 rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource, sslen, NULL, 0, NULL, NULL);
1204 if (tlen)
1206 const BYTE *src = source;
1207 LPBYTE dst = target;
1208 int i, j = 0, k = 0;
1210 if (rc < tlen)
1211 tlen = rc;
1212 for (i = 0; i < sslen; ++i)
1214 int len;
1216 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)ssource + i, 1,
1217 NULL, 0, NULL, NULL);
1218 for (; len > 0; --len)
1220 dst[j++] = src[k];
1222 if (j >= tlen)
1223 goto end;
1225 ++k;
1227 end:
1228 rc = j;
1231 else if (!is_himc_ime_unicode(data) && unicode)
1233 rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource, sslen, NULL, 0);
1234 if (tlen)
1236 const BYTE *src = source;
1237 LPBYTE dst = target;
1238 int i, j = 0;
1240 if (rc < tlen)
1241 tlen = rc;
1242 for (i = 0; i < sslen; ++i)
1244 if (IsDBCSLeadByte(((LPSTR)ssource)[i]))
1245 continue;
1247 dst[j++] = src[i];
1249 if (j >= tlen)
1250 break;
1252 rc = j;
1255 else
1257 memcpy( target, source, min(slen,tlen));
1258 rc = slen;
1261 return rc;
1264 static INT CopyCompClauseIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE ssource,
1265 LPBYTE target, INT tlen, BOOL unicode )
1267 INT rc;
1269 if (is_himc_ime_unicode(data) && !unicode)
1271 if (tlen)
1273 int i;
1275 if (slen < tlen)
1276 tlen = slen;
1277 tlen /= sizeof (DWORD);
1278 for (i = 0; i < tlen; ++i)
1280 ((DWORD *)target)[i] = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource,
1281 ((DWORD *)source)[i],
1282 NULL, 0,
1283 NULL, NULL);
1285 rc = sizeof (DWORD) * i;
1287 else
1288 rc = slen;
1290 else if (!is_himc_ime_unicode(data) && unicode)
1292 if (tlen)
1294 int i;
1296 if (slen < tlen)
1297 tlen = slen;
1298 tlen /= sizeof (DWORD);
1299 for (i = 0; i < tlen; ++i)
1301 ((DWORD *)target)[i] = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource,
1302 ((DWORD *)source)[i],
1303 NULL, 0);
1305 rc = sizeof (DWORD) * i;
1307 else
1308 rc = slen;
1310 else
1312 memcpy( target, source, min(slen,tlen));
1313 rc = slen;
1316 return rc;
1319 static INT CopyCompOffsetIMEtoClient(InputContextData *data, DWORD offset, LPBYTE ssource, BOOL unicode)
1321 int rc;
1323 if (is_himc_ime_unicode(data) && !unicode)
1325 rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource, offset, NULL, 0, NULL, NULL);
1327 else if (!is_himc_ime_unicode(data) && unicode)
1329 rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource, offset, NULL, 0);
1331 else
1332 rc = offset;
1334 return rc;
1337 static LONG ImmGetCompositionStringT( HIMC hIMC, DWORD dwIndex, LPVOID lpBuf,
1338 DWORD dwBufLen, BOOL unicode)
1340 LONG rc = 0;
1341 InputContextData *data = get_imc_data(hIMC);
1342 LPCOMPOSITIONSTRING compstr;
1343 LPBYTE compdata;
1345 TRACE("(%p, 0x%x, %p, %d)\n", hIMC, dwIndex, lpBuf, dwBufLen);
1347 if (!data)
1348 return FALSE;
1350 if (!data->IMC.hCompStr)
1351 return FALSE;
1353 compdata = ImmLockIMCC(data->IMC.hCompStr);
1354 compstr = (LPCOMPOSITIONSTRING)compdata;
1356 switch (dwIndex)
1358 case GCS_RESULTSTR:
1359 TRACE("GCS_RESULTSTR\n");
1360 rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwResultStrOffset, compstr->dwResultStrLen, lpBuf, dwBufLen, unicode);
1361 break;
1362 case GCS_COMPSTR:
1363 TRACE("GCS_COMPSTR\n");
1364 rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen, lpBuf, dwBufLen, unicode);
1365 break;
1366 case GCS_COMPATTR:
1367 TRACE("GCS_COMPATTR\n");
1368 rc = CopyCompAttrIMEtoClient(data, compdata + compstr->dwCompAttrOffset, compstr->dwCompAttrLen,
1369 compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen,
1370 lpBuf, dwBufLen, unicode);
1371 break;
1372 case GCS_COMPCLAUSE:
1373 TRACE("GCS_COMPCLAUSE\n");
1374 rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwCompClauseOffset,compstr->dwCompClauseLen,
1375 compdata + compstr->dwCompStrOffset,
1376 lpBuf, dwBufLen, unicode);
1377 break;
1378 case GCS_RESULTCLAUSE:
1379 TRACE("GCS_RESULTCLAUSE\n");
1380 rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwResultClauseOffset,compstr->dwResultClauseLen,
1381 compdata + compstr->dwResultStrOffset,
1382 lpBuf, dwBufLen, unicode);
1383 break;
1384 case GCS_RESULTREADSTR:
1385 TRACE("GCS_RESULTREADSTR\n");
1386 rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwResultReadStrOffset, compstr->dwResultReadStrLen, lpBuf, dwBufLen, unicode);
1387 break;
1388 case GCS_RESULTREADCLAUSE:
1389 TRACE("GCS_RESULTREADCLAUSE\n");
1390 rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwResultReadClauseOffset,compstr->dwResultReadClauseLen,
1391 compdata + compstr->dwResultStrOffset,
1392 lpBuf, dwBufLen, unicode);
1393 break;
1394 case GCS_COMPREADSTR:
1395 TRACE("GCS_COMPREADSTR\n");
1396 rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwCompReadStrOffset, compstr->dwCompReadStrLen, lpBuf, dwBufLen, unicode);
1397 break;
1398 case GCS_COMPREADATTR:
1399 TRACE("GCS_COMPREADATTR\n");
1400 rc = CopyCompAttrIMEtoClient(data, compdata + compstr->dwCompReadAttrOffset, compstr->dwCompReadAttrLen,
1401 compdata + compstr->dwCompReadStrOffset, compstr->dwCompReadStrLen,
1402 lpBuf, dwBufLen, unicode);
1403 break;
1404 case GCS_COMPREADCLAUSE:
1405 TRACE("GCS_COMPREADCLAUSE\n");
1406 rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwCompReadClauseOffset,compstr->dwCompReadClauseLen,
1407 compdata + compstr->dwCompStrOffset,
1408 lpBuf, dwBufLen, unicode);
1409 break;
1410 case GCS_CURSORPOS:
1411 TRACE("GCS_CURSORPOS\n");
1412 rc = CopyCompOffsetIMEtoClient(data, compstr->dwCursorPos, compdata + compstr->dwCompStrOffset, unicode);
1413 break;
1414 case GCS_DELTASTART:
1415 TRACE("GCS_DELTASTART\n");
1416 rc = CopyCompOffsetIMEtoClient(data, compstr->dwDeltaStart, compdata + compstr->dwCompStrOffset, unicode);
1417 break;
1418 default:
1419 FIXME("Unhandled index 0x%x\n",dwIndex);
1420 break;
1423 ImmUnlockIMCC(data->IMC.hCompStr);
1425 return rc;
1428 /***********************************************************************
1429 * ImmGetCompositionStringA (IMM32.@)
1431 LONG WINAPI ImmGetCompositionStringA(
1432 HIMC hIMC, DWORD dwIndex, LPVOID lpBuf, DWORD dwBufLen)
1434 return ImmGetCompositionStringT(hIMC, dwIndex, lpBuf, dwBufLen, FALSE);
1438 /***********************************************************************
1439 * ImmGetCompositionStringW (IMM32.@)
1441 LONG WINAPI ImmGetCompositionStringW(
1442 HIMC hIMC, DWORD dwIndex,
1443 LPVOID lpBuf, DWORD dwBufLen)
1445 return ImmGetCompositionStringT(hIMC, dwIndex, lpBuf, dwBufLen, TRUE);
1448 /***********************************************************************
1449 * ImmGetCompositionWindow (IMM32.@)
1451 BOOL WINAPI ImmGetCompositionWindow(HIMC hIMC, LPCOMPOSITIONFORM lpCompForm)
1453 InputContextData *data = get_imc_data(hIMC);
1455 TRACE("(%p, %p)\n", hIMC, lpCompForm);
1457 if (!data)
1458 return FALSE;
1460 *lpCompForm = data->IMC.cfCompForm;
1461 return TRUE;
1464 /***********************************************************************
1465 * ImmGetContext (IMM32.@)
1468 HIMC WINAPI ImmGetContext(HWND hWnd)
1470 HIMC rc;
1472 TRACE("%p\n", hWnd);
1474 if (!IsWindow(hWnd))
1476 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
1477 return NULL;
1480 rc = GetPropW(hWnd,szwWineIMCProperty);
1481 if (rc == (HIMC)-1)
1482 rc = NULL;
1483 else if (rc == NULL)
1484 rc = get_default_context( hWnd );
1486 if (rc)
1488 InputContextData *data = rc;
1489 data->IMC.hWnd = hWnd;
1492 TRACE("returning %p\n", rc);
1494 return rc;
1497 /***********************************************************************
1498 * ImmGetConversionListA (IMM32.@)
1500 DWORD WINAPI ImmGetConversionListA(
1501 HKL hKL, HIMC hIMC,
1502 LPCSTR pSrc, LPCANDIDATELIST lpDst,
1503 DWORD dwBufLen, UINT uFlag)
1505 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1506 TRACE("(%p, %p, %s, %p, %d, %d):\n", hKL, hIMC, debugstr_a(pSrc), lpDst,
1507 dwBufLen, uFlag);
1508 if (immHkl->hIME && immHkl->pImeConversionList)
1510 if (!is_kbd_ime_unicode(immHkl))
1511 return immHkl->pImeConversionList(hIMC,(LPCWSTR)pSrc,lpDst,dwBufLen,uFlag);
1512 else
1514 LPCANDIDATELIST lpwDst;
1515 DWORD ret = 0, len;
1516 LPWSTR pwSrc = strdupAtoW(pSrc);
1518 len = immHkl->pImeConversionList(hIMC, pwSrc, NULL, 0, uFlag);
1519 lpwDst = HeapAlloc(GetProcessHeap(), 0, len);
1520 if ( lpwDst )
1522 immHkl->pImeConversionList(hIMC, pwSrc, lpwDst, len, uFlag);
1523 ret = convert_candidatelist_WtoA( lpwDst, lpDst, dwBufLen);
1524 HeapFree(GetProcessHeap(), 0, lpwDst);
1526 HeapFree(GetProcessHeap(), 0, pwSrc);
1528 return ret;
1531 else
1532 return 0;
1535 /***********************************************************************
1536 * ImmGetConversionListW (IMM32.@)
1538 DWORD WINAPI ImmGetConversionListW(
1539 HKL hKL, HIMC hIMC,
1540 LPCWSTR pSrc, LPCANDIDATELIST lpDst,
1541 DWORD dwBufLen, UINT uFlag)
1543 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1544 TRACE("(%p, %p, %s, %p, %d, %d):\n", hKL, hIMC, debugstr_w(pSrc), lpDst,
1545 dwBufLen, uFlag);
1546 if (immHkl->hIME && immHkl->pImeConversionList)
1548 if (is_kbd_ime_unicode(immHkl))
1549 return immHkl->pImeConversionList(hIMC,pSrc,lpDst,dwBufLen,uFlag);
1550 else
1552 LPCANDIDATELIST lpaDst;
1553 DWORD ret = 0, len;
1554 LPSTR paSrc = strdupWtoA(pSrc);
1556 len = immHkl->pImeConversionList(hIMC, (LPCWSTR)paSrc, NULL, 0, uFlag);
1557 lpaDst = HeapAlloc(GetProcessHeap(), 0, len);
1558 if ( lpaDst )
1560 immHkl->pImeConversionList(hIMC, (LPCWSTR)paSrc, lpaDst, len, uFlag);
1561 ret = convert_candidatelist_AtoW( lpaDst, lpDst, dwBufLen);
1562 HeapFree(GetProcessHeap(), 0, lpaDst);
1564 HeapFree(GetProcessHeap(), 0, paSrc);
1566 return ret;
1569 else
1570 return 0;
1573 /***********************************************************************
1574 * ImmGetConversionStatus (IMM32.@)
1576 BOOL WINAPI ImmGetConversionStatus(
1577 HIMC hIMC, LPDWORD lpfdwConversion, LPDWORD lpfdwSentence)
1579 InputContextData *data = get_imc_data(hIMC);
1581 TRACE("%p %p %p\n", hIMC, lpfdwConversion, lpfdwSentence);
1583 if (!data)
1584 return FALSE;
1586 if (lpfdwConversion)
1587 *lpfdwConversion = data->IMC.fdwConversion;
1588 if (lpfdwSentence)
1589 *lpfdwSentence = data->IMC.fdwSentence;
1591 return TRUE;
1594 /***********************************************************************
1595 * ImmGetDefaultIMEWnd (IMM32.@)
1597 HWND WINAPI ImmGetDefaultIMEWnd(HWND hWnd)
1599 HWND ret, new = NULL;
1600 IMMThreadData* thread_data = IMM_GetThreadData(hWnd);
1601 if (!thread_data)
1602 return NULL;
1603 if (thread_data->hwndDefault == NULL && thread_data->threadID == GetCurrentThreadId())
1605 /* Do not create the window inside of a critical section */
1606 LeaveCriticalSection(&threaddata_cs);
1607 new = CreateWindowExW( WS_EX_TOOLWINDOW,
1608 szwIME, NULL, WS_POPUP, 0, 0, 1, 1, 0, 0, 0, 0);
1609 /* thread_data is in the current thread so we can assume it's still valid */
1610 EnterCriticalSection(&threaddata_cs);
1611 /* See if anyone beat us */
1612 if (thread_data->hwndDefault == NULL)
1614 thread_data->hwndDefault = new;
1615 new = NULL;
1618 ret = thread_data->hwndDefault;
1619 LeaveCriticalSection(&threaddata_cs);
1620 TRACE("Default is %p\n",ret);
1621 /* Clean up an unused new window outside of the critical section */
1622 if (new != NULL)
1624 DestroyWindow(new);
1626 TRACE("Default is %p\n",ret);
1627 return ret;
1630 /***********************************************************************
1631 * ImmGetDescriptionA (IMM32.@)
1633 UINT WINAPI ImmGetDescriptionA(
1634 HKL hKL, LPSTR lpszDescription, UINT uBufLen)
1636 WCHAR *buf;
1637 DWORD len;
1639 TRACE("%p %p %d\n", hKL, lpszDescription, uBufLen);
1641 /* find out how many characters in the unicode buffer */
1642 len = ImmGetDescriptionW( hKL, NULL, 0 );
1643 if (!len)
1644 return 0;
1646 /* allocate a buffer of that size */
1647 buf = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof (WCHAR) );
1648 if( !buf )
1649 return 0;
1651 /* fetch the unicode buffer */
1652 len = ImmGetDescriptionW( hKL, buf, len + 1 );
1654 /* convert it back to ASCII */
1655 len = WideCharToMultiByte( CP_ACP, 0, buf, len + 1,
1656 lpszDescription, uBufLen, NULL, NULL );
1658 HeapFree( GetProcessHeap(), 0, buf );
1660 if (len == 0)
1661 return 0;
1663 return len - 1;
1666 /***********************************************************************
1667 * ImmGetDescriptionW (IMM32.@)
1669 UINT WINAPI ImmGetDescriptionW(HKL hKL, LPWSTR lpszDescription, UINT uBufLen)
1671 static const WCHAR name[] = { 'W','i','n','e',' ','X','I','M',0 };
1673 FIXME("(%p, %p, %d): semi stub\n", hKL, lpszDescription, uBufLen);
1675 if (!hKL) return 0;
1676 if (!uBufLen) return lstrlenW( name );
1677 lstrcpynW( lpszDescription, name, uBufLen );
1678 return lstrlenW( lpszDescription );
1681 /***********************************************************************
1682 * ImmGetGuideLineA (IMM32.@)
1684 DWORD WINAPI ImmGetGuideLineA(
1685 HIMC hIMC, DWORD dwIndex, LPSTR lpBuf, DWORD dwBufLen)
1687 FIXME("(%p, %d, %s, %d): stub\n",
1688 hIMC, dwIndex, debugstr_a(lpBuf), dwBufLen
1690 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1691 return 0;
1694 /***********************************************************************
1695 * ImmGetGuideLineW (IMM32.@)
1697 DWORD WINAPI ImmGetGuideLineW(HIMC hIMC, DWORD dwIndex, LPWSTR lpBuf, DWORD dwBufLen)
1699 FIXME("(%p, %d, %s, %d): stub\n",
1700 hIMC, dwIndex, debugstr_w(lpBuf), dwBufLen
1702 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1703 return 0;
1706 /***********************************************************************
1707 * ImmGetIMEFileNameA (IMM32.@)
1709 UINT WINAPI ImmGetIMEFileNameA( HKL hKL, LPSTR lpszFileName, UINT uBufLen)
1711 LPWSTR bufW = NULL;
1712 UINT wBufLen = uBufLen;
1713 UINT rc;
1715 if (uBufLen && lpszFileName)
1716 bufW = HeapAlloc(GetProcessHeap(),0,uBufLen * sizeof(WCHAR));
1717 else /* We need this to get the number of byte required */
1719 bufW = HeapAlloc(GetProcessHeap(),0,MAX_PATH * sizeof(WCHAR));
1720 wBufLen = MAX_PATH;
1723 rc = ImmGetIMEFileNameW(hKL,bufW,wBufLen);
1725 if (rc > 0)
1727 if (uBufLen && lpszFileName)
1728 rc = WideCharToMultiByte(CP_ACP, 0, bufW, -1, lpszFileName,
1729 uBufLen, NULL, NULL);
1730 else /* get the length */
1731 rc = WideCharToMultiByte(CP_ACP, 0, bufW, -1, NULL, 0, NULL,
1732 NULL);
1735 HeapFree(GetProcessHeap(),0,bufW);
1736 return rc;
1739 /***********************************************************************
1740 * ImmGetIMEFileNameW (IMM32.@)
1742 UINT WINAPI ImmGetIMEFileNameW(HKL hKL, LPWSTR lpszFileName, UINT uBufLen)
1744 HKEY hkey;
1745 DWORD length;
1746 DWORD rc;
1747 WCHAR regKey[sizeof(szImeRegFmt)/sizeof(WCHAR)+8];
1749 wsprintfW( regKey, szImeRegFmt, (ULONG_PTR)hKL );
1750 rc = RegOpenKeyW( HKEY_LOCAL_MACHINE, regKey, &hkey);
1751 if (rc != ERROR_SUCCESS)
1753 SetLastError(rc);
1754 return 0;
1757 length = 0;
1758 rc = RegGetValueW(hkey, NULL, szImeFileW, RRF_RT_REG_SZ, NULL, NULL, &length);
1760 if (rc != ERROR_SUCCESS)
1762 RegCloseKey(hkey);
1763 SetLastError(rc);
1764 return 0;
1766 if (length > uBufLen * sizeof(WCHAR) || !lpszFileName)
1768 RegCloseKey(hkey);
1769 if (lpszFileName)
1771 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1772 return 0;
1774 else
1775 return length / sizeof(WCHAR);
1778 RegGetValueW(hkey, NULL, szImeFileW, RRF_RT_REG_SZ, NULL, lpszFileName, &length);
1780 RegCloseKey(hkey);
1782 return length / sizeof(WCHAR);
1785 /***********************************************************************
1786 * ImmGetOpenStatus (IMM32.@)
1788 BOOL WINAPI ImmGetOpenStatus(HIMC hIMC)
1790 InputContextData *data = get_imc_data(hIMC);
1791 static int i;
1793 if (!data)
1794 return FALSE;
1796 TRACE("(%p): semi-stub\n", hIMC);
1798 if (!i++)
1799 FIXME("(%p): semi-stub\n", hIMC);
1801 return data->IMC.fOpen;
1804 /***********************************************************************
1805 * ImmGetProperty (IMM32.@)
1807 DWORD WINAPI ImmGetProperty(HKL hKL, DWORD fdwIndex)
1809 DWORD rc = 0;
1810 ImmHkl *kbd;
1812 TRACE("(%p, %d)\n", hKL, fdwIndex);
1813 kbd = IMM_GetImmHkl(hKL);
1815 if (kbd && kbd->hIME)
1817 switch (fdwIndex)
1819 case IGP_PROPERTY: rc = kbd->imeInfo.fdwProperty; break;
1820 case IGP_CONVERSION: rc = kbd->imeInfo.fdwConversionCaps; break;
1821 case IGP_SENTENCE: rc = kbd->imeInfo.fdwSentenceCaps; break;
1822 case IGP_SETCOMPSTR: rc = kbd->imeInfo.fdwSCSCaps; break;
1823 case IGP_SELECT: rc = kbd->imeInfo.fdwSelectCaps; break;
1824 case IGP_GETIMEVERSION: rc = IMEVER_0400; break;
1825 case IGP_UI: rc = 0; break;
1826 default: rc = 0;
1829 return rc;
1832 /***********************************************************************
1833 * ImmGetRegisterWordStyleA (IMM32.@)
1835 UINT WINAPI ImmGetRegisterWordStyleA(
1836 HKL hKL, UINT nItem, LPSTYLEBUFA lpStyleBuf)
1838 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1839 TRACE("(%p, %d, %p):\n", hKL, nItem, lpStyleBuf);
1840 if (immHkl->hIME && immHkl->pImeGetRegisterWordStyle)
1842 if (!is_kbd_ime_unicode(immHkl))
1843 return immHkl->pImeGetRegisterWordStyle(nItem,(LPSTYLEBUFW)lpStyleBuf);
1844 else
1846 STYLEBUFW sbw;
1847 UINT rc;
1849 rc = immHkl->pImeGetRegisterWordStyle(nItem,&sbw);
1850 WideCharToMultiByte(CP_ACP, 0, sbw.szDescription, -1,
1851 lpStyleBuf->szDescription, 32, NULL, NULL);
1852 lpStyleBuf->dwStyle = sbw.dwStyle;
1853 return rc;
1856 else
1857 return 0;
1860 /***********************************************************************
1861 * ImmGetRegisterWordStyleW (IMM32.@)
1863 UINT WINAPI ImmGetRegisterWordStyleW(
1864 HKL hKL, UINT nItem, LPSTYLEBUFW lpStyleBuf)
1866 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1867 TRACE("(%p, %d, %p):\n", hKL, nItem, lpStyleBuf);
1868 if (immHkl->hIME && immHkl->pImeGetRegisterWordStyle)
1870 if (is_kbd_ime_unicode(immHkl))
1871 return immHkl->pImeGetRegisterWordStyle(nItem,lpStyleBuf);
1872 else
1874 STYLEBUFA sba;
1875 UINT rc;
1877 rc = immHkl->pImeGetRegisterWordStyle(nItem,(LPSTYLEBUFW)&sba);
1878 MultiByteToWideChar(CP_ACP, 0, sba.szDescription, -1,
1879 lpStyleBuf->szDescription, 32);
1880 lpStyleBuf->dwStyle = sba.dwStyle;
1881 return rc;
1884 else
1885 return 0;
1888 /***********************************************************************
1889 * ImmGetStatusWindowPos (IMM32.@)
1891 BOOL WINAPI ImmGetStatusWindowPos(HIMC hIMC, LPPOINT lpptPos)
1893 InputContextData *data = get_imc_data(hIMC);
1895 TRACE("(%p, %p)\n", hIMC, lpptPos);
1897 if (!data || !lpptPos)
1898 return FALSE;
1900 *lpptPos = data->IMC.ptStatusWndPos;
1902 return TRUE;
1905 /***********************************************************************
1906 * ImmGetVirtualKey (IMM32.@)
1908 UINT WINAPI ImmGetVirtualKey(HWND hWnd)
1910 OSVERSIONINFOA version;
1911 InputContextData *data = ImmGetContext( hWnd );
1912 TRACE("%p\n", hWnd);
1914 if ( data )
1915 return data->lastVK;
1917 version.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
1918 GetVersionExA( &version );
1919 switch(version.dwPlatformId)
1921 case VER_PLATFORM_WIN32_WINDOWS:
1922 return VK_PROCESSKEY;
1923 case VER_PLATFORM_WIN32_NT:
1924 return 0;
1925 default:
1926 FIXME("%d not supported\n",version.dwPlatformId);
1927 return VK_PROCESSKEY;
1931 /***********************************************************************
1932 * ImmInstallIMEA (IMM32.@)
1934 HKL WINAPI ImmInstallIMEA(
1935 LPCSTR lpszIMEFileName, LPCSTR lpszLayoutText)
1937 LPWSTR lpszwIMEFileName;
1938 LPWSTR lpszwLayoutText;
1939 HKL hkl;
1941 TRACE ("(%s, %s)\n", debugstr_a(lpszIMEFileName),
1942 debugstr_a(lpszLayoutText));
1944 lpszwIMEFileName = strdupAtoW(lpszIMEFileName);
1945 lpszwLayoutText = strdupAtoW(lpszLayoutText);
1947 hkl = ImmInstallIMEW(lpszwIMEFileName, lpszwLayoutText);
1949 HeapFree(GetProcessHeap(),0,lpszwIMEFileName);
1950 HeapFree(GetProcessHeap(),0,lpszwLayoutText);
1951 return hkl;
1954 /***********************************************************************
1955 * ImmInstallIMEW (IMM32.@)
1957 HKL WINAPI ImmInstallIMEW(
1958 LPCWSTR lpszIMEFileName, LPCWSTR lpszLayoutText)
1960 INT lcid = GetUserDefaultLCID();
1961 INT count;
1962 HKL hkl;
1963 DWORD rc;
1964 HKEY hkey;
1965 WCHAR regKey[sizeof(szImeRegFmt)/sizeof(WCHAR)+8];
1967 TRACE ("(%s, %s):\n", debugstr_w(lpszIMEFileName),
1968 debugstr_w(lpszLayoutText));
1970 /* Start with 2. e001 will be blank and so default to the wine internal IME */
1971 count = 2;
1973 while (count < 0xfff)
1975 DWORD disposition = 0;
1977 hkl = (HKL)MAKELPARAM( lcid, 0xe000 | count );
1978 wsprintfW( regKey, szImeRegFmt, (ULONG_PTR)hkl);
1980 rc = RegCreateKeyExW(HKEY_LOCAL_MACHINE, regKey, 0, NULL, 0, KEY_WRITE, NULL, &hkey, &disposition);
1981 if (rc == ERROR_SUCCESS && disposition == REG_CREATED_NEW_KEY)
1982 break;
1983 else if (rc == ERROR_SUCCESS)
1984 RegCloseKey(hkey);
1986 count++;
1989 if (count == 0xfff)
1991 WARN("Unable to find slot to install IME\n");
1992 return 0;
1995 if (rc == ERROR_SUCCESS)
1997 rc = RegSetValueExW(hkey, szImeFileW, 0, REG_SZ, (const BYTE*)lpszIMEFileName,
1998 (lstrlenW(lpszIMEFileName) + 1) * sizeof(WCHAR));
1999 if (rc == ERROR_SUCCESS)
2000 rc = RegSetValueExW(hkey, szLayoutTextW, 0, REG_SZ, (const BYTE*)lpszLayoutText,
2001 (lstrlenW(lpszLayoutText) + 1) * sizeof(WCHAR));
2002 RegCloseKey(hkey);
2003 return hkl;
2005 else
2007 WARN("Unable to set IME registry values\n");
2008 return 0;
2012 /***********************************************************************
2013 * ImmIsIME (IMM32.@)
2015 BOOL WINAPI ImmIsIME(HKL hKL)
2017 ImmHkl *ptr;
2018 TRACE("(%p):\n", hKL);
2019 ptr = IMM_GetImmHkl(hKL);
2020 return (ptr && ptr->hIME);
2023 /***********************************************************************
2024 * ImmIsUIMessageA (IMM32.@)
2026 BOOL WINAPI ImmIsUIMessageA(
2027 HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam)
2029 TRACE("(%p, %x, %ld, %ld)\n", hWndIME, msg, wParam, lParam);
2030 if ((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
2031 (msg == WM_IME_SETCONTEXT) ||
2032 (msg == WM_IME_NOTIFY) ||
2033 (msg == WM_IME_COMPOSITIONFULL) ||
2034 (msg == WM_IME_SELECT) ||
2035 (msg == 0x287 /* FIXME: WM_IME_SYSTEM */))
2037 if (hWndIME)
2038 SendMessageA(hWndIME, msg, wParam, lParam);
2040 return TRUE;
2042 return FALSE;
2045 /***********************************************************************
2046 * ImmIsUIMessageW (IMM32.@)
2048 BOOL WINAPI ImmIsUIMessageW(
2049 HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam)
2051 TRACE("(%p, %x, %ld, %ld)\n", hWndIME, msg, wParam, lParam);
2052 if ((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
2053 (msg == WM_IME_SETCONTEXT) ||
2054 (msg == WM_IME_NOTIFY) ||
2055 (msg == WM_IME_COMPOSITIONFULL) ||
2056 (msg == WM_IME_SELECT) ||
2057 (msg == 0x287 /* FIXME: WM_IME_SYSTEM */))
2059 if (hWndIME)
2060 SendMessageW(hWndIME, msg, wParam, lParam);
2062 return TRUE;
2064 return FALSE;
2067 /***********************************************************************
2068 * ImmNotifyIME (IMM32.@)
2070 BOOL WINAPI ImmNotifyIME(
2071 HIMC hIMC, DWORD dwAction, DWORD dwIndex, DWORD dwValue)
2073 InputContextData *data = get_imc_data(hIMC);
2075 TRACE("(%p, %d, %d, %d)\n",
2076 hIMC, dwAction, dwIndex, dwValue);
2078 if (hIMC == NULL)
2080 SetLastError(ERROR_SUCCESS);
2081 return FALSE;
2084 if (!data || ! data->immKbd->pNotifyIME)
2086 return FALSE;
2089 return data->immKbd->pNotifyIME(hIMC,dwAction,dwIndex,dwValue);
2092 /***********************************************************************
2093 * ImmRegisterWordA (IMM32.@)
2095 BOOL WINAPI ImmRegisterWordA(
2096 HKL hKL, LPCSTR lpszReading, DWORD dwStyle, LPCSTR lpszRegister)
2098 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
2099 TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_a(lpszReading), dwStyle,
2100 debugstr_a(lpszRegister));
2101 if (immHkl->hIME && immHkl->pImeRegisterWord)
2103 if (!is_kbd_ime_unicode(immHkl))
2104 return immHkl->pImeRegisterWord((LPCWSTR)lpszReading,dwStyle,
2105 (LPCWSTR)lpszRegister);
2106 else
2108 LPWSTR lpszwReading = strdupAtoW(lpszReading);
2109 LPWSTR lpszwRegister = strdupAtoW(lpszRegister);
2110 BOOL rc;
2112 rc = immHkl->pImeRegisterWord(lpszwReading,dwStyle,lpszwRegister);
2113 HeapFree(GetProcessHeap(),0,lpszwReading);
2114 HeapFree(GetProcessHeap(),0,lpszwRegister);
2115 return rc;
2118 else
2119 return FALSE;
2122 /***********************************************************************
2123 * ImmRegisterWordW (IMM32.@)
2125 BOOL WINAPI ImmRegisterWordW(
2126 HKL hKL, LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszRegister)
2128 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
2129 TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_w(lpszReading), dwStyle,
2130 debugstr_w(lpszRegister));
2131 if (immHkl->hIME && immHkl->pImeRegisterWord)
2133 if (is_kbd_ime_unicode(immHkl))
2134 return immHkl->pImeRegisterWord(lpszReading,dwStyle,lpszRegister);
2135 else
2137 LPSTR lpszaReading = strdupWtoA(lpszReading);
2138 LPSTR lpszaRegister = strdupWtoA(lpszRegister);
2139 BOOL rc;
2141 rc = immHkl->pImeRegisterWord((LPCWSTR)lpszaReading,dwStyle,
2142 (LPCWSTR)lpszaRegister);
2143 HeapFree(GetProcessHeap(),0,lpszaReading);
2144 HeapFree(GetProcessHeap(),0,lpszaRegister);
2145 return rc;
2148 else
2149 return FALSE;
2152 /***********************************************************************
2153 * ImmReleaseContext (IMM32.@)
2155 BOOL WINAPI ImmReleaseContext(HWND hWnd, HIMC hIMC)
2157 static BOOL shown = FALSE;
2159 if (!shown) {
2160 FIXME("(%p, %p): stub\n", hWnd, hIMC);
2161 shown = TRUE;
2163 return TRUE;
2166 /***********************************************************************
2167 * ImmRequestMessageA(IMM32.@)
2169 LRESULT WINAPI ImmRequestMessageA(HIMC hIMC, WPARAM wParam, LPARAM lParam)
2171 InputContextData *data = get_imc_data(hIMC);
2173 TRACE("%p %ld %ld\n", hIMC, wParam, wParam);
2175 if (data) return SendMessageA(data->IMC.hWnd, WM_IME_REQUEST, wParam, lParam);
2177 SetLastError(ERROR_INVALID_HANDLE);
2178 return 0;
2181 /***********************************************************************
2182 * ImmRequestMessageW(IMM32.@)
2184 LRESULT WINAPI ImmRequestMessageW(HIMC hIMC, WPARAM wParam, LPARAM lParam)
2186 InputContextData *data = get_imc_data(hIMC);
2188 TRACE("%p %ld %ld\n", hIMC, wParam, wParam);
2190 if (data) return SendMessageW(data->IMC.hWnd, WM_IME_REQUEST, wParam, lParam);
2192 SetLastError(ERROR_INVALID_HANDLE);
2193 return 0;
2196 /***********************************************************************
2197 * ImmSetCandidateWindow (IMM32.@)
2199 BOOL WINAPI ImmSetCandidateWindow(
2200 HIMC hIMC, LPCANDIDATEFORM lpCandidate)
2202 InputContextData *data = get_imc_data(hIMC);
2204 TRACE("(%p, %p)\n", hIMC, lpCandidate);
2206 if (!data || !lpCandidate)
2207 return FALSE;
2209 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2210 return FALSE;
2212 TRACE("\t%x, %x, (%i,%i), (%i,%i - %i,%i)\n",
2213 lpCandidate->dwIndex, lpCandidate->dwStyle,
2214 lpCandidate->ptCurrentPos.x, lpCandidate->ptCurrentPos.y,
2215 lpCandidate->rcArea.top, lpCandidate->rcArea.left,
2216 lpCandidate->rcArea.bottom, lpCandidate->rcArea.right);
2218 if ( lpCandidate->dwIndex >= (sizeof(data->IMC.cfCandForm) / sizeof(CANDIDATEFORM)) )
2219 return FALSE;
2221 data->IMC.cfCandForm[lpCandidate->dwIndex] = *lpCandidate;
2222 ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCANDIDATEPOS);
2223 ImmInternalSendIMENotify(data, IMN_SETCANDIDATEPOS, 1 << lpCandidate->dwIndex);
2225 return TRUE;
2228 /***********************************************************************
2229 * ImmSetCompositionFontA (IMM32.@)
2231 BOOL WINAPI ImmSetCompositionFontA(HIMC hIMC, LPLOGFONTA lplf)
2233 InputContextData *data = get_imc_data(hIMC);
2234 TRACE("(%p, %p)\n", hIMC, lplf);
2236 if (!data || !lplf)
2238 SetLastError(ERROR_INVALID_HANDLE);
2239 return FALSE;
2242 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2243 return FALSE;
2245 memcpy(&data->IMC.lfFont.W,lplf,sizeof(LOGFONTA));
2246 MultiByteToWideChar(CP_ACP, 0, lplf->lfFaceName, -1, data->IMC.lfFont.W.lfFaceName,
2247 LF_FACESIZE);
2248 ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCOMPOSITIONFONT);
2249 ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONFONT, 0);
2251 return TRUE;
2254 /***********************************************************************
2255 * ImmSetCompositionFontW (IMM32.@)
2257 BOOL WINAPI ImmSetCompositionFontW(HIMC hIMC, LPLOGFONTW lplf)
2259 InputContextData *data = get_imc_data(hIMC);
2260 TRACE("(%p, %p)\n", hIMC, lplf);
2262 if (!data || !lplf)
2264 SetLastError(ERROR_INVALID_HANDLE);
2265 return FALSE;
2268 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2269 return FALSE;
2271 data->IMC.lfFont.W = *lplf;
2272 ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCOMPOSITIONFONT);
2273 ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONFONT, 0);
2275 return TRUE;
2278 /***********************************************************************
2279 * ImmSetCompositionStringA (IMM32.@)
2281 BOOL WINAPI ImmSetCompositionStringA(
2282 HIMC hIMC, DWORD dwIndex,
2283 LPCVOID lpComp, DWORD dwCompLen,
2284 LPCVOID lpRead, DWORD dwReadLen)
2286 DWORD comp_len;
2287 DWORD read_len;
2288 WCHAR *CompBuffer = NULL;
2289 WCHAR *ReadBuffer = NULL;
2290 BOOL rc;
2291 InputContextData *data = get_imc_data(hIMC);
2293 TRACE("(%p, %d, %p, %d, %p, %d):\n",
2294 hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
2296 if (!data)
2297 return FALSE;
2299 if (!(dwIndex == SCS_SETSTR ||
2300 dwIndex == SCS_CHANGEATTR ||
2301 dwIndex == SCS_CHANGECLAUSE ||
2302 dwIndex == SCS_SETRECONVERTSTRING ||
2303 dwIndex == SCS_QUERYRECONVERTSTRING))
2304 return FALSE;
2306 if (!is_himc_ime_unicode(data))
2307 return data->immKbd->pImeSetCompositionString(hIMC, dwIndex, lpComp,
2308 dwCompLen, lpRead, dwReadLen);
2310 comp_len = MultiByteToWideChar(CP_ACP, 0, lpComp, dwCompLen, NULL, 0);
2311 if (comp_len)
2313 CompBuffer = HeapAlloc(GetProcessHeap(),0,comp_len * sizeof(WCHAR));
2314 MultiByteToWideChar(CP_ACP, 0, lpComp, dwCompLen, CompBuffer, comp_len);
2317 read_len = MultiByteToWideChar(CP_ACP, 0, lpRead, dwReadLen, NULL, 0);
2318 if (read_len)
2320 ReadBuffer = HeapAlloc(GetProcessHeap(),0,read_len * sizeof(WCHAR));
2321 MultiByteToWideChar(CP_ACP, 0, lpRead, dwReadLen, ReadBuffer, read_len);
2324 rc = ImmSetCompositionStringW(hIMC, dwIndex, CompBuffer, comp_len,
2325 ReadBuffer, read_len);
2327 HeapFree(GetProcessHeap(), 0, CompBuffer);
2328 HeapFree(GetProcessHeap(), 0, ReadBuffer);
2330 return rc;
2333 /***********************************************************************
2334 * ImmSetCompositionStringW (IMM32.@)
2336 BOOL WINAPI ImmSetCompositionStringW(
2337 HIMC hIMC, DWORD dwIndex,
2338 LPCVOID lpComp, DWORD dwCompLen,
2339 LPCVOID lpRead, DWORD dwReadLen)
2341 DWORD comp_len;
2342 DWORD read_len;
2343 CHAR *CompBuffer = NULL;
2344 CHAR *ReadBuffer = NULL;
2345 BOOL rc;
2346 InputContextData *data = get_imc_data(hIMC);
2348 TRACE("(%p, %d, %p, %d, %p, %d):\n",
2349 hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
2351 if (!data)
2352 return FALSE;
2354 if (!(dwIndex == SCS_SETSTR ||
2355 dwIndex == SCS_CHANGEATTR ||
2356 dwIndex == SCS_CHANGECLAUSE ||
2357 dwIndex == SCS_SETRECONVERTSTRING ||
2358 dwIndex == SCS_QUERYRECONVERTSTRING))
2359 return FALSE;
2361 if (is_himc_ime_unicode(data))
2362 return data->immKbd->pImeSetCompositionString(hIMC, dwIndex, lpComp,
2363 dwCompLen, lpRead, dwReadLen);
2365 comp_len = WideCharToMultiByte(CP_ACP, 0, lpComp, dwCompLen, NULL, 0, NULL,
2366 NULL);
2367 if (comp_len)
2369 CompBuffer = HeapAlloc(GetProcessHeap(),0,comp_len);
2370 WideCharToMultiByte(CP_ACP, 0, lpComp, dwCompLen, CompBuffer, comp_len,
2371 NULL, NULL);
2374 read_len = WideCharToMultiByte(CP_ACP, 0, lpRead, dwReadLen, NULL, 0, NULL,
2375 NULL);
2376 if (read_len)
2378 ReadBuffer = HeapAlloc(GetProcessHeap(),0,read_len);
2379 WideCharToMultiByte(CP_ACP, 0, lpRead, dwReadLen, ReadBuffer, read_len,
2380 NULL, NULL);
2383 rc = ImmSetCompositionStringA(hIMC, dwIndex, CompBuffer, comp_len,
2384 ReadBuffer, read_len);
2386 HeapFree(GetProcessHeap(), 0, CompBuffer);
2387 HeapFree(GetProcessHeap(), 0, ReadBuffer);
2389 return rc;
2392 /***********************************************************************
2393 * ImmSetCompositionWindow (IMM32.@)
2395 BOOL WINAPI ImmSetCompositionWindow(
2396 HIMC hIMC, LPCOMPOSITIONFORM lpCompForm)
2398 BOOL reshow = FALSE;
2399 InputContextData *data = get_imc_data(hIMC);
2401 TRACE("(%p, %p)\n", hIMC, lpCompForm);
2402 TRACE("\t%x, (%i,%i), (%i,%i - %i,%i)\n",lpCompForm->dwStyle,
2403 lpCompForm->ptCurrentPos.x, lpCompForm->ptCurrentPos.y, lpCompForm->rcArea.top,
2404 lpCompForm->rcArea.left, lpCompForm->rcArea.bottom, lpCompForm->rcArea.right);
2406 if (!data)
2408 SetLastError(ERROR_INVALID_HANDLE);
2409 return FALSE;
2412 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2413 return FALSE;
2415 data->IMC.cfCompForm = *lpCompForm;
2417 if (IsWindowVisible(data->immKbd->UIWnd))
2419 reshow = TRUE;
2420 ShowWindow(data->immKbd->UIWnd,SW_HIDE);
2423 /* FIXME: this is a partial stub */
2425 if (reshow)
2426 ShowWindow(data->immKbd->UIWnd,SW_SHOWNOACTIVATE);
2428 ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONWINDOW, 0);
2429 return TRUE;
2432 /***********************************************************************
2433 * ImmSetConversionStatus (IMM32.@)
2435 BOOL WINAPI ImmSetConversionStatus(
2436 HIMC hIMC, DWORD fdwConversion, DWORD fdwSentence)
2438 DWORD oldConversion, oldSentence;
2439 InputContextData *data = get_imc_data(hIMC);
2441 TRACE("%p %d %d\n", hIMC, fdwConversion, fdwSentence);
2443 if (!data)
2445 SetLastError(ERROR_INVALID_HANDLE);
2446 return FALSE;
2449 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2450 return FALSE;
2452 if ( fdwConversion != data->IMC.fdwConversion )
2454 oldConversion = data->IMC.fdwConversion;
2455 data->IMC.fdwConversion = fdwConversion;
2456 ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, oldConversion, IMC_SETCONVERSIONMODE);
2457 ImmInternalSendIMENotify(data, IMN_SETCONVERSIONMODE, 0);
2459 if ( fdwSentence != data->IMC.fdwSentence )
2461 oldSentence = data->IMC.fdwSentence;
2462 data->IMC.fdwSentence = fdwSentence;
2463 ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, oldSentence, IMC_SETSENTENCEMODE);
2464 ImmInternalSendIMENotify(data, IMN_SETSENTENCEMODE, 0);
2467 return TRUE;
2470 /***********************************************************************
2471 * ImmSetOpenStatus (IMM32.@)
2473 BOOL WINAPI ImmSetOpenStatus(HIMC hIMC, BOOL fOpen)
2475 InputContextData *data = get_imc_data(hIMC);
2477 TRACE("%p %d\n", hIMC, fOpen);
2479 if (!data)
2481 SetLastError(ERROR_INVALID_HANDLE);
2482 return FALSE;
2485 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2486 return FALSE;
2488 if (data->immKbd->UIWnd == NULL)
2490 /* create the ime window */
2491 data->immKbd->UIWnd = CreateWindowExW( WS_EX_TOOLWINDOW,
2492 data->immKbd->imeClassName, NULL, WS_POPUP, 0, 0, 1, 1, 0,
2493 0, data->immKbd->hIME, 0);
2494 SetWindowLongPtrW(data->immKbd->UIWnd, IMMGWL_IMC, (LONG_PTR)data);
2496 else if (fOpen)
2497 SetWindowLongPtrW(data->immKbd->UIWnd, IMMGWL_IMC, (LONG_PTR)data);
2499 if (!fOpen != !data->IMC.fOpen)
2501 data->IMC.fOpen = fOpen;
2502 ImmNotifyIME( hIMC, NI_CONTEXTUPDATED, 0, IMC_SETOPENSTATUS);
2503 ImmInternalSendIMENotify(data, IMN_SETOPENSTATUS, 0);
2506 return TRUE;
2509 /***********************************************************************
2510 * ImmSetStatusWindowPos (IMM32.@)
2512 BOOL WINAPI ImmSetStatusWindowPos(HIMC hIMC, LPPOINT lpptPos)
2514 InputContextData *data = get_imc_data(hIMC);
2516 TRACE("(%p, %p)\n", hIMC, lpptPos);
2518 if (!data || !lpptPos)
2520 SetLastError(ERROR_INVALID_HANDLE);
2521 return FALSE;
2524 if (IMM_IsCrossThreadAccess(NULL, hIMC))
2525 return FALSE;
2527 TRACE("\t(%i,%i)\n", lpptPos->x, lpptPos->y);
2529 data->IMC.ptStatusWndPos = *lpptPos;
2530 ImmNotifyIME( hIMC, NI_CONTEXTUPDATED, 0, IMC_SETSTATUSWINDOWPOS);
2531 ImmInternalSendIMENotify(data, IMN_SETSTATUSWINDOWPOS, 0);
2533 return TRUE;
2536 /***********************************************************************
2537 * ImmCreateSoftKeyboard(IMM32.@)
2539 HWND WINAPI ImmCreateSoftKeyboard(UINT uType, UINT hOwner, int x, int y)
2541 FIXME("(%d, %d, %d, %d): stub\n", uType, hOwner, x, y);
2542 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2543 return 0;
2546 /***********************************************************************
2547 * ImmDestroySoftKeyboard(IMM32.@)
2549 BOOL WINAPI ImmDestroySoftKeyboard(HWND hSoftWnd)
2551 FIXME("(%p): stub\n", hSoftWnd);
2552 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2553 return FALSE;
2556 /***********************************************************************
2557 * ImmShowSoftKeyboard(IMM32.@)
2559 BOOL WINAPI ImmShowSoftKeyboard(HWND hSoftWnd, int nCmdShow)
2561 FIXME("(%p, %d): stub\n", hSoftWnd, nCmdShow);
2562 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2563 return FALSE;
2566 /***********************************************************************
2567 * ImmSimulateHotKey (IMM32.@)
2569 BOOL WINAPI ImmSimulateHotKey(HWND hWnd, DWORD dwHotKeyID)
2571 FIXME("(%p, %d): stub\n", hWnd, dwHotKeyID);
2572 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2573 return FALSE;
2576 /***********************************************************************
2577 * ImmUnregisterWordA (IMM32.@)
2579 BOOL WINAPI ImmUnregisterWordA(
2580 HKL hKL, LPCSTR lpszReading, DWORD dwStyle, LPCSTR lpszUnregister)
2582 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
2583 TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_a(lpszReading), dwStyle,
2584 debugstr_a(lpszUnregister));
2585 if (immHkl->hIME && immHkl->pImeUnregisterWord)
2587 if (!is_kbd_ime_unicode(immHkl))
2588 return immHkl->pImeUnregisterWord((LPCWSTR)lpszReading,dwStyle,
2589 (LPCWSTR)lpszUnregister);
2590 else
2592 LPWSTR lpszwReading = strdupAtoW(lpszReading);
2593 LPWSTR lpszwUnregister = strdupAtoW(lpszUnregister);
2594 BOOL rc;
2596 rc = immHkl->pImeUnregisterWord(lpszwReading,dwStyle,lpszwUnregister);
2597 HeapFree(GetProcessHeap(),0,lpszwReading);
2598 HeapFree(GetProcessHeap(),0,lpszwUnregister);
2599 return rc;
2602 else
2603 return FALSE;
2606 /***********************************************************************
2607 * ImmUnregisterWordW (IMM32.@)
2609 BOOL WINAPI ImmUnregisterWordW(
2610 HKL hKL, LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszUnregister)
2612 ImmHkl *immHkl = IMM_GetImmHkl(hKL);
2613 TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_w(lpszReading), dwStyle,
2614 debugstr_w(lpszUnregister));
2615 if (immHkl->hIME && immHkl->pImeUnregisterWord)
2617 if (is_kbd_ime_unicode(immHkl))
2618 return immHkl->pImeUnregisterWord(lpszReading,dwStyle,lpszUnregister);
2619 else
2621 LPSTR lpszaReading = strdupWtoA(lpszReading);
2622 LPSTR lpszaUnregister = strdupWtoA(lpszUnregister);
2623 BOOL rc;
2625 rc = immHkl->pImeUnregisterWord((LPCWSTR)lpszaReading,dwStyle,
2626 (LPCWSTR)lpszaUnregister);
2627 HeapFree(GetProcessHeap(),0,lpszaReading);
2628 HeapFree(GetProcessHeap(),0,lpszaUnregister);
2629 return rc;
2632 else
2633 return FALSE;
2636 /***********************************************************************
2637 * ImmGetImeMenuItemsA (IMM32.@)
2639 DWORD WINAPI ImmGetImeMenuItemsA( HIMC hIMC, DWORD dwFlags, DWORD dwType,
2640 LPIMEMENUITEMINFOA lpImeParentMenu, LPIMEMENUITEMINFOA lpImeMenu,
2641 DWORD dwSize)
2643 InputContextData *data = get_imc_data(hIMC);
2644 TRACE("(%p, %i, %i, %p, %p, %i):\n", hIMC, dwFlags, dwType,
2645 lpImeParentMenu, lpImeMenu, dwSize);
2647 if (!data)
2649 SetLastError(ERROR_INVALID_HANDLE);
2650 return 0;
2653 if (data->immKbd->hIME && data->immKbd->pImeGetImeMenuItems)
2655 if (!is_himc_ime_unicode(data) || (!lpImeParentMenu && !lpImeMenu))
2656 return data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2657 (IMEMENUITEMINFOW*)lpImeParentMenu,
2658 (IMEMENUITEMINFOW*)lpImeMenu, dwSize);
2659 else
2661 IMEMENUITEMINFOW lpImeParentMenuW;
2662 IMEMENUITEMINFOW *lpImeMenuW, *parent = NULL;
2663 DWORD rc;
2665 if (lpImeParentMenu)
2666 parent = &lpImeParentMenuW;
2667 if (lpImeMenu)
2669 int count = dwSize / sizeof(LPIMEMENUITEMINFOA);
2670 dwSize = count * sizeof(IMEMENUITEMINFOW);
2671 lpImeMenuW = HeapAlloc(GetProcessHeap(), 0, dwSize);
2673 else
2674 lpImeMenuW = NULL;
2676 rc = data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2677 parent, lpImeMenuW, dwSize);
2679 if (lpImeParentMenu)
2681 memcpy(lpImeParentMenu,&lpImeParentMenuW,sizeof(IMEMENUITEMINFOA));
2682 lpImeParentMenu->hbmpItem = lpImeParentMenuW.hbmpItem;
2683 WideCharToMultiByte(CP_ACP, 0, lpImeParentMenuW.szString,
2684 -1, lpImeParentMenu->szString, IMEMENUITEM_STRING_SIZE,
2685 NULL, NULL);
2687 if (lpImeMenu && rc)
2689 unsigned int i;
2690 for (i = 0; i < rc; i++)
2692 memcpy(&lpImeMenu[i],&lpImeMenuW[1],sizeof(IMEMENUITEMINFOA));
2693 lpImeMenu[i].hbmpItem = lpImeMenuW[i].hbmpItem;
2694 WideCharToMultiByte(CP_ACP, 0, lpImeMenuW[i].szString,
2695 -1, lpImeMenu[i].szString, IMEMENUITEM_STRING_SIZE,
2696 NULL, NULL);
2699 HeapFree(GetProcessHeap(),0,lpImeMenuW);
2700 return rc;
2703 else
2704 return 0;
2707 /***********************************************************************
2708 * ImmGetImeMenuItemsW (IMM32.@)
2710 DWORD WINAPI ImmGetImeMenuItemsW( HIMC hIMC, DWORD dwFlags, DWORD dwType,
2711 LPIMEMENUITEMINFOW lpImeParentMenu, LPIMEMENUITEMINFOW lpImeMenu,
2712 DWORD dwSize)
2714 InputContextData *data = get_imc_data(hIMC);
2715 TRACE("(%p, %i, %i, %p, %p, %i):\n", hIMC, dwFlags, dwType,
2716 lpImeParentMenu, lpImeMenu, dwSize);
2718 if (!data)
2720 SetLastError(ERROR_INVALID_HANDLE);
2721 return 0;
2724 if (data->immKbd->hIME && data->immKbd->pImeGetImeMenuItems)
2726 if (is_himc_ime_unicode(data) || (!lpImeParentMenu && !lpImeMenu))
2727 return data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2728 lpImeParentMenu, lpImeMenu, dwSize);
2729 else
2731 IMEMENUITEMINFOA lpImeParentMenuA;
2732 IMEMENUITEMINFOA *lpImeMenuA, *parent = NULL;
2733 DWORD rc;
2735 if (lpImeParentMenu)
2736 parent = &lpImeParentMenuA;
2737 if (lpImeMenu)
2739 int count = dwSize / sizeof(LPIMEMENUITEMINFOW);
2740 dwSize = count * sizeof(IMEMENUITEMINFOA);
2741 lpImeMenuA = HeapAlloc(GetProcessHeap(), 0, dwSize);
2743 else
2744 lpImeMenuA = NULL;
2746 rc = data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2747 (IMEMENUITEMINFOW*)parent,
2748 (IMEMENUITEMINFOW*)lpImeMenuA, dwSize);
2750 if (lpImeParentMenu)
2752 memcpy(lpImeParentMenu,&lpImeParentMenuA,sizeof(IMEMENUITEMINFOA));
2753 lpImeParentMenu->hbmpItem = lpImeParentMenuA.hbmpItem;
2754 MultiByteToWideChar(CP_ACP, 0, lpImeParentMenuA.szString,
2755 -1, lpImeParentMenu->szString, IMEMENUITEM_STRING_SIZE);
2757 if (lpImeMenu && rc)
2759 unsigned int i;
2760 for (i = 0; i < rc; i++)
2762 memcpy(&lpImeMenu[i],&lpImeMenuA[1],sizeof(IMEMENUITEMINFOA));
2763 lpImeMenu[i].hbmpItem = lpImeMenuA[i].hbmpItem;
2764 MultiByteToWideChar(CP_ACP, 0, lpImeMenuA[i].szString,
2765 -1, lpImeMenu[i].szString, IMEMENUITEM_STRING_SIZE);
2768 HeapFree(GetProcessHeap(),0,lpImeMenuA);
2769 return rc;
2772 else
2773 return 0;
2776 /***********************************************************************
2777 * ImmLockIMC(IMM32.@)
2779 LPINPUTCONTEXT WINAPI ImmLockIMC(HIMC hIMC)
2781 InputContextData *data = get_imc_data(hIMC);
2783 if (!data)
2784 return NULL;
2785 data->dwLock++;
2786 return &data->IMC;
2789 /***********************************************************************
2790 * ImmUnlockIMC(IMM32.@)
2792 BOOL WINAPI ImmUnlockIMC(HIMC hIMC)
2794 InputContextData *data = get_imc_data(hIMC);
2796 if (!data)
2797 return FALSE;
2798 if (data->dwLock)
2799 data->dwLock--;
2800 return TRUE;
2803 /***********************************************************************
2804 * ImmGetIMCLockCount(IMM32.@)
2806 DWORD WINAPI ImmGetIMCLockCount(HIMC hIMC)
2808 InputContextData *data = get_imc_data(hIMC);
2809 if (!data)
2810 return 0;
2811 return data->dwLock;
2814 /***********************************************************************
2815 * ImmCreateIMCC(IMM32.@)
2817 HIMCC WINAPI ImmCreateIMCC(DWORD size)
2819 return GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE, size);
2822 /***********************************************************************
2823 * ImmDestroyIMCC(IMM32.@)
2825 HIMCC WINAPI ImmDestroyIMCC(HIMCC block)
2827 return GlobalFree(block);
2830 /***********************************************************************
2831 * ImmLockIMCC(IMM32.@)
2833 LPVOID WINAPI ImmLockIMCC(HIMCC imcc)
2835 return GlobalLock(imcc);
2838 /***********************************************************************
2839 * ImmUnlockIMCC(IMM32.@)
2841 BOOL WINAPI ImmUnlockIMCC(HIMCC imcc)
2843 return GlobalUnlock(imcc);
2846 /***********************************************************************
2847 * ImmGetIMCCLockCount(IMM32.@)
2849 DWORD WINAPI ImmGetIMCCLockCount(HIMCC imcc)
2851 return GlobalFlags(imcc) & GMEM_LOCKCOUNT;
2854 /***********************************************************************
2855 * ImmReSizeIMCC(IMM32.@)
2857 HIMCC WINAPI ImmReSizeIMCC(HIMCC imcc, DWORD size)
2859 return GlobalReAlloc(imcc, size, GMEM_ZEROINIT | GMEM_MOVEABLE);
2862 /***********************************************************************
2863 * ImmGetIMCCSize(IMM32.@)
2865 DWORD WINAPI ImmGetIMCCSize(HIMCC imcc)
2867 return GlobalSize(imcc);
2870 /***********************************************************************
2871 * ImmGenerateMessage(IMM32.@)
2873 BOOL WINAPI ImmGenerateMessage(HIMC hIMC)
2875 InputContextData *data = get_imc_data(hIMC);
2877 if (!data)
2879 SetLastError(ERROR_INVALID_HANDLE);
2880 return FALSE;
2883 TRACE("%i messages queued\n",data->IMC.dwNumMsgBuf);
2884 if (data->IMC.dwNumMsgBuf > 0)
2886 LPTRANSMSG lpTransMsg;
2887 DWORD i;
2889 lpTransMsg = ImmLockIMCC(data->IMC.hMsgBuf);
2890 for (i = 0; i < data->IMC.dwNumMsgBuf; i++)
2891 ImmInternalPostIMEMessage(data, lpTransMsg[i].message, lpTransMsg[i].wParam, lpTransMsg[i].lParam);
2893 ImmUnlockIMCC(data->IMC.hMsgBuf);
2895 data->IMC.dwNumMsgBuf = 0;
2898 return TRUE;
2901 /***********************************************************************
2902 * ImmTranslateMessage(IMM32.@)
2903 * ( Undocumented, call internally and from user32.dll )
2905 BOOL WINAPI ImmTranslateMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lKeyData)
2907 InputContextData *data;
2908 HIMC imc = ImmGetContext(hwnd);
2909 BYTE state[256];
2910 UINT scancode;
2911 LPVOID list = 0;
2912 UINT msg_count;
2913 UINT uVirtKey;
2914 static const DWORD list_count = 10;
2916 TRACE("%p %x %x %x\n",hwnd, msg, (UINT)wParam, (UINT)lKeyData);
2918 if (imc)
2919 data = imc;
2920 else
2921 return FALSE;
2923 if (!data->immKbd->hIME || !data->immKbd->pImeToAsciiEx)
2924 return FALSE;
2926 GetKeyboardState(state);
2927 scancode = lKeyData >> 0x10 & 0xff;
2929 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list_count * sizeof(TRANSMSG) + sizeof(DWORD));
2930 ((DWORD*)list)[0] = list_count;
2932 if (data->immKbd->imeInfo.fdwProperty & IME_PROP_KBD_CHAR_FIRST)
2934 WCHAR chr;
2936 if (!is_himc_ime_unicode(data))
2937 ToAscii(data->lastVK, scancode, state, &chr, 0);
2938 else
2939 ToUnicodeEx(data->lastVK, scancode, state, &chr, 1, 0, GetKeyboardLayout(0));
2940 uVirtKey = MAKELONG(data->lastVK,chr);
2942 else
2943 uVirtKey = data->lastVK;
2945 msg_count = data->immKbd->pImeToAsciiEx(uVirtKey, scancode, state, list, 0, imc);
2946 TRACE("%i messages generated\n",msg_count);
2947 if (msg_count && msg_count <= list_count)
2949 UINT i;
2950 LPTRANSMSG msgs = (LPTRANSMSG)((LPBYTE)list + sizeof(DWORD));
2952 for (i = 0; i < msg_count; i++)
2953 ImmInternalPostIMEMessage(data, msgs[i].message, msgs[i].wParam, msgs[i].lParam);
2955 else if (msg_count > list_count)
2956 ImmGenerateMessage(imc);
2958 HeapFree(GetProcessHeap(),0,list);
2960 data->lastVK = VK_PROCESSKEY;
2962 return (msg_count > 0);
2965 /***********************************************************************
2966 * ImmProcessKey(IMM32.@)
2967 * ( Undocumented, called from user32.dll )
2969 BOOL WINAPI ImmProcessKey(HWND hwnd, HKL hKL, UINT vKey, LPARAM lKeyData, DWORD unknown)
2971 InputContextData *data;
2972 HIMC imc = ImmGetContext(hwnd);
2973 BYTE state[256];
2975 TRACE("%p %p %x %x %x\n",hwnd, hKL, vKey, (UINT)lKeyData, unknown);
2977 if (imc)
2978 data = imc;
2979 else
2980 return FALSE;
2982 /* Make sure we are inputting to the correct keyboard */
2983 if (data->immKbd->hkl != hKL)
2985 ImmHkl *new_hkl = IMM_GetImmHkl(hKL);
2986 if (new_hkl)
2988 data->immKbd->pImeSelect(imc, FALSE);
2989 data->immKbd->uSelected--;
2990 data->immKbd = new_hkl;
2991 data->immKbd->pImeSelect(imc, TRUE);
2992 data->immKbd->uSelected++;
2994 else
2995 return FALSE;
2998 if (!data->immKbd->hIME || !data->immKbd->pImeProcessKey)
2999 return FALSE;
3001 GetKeyboardState(state);
3002 if (data->immKbd->pImeProcessKey(imc, vKey, lKeyData, state))
3004 data->lastVK = vKey;
3005 return TRUE;
3008 data->lastVK = VK_PROCESSKEY;
3009 return FALSE;
3012 /***********************************************************************
3013 * ImmDisableTextFrameService(IMM32.@)
3015 BOOL WINAPI ImmDisableTextFrameService(DWORD idThread)
3017 FIXME("Stub\n");
3018 return FALSE;
3021 /***********************************************************************
3022 * ImmEnumInputContext(IMM32.@)
3025 BOOL WINAPI ImmEnumInputContext(DWORD idThread, IMCENUMPROC lpfn, LPARAM lParam)
3027 FIXME("Stub\n");
3028 return FALSE;
3031 /***********************************************************************
3032 * ImmGetHotKey(IMM32.@)
3035 BOOL WINAPI ImmGetHotKey(DWORD hotkey, UINT *modifiers, UINT *key, HKL hkl)
3037 FIXME("%x, %p, %p, %p: stub\n", hotkey, modifiers, key, hkl);
3038 return FALSE;