imm.c 91 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
2
 * IMM32 library
Alexandre Julliard's avatar
Alexandre Julliard committed
3
 *
4
 * Copyright 1998 Patrik Stridvall
5
 * Copyright 2002, 2003, 2007 CodeWeavers, Aric Stewart
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
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"
Alexandre Julliard's avatar
Alexandre Julliard committed
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 40 41
#define IMM_INIT_MAGIC 0x19650412
BOOL WINAPI User32InitializeImmEntryTable(DWORD);

42 43 44 45 46 47 48
typedef struct _tagImmHkl{
    struct list entry;
    HKL         hkl;
    HMODULE     hIME;
    IMEINFO     imeInfo;
    WCHAR       imeClassName[17]; /* 16 character max */
    ULONG       uSelected;
49
    HWND        UIWnd;
50 51

    /* Function Pointers */
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    BOOL (WINAPI *pImeInquire)(IMEINFO *, WCHAR *, const WCHAR *);
    BOOL (WINAPI *pImeConfigure)(HKL, HWND, DWORD, void *);
    BOOL (WINAPI *pImeDestroy)(UINT);
    LRESULT (WINAPI *pImeEscape)(HIMC, UINT, void *);
    BOOL (WINAPI *pImeSelect)(HIMC, BOOL);
    BOOL (WINAPI *pImeSetActiveContext)(HIMC, BOOL);
    UINT (WINAPI *pImeToAsciiEx)(UINT, UINT, const BYTE *, DWORD *, UINT, HIMC);
    BOOL (WINAPI *pNotifyIME)(HIMC, DWORD, DWORD, DWORD);
    BOOL (WINAPI *pImeRegisterWord)(const WCHAR *, DWORD, const WCHAR *);
    BOOL (WINAPI *pImeUnregisterWord)(const WCHAR *, DWORD, const WCHAR *);
    UINT (WINAPI *pImeEnumRegisterWord)(REGISTERWORDENUMPROCW, const WCHAR *, DWORD, const WCHAR *, void *);
    BOOL (WINAPI *pImeSetCompositionString)(HIMC, DWORD, const void *, DWORD, const void *, DWORD);
    DWORD (WINAPI *pImeConversionList)(HIMC, const WCHAR *, CANDIDATELIST *, DWORD, UINT);
    BOOL (WINAPI *pImeProcessKey)(HIMC, UINT, LPARAM, const BYTE *);
    UINT (WINAPI *pImeGetRegisterWordStyle)(UINT, STYLEBUFW *);
    DWORD (WINAPI *pImeGetImeMenuItems)(HIMC, DWORD, DWORD, IMEMENUITEMINFOW *, IMEMENUITEMINFOW *, DWORD);
68 69
} ImmHkl;

70 71
typedef struct tagInputContextData
{
72 73
        DWORD           dwLock;
        INPUTCONTEXT    IMC;
74
        DWORD           threadID;
75 76

        ImmHkl          *immKbd;
77
        UINT            lastVK;
78
        BOOL            threadDefault;
79
        DWORD           magic;
80
} InputContextData;
81

82 83
#define WINE_IMC_VALID_MAGIC 0x56434D49

84 85 86 87 88 89
typedef struct _tagTRANSMSG {
    UINT message;
    WPARAM wParam;
    LPARAM lParam;
} TRANSMSG, *LPTRANSMSG;

90
typedef struct _tagIMMThreadData {
91 92
    struct list entry;
    DWORD threadID;
93 94
    HIMC defaultContext;
    HWND hwndDefault;
95
    BOOL disableIME;
96
    DWORD windowRefs;
97
} IMMThreadData;
98

99
static struct list ImmHklList = LIST_INIT(ImmHklList);
100
static struct list ImmThreadDataList = LIST_INIT(ImmThreadDataList);
101

102
static const WCHAR szwWineIMCProperty[] = {'W','i','n','e','I','m','m','H','I','M','C','P','r','o','p','e','r','t','y',0};
103

104 105
static const WCHAR szImeFileW[] = {'I','m','e',' ','F','i','l','e',0};
static const WCHAR szLayoutTextW[] = {'L','a','y','o','u','t',' ','T','e','x','t',0};
106
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};
107

108
static const WCHAR szwIME[] = {'I','M','E',0};
109
static const WCHAR szwDefaultIME[] = {'D','e','f','a','u','l','t',' ','I','M','E',0};
110

111 112 113 114 115 116 117 118
static CRITICAL_SECTION threaddata_cs;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &threaddata_cs,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
};
static CRITICAL_SECTION threaddata_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
119
static BOOL disable_ime;
120

121 122 123 124 125 126 127 128 129
static inline BOOL is_himc_ime_unicode(const InputContextData *data)
{
    return !!(data->immKbd->imeInfo.fdwProperty & IME_PROP_UNICODE);
}

static inline BOOL is_kbd_ime_unicode(const ImmHkl *hkl)
{
    return !!(hkl->imeInfo.fdwProperty & IME_PROP_UNICODE);
}
130

131
static BOOL IMM_DestroyContext(HIMC hIMC);
132
static InputContextData* get_imc_data(HIMC hIMC);
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
static inline WCHAR *strdupAtoW( const char *str )
{
    WCHAR *ret = NULL;
    if (str)
    {
        DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
        if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
            MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
    }
    return ret;
}

static inline CHAR *strdupWtoA( const WCHAR *str )
{
    CHAR *ret = NULL;
    if (str)
    {
        DWORD len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
        if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
            WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
    }
    return ret;
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
static DWORD convert_candidatelist_WtoA(
        LPCANDIDATELIST lpSrc, LPCANDIDATELIST lpDst, DWORD dwBufLen)
{
    DWORD ret, i, len;

    ret = FIELD_OFFSET( CANDIDATELIST, dwOffset[lpSrc->dwCount] );
    if ( lpDst && dwBufLen > 0 )
    {
        *lpDst = *lpSrc;
        lpDst->dwOffset[0] = ret;
    }

    for ( i = 0; i < lpSrc->dwCount; i++)
    {
        LPBYTE src = (LPBYTE)lpSrc + lpSrc->dwOffset[i];

        if ( lpDst && dwBufLen > 0 )
        {
            LPBYTE dest = (LPBYTE)lpDst + lpDst->dwOffset[i];

            len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1,
                                      (LPSTR)dest, dwBufLen, NULL, NULL);

            if ( i + 1 < lpSrc->dwCount )
                lpDst->dwOffset[i+1] = lpDst->dwOffset[i] + len * sizeof(char);
            dwBufLen -= len * sizeof(char);
        }
        else
            len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, NULL, 0, NULL, NULL);

        ret += len * sizeof(char);
    }

    if ( lpDst )
        lpDst->dwSize = ret;

    return ret;
}

static DWORD convert_candidatelist_AtoW(
        LPCANDIDATELIST lpSrc, LPCANDIDATELIST lpDst, DWORD dwBufLen)
{
    DWORD ret, i, len;

    ret = FIELD_OFFSET( CANDIDATELIST, dwOffset[lpSrc->dwCount] );
    if ( lpDst && dwBufLen > 0 )
    {
        *lpDst = *lpSrc;
        lpDst->dwOffset[0] = ret;
    }

    for ( i = 0; i < lpSrc->dwCount; i++)
    {
        LPBYTE src = (LPBYTE)lpSrc + lpSrc->dwOffset[i];

        if ( lpDst && dwBufLen > 0 )
        {
            LPBYTE dest = (LPBYTE)lpDst + lpDst->dwOffset[i];

            len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1,
                                      (LPWSTR)dest, dwBufLen);

            if ( i + 1 < lpSrc->dwCount )
                lpDst->dwOffset[i+1] = lpDst->dwOffset[i] + len * sizeof(WCHAR);
            dwBufLen -= len * sizeof(WCHAR);
        }
        else
            len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, NULL, 0);

        ret += len * sizeof(WCHAR);
    }

    if ( lpDst )
        lpDst->dwSize = ret;

    return ret;
}

236
static IMMThreadData *IMM_GetThreadData(HWND hwnd, DWORD thread)
237
{
238
    IMMThreadData *data;
239
    DWORD process;
240

241 242 243 244 245
    if (hwnd)
    {
        if (!(thread = GetWindowThreadProcessId(hwnd, &process))) return NULL;
        if (process != GetCurrentProcessId()) return NULL;
    }
246 247 248 249 250 251 252 253 254 255
    else if (thread)
    {
        HANDLE h = OpenThread(THREAD_QUERY_INFORMATION, FALSE, thread);
        if (!h) return NULL;
        process = GetProcessIdOfThread(h);
        CloseHandle(h);
        if (process != GetCurrentProcessId()) return NULL;
    }
    else
        thread = GetCurrentThreadId();
256 257 258

    EnterCriticalSection(&threaddata_cs);
    LIST_FOR_EACH_ENTRY(data, &ImmThreadDataList, IMMThreadData, entry)
259
        if (data->threadID == thread) return data;
260

261 262
    data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
    data->threadID = thread;
263
    list_add_head(&ImmThreadDataList,&data->entry);
264
    TRACE("Thread Data Created (%x)\n",thread);
265
    return data;
266 267
}

268 269 270 271 272 273 274 275 276 277
static BOOL IMM_IsDefaultContext(HIMC imc)
{
    InputContextData *data = get_imc_data(imc);

    if (!data)
        return FALSE;

    return data->threadDefault;
}

278 279
static void IMM_FreeThreadData(void)
{
280 281 282 283
    IMMThreadData *data;

    EnterCriticalSection(&threaddata_cs);
    LIST_FOR_EACH_ENTRY(data, &ImmThreadDataList, IMMThreadData, entry)
284
    {
285 286 287 288 289 290 291 292 293
        if (data->threadID == GetCurrentThreadId())
        {
            list_remove(&data->entry);
            LeaveCriticalSection(&threaddata_cs);
            IMM_DestroyContext(data->defaultContext);
            HeapFree(GetProcessHeap(),0,data);
            TRACE("Thread Data Destroyed\n");
            return;
        }
294
    }
295
    LeaveCriticalSection(&threaddata_cs);
296 297
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
static HMODULE load_graphics_driver(void)
{
    static const WCHAR display_device_guid_propW[] = {
        '_','_','w','i','n','e','_','d','i','s','p','l','a','y','_',
        'd','e','v','i','c','e','_','g','u','i','d',0 };
    static const WCHAR key_pathW[] = {
        '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','\\',
        'V','i','d','e','o','\\','{',0};
    static const WCHAR displayW[] = {'}','\\','0','0','0','0',0};
    static const WCHAR driverW[] = {'G','r','a','p','h','i','c','s','D','r','i','v','e','r',0};

    HMODULE ret = 0;
    HKEY hkey;
    DWORD size;
    WCHAR path[MAX_PATH];
315
    WCHAR key[ARRAY_SIZE( key_pathW ) + ARRAY_SIZE( displayW ) + 40];
316 317 318 319
    UINT guid_atom = HandleToULong( GetPropW( GetDesktopWindow(), display_device_guid_propW ));

    if (!guid_atom) return 0;
    memcpy( key, key_pathW, sizeof(key_pathW) );
320 321
    if (!GlobalGetAtomNameW( guid_atom, key + lstrlenW(key), 40 )) return 0;
    lstrcatW( key, displayW );
322 323 324 325 326 327 328 329
    if (RegOpenKeyW( HKEY_LOCAL_MACHINE, key, &hkey )) return 0;
    size = sizeof(path);
    if (!RegQueryValueExW( hkey, driverW, NULL, NULL, (BYTE *)path, &size )) ret = LoadLibraryW( path );
    RegCloseKey( hkey );
    TRACE( "%s %p\n", debugstr_w(path), ret );
    return ret;
}

330 331 332 333 334 335 336
/* ImmHkl loading and freeing */
#define LOAD_FUNCPTR(f) if((ptr->p##f = (LPVOID)GetProcAddress(ptr->hIME, #f)) == NULL){WARN("Can't find function %s in ime\n", #f);}
static ImmHkl *IMM_GetImmHkl(HKL hkl)
{
    ImmHkl *ptr;
    WCHAR filename[MAX_PATH];

337
    TRACE("Seeking ime for keyboard %p\n",hkl);
338 339 340 341 342 343 344 345 346 347 348

    LIST_FOR_EACH_ENTRY(ptr, &ImmHklList, ImmHkl, entry)
    {
        if (ptr->hkl == hkl)
            return ptr;
    }
    /* not found... create it */

    ptr = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ImmHkl));

    ptr->hkl = hkl;
349
    if (ImmGetIMEFileNameW(hkl, filename, MAX_PATH)) ptr->hIME = LoadLibraryW(filename);
350
    if (!ptr->hIME) ptr->hIME = load_graphics_driver();
351 352 353
    if (ptr->hIME)
    {
        LOAD_FUNCPTR(ImeInquire);
354
        if (!ptr->pImeInquire || !ptr->pImeInquire(&ptr->imeInfo, ptr->imeClassName, NULL))
355 356 357 358 359 360
        {
            FreeLibrary(ptr->hIME);
            ptr->hIME = NULL;
        }
        else
        {
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
            LOAD_FUNCPTR(ImeDestroy);
            LOAD_FUNCPTR(ImeSelect);
            if (!ptr->pImeSelect || !ptr->pImeDestroy)
            {
                FreeLibrary(ptr->hIME);
                ptr->hIME = NULL;
            }
            else
            {
                LOAD_FUNCPTR(ImeConfigure);
                LOAD_FUNCPTR(ImeEscape);
                LOAD_FUNCPTR(ImeSetActiveContext);
                LOAD_FUNCPTR(ImeToAsciiEx);
                LOAD_FUNCPTR(NotifyIME);
                LOAD_FUNCPTR(ImeRegisterWord);
                LOAD_FUNCPTR(ImeUnregisterWord);
                LOAD_FUNCPTR(ImeEnumRegisterWord);
                LOAD_FUNCPTR(ImeSetCompositionString);
                LOAD_FUNCPTR(ImeConversionList);
                LOAD_FUNCPTR(ImeProcessKey);
                LOAD_FUNCPTR(ImeGetRegisterWordStyle);
                LOAD_FUNCPTR(ImeGetImeMenuItems);
                /* make sure our classname is WCHAR */
                if (!is_kbd_ime_unicode(ptr))
                {
                    WCHAR bufW[17];
                    MultiByteToWideChar(CP_ACP, 0, (LPSTR)ptr->imeClassName,
                                        -1, bufW, 17);
                    lstrcpyW(ptr->imeClassName, bufW);
                }
            }
392 393 394 395 396 397 398 399
        }
    }
    list_add_head(&ImmHklList,&ptr->entry);

    return ptr;
}
#undef LOAD_FUNCPTR

400 401 402 403 404 405
HWND WINAPI __wine_get_ui_window(HKL hkl)
{
    ImmHkl *immHkl = IMM_GetImmHkl(hkl);
    return immHkl->UIWnd;
}

406 407 408 409
static void IMM_FreeAllImmHkl(void)
{
    ImmHkl *ptr,*cursor2;

410
    LIST_FOR_EACH_ENTRY_SAFE(ptr, cursor2, &ImmHklList, ImmHkl, entry)
411 412 413 414 415 416 417
    {
        list_remove(&ptr->entry);
        if (ptr->hIME)
        {
            ptr->pImeDestroy(1);
            FreeLibrary(ptr->hIME);
        }
418 419
        if (ptr->UIWnd)
            DestroyWindow(ptr->UIWnd);
420 421 422 423
        HeapFree(GetProcessHeap(),0,ptr);
    }
}

