msxml3: Store output stream for processor.
[wine] / dlls / imm32 / imm.c
1 /*
2  * IMM32 library
3  *
4  * Copyright 1998 Patrik Stridvall
5  * Copyright 2002, 2003, 2007 CodeWeavers, Aric Stewart
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <stdarg.h>
23 #include <stdio.h>
24
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
37 WINE_DEFAULT_DEBUG_CHANNEL(imm);
38
39 typedef struct tagIMCCInternal
40 {
41     DWORD dwLock;
42     DWORD dwSize;
43 } IMCCInternal;
44
45 #define MAKE_FUNCPTR(f) typeof(f) * p##f
46 typedef struct _tagImmHkl{
47     struct list entry;
48     HKL         hkl;
49     HMODULE     hIME;
50     IMEINFO     imeInfo;
51     WCHAR       imeClassName[17]; /* 16 character max */
52     ULONG       uSelected;
53
54     /* Function Pointers */
55     MAKE_FUNCPTR(ImeInquire);
56     MAKE_FUNCPTR(ImeConfigure);
57     MAKE_FUNCPTR(ImeDestroy);
58     MAKE_FUNCPTR(ImeEscape);
59     MAKE_FUNCPTR(ImeSelect);
60     MAKE_FUNCPTR(ImeSetActiveContext);
61     MAKE_FUNCPTR(ImeToAsciiEx);
62     MAKE_FUNCPTR(NotifyIME);
63     MAKE_FUNCPTR(ImeRegisterWord);
64     MAKE_FUNCPTR(ImeUnregisterWord);
65     MAKE_FUNCPTR(ImeEnumRegisterWord);
66     MAKE_FUNCPTR(ImeSetCompositionString);
67     MAKE_FUNCPTR(ImeConversionList);
68     MAKE_FUNCPTR(ImeProcessKey);
69     MAKE_FUNCPTR(ImeGetRegisterWordStyle);
70     MAKE_FUNCPTR(ImeGetImeMenuItems);
71 } ImmHkl;
72 #undef MAKE_FUNCPTR
73
74 typedef struct tagInputContextData
75 {
76         DWORD           dwLock;
77         INPUTCONTEXT    IMC;
78
79         ImmHkl          *immKbd;
80         HWND            imeWnd;
81         UINT            lastVK;
82 } InputContextData;
83
84 typedef struct _tagTRANSMSG {
85     UINT message;
86     WPARAM wParam;
87     LPARAM lParam;
88 } TRANSMSG, *LPTRANSMSG;
89
90 typedef struct _tagIMMThreadData {
91     HIMC defaultContext;
92     HWND hwndDefault;
93 } IMMThreadData;
94
95 static DWORD tlsIndex = 0;
96 static struct list ImmHklList = LIST_INIT(ImmHklList);
97
98 /* MSIME messages */
99 static UINT WM_MSIME_SERVICE;
100 static UINT WM_MSIME_RECONVERTOPTIONS;
101 static UINT WM_MSIME_MOUSE;
102 static UINT WM_MSIME_RECONVERTREQUEST;
103 static UINT WM_MSIME_RECONVERT;
104 static UINT WM_MSIME_QUERYPOSITION;
105 static UINT WM_MSIME_DOCUMENTFEED;
106
107 static const WCHAR szwWineIMCProperty[] = {'W','i','n','e','I','m','m','H','I','M','C','P','r','o','p','e','r','t','y',0};
108
109 static const WCHAR szImeFileW[] = {'I','m','e',' ','F','i','l','e',0};
110 static const WCHAR szLayoutTextW[] = {'L','a','y','o','u','t',' ','T','e','x','t',0};
111 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};
112
113
114 #define is_himc_ime_unicode(p)  (p->immKbd->imeInfo.fdwProperty & IME_PROP_UNICODE)
115 #define is_kbd_ime_unicode(p)  (p->imeInfo.fdwProperty & IME_PROP_UNICODE)
116
117 static BOOL IMM_DestroyContext(HIMC hIMC);
118
119 static inline WCHAR *strdupAtoW( const char *str )
120 {
121     WCHAR *ret = NULL;
122     if (str)
123     {
124         DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
125         if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
126             MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
127     }
128     return ret;
129 }
130
131 static inline CHAR *strdupWtoA( const WCHAR *str )
132 {
133     CHAR *ret = NULL;
134     if (str)
135     {
136         DWORD len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
137         if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
138             WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
139     }
140     return ret;
141 }
142
143 static DWORD convert_candidatelist_WtoA(
144         LPCANDIDATELIST lpSrc, LPCANDIDATELIST lpDst, DWORD dwBufLen)
145 {
146     DWORD ret, i, len;
147
148     ret = FIELD_OFFSET( CANDIDATELIST, dwOffset[lpSrc->dwCount] );
149     if ( lpDst && dwBufLen > 0 )
150     {
151         *lpDst = *lpSrc;
152         lpDst->dwOffset[0] = ret;
153     }
154
155     for ( i = 0; i < lpSrc->dwCount; i++)
156     {
157         LPBYTE src = (LPBYTE)lpSrc + lpSrc->dwOffset[i];
158
159         if ( lpDst && dwBufLen > 0 )
160         {
161             LPBYTE dest = (LPBYTE)lpDst + lpDst->dwOffset[i];
162
163             len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1,
164                                       (LPSTR)dest, dwBufLen, NULL, NULL);
165
166             if ( i + 1 < lpSrc->dwCount )
167                 lpDst->dwOffset[i+1] = lpDst->dwOffset[i] + len * sizeof(char);
168             dwBufLen -= len * sizeof(char);
169         }
170         else
171             len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, NULL, 0, NULL, NULL);
172
173         ret += len * sizeof(char);
174     }
175
176     if ( lpDst )
177         lpDst->dwSize = ret;
178
179     return ret;
180 }
181
182 static DWORD convert_candidatelist_AtoW(
183         LPCANDIDATELIST lpSrc, LPCANDIDATELIST lpDst, DWORD dwBufLen)
184 {
185     DWORD ret, i, len;
186
187     ret = FIELD_OFFSET( CANDIDATELIST, dwOffset[lpSrc->dwCount] );
188     if ( lpDst && dwBufLen > 0 )
189     {
190         *lpDst = *lpSrc;
191         lpDst->dwOffset[0] = ret;
192     }
193
194     for ( i = 0; i < lpSrc->dwCount; i++)
195     {
196         LPBYTE src = (LPBYTE)lpSrc + lpSrc->dwOffset[i];
197
198         if ( lpDst && dwBufLen > 0 )
199         {
200             LPBYTE dest = (LPBYTE)lpDst + lpDst->dwOffset[i];
201
202             len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1,
203                                       (LPWSTR)dest, dwBufLen);
204
205             if ( i + 1 < lpSrc->dwCount )
206                 lpDst->dwOffset[i+1] = lpDst->dwOffset[i] + len * sizeof(WCHAR);
207             dwBufLen -= len * sizeof(WCHAR);
208         }
209         else
210             len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, NULL, 0);
211
212         ret += len * sizeof(WCHAR);
213     }
214
215     if ( lpDst )
216         lpDst->dwSize = ret;
217
218     return ret;
219 }
220
221 static IMMThreadData* IMM_GetThreadData(void)
222 {
223     IMMThreadData* data = TlsGetValue(tlsIndex);
224     if (!data)
225     {
226         data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
227                          sizeof(IMMThreadData));
228         TlsSetValue(tlsIndex,data);
229         TRACE("Thread Data Created\n");
230     }
231     return data;
232 }
233
234 static void IMM_FreeThreadData(void)
235 {
236     IMMThreadData* data = TlsGetValue(tlsIndex);
237     if (data)
238     {
239         IMM_DestroyContext(data->defaultContext);
240         DestroyWindow(data->hwndDefault);
241         HeapFree(GetProcessHeap(),0,data);
242         TRACE("Thread Data Destroyed\n");
243     }
244 }
245
246 static HMODULE LoadDefaultWineIME(void)
247 {
248     char buffer[MAX_PATH], libname[32], *name, *next;
249     HMODULE module = 0;
250     HKEY hkey;
251
252     TRACE("Attempting to fall back to wine default IME\n");
253
254     strcpy( buffer, "x11" );  /* default value */
255     /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
256     if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hkey ))
257     {
258         DWORD type, count = sizeof(buffer);
259         RegQueryValueExA( hkey, "Ime", 0, &type, (LPBYTE) buffer, &count );
260         RegCloseKey( hkey );
261     }
262
263     name = buffer;
264     while (name)
265     {
266         next = strchr( name, ',' );
267         if (next) *next++ = 0;
268
269         snprintf( libname, sizeof(libname), "wine%s.drv", name );
270         if ((module = LoadLibraryA( libname )) != 0) break;
271         name = next;
272     }
273
274     return module;
275 }
276
277 /* ImmHkl loading and freeing */
278 #define LOAD_FUNCPTR(f) if((ptr->p##f = (LPVOID)GetProcAddress(ptr->hIME, #f)) == NULL){WARN("Can't find function %s in ime\n", #f);}
279 static ImmHkl *IMM_GetImmHkl(HKL hkl)
280 {
281     ImmHkl *ptr;
282     WCHAR filename[MAX_PATH];
283
284     TRACE("Seeking ime for keyboard %p\n",hkl);
285
286     LIST_FOR_EACH_ENTRY(ptr, &ImmHklList, ImmHkl, entry)
287     {
288         if (ptr->hkl == hkl)
289             return ptr;
290     }
291     /* not found... create it */
292
293     ptr = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ImmHkl));
294
295     ptr->hkl = hkl;
296     if (ImmGetIMEFileNameW(hkl, filename, MAX_PATH)) ptr->hIME = LoadLibraryW(filename);
297     if (!ptr->hIME)
298         ptr->hIME = LoadDefaultWineIME();
299     if (ptr->hIME)
300     {
301         LOAD_FUNCPTR(ImeInquire);
302         if (!ptr->pImeInquire || !ptr->pImeInquire(&ptr->imeInfo, ptr->imeClassName, NULL))
303         {
304             FreeLibrary(ptr->hIME);
305             ptr->hIME = NULL;
306         }
307         else
308         {
309             LOAD_FUNCPTR(ImeDestroy);
310             LOAD_FUNCPTR(ImeSelect);
311             if (!ptr->pImeSelect || !ptr->pImeDestroy)
312             {
313                 FreeLibrary(ptr->hIME);
314                 ptr->hIME = NULL;
315             }
316             else
317             {
318                 LOAD_FUNCPTR(ImeConfigure);
319                 LOAD_FUNCPTR(ImeEscape);
320                 LOAD_FUNCPTR(ImeSetActiveContext);
321                 LOAD_FUNCPTR(ImeToAsciiEx);
322                 LOAD_FUNCPTR(NotifyIME);
323                 LOAD_FUNCPTR(ImeRegisterWord);
324                 LOAD_FUNCPTR(ImeUnregisterWord);
325                 LOAD_FUNCPTR(ImeEnumRegisterWord);
326                 LOAD_FUNCPTR(ImeSetCompositionString);
327                 LOAD_FUNCPTR(ImeConversionList);
328                 LOAD_FUNCPTR(ImeProcessKey);
329                 LOAD_FUNCPTR(ImeGetRegisterWordStyle);
330                 LOAD_FUNCPTR(ImeGetImeMenuItems);
331                 /* make sure our classname is WCHAR */
332                 if (!is_kbd_ime_unicode(ptr))
333                 {
334                     WCHAR bufW[17];
335                     MultiByteToWideChar(CP_ACP, 0, (LPSTR)ptr->imeClassName,
336                                         -1, bufW, 17);
337                     lstrcpyW(ptr->imeClassName, bufW);
338                 }
339             }
340         }
341     }
342     list_add_head(&ImmHklList,&ptr->entry);
343
344     return ptr;
345 }
346 #undef LOAD_FUNCPTR
347
348 static void IMM_FreeAllImmHkl(void)
349 {
350     ImmHkl *ptr,*cursor2;
351
352     LIST_FOR_EACH_ENTRY_SAFE(ptr, cursor2, &ImmHklList, ImmHkl, entry)
353     {
354         list_remove(&ptr->entry);
355         if (ptr->hIME)
356         {
357             ptr->pImeDestroy(1);
358             FreeLibrary(ptr->hIME);
359         }
360         HeapFree(GetProcessHeap(),0,ptr);
361     }
362 }
363
364 static void IMM_RegisterMessages(void)
365 {
366     WM_MSIME_SERVICE = RegisterWindowMessageA("MSIMEService");
367     WM_MSIME_RECONVERTOPTIONS = RegisterWindowMessageA("MSIMEReconvertOptions");
368     WM_MSIME_MOUSE = RegisterWindowMessageA("MSIMEMouseOperation");
369     WM_MSIME_RECONVERTREQUEST = RegisterWindowMessageA("MSIMEReconvertRequest");
370     WM_MSIME_RECONVERT = RegisterWindowMessageA("MSIMEReconvert");
371     WM_MSIME_QUERYPOSITION = RegisterWindowMessageA("MSIMEQueryPosition");
372     WM_MSIME_DOCUMENTFEED = RegisterWindowMessageA("MSIMEDocumentFeed");
373 }
374
375 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved)
376 {
377     TRACE("%p, %x, %p\n",hInstDLL,fdwReason,lpReserved);
378     switch (fdwReason)
379     {
380         case DLL_PROCESS_ATTACH:
381             IMM_RegisterMessages();
382             tlsIndex = TlsAlloc();
383             if (tlsIndex == TLS_OUT_OF_INDEXES)
384                 return FALSE;
385             break;
386         case DLL_THREAD_ATTACH:
387             break;
388         case DLL_THREAD_DETACH:
389             IMM_FreeThreadData();
390             break;
391         case DLL_PROCESS_DETACH:
392             IMM_FreeThreadData();
393             IMM_FreeAllImmHkl();
394             TlsFree(tlsIndex);
395             break;
396     }
397     return TRUE;
398 }
399
400 /* for posting messages as the IME */
401 static void ImmInternalPostIMEMessage(InputContextData *data, UINT msg, WPARAM wParam, LPARAM lParam)
402 {
403     HWND target = GetFocus();
404     if (!target)
405        PostMessageW(data->IMC.hWnd,msg,wParam,lParam);
406     else
407        PostMessageW(target, msg, wParam, lParam);
408 }
409
410 static LRESULT ImmInternalSendIMENotify(InputContextData *data, WPARAM notify, LPARAM lParam)
411 {
412     HWND target;
413
414     target = data->IMC.hWnd;
415     if (!target) target = GetFocus();
416
417     if (target)
418        return SendMessageW(target, WM_IME_NOTIFY, notify, lParam);
419
420     return 0;
421 }
422
423 static HIMCC ImmCreateBlankCompStr(void)
424 {
425     HIMCC rc;
426     LPCOMPOSITIONSTRING ptr;
427     rc = ImmCreateIMCC(sizeof(COMPOSITIONSTRING));
428     ptr = ImmLockIMCC(rc);
429     memset(ptr,0,sizeof(COMPOSITIONSTRING));
430     ptr->dwSize = sizeof(COMPOSITIONSTRING);
431     ImmUnlockIMCC(rc);
432     return rc;
433 }
434
435 /***********************************************************************
436  *              ImmAssociateContext (IMM32.@)
437  */
438 HIMC WINAPI ImmAssociateContext(HWND hWnd, HIMC hIMC)
439 {
440     HIMC old = NULL;
441     InputContextData *data = hIMC;
442
443     TRACE("(%p, %p):\n", hWnd, hIMC);
444
445     if (!IMM_GetThreadData()->defaultContext)
446         IMM_GetThreadData()->defaultContext = ImmCreateContext();
447
448     /*
449      * If already associated just return
450      */
451     if (hIMC && data->IMC.hWnd == hWnd)
452         return hIMC;
453
454     if (hWnd)
455     {
456         old = RemovePropW(hWnd,szwWineIMCProperty);
457
458         if (old == NULL)
459             old = IMM_GetThreadData()->defaultContext;
460         else if (old == (HIMC)-1)
461             old = NULL;
462
463         if (hIMC != IMM_GetThreadData()->defaultContext)
464         {
465             if (hIMC == NULL) /* Meaning disable imm for that window*/
466                 SetPropW(hWnd,szwWineIMCProperty,(HANDLE)-1);
467             else
468                 SetPropW(hWnd,szwWineIMCProperty,hIMC);
469         }
470
471         if (old)
472         {
473             InputContextData *old_data = old;
474             if (old_data->IMC.hWnd == hWnd)
475                 old_data->IMC.hWnd = NULL;
476         }
477     }
478
479     if (!hIMC)
480         return old;
481
482     if (IsWindow(data->IMC.hWnd))
483     {
484         /*
485          * Post a message that your context is switching
486          */
487         SendMessageW(data->IMC.hWnd, WM_IME_SETCONTEXT, FALSE, ISC_SHOWUIALL);
488     }
489
490     data->IMC.hWnd = hWnd;
491
492     if (IsWindow(data->IMC.hWnd))
493     {
494         /*
495          * Post a message that your context is switching
496          */
497         SendMessageW(data->IMC.hWnd, WM_IME_SETCONTEXT, TRUE, ISC_SHOWUIALL);
498     }
499
500     return old;
501 }
502
503
504 /*
505  * Helper function for ImmAssociateContextEx
506  */
507 static BOOL CALLBACK _ImmAssociateContextExEnumProc(HWND hwnd, LPARAM lParam)
508 {
509     HIMC hImc = (HIMC)lParam;
510     ImmAssociateContext(hwnd,hImc);
511     return TRUE;
512 }
513
514 /***********************************************************************
515  *              ImmAssociateContextEx (IMM32.@)
516  */
517 BOOL WINAPI ImmAssociateContextEx(HWND hWnd, HIMC hIMC, DWORD dwFlags)
518 {
519     TRACE("(%p, %p, 0x%x):\n", hWnd, hIMC, dwFlags);
520
521     if (!IMM_GetThreadData()->defaultContext)
522         IMM_GetThreadData()->defaultContext = ImmCreateContext();
523
524     if (!hWnd) return FALSE;
525
526     switch (dwFlags)
527     {
528     case 0:
529         ImmAssociateContext(hWnd,hIMC);
530         return TRUE;
531     case IACE_DEFAULT:
532         ImmAssociateContext(hWnd,IMM_GetThreadData()->defaultContext);
533         return TRUE;
534     case IACE_IGNORENOCONTEXT:
535         if (GetPropW(hWnd,szwWineIMCProperty))
536             ImmAssociateContext(hWnd,hIMC);
537         return TRUE;
538     case IACE_CHILDREN:
539         EnumChildWindows(hWnd,_ImmAssociateContextExEnumProc,(LPARAM)hIMC);
540         return TRUE;
541     default:
542         FIXME("Unknown dwFlags 0x%x\n",dwFlags);
543         return FALSE;
544     }
545 }
546
547 /***********************************************************************
548  *              ImmConfigureIMEA (IMM32.@)
549  */
550 BOOL WINAPI ImmConfigureIMEA(
551   HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
552 {
553     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
554
555     TRACE("(%p, %p, %d, %p):\n", hKL, hWnd, dwMode, lpData);
556
557     if (dwMode == IME_CONFIG_REGISTERWORD && !lpData)
558         return FALSE;
559
560     if (immHkl->hIME && immHkl->pImeConfigure)
561     {
562         if (dwMode != IME_CONFIG_REGISTERWORD || !is_kbd_ime_unicode(immHkl))
563             return immHkl->pImeConfigure(hKL,hWnd,dwMode,lpData);
564         else
565         {
566             REGISTERWORDW rww;
567             REGISTERWORDA *rwa = lpData;
568             BOOL rc;
569
570             rww.lpReading = strdupAtoW(rwa->lpReading);
571             rww.lpWord = strdupAtoW(rwa->lpWord);
572             rc = immHkl->pImeConfigure(hKL,hWnd,dwMode,&rww);
573             HeapFree(GetProcessHeap(),0,rww.lpReading);
574             HeapFree(GetProcessHeap(),0,rww.lpWord);
575             return rc;
576         }
577     }
578     else
579         return FALSE;
580 }
581
582 /***********************************************************************
583  *              ImmConfigureIMEW (IMM32.@)
584  */
585 BOOL WINAPI ImmConfigureIMEW(
586   HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
587 {
588     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
589
590     TRACE("(%p, %p, %d, %p):\n", hKL, hWnd, dwMode, lpData);
591
592     if (dwMode == IME_CONFIG_REGISTERWORD && !lpData)
593         return FALSE;
594
595     if (immHkl->hIME && immHkl->pImeConfigure)
596     {
597         if (dwMode != IME_CONFIG_REGISTERWORD || is_kbd_ime_unicode(immHkl))
598             return immHkl->pImeConfigure(hKL,hWnd,dwMode,lpData);
599         else
600         {
601             REGISTERWORDW *rww = lpData;
602             REGISTERWORDA rwa;
603             BOOL rc;
604
605             rwa.lpReading = strdupWtoA(rww->lpReading);
606             rwa.lpWord = strdupWtoA(rww->lpWord);
607             rc = immHkl->pImeConfigure(hKL,hWnd,dwMode,&rwa);
608             HeapFree(GetProcessHeap(),0,rwa.lpReading);
609             HeapFree(GetProcessHeap(),0,rwa.lpWord);
610             return rc;
611         }
612     }
613     else
614         return FALSE;
615 }
616
617 /***********************************************************************
618  *              ImmCreateContext (IMM32.@)
619  */
620 HIMC WINAPI ImmCreateContext(void)
621 {
622     InputContextData *new_context;
623     LPGUIDELINE gl;
624     LPCANDIDATEINFO ci;
625
626     new_context = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(InputContextData));
627
628     /* Load the IME */
629     new_context->immKbd = IMM_GetImmHkl(GetKeyboardLayout(0));
630
631     if (!new_context->immKbd->hIME)
632     {
633         TRACE("IME dll could not be loaded\n");
634         HeapFree(GetProcessHeap(),0,new_context);
635         return 0;
636     }
637
638     /* the HIMCCs are never NULL */
639     new_context->IMC.hCompStr = ImmCreateBlankCompStr();
640     new_context->IMC.hMsgBuf = ImmCreateIMCC(0);
641     new_context->IMC.hCandInfo = ImmCreateIMCC(sizeof(CANDIDATEINFO));
642     ci = ImmLockIMCC(new_context->IMC.hCandInfo);
643     memset(ci,0,sizeof(CANDIDATEINFO));
644     ci->dwSize = sizeof(CANDIDATEINFO);
645     ImmUnlockIMCC(new_context->IMC.hCandInfo);
646     new_context->IMC.hGuideLine = ImmCreateIMCC(sizeof(GUIDELINE));
647     gl = ImmLockIMCC(new_context->IMC.hGuideLine);
648     memset(gl,0,sizeof(GUIDELINE));
649     gl->dwSize = sizeof(GUIDELINE);
650     ImmUnlockIMCC(new_context->IMC.hGuideLine);
651
652     /* Initialize the IME Private */
653     new_context->IMC.hPrivate = ImmCreateIMCC(new_context->immKbd->imeInfo.dwPrivateDataSize);
654
655     if (!new_context->immKbd->pImeSelect(new_context, TRUE))
656     {
657         TRACE("Selection of IME failed\n");
658         IMM_DestroyContext(new_context);
659         return 0;
660     }
661     SendMessageW(GetFocus(), WM_IME_SELECT, TRUE, (LPARAM)GetKeyboardLayout(0));
662
663     new_context->immKbd->uSelected++;
664     TRACE("Created context %p\n",new_context);
665
666     return new_context;
667 }
668
669 static BOOL IMM_DestroyContext(HIMC hIMC)
670 {
671     InputContextData *data = hIMC;
672
673     TRACE("Destroying %p\n",hIMC);
674
675     if (hIMC)
676     {
677         data->immKbd->uSelected --;
678         data->immKbd->pImeSelect(hIMC, FALSE);
679         SendMessageW(data->IMC.hWnd, WM_IME_SELECT, FALSE, (LPARAM)GetKeyboardLayout(0));
680
681         if (IMM_GetThreadData()->hwndDefault == data->imeWnd)
682             IMM_GetThreadData()->hwndDefault = NULL;
683         DestroyWindow(data->imeWnd);
684
685         ImmDestroyIMCC(data->IMC.hCompStr);
686         ImmDestroyIMCC(data->IMC.hCandInfo);
687         ImmDestroyIMCC(data->IMC.hGuideLine);
688         ImmDestroyIMCC(data->IMC.hPrivate);
689         ImmDestroyIMCC(data->IMC.hMsgBuf);
690
691         HeapFree(GetProcessHeap(),0,data);
692     }
693     return TRUE;
694 }
695
696 /***********************************************************************
697  *              ImmDestroyContext (IMM32.@)
698  */
699 BOOL WINAPI ImmDestroyContext(HIMC hIMC)
700 {
701     if (hIMC != IMM_GetThreadData()->defaultContext)
702         return IMM_DestroyContext(hIMC);
703     else
704         return FALSE;
705 }
706
707 /***********************************************************************
708  *              ImmDisableIME (IMM32.@)
709  */
710 BOOL WINAPI ImmDisableIME(DWORD idThread)
711 {
712     FIXME("(%d): stub\n", idThread);
713     return TRUE;
714 }
715
716 /***********************************************************************
717  *              ImmEnumRegisterWordA (IMM32.@)
718  */
719 UINT WINAPI ImmEnumRegisterWordA(
720   HKL hKL, REGISTERWORDENUMPROCA lpfnEnumProc,
721   LPCSTR lpszReading, DWORD dwStyle,
722   LPCSTR lpszRegister, LPVOID lpData)
723 {
724     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
725     TRACE("(%p, %p, %s, %d, %s, %p):\n", hKL, lpfnEnumProc,
726         debugstr_a(lpszReading), dwStyle, debugstr_a(lpszRegister), lpData);
727     if (immHkl->hIME && immHkl->pImeEnumRegisterWord)
728     {
729         if (!is_kbd_ime_unicode(immHkl))
730             return immHkl->pImeEnumRegisterWord((REGISTERWORDENUMPROCW)lpfnEnumProc,
731                 (LPCWSTR)lpszReading, dwStyle, (LPCWSTR)lpszRegister, lpData);
732         else
733         {
734             LPWSTR lpszwReading = strdupAtoW(lpszReading);
735             LPWSTR lpszwRegister = strdupAtoW(lpszRegister);
736             BOOL rc;
737
738             rc = immHkl->pImeEnumRegisterWord((REGISTERWORDENUMPROCW)lpfnEnumProc,
739                                               lpszwReading, dwStyle, lpszwRegister,
740                                               lpData);
741
742             HeapFree(GetProcessHeap(),0,lpszwReading);
743             HeapFree(GetProcessHeap(),0,lpszwRegister);
744             return rc;
745         }
746     }
747     else
748         return 0;
749 }
750
751 /***********************************************************************
752  *              ImmEnumRegisterWordW (IMM32.@)
753  */
754 UINT WINAPI ImmEnumRegisterWordW(
755   HKL hKL, REGISTERWORDENUMPROCW lpfnEnumProc,
756   LPCWSTR lpszReading, DWORD dwStyle,
757   LPCWSTR lpszRegister, LPVOID lpData)
758 {
759     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
760     TRACE("(%p, %p, %s, %d, %s, %p):\n", hKL, lpfnEnumProc,
761         debugstr_w(lpszReading), dwStyle, debugstr_w(lpszRegister), lpData);
762     if (immHkl->hIME && immHkl->pImeEnumRegisterWord)
763     {
764         if (is_kbd_ime_unicode(immHkl))
765             return immHkl->pImeEnumRegisterWord(lpfnEnumProc, lpszReading, dwStyle,
766                                             lpszRegister, lpData);
767         else
768         {
769             LPSTR lpszaReading = strdupWtoA(lpszReading);
770             LPSTR lpszaRegister = strdupWtoA(lpszRegister);
771             BOOL rc;
772
773             rc = immHkl->pImeEnumRegisterWord(lpfnEnumProc, (LPCWSTR)lpszaReading,
774                                               dwStyle, (LPCWSTR)lpszaRegister, lpData);
775
776             HeapFree(GetProcessHeap(),0,lpszaReading);
777             HeapFree(GetProcessHeap(),0,lpszaRegister);
778             return rc;
779         }
780     }
781     else
782         return 0;
783 }
784
785 static inline BOOL EscapeRequiresWA(UINT uEscape)
786 {
787         if (uEscape == IME_ESC_GET_EUDC_DICTIONARY ||
788             uEscape == IME_ESC_SET_EUDC_DICTIONARY ||
789             uEscape == IME_ESC_IME_NAME ||
790             uEscape == IME_ESC_GETHELPFILENAME)
791         return TRUE;
792     return FALSE;
793 }
794
795 /***********************************************************************
796  *              ImmEscapeA (IMM32.@)
797  */
798 LRESULT WINAPI ImmEscapeA(
799   HKL hKL, HIMC hIMC,
800   UINT uEscape, LPVOID lpData)
801 {
802     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
803     TRACE("(%p, %p, %d, %p):\n", hKL, hIMC, uEscape, lpData);
804
805     if (immHkl->hIME && immHkl->pImeEscape)
806     {
807         if (!EscapeRequiresWA(uEscape) || !is_kbd_ime_unicode(immHkl))
808             return immHkl->pImeEscape(hIMC,uEscape,lpData);
809         else
810         {
811             WCHAR buffer[81]; /* largest required buffer should be 80 */
812             LRESULT rc;
813             if (uEscape == IME_ESC_SET_EUDC_DICTIONARY)
814             {
815                 MultiByteToWideChar(CP_ACP,0,lpData,-1,buffer,81);
816                 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
817             }
818             else
819             {
820                 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
821                 WideCharToMultiByte(CP_ACP,0,buffer,-1,lpData,80, NULL, NULL);
822             }
823             return rc;
824         }
825     }
826     else
827         return 0;
828 }
829
830 /***********************************************************************
831  *              ImmEscapeW (IMM32.@)
832  */
833 LRESULT WINAPI ImmEscapeW(
834   HKL hKL, HIMC hIMC,
835   UINT uEscape, LPVOID lpData)
836 {
837     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
838     TRACE("(%p, %p, %d, %p):\n", hKL, hIMC, uEscape, lpData);
839
840     if (immHkl->hIME && immHkl->pImeEscape)
841     {
842         if (!EscapeRequiresWA(uEscape) || is_kbd_ime_unicode(immHkl))
843             return immHkl->pImeEscape(hIMC,uEscape,lpData);
844         else
845         {
846             CHAR buffer[81]; /* largest required buffer should be 80 */
847             LRESULT rc;
848             if (uEscape == IME_ESC_SET_EUDC_DICTIONARY)
849             {
850                 WideCharToMultiByte(CP_ACP,0,lpData,-1,buffer,81, NULL, NULL);
851                 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
852             }
853             else
854             {
855                 rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
856                 MultiByteToWideChar(CP_ACP,0,buffer,-1,lpData,80);
857             }
858             return rc;
859         }
860     }
861     else
862         return 0;
863 }
864
865 /***********************************************************************
866  *              ImmGetCandidateListA (IMM32.@)
867  */
868 DWORD WINAPI ImmGetCandidateListA(
869   HIMC hIMC, DWORD dwIndex,
870   LPCANDIDATELIST lpCandList, DWORD dwBufLen)
871 {
872     InputContextData *data = hIMC;
873     LPCANDIDATEINFO candinfo;
874     LPCANDIDATELIST candlist;
875     DWORD ret = 0;
876
877     TRACE("%p, %d, %p, %d\n", hIMC, dwIndex, lpCandList, dwBufLen);
878
879     if (!data || !data->IMC.hCandInfo)
880        return 0;
881
882     candinfo = ImmLockIMCC(data->IMC.hCandInfo);
883     if ( dwIndex >= candinfo->dwCount ||
884          dwIndex >= (sizeof(candinfo->dwOffset) / sizeof(DWORD)) )
885         goto done;
886
887     candlist = (LPCANDIDATELIST)((LPBYTE)candinfo + candinfo->dwOffset[dwIndex]);
888     if ( !candlist->dwSize || !candlist->dwCount )
889         goto done;
890
891     if ( !is_himc_ime_unicode(data) )
892     {
893         ret = candlist->dwSize;
894         if ( lpCandList && dwBufLen >= ret )
895             memcpy(lpCandList, candlist, ret);
896     }
897     else
898         ret = convert_candidatelist_WtoA( candlist, lpCandList, dwBufLen);
899
900 done:
901     ImmUnlockIMCC(data->IMC.hCandInfo);
902     return ret;
903 }
904
905 /***********************************************************************
906  *              ImmGetCandidateListCountA (IMM32.@)
907  */
908 DWORD WINAPI ImmGetCandidateListCountA(
909   HIMC hIMC, LPDWORD lpdwListCount)
910 {
911     InputContextData *data = hIMC;
912     LPCANDIDATEINFO candinfo;
913     DWORD ret, count;
914
915     TRACE("%p, %p\n", hIMC, lpdwListCount);
916
917     if (!data || !lpdwListCount || !data->IMC.hCandInfo)
918        return 0;
919
920     candinfo = ImmLockIMCC(data->IMC.hCandInfo);
921
922     *lpdwListCount = count = candinfo->dwCount;
923
924     if ( !is_himc_ime_unicode(data) )
925         ret = candinfo->dwSize;
926     else
927     {
928         ret = sizeof(CANDIDATEINFO);
929         while ( count-- )
930             ret += ImmGetCandidateListA(hIMC, count, NULL, 0);
931     }
932
933     ImmUnlockIMCC(data->IMC.hCandInfo);
934     return ret;
935 }
936
937 /***********************************************************************
938  *              ImmGetCandidateListCountW (IMM32.@)
939  */
940 DWORD WINAPI ImmGetCandidateListCountW(
941   HIMC hIMC, LPDWORD lpdwListCount)
942 {
943     InputContextData *data = hIMC;
944     LPCANDIDATEINFO candinfo;
945     DWORD ret, count;
946
947     TRACE("%p, %p\n", hIMC, lpdwListCount);
948
949     if (!data || !lpdwListCount || !data->IMC.hCandInfo)
950        return 0;
951
952     candinfo = ImmLockIMCC(data->IMC.hCandInfo);
953
954     *lpdwListCount = count = candinfo->dwCount;
955
956     if ( is_himc_ime_unicode(data) )
957         ret = candinfo->dwSize;
958     else
959     {
960         ret = sizeof(CANDIDATEINFO);
961         while ( count-- )
962             ret += ImmGetCandidateListW(hIMC, count, NULL, 0);
963     }
964
965     ImmUnlockIMCC(data->IMC.hCandInfo);
966     return ret;
967 }
968
969 /***********************************************************************
970  *              ImmGetCandidateListW (IMM32.@)
971  */
972 DWORD WINAPI ImmGetCandidateListW(
973   HIMC hIMC, DWORD dwIndex,
974   LPCANDIDATELIST lpCandList, DWORD dwBufLen)
975 {
976     InputContextData *data = hIMC;
977     LPCANDIDATEINFO candinfo;
978     LPCANDIDATELIST candlist;
979     DWORD ret = 0;
980
981     TRACE("%p, %d, %p, %d\n", hIMC, dwIndex, lpCandList, dwBufLen);
982
983     if (!data || !data->IMC.hCandInfo)
984        return 0;
985
986     candinfo = ImmLockIMCC(data->IMC.hCandInfo);
987     if ( dwIndex >= candinfo->dwCount ||
988          dwIndex >= (sizeof(candinfo->dwOffset) / sizeof(DWORD)) )
989         goto done;
990
991     candlist = (LPCANDIDATELIST)((LPBYTE)candinfo + candinfo->dwOffset[dwIndex]);
992     if ( !candlist->dwSize || !candlist->dwCount )
993         goto done;
994
995     if ( is_himc_ime_unicode(data) )
996     {
997         ret = candlist->dwSize;
998         if ( lpCandList && dwBufLen >= ret )
999             memcpy(lpCandList, candlist, ret);
1000     }
1001     else
1002         ret = convert_candidatelist_AtoW( candlist, lpCandList, dwBufLen);
1003
1004 done:
1005     ImmUnlockIMCC(data->IMC.hCandInfo);
1006     return ret;
1007 }
1008
1009 /***********************************************************************
1010  *              ImmGetCandidateWindow (IMM32.@)
1011  */
1012 BOOL WINAPI ImmGetCandidateWindow(
1013   HIMC hIMC, DWORD dwIndex, LPCANDIDATEFORM lpCandidate)
1014 {
1015     InputContextData *data = hIMC;
1016
1017     TRACE("%p, %d, %p\n", hIMC, dwIndex, lpCandidate);
1018
1019     if (!data || !lpCandidate)
1020         return FALSE;
1021
1022     if ( dwIndex >= (sizeof(data->IMC.cfCandForm) / sizeof(CANDIDATEFORM)) )
1023         return FALSE;
1024
1025     *lpCandidate = data->IMC.cfCandForm[dwIndex];
1026
1027     return TRUE;
1028 }
1029
1030 /***********************************************************************
1031  *              ImmGetCompositionFontA (IMM32.@)
1032  */
1033 BOOL WINAPI ImmGetCompositionFontA(HIMC hIMC, LPLOGFONTA lplf)
1034 {
1035     LOGFONTW lfW;
1036     BOOL rc;
1037
1038     TRACE("(%p, %p):\n", hIMC, lplf);
1039
1040     rc = ImmGetCompositionFontW(hIMC,&lfW);
1041     if (!rc || !lplf)
1042         return FALSE;
1043
1044     memcpy(lplf,&lfW,sizeof(LOGFONTA));
1045     WideCharToMultiByte(CP_ACP, 0, lfW.lfFaceName, -1, lplf->lfFaceName,
1046                         LF_FACESIZE, NULL, NULL);
1047     return TRUE;
1048 }
1049
1050 /***********************************************************************
1051  *              ImmGetCompositionFontW (IMM32.@)
1052  */
1053 BOOL WINAPI ImmGetCompositionFontW(HIMC hIMC, LPLOGFONTW lplf)
1054 {
1055     InputContextData *data = hIMC;
1056
1057     TRACE("(%p, %p):\n", hIMC, lplf);
1058
1059     if (!data || !lplf)
1060         return FALSE;
1061
1062     *lplf = data->IMC.lfFont.W;
1063
1064     return TRUE;
1065 }
1066
1067
1068 /* Helpers for the GetCompositionString functions */
1069
1070 static INT CopyCompStringIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE target, INT tlen,
1071                                      BOOL unicode )
1072 {
1073     INT rc;
1074
1075     if (is_himc_ime_unicode(data) && !unicode)
1076         rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)source, slen, (LPSTR)target, tlen, NULL, NULL);
1077     else if (!is_himc_ime_unicode(data) && unicode)
1078         rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)source, slen, (LPWSTR)target, tlen) * sizeof(WCHAR);
1079     else
1080     {
1081         int dlen = (unicode)?sizeof(WCHAR):sizeof(CHAR);
1082         memcpy( target, source, min(slen,tlen)*dlen);
1083         rc = slen*dlen;
1084     }
1085
1086     return rc;
1087 }
1088
1089 static INT CopyCompAttrIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE ssource, INT sslen,
1090                                    LPBYTE target, INT tlen, BOOL unicode )
1091 {
1092     INT rc;
1093
1094     if (is_himc_ime_unicode(data) && !unicode)
1095     {
1096         rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource, sslen, NULL, 0, NULL, NULL);
1097         if (tlen)
1098         {
1099             const BYTE *src = source;
1100             LPBYTE dst = target;
1101             int i, j = 0, k = 0;
1102
1103             if (rc < tlen)
1104                 tlen = rc;
1105             for (i = 0; i < sslen; ++i)
1106             {
1107                 int len;
1108
1109                 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)ssource + i, 1,
1110                                           NULL, 0, NULL, NULL);
1111                 for (; len > 0; --len)
1112                 {
1113                     dst[j++] = src[k];
1114
1115                     if (j >= tlen)
1116                         goto end;
1117                 }
1118                 ++k;
1119             }
1120         end:
1121             rc = j;
1122         }
1123     }
1124     else if (!is_himc_ime_unicode(data) && unicode)
1125     {
1126         rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource, sslen, NULL, 0);
1127         if (tlen)
1128         {
1129             const BYTE *src = source;
1130             LPBYTE dst = target;
1131             int i, j = 0;
1132
1133             if (rc < tlen)
1134                 tlen = rc;
1135             for (i = 0; i < sslen; ++i)
1136             {
1137                 if (IsDBCSLeadByte(((LPSTR)ssource)[i]))
1138                     continue;
1139
1140                 dst[j++] = src[i];
1141
1142                 if (j >= tlen)
1143                     break;
1144             }
1145             rc = j;
1146         }
1147     }
1148     else
1149     {
1150         memcpy( target, source, min(slen,tlen));
1151         rc = slen;
1152     }
1153
1154     return rc;
1155 }
1156
1157 static INT CopyCompClauseIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE ssource, INT sslen,
1158                                      LPBYTE target, INT tlen, BOOL unicode )
1159 {
1160     INT rc;
1161
1162     if (is_himc_ime_unicode(data) && !unicode)
1163     {
1164         if (tlen)
1165         {
1166             int i;
1167
1168             if (slen < tlen)
1169                 tlen = slen;
1170             tlen /= sizeof (DWORD);
1171             for (i = 0; i < tlen; ++i)
1172             {
1173                 ((DWORD *)target)[i] = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource,
1174                                                           ((DWORD *)source)[i],
1175                                                           NULL, 0,
1176                                                           NULL, NULL);
1177             }
1178             rc = sizeof (DWORD) * i;
1179         }
1180         else
1181             rc = slen;
1182     }
1183     else if (!is_himc_ime_unicode(data) && unicode)
1184     {
1185         if (tlen)
1186         {
1187             int i;
1188
1189             if (slen < tlen)
1190                 tlen = slen;
1191             tlen /= sizeof (DWORD);
1192             for (i = 0; i < tlen; ++i)
1193             {
1194                 ((DWORD *)target)[i] = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource,
1195                                                           ((DWORD *)source)[i],
1196                                                           NULL, 0);
1197             }
1198             rc = sizeof (DWORD) * i;
1199         }
1200         else
1201             rc = slen;
1202     }
1203     else
1204     {
1205         memcpy( target, source, min(slen,tlen));
1206         rc = slen;
1207     }
1208
1209     return rc;
1210 }
1211
1212 static INT CopyCompOffsetIMEtoClient(InputContextData *data, DWORD offset, LPBYTE ssource, BOOL unicode)
1213 {
1214     int rc;
1215
1216     if (is_himc_ime_unicode(data) && !unicode)
1217     {
1218         rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource, offset, NULL, 0, NULL, NULL);
1219     }
1220     else if (!is_himc_ime_unicode(data) && unicode)
1221     {
1222         rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource, offset, NULL, 0);
1223     }
1224     else
1225         rc = offset;
1226
1227     return rc;
1228 }
1229
1230 static LONG ImmGetCompositionStringT( HIMC hIMC, DWORD dwIndex, LPVOID lpBuf,
1231                                       DWORD dwBufLen, BOOL unicode)
1232 {
1233     LONG rc = 0;
1234     InputContextData *data = hIMC;
1235     LPCOMPOSITIONSTRING compstr;
1236     LPBYTE compdata;
1237
1238     TRACE("(%p, 0x%x, %p, %d)\n", hIMC, dwIndex, lpBuf, dwBufLen);
1239
1240     if (!data)
1241        return FALSE;
1242
1243     if (!data->IMC.hCompStr)
1244        return FALSE;
1245
1246     compdata = ImmLockIMCC(data->IMC.hCompStr);
1247     compstr = (LPCOMPOSITIONSTRING)compdata;
1248
1249     switch (dwIndex)
1250     {
1251     case GCS_RESULTSTR:
1252         TRACE("GCS_RESULTSTR\n");
1253         rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwResultStrOffset, compstr->dwResultStrLen, lpBuf, dwBufLen, unicode);
1254         break;
1255     case GCS_COMPSTR:
1256         TRACE("GCS_COMPSTR\n");
1257         rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen, lpBuf, dwBufLen, unicode);
1258         break;
1259     case GCS_COMPATTR:
1260         TRACE("GCS_COMPATTR\n");
1261         rc = CopyCompAttrIMEtoClient(data, compdata + compstr->dwCompAttrOffset, compstr->dwCompAttrLen,
1262                                      compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen,
1263                                      lpBuf, dwBufLen, unicode);
1264         break;
1265     case GCS_COMPCLAUSE:
1266         TRACE("GCS_COMPCLAUSE\n");
1267         rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwCompClauseOffset,compstr->dwCompClauseLen,
1268                                        compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen,
1269                                        lpBuf, dwBufLen, unicode);
1270         break;
1271     case GCS_RESULTCLAUSE:
1272         TRACE("GCS_RESULTCLAUSE\n");
1273         rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwResultClauseOffset,compstr->dwResultClauseLen,
1274                                        compdata + compstr->dwResultStrOffset, compstr->dwResultStrLen,
1275                                        lpBuf, dwBufLen, unicode);
1276         break;
1277     case GCS_RESULTREADSTR:
1278         TRACE("GCS_RESULTREADSTR\n");
1279         rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwResultReadStrOffset, compstr->dwResultReadStrLen, lpBuf, dwBufLen, unicode);
1280         break;
1281     case GCS_RESULTREADCLAUSE:
1282         TRACE("GCS_RESULTREADCLAUSE\n");
1283         rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwResultReadClauseOffset,compstr->dwResultReadClauseLen,
1284                                        compdata + compstr->dwResultStrOffset, compstr->dwResultStrLen,
1285                                        lpBuf, dwBufLen, unicode);
1286         break;
1287     case GCS_COMPREADSTR:
1288         TRACE("GCS_COMPREADSTR\n");
1289         rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwCompReadStrOffset, compstr->dwCompReadStrLen, lpBuf, dwBufLen, unicode);
1290         break;
1291     case GCS_COMPREADATTR:
1292         TRACE("GCS_COMPREADATTR\n");
1293         rc = CopyCompAttrIMEtoClient(data, compdata + compstr->dwCompReadAttrOffset, compstr->dwCompReadAttrLen,
1294                                      compdata + compstr->dwCompReadStrOffset, compstr->dwCompReadStrLen,
1295                                      lpBuf, dwBufLen, unicode);
1296         break;
1297     case GCS_COMPREADCLAUSE:
1298         TRACE("GCS_COMPREADCLAUSE\n");
1299         rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwCompReadClauseOffset,compstr->dwCompReadClauseLen,
1300                                        compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen,
1301                                        lpBuf, dwBufLen, unicode);
1302         break;
1303     case GCS_CURSORPOS:
1304         TRACE("GCS_CURSORPOS\n");
1305         rc = CopyCompOffsetIMEtoClient(data, compstr->dwCursorPos, compdata + compstr->dwCompStrOffset, unicode);
1306         break;
1307     case GCS_DELTASTART:
1308         TRACE("GCS_DELTASTART\n");
1309         rc = CopyCompOffsetIMEtoClient(data, compstr->dwDeltaStart, compdata + compstr->dwCompStrOffset, unicode);
1310         break;
1311     default:
1312         FIXME("Unhandled index 0x%x\n",dwIndex);
1313         break;
1314     }
1315
1316     ImmUnlockIMCC(data->IMC.hCompStr);
1317
1318     return rc;
1319 }
1320
1321 /***********************************************************************
1322  *              ImmGetCompositionStringA (IMM32.@)
1323  */
1324 LONG WINAPI ImmGetCompositionStringA(
1325   HIMC hIMC, DWORD dwIndex, LPVOID lpBuf, DWORD dwBufLen)
1326 {
1327     return ImmGetCompositionStringT(hIMC, dwIndex, lpBuf, dwBufLen, FALSE);
1328 }
1329
1330
1331 /***********************************************************************
1332  *              ImmGetCompositionStringW (IMM32.@)
1333  */
1334 LONG WINAPI ImmGetCompositionStringW(
1335   HIMC hIMC, DWORD dwIndex,
1336   LPVOID lpBuf, DWORD dwBufLen)
1337 {
1338     return ImmGetCompositionStringT(hIMC, dwIndex, lpBuf, dwBufLen, TRUE);
1339 }
1340
1341 /***********************************************************************
1342  *              ImmGetCompositionWindow (IMM32.@)
1343  */
1344 BOOL WINAPI ImmGetCompositionWindow(HIMC hIMC, LPCOMPOSITIONFORM lpCompForm)
1345 {
1346     InputContextData *data = hIMC;
1347
1348     TRACE("(%p, %p)\n", hIMC, lpCompForm);
1349
1350     if (!data)
1351         return FALSE;
1352
1353     *lpCompForm = data->IMC.cfCompForm;
1354     return 1;
1355 }
1356
1357 /***********************************************************************
1358  *              ImmGetContext (IMM32.@)
1359  *
1360  */
1361 HIMC WINAPI ImmGetContext(HWND hWnd)
1362 {
1363     HIMC rc = NULL;
1364
1365     TRACE("%p\n", hWnd);
1366     if (!IMM_GetThreadData()->defaultContext)
1367         IMM_GetThreadData()->defaultContext = ImmCreateContext();
1368
1369     rc = GetPropW(hWnd,szwWineIMCProperty);
1370     if (rc == (HIMC)-1)
1371         rc = NULL;
1372     else if (rc == NULL)
1373         rc = IMM_GetThreadData()->defaultContext;
1374
1375     if (rc)
1376     {
1377         InputContextData *data = rc;
1378         data->IMC.hWnd = hWnd;
1379     }
1380     TRACE("returning %p\n", rc);
1381
1382     return rc;
1383 }
1384
1385 /***********************************************************************
1386  *              ImmGetConversionListA (IMM32.@)
1387  */
1388 DWORD WINAPI ImmGetConversionListA(
1389   HKL hKL, HIMC hIMC,
1390   LPCSTR pSrc, LPCANDIDATELIST lpDst,
1391   DWORD dwBufLen, UINT uFlag)
1392 {
1393     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1394     TRACE("(%p, %p, %s, %p, %d, %d):\n", hKL, hIMC, debugstr_a(pSrc), lpDst,
1395                 dwBufLen, uFlag);
1396     if (immHkl->hIME && immHkl->pImeConversionList)
1397     {
1398         if (!is_kbd_ime_unicode(immHkl))
1399             return immHkl->pImeConversionList(hIMC,(LPCWSTR)pSrc,lpDst,dwBufLen,uFlag);
1400         else
1401         {
1402             LPCANDIDATELIST lpwDst;
1403             DWORD ret = 0, len;
1404             LPWSTR pwSrc = strdupAtoW(pSrc);
1405
1406             len = immHkl->pImeConversionList(hIMC, pwSrc, NULL, 0, uFlag);
1407             lpwDst = HeapAlloc(GetProcessHeap(), 0, len);
1408             if ( lpwDst )
1409             {
1410                 immHkl->pImeConversionList(hIMC, pwSrc, lpwDst, len, uFlag);
1411                 ret = convert_candidatelist_WtoA( lpwDst, lpDst, dwBufLen);
1412                 HeapFree(GetProcessHeap(), 0, lpwDst);
1413             }
1414             HeapFree(GetProcessHeap(), 0, pwSrc);
1415
1416             return ret;
1417         }
1418     }
1419     else
1420         return 0;
1421 }
1422
1423 /***********************************************************************
1424  *              ImmGetConversionListW (IMM32.@)
1425  */
1426 DWORD WINAPI ImmGetConversionListW(
1427   HKL hKL, HIMC hIMC,
1428   LPCWSTR pSrc, LPCANDIDATELIST lpDst,
1429   DWORD dwBufLen, UINT uFlag)
1430 {
1431     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1432     TRACE("(%p, %p, %s, %p, %d, %d):\n", hKL, hIMC, debugstr_w(pSrc), lpDst,
1433                 dwBufLen, uFlag);
1434     if (immHkl->hIME && immHkl->pImeConversionList)
1435     {
1436         if (is_kbd_ime_unicode(immHkl))
1437             return immHkl->pImeConversionList(hIMC,pSrc,lpDst,dwBufLen,uFlag);
1438         else
1439         {
1440             LPCANDIDATELIST lpaDst;
1441             DWORD ret = 0, len;
1442             LPSTR paSrc = strdupWtoA(pSrc);
1443
1444             len = immHkl->pImeConversionList(hIMC, (LPCWSTR)paSrc, NULL, 0, uFlag);
1445             lpaDst = HeapAlloc(GetProcessHeap(), 0, len);
1446             if ( lpaDst )
1447             {
1448                 immHkl->pImeConversionList(hIMC, (LPCWSTR)paSrc, lpaDst, len, uFlag);
1449                 ret = convert_candidatelist_AtoW( lpaDst, lpDst, dwBufLen);
1450                 HeapFree(GetProcessHeap(), 0, lpaDst);
1451             }
1452             HeapFree(GetProcessHeap(), 0, paSrc);
1453
1454             return ret;
1455         }
1456     }
1457     else
1458         return 0;
1459 }
1460
1461 /***********************************************************************
1462  *              ImmGetConversionStatus (IMM32.@)
1463  */
1464 BOOL WINAPI ImmGetConversionStatus(
1465   HIMC hIMC, LPDWORD lpfdwConversion, LPDWORD lpfdwSentence)
1466 {
1467     InputContextData *data = hIMC;
1468
1469     TRACE("%p %p %p\n", hIMC, lpfdwConversion, lpfdwSentence);
1470
1471     if (!data)
1472         return FALSE;
1473
1474     if (lpfdwConversion)
1475         *lpfdwConversion = data->IMC.fdwConversion;
1476     if (lpfdwSentence)
1477         *lpfdwSentence = data->IMC.fdwSentence;
1478
1479     return TRUE;
1480 }
1481
1482 /***********************************************************************
1483  *              ImmGetDefaultIMEWnd (IMM32.@)
1484  */
1485 HWND WINAPI ImmGetDefaultIMEWnd(HWND hWnd)
1486 {
1487     TRACE("Default is %p\n",IMM_GetThreadData()->hwndDefault);
1488     return IMM_GetThreadData()->hwndDefault;
1489 }
1490
1491 /***********************************************************************
1492  *              ImmGetDescriptionA (IMM32.@)
1493  */
1494 UINT WINAPI ImmGetDescriptionA(
1495   HKL hKL, LPSTR lpszDescription, UINT uBufLen)
1496 {
1497   WCHAR *buf;
1498   DWORD len;
1499
1500   TRACE("%p %p %d\n", hKL, lpszDescription, uBufLen);
1501
1502   /* find out how many characters in the unicode buffer */
1503   len = ImmGetDescriptionW( hKL, NULL, 0 );
1504
1505   /* allocate a buffer of that size */
1506   buf = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof (WCHAR) );
1507   if( !buf )
1508   return 0;
1509
1510   /* fetch the unicode buffer */
1511   len = ImmGetDescriptionW( hKL, buf, len + 1 );
1512
1513   /* convert it back to ASCII */
1514   len = WideCharToMultiByte( CP_ACP, 0, buf, len + 1,
1515                              lpszDescription, uBufLen, NULL, NULL );
1516
1517   HeapFree( GetProcessHeap(), 0, buf );
1518
1519   return len;
1520 }
1521
1522 /***********************************************************************
1523  *              ImmGetDescriptionW (IMM32.@)
1524  */
1525 UINT WINAPI ImmGetDescriptionW(HKL hKL, LPWSTR lpszDescription, UINT uBufLen)
1526 {
1527   static const WCHAR name[] = { 'W','i','n','e',' ','X','I','M',0 };
1528
1529   FIXME("(%p, %p, %d): semi stub\n", hKL, lpszDescription, uBufLen);
1530
1531   if (!uBufLen) return lstrlenW( name );
1532   lstrcpynW( lpszDescription, name, uBufLen );
1533   return lstrlenW( lpszDescription );
1534 }
1535
1536 /***********************************************************************
1537  *              ImmGetGuideLineA (IMM32.@)
1538  */
1539 DWORD WINAPI ImmGetGuideLineA(
1540   HIMC hIMC, DWORD dwIndex, LPSTR lpBuf, DWORD dwBufLen)
1541 {
1542   FIXME("(%p, %d, %s, %d): stub\n",
1543     hIMC, dwIndex, debugstr_a(lpBuf), dwBufLen
1544   );
1545   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1546   return 0;
1547 }
1548
1549 /***********************************************************************
1550  *              ImmGetGuideLineW (IMM32.@)
1551  */
1552 DWORD WINAPI ImmGetGuideLineW(HIMC hIMC, DWORD dwIndex, LPWSTR lpBuf, DWORD dwBufLen)
1553 {
1554   FIXME("(%p, %d, %s, %d): stub\n",
1555     hIMC, dwIndex, debugstr_w(lpBuf), dwBufLen
1556   );
1557   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1558   return 0;
1559 }
1560
1561 /***********************************************************************
1562  *              ImmGetIMEFileNameA (IMM32.@)
1563  */
1564 UINT WINAPI ImmGetIMEFileNameA( HKL hKL, LPSTR lpszFileName, UINT uBufLen)
1565 {
1566     LPWSTR bufW = NULL;
1567     UINT wBufLen = uBufLen;
1568     UINT rc;
1569
1570     if (uBufLen && lpszFileName)
1571         bufW = HeapAlloc(GetProcessHeap(),0,uBufLen * sizeof(WCHAR));
1572     else /* We need this to get the number of byte required */
1573     {
1574         bufW = HeapAlloc(GetProcessHeap(),0,MAX_PATH * sizeof(WCHAR));
1575         wBufLen = MAX_PATH;
1576     }
1577
1578     rc = ImmGetIMEFileNameW(hKL,bufW,wBufLen);
1579
1580     if (rc > 0)
1581     {
1582         if (uBufLen && lpszFileName)
1583             rc = WideCharToMultiByte(CP_ACP, 0, bufW, -1, lpszFileName,
1584                                  uBufLen, NULL, NULL);
1585         else /* get the length */
1586             rc = WideCharToMultiByte(CP_ACP, 0, bufW, -1, NULL, 0, NULL,
1587                                      NULL);
1588     }
1589
1590     HeapFree(GetProcessHeap(),0,bufW);
1591     return rc;
1592 }
1593
1594 /***********************************************************************
1595  *              ImmGetIMEFileNameW (IMM32.@)
1596  */
1597 UINT WINAPI ImmGetIMEFileNameW(HKL hKL, LPWSTR lpszFileName, UINT uBufLen)
1598 {
1599     HKEY hkey;
1600     DWORD length;
1601     DWORD rc;
1602     WCHAR regKey[sizeof(szImeRegFmt)/sizeof(WCHAR)+8];
1603
1604     wsprintfW( regKey, szImeRegFmt, (ULONG_PTR)hKL );
1605     rc = RegOpenKeyW( HKEY_LOCAL_MACHINE, regKey, &hkey);
1606     if (rc != ERROR_SUCCESS)
1607     {
1608         SetLastError(rc);
1609         return 0;
1610     }
1611
1612     length = 0;
1613     rc = RegGetValueW(hkey, NULL, szImeFileW, RRF_RT_REG_SZ, NULL, NULL, &length);
1614
1615     if (rc != ERROR_SUCCESS)
1616     {
1617         RegCloseKey(hkey);
1618         SetLastError(rc);
1619         return 0;
1620     }
1621     if (length > uBufLen * sizeof(WCHAR) || !lpszFileName)
1622     {
1623         RegCloseKey(hkey);
1624         if (lpszFileName)
1625         {
1626             SetLastError(ERROR_INSUFFICIENT_BUFFER);
1627             return 0;
1628         }
1629         else
1630             return length / sizeof(WCHAR);
1631     }
1632
1633     RegGetValueW(hkey, NULL, szImeFileW, RRF_RT_REG_SZ, NULL, lpszFileName, &length);
1634
1635     RegCloseKey(hkey);
1636
1637     return length / sizeof(WCHAR);
1638 }
1639
1640 /***********************************************************************
1641  *              ImmGetOpenStatus (IMM32.@)
1642  */
1643 BOOL WINAPI ImmGetOpenStatus(HIMC hIMC)
1644 {
1645   InputContextData *data = hIMC;
1646   static int i;
1647
1648     if (!data)
1649         return FALSE;
1650
1651     TRACE("(%p): semi-stub\n", hIMC);
1652
1653     if (!i++)
1654       FIXME("(%p): semi-stub\n", hIMC);
1655
1656   return data->IMC.fOpen;
1657 }
1658
1659 /***********************************************************************
1660  *              ImmGetProperty (IMM32.@)
1661  */
1662 DWORD WINAPI ImmGetProperty(HKL hKL, DWORD fdwIndex)
1663 {
1664     DWORD rc = 0;
1665     ImmHkl *kbd;
1666
1667     TRACE("(%p, %d)\n", hKL, fdwIndex);
1668     kbd = IMM_GetImmHkl(hKL);
1669
1670     if (kbd && kbd->hIME)
1671     {
1672         switch (fdwIndex)
1673         {
1674             case IGP_PROPERTY: rc = kbd->imeInfo.fdwProperty; break;
1675             case IGP_CONVERSION: rc = kbd->imeInfo.fdwConversionCaps; break;
1676             case IGP_SENTENCE: rc = kbd->imeInfo.fdwSentenceCaps; break;
1677             case IGP_SETCOMPSTR: rc = kbd->imeInfo.fdwSCSCaps; break;
1678             case IGP_SELECT: rc = kbd->imeInfo.fdwSelectCaps; break;
1679             case IGP_GETIMEVERSION: rc = IMEVER_0400; break;
1680             case IGP_UI: rc = 0; break;
1681             default: rc = 0;
1682         }
1683     }
1684     return rc;
1685 }
1686
1687 /***********************************************************************
1688  *              ImmGetRegisterWordStyleA (IMM32.@)
1689  */
1690 UINT WINAPI ImmGetRegisterWordStyleA(
1691   HKL hKL, UINT nItem, LPSTYLEBUFA lpStyleBuf)
1692 {
1693     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1694     TRACE("(%p, %d, %p):\n", hKL, nItem, lpStyleBuf);
1695     if (immHkl->hIME && immHkl->pImeGetRegisterWordStyle)
1696     {
1697         if (!is_kbd_ime_unicode(immHkl))
1698             return immHkl->pImeGetRegisterWordStyle(nItem,(LPSTYLEBUFW)lpStyleBuf);
1699         else
1700         {
1701             STYLEBUFW sbw;
1702             UINT rc;
1703
1704             rc = immHkl->pImeGetRegisterWordStyle(nItem,&sbw);
1705             WideCharToMultiByte(CP_ACP, 0, sbw.szDescription, -1,
1706                 lpStyleBuf->szDescription, 32, NULL, NULL);
1707             lpStyleBuf->dwStyle = sbw.dwStyle;
1708             return rc;
1709         }
1710     }
1711     else
1712         return 0;
1713 }
1714
1715 /***********************************************************************
1716  *              ImmGetRegisterWordStyleW (IMM32.@)
1717  */
1718 UINT WINAPI ImmGetRegisterWordStyleW(
1719   HKL hKL, UINT nItem, LPSTYLEBUFW lpStyleBuf)
1720 {
1721     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1722     TRACE("(%p, %d, %p):\n", hKL, nItem, lpStyleBuf);
1723     if (immHkl->hIME && immHkl->pImeGetRegisterWordStyle)
1724     {
1725         if (is_kbd_ime_unicode(immHkl))
1726             return immHkl->pImeGetRegisterWordStyle(nItem,lpStyleBuf);
1727         else
1728         {
1729             STYLEBUFA sba;
1730             UINT rc;
1731
1732             rc = immHkl->pImeGetRegisterWordStyle(nItem,(LPSTYLEBUFW)&sba);
1733             MultiByteToWideChar(CP_ACP, 0, sba.szDescription, -1,
1734                 lpStyleBuf->szDescription, 32);
1735             lpStyleBuf->dwStyle = sba.dwStyle;
1736             return rc;
1737         }
1738     }
1739     else
1740         return 0;
1741 }
1742
1743 /***********************************************************************
1744  *              ImmGetStatusWindowPos (IMM32.@)
1745  */
1746 BOOL WINAPI ImmGetStatusWindowPos(HIMC hIMC, LPPOINT lpptPos)
1747 {
1748     InputContextData *data = hIMC;
1749
1750     TRACE("(%p, %p)\n", hIMC, lpptPos);
1751
1752     if (!data || !lpptPos)
1753         return FALSE;
1754
1755     *lpptPos = data->IMC.ptStatusWndPos;
1756
1757     return TRUE;
1758 }
1759
1760 /***********************************************************************
1761  *              ImmGetVirtualKey (IMM32.@)
1762  */
1763 UINT WINAPI ImmGetVirtualKey(HWND hWnd)
1764 {
1765   OSVERSIONINFOA version;
1766   InputContextData *data = ImmGetContext( hWnd );
1767   TRACE("%p\n", hWnd);
1768
1769   if ( data )
1770       return data->lastVK;
1771
1772   version.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
1773   GetVersionExA( &version );
1774   switch(version.dwPlatformId)
1775   {
1776   case VER_PLATFORM_WIN32_WINDOWS:
1777       return VK_PROCESSKEY;
1778   case VER_PLATFORM_WIN32_NT:
1779       return 0;
1780   default:
1781       FIXME("%d not supported\n",version.dwPlatformId);
1782       return VK_PROCESSKEY;
1783   }
1784 }
1785
1786 /***********************************************************************
1787  *              ImmInstallIMEA (IMM32.@)
1788  */
1789 HKL WINAPI ImmInstallIMEA(
1790   LPCSTR lpszIMEFileName, LPCSTR lpszLayoutText)
1791 {
1792     LPWSTR lpszwIMEFileName;
1793     LPWSTR lpszwLayoutText;
1794     HKL hkl;
1795
1796     TRACE ("(%s, %s)\n", debugstr_a(lpszIMEFileName),
1797                          debugstr_a(lpszLayoutText));
1798
1799     lpszwIMEFileName = strdupAtoW(lpszIMEFileName);
1800     lpszwLayoutText = strdupAtoW(lpszLayoutText);
1801
1802     hkl = ImmInstallIMEW(lpszwIMEFileName, lpszwLayoutText);
1803
1804     HeapFree(GetProcessHeap(),0,lpszwIMEFileName);
1805     HeapFree(GetProcessHeap(),0,lpszwLayoutText);
1806     return hkl;
1807 }
1808
1809 /***********************************************************************
1810  *              ImmInstallIMEW (IMM32.@)
1811  */
1812 HKL WINAPI ImmInstallIMEW(
1813   LPCWSTR lpszIMEFileName, LPCWSTR lpszLayoutText)
1814 {
1815     INT lcid = GetUserDefaultLCID();
1816     INT count;
1817     HKL hkl;
1818     DWORD rc;
1819     HKEY hkey;
1820     WCHAR regKey[sizeof(szImeRegFmt)/sizeof(WCHAR)+8];
1821
1822     TRACE ("(%s, %s):\n", debugstr_w(lpszIMEFileName),
1823                           debugstr_w(lpszLayoutText));
1824
1825     /* Start with 2.  e001 will be blank and so default to the wine internal IME */
1826     count = 2;
1827
1828     while (count < 0xfff)
1829     {
1830         DWORD disposition = 0;
1831
1832         hkl = (HKL)MAKELPARAM( lcid, 0xe000 | count );
1833         wsprintfW( regKey, szImeRegFmt, (ULONG_PTR)hkl);
1834
1835         rc = RegCreateKeyExW(HKEY_LOCAL_MACHINE, regKey, 0, NULL, 0, KEY_WRITE, NULL, &hkey, &disposition);
1836         if (rc == ERROR_SUCCESS && disposition == REG_CREATED_NEW_KEY)
1837             break;
1838         else if (rc == ERROR_SUCCESS)
1839             RegCloseKey(hkey);
1840
1841         count++;
1842     }
1843
1844     if (count == 0xfff)
1845     {
1846         WARN("Unable to find slot to install IME\n");
1847         return 0;
1848     }
1849
1850     if (rc == ERROR_SUCCESS)
1851     {
1852         rc = RegSetValueExW(hkey, szImeFileW, 0, REG_SZ, (const BYTE*)lpszIMEFileName,
1853                             (lstrlenW(lpszIMEFileName) + 1) * sizeof(WCHAR));
1854         if (rc == ERROR_SUCCESS)
1855             rc = RegSetValueExW(hkey, szLayoutTextW, 0, REG_SZ, (const BYTE*)lpszLayoutText,
1856                                 (lstrlenW(lpszLayoutText) + 1) * sizeof(WCHAR));
1857         RegCloseKey(hkey);
1858         return hkl;
1859     }
1860     else
1861     {
1862         WARN("Unable to set IME registry values\n");
1863         return 0;
1864     }
1865 }
1866
1867 /***********************************************************************
1868  *              ImmIsIME (IMM32.@)
1869  */
1870 BOOL WINAPI ImmIsIME(HKL hKL)
1871 {
1872     ImmHkl *ptr;
1873     TRACE("(%p):\n", hKL);
1874     ptr = IMM_GetImmHkl(hKL);
1875     return (ptr && ptr->hIME);
1876 }
1877
1878 /***********************************************************************
1879  *              ImmIsUIMessageA (IMM32.@)
1880  */
1881 BOOL WINAPI ImmIsUIMessageA(
1882   HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam)
1883 {
1884     BOOL rc = FALSE;
1885
1886     TRACE("(%p, %x, %ld, %ld)\n", hWndIME, msg, wParam, lParam);
1887     if ((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
1888         (msg >= WM_IME_SETCONTEXT && msg <= WM_IME_KEYUP) ||
1889         (msg == WM_MSIME_SERVICE) ||
1890         (msg == WM_MSIME_RECONVERTOPTIONS) ||
1891         (msg == WM_MSIME_MOUSE) ||
1892         (msg == WM_MSIME_RECONVERTREQUEST) ||
1893         (msg == WM_MSIME_RECONVERT) ||
1894         (msg == WM_MSIME_QUERYPOSITION) ||
1895         (msg == WM_MSIME_DOCUMENTFEED))
1896
1897     {
1898         if (!IMM_GetThreadData()->hwndDefault)
1899             ImmGetDefaultIMEWnd(NULL);
1900
1901         if (hWndIME == NULL)
1902             PostMessageA(IMM_GetThreadData()->hwndDefault, msg, wParam, lParam);
1903
1904         rc = TRUE;
1905     }
1906     return rc;
1907 }
1908
1909 /***********************************************************************
1910  *              ImmIsUIMessageW (IMM32.@)
1911  */
1912 BOOL WINAPI ImmIsUIMessageW(
1913   HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam)
1914 {
1915     BOOL rc = FALSE;
1916     TRACE("(%p, %d, %ld, %ld):\n", hWndIME, msg, wParam, lParam);
1917     if ((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
1918         (msg >= WM_IME_SETCONTEXT && msg <= WM_IME_KEYUP) ||
1919         (msg == WM_MSIME_SERVICE) ||
1920         (msg == WM_MSIME_RECONVERTOPTIONS) ||
1921         (msg == WM_MSIME_MOUSE) ||
1922         (msg == WM_MSIME_RECONVERTREQUEST) ||
1923         (msg == WM_MSIME_RECONVERT) ||
1924         (msg == WM_MSIME_QUERYPOSITION) ||
1925         (msg == WM_MSIME_DOCUMENTFEED))
1926         rc = TRUE;
1927     return rc;
1928 }
1929
1930 /***********************************************************************
1931  *              ImmNotifyIME (IMM32.@)
1932  */
1933 BOOL WINAPI ImmNotifyIME(
1934   HIMC hIMC, DWORD dwAction, DWORD dwIndex, DWORD dwValue)
1935 {
1936     InputContextData *data = hIMC;
1937
1938     TRACE("(%p, %d, %d, %d)\n",
1939         hIMC, dwAction, dwIndex, dwValue);
1940
1941     if (!data || ! data->immKbd->pNotifyIME)
1942         return FALSE;
1943
1944     return data->immKbd->pNotifyIME(hIMC,dwAction,dwIndex,dwValue);
1945 }
1946
1947 /***********************************************************************
1948  *              ImmRegisterWordA (IMM32.@)
1949  */
1950 BOOL WINAPI ImmRegisterWordA(
1951   HKL hKL, LPCSTR lpszReading, DWORD dwStyle, LPCSTR lpszRegister)
1952 {
1953     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1954     TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_a(lpszReading), dwStyle,
1955                     debugstr_a(lpszRegister));
1956     if (immHkl->hIME && immHkl->pImeRegisterWord)
1957     {
1958         if (!is_kbd_ime_unicode(immHkl))
1959             return immHkl->pImeRegisterWord((LPCWSTR)lpszReading,dwStyle,
1960                                             (LPCWSTR)lpszRegister);
1961         else
1962         {
1963             LPWSTR lpszwReading = strdupAtoW(lpszReading);
1964             LPWSTR lpszwRegister = strdupAtoW(lpszRegister);
1965             BOOL rc;
1966
1967             rc = immHkl->pImeRegisterWord(lpszwReading,dwStyle,lpszwRegister);
1968             HeapFree(GetProcessHeap(),0,lpszwReading);
1969             HeapFree(GetProcessHeap(),0,lpszwRegister);
1970             return rc;
1971         }
1972     }
1973     else
1974         return FALSE;
1975 }
1976
1977 /***********************************************************************
1978  *              ImmRegisterWordW (IMM32.@)
1979  */
1980 BOOL WINAPI ImmRegisterWordW(
1981   HKL hKL, LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszRegister)
1982 {
1983     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
1984     TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_w(lpszReading), dwStyle,
1985                     debugstr_w(lpszRegister));
1986     if (immHkl->hIME && immHkl->pImeRegisterWord)
1987     {
1988         if (is_kbd_ime_unicode(immHkl))
1989             return immHkl->pImeRegisterWord(lpszReading,dwStyle,lpszRegister);
1990         else
1991         {
1992             LPSTR lpszaReading = strdupWtoA(lpszReading);
1993             LPSTR lpszaRegister = strdupWtoA(lpszRegister);
1994             BOOL rc;
1995
1996             rc = immHkl->pImeRegisterWord((LPCWSTR)lpszaReading,dwStyle,
1997                                           (LPCWSTR)lpszaRegister);
1998             HeapFree(GetProcessHeap(),0,lpszaReading);
1999             HeapFree(GetProcessHeap(),0,lpszaRegister);
2000             return rc;
2001         }
2002     }
2003     else
2004         return FALSE;
2005 }
2006
2007 /***********************************************************************
2008  *              ImmReleaseContext (IMM32.@)
2009  */
2010 BOOL WINAPI ImmReleaseContext(HWND hWnd, HIMC hIMC)
2011 {
2012   static int shown = 0;
2013
2014   if (!shown) {
2015      FIXME("(%p, %p): stub\n", hWnd, hIMC);
2016      shown = 1;
2017   }
2018   return TRUE;
2019 }
2020
2021 /***********************************************************************
2022 *              ImmRequestMessageA(IMM32.@)
2023 */
2024 LRESULT WINAPI ImmRequestMessageA(HIMC hIMC, WPARAM wParam, LPARAM lParam)
2025 {
2026     InputContextData *data = hIMC;
2027
2028     TRACE("%p %ld %ld\n", hIMC, wParam, wParam);
2029
2030     if (data && IsWindow(data->IMC.hWnd))
2031         return SendMessageA(data->IMC.hWnd, WM_IME_REQUEST, wParam, lParam);
2032
2033      return 0;
2034 }
2035
2036 /***********************************************************************
2037 *              ImmRequestMessageW(IMM32.@)
2038 */
2039 LRESULT WINAPI ImmRequestMessageW(HIMC hIMC, WPARAM wParam, LPARAM lParam)
2040 {
2041     InputContextData *data = hIMC;
2042
2043     TRACE("%p %ld %ld\n", hIMC, wParam, wParam);
2044
2045     if (data && IsWindow(data->IMC.hWnd))
2046         return SendMessageW(data->IMC.hWnd, WM_IME_REQUEST, wParam, lParam);
2047
2048      return 0;
2049 }
2050
2051 /***********************************************************************
2052  *              ImmSetCandidateWindow (IMM32.@)
2053  */
2054 BOOL WINAPI ImmSetCandidateWindow(
2055   HIMC hIMC, LPCANDIDATEFORM lpCandidate)
2056 {
2057     InputContextData *data = hIMC;
2058
2059     TRACE("(%p, %p)\n", hIMC, lpCandidate);
2060
2061     if (!data || !lpCandidate)
2062         return FALSE;
2063
2064     TRACE("\t%x, %x, (%i,%i), (%i,%i - %i,%i)\n",
2065             lpCandidate->dwIndex, lpCandidate->dwStyle,
2066             lpCandidate->ptCurrentPos.x, lpCandidate->ptCurrentPos.y,
2067             lpCandidate->rcArea.top, lpCandidate->rcArea.left,
2068             lpCandidate->rcArea.bottom, lpCandidate->rcArea.right);
2069
2070     if ( lpCandidate->dwIndex >= (sizeof(data->IMC.cfCandForm) / sizeof(CANDIDATEFORM)) )
2071         return FALSE;
2072
2073     data->IMC.cfCandForm[lpCandidate->dwIndex] = *lpCandidate;
2074     ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCANDIDATEPOS);
2075     ImmInternalSendIMENotify(data, IMN_SETCANDIDATEPOS, 1 << lpCandidate->dwIndex);
2076
2077     return TRUE;
2078 }
2079
2080 /***********************************************************************
2081  *              ImmSetCompositionFontA (IMM32.@)
2082  */
2083 BOOL WINAPI ImmSetCompositionFontA(HIMC hIMC, LPLOGFONTA lplf)
2084 {
2085     InputContextData *data = hIMC;
2086     TRACE("(%p, %p)\n", hIMC, lplf);
2087
2088     if (!data || !lplf)
2089         return FALSE;
2090
2091     memcpy(&data->IMC.lfFont.W,lplf,sizeof(LOGFONTA));
2092     MultiByteToWideChar(CP_ACP, 0, lplf->lfFaceName, -1, data->IMC.lfFont.W.lfFaceName,
2093                         LF_FACESIZE);
2094     ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCOMPOSITIONFONT);
2095     ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONFONT, 0);
2096
2097     return TRUE;
2098 }
2099
2100 /***********************************************************************
2101  *              ImmSetCompositionFontW (IMM32.@)
2102  */
2103 BOOL WINAPI ImmSetCompositionFontW(HIMC hIMC, LPLOGFONTW lplf)
2104 {
2105     InputContextData *data = hIMC;
2106     TRACE("(%p, %p)\n", hIMC, lplf);
2107
2108     if (!data || !lplf)
2109         return FALSE;
2110
2111     data->IMC.lfFont.W = *lplf;
2112     ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCOMPOSITIONFONT);
2113     ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONFONT, 0);
2114
2115     return TRUE;
2116 }
2117
2118 /***********************************************************************
2119  *              ImmSetCompositionStringA (IMM32.@)
2120  */
2121 BOOL WINAPI ImmSetCompositionStringA(
2122   HIMC hIMC, DWORD dwIndex,
2123   LPCVOID lpComp, DWORD dwCompLen,
2124   LPCVOID lpRead, DWORD dwReadLen)
2125 {
2126     DWORD comp_len;
2127     DWORD read_len;
2128     WCHAR *CompBuffer = NULL;
2129     WCHAR *ReadBuffer = NULL;
2130     BOOL rc;
2131     InputContextData *data = hIMC;
2132
2133     TRACE("(%p, %d, %p, %d, %p, %d):\n",
2134             hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
2135
2136     if (!data)
2137         return FALSE;
2138
2139     if (!(dwIndex == SCS_SETSTR ||
2140           dwIndex == SCS_CHANGEATTR ||
2141           dwIndex == SCS_CHANGECLAUSE ||
2142           dwIndex == SCS_SETRECONVERTSTRING ||
2143           dwIndex == SCS_QUERYRECONVERTSTRING))
2144         return FALSE;
2145
2146     if (!is_himc_ime_unicode(data))
2147         return data->immKbd->pImeSetCompositionString(hIMC, dwIndex, lpComp,
2148                         dwCompLen, lpRead, dwReadLen);
2149
2150     comp_len = MultiByteToWideChar(CP_ACP, 0, lpComp, dwCompLen, NULL, 0);
2151     if (comp_len)
2152     {
2153         CompBuffer = HeapAlloc(GetProcessHeap(),0,comp_len * sizeof(WCHAR));
2154         MultiByteToWideChar(CP_ACP, 0, lpComp, dwCompLen, CompBuffer, comp_len);
2155     }
2156
2157     read_len = MultiByteToWideChar(CP_ACP, 0, lpRead, dwReadLen, NULL, 0);
2158     if (read_len)
2159     {
2160         ReadBuffer = HeapAlloc(GetProcessHeap(),0,read_len * sizeof(WCHAR));
2161         MultiByteToWideChar(CP_ACP, 0, lpRead, dwReadLen, ReadBuffer, read_len);
2162     }
2163
2164     rc =  ImmSetCompositionStringW(hIMC, dwIndex, CompBuffer, comp_len,
2165                                    ReadBuffer, read_len);
2166
2167     HeapFree(GetProcessHeap(), 0, CompBuffer);
2168     HeapFree(GetProcessHeap(), 0, ReadBuffer);
2169
2170     return rc;
2171 }
2172
2173 /***********************************************************************
2174  *              ImmSetCompositionStringW (IMM32.@)
2175  */
2176 BOOL WINAPI ImmSetCompositionStringW(
2177         HIMC hIMC, DWORD dwIndex,
2178         LPCVOID lpComp, DWORD dwCompLen,
2179         LPCVOID lpRead, DWORD dwReadLen)
2180 {
2181     DWORD comp_len;
2182     DWORD read_len;
2183     CHAR *CompBuffer = NULL;
2184     CHAR *ReadBuffer = NULL;
2185     BOOL rc;
2186     InputContextData *data = hIMC;
2187
2188     TRACE("(%p, %d, %p, %d, %p, %d):\n",
2189             hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
2190
2191     if (!data)
2192         return FALSE;
2193
2194     if (!(dwIndex == SCS_SETSTR ||
2195           dwIndex == SCS_CHANGEATTR ||
2196           dwIndex == SCS_CHANGECLAUSE ||
2197           dwIndex == SCS_SETRECONVERTSTRING ||
2198           dwIndex == SCS_QUERYRECONVERTSTRING))
2199         return FALSE;
2200
2201     if (is_himc_ime_unicode(data))
2202         return data->immKbd->pImeSetCompositionString(hIMC, dwIndex, lpComp,
2203                         dwCompLen, lpRead, dwReadLen);
2204
2205     comp_len = WideCharToMultiByte(CP_ACP, 0, lpComp, dwCompLen, NULL, 0, NULL,
2206                                    NULL);
2207     if (comp_len)
2208     {
2209         CompBuffer = HeapAlloc(GetProcessHeap(),0,comp_len);
2210         WideCharToMultiByte(CP_ACP, 0, lpComp, dwCompLen, CompBuffer, comp_len,
2211                             NULL, NULL);
2212     }
2213
2214     read_len = WideCharToMultiByte(CP_ACP, 0, lpRead, dwReadLen, NULL, 0, NULL,
2215                                    NULL);
2216     if (read_len)
2217     {
2218         ReadBuffer = HeapAlloc(GetProcessHeap(),0,read_len);
2219         WideCharToMultiByte(CP_ACP, 0, lpRead, dwReadLen, ReadBuffer, read_len,
2220                             NULL, NULL);
2221     }
2222
2223     rc =  ImmSetCompositionStringA(hIMC, dwIndex, CompBuffer, comp_len,
2224                                    ReadBuffer, read_len);
2225
2226     HeapFree(GetProcessHeap(), 0, CompBuffer);
2227     HeapFree(GetProcessHeap(), 0, ReadBuffer);
2228
2229     return rc;
2230 }
2231
2232 /***********************************************************************
2233  *              ImmSetCompositionWindow (IMM32.@)
2234  */
2235 BOOL WINAPI ImmSetCompositionWindow(
2236   HIMC hIMC, LPCOMPOSITIONFORM lpCompForm)
2237 {
2238     BOOL reshow = FALSE;
2239     InputContextData *data = hIMC;
2240
2241     TRACE("(%p, %p)\n", hIMC, lpCompForm);
2242     TRACE("\t%x, (%i,%i), (%i,%i - %i,%i)\n",lpCompForm->dwStyle,
2243           lpCompForm->ptCurrentPos.x, lpCompForm->ptCurrentPos.y, lpCompForm->rcArea.top,
2244           lpCompForm->rcArea.left, lpCompForm->rcArea.bottom, lpCompForm->rcArea.right);
2245
2246     if (!data)
2247         return FALSE;
2248
2249     data->IMC.cfCompForm = *lpCompForm;
2250
2251     if (IsWindowVisible(IMM_GetThreadData()->hwndDefault))
2252     {
2253         reshow = TRUE;
2254         ShowWindow(IMM_GetThreadData()->hwndDefault,SW_HIDE);
2255     }
2256
2257     /* FIXME: this is a partial stub */
2258
2259     if (reshow)
2260         ShowWindow(IMM_GetThreadData()->hwndDefault,SW_SHOWNOACTIVATE);
2261
2262     ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONWINDOW, 0);
2263     return TRUE;
2264 }
2265
2266 /***********************************************************************
2267  *              ImmSetConversionStatus (IMM32.@)
2268  */
2269 BOOL WINAPI ImmSetConversionStatus(
2270   HIMC hIMC, DWORD fdwConversion, DWORD fdwSentence)
2271 {
2272     DWORD oldConversion, oldSentence;
2273     InputContextData *data = hIMC;
2274
2275     TRACE("%p %d %d\n", hIMC, fdwConversion, fdwSentence);
2276
2277     if (!data)
2278         return FALSE;
2279
2280     if ( fdwConversion != data->IMC.fdwConversion )
2281     {
2282         oldConversion = data->IMC.fdwConversion;
2283         data->IMC.fdwConversion = fdwConversion;
2284         ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, oldConversion, IMC_SETCONVERSIONMODE);
2285         ImmInternalSendIMENotify(data, IMN_SETCONVERSIONMODE, 0);
2286     }
2287     if ( fdwSentence != data->IMC.fdwSentence )
2288     {
2289         oldSentence = data->IMC.fdwSentence;
2290         data->IMC.fdwSentence = fdwSentence;
2291         ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, oldSentence, IMC_SETSENTENCEMODE);
2292         ImmInternalSendIMENotify(data, IMN_SETSENTENCEMODE, 0);
2293     }
2294
2295     return TRUE;
2296 }
2297
2298 /***********************************************************************
2299  *              ImmSetOpenStatus (IMM32.@)
2300  */
2301 BOOL WINAPI ImmSetOpenStatus(HIMC hIMC, BOOL fOpen)
2302 {
2303     InputContextData *data = hIMC;
2304
2305     TRACE("%p %d\n", hIMC, fOpen);
2306
2307     if (!data)
2308         return FALSE;
2309
2310     if (data->imeWnd == NULL)
2311     {
2312         /* create the ime window */
2313         data->imeWnd = CreateWindowExW( WS_EX_TOOLWINDOW,
2314                     data->immKbd->imeClassName, NULL, WS_POPUP, 0, 0, 1, 1, 0,
2315                     0, data->immKbd->hIME, 0);
2316         SetWindowLongPtrW(data->imeWnd, IMMGWL_IMC, (LONG_PTR)data);
2317         IMM_GetThreadData()->hwndDefault = data->imeWnd;
2318     }
2319
2320     if (!fOpen != !data->IMC.fOpen)
2321     {
2322         data->IMC.fOpen = fOpen;
2323         ImmNotifyIME( hIMC, NI_CONTEXTUPDATED, 0, IMC_SETOPENSTATUS);
2324         ImmInternalSendIMENotify(data, IMN_SETOPENSTATUS, 0);
2325     }
2326
2327     return TRUE;
2328 }
2329
2330 /***********************************************************************
2331  *              ImmSetStatusWindowPos (IMM32.@)
2332  */
2333 BOOL WINAPI ImmSetStatusWindowPos(HIMC hIMC, LPPOINT lpptPos)
2334 {
2335     InputContextData *data = hIMC;
2336
2337     TRACE("(%p, %p)\n", hIMC, lpptPos);
2338
2339     if (!data || !lpptPos)
2340         return FALSE;
2341
2342     TRACE("\t(%i,%i)\n", lpptPos->x, lpptPos->y);
2343
2344     data->IMC.ptStatusWndPos = *lpptPos;
2345     ImmNotifyIME( hIMC, NI_CONTEXTUPDATED, 0, IMC_SETSTATUSWINDOWPOS);
2346     ImmInternalSendIMENotify(data, IMN_SETSTATUSWINDOWPOS, 0);
2347
2348     return TRUE;
2349 }
2350
2351 /***********************************************************************
2352  *              ImmCreateSoftKeyboard(IMM32.@)
2353  */
2354 HWND WINAPI ImmCreateSoftKeyboard(UINT uType, UINT hOwner, int x, int y)
2355 {
2356     FIXME("(%d, %d, %d, %d): stub\n", uType, hOwner, x, y);
2357     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2358     return 0;
2359 }
2360
2361 /***********************************************************************
2362  *              ImmDestroySoftKeyboard(IMM32.@)
2363  */
2364 BOOL WINAPI ImmDestroySoftKeyboard(HWND hSoftWnd)
2365 {
2366     FIXME("(%p): stub\n", hSoftWnd);
2367     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2368     return FALSE;
2369 }
2370
2371 /***********************************************************************
2372  *              ImmShowSoftKeyboard(IMM32.@)
2373  */
2374 BOOL WINAPI ImmShowSoftKeyboard(HWND hSoftWnd, int nCmdShow)
2375 {
2376     FIXME("(%p, %d): stub\n", hSoftWnd, nCmdShow);
2377     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2378     return FALSE;
2379 }
2380
2381 /***********************************************************************
2382  *              ImmSimulateHotKey (IMM32.@)
2383  */
2384 BOOL WINAPI ImmSimulateHotKey(HWND hWnd, DWORD dwHotKeyID)
2385 {
2386   FIXME("(%p, %d): stub\n", hWnd, dwHotKeyID);
2387   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2388   return FALSE;
2389 }
2390
2391 /***********************************************************************
2392  *              ImmUnregisterWordA (IMM32.@)
2393  */
2394 BOOL WINAPI ImmUnregisterWordA(
2395   HKL hKL, LPCSTR lpszReading, DWORD dwStyle, LPCSTR lpszUnregister)
2396 {
2397     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
2398     TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_a(lpszReading), dwStyle,
2399             debugstr_a(lpszUnregister));
2400     if (immHkl->hIME && immHkl->pImeUnregisterWord)
2401     {
2402         if (!is_kbd_ime_unicode(immHkl))
2403             return immHkl->pImeUnregisterWord((LPCWSTR)lpszReading,dwStyle,
2404                                               (LPCWSTR)lpszUnregister);
2405         else
2406         {
2407             LPWSTR lpszwReading = strdupAtoW(lpszReading);
2408             LPWSTR lpszwUnregister = strdupAtoW(lpszUnregister);
2409             BOOL rc;
2410
2411             rc = immHkl->pImeUnregisterWord(lpszwReading,dwStyle,lpszwUnregister);
2412             HeapFree(GetProcessHeap(),0,lpszwReading);
2413             HeapFree(GetProcessHeap(),0,lpszwUnregister);
2414             return rc;
2415         }
2416     }
2417     else
2418         return FALSE;
2419 }
2420
2421 /***********************************************************************
2422  *              ImmUnregisterWordW (IMM32.@)
2423  */
2424 BOOL WINAPI ImmUnregisterWordW(
2425   HKL hKL, LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszUnregister)
2426 {
2427     ImmHkl *immHkl = IMM_GetImmHkl(hKL);
2428     TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_w(lpszReading), dwStyle,
2429             debugstr_w(lpszUnregister));
2430     if (immHkl->hIME && immHkl->pImeUnregisterWord)
2431     {
2432         if (is_kbd_ime_unicode(immHkl))
2433             return immHkl->pImeUnregisterWord(lpszReading,dwStyle,lpszUnregister);
2434         else
2435         {
2436             LPSTR lpszaReading = strdupWtoA(lpszReading);
2437             LPSTR lpszaUnregister = strdupWtoA(lpszUnregister);
2438             BOOL rc;
2439
2440             rc = immHkl->pImeUnregisterWord((LPCWSTR)lpszaReading,dwStyle,
2441                                             (LPCWSTR)lpszaUnregister);
2442             HeapFree(GetProcessHeap(),0,lpszaReading);
2443             HeapFree(GetProcessHeap(),0,lpszaUnregister);
2444             return rc;
2445         }
2446     }
2447     else
2448         return FALSE;
2449 }
2450
2451 /***********************************************************************
2452  *              ImmGetImeMenuItemsA (IMM32.@)
2453  */
2454 DWORD WINAPI ImmGetImeMenuItemsA( HIMC hIMC, DWORD dwFlags, DWORD dwType,
2455    LPIMEMENUITEMINFOA lpImeParentMenu, LPIMEMENUITEMINFOA lpImeMenu,
2456     DWORD dwSize)
2457 {
2458     InputContextData *data = hIMC;
2459     TRACE("(%p, %i, %i, %p, %p, %i):\n", hIMC, dwFlags, dwType,
2460         lpImeParentMenu, lpImeMenu, dwSize);
2461     if (data->immKbd->hIME && data->immKbd->pImeGetImeMenuItems)
2462     {
2463         if (!is_himc_ime_unicode(data) || (!lpImeParentMenu && !lpImeMenu))
2464             return data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2465                                 (IMEMENUITEMINFOW*)lpImeParentMenu,
2466                                 (IMEMENUITEMINFOW*)lpImeMenu, dwSize);
2467         else
2468         {
2469             IMEMENUITEMINFOW lpImeParentMenuW;
2470             IMEMENUITEMINFOW *lpImeMenuW, *parent = NULL;
2471             DWORD rc;
2472
2473             if (lpImeParentMenu)
2474                 parent = &lpImeParentMenuW;
2475             if (lpImeMenu)
2476             {
2477                 int count = dwSize / sizeof(LPIMEMENUITEMINFOA);
2478                 dwSize = count * sizeof(IMEMENUITEMINFOW);
2479                 lpImeMenuW = HeapAlloc(GetProcessHeap(), 0, dwSize);
2480             }
2481             else
2482                 lpImeMenuW = NULL;
2483
2484             rc = data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2485                                 parent, lpImeMenuW, dwSize);
2486
2487             if (lpImeParentMenu)
2488             {
2489                 memcpy(lpImeParentMenu,&lpImeParentMenuW,sizeof(IMEMENUITEMINFOA));
2490                 lpImeParentMenu->hbmpItem = lpImeParentMenuW.hbmpItem;
2491                 WideCharToMultiByte(CP_ACP, 0, lpImeParentMenuW.szString,
2492                     -1, lpImeParentMenu->szString, IMEMENUITEM_STRING_SIZE,
2493                     NULL, NULL);
2494             }
2495             if (lpImeMenu && rc)
2496             {
2497                 unsigned int i;
2498                 for (i = 0; i < rc; i++)
2499                 {
2500                     memcpy(&lpImeMenu[i],&lpImeMenuW[1],sizeof(IMEMENUITEMINFOA));
2501                     lpImeMenu[i].hbmpItem = lpImeMenuW[i].hbmpItem;
2502                     WideCharToMultiByte(CP_ACP, 0, lpImeMenuW[i].szString,
2503                         -1, lpImeMenu[i].szString, IMEMENUITEM_STRING_SIZE,
2504                         NULL, NULL);
2505                 }
2506             }
2507             HeapFree(GetProcessHeap(),0,lpImeMenuW);
2508             return rc;
2509         }
2510     }
2511     else
2512         return 0;
2513 }
2514
2515 /***********************************************************************
2516 *               ImmGetImeMenuItemsW (IMM32.@)
2517 */
2518 DWORD WINAPI ImmGetImeMenuItemsW( HIMC hIMC, DWORD dwFlags, DWORD dwType,
2519    LPIMEMENUITEMINFOW lpImeParentMenu, LPIMEMENUITEMINFOW lpImeMenu,
2520    DWORD dwSize)
2521 {
2522     InputContextData *data = hIMC;
2523     TRACE("(%p, %i, %i, %p, %p, %i):\n", hIMC, dwFlags, dwType,
2524         lpImeParentMenu, lpImeMenu, dwSize);
2525     if (data->immKbd->hIME && data->immKbd->pImeGetImeMenuItems)
2526     {
2527         if (is_himc_ime_unicode(data) || (!lpImeParentMenu && !lpImeMenu))
2528             return data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2529                                 lpImeParentMenu, lpImeMenu, dwSize);
2530         else
2531         {
2532             IMEMENUITEMINFOA lpImeParentMenuA;
2533             IMEMENUITEMINFOA *lpImeMenuA, *parent = NULL;
2534             DWORD rc;
2535
2536             if (lpImeParentMenu)
2537                 parent = &lpImeParentMenuA;
2538             if (lpImeMenu)
2539             {
2540                 int count = dwSize / sizeof(LPIMEMENUITEMINFOW);
2541                 dwSize = count * sizeof(IMEMENUITEMINFOA);
2542                 lpImeMenuA = HeapAlloc(GetProcessHeap(), 0, dwSize);
2543             }
2544             else
2545                 lpImeMenuA = NULL;
2546
2547             rc = data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
2548                                 (IMEMENUITEMINFOW*)parent,
2549                                 (IMEMENUITEMINFOW*)lpImeMenuA, dwSize);
2550
2551             if (lpImeParentMenu)
2552             {
2553                 memcpy(lpImeParentMenu,&lpImeParentMenuA,sizeof(IMEMENUITEMINFOA));
2554                 lpImeParentMenu->hbmpItem = lpImeParentMenuA.hbmpItem;
2555                 MultiByteToWideChar(CP_ACP, 0, lpImeParentMenuA.szString,
2556                     -1, lpImeParentMenu->szString, IMEMENUITEM_STRING_SIZE);
2557             }
2558             if (lpImeMenu && rc)
2559             {
2560                 unsigned int i;
2561                 for (i = 0; i < rc; i++)
2562                 {
2563                     memcpy(&lpImeMenu[i],&lpImeMenuA[1],sizeof(IMEMENUITEMINFOA));
2564                     lpImeMenu[i].hbmpItem = lpImeMenuA[i].hbmpItem;
2565                     MultiByteToWideChar(CP_ACP, 0, lpImeMenuA[i].szString,
2566                         -1, lpImeMenu[i].szString, IMEMENUITEM_STRING_SIZE);
2567                 }
2568             }
2569             HeapFree(GetProcessHeap(),0,lpImeMenuA);
2570             return rc;
2571         }
2572     }
2573     else
2574         return 0;
2575 }
2576
2577 /***********************************************************************
2578 *               ImmLockIMC(IMM32.@)
2579 */
2580 LPINPUTCONTEXT WINAPI ImmLockIMC(HIMC hIMC)
2581 {
2582     InputContextData *data = hIMC;
2583
2584     if (!data)
2585         return NULL;
2586     data->dwLock++;
2587     return &data->IMC;
2588 }
2589
2590 /***********************************************************************
2591 *               ImmUnlockIMC(IMM32.@)
2592 */
2593 BOOL WINAPI ImmUnlockIMC(HIMC hIMC)
2594 {
2595     InputContextData *data = hIMC;
2596     data->dwLock--;
2597     return (data->dwLock!=0);
2598 }
2599
2600 /***********************************************************************
2601 *               ImmGetIMCLockCount(IMM32.@)
2602 */
2603 DWORD WINAPI ImmGetIMCLockCount(HIMC hIMC)
2604 {
2605     InputContextData *data = hIMC;
2606     return data->dwLock;
2607 }
2608
2609 /***********************************************************************
2610 *               ImmCreateIMCC(IMM32.@)
2611 */
2612 HIMCC  WINAPI ImmCreateIMCC(DWORD size)
2613 {
2614     IMCCInternal *internal;
2615     int real_size = size + sizeof(IMCCInternal);
2616
2617     internal = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, real_size);
2618     if (internal == NULL)
2619         return NULL;
2620
2621     internal->dwSize = size;
2622     return  internal;
2623 }
2624
2625 /***********************************************************************
2626 *       ImmDestroyIMCC(IMM32.@)
2627 */
2628 HIMCC WINAPI ImmDestroyIMCC(HIMCC block)
2629 {
2630     HeapFree(GetProcessHeap(),0,block);
2631     return NULL;
2632 }
2633
2634 /***********************************************************************
2635 *               ImmLockIMCC(IMM32.@)
2636 */
2637 LPVOID WINAPI ImmLockIMCC(HIMCC imcc)
2638 {
2639     IMCCInternal *internal;
2640     internal = imcc;
2641
2642     internal->dwLock ++;
2643     return internal + 1;
2644 }
2645
2646 /***********************************************************************
2647 *               ImmUnlockIMCC(IMM32.@)
2648 */
2649 BOOL WINAPI ImmUnlockIMCC(HIMCC imcc)
2650 {
2651     IMCCInternal *internal;
2652     internal = imcc;
2653
2654     internal->dwLock --;
2655     return (internal->dwLock!=0);
2656 }
2657
2658 /***********************************************************************
2659 *               ImmGetIMCCLockCount(IMM32.@)
2660 */
2661 DWORD WINAPI ImmGetIMCCLockCount(HIMCC imcc)
2662 {
2663     IMCCInternal *internal;
2664     internal = imcc;
2665
2666     return internal->dwLock;
2667 }
2668
2669 /***********************************************************************
2670 *               ImmReSizeIMCC(IMM32.@)
2671 */
2672 HIMCC  WINAPI ImmReSizeIMCC(HIMCC imcc, DWORD size)
2673 {
2674     IMCCInternal *internal,*newone;
2675     int real_size = size + sizeof(IMCCInternal);
2676
2677     internal = imcc;
2678
2679     newone = HeapReAlloc(GetProcessHeap(), 0, internal, real_size);
2680     newone->dwSize = size;
2681
2682     return newone;
2683 }
2684
2685 /***********************************************************************
2686 *               ImmGetIMCCSize(IMM32.@)
2687 */
2688 DWORD WINAPI ImmGetIMCCSize(HIMCC imcc)
2689 {
2690     IMCCInternal *internal;
2691     internal = imcc;
2692
2693     return internal->dwSize;
2694 }
2695
2696 /***********************************************************************
2697 *               ImmGenerateMessage(IMM32.@)
2698 */
2699 BOOL WINAPI ImmGenerateMessage(HIMC hIMC)
2700 {
2701     InputContextData *data = hIMC;
2702
2703     TRACE("%i messages queued\n",data->IMC.dwNumMsgBuf);
2704     if (data->IMC.dwNumMsgBuf > 0)
2705     {
2706         LPTRANSMSG lpTransMsg;
2707         DWORD i;
2708
2709         lpTransMsg = ImmLockIMCC(data->IMC.hMsgBuf);
2710         for (i = 0; i < data->IMC.dwNumMsgBuf; i++)
2711             ImmInternalPostIMEMessage(data, lpTransMsg[i].message, lpTransMsg[i].wParam, lpTransMsg[i].lParam);
2712
2713         ImmUnlockIMCC(data->IMC.hMsgBuf);
2714
2715         data->IMC.dwNumMsgBuf = 0;
2716     }
2717
2718     return TRUE;
2719 }
2720
2721 /***********************************************************************
2722 *       ImmTranslateMessage(IMM32.@)
2723 *       ( Undocumented, call internally and from user32.dll )
2724 */
2725 BOOL WINAPI ImmTranslateMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lKeyData)
2726 {
2727     InputContextData *data;
2728     HIMC imc = ImmGetContext(hwnd);
2729     BYTE state[256];
2730     UINT scancode;
2731     LPVOID list = 0;
2732     UINT msg_count;
2733     UINT uVirtKey;
2734     static const DWORD list_count = 10;
2735
2736     TRACE("%p %x %x %x\n",hwnd, msg, (UINT)wParam, (UINT)lKeyData);
2737
2738     if (imc)
2739         data = imc;
2740     else
2741         return FALSE;
2742
2743     if (!data->immKbd->hIME || !data->immKbd->pImeToAsciiEx)
2744         return FALSE;
2745
2746     GetKeyboardState(state);
2747     scancode = lKeyData >> 0x10 & 0xff;
2748
2749     list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list_count * sizeof(TRANSMSG) + sizeof(DWORD));
2750     ((DWORD*)list)[0] = list_count;
2751
2752     if (data->immKbd->imeInfo.fdwProperty & IME_PROP_KBD_CHAR_FIRST)
2753     {
2754         WCHAR chr;
2755
2756         if (!is_himc_ime_unicode(data))
2757             ToAscii(data->lastVK, scancode, state, &chr, 0);
2758         else
2759             ToUnicodeEx(data->lastVK, scancode, state, &chr, 1, 0, GetKeyboardLayout(0));
2760         uVirtKey = MAKELONG(data->lastVK,chr);
2761     }
2762     else
2763         uVirtKey = data->lastVK;
2764
2765     msg_count = data->immKbd->pImeToAsciiEx(uVirtKey, scancode, state, list, 0, imc);
2766     TRACE("%i messages generated\n",msg_count);
2767     if (msg_count && msg_count <= list_count)
2768     {
2769         UINT i;
2770         LPTRANSMSG msgs = (LPTRANSMSG)((LPBYTE)list + sizeof(DWORD));
2771
2772         for (i = 0; i < msg_count; i++)
2773             ImmInternalPostIMEMessage(data, msgs[i].message, msgs[i].wParam, msgs[i].lParam);
2774     }
2775     else if (msg_count > list_count)
2776         ImmGenerateMessage(imc);
2777
2778     HeapFree(GetProcessHeap(),0,list);
2779
2780     data->lastVK = VK_PROCESSKEY;
2781
2782     return (msg_count > 0);
2783 }
2784
2785 /***********************************************************************
2786 *               ImmProcessKey(IMM32.@)
2787 *       ( Undocumented, called from user32.dll )
2788 */
2789 BOOL WINAPI ImmProcessKey(HWND hwnd, HKL hKL, UINT vKey, LPARAM lKeyData, DWORD unknown)
2790 {
2791     InputContextData *data;
2792     HIMC imc = ImmGetContext(hwnd);
2793     BYTE state[256];
2794
2795     TRACE("%p %p %x %x %x\n",hwnd, hKL, vKey, (UINT)lKeyData, unknown);
2796
2797     if (imc)
2798         data = imc;
2799     else
2800         return FALSE;
2801
2802     if (!data->immKbd->hIME || !data->immKbd->pImeProcessKey)
2803         return FALSE;
2804
2805     GetKeyboardState(state);
2806     if (data->immKbd->pImeProcessKey(imc, vKey, lKeyData, state))
2807     {
2808         data->lastVK = vKey;
2809         return TRUE;
2810     }
2811
2812     data->lastVK = VK_PROCESSKEY;
2813     return FALSE;
2814 }
2815
2816 /***********************************************************************
2817 *               ImmDisableTextFrameService(IMM32.@)
2818 */
2819 BOOL WINAPI ImmDisableTextFrameService(DWORD idThread)
2820 {
2821     FIXME("Stub\n");
2822     return FALSE;
2823 }