424 425
BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved)
{
426
    TRACE("%p, %x, %p\n",hInstDLL,fdwReason,lpReserved);
427 428 429
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
430 431 432 433
            if (!User32InitializeImmEntryTable(IMM_INIT_MAGIC))
            {
                return FALSE;
            }
434 435 436 437 438
            break;
        case DLL_THREAD_ATTACH:
            break;
        case DLL_THREAD_DETACH:
            IMM_FreeThreadData();
439 440
            break;
        case DLL_PROCESS_DETACH:
441
            if (lpReserved) break;
442
            IMM_FreeThreadData();
443
            IMM_FreeAllImmHkl();
444 445 446 447 448
            break;
    }
    return TRUE;
}

449
/* for posting messages as the IME */
450
static void ImmInternalPostIMEMessage(InputContextData *data, UINT msg, WPARAM wParam, LPARAM lParam)
451 452 453
{
    HWND target = GetFocus();
    if (!target)
454
       PostMessageW(data->IMC.hWnd,msg,wParam,lParam);
455
    else
456 457 458
       PostMessageW(target, msg, wParam, lParam);
}

459 460 461 462 463 464 465 466 467 468
/* for sending messages as the IME */
static void ImmInternalSendIMEMessage(InputContextData *data, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HWND target = GetFocus();
    if (!target)
       SendMessageW(data->IMC.hWnd,msg,wParam,lParam);
    else
       SendMessageW(target, msg, wParam, lParam);
}

469
static LRESULT ImmInternalSendIMENotify(InputContextData *data, WPARAM notify, LPARAM lParam)
470
{
471
    HWND target;
472

473
    target = data->IMC.hWnd;
474 475 476 477
    if (!target) target = GetFocus();

    if (target)
       return SendMessageW(target, WM_IME_NOTIFY, notify, lParam);
478

479
    return 0;
480
}
481

482 483 484 485 486
static HIMCC ImmCreateBlankCompStr(void)
{
    HIMCC rc;
    LPCOMPOSITIONSTRING ptr;
    rc = ImmCreateIMCC(sizeof(COMPOSITIONSTRING));
487
    ptr = ImmLockIMCC(rc);
488 489 490 491 492 493
    memset(ptr,0,sizeof(COMPOSITIONSTRING));
    ptr->dwSize = sizeof(COMPOSITIONSTRING);
    ImmUnlockIMCC(rc);
    return rc;
}

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
static InputContextData* get_imc_data(HIMC hIMC)
{
    InputContextData *data = hIMC;

    if (hIMC == NULL)
        return NULL;

    if(IsBadReadPtr(data, sizeof(InputContextData)) || data->magic != WINE_IMC_VALID_MAGIC)
    {
        SetLastError(ERROR_INVALID_HANDLE);
        return NULL;
    }
    return data;
}

509
static HIMC get_default_context( HWND hwnd )
510
{
511
    HIMC ret;
512
    IMMThreadData* thread_data = IMM_GetThreadData( hwnd, 0 );
513

514
    if (!thread_data) return 0;
515

516
    if (thread_data->defaultContext)
517
    {
518
        ret = thread_data->defaultContext;
519
        LeaveCriticalSection(&threaddata_cs);
520 521
        return ret;
    }
522

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    /* can't create a default context in another thread */
    if (thread_data->threadID != GetCurrentThreadId())
    {
        LeaveCriticalSection(&threaddata_cs);
        return 0;
    }

    LeaveCriticalSection(&threaddata_cs);

    ret = ImmCreateContext();
    if (!ret) return 0;
    ((InputContextData*)ret)->threadDefault = TRUE;

    /* thread_data is in the current thread so we can assume it's still valid */
    EnterCriticalSection(&threaddata_cs);

    if (thread_data->defaultContext) /* someone beat us */
    {
        IMM_DestroyContext( ret );
        ret = thread_data->defaultContext;
543
    }
544
    else thread_data->defaultContext = ret;
545

546 547
    LeaveCriticalSection(&threaddata_cs);
    return ret;
548 549
}

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
static BOOL IMM_IsCrossThreadAccess(HWND hWnd,  HIMC hIMC)
{
    InputContextData *data;

    if (hWnd)
    {
        DWORD thread = GetWindowThreadProcessId(hWnd, NULL);
        if (thread != GetCurrentThreadId()) return TRUE;
    }
    data = get_imc_data(hIMC);
    if (data && data->threadID != GetCurrentThreadId())
        return TRUE;

    return FALSE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
566
/***********************************************************************
567
 *		ImmAssociateContext (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
568
 */
569
HIMC WINAPI ImmAssociateContext(HWND hWnd, HIMC hIMC)
Alexandre Julliard's avatar
Alexandre Julliard committed
570
{
571
    HIMC old = NULL;
572
    InputContextData *data = get_imc_data(hIMC);
573

574
    TRACE("(%p, %p):\n", hWnd, hIMC);
575

576 577 578
    if(hIMC && !data)
        return NULL;

579 580 581
    /*
     * If already associated just return
     */
582
    if (hIMC && data->IMC.hWnd == hWnd)
583
        return hIMC;
584

585 586 587
    if (hIMC && IMM_IsCrossThreadAccess(hWnd, hIMC))
        return NULL;

588 589
    if (hWnd)
    {
590
        HIMC defaultContext = get_default_context( hWnd );
591
        old = RemovePropW(hWnd,szwWineIMCProperty);
592 593

        if (old == NULL)
594
            old = defaultContext;
595 596 597
        else if (old == (HIMC)-1)
            old = NULL;

598
        if (hIMC != defaultContext)
599 600 601 602
        {
            if (hIMC == NULL) /* Meaning disable imm for that window*/
                SetPropW(hWnd,szwWineIMCProperty,(HANDLE)-1);
            else
603
                SetPropW(hWnd,szwWineIMCProperty,hIMC);
604
        }
605 606 607

        if (old)
        {
608
            InputContextData *old_data = old;
609 610 611
            if (old_data->IMC.hWnd == hWnd)
                old_data->IMC.hWnd = NULL;
        }
612 613 614 615 616
    }

    if (!hIMC)
        return old;

617
    SendMessageW(data->IMC.hWnd, WM_IME_SETCONTEXT, FALSE, ISC_SHOWUIALL);
618
    data->IMC.hWnd = hWnd;
619
    SendMessageW(data->IMC.hWnd, WM_IME_SETCONTEXT, TRUE, ISC_SHOWUIALL);
620

621
    return old;
Alexandre Julliard's avatar
Alexandre Julliard committed
622 623
}

624 625 626 627 628 629 630 631 632 633 634

/*
 * Helper function for ImmAssociateContextEx
 */
static BOOL CALLBACK _ImmAssociateContextExEnumProc(HWND hwnd, LPARAM lParam)
{
    HIMC hImc = (HIMC)lParam;
    ImmAssociateContext(hwnd,hImc);
    return TRUE;
}

635 636 637 638 639
/***********************************************************************
 *              ImmAssociateContextEx (IMM32.@)
 */
BOOL WINAPI ImmAssociateContextEx(HWND hWnd, HIMC hIMC, DWORD dwFlags)
{
640
    TRACE("(%p, %p, 0x%x):\n", hWnd, hIMC, dwFlags);
641

642 643
    if (!hWnd)
        return FALSE;
644 645

    switch (dwFlags)
646
    {
647 648 649 650
    case 0:
        ImmAssociateContext(hWnd,hIMC);
        return TRUE;
    case IACE_DEFAULT:
651 652 653
    {
        HIMC defaultContext = get_default_context( hWnd );
        if (!defaultContext) return FALSE;
654
        ImmAssociateContext(hWnd,defaultContext);
655
        return TRUE;
656
    }
657
    case IACE_IGNORENOCONTEXT:
658
        if (GetPropW(hWnd,szwWineIMCProperty))
659 660
            ImmAssociateContext(hWnd,hIMC);
        return TRUE;
661
    case IACE_CHILDREN:
662 663
        EnumChildWindows(hWnd,_ImmAssociateContextExEnumProc,(LPARAM)hIMC);
        return TRUE;
664 665
    default:
        FIXME("Unknown dwFlags 0x%x\n",dwFlags);
666 667
        return FALSE;
    }
668 669
}

Alexandre Julliard's avatar
Alexandre Julliard committed
670
/***********************************************************************
671
 *		ImmConfigureIMEA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
672
 */
673 674
BOOL WINAPI ImmConfigureIMEA(
  HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
Alexandre Julliard's avatar
Alexandre Julliard committed
675
{
676 677 678 679
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);

    TRACE("(%p, %p, %d, %p):\n", hKL, hWnd, dwMode, lpData);

680 681 682
    if (dwMode == IME_CONFIG_REGISTERWORD && !lpData)
        return FALSE;

683 684 685 686 687 688 689
    if (immHkl->hIME && immHkl->pImeConfigure)
    {
        if (dwMode != IME_CONFIG_REGISTERWORD || !is_kbd_ime_unicode(immHkl))
            return immHkl->pImeConfigure(hKL,hWnd,dwMode,lpData);
        else
        {
            REGISTERWORDW rww;
690
            REGISTERWORDA *rwa = lpData;
691 692 693 694 695 696 697 698 699 700 701 702
            BOOL rc;

            rww.lpReading = strdupAtoW(rwa->lpReading);
            rww.lpWord = strdupAtoW(rwa->lpWord);
            rc = immHkl->pImeConfigure(hKL,hWnd,dwMode,&rww);
            HeapFree(GetProcessHeap(),0,rww.lpReading);
            HeapFree(GetProcessHeap(),0,rww.lpWord);
            return rc;
        }
    }
    else
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
703 704 705
}

/***********************************************************************
706
 *		ImmConfigureIMEW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
707
 */
708 709
BOOL WINAPI ImmConfigureIMEW(
  HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
Alexandre Julliard's avatar
Alexandre Julliard committed
710
{
711 712 713 714
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);

    TRACE("(%p, %p, %d, %p):\n", hKL, hWnd, dwMode, lpData);

715 716 717
    if (dwMode == IME_CONFIG_REGISTERWORD && !lpData)
        return FALSE;

718 719 720 721 722 723
    if (immHkl->hIME && immHkl->pImeConfigure)
    {
        if (dwMode != IME_CONFIG_REGISTERWORD || is_kbd_ime_unicode(immHkl))
            return immHkl->pImeConfigure(hKL,hWnd,dwMode,lpData);
        else
        {
724
            REGISTERWORDW *rww = lpData;
725 726 727 728 729 730 731 732 733 734 735 736 737
            REGISTERWORDA rwa;
            BOOL rc;

            rwa.lpReading = strdupWtoA(rww->lpReading);
            rwa.lpWord = strdupWtoA(rww->lpWord);
            rc = immHkl->pImeConfigure(hKL,hWnd,dwMode,&rwa);
            HeapFree(GetProcessHeap(),0,rwa.lpReading);
            HeapFree(GetProcessHeap(),0,rwa.lpWord);
            return rc;
        }
    }
    else
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
738 739
}

Eric Kohl's avatar
Eric Kohl committed
740
/***********************************************************************
741
 *		ImmCreateContext (IMM32.@)
Eric Kohl's avatar
Eric Kohl committed
742
 */
743
HIMC WINAPI ImmCreateContext(void)
Eric Kohl's avatar
Eric Kohl committed
744
{
745
    InputContextData *new_context;
746 747
    LPGUIDELINE gl;
    LPCANDIDATEINFO ci;
748
    int i;
749

750
    new_context = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(InputContextData));
751

752 753 754 755
    /* Load the IME */
    new_context->immKbd = IMM_GetImmHkl(GetKeyboardLayout(0));

    if (!new_context->immKbd->hIME)
756
    {
757
        TRACE("IME dll could not be loaded\n");
758 759 760
        HeapFree(GetProcessHeap(),0,new_context);
        return 0;
    }
761

762
    /* the HIMCCs are never NULL */
763
    new_context->IMC.hCompStr = ImmCreateBlankCompStr();
764 765 766 767 768 769 770 771 772 773 774
    new_context->IMC.hMsgBuf = ImmCreateIMCC(0);
    new_context->IMC.hCandInfo = ImmCreateIMCC(sizeof(CANDIDATEINFO));
    ci = ImmLockIMCC(new_context->IMC.hCandInfo);
    memset(ci,0,sizeof(CANDIDATEINFO));
    ci->dwSize = sizeof(CANDIDATEINFO);
    ImmUnlockIMCC(new_context->IMC.hCandInfo);
    new_context->IMC.hGuideLine = ImmCreateIMCC(sizeof(GUIDELINE));
    gl = ImmLockIMCC(new_context->IMC.hGuideLine);
    memset(gl,0,sizeof(GUIDELINE));
    gl->dwSize = sizeof(GUIDELINE);
    ImmUnlockIMCC(new_context->IMC.hGuideLine);
775

776
    for (i = 0; i < ARRAY_SIZE(new_context->IMC.cfCandForm); i++)
777 778
        new_context->IMC.cfCandForm[i].dwIndex = ~0u;

779 780 781
    /* Initialize the IME Private */
    new_context->IMC.hPrivate = ImmCreateIMCC(new_context->immKbd->imeInfo.dwPrivateDataSize);

782 783 784
    new_context->IMC.fdwConversion = new_context->immKbd->imeInfo.fdwConversionCaps;
    new_context->IMC.fdwSentence = new_context->immKbd->imeInfo.fdwSentenceCaps;

785
    if (!new_context->immKbd->pImeSelect(new_context, TRUE))
786 787
    {
        TRACE("Selection of IME failed\n");
788
        IMM_DestroyContext(new_context);
789 790
        return 0;
    }
791
    new_context->threadID = GetCurrentThreadId();
792
    SendMessageW(GetFocus(), WM_IME_SELECT, TRUE, (LPARAM)new_context->immKbd);
793 794

    new_context->immKbd->uSelected++;
795
    TRACE("Created context %p\n",new_context);
796

797
    new_context->magic = WINE_IMC_VALID_MAGIC;
798
    return new_context;
799 800
}

801
static BOOL IMM_DestroyContext(HIMC hIMC)
802
{
803
    InputContextData *data = get_imc_data(hIMC);
804 805 806

    TRACE("Destroying %p\n",hIMC);

807 808
    if (!data)
        return FALSE;
809

810 811
    data->immKbd->uSelected --;
    data->immKbd->pImeSelect(hIMC, FALSE);
812
    SendMessageW(data->IMC.hWnd, WM_IME_SELECT, FALSE, (LPARAM)data->immKbd);
813 814 815 816 817 818 819 820 821

    ImmDestroyIMCC(data->IMC.hCompStr);
    ImmDestroyIMCC(data->IMC.hCandInfo);
    ImmDestroyIMCC(data->IMC.hGuideLine);
    ImmDestroyIMCC(data->IMC.hPrivate);
    ImmDestroyIMCC(data->IMC.hMsgBuf);

    data->magic = 0;
    HeapFree(GetProcessHeap(),0,data);
822 823

    return TRUE;
Eric Kohl's avatar
Eric Kohl committed
824 825
}

826 827 828 829 830
/***********************************************************************
 *		ImmDestroyContext (IMM32.@)
 */
BOOL WINAPI ImmDestroyContext(HIMC hIMC)
{
831
    if (!IMM_IsDefaultContext(hIMC) && !IMM_IsCrossThreadAccess(NULL, hIMC))
832 833 834 835 836
        return IMM_DestroyContext(hIMC);
    else
        return FALSE;
}

837 838 839 840 841
/***********************************************************************
 *		ImmDisableIME (IMM32.@)
 */
BOOL WINAPI ImmDisableIME(DWORD idThread)
{
842 843 844 845 846 847 848 849
    if (idThread == (DWORD)-1)
        disable_ime = TRUE;
    else {
        IMMThreadData *thread_data = IMM_GetThreadData(NULL, idThread);
        if (!thread_data) return FALSE;
        thread_data->disableIME = TRUE;
        LeaveCriticalSection(&threaddata_cs);
    }
850 851 852
    return TRUE;
}

853 854 855 856 857 858 859 860
/***********************************************************************
 *		ImmEnumRegisterWordA (IMM32.@)
 */
UINT WINAPI ImmEnumRegisterWordA(
  HKL hKL, REGISTERWORDENUMPROCA lpfnEnumProc,
  LPCSTR lpszReading, DWORD dwStyle,
  LPCSTR lpszRegister, LPVOID lpData)
{
861 862 863 864 865 866 867 868 869 870
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %p, %s, %d, %s, %p):\n", hKL, lpfnEnumProc,
        debugstr_a(lpszReading), dwStyle, debugstr_a(lpszRegister), lpData);
    if (immHkl->hIME && immHkl->pImeEnumRegisterWord)
    {
        if (!is_kbd_ime_unicode(immHkl))
            return immHkl->pImeEnumRegisterWord((REGISTERWORDENUMPROCW)lpfnEnumProc,
                (LPCWSTR)lpszReading, dwStyle, (LPCWSTR)lpszRegister, lpData);
        else
        {
871 872 873 874 875 876 877 878 879 880 881
            LPWSTR lpszwReading = strdupAtoW(lpszReading);
            LPWSTR lpszwRegister = strdupAtoW(lpszRegister);
            BOOL rc;

            rc = immHkl->pImeEnumRegisterWord((REGISTERWORDENUMPROCW)lpfnEnumProc,
                                              lpszwReading, dwStyle, lpszwRegister,
                                              lpData);

            HeapFree(GetProcessHeap(),0,lpszwReading);
            HeapFree(GetProcessHeap(),0,lpszwRegister);
            return rc;
882 883 884 885
        }
    }
    else
        return 0;
886 887 888 889 890 891 892 893 894 895
}

/***********************************************************************
 *		ImmEnumRegisterWordW (IMM32.@)
 */
UINT WINAPI ImmEnumRegisterWordW(
  HKL hKL, REGISTERWORDENUMPROCW lpfnEnumProc,
  LPCWSTR lpszReading, DWORD dwStyle,
  LPCWSTR lpszRegister, LPVOID lpData)
{
896 897 898 899 900 901 902 903 904 905
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %p, %s, %d, %s, %p):\n", hKL, lpfnEnumProc,
        debugstr_w(lpszReading), dwStyle, debugstr_w(lpszRegister), lpData);
    if (immHkl->hIME && immHkl->pImeEnumRegisterWord)
    {
        if (is_kbd_ime_unicode(immHkl))
            return immHkl->pImeEnumRegisterWord(lpfnEnumProc, lpszReading, dwStyle,
                                            lpszRegister, lpData);
        else
        {
906 907 908 909 910 911 912 913 914 915
            LPSTR lpszaReading = strdupWtoA(lpszReading);
            LPSTR lpszaRegister = strdupWtoA(lpszRegister);
            BOOL rc;

            rc = immHkl->pImeEnumRegisterWord(lpfnEnumProc, (LPCWSTR)lpszaReading,
                                              dwStyle, (LPCWSTR)lpszaRegister, lpData);

            HeapFree(GetProcessHeap(),0,lpszaReading);
            HeapFree(GetProcessHeap(),0,lpszaRegister);
            return rc;
916 917 918 919
        }
    }
    else
        return 0;
920 921
}

922 923 924 925 926 927 928 929 930 931
static inline BOOL EscapeRequiresWA(UINT uEscape)
{
        if (uEscape == IME_ESC_GET_EUDC_DICTIONARY ||
            uEscape == IME_ESC_SET_EUDC_DICTIONARY ||
            uEscape == IME_ESC_IME_NAME ||
            uEscape == IME_ESC_GETHELPFILENAME)
        return TRUE;
    return FALSE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
932
/***********************************************************************
933
 *		ImmEscapeA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
934
 */
935
LRESULT WINAPI ImmEscapeA(
936
  HKL hKL, HIMC hIMC,
937
  UINT uEscape, LPVOID lpData)
Alexandre Julliard's avatar
Alexandre Julliard committed
938
{
939 940 941 942 943
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %p, %d, %p):\n", hKL, hIMC, uEscape, lpData);

    if (immHkl->hIME && immHkl->pImeEscape)
    {
944
        if (!EscapeRequiresWA(uEscape) || !is_kbd_ime_unicode(immHkl))
945 946 947
            return immHkl->pImeEscape(hIMC,uEscape,lpData);
        else
        {
948 949 950 951
            WCHAR buffer[81]; /* largest required buffer should be 80 */
            LRESULT rc;
            if (uEscape == IME_ESC_SET_EUDC_DICTIONARY)
            {
952
                MultiByteToWideChar(CP_ACP,0,lpData,-1,buffer,81);
953 954 955 956 957
                rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
            }
            else
            {
                rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
958
                WideCharToMultiByte(CP_ACP,0,buffer,-1,lpData,80, NULL, NULL);
959 960
            }
            return rc;
961 962 963 964
        }
    }
    else
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
965 966 967
}

/***********************************************************************
968
 *		ImmEscapeW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
969
 */
970 971 972
LRESULT WINAPI ImmEscapeW(
  HKL hKL, HIMC hIMC,
  UINT uEscape, LPVOID lpData)
Alexandre Julliard's avatar
Alexandre Julliard committed
973
{
974 975 976 977 978
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %p, %d, %p):\n", hKL, hIMC, uEscape, lpData);

    if (immHkl->hIME && immHkl->pImeEscape)
    {
979
        if (!EscapeRequiresWA(uEscape) || is_kbd_ime_unicode(immHkl))
980 981 982
            return immHkl->pImeEscape(hIMC,uEscape,lpData);
        else
        {
983 984 985 986
            CHAR buffer[81]; /* largest required buffer should be 80 */
            LRESULT rc;
            if (uEscape == IME_ESC_SET_EUDC_DICTIONARY)
            {
987
                WideCharToMultiByte(CP_ACP,0,lpData,-1,buffer,81, NULL, NULL);
988 989 990 991 992
                rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
            }
            else
            {
                rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
993
                MultiByteToWideChar(CP_ACP,0,buffer,-1,lpData,80);
994 995
            }
            return rc;
996 997 998 999
        }
    }
    else
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1000 1001 1002
}

/***********************************************************************
1003
 *		ImmGetCandidateListA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1004
 */
1005
DWORD WINAPI ImmGetCandidateListA(
1006
  HIMC hIMC, DWORD dwIndex,
Alexandre Julliard's avatar
Alexandre Julliard committed
1007
  LPCANDIDATELIST lpCandList, DWORD dwBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1008
{
1009
    InputContextData *data = get_imc_data(hIMC);
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
    LPCANDIDATEINFO candinfo;
    LPCANDIDATELIST candlist;
    DWORD ret = 0;

    TRACE("%p, %d, %p, %d\n", hIMC, dwIndex, lpCandList, dwBufLen);

    if (!data || !data->IMC.hCandInfo)
       return 0;

    candinfo = ImmLockIMCC(data->IMC.hCandInfo);
1020
    if (dwIndex >= candinfo->dwCount || dwIndex >= ARRAY_SIZE(candinfo->dwOffset))
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
        goto done;

    candlist = (LPCANDIDATELIST)((LPBYTE)candinfo + candinfo->dwOffset[dwIndex]);
    if ( !candlist->dwSize || !candlist->dwCount )
        goto done;

    if ( !is_himc_ime_unicode(data) )
    {
        ret = candlist->dwSize;
        if ( lpCandList && dwBufLen >= ret )
            memcpy(lpCandList, candlist, ret);
    }
    else
        ret = convert_candidatelist_WtoA( candlist, lpCandList, dwBufLen);

done:
    ImmUnlockIMCC(data->IMC.hCandInfo);
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1039 1040 1041
}

/***********************************************************************
1042
 *		ImmGetCandidateListCountA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1043
 */
1044 1045
DWORD WINAPI ImmGetCandidateListCountA(
  HIMC hIMC, LPDWORD lpdwListCount)
Alexandre Julliard's avatar
Alexandre Julliard committed
1046
{
1047
    InputContextData *data = get_imc_data(hIMC);
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    LPCANDIDATEINFO candinfo;
    DWORD ret, count;

    TRACE("%p, %p\n", hIMC, lpdwListCount);

    if (!data || !lpdwListCount || !data->IMC.hCandInfo)
       return 0;

    candinfo = ImmLockIMCC(data->IMC.hCandInfo);

    *lpdwListCount = count = candinfo->dwCount;

    if ( !is_himc_ime_unicode(data) )
        ret = candinfo->dwSize;
    else
    {
        ret = sizeof(CANDIDATEINFO);
        while ( count-- )
            ret += ImmGetCandidateListA(hIMC, count, NULL, 0);
    }

    ImmUnlockIMCC(data->IMC.hCandInfo);
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1071 1072 1073
}

/***********************************************************************
1074
 *		ImmGetCandidateListCountW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1075
 */
1076 1077
DWORD WINAPI ImmGetCandidateListCountW(
  HIMC hIMC, LPDWORD lpdwListCount)
Alexandre Julliard's avatar
Alexandre Julliard committed
1078
{
1079
    InputContextData *data = get_imc_data(hIMC);
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
    LPCANDIDATEINFO candinfo;
    DWORD ret, count;

    TRACE("%p, %p\n", hIMC, lpdwListCount);

    if (!data || !lpdwListCount || !data->IMC.hCandInfo)
       return 0;

    candinfo = ImmLockIMCC(data->IMC.hCandInfo);

    *lpdwListCount = count = candinfo->dwCount;

    if ( is_himc_ime_unicode(data) )
        ret = candinfo->dwSize;
    else
    {
        ret = sizeof(CANDIDATEINFO);
        while ( count-- )
            ret += ImmGetCandidateListW(hIMC, count, NULL, 0);
    }

    ImmUnlockIMCC(data->IMC.hCandInfo);
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1103 1104 1105
}

/***********************************************************************
1106
 *		ImmGetCandidateListW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1107
 */
1108
DWORD WINAPI ImmGetCandidateListW(
1109
  HIMC hIMC, DWORD dwIndex,
Alexandre Julliard's avatar
Alexandre Julliard committed
1110
  LPCANDIDATELIST lpCandList, DWORD dwBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1111
{
1112
    InputContextData *data = get_imc_data(hIMC);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    LPCANDIDATEINFO candinfo;
    LPCANDIDATELIST candlist;
    DWORD ret = 0;

    TRACE("%p, %d, %p, %d\n", hIMC, dwIndex, lpCandList, dwBufLen);

    if (!data || !data->IMC.hCandInfo)
       return 0;

    candinfo = ImmLockIMCC(data->IMC.hCandInfo);
1123
    if (dwIndex >= candinfo->dwCount || dwIndex >= ARRAY_SIZE(candinfo->dwOffset))
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
        goto done;

    candlist = (LPCANDIDATELIST)((LPBYTE)candinfo + candinfo->dwOffset[dwIndex]);
    if ( !candlist->dwSize || !candlist->dwCount )
        goto done;

    if ( is_himc_ime_unicode(data) )
    {
        ret = candlist->dwSize;
        if ( lpCandList && dwBufLen >= ret )
            memcpy(lpCandList, candlist, ret);
    }
    else
        ret = convert_candidatelist_AtoW( candlist, lpCandList, dwBufLen);

done:
    ImmUnlockIMCC(data->IMC.hCandInfo);
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1142 1143 1144
}

/***********************************************************************
1145
 *		ImmGetCandidateWindow (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1146
 */
1147
BOOL WINAPI ImmGetCandidateWindow(
1148
  HIMC hIMC, DWORD dwIndex, LPCANDIDATEFORM lpCandidate)
Alexandre Julliard's avatar
Alexandre Julliard committed
1149
{
1150
    InputContextData *data = get_imc_data(hIMC);
1151 1152 1153 1154 1155 1156

    TRACE("%p, %d, %p\n", hIMC, dwIndex, lpCandidate);

    if (!data || !lpCandidate)
        return FALSE;

1157
    if (dwIndex >= ARRAY_SIZE(data->IMC.cfCandForm))
1158 1159
        return FALSE;

1160 1161 1162
    if (data->IMC.cfCandForm[dwIndex].dwIndex != dwIndex)
        return FALSE;

1163 1164 1165
    *lpCandidate = data->IMC.cfCandForm[dwIndex];

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1166 1167 1168
}

/***********************************************************************
1169
 *		ImmGetCompositionFontA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1170
 */
1171
BOOL WINAPI ImmGetCompositionFontA(HIMC hIMC, LPLOGFONTA lplf)
Alexandre Julliard's avatar
Alexandre Julliard committed
1172
{
1173 1174 1175 1176 1177 1178
    LOGFONTW lfW;
    BOOL rc;

    TRACE("(%p, %p):\n", hIMC, lplf);

    rc = ImmGetCompositionFontW(hIMC,&lfW);
1179 1180 1181 1182 1183
    if (!rc || !lplf)
        return FALSE;

    memcpy(lplf,&lfW,sizeof(LOGFONTA));
    WideCharToMultiByte(CP_ACP, 0, lfW.lfFaceName, -1, lplf->lfFaceName,
1184
                        LF_FACESIZE, NULL, NULL);
1185
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1186 1187 1188
}

/***********************************************************************
1189
 *		ImmGetCompositionFontW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1190
 */
1191
BOOL WINAPI ImmGetCompositionFontW(HIMC hIMC, LPLOGFONTW lplf)
Alexandre Julliard's avatar
Alexandre Julliard committed
1192
{
1193
    InputContextData *data = get_imc_data(hIMC);
1194 1195 1196

    TRACE("(%p, %p):\n", hIMC, lplf);

1197
    if (!data || !lplf)
1198 1199 1200 1201 1202
        return FALSE;

    *lplf = data->IMC.lfFont.W;

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1203 1204
}

1205 1206 1207

/* Helpers for the GetCompositionString functions */

1208 1209 1210 1211
/* Source encoding is defined by context, source length is always given in respective characters. Destination buffer
   length is always in bytes. */
static INT CopyCompStringIMEtoClient(const InputContextData *data, const void *src, INT src_len, void *dst,
        INT dst_len, BOOL unicode)
1212
{
1213 1214
    int char_size = unicode ? sizeof(WCHAR) : sizeof(char);
    INT ret;
1215

1216 1217 1218
    if (is_himc_ime_unicode(data) ^ unicode)
    {
        if (unicode)
1219
            ret = MultiByteToWideChar(CP_ACP, 0, src, src_len, dst, dst_len / sizeof(WCHAR));
1220 1221 1222 1223
        else
            ret = WideCharToMultiByte(CP_ACP, 0, src, src_len, dst, dst_len, NULL, NULL);
        ret *= char_size;
    }
1224 1225
    else
    {
1226 1227 1228 1229 1230 1231 1232
        if (dst_len)
        {
            ret = min(src_len * char_size, dst_len);
            memcpy(dst, src, ret);
        }
        else
            ret = src_len * char_size;
1233 1234
    }

1235
    return ret;
1236 1237
}

1238 1239 1240 1241
/* Composition string encoding is defined by context, returned attributes correspond to string, converted according to
   passed mode. String length is in characters, attributes are in byte arrays. */
static INT CopyCompAttrIMEtoClient(const InputContextData *data, const BYTE *src, INT src_len, const void *comp_string,
        INT str_len, BYTE *dst, INT dst_len, BOOL unicode)
1242
{
1243 1244 1245 1246 1247 1248
    union
    {
        const void *str;
        const WCHAR *strW;
        const char *strA;
    } string;
1249 1250
    INT rc;

1251 1252
    string.str = comp_string;

1253 1254
    if (is_himc_ime_unicode(data) && !unicode)
    {
1255 1256
        rc = WideCharToMultiByte(CP_ACP, 0, string.strW, str_len, NULL, 0, NULL, NULL);
        if (dst_len)
1257 1258 1259
        {
            int i, j = 0, k = 0;

1260 1261 1262
            if (rc < dst_len)
                dst_len = rc;
            for (i = 0; i < str_len; ++i)
1263 1264 1265
            {
                int len;

1266
                len = WideCharToMultiByte(CP_ACP, 0, string.strW + i, 1, NULL, 0, NULL, NULL);
1267 1268 1269 1270
                for (; len > 0; --len)
                {
                    dst[j++] = src[k];

1271
                    if (j >= dst_len)
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
                        goto end;
                }
                ++k;
            }
        end:
            rc = j;
        }
    }
    else if (!is_himc_ime_unicode(data) && unicode)
    {
1282 1283
        rc = MultiByteToWideChar(CP_ACP, 0, string.strA, str_len, NULL, 0);
        if (dst_len)
1284 1285 1286
        {
            int i, j = 0;

1287 1288 1289
            if (rc < dst_len)
                dst_len = rc;
            for (i = 0; i < str_len; ++i)
1290
            {
1291
                if (IsDBCSLeadByte(string.strA[i]))
1292 1293 1294 1295
                    continue;

                dst[j++] = src[i];

1296
                if (j >= dst_len)
1297 1298 1299 1300 1301 1302 1303
                    break;
            }
            rc = j;
        }
    }
    else
    {
1304 1305
        memcpy(dst, src, min(src_len, dst_len));
        rc = src_len;
1306
    }
1307

1308
    return rc;
1309 1310
}

1311
static INT CopyCompClauseIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE ssource,
1312 1313
                                     LPBYTE target, INT tlen, BOOL unicode )
{
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
    INT rc;

    if (is_himc_ime_unicode(data) && !unicode)
    {
        if (tlen)
        {
            int i;

            if (slen < tlen)
                tlen = slen;
            tlen /= sizeof (DWORD);
            for (i = 0; i < tlen; ++i)
            {
                ((DWORD *)target)[i] = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource,
                                                          ((DWORD *)source)[i],
                                                          NULL, 0,
                                                          NULL, NULL);
            }
            rc = sizeof (DWORD) * i;
        }
        else
            rc = slen;
    }
    else if (!is_himc_ime_unicode(data) && unicode)
    {
        if (tlen)
        {
            int i;

            if (slen < tlen)
                tlen = slen;
            tlen /= sizeof (DWORD);
            for (i = 0; i < tlen; ++i)
            {
                ((DWORD *)target)[i] = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource,
                                                          ((DWORD *)source)[i],
                                                          NULL, 0);
            }
            rc = sizeof (DWORD) * i;
        }
        else
            rc = slen;
    }
    else
    {
        memcpy( target, source, min(slen,tlen));
        rc = slen;
    }
1362

1363
    return rc;
1364 1365 1366 1367
}

static INT CopyCompOffsetIMEtoClient(InputContextData *data, DWORD offset, LPBYTE ssource, BOOL unicode)
{
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
    int rc;

    if (is_himc_ime_unicode(data) && !unicode)
    {
        rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource, offset, NULL, 0, NULL, NULL);
    }
    else if (!is_himc_ime_unicode(data) && unicode)
    {
        rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource, offset, NULL, 0);
    }
    else
        rc = offset;

    return rc;
1382 1383 1384 1385
}

static LONG ImmGetCompositionStringT( HIMC hIMC, DWORD dwIndex, LPVOID lpBuf,
                                      DWORD dwBufLen, BOOL unicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
1386
{
1387
    LONG rc = 0;
1388
    InputContextData *data = get_imc_data(hIMC);
1389 1390
    LPCOMPOSITIONSTRING compstr;
    LPBYTE compdata;
1391

1392
    TRACE("(%p, 0x%x, %p, %d)\n", hIMC, dwIndex, lpBuf, dwBufLen);
1393

1394 1395
    if (!data)
       return FALSE;
1396

1397 1398 1399 1400 1401 1402
    if (!data->IMC.hCompStr)
       return FALSE;

    compdata = ImmLockIMCC(data->IMC.hCompStr);
    compstr = (LPCOMPOSITIONSTRING)compdata;

1403
    switch (dwIndex)
1404
    {
1405
    case GCS_RESULTSTR:
1406 1407
        TRACE("GCS_RESULTSTR\n");
        rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwResultStrOffset, compstr->dwResultStrLen, lpBuf, dwBufLen, unicode);
1408 1409
        break;
    case GCS_COMPSTR:
1410 1411
        TRACE("GCS_COMPSTR\n");
        rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen, lpBuf, dwBufLen, unicode);
1412 1413
        break;
    case GCS_COMPATTR:
1414 1415 1416 1417
        TRACE("GCS_COMPATTR\n");
        rc = CopyCompAttrIMEtoClient(data, compdata + compstr->dwCompAttrOffset, compstr->dwCompAttrLen,
                                     compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen,
                                     lpBuf, dwBufLen, unicode);
1418 1419
        break;
    case GCS_COMPCLAUSE:
1420 1421
        TRACE("GCS_COMPCLAUSE\n");
        rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwCompClauseOffset,compstr->dwCompClauseLen,
1422
                                       compdata + compstr->dwCompStrOffset,
1423
                                       lpBuf, dwBufLen, unicode);
1424 1425
        break;
    case GCS_RESULTCLAUSE:
1426 1427
        TRACE("GCS_RESULTCLAUSE\n");
        rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwResultClauseOffset,compstr->dwResultClauseLen,
1428
                                       compdata + compstr->dwResultStrOffset,
1429
                                       lpBuf, dwBufLen, unicode);
1430
        break;
1431
    case GCS_RESULTREADSTR:
1432 1433
        TRACE("GCS_RESULTREADSTR\n");
        rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwResultReadStrOffset, compstr->dwResultReadStrLen, lpBuf, dwBufLen, unicode);
1434 1435
        break;
    case GCS_RESULTREADCLAUSE:
1436 1437
        TRACE("GCS_RESULTREADCLAUSE\n");
        rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwResultReadClauseOffset,compstr->dwResultReadClauseLen,
1438
                                       compdata + compstr->dwResultStrOffset,
1439
                                       lpBuf, dwBufLen, unicode);
1440 1441
        break;
    case GCS_COMPREADSTR:
1442 1443
        TRACE("GCS_COMPREADSTR\n");
        rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwCompReadStrOffset, compstr->dwCompReadStrLen, lpBuf, dwBufLen, unicode);
1444 1445
        break;
    case GCS_COMPREADATTR:
1446 1447 1448 1449
        TRACE("GCS_COMPREADATTR\n");
        rc = CopyCompAttrIMEtoClient(data, compdata + compstr->dwCompReadAttrOffset, compstr->dwCompReadAttrLen,
                                     compdata + compstr->dwCompReadStrOffset, compstr->dwCompReadStrLen,
                                     lpBuf, dwBufLen, unicode);
1450 1451
        break;
    case GCS_COMPREADCLAUSE:
1452 1453
        TRACE("GCS_COMPREADCLAUSE\n");
        rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwCompReadClauseOffset,compstr->dwCompReadClauseLen,
1454
                                       compdata + compstr->dwCompStrOffset,
1455
                                       lpBuf, dwBufLen, unicode);
1456
        break;
1457
    case GCS_CURSORPOS:
Kusanagi Kouichi's avatar
Kusanagi Kouichi committed
1458
        TRACE("GCS_CURSORPOS\n");
1459
        rc = CopyCompOffsetIMEtoClient(data, compstr->dwCursorPos, compdata + compstr->dwCompStrOffset, unicode);
1460 1461
        break;
    case GCS_DELTASTART:
1462
        TRACE("GCS_DELTASTART\n");
1463
        rc = CopyCompOffsetIMEtoClient(data, compstr->dwDeltaStart, compdata + compstr->dwCompStrOffset, unicode);
1464 1465
        break;
    default:
1466
        FIXME("Unhandled index 0x%x\n",dwIndex);
1467
        break;
1468
    }
1469

1470 1471
    ImmUnlockIMCC(data->IMC.hCompStr);

1472
    return rc;
Alexandre Julliard's avatar
Alexandre Julliard committed
1473 1474
}

1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
/***********************************************************************
 *		ImmGetCompositionStringA (IMM32.@)
 */
LONG WINAPI ImmGetCompositionStringA(
  HIMC hIMC, DWORD dwIndex, LPVOID lpBuf, DWORD dwBufLen)
{
    return ImmGetCompositionStringT(hIMC, dwIndex, lpBuf, dwBufLen, FALSE);
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1485
/***********************************************************************
1486
 *		ImmGetCompositionStringW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1487
 */
1488
LONG WINAPI ImmGetCompositionStringW(
1489
  HIMC hIMC, DWORD dwIndex,
Alexandre Julliard's avatar
Alexandre Julliard committed
1490
  LPVOID lpBuf, DWORD dwBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1491
{
1492
    return ImmGetCompositionStringT(hIMC, dwIndex, lpBuf, dwBufLen, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1493 1494 1495
}

/***********************************************************************
1496
 *		ImmGetCompositionWindow (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1497
 */
1498
BOOL WINAPI ImmGetCompositionWindow(HIMC hIMC, LPCOMPOSITIONFORM lpCompForm)
Alexandre Julliard's avatar
Alexandre Julliard committed
1499
{
1500
    InputContextData *data = get_imc_data(hIMC);
1501 1502 1503 1504 1505 1506

    TRACE("(%p, %p)\n", hIMC, lpCompForm);

    if (!data)
        return FALSE;

1507
    *lpCompForm = data->IMC.cfCompForm;
1508
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1509 1510 1511
}

/***********************************************************************
1512
 *		ImmGetContext (IMM32.@)
1513
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1514
 */
1515
HIMC WINAPI ImmGetContext(HWND hWnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
1516
{
1517
    HIMC rc;
1518

1519
    TRACE("%p\n", hWnd);
1520 1521 1522 1523 1524 1525

    if (!IsWindow(hWnd))
    {
        SetLastError(ERROR_INVALID_WINDOW_HANDLE);
        return NULL;
    }
1526

1527
    rc = GetPropW(hWnd,szwWineIMCProperty);
1528 1529 1530
    if (rc == (HIMC)-1)
        rc = NULL;
    else if (rc == NULL)
1531
        rc = get_default_context( hWnd );
1532

1533 1534
    if (rc)
    {
1535
        InputContextData *data = rc;
1536 1537
        data->IMC.hWnd = hWnd;
    }
1538

1539 1540 1541
    TRACE("returning %p\n", rc);

    return rc;
Alexandre Julliard's avatar
Alexandre Julliard committed
1542 1543 1544
}

/***********************************************************************
1545
 *		ImmGetConversionListA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1546
 */
1547 1548
DWORD WINAPI ImmGetConversionListA(
  HKL hKL, HIMC hIMC,
Alexandre Julliard's avatar
Alexandre Julliard committed
1549
  LPCSTR pSrc, LPCANDIDATELIST lpDst,
1550
  DWORD dwBufLen, UINT uFlag)
Alexandre Julliard's avatar
Alexandre Julliard committed
1551
{
1552 1553 1554 1555 1556 1557 1558 1559 1560
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %p, %s, %p, %d, %d):\n", hKL, hIMC, debugstr_a(pSrc), lpDst,
                dwBufLen, uFlag);
    if (immHkl->hIME && immHkl->pImeConversionList)
    {
        if (!is_kbd_ime_unicode(immHkl))
            return immHkl->pImeConversionList(hIMC,(LPCWSTR)pSrc,lpDst,dwBufLen,uFlag);
        else
        {
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
            LPCANDIDATELIST lpwDst;
            DWORD ret = 0, len;
            LPWSTR pwSrc = strdupAtoW(pSrc);

            len = immHkl->pImeConversionList(hIMC, pwSrc, NULL, 0, uFlag);
            lpwDst = HeapAlloc(GetProcessHeap(), 0, len);
            if ( lpwDst )
            {
                immHkl->pImeConversionList(hIMC, pwSrc, lpwDst, len, uFlag);
                ret = convert_candidatelist_WtoA( lpwDst, lpDst, dwBufLen);
                HeapFree(GetProcessHeap(), 0, lpwDst);
            }
            HeapFree(GetProcessHeap(), 0, pwSrc);

            return ret;
1576 1577 1578 1579
        }
    }
    else
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1580 1581 1582
}

/***********************************************************************
1583
 *		ImmGetConversionListW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1584
 */
1585 1586
DWORD WINAPI ImmGetConversionListW(
  HKL hKL, HIMC hIMC,
Alexandre Julliard's avatar
Alexandre Julliard committed
1587
  LPCWSTR pSrc, LPCANDIDATELIST lpDst,
1588
  DWORD dwBufLen, UINT uFlag)
Alexandre Julliard's avatar
Alexandre Julliard committed
1589
{
1590 1591 1592 1593 1594 1595 1596 1597 1598
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %p, %s, %p, %d, %d):\n", hKL, hIMC, debugstr_w(pSrc), lpDst,
                dwBufLen, uFlag);
    if (immHkl->hIME && immHkl->pImeConversionList)
    {
        if (is_kbd_ime_unicode(immHkl))
            return immHkl->pImeConversionList(hIMC,pSrc,lpDst,dwBufLen,uFlag);
        else
        {
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
            LPCANDIDATELIST lpaDst;
            DWORD ret = 0, len;
            LPSTR paSrc = strdupWtoA(pSrc);

            len = immHkl->pImeConversionList(hIMC, (LPCWSTR)paSrc, NULL, 0, uFlag);
            lpaDst = HeapAlloc(GetProcessHeap(), 0, len);
            if ( lpaDst )
            {
                immHkl->pImeConversionList(hIMC, (LPCWSTR)paSrc, lpaDst, len, uFlag);
                ret = convert_candidatelist_AtoW( lpaDst, lpDst, dwBufLen);
                HeapFree(GetProcessHeap(), 0, lpaDst);
            }
            HeapFree(GetProcessHeap(), 0, paSrc);

            return ret;
1614 1615 1616 1617
        }
    }
    else
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1618 1619 1620
}

/***********************************************************************
1621
 *		ImmGetConversionStatus (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1622
 */
1623 1624
BOOL WINAPI ImmGetConversionStatus(
  HIMC hIMC, LPDWORD lpfdwConversion, LPDWORD lpfdwSentence)
Alexandre Julliard's avatar
Alexandre Julliard committed
1625
{
1626
    InputContextData *data = get_imc_data(hIMC);
1627 1628 1629 1630 1631 1632

    TRACE("%p %p %p\n", hIMC, lpfdwConversion, lpfdwSentence);

    if (!data)
        return FALSE;

1633
    if (lpfdwConversion)
1634
        *lpfdwConversion = data->IMC.fdwConversion;
1635
    if (lpfdwSentence)
1636 1637
        *lpfdwSentence = data->IMC.fdwSentence;

1638
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1639 1640
}

1641 1642 1643 1644
static BOOL needs_ime_window(HWND hwnd)
{
    WCHAR classW[8];

1645
    if (GetClassNameW(hwnd, classW, ARRAY_SIZE(classW)) && !lstrcmpW(classW, szwIME))
1646 1647 1648 1649 1650 1651
        return FALSE;
    if (GetClassLongPtrW(hwnd, GCL_STYLE) & CS_IME) return FALSE;

    return TRUE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1652
/***********************************************************************
1653
 *		__wine_register_window (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1654
 */
1655
BOOL WINAPI __wine_register_window(HWND hwnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
1656
{
1657 1658 1659 1660 1661 1662 1663 1664
    HWND new = NULL;
    IMMThreadData *thread_data;
    TRACE("(%p)\n", hwnd);

    if (!needs_ime_window(hwnd))
        return FALSE;

    thread_data = IMM_GetThreadData(hwnd, 0);
1665
    if (!thread_data)
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
        return FALSE;

    if (thread_data->disableIME || disable_ime)
    {
        TRACE("IME for this thread is disabled\n");
        LeaveCriticalSection(&threaddata_cs);
        return FALSE;
    }
    thread_data->windowRefs++;
    TRACE("windowRefs=%u, hwndDefault=%p\n",
          thread_data->windowRefs, thread_data->hwndDefault);

    /* Create default IME window */
    if (thread_data->windowRefs == 1)
1680 1681 1682
    {
        /* Do not create the window inside of a critical section */
        LeaveCriticalSection(&threaddata_cs);
1683 1684 1685
        new = CreateWindowExW( 0, szwIME, szwDefaultIME,
                               WS_POPUP | WS_DISABLED | WS_CLIPSIBLINGS,
                               0, 0, 1, 1, 0, 0, 0, 0);
1686 1687
        /* thread_data is in the current thread so we can assume it's still valid */
        EnterCriticalSection(&threaddata_cs);
1688 1689 1690 1691 1692
        /* See if anyone beat us */
        if (thread_data->hwndDefault == NULL)
        {
            thread_data->hwndDefault = new;
            new = NULL;
1693
            TRACE("Default is %p\n", thread_data->hwndDefault);
1694 1695
        }
    }
1696

1697
    LeaveCriticalSection(&threaddata_cs);
1698

1699 1700 1701
    /* Clean up an unused new window outside of the critical section */
    if (new != NULL)
        DestroyWindow(new);
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
    return TRUE;
}

/***********************************************************************
 *		__wine_unregister_window (IMM32.@)
 */
void WINAPI __wine_unregister_window(HWND hwnd)
{
    HWND to_destroy = 0;
    IMMThreadData *thread_data;
    TRACE("(%p)\n", hwnd);

    thread_data = IMM_GetThreadData(hwnd, 0);
    if (!thread_data) return;

    thread_data->windowRefs--;
    TRACE("windowRefs=%u, hwndDefault=%p\n",
          thread_data->windowRefs, thread_data->hwndDefault);

    /* Destroy default IME window */
    if (thread_data->windowRefs == 0 && thread_data->hwndDefault)
    {
        to_destroy = thread_data->hwndDefault;
        thread_data->hwndDefault = NULL;
1726
    }
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
    LeaveCriticalSection(&threaddata_cs);

    if (to_destroy) DestroyWindow( to_destroy );
}

/***********************************************************************
 *		ImmGetDefaultIMEWnd (IMM32.@)
 */
HWND WINAPI ImmGetDefaultIMEWnd(HWND hWnd)
{
    HWND ret;
    IMMThreadData* thread_data = IMM_GetThreadData(hWnd, 0);
    if (!thread_data)
        return NULL;
    ret = thread_data->hwndDefault;
    LeaveCriticalSection(&threaddata_cs);
1743
    TRACE("Default is %p\n",ret);
1744
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1745 1746 1747
}

/***********************************************************************
1748
 *		ImmGetDescriptionA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1749
 */
1750 1751
UINT WINAPI ImmGetDescriptionA(
  HKL hKL, LPSTR lpszDescription, UINT uBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1752
{
1753 1754 1755 1756 1757 1758 1759
  WCHAR *buf;
  DWORD len;

  TRACE("%p %p %d\n", hKL, lpszDescription, uBufLen);

  /* find out how many characters in the unicode buffer */
  len = ImmGetDescriptionW( hKL, NULL, 0 );
1760 1761
  if (!len)
    return 0;
1762 1763 1764 1765

  /* allocate a buffer of that size */
  buf = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof (WCHAR) );
  if( !buf )
Alexandre Julliard's avatar
Alexandre Julliard committed
1766
  return 0;
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776

  /* fetch the unicode buffer */
  len = ImmGetDescriptionW( hKL, buf, len + 1 );

  /* convert it back to ASCII */
  len = WideCharToMultiByte( CP_ACP, 0, buf, len + 1,
                             lpszDescription, uBufLen, NULL, NULL );

  HeapFree( GetProcessHeap(), 0, buf );

1777 1778 1779 1780
  if (len == 0)
    return 0;

  return len - 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1781 1782 1783
}

/***********************************************************************
1784
 *		ImmGetDescriptionW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1785
 */
1786
UINT WINAPI ImmGetDescriptionW(HKL hKL, LPWSTR lpszDescription, UINT uBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1787
{
1788 1789 1790 1791
  static const WCHAR name[] = { 'W','i','n','e',' ','X','I','M',0 };

  FIXME("(%p, %p, %d): semi stub\n", hKL, lpszDescription, uBufLen);

1792
  if (!hKL) return 0;
1793 1794 1795
  if (!uBufLen) return lstrlenW( name );
  lstrcpynW( lpszDescription, name, uBufLen );
  return lstrlenW( lpszDescription );
Alexandre Julliard's avatar
Alexandre Julliard committed
1796 1797 1798
}

/***********************************************************************
1799
 *		ImmGetGuideLineA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1800
 */
1801 1802
DWORD WINAPI ImmGetGuideLineA(
  HIMC hIMC, DWORD dwIndex, LPSTR lpBuf, DWORD dwBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1803
{
1804
  FIXME("(%p, %d, %s, %d): stub\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1805 1806 1807 1808
    hIMC, dwIndex, debugstr_a(lpBuf), dwBufLen
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1809 1810 1811
}

/***********************************************************************
1812
 *		ImmGetGuideLineW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1813
 */
1814
DWORD WINAPI ImmGetGuideLineW(HIMC hIMC, DWORD dwIndex, LPWSTR lpBuf, DWORD dwBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1815
{
1816
  FIXME("(%p, %d, %s, %d): stub\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1817 1818 1819 1820
    hIMC, dwIndex, debugstr_w(lpBuf), dwBufLen
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1821 1822
}

1823 1824 1825
/***********************************************************************
 *		ImmGetIMEFileNameA (IMM32.@)
 */
1826
UINT WINAPI ImmGetIMEFileNameA( HKL hKL, LPSTR lpszFileName, UINT uBufLen)
1827
{
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
    LPWSTR bufW = NULL;
    UINT wBufLen = uBufLen;
    UINT rc;

    if (uBufLen && lpszFileName)
        bufW = HeapAlloc(GetProcessHeap(),0,uBufLen * sizeof(WCHAR));
    else /* We need this to get the number of byte required */
    {
        bufW = HeapAlloc(GetProcessHeap(),0,MAX_PATH * sizeof(WCHAR));
        wBufLen = MAX_PATH;
    }

    rc = ImmGetIMEFileNameW(hKL,bufW,wBufLen);

    if (rc > 0)
    {
        if (uBufLen && lpszFileName)
            rc = WideCharToMultiByte(CP_ACP, 0, bufW, -1, lpszFileName,
                                 uBufLen, NULL, NULL);
        else /* get the length */
            rc = WideCharToMultiByte(CP_ACP, 0, bufW, -1, NULL, 0, NULL,
                                     NULL);
    }

    HeapFree(GetProcessHeap(),0,bufW);
    return rc;
1854 1855 1856 1857 1858
}

/***********************************************************************
 *		ImmGetIMEFileNameW (IMM32.@)
 */
1859
UINT WINAPI ImmGetIMEFileNameW(HKL hKL, LPWSTR lpszFileName, UINT uBufLen)
1860
{
1861 1862 1863
    HKEY hkey;
    DWORD length;
    DWORD rc;
1864
    WCHAR regKey[ARRAY_SIZE(szImeRegFmt)+8];
1865

1866
    wsprintfW( regKey, szImeRegFmt, (ULONG_PTR)hKL );
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
    rc = RegOpenKeyW( HKEY_LOCAL_MACHINE, regKey, &hkey);
    if (rc != ERROR_SUCCESS)
    {
        SetLastError(rc);
        return 0;
    }

    length = 0;
    rc = RegGetValueW(hkey, NULL, szImeFileW, RRF_RT_REG_SZ, NULL, NULL, &length);

    if (rc != ERROR_SUCCESS)
    {
        RegCloseKey(hkey);
        SetLastError(rc);
        return 0;
    }
    if (length > uBufLen * sizeof(WCHAR) || !lpszFileName)
    {
        RegCloseKey(hkey);
        if (lpszFileName)
        {
            SetLastError(ERROR_INSUFFICIENT_BUFFER);
            return 0;
        }
        else
            return length / sizeof(WCHAR);
    }

    RegGetValueW(hkey, NULL, szImeFileW, RRF_RT_REG_SZ, NULL, lpszFileName, &length);

    RegCloseKey(hkey);

    return length / sizeof(WCHAR);
1900 1901
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1902
/***********************************************************************
1903
 *		ImmGetOpenStatus (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1904
 */
1905
BOOL WINAPI ImmGetOpenStatus(HIMC hIMC)
Alexandre Julliard's avatar
Alexandre Julliard committed
1906
{
1907
  InputContextData *data = get_imc_data(hIMC);
1908
  static int i;
1909

1910 1911
    if (!data)
        return FALSE;
1912 1913 1914 1915 1916

    TRACE("(%p): semi-stub\n", hIMC);

    if (!i++)
      FIXME("(%p): semi-stub\n", hIMC);
1917

1918
  return data->IMC.fOpen;
Alexandre Julliard's avatar
Alexandre Julliard committed
1919 1920
}

1921 1922 1923 1924 1925
/***********************************************************************
 *		ImmGetProperty (IMM32.@)
 */
DWORD WINAPI ImmGetProperty(HKL hKL, DWORD fdwIndex)
{
1926
    DWORD rc = 0;
1927 1928
    ImmHkl *kbd;

1929
    TRACE("(%p, %d)\n", hKL, fdwIndex);
1930
    kbd = IMM_GetImmHkl(hKL);
1931

1932
    if (kbd && kbd->hIME)
1933
    {
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
        switch (fdwIndex)
        {
            case IGP_PROPERTY: rc = kbd->imeInfo.fdwProperty; break;
            case IGP_CONVERSION: rc = kbd->imeInfo.fdwConversionCaps; break;
            case IGP_SENTENCE: rc = kbd->imeInfo.fdwSentenceCaps; break;
            case IGP_SETCOMPSTR: rc = kbd->imeInfo.fdwSCSCaps; break;
            case IGP_SELECT: rc = kbd->imeInfo.fdwSelectCaps; break;
            case IGP_GETIMEVERSION: rc = IMEVER_0400; break;
            case IGP_UI: rc = 0; break;
            default: rc = 0;
        }
1945 1946
    }
    return rc;
1947 1948 1949 1950 1951 1952 1953 1954
}

/***********************************************************************
 *		ImmGetRegisterWordStyleA (IMM32.@)
 */
UINT WINAPI ImmGetRegisterWordStyleA(
  HKL hKL, UINT nItem, LPSTYLEBUFA lpStyleBuf)
{
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %d, %p):\n", hKL, nItem, lpStyleBuf);
    if (immHkl->hIME && immHkl->pImeGetRegisterWordStyle)
    {
        if (!is_kbd_ime_unicode(immHkl))
            return immHkl->pImeGetRegisterWordStyle(nItem,(LPSTYLEBUFW)lpStyleBuf);
        else
        {
            STYLEBUFW sbw;
            UINT rc;

            rc = immHkl->pImeGetRegisterWordStyle(nItem,&sbw);
            WideCharToMultiByte(CP_ACP, 0, sbw.szDescription, -1,
                lpStyleBuf->szDescription, 32, NULL, NULL);
            lpStyleBuf->dwStyle = sbw.dwStyle;
            return rc;
        }
    }
    else
        return 0;
1975 1976 1977 1978 1979 1980 1981 1982
}

/***********************************************************************
 *		ImmGetRegisterWordStyleW (IMM32.@)
 */
UINT WINAPI ImmGetRegisterWordStyleW(
  HKL hKL, UINT nItem, LPSTYLEBUFW lpStyleBuf)
{
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %d, %p):\n", hKL, nItem, lpStyleBuf);
    if (immHkl->hIME && immHkl->pImeGetRegisterWordStyle)
    {
        if (is_kbd_ime_unicode(immHkl))
            return immHkl->pImeGetRegisterWordStyle(nItem,lpStyleBuf);
        else
        {
            STYLEBUFA sba;
            UINT rc;

            rc = immHkl->pImeGetRegisterWordStyle(nItem,(LPSTYLEBUFW)&sba);
            MultiByteToWideChar(CP_ACP, 0, sba.szDescription, -1,
                lpStyleBuf->szDescription, 32);
            lpStyleBuf->dwStyle = sba.dwStyle;
            return rc;
        }
    }
    else
        return 0;
2003 2004
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2005
/***********************************************************************
2006
 *		ImmGetStatusWindowPos (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2007
 */
2008
BOOL WINAPI ImmGetStatusWindowPos(HIMC hIMC, LPPOINT lpptPos)
Alexandre Julliard's avatar
Alexandre Julliard committed
2009
{
2010
    InputContextData *data = get_imc_data(hIMC);
2011 2012 2013 2014 2015 2016 2017 2018 2019

    TRACE("(%p, %p)\n", hIMC, lpptPos);

    if (!data || !lpptPos)
        return FALSE;

    *lpptPos = data->IMC.ptStatusWndPos;

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2020 2021 2022
}

/***********************************************************************
2023
 *		ImmGetVirtualKey (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2024
 */
2025
UINT WINAPI ImmGetVirtualKey(HWND hWnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
2026
{
2027
  OSVERSIONINFOA version;
2028
  InputContextData *data = ImmGetContext( hWnd );
2029 2030 2031 2032 2033
  TRACE("%p\n", hWnd);

  if ( data )
      return data->lastVK;

2034
  version.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
2035 2036 2037 2038
  GetVersionExA( &version );
  switch(version.dwPlatformId)
  {
  case VER_PLATFORM_WIN32_WINDOWS:
Alexandre Julliard's avatar
Alexandre Julliard committed
2039
      return VK_PROCESSKEY;
2040
  case VER_PLATFORM_WIN32_NT:
Alexandre Julliard's avatar
Alexandre Julliard committed
2041
      return 0;
2042
  default:
2043
      FIXME("%d not supported\n",version.dwPlatformId);
2044 2045
      return VK_PROCESSKEY;
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
2046 2047
}

2048 2049 2050 2051 2052 2053
/***********************************************************************
 *		ImmInstallIMEA (IMM32.@)
 */
HKL WINAPI ImmInstallIMEA(
  LPCSTR lpszIMEFileName, LPCSTR lpszLayoutText)
{
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
    LPWSTR lpszwIMEFileName;
    LPWSTR lpszwLayoutText;
    HKL hkl;

    TRACE ("(%s, %s)\n", debugstr_a(lpszIMEFileName),
                         debugstr_a(lpszLayoutText));

    lpszwIMEFileName = strdupAtoW(lpszIMEFileName);
    lpszwLayoutText = strdupAtoW(lpszLayoutText);

    hkl = ImmInstallIMEW(lpszwIMEFileName, lpszwLayoutText);

    HeapFree(GetProcessHeap(),0,lpszwIMEFileName);
    HeapFree(GetProcessHeap(),0,lpszwLayoutText);
    return hkl;
2069 2070 2071 2072 2073 2074 2075 2076
}

/***********************************************************************
 *		ImmInstallIMEW (IMM32.@)
 */
HKL WINAPI ImmInstallIMEW(
  LPCWSTR lpszIMEFileName, LPCWSTR lpszLayoutText)
{
2077 2078 2079 2080 2081
    INT lcid = GetUserDefaultLCID();
    INT count;
    HKL hkl;
    DWORD rc;
    HKEY hkey;
2082
    WCHAR regKey[ARRAY_SIZE(szImeRegFmt)+8];
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093

    TRACE ("(%s, %s):\n", debugstr_w(lpszIMEFileName),
                          debugstr_w(lpszLayoutText));

    /* Start with 2.  e001 will be blank and so default to the wine internal IME */
    count = 2;

    while (count < 0xfff)
    {
        DWORD disposition = 0;

2094 2095
        hkl = (HKL)MAKELPARAM( lcid, 0xe000 | count );
        wsprintfW( regKey, szImeRegFmt, (ULONG_PTR)hkl);
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113

        rc = RegCreateKeyExW(HKEY_LOCAL_MACHINE, regKey, 0, NULL, 0, KEY_WRITE, NULL, &hkey, &disposition);
        if (rc == ERROR_SUCCESS && disposition == REG_CREATED_NEW_KEY)
            break;
        else if (rc == ERROR_SUCCESS)
            RegCloseKey(hkey);

        count++;
    }

    if (count == 0xfff)
    {
        WARN("Unable to find slot to install IME\n");
        return 0;
    }

    if (rc == ERROR_SUCCESS)
    {
2114
        rc = RegSetValueExW(hkey, szImeFileW, 0, REG_SZ, (const BYTE*)lpszIMEFileName,
2115 2116
                            (lstrlenW(lpszIMEFileName) + 1) * sizeof(WCHAR));
        if (rc == ERROR_SUCCESS)
2117
            rc = RegSetValueExW(hkey, szLayoutTextW, 0, REG_SZ, (const BYTE*)lpszLayoutText,
2118 2119 2120 2121 2122 2123 2124 2125 2126
                                (lstrlenW(lpszLayoutText) + 1) * sizeof(WCHAR));
        RegCloseKey(hkey);
        return hkl;
    }
    else
    {
        WARN("Unable to set IME registry values\n");
        return 0;
    }
2127 2128 2129 2130 2131 2132 2133
}

/***********************************************************************
 *		ImmIsIME (IMM32.@)
 */
BOOL WINAPI ImmIsIME(HKL hKL)
{
2134 2135 2136 2137
    ImmHkl *ptr;
    TRACE("(%p):\n", hKL);
    ptr = IMM_GetImmHkl(hKL);
    return (ptr && ptr->hIME);
2138 2139 2140 2141 2142 2143 2144 2145
}

/***********************************************************************
 *		ImmIsUIMessageA (IMM32.@)
 */
BOOL WINAPI ImmIsUIMessageA(
  HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam)
{
2146
    TRACE("(%p, %x, %ld, %ld)\n", hWndIME, msg, wParam, lParam);
2147
    if ((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
2148 2149 2150 2151
            (msg == WM_IME_SETCONTEXT) ||
            (msg == WM_IME_NOTIFY) ||
            (msg == WM_IME_COMPOSITIONFULL) ||
            (msg == WM_IME_SELECT) ||
2152
            (msg == 0x287 /* FIXME: WM_IME_SYSTEM */))
2153
    {
2154 2155
        if (hWndIME)
            SendMessageA(hWndIME, msg, wParam, lParam);
2156

2157
        return TRUE;
2158
    }
2159
    return FALSE;
2160 2161 2162 2163 2164 2165 2166 2167
}

/***********************************************************************
 *		ImmIsUIMessageW (IMM32.@)
 */
BOOL WINAPI ImmIsUIMessageW(
  HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam)
{
2168
    TRACE("(%p, %x, %ld, %ld)\n", hWndIME, msg, wParam, lParam);
2169
    if ((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
2170 2171 2172 2173
            (msg == WM_IME_SETCONTEXT) ||
            (msg == WM_IME_NOTIFY) ||
            (msg == WM_IME_COMPOSITIONFULL) ||
            (msg == WM_IME_SELECT) ||
2174
            (msg == 0x287 /* FIXME: WM_IME_SYSTEM */))
2175
    {
2176 2177
        if (hWndIME)
            SendMessageW(hWndIME, msg, wParam, lParam);
2178

2179
        return TRUE;
2180
    }
2181
    return FALSE;
2182 2183
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2184
/***********************************************************************
2185
 *		ImmNotifyIME (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2186
 */
2187 2188
BOOL WINAPI ImmNotifyIME(
  HIMC hIMC, DWORD dwAction, DWORD dwIndex, DWORD dwValue)
Alexandre Julliard's avatar
Alexandre Julliard committed
2189
{
2190
    InputContextData *data = get_imc_data(hIMC);
2191

2192
    TRACE("(%p, %d, %d, %d)\n",
2193 2194
        hIMC, dwAction, dwIndex, dwValue);

2195 2196 2197 2198 2199 2200
    if (hIMC == NULL)
    {
        SetLastError(ERROR_SUCCESS);
        return FALSE;
    }

2201
    if (!data || ! data->immKbd->pNotifyIME)
2202
    {
2203
        return FALSE;
2204
    }
2205

2206
    return data->immKbd->pNotifyIME(hIMC,dwAction,dwIndex,dwValue);
Alexandre Julliard's avatar
Alexandre Julliard committed
2207 2208 2209
}

/***********************************************************************
2210
 *		ImmRegisterWordA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2211
 */
2212 2213
BOOL WINAPI ImmRegisterWordA(
  HKL hKL, LPCSTR lpszReading, DWORD dwStyle, LPCSTR lpszRegister)
Alexandre Julliard's avatar
Alexandre Julliard committed
2214
{
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_a(lpszReading), dwStyle,
                    debugstr_a(lpszRegister));
    if (immHkl->hIME && immHkl->pImeRegisterWord)
    {
        if (!is_kbd_ime_unicode(immHkl))
            return immHkl->pImeRegisterWord((LPCWSTR)lpszReading,dwStyle,
                                            (LPCWSTR)lpszRegister);
        else
        {
            LPWSTR lpszwReading = strdupAtoW(lpszReading);
            LPWSTR lpszwRegister = strdupAtoW(lpszRegister);
            BOOL rc;

            rc = immHkl->pImeRegisterWord(lpszwReading,dwStyle,lpszwRegister);
            HeapFree(GetProcessHeap(),0,lpszwReading);
            HeapFree(GetProcessHeap(),0,lpszwRegister);
            return rc;
        }
    }
    else
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2237 2238 2239
}

/***********************************************************************
2240
 *		ImmRegisterWordW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2241
 */
2242 2243
BOOL WINAPI ImmRegisterWordW(
  HKL hKL, LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszRegister)
Alexandre Julliard's avatar
Alexandre Julliard committed
2244
{
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_w(lpszReading), dwStyle,
                    debugstr_w(lpszRegister));
    if (immHkl->hIME && immHkl->pImeRegisterWord)
    {
        if (is_kbd_ime_unicode(immHkl))
            return immHkl->pImeRegisterWord(lpszReading,dwStyle,lpszRegister);
        else
        {
            LPSTR lpszaReading = strdupWtoA(lpszReading);
            LPSTR lpszaRegister = strdupWtoA(lpszRegister);
            BOOL rc;

            rc = immHkl->pImeRegisterWord((LPCWSTR)lpszaReading,dwStyle,
                                          (LPCWSTR)lpszaRegister);
            HeapFree(GetProcessHeap(),0,lpszaReading);
            HeapFree(GetProcessHeap(),0,lpszaRegister);
            return rc;
        }
    }
    else
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2267 2268 2269
}

/***********************************************************************
2270
 *		ImmReleaseContext (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2271
 */
2272
BOOL WINAPI ImmReleaseContext(HWND hWnd, HIMC hIMC)
Alexandre Julliard's avatar
Alexandre Julliard committed
2273
{
2274
  static BOOL shown = FALSE;
2275

2276 2277
  if (!shown) {
     FIXME("(%p, %p): stub\n", hWnd, hIMC);
2278
     shown = TRUE;
2279 2280
  }
  return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2281 2282
}

2283 2284 2285 2286 2287
/***********************************************************************
*              ImmRequestMessageA(IMM32.@)
*/
LRESULT WINAPI ImmRequestMessageA(HIMC hIMC, WPARAM wParam, LPARAM lParam)
{
2288
    InputContextData *data = get_imc_data(hIMC);
2289 2290 2291

    TRACE("%p %ld %ld\n", hIMC, wParam, wParam);

2292
    if (data) return SendMessageA(data->IMC.hWnd, WM_IME_REQUEST, wParam, lParam);
2293

2294 2295
    SetLastError(ERROR_INVALID_HANDLE);
    return 0;
2296 2297 2298 2299 2300 2301 2302
}

/***********************************************************************
*              ImmRequestMessageW(IMM32.@)
*/
LRESULT WINAPI ImmRequestMessageW(HIMC hIMC, WPARAM wParam, LPARAM lParam)
{
2303
    InputContextData *data = get_imc_data(hIMC);
2304 2305 2306

    TRACE("%p %ld %ld\n", hIMC, wParam, wParam);

2307
    if (data) return SendMessageW(data->IMC.hWnd, WM_IME_REQUEST, wParam, lParam);
2308

2309 2310
    SetLastError(ERROR_INVALID_HANDLE);
    return 0;
2311 2312
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2313
/***********************************************************************
2314
 *		ImmSetCandidateWindow (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2315
 */
2316 2317
BOOL WINAPI ImmSetCandidateWindow(
  HIMC hIMC, LPCANDIDATEFORM lpCandidate)
Alexandre Julliard's avatar
Alexandre Julliard committed
2318
{
2319
    InputContextData *data = get_imc_data(hIMC);
2320 2321 2322 2323 2324 2325

    TRACE("(%p, %p)\n", hIMC, lpCandidate);

    if (!data || !lpCandidate)
        return FALSE;

2326 2327 2328
    if (IMM_IsCrossThreadAccess(NULL, hIMC))
        return FALSE;

2329 2330 2331 2332
    TRACE("\t%x, %x, %s, %s\n",
          lpCandidate->dwIndex, lpCandidate->dwStyle,
          wine_dbgstr_point(&lpCandidate->ptCurrentPos),
          wine_dbgstr_rect(&lpCandidate->rcArea));
2333

2334
    if (lpCandidate->dwIndex >= ARRAY_SIZE(data->IMC.cfCandForm))
2335 2336 2337 2338 2339 2340 2341
        return FALSE;

    data->IMC.cfCandForm[lpCandidate->dwIndex] = *lpCandidate;
    ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCANDIDATEPOS);
    ImmInternalSendIMENotify(data, IMN_SETCANDIDATEPOS, 1 << lpCandidate->dwIndex);

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2342 2343 2344
}

/***********************************************************************
2345
 *		ImmSetCompositionFontA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2346
 */
2347
BOOL WINAPI ImmSetCompositionFontA(HIMC hIMC, LPLOGFONTA lplf)
Alexandre Julliard's avatar
Alexandre Julliard committed
2348
{
2349
    InputContextData *data = get_imc_data(hIMC);
2350 2351
    TRACE("(%p, %p)\n", hIMC, lplf);

2352
    if (!data || !lplf)
2353 2354
    {
        SetLastError(ERROR_INVALID_HANDLE);
2355
        return FALSE;
2356
    }
2357

2358 2359 2360
    if (IMM_IsCrossThreadAccess(NULL, hIMC))
        return FALSE;

2361 2362
    memcpy(&data->IMC.lfFont.W,lplf,sizeof(LOGFONTA));
    MultiByteToWideChar(CP_ACP, 0, lplf->lfFaceName, -1, data->IMC.lfFont.W.lfFaceName,
2363
                        LF_FACESIZE);
2364
    ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCOMPOSITIONFONT);
2365
    ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONFONT, 0);
2366 2367

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2368 2369 2370
}

/***********************************************************************
2371
 *		ImmSetCompositionFontW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2372
 */
2373
BOOL WINAPI ImmSetCompositionFontW(HIMC hIMC, LPLOGFONTW lplf)
Alexandre Julliard's avatar
Alexandre Julliard committed
2374
{
2375
    InputContextData *data = get_imc_data(hIMC);
2376 2377
    TRACE("(%p, %p)\n", hIMC, lplf);

2378
    if (!data || !lplf)
2379 2380
    {
        SetLastError(ERROR_INVALID_HANDLE);
2381
        return FALSE;
2382
    }
2383

2384 2385 2386
    if (IMM_IsCrossThreadAccess(NULL, hIMC))
        return FALSE;

2387
    data->IMC.lfFont.W = *lplf;
2388
    ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCOMPOSITIONFONT);
2389
    ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONFONT, 0);
2390 2391

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2392 2393 2394
}

/***********************************************************************
2395
 *		ImmSetCompositionStringA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2396
 */
2397
BOOL WINAPI ImmSetCompositionStringA(
2398 2399
  HIMC hIMC, DWORD dwIndex,
  LPCVOID lpComp, DWORD dwCompLen,
Alexandre Julliard's avatar
Alexandre Julliard committed
2400
  LPCVOID lpRead, DWORD dwReadLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
2401
{
2402 2403 2404 2405 2406
    DWORD comp_len;
    DWORD read_len;
    WCHAR *CompBuffer = NULL;
    WCHAR *ReadBuffer = NULL;
    BOOL rc;
2407
    InputContextData *data = get_imc_data(hIMC);
2408

2409
    TRACE("(%p, %d, %p, %d, %p, %d):\n",
2410 2411
            hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);

2412 2413 2414
    if (!data)
        return FALSE;

2415 2416 2417 2418 2419 2420 2421
    if (!(dwIndex == SCS_SETSTR ||
          dwIndex == SCS_CHANGEATTR ||
          dwIndex == SCS_CHANGECLAUSE ||
          dwIndex == SCS_SETRECONVERTSTRING ||
          dwIndex == SCS_QUERYRECONVERTSTRING))
        return FALSE;

2422 2423 2424 2425
    if (!is_himc_ime_unicode(data))
        return data->immKbd->pImeSetCompositionString(hIMC, dwIndex, lpComp,
                        dwCompLen, lpRead, dwReadLen);

2426 2427 2428
    comp_len = MultiByteToWideChar(CP_ACP, 0, lpComp, dwCompLen, NULL, 0);
    if (comp_len)
    {
2429
        CompBuffer = HeapAlloc(GetProcessHeap(),0,comp_len * sizeof(WCHAR));
2430 2431 2432 2433 2434 2435
        MultiByteToWideChar(CP_ACP, 0, lpComp, dwCompLen, CompBuffer, comp_len);
    }

    read_len = MultiByteToWideChar(CP_ACP, 0, lpRead, dwReadLen, NULL, 0);
    if (read_len)
    {
2436
        ReadBuffer = HeapAlloc(GetProcessHeap(),0,read_len * sizeof(WCHAR));
2437 2438 2439 2440 2441 2442
        MultiByteToWideChar(CP_ACP, 0, lpRead, dwReadLen, ReadBuffer, read_len);
    }

    rc =  ImmSetCompositionStringW(hIMC, dwIndex, CompBuffer, comp_len,
                                   ReadBuffer, read_len);

2443 2444
    HeapFree(GetProcessHeap(), 0, CompBuffer);
    HeapFree(GetProcessHeap(), 0, ReadBuffer);
2445 2446

    return rc;
Alexandre Julliard's avatar
Alexandre Julliard committed
2447 2448 2449
}

/***********************************************************************
2450
 *		ImmSetCompositionStringW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2451
 */
2452 2453
BOOL WINAPI ImmSetCompositionStringW(
	HIMC hIMC, DWORD dwIndex,
Alexandre Julliard's avatar
Alexandre Julliard committed
2454 2455 2456
	LPCVOID lpComp, DWORD dwCompLen,
	LPCVOID lpRead, DWORD dwReadLen)
{
2457 2458 2459 2460 2461
    DWORD comp_len;
    DWORD read_len;
    CHAR *CompBuffer = NULL;
    CHAR *ReadBuffer = NULL;
    BOOL rc;
2462
    InputContextData *data = get_imc_data(hIMC);
2463

2464 2465
    TRACE("(%p, %d, %p, %d, %p, %d):\n",
            hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
2466

2467
    if (!data)
2468
        return FALSE;
2469

2470 2471 2472 2473 2474 2475 2476
    if (!(dwIndex == SCS_SETSTR ||
          dwIndex == SCS_CHANGEATTR ||
          dwIndex == SCS_CHANGECLAUSE ||
          dwIndex == SCS_SETRECONVERTSTRING ||
          dwIndex == SCS_QUERYRECONVERTSTRING))
        return FALSE;

2477
    if (is_himc_ime_unicode(data))
2478 2479
        return data->immKbd->pImeSetCompositionString(hIMC, dwIndex, lpComp,
                        dwCompLen, lpRead, dwReadLen);
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505

    comp_len = WideCharToMultiByte(CP_ACP, 0, lpComp, dwCompLen, NULL, 0, NULL,
                                   NULL);
    if (comp_len)
    {
        CompBuffer = HeapAlloc(GetProcessHeap(),0,comp_len);
        WideCharToMultiByte(CP_ACP, 0, lpComp, dwCompLen, CompBuffer, comp_len,
                            NULL, NULL);
    }

    read_len = WideCharToMultiByte(CP_ACP, 0, lpRead, dwReadLen, NULL, 0, NULL,
                                   NULL);
    if (read_len)
    {
        ReadBuffer = HeapAlloc(GetProcessHeap(),0,read_len);
        WideCharToMultiByte(CP_ACP, 0, lpRead, dwReadLen, ReadBuffer, read_len,
                            NULL, NULL);
    }

    rc =  ImmSetCompositionStringA(hIMC, dwIndex, CompBuffer, comp_len,
                                   ReadBuffer, read_len);

    HeapFree(GetProcessHeap(), 0, CompBuffer);
    HeapFree(GetProcessHeap(), 0, ReadBuffer);

    return rc;
Alexandre Julliard's avatar
Alexandre Julliard committed
2506 2507 2508
}

/***********************************************************************
2509
 *		ImmSetCompositionWindow (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2510
 */
2511 2512
BOOL WINAPI ImmSetCompositionWindow(
  HIMC hIMC, LPCOMPOSITIONFORM lpCompForm)
Alexandre Julliard's avatar
Alexandre Julliard committed
2513
{
2514
    BOOL reshow = FALSE;
2515
    InputContextData *data = get_imc_data(hIMC);
2516 2517

    TRACE("(%p, %p)\n", hIMC, lpCompForm);
2518
    if (lpCompForm)
2519 2520 2521
        TRACE("\t%x, %s, %s\n", lpCompForm->dwStyle,
              wine_dbgstr_point(&lpCompForm->ptCurrentPos),
              wine_dbgstr_rect(&lpCompForm->rcArea));
2522 2523

    if (!data)
2524 2525
    {
        SetLastError(ERROR_INVALID_HANDLE);
2526
        return FALSE;
2527
    }
2528

2529 2530 2531
    if (IMM_IsCrossThreadAccess(NULL, hIMC))
        return FALSE;

2532
    data->IMC.cfCompForm = *lpCompForm;
2533

2534
    if (IsWindowVisible(data->immKbd->UIWnd))
2535 2536
    {
        reshow = TRUE;
2537
        ShowWindow(data->immKbd->UIWnd,SW_HIDE);
2538 2539
    }

2540
    /* FIXME: this is a partial stub */
2541 2542

    if (reshow)
2543
        ShowWindow(data->immKbd->UIWnd,SW_SHOWNOACTIVATE);
2544

2545
    ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONWINDOW, 0);
2546
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2547 2548 2549
}

/***********************************************************************
2550
 *		ImmSetConversionStatus (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2551
 */
2552 2553
BOOL WINAPI ImmSetConversionStatus(
  HIMC hIMC, DWORD fdwConversion, DWORD fdwSentence)
Alexandre Julliard's avatar
Alexandre Julliard committed
2554
{
2555
    DWORD oldConversion, oldSentence;
2556
    InputContextData *data = get_imc_data(hIMC);
2557

2558 2559 2560
    TRACE("%p %d %d\n", hIMC, fdwConversion, fdwSentence);

    if (!data)
2561 2562
    {
        SetLastError(ERROR_INVALID_HANDLE);
2563
        return FALSE;
2564
    }
2565

2566 2567 2568
    if (IMM_IsCrossThreadAccess(NULL, hIMC))
        return FALSE;

2569 2570
    if ( fdwConversion != data->IMC.fdwConversion )
    {
2571
        oldConversion = data->IMC.fdwConversion;
2572
        data->IMC.fdwConversion = fdwConversion;
2573
        ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, oldConversion, IMC_SETCONVERSIONMODE);
2574 2575 2576 2577
        ImmInternalSendIMENotify(data, IMN_SETCONVERSIONMODE, 0);
    }
    if ( fdwSentence != data->IMC.fdwSentence )
    {
2578
        oldSentence = data->IMC.fdwSentence;
2579
        data->IMC.fdwSentence = fdwSentence;
2580
        ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, oldSentence, IMC_SETSENTENCEMODE);
2581 2582 2583 2584
        ImmInternalSendIMENotify(data, IMN_SETSENTENCEMODE, 0);
    }

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2585 2586 2587
}

/***********************************************************************
2588
 *		ImmSetOpenStatus (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2589
 */
2590
BOOL WINAPI ImmSetOpenStatus(HIMC hIMC, BOOL fOpen)
Alexandre Julliard's avatar
Alexandre Julliard committed
2591
{
2592
    InputContextData *data = get_imc_data(hIMC);
2593 2594

    TRACE("%p %d\n", hIMC, fOpen);
2595 2596

    if (!data)
2597 2598
    {
        SetLastError(ERROR_INVALID_HANDLE);
2599
        return FALSE;
2600
    }
2601

2602 2603 2604
    if (IMM_IsCrossThreadAccess(NULL, hIMC))
        return FALSE;

2605
    if (data->immKbd->UIWnd == NULL)
2606
    {
2607
        /* create the ime window */
2608
        data->immKbd->UIWnd = CreateWindowExW( WS_EX_TOOLWINDOW,
2609 2610
                    data->immKbd->imeClassName, NULL, WS_POPUP, 0, 0, 1, 1, 0,
                    0, data->immKbd->hIME, 0);
2611
        SetWindowLongPtrW(data->immKbd->UIWnd, IMMGWL_IMC, (LONG_PTR)data);
2612
    }
2613 2614
    else if (fOpen)
        SetWindowLongPtrW(data->immKbd->UIWnd, IMMGWL_IMC, (LONG_PTR)data);
2615

2616 2617 2618 2619
    if (!fOpen != !data->IMC.fOpen)
    {
        data->IMC.fOpen = fOpen;
        ImmNotifyIME( hIMC, NI_CONTEXTUPDATED, 0, IMC_SETOPENSTATUS);
ByeongSik Jeon's avatar
ByeongSik Jeon committed
2620
        ImmInternalSendIMENotify(data, IMN_SETOPENSTATUS, 0);
2621 2622 2623
    }

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2624 2625 2626
}

/***********************************************************************
2627
 *		ImmSetStatusWindowPos (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2628
 */
2629
BOOL WINAPI ImmSetStatusWindowPos(HIMC hIMC, LPPOINT lpptPos)
Alexandre Julliard's avatar
Alexandre Julliard committed
2630
{
2631
    InputContextData *data = get_imc_data(hIMC);
2632 2633 2634 2635

    TRACE("(%p, %p)\n", hIMC, lpptPos);

    if (!data || !lpptPos)
2636 2637
    {
        SetLastError(ERROR_INVALID_HANDLE);
2638
        return FALSE;
2639
    }
2640

2641 2642 2643
    if (IMM_IsCrossThreadAccess(NULL, hIMC))
        return FALSE;

2644
    TRACE("\t%s\n", wine_dbgstr_point(lpptPos));
2645 2646 2647 2648 2649 2650

    data->IMC.ptStatusWndPos = *lpptPos;
    ImmNotifyIME( hIMC, NI_CONTEXTUPDATED, 0, IMC_SETSTATUSWINDOWPOS);
    ImmInternalSendIMENotify(data, IMN_SETSTATUSWINDOWPOS, 0);

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2651 2652
}

2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682
/***********************************************************************
 *              ImmCreateSoftKeyboard(IMM32.@)
 */
HWND WINAPI ImmCreateSoftKeyboard(UINT uType, UINT hOwner, int x, int y)
{
    FIXME("(%d, %d, %d, %d): stub\n", uType, hOwner, x, y);
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return 0;
}

/***********************************************************************
 *              ImmDestroySoftKeyboard(IMM32.@)
 */
BOOL WINAPI ImmDestroySoftKeyboard(HWND hSoftWnd)
{
    FIXME("(%p): stub\n", hSoftWnd);
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return FALSE;
}

/***********************************************************************
 *              ImmShowSoftKeyboard(IMM32.@)
 */
BOOL WINAPI ImmShowSoftKeyboard(HWND hSoftWnd, int nCmdShow)
{
    FIXME("(%p, %d): stub\n", hSoftWnd, nCmdShow);
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return FALSE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2683
/***********************************************************************
2684
 *		ImmSimulateHotKey (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2685
 */
2686
BOOL WINAPI ImmSimulateHotKey(HWND hWnd, DWORD dwHotKeyID)
Alexandre Julliard's avatar
Alexandre Julliard committed
2687
{
2688
  FIXME("(%p, %d): stub\n", hWnd, dwHotKeyID);
Alexandre Julliard's avatar
Alexandre Julliard committed
2689 2690
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2691 2692 2693
}

/***********************************************************************
2694
 *		ImmUnregisterWordA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2695
 */
2696 2697
BOOL WINAPI ImmUnregisterWordA(
  HKL hKL, LPCSTR lpszReading, DWORD dwStyle, LPCSTR lpszUnregister)
Alexandre Julliard's avatar
Alexandre Julliard committed
2698
{
2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_a(lpszReading), dwStyle,
            debugstr_a(lpszUnregister));
    if (immHkl->hIME && immHkl->pImeUnregisterWord)
    {
        if (!is_kbd_ime_unicode(immHkl))
            return immHkl->pImeUnregisterWord((LPCWSTR)lpszReading,dwStyle,
                                              (LPCWSTR)lpszUnregister);
        else
        {
            LPWSTR lpszwReading = strdupAtoW(lpszReading);
            LPWSTR lpszwUnregister = strdupAtoW(lpszUnregister);
            BOOL rc;

            rc = immHkl->pImeUnregisterWord(lpszwReading,dwStyle,lpszwUnregister);
            HeapFree(GetProcessHeap(),0,lpszwReading);
            HeapFree(GetProcessHeap(),0,lpszwUnregister);
            return rc;
        }
    }
    else
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2721 2722 2723
}

/***********************************************************************
2724
 *		ImmUnregisterWordW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2725
 */
2726 2727
BOOL WINAPI ImmUnregisterWordW(
  HKL hKL, LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszUnregister)
Alexandre Julliard's avatar
Alexandre Julliard committed
2728
{
2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %s, %d, %s):\n", hKL, debugstr_w(lpszReading), dwStyle,
            debugstr_w(lpszUnregister));
    if (immHkl->hIME && immHkl->pImeUnregisterWord)
    {
        if (is_kbd_ime_unicode(immHkl))
            return immHkl->pImeUnregisterWord(lpszReading,dwStyle,lpszUnregister);
        else
        {
            LPSTR lpszaReading = strdupWtoA(lpszReading);
            LPSTR lpszaUnregister = strdupWtoA(lpszUnregister);
            BOOL rc;

            rc = immHkl->pImeUnregisterWord((LPCWSTR)lpszaReading,dwStyle,
                                            (LPCWSTR)lpszaUnregister);
            HeapFree(GetProcessHeap(),0,lpszaReading);
            HeapFree(GetProcessHeap(),0,lpszaUnregister);
            return rc;
        }
    }
    else
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2751
}
2752

2753 2754 2755 2756 2757 2758 2759
/***********************************************************************
 *		ImmGetImeMenuItemsA (IMM32.@)
 */
DWORD WINAPI ImmGetImeMenuItemsA( HIMC hIMC, DWORD dwFlags, DWORD dwType,
   LPIMEMENUITEMINFOA lpImeParentMenu, LPIMEMENUITEMINFOA lpImeMenu,
    DWORD dwSize)
{
2760
    InputContextData *data = get_imc_data(hIMC);
2761 2762
    TRACE("(%p, %i, %i, %p, %p, %i):\n", hIMC, dwFlags, dwType,
        lpImeParentMenu, lpImeMenu, dwSize);
2763 2764 2765 2766 2767 2768 2769

    if (!data)
    {
        SetLastError(ERROR_INVALID_HANDLE);
        return 0;
    }

2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
    if (data->immKbd->hIME && data->immKbd->pImeGetImeMenuItems)
    {
        if (!is_himc_ime_unicode(data) || (!lpImeParentMenu && !lpImeMenu))
            return data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
                                (IMEMENUITEMINFOW*)lpImeParentMenu,
                                (IMEMENUITEMINFOW*)lpImeMenu, dwSize);
        else
        {
            IMEMENUITEMINFOW lpImeParentMenuW;
            IMEMENUITEMINFOW *lpImeMenuW, *parent = NULL;
            DWORD rc;

            if (lpImeParentMenu)
                parent = &lpImeParentMenuW;
            if (lpImeMenu)
            {
                int count = dwSize / sizeof(LPIMEMENUITEMINFOA);
                dwSize = count * sizeof(IMEMENUITEMINFOW);
                lpImeMenuW = HeapAlloc(GetProcessHeap(), 0, dwSize);
            }
            else
                lpImeMenuW = NULL;

            rc = data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
                                parent, lpImeMenuW, dwSize);

            if (lpImeParentMenu)
            {
                memcpy(lpImeParentMenu,&lpImeParentMenuW,sizeof(IMEMENUITEMINFOA));
                lpImeParentMenu->hbmpItem = lpImeParentMenuW.hbmpItem;
                WideCharToMultiByte(CP_ACP, 0, lpImeParentMenuW.szString,
                    -1, lpImeParentMenu->szString, IMEMENUITEM_STRING_SIZE,
                    NULL, NULL);
            }
            if (lpImeMenu && rc)
            {
2806
                unsigned int i;
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821
                for (i = 0; i < rc; i++)
                {
                    memcpy(&lpImeMenu[i],&lpImeMenuW[1],sizeof(IMEMENUITEMINFOA));
                    lpImeMenu[i].hbmpItem = lpImeMenuW[i].hbmpItem;
                    WideCharToMultiByte(CP_ACP, 0, lpImeMenuW[i].szString,
                        -1, lpImeMenu[i].szString, IMEMENUITEM_STRING_SIZE,
                        NULL, NULL);
                }
            }
            HeapFree(GetProcessHeap(),0,lpImeMenuW);
            return rc;
        }
    }
    else
        return 0;
2822 2823 2824 2825 2826 2827 2828 2829 2830
}

/***********************************************************************
*		ImmGetImeMenuItemsW (IMM32.@)
*/
DWORD WINAPI ImmGetImeMenuItemsW( HIMC hIMC, DWORD dwFlags, DWORD dwType,
   LPIMEMENUITEMINFOW lpImeParentMenu, LPIMEMENUITEMINFOW lpImeMenu,
   DWORD dwSize)
{
2831
    InputContextData *data = get_imc_data(hIMC);
2832 2833
    TRACE("(%p, %i, %i, %p, %p, %i):\n", hIMC, dwFlags, dwType,
        lpImeParentMenu, lpImeMenu, dwSize);
2834 2835 2836 2837 2838 2839 2840

    if (!data)
    {
        SetLastError(ERROR_INVALID_HANDLE);
        return 0;
    }

2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
    if (data->immKbd->hIME && data->immKbd->pImeGetImeMenuItems)
    {
        if (is_himc_ime_unicode(data) || (!lpImeParentMenu && !lpImeMenu))
            return data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
                                lpImeParentMenu, lpImeMenu, dwSize);
        else
        {
            IMEMENUITEMINFOA lpImeParentMenuA;
            IMEMENUITEMINFOA *lpImeMenuA, *parent = NULL;
            DWORD rc;

            if (lpImeParentMenu)
                parent = &lpImeParentMenuA;
            if (lpImeMenu)
            {
                int count = dwSize / sizeof(LPIMEMENUITEMINFOW);
                dwSize = count * sizeof(IMEMENUITEMINFOA);
                lpImeMenuA = HeapAlloc(GetProcessHeap(), 0, dwSize);
            }
            else
                lpImeMenuA = NULL;

            rc = data->immKbd->pImeGetImeMenuItems(hIMC, dwFlags, dwType,
                                (IMEMENUITEMINFOW*)parent,
                                (IMEMENUITEMINFOW*)lpImeMenuA, dwSize);

            if (lpImeParentMenu)
            {
                memcpy(lpImeParentMenu,&lpImeParentMenuA,sizeof(IMEMENUITEMINFOA));
                lpImeParentMenu->hbmpItem = lpImeParentMenuA.hbmpItem;
                MultiByteToWideChar(CP_ACP, 0, lpImeParentMenuA.szString,
                    -1, lpImeParentMenu->szString, IMEMENUITEM_STRING_SIZE);
            }
            if (lpImeMenu && rc)
            {
2876
                unsigned int i;
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
                for (i = 0; i < rc; i++)
                {
                    memcpy(&lpImeMenu[i],&lpImeMenuA[1],sizeof(IMEMENUITEMINFOA));
                    lpImeMenu[i].hbmpItem = lpImeMenuA[i].hbmpItem;
                    MultiByteToWideChar(CP_ACP, 0, lpImeMenuA[i].szString,
                        -1, lpImeMenu[i].szString, IMEMENUITEM_STRING_SIZE);
                }
            }
            HeapFree(GetProcessHeap(),0,lpImeMenuA);
            return rc;
        }
    }
    else
        return 0;
2891
}
2892

2893 2894 2895 2896 2897
/***********************************************************************
*		ImmLockIMC(IMM32.@)
*/
LPINPUTCONTEXT WINAPI ImmLockIMC(HIMC hIMC)
{
2898
    InputContextData *data = get_imc_data(hIMC);
2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910

    if (!data)
        return NULL;
    data->dwLock++;
    return &data->IMC;
}

/***********************************************************************
*		ImmUnlockIMC(IMM32.@)
*/
BOOL WINAPI ImmUnlockIMC(HIMC hIMC)
{
2911 2912 2913 2914
    InputContextData *data = get_imc_data(hIMC);

    if (!data)
        return FALSE;
Qian Hong's avatar
Qian Hong committed
2915 2916 2917
    if (data->dwLock)
        data->dwLock--;
    return TRUE;
2918 2919 2920 2921 2922 2923 2924
}

/***********************************************************************
*		ImmGetIMCLockCount(IMM32.@)
*/
DWORD WINAPI ImmGetIMCLockCount(HIMC hIMC)
{
2925 2926 2927
    InputContextData *data = get_imc_data(hIMC);
    if (!data)
        return 0;
2928 2929 2930 2931 2932 2933 2934 2935
    return data->dwLock;
}

/***********************************************************************
*		ImmCreateIMCC(IMM32.@)
*/
HIMCC  WINAPI ImmCreateIMCC(DWORD size)
{
2936
    return GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE, size);
2937 2938 2939 2940 2941 2942 2943
}

/***********************************************************************
*       ImmDestroyIMCC(IMM32.@)
*/
HIMCC WINAPI ImmDestroyIMCC(HIMCC block)
{
2944
    return GlobalFree(block);
2945 2946 2947 2948 2949 2950 2951
}

/***********************************************************************
*		ImmLockIMCC(IMM32.@)
*/
LPVOID WINAPI ImmLockIMCC(HIMCC imcc)
{
2952
    return GlobalLock(imcc);
2953 2954 2955 2956 2957 2958 2959
}

/***********************************************************************
*		ImmUnlockIMCC(IMM32.@)
*/
BOOL WINAPI ImmUnlockIMCC(HIMCC imcc)
{
2960
    return GlobalUnlock(imcc);
2961 2962 2963 2964 2965 2966 2967
}

/***********************************************************************
*		ImmGetIMCCLockCount(IMM32.@)
*/
DWORD WINAPI ImmGetIMCCLockCount(HIMCC imcc)
{
2968
    return GlobalFlags(imcc) & GMEM_LOCKCOUNT;
2969 2970 2971 2972 2973 2974 2975
}

/***********************************************************************
*		ImmReSizeIMCC(IMM32.@)
*/
HIMCC  WINAPI ImmReSizeIMCC(HIMCC imcc, DWORD size)
{
2976
    return GlobalReAlloc(imcc, size, GMEM_ZEROINIT | GMEM_MOVEABLE);
2977 2978 2979 2980 2981 2982 2983
}

/***********************************************************************
*		ImmGetIMCCSize(IMM32.@)
*/
DWORD WINAPI ImmGetIMCCSize(HIMCC imcc)
{
2984
    return GlobalSize(imcc);
2985 2986
}

2987 2988 2989 2990 2991
/***********************************************************************
*		ImmGenerateMessage(IMM32.@)
*/
BOOL WINAPI ImmGenerateMessage(HIMC hIMC)
{
2992 2993 2994 2995 2996 2997 2998
    InputContextData *data = get_imc_data(hIMC);

    if (!data)
    {
        SetLastError(ERROR_INVALID_HANDLE);
        return FALSE;
    }
2999 3000 3001 3002 3003

    TRACE("%i messages queued\n",data->IMC.dwNumMsgBuf);
    if (data->IMC.dwNumMsgBuf > 0)
    {
        LPTRANSMSG lpTransMsg;
3004 3005
        HIMCC hMsgBuf;
        DWORD i, dwNumMsgBuf;
3006

3007 3008 3009 3010
        /* We are going to detach our hMsgBuff so that if processing messages
           generates new messages they go into a new buffer */
        hMsgBuf = data->IMC.hMsgBuf;
        dwNumMsgBuf = data->IMC.dwNumMsgBuf;
3011

3012
        data->IMC.hMsgBuf = ImmCreateIMCC(0);
3013
        data->IMC.dwNumMsgBuf = 0;
3014 3015 3016 3017 3018 3019 3020

        lpTransMsg = ImmLockIMCC(hMsgBuf);
        for (i = 0; i < dwNumMsgBuf; i++)
            ImmInternalSendIMEMessage(data, lpTransMsg[i].message, lpTransMsg[i].wParam, lpTransMsg[i].lParam);

        ImmUnlockIMCC(hMsgBuf);
        ImmDestroyIMCC(hMsgBuf);
3021 3022 3023 3024
    }

    return TRUE;
}
3025 3026 3027 3028 3029

/***********************************************************************
*       ImmTranslateMessage(IMM32.@)
*       ( Undocumented, call internally and from user32.dll )
*/
3030
BOOL WINAPI ImmTranslateMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lKeyData)
3031 3032 3033 3034 3035 3036 3037 3038
{
    InputContextData *data;
    HIMC imc = ImmGetContext(hwnd);
    BYTE state[256];
    UINT scancode;
    LPVOID list = 0;
    UINT msg_count;
    UINT uVirtKey;
3039
    static const DWORD list_count = 10;
3040

3041
    TRACE("%p %x %x %x\n",hwnd, msg, (UINT)wParam, (UINT)lKeyData);
3042 3043

    if (imc)
3044
        data = imc;
3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058
    else
        return FALSE;

    if (!data->immKbd->hIME || !data->immKbd->pImeToAsciiEx)
        return FALSE;

    GetKeyboardState(state);
    scancode = lKeyData >> 0x10 & 0xff;

    list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list_count * sizeof(TRANSMSG) + sizeof(DWORD));
    ((DWORD*)list)[0] = list_count;

    if (data->immKbd->imeInfo.fdwProperty & IME_PROP_KBD_CHAR_FIRST)
    {
3059 3060
        WCHAR chr;

3061 3062
        if (!is_himc_ime_unicode(data))
            ToAscii(data->lastVK, scancode, state, &chr, 0);
3063 3064
        else
            ToUnicodeEx(data->lastVK, scancode, state, &chr, 1, 0, GetKeyboardLayout(0));
3065 3066 3067 3068 3069 3070 3071 3072 3073
        uVirtKey = MAKELONG(data->lastVK,chr);
    }
    else
        uVirtKey = data->lastVK;

    msg_count = data->immKbd->pImeToAsciiEx(uVirtKey, scancode, state, list, 0, imc);
    TRACE("%i messages generated\n",msg_count);
    if (msg_count && msg_count <= list_count)
    {
3074
        UINT i;
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084
        LPTRANSMSG msgs = (LPTRANSMSG)((LPBYTE)list + sizeof(DWORD));

        for (i = 0; i < msg_count; i++)
            ImmInternalPostIMEMessage(data, msgs[i].message, msgs[i].wParam, msgs[i].lParam);
    }
    else if (msg_count > list_count)
        ImmGenerateMessage(imc);

    HeapFree(GetProcessHeap(),0,list);

3085 3086
    data->lastVK = VK_PROCESSKEY;

3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102
    return (msg_count > 0);
}

/***********************************************************************
*		ImmProcessKey(IMM32.@)
*       ( Undocumented, called from user32.dll )
*/
BOOL WINAPI ImmProcessKey(HWND hwnd, HKL hKL, UINT vKey, LPARAM lKeyData, DWORD unknown)
{
    InputContextData *data;
    HIMC imc = ImmGetContext(hwnd);
    BYTE state[256];

    TRACE("%p %p %x %x %x\n",hwnd, hKL, vKey, (UINT)lKeyData, unknown);

    if (imc)
3103
        data = imc;
3104 3105 3106
    else
        return FALSE;

3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122
    /* Make sure we are inputting to the correct keyboard */
    if (data->immKbd->hkl != hKL)
    {
        ImmHkl *new_hkl = IMM_GetImmHkl(hKL);
        if (new_hkl)
        {
            data->immKbd->pImeSelect(imc, FALSE);
            data->immKbd->uSelected--;
            data->immKbd = new_hkl;
            data->immKbd->pImeSelect(imc, TRUE);
            data->immKbd->uSelected++;
        }
        else
            return FALSE;
    }

3123 3124 3125 3126 3127 3128
    if (!data->immKbd->hIME || !data->immKbd->pImeProcessKey)
        return FALSE;

    GetKeyboardState(state);
    if (data->immKbd->pImeProcessKey(imc, vKey, lKeyData, state))
    {
3129 3130
        data->lastVK = vKey;
        return TRUE;
3131 3132
    }

3133
    data->lastVK = VK_PROCESSKEY;
3134 3135
    return FALSE;
}
3136 3137 3138 3139 3140 3141 3142 3143 3144

/***********************************************************************
*		ImmDisableTextFrameService(IMM32.@)
*/
BOOL WINAPI ImmDisableTextFrameService(DWORD idThread)
{
    FIXME("Stub\n");
    return FALSE;
}
3145 3146 3147 3148 3149 3150 3151 3152 3153 3154

/***********************************************************************
 *              ImmEnumInputContext(IMM32.@)
 */

BOOL WINAPI ImmEnumInputContext(DWORD idThread, IMCENUMPROC lpfn, LPARAM lParam)
{
    FIXME("Stub\n");
    return FALSE;
}
3155 3156 3157 3158 3159 3160 3161 3162 3163 3164

/***********************************************************************
 *              ImmGetHotKey(IMM32.@)
 */

BOOL WINAPI ImmGetHotKey(DWORD hotkey, UINT *modifiers, UINT *key, HKL hkl)
{
    FIXME("%x, %p, %p, %p: stub\n", hotkey, modifiers, key, hkl);
    return FALSE;
}
3165 3166

/***********************************************************************
3167
 *              ImmDisableLegacyIME(IMM32.@)
3168 3169 3170 3171 3172 3173
 */
BOOL WINAPI ImmDisableLegacyIME(void)
{
    FIXME("stub\n");
    return TRUE;
}