imm.c 82.1 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 42 43 44
typedef struct tagIMCCInternal
{
    DWORD dwLock;
    DWORD dwSize;
} IMCCInternal;

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#define MAKE_FUNCPTR(f) typeof(f) * p##f
typedef struct _tagImmHkl{
    struct list entry;
    HKL         hkl;
    HMODULE     hIME;
    IMEINFO     imeInfo;
    WCHAR       imeClassName[17]; /* 16 character max */
    ULONG       uSelected;

    /* Function Pointers */
    MAKE_FUNCPTR(ImeInquire);
    MAKE_FUNCPTR(ImeConfigure);
    MAKE_FUNCPTR(ImeDestroy);
    MAKE_FUNCPTR(ImeEscape);
    MAKE_FUNCPTR(ImeSelect);
    MAKE_FUNCPTR(ImeSetActiveContext);
    MAKE_FUNCPTR(ImeToAsciiEx);
    MAKE_FUNCPTR(NotifyIME);
    MAKE_FUNCPTR(ImeRegisterWord);
    MAKE_FUNCPTR(ImeUnregisterWord);
    MAKE_FUNCPTR(ImeEnumRegisterWord);
    MAKE_FUNCPTR(ImeSetCompositionString);
    MAKE_FUNCPTR(ImeConversionList);
    MAKE_FUNCPTR(ImeProcessKey);
    MAKE_FUNCPTR(ImeGetRegisterWordStyle);
    MAKE_FUNCPTR(ImeGetImeMenuItems);
} ImmHkl;
#undef MAKE_FUNCPTR

74 75
typedef struct tagInputContextData
{
76 77
        DWORD           dwLock;
        INPUTCONTEXT    IMC;
78 79

        ImmHkl          *immKbd;
80
        HWND            imeWnd;
81
        UINT            lastVK;
82
} InputContextData;
83

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

90 91 92 93
typedef struct _tagIMMThreadData {
    HIMC defaultContext;
    HWND hwndDefault;
} IMMThreadData;
94

95
static DWORD tlsIndex = 0;
96 97
static struct list ImmHklList = LIST_INIT(ImmHklList);

98 99 100 101 102 103 104 105 106
/* MSIME messages */
static UINT WM_MSIME_SERVICE;
static UINT WM_MSIME_RECONVERTOPTIONS;
static UINT WM_MSIME_MOUSE;
static UINT WM_MSIME_RECONVERTREQUEST;
static UINT WM_MSIME_RECONVERT;
static UINT WM_MSIME_QUERYPOSITION;
static UINT WM_MSIME_DOCUMENTFEED;

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

109 110
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};
111
static const WCHAR szImeRegFmt[] = {'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\','C','o','n','t','r','o','l','\\','K','e','y','b','o','a','r','d',' ','L','a','y','o','u','t','s','\\','%','0','8','l','x',0};
112 113


114
#define is_himc_ime_unicode(p)  (p->immKbd->imeInfo.fdwProperty & IME_PROP_UNICODE)
115 116
#define is_kbd_ime_unicode(p)  (p->imeInfo.fdwProperty & IME_PROP_UNICODE)

117 118
static BOOL IMM_DestroyContext(HIMC hIMC);

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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;
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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
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;
}

221 222
static IMMThreadData* IMM_GetThreadData(void)
{
223 224 225 226 227 228 229 230 231
    IMMThreadData* data = TlsGetValue(tlsIndex);
    if (!data)
    {
        data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                         sizeof(IMMThreadData));
        TlsSetValue(tlsIndex,data);
        TRACE("Thread Data Created\n");
    }
    return data;
232 233 234 235 236
}

static void IMM_FreeThreadData(void)
{
    IMMThreadData* data = TlsGetValue(tlsIndex);
237 238 239 240 241 242 243
    if (data)
    {
        IMM_DestroyContext(data->defaultContext);
        DestroyWindow(data->hwndDefault);
        HeapFree(GetProcessHeap(),0,data);
        TRACE("Thread Data Destroyed\n");
    }
244 245
}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
static HMODULE LoadDefaultWineIME(void)
{
    char buffer[MAX_PATH], libname[32], *name, *next;
    HMODULE module = 0;
    HKEY hkey;

    TRACE("Attempting to fall back to wine default IME\n");

    strcpy( buffer, "x11" );  /* default value */
    /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
    if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hkey ))
    {
        DWORD type, count = sizeof(buffer);
        RegQueryValueExA( hkey, "Ime", 0, &type, (LPBYTE) buffer, &count );
        RegCloseKey( hkey );
    }

    name = buffer;
    while (name)
    {
        next = strchr( name, ',' );
        if (next) *next++ = 0;

        snprintf( libname, sizeof(libname), "wine%s.drv", name );
        if ((module = LoadLibraryA( libname )) != 0) break;
        name = next;
    }

    return module;
}

277 278 279 280 281 282 283
/* 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];

284
    TRACE("Seeking ime for keyboard %p\n",hkl);
285 286 287 288 289 290 291 292 293 294 295

    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;
296
    if (ImmGetIMEFileNameW(hkl, filename, MAX_PATH)) ptr->hIME = LoadLibraryW(filename);
297 298
    if (!ptr->hIME)
        ptr->hIME = LoadDefaultWineIME();
299 300 301
    if (ptr->hIME)
    {
        LOAD_FUNCPTR(ImeInquire);
302
        if (!ptr->pImeInquire || !ptr->pImeInquire(&ptr->imeInfo, ptr->imeClassName, NULL))
303 304 305 306 307 308
        {
            FreeLibrary(ptr->hIME);
            ptr->hIME = NULL;
        }
        else
        {
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
            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);
                }
            }
340 341 342 343 344 345 346 347 348 349 350 351
        }
    }
    list_add_head(&ImmHklList,&ptr->entry);

    return ptr;
}
#undef LOAD_FUNCPTR

static void IMM_FreeAllImmHkl(void)
{
    ImmHkl *ptr,*cursor2;

352
    LIST_FOR_EACH_ENTRY_SAFE(ptr, cursor2, &ImmHklList, ImmHkl, entry)
353 354 355 356 357 358 359 360 361 362 363
    {
        list_remove(&ptr->entry);
        if (ptr->hIME)
        {
            ptr->pImeDestroy(1);
            FreeLibrary(ptr->hIME);
        }
        HeapFree(GetProcessHeap(),0,ptr);
    }
}

364 365 366 367 368 369 370 371 372 373 374
static void IMM_RegisterMessages(void)
{
    WM_MSIME_SERVICE = RegisterWindowMessageA("MSIMEService");
    WM_MSIME_RECONVERTOPTIONS = RegisterWindowMessageA("MSIMEReconvertOptions");
    WM_MSIME_MOUSE = RegisterWindowMessageA("MSIMEMouseOperation");
    WM_MSIME_RECONVERTREQUEST = RegisterWindowMessageA("MSIMEReconvertRequest");
    WM_MSIME_RECONVERT = RegisterWindowMessageA("MSIMEReconvert");
    WM_MSIME_QUERYPOSITION = RegisterWindowMessageA("MSIMEQueryPosition");
    WM_MSIME_DOCUMENTFEED = RegisterWindowMessageA("MSIMEDocumentFeed");
}

375 376
BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved)
{
377
    TRACE("%p, %x, %p\n",hInstDLL,fdwReason,lpReserved);
378 379 380
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
381
            IMM_RegisterMessages();
382
            tlsIndex = TlsAlloc();
383
            if (tlsIndex == TLS_OUT_OF_INDEXES)
384
                return FALSE;
385 386 387 388 389
            break;
        case DLL_THREAD_ATTACH:
            break;
        case DLL_THREAD_DETACH:
            IMM_FreeThreadData();
390 391
            break;
        case DLL_PROCESS_DETACH:
392
            IMM_FreeThreadData();
393
            IMM_FreeAllImmHkl();
394
            TlsFree(tlsIndex);
395 396 397 398 399
            break;
    }
    return TRUE;
}

400
/* for posting messages as the IME */
401
static void ImmInternalPostIMEMessage(InputContextData *data, UINT msg, WPARAM wParam, LPARAM lParam)
402 403 404
{
    HWND target = GetFocus();
    if (!target)
405
       PostMessageW(data->IMC.hWnd,msg,wParam,lParam);
406
    else
407 408 409
       PostMessageW(target, msg, wParam, lParam);
}

410
static LRESULT ImmInternalSendIMENotify(InputContextData *data, WPARAM notify, LPARAM lParam)
411
{
412
    HWND target;
413

414
    target = data->IMC.hWnd;
415 416 417 418
    if (!target) target = GetFocus();

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

420
    return 0;
421
}
422

423 424 425 426 427
static HIMCC ImmCreateBlankCompStr(void)
{
    HIMCC rc;
    LPCOMPOSITIONSTRING ptr;
    rc = ImmCreateIMCC(sizeof(COMPOSITIONSTRING));
428
    ptr = ImmLockIMCC(rc);
429 430 431 432 433 434
    memset(ptr,0,sizeof(COMPOSITIONSTRING));
    ptr->dwSize = sizeof(COMPOSITIONSTRING);
    ImmUnlockIMCC(rc);
    return rc;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
435
/***********************************************************************
436
 *		ImmAssociateContext (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
437
 */
438
HIMC WINAPI ImmAssociateContext(HWND hWnd, HIMC hIMC)
Alexandre Julliard's avatar
Alexandre Julliard committed
439
{
440
    HIMC old = NULL;
441
    InputContextData *data = hIMC;
442

443
    TRACE("(%p, %p):\n", hWnd, hIMC);
444

445 446
    if (!IMM_GetThreadData()->defaultContext)
        IMM_GetThreadData()->defaultContext = ImmCreateContext();
447 448 449 450

    /*
     * If already associated just return
     */
451
    if (hIMC && data->IMC.hWnd == hWnd)
452
        return hIMC;
453

454 455
    if (hWnd)
    {
456
        old = RemovePropW(hWnd,szwWineIMCProperty);
457 458

        if (old == NULL)
459
            old = IMM_GetThreadData()->defaultContext;
460 461 462
        else if (old == (HIMC)-1)
            old = NULL;

463
        if (hIMC != IMM_GetThreadData()->defaultContext)
464 465 466 467
        {
            if (hIMC == NULL) /* Meaning disable imm for that window*/
                SetPropW(hWnd,szwWineIMCProperty,(HANDLE)-1);
            else
468
                SetPropW(hWnd,szwWineIMCProperty,hIMC);
469
        }
470 471 472

        if (old)
        {
473
            InputContextData *old_data = old;
474 475 476
            if (old_data->IMC.hWnd == hWnd)
                old_data->IMC.hWnd = NULL;
        }
477 478 479 480 481
    }

    if (!hIMC)
        return old;

482
    if (IsWindow(data->IMC.hWnd))
483 484 485 486
    {
        /*
         * Post a message that your context is switching
         */
487
        SendMessageW(data->IMC.hWnd, WM_IME_SETCONTEXT, FALSE, ISC_SHOWUIALL);
488 489
    }

490
    data->IMC.hWnd = hWnd;
491

492
    if (IsWindow(data->IMC.hWnd))
493 494 495 496
    {
        /*
         * Post a message that your context is switching
         */
497
        SendMessageW(data->IMC.hWnd, WM_IME_SETCONTEXT, TRUE, ISC_SHOWUIALL);
498 499
    }

500
    return old;
Alexandre Julliard's avatar
Alexandre Julliard committed
501 502
}

503 504 505 506 507 508 509 510 511 512 513

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

514 515 516 517 518
/***********************************************************************
 *              ImmAssociateContextEx (IMM32.@)
 */
BOOL WINAPI ImmAssociateContextEx(HWND hWnd, HIMC hIMC, DWORD dwFlags)
{
519
    TRACE("(%p, %p, 0x%x):\n", hWnd, hIMC, dwFlags);
520 521 522 523

    if (!IMM_GetThreadData()->defaultContext)
        IMM_GetThreadData()->defaultContext = ImmCreateContext();

524 525 526
    if (!hWnd) return FALSE;

    switch (dwFlags)
527
    {
528 529 530 531
    case 0:
        ImmAssociateContext(hWnd,hIMC);
        return TRUE;
    case IACE_DEFAULT:
532 533
        ImmAssociateContext(hWnd,IMM_GetThreadData()->defaultContext);
        return TRUE;
534
    case IACE_IGNORENOCONTEXT:
535
        if (GetPropW(hWnd,szwWineIMCProperty))
536 537
            ImmAssociateContext(hWnd,hIMC);
        return TRUE;
538
    case IACE_CHILDREN:
539 540
        EnumChildWindows(hWnd,_ImmAssociateContextExEnumProc,(LPARAM)hIMC);
        return TRUE;
541 542
    default:
        FIXME("Unknown dwFlags 0x%x\n",dwFlags);
543 544
        return FALSE;
    }
545 546
}

Alexandre Julliard's avatar
Alexandre Julliard committed
547
/***********************************************************************
548
 *		ImmConfigureIMEA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
549
 */
550 551
BOOL WINAPI ImmConfigureIMEA(
  HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
Alexandre Julliard's avatar
Alexandre Julliard committed
552
{
553 554 555 556
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);

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

557 558 559
    if (dwMode == IME_CONFIG_REGISTERWORD && !lpData)
        return FALSE;

560 561 562 563 564 565 566
    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;
567
            REGISTERWORDA *rwa = lpData;
568 569 570 571 572 573 574 575 576 577 578 579
            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
580 581 582
}

/***********************************************************************
583
 *		ImmConfigureIMEW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
584
 */
585 586
BOOL WINAPI ImmConfigureIMEW(
  HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
Alexandre Julliard's avatar
Alexandre Julliard committed
587
{
588 589 590 591
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);

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

592 593 594
    if (dwMode == IME_CONFIG_REGISTERWORD && !lpData)
        return FALSE;

595 596 597 598 599 600
    if (immHkl->hIME && immHkl->pImeConfigure)
    {
        if (dwMode != IME_CONFIG_REGISTERWORD || is_kbd_ime_unicode(immHkl))
            return immHkl->pImeConfigure(hKL,hWnd,dwMode,lpData);
        else
        {
601
            REGISTERWORDW *rww = lpData;
602 603 604 605 606 607 608 609 610 611 612 613 614
            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
615 616
}

Eric Kohl's avatar
Eric Kohl committed
617
/***********************************************************************
618
 *		ImmCreateContext (IMM32.@)
Eric Kohl's avatar
Eric Kohl committed
619
 */
620
HIMC WINAPI ImmCreateContext(void)
Eric Kohl's avatar
Eric Kohl committed
621
{
622
    InputContextData *new_context;
623 624
    LPGUIDELINE gl;
    LPCANDIDATEINFO ci;
625

626
    new_context = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(InputContextData));
627

628 629 630 631
    /* Load the IME */
    new_context->immKbd = IMM_GetImmHkl(GetKeyboardLayout(0));

    if (!new_context->immKbd->hIME)
632
    {
633
        TRACE("IME dll could not be loaded\n");
634 635 636
        HeapFree(GetProcessHeap(),0,new_context);
        return 0;
    }
637

638
    /* the HIMCCs are never NULL */
639
    new_context->IMC.hCompStr = ImmCreateBlankCompStr();
640 641 642 643 644 645 646 647 648 649 650
    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);
651

652 653 654
    /* Initialize the IME Private */
    new_context->IMC.hPrivate = ImmCreateIMCC(new_context->immKbd->imeInfo.dwPrivateDataSize);

655
    if (!new_context->immKbd->pImeSelect(new_context, TRUE))
656 657
    {
        TRACE("Selection of IME failed\n");
658
        IMM_DestroyContext(new_context);
659 660
        return 0;
    }
661
    SendMessageW(GetFocus(), WM_IME_SELECT, TRUE, (LPARAM)GetKeyboardLayout(0));
662 663

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

666
    return new_context;
667 668
}

669
static BOOL IMM_DestroyContext(HIMC hIMC)
670
{
671
    InputContextData *data = hIMC;
672 673 674 675 676

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

    if (hIMC)
    {
677
        data->immKbd->uSelected --;
678
        data->immKbd->pImeSelect(hIMC, FALSE);
679
        SendMessageW(data->IMC.hWnd, WM_IME_SELECT, FALSE, (LPARAM)GetKeyboardLayout(0));
680

681 682
        if (IMM_GetThreadData()->hwndDefault == data->imeWnd)
            IMM_GetThreadData()->hwndDefault = NULL;
683
        DestroyWindow(data->imeWnd);
684

685 686 687 688 689
        ImmDestroyIMCC(data->IMC.hCompStr);
        ImmDestroyIMCC(data->IMC.hCandInfo);
        ImmDestroyIMCC(data->IMC.hGuideLine);
        ImmDestroyIMCC(data->IMC.hPrivate);
        ImmDestroyIMCC(data->IMC.hMsgBuf);
690 691 692 693

        HeapFree(GetProcessHeap(),0,data);
    }
    return TRUE;
Eric Kohl's avatar
Eric Kohl committed
694 695
}

696 697 698 699 700 701 702 703 704 705 706
/***********************************************************************
 *		ImmDestroyContext (IMM32.@)
 */
BOOL WINAPI ImmDestroyContext(HIMC hIMC)
{
    if (hIMC != IMM_GetThreadData()->defaultContext)
        return IMM_DestroyContext(hIMC);
    else
        return FALSE;
}

707 708 709 710 711
/***********************************************************************
 *		ImmDisableIME (IMM32.@)
 */
BOOL WINAPI ImmDisableIME(DWORD idThread)
{
712
    FIXME("(%d): stub\n", idThread);
713 714 715
    return TRUE;
}

716 717 718 719 720 721 722 723
/***********************************************************************
 *		ImmEnumRegisterWordA (IMM32.@)
 */
UINT WINAPI ImmEnumRegisterWordA(
  HKL hKL, REGISTERWORDENUMPROCA lpfnEnumProc,
  LPCSTR lpszReading, DWORD dwStyle,
  LPCSTR lpszRegister, LPVOID lpData)
{
724 725 726 727 728 729 730 731 732 733
    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
        {
734 735 736 737 738 739 740 741 742 743 744
            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;
745 746 747 748
        }
    }
    else
        return 0;
749 750 751 752 753 754 755 756 757 758
}

/***********************************************************************
 *		ImmEnumRegisterWordW (IMM32.@)
 */
UINT WINAPI ImmEnumRegisterWordW(
  HKL hKL, REGISTERWORDENUMPROCW lpfnEnumProc,
  LPCWSTR lpszReading, DWORD dwStyle,
  LPCWSTR lpszRegister, LPVOID lpData)
{
759 760 761 762 763 764 765 766 767 768
    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
        {
769 770 771 772 773 774 775 776 777 778
            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;
779 780 781 782
        }
    }
    else
        return 0;
783 784
}

785 786 787 788 789 790 791 792 793 794
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
795
/***********************************************************************
796
 *		ImmEscapeA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
797
 */
798
LRESULT WINAPI ImmEscapeA(
799
  HKL hKL, HIMC hIMC,
800
  UINT uEscape, LPVOID lpData)
Alexandre Julliard's avatar
Alexandre Julliard committed
801
{
802 803 804 805 806
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %p, %d, %p):\n", hKL, hIMC, uEscape, lpData);

    if (immHkl->hIME && immHkl->pImeEscape)
    {
807
        if (!EscapeRequiresWA(uEscape) || !is_kbd_ime_unicode(immHkl))
808 809 810
            return immHkl->pImeEscape(hIMC,uEscape,lpData);
        else
        {
811 812 813 814
            WCHAR buffer[81]; /* largest required buffer should be 80 */
            LRESULT rc;
            if (uEscape == IME_ESC_SET_EUDC_DICTIONARY)
            {
815
                MultiByteToWideChar(CP_ACP,0,lpData,-1,buffer,81);
816 817 818 819 820
                rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
            }
            else
            {
                rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
821
                WideCharToMultiByte(CP_ACP,0,buffer,-1,lpData,80, NULL, NULL);
822 823
            }
            return rc;
824 825 826 827
        }
    }
    else
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
828 829 830
}

/***********************************************************************
831
 *		ImmEscapeW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
832
 */
833 834 835
LRESULT WINAPI ImmEscapeW(
  HKL hKL, HIMC hIMC,
  UINT uEscape, LPVOID lpData)
Alexandre Julliard's avatar
Alexandre Julliard committed
836
{
837 838 839 840 841
    ImmHkl *immHkl = IMM_GetImmHkl(hKL);
    TRACE("(%p, %p, %d, %p):\n", hKL, hIMC, uEscape, lpData);

    if (immHkl->hIME && immHkl->pImeEscape)
    {
842
        if (!EscapeRequiresWA(uEscape) || is_kbd_ime_unicode(immHkl))
843 844 845
            return immHkl->pImeEscape(hIMC,uEscape,lpData);
        else
        {
846 847 848 849
            CHAR buffer[81]; /* largest required buffer should be 80 */
            LRESULT rc;
            if (uEscape == IME_ESC_SET_EUDC_DICTIONARY)
            {
850
                WideCharToMultiByte(CP_ACP,0,lpData,-1,buffer,81, NULL, NULL);
851 852 853 854 855
                rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
            }
            else
            {
                rc = immHkl->pImeEscape(hIMC,uEscape,buffer);
856
                MultiByteToWideChar(CP_ACP,0,buffer,-1,lpData,80);
857 858
            }
            return rc;
859 860 861 862
        }
    }
    else
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
863 864 865
}

/***********************************************************************
866
 *		ImmGetCandidateListA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
867
 */
868
DWORD WINAPI ImmGetCandidateListA(
869
  HIMC hIMC, DWORD dwIndex,
Alexandre Julliard's avatar
Alexandre Julliard committed
870
  LPCANDIDATELIST lpCandList, DWORD dwBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
871
{
872
    InputContextData *data = hIMC;
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
    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);
    if ( dwIndex >= candinfo->dwCount ||
         dwIndex >= (sizeof(candinfo->dwOffset) / sizeof(DWORD)) )
        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
903 904 905
}

/***********************************************************************
906
 *		ImmGetCandidateListCountA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
907
 */
908 909
DWORD WINAPI ImmGetCandidateListCountA(
  HIMC hIMC, LPDWORD lpdwListCount)
Alexandre Julliard's avatar
Alexandre Julliard committed
910
{
911
    InputContextData *data = hIMC;
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
    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
935 936 937
}

/***********************************************************************
938
 *		ImmGetCandidateListCountW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
939
 */
940 941
DWORD WINAPI ImmGetCandidateListCountW(
  HIMC hIMC, LPDWORD lpdwListCount)
Alexandre Julliard's avatar
Alexandre Julliard committed
942
{
943
    InputContextData *data = hIMC;
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
    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
967 968 969
}

/***********************************************************************
970
 *		ImmGetCandidateListW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
971
 */
972
DWORD WINAPI ImmGetCandidateListW(
973
  HIMC hIMC, DWORD dwIndex,
Alexandre Julliard's avatar
Alexandre Julliard committed
974
  LPCANDIDATELIST lpCandList, DWORD dwBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
975
{
976
    InputContextData *data = hIMC;
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    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);
    if ( dwIndex >= candinfo->dwCount ||
         dwIndex >= (sizeof(candinfo->dwOffset) / sizeof(DWORD)) )
        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
1007 1008 1009
}

/***********************************************************************
1010
 *		ImmGetCandidateWindow (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1011
 */
1012
BOOL WINAPI ImmGetCandidateWindow(
1013
  HIMC hIMC, DWORD dwIndex, LPCANDIDATEFORM lpCandidate)
Alexandre Julliard's avatar
Alexandre Julliard committed
1014
{
1015
    InputContextData *data = hIMC;
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

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

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

    if ( dwIndex >= (sizeof(data->IMC.cfCandForm) / sizeof(CANDIDATEFORM)) )
        return FALSE;

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

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1028 1029 1030
}

/***********************************************************************
1031
 *		ImmGetCompositionFontA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1032
 */
1033
BOOL WINAPI ImmGetCompositionFontA(HIMC hIMC, LPLOGFONTA lplf)
Alexandre Julliard's avatar
Alexandre Julliard committed
1034
{
1035 1036 1037 1038 1039 1040
    LOGFONTW lfW;
    BOOL rc;

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

    rc = ImmGetCompositionFontW(hIMC,&lfW);
1041 1042 1043 1044 1045
    if (!rc || !lplf)
        return FALSE;

    memcpy(lplf,&lfW,sizeof(LOGFONTA));
    WideCharToMultiByte(CP_ACP, 0, lfW.lfFaceName, -1, lplf->lfFaceName,
1046
                        LF_FACESIZE, NULL, NULL);
1047
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1048 1049 1050
}

/***********************************************************************
1051
 *		ImmGetCompositionFontW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1052
 */
1053
BOOL WINAPI ImmGetCompositionFontW(HIMC hIMC, LPLOGFONTW lplf)
Alexandre Julliard's avatar
Alexandre Julliard committed
1054
{
1055
    InputContextData *data = hIMC;
1056 1057 1058

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

1059
    if (!data || !lplf)
1060 1061 1062 1063 1064
        return FALSE;

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

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1065 1066
}

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

/* Helpers for the GetCompositionString functions */

static INT CopyCompStringIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE target, INT tlen,
                                     BOOL unicode )
{
    INT rc;

    if (is_himc_ime_unicode(data) && !unicode)
        rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)source, slen, (LPSTR)target, tlen, NULL, NULL);
    else if (!is_himc_ime_unicode(data) && unicode)
        rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)source, slen, (LPWSTR)target, tlen) * sizeof(WCHAR);
    else
    {
        int dlen = (unicode)?sizeof(WCHAR):sizeof(CHAR);
        memcpy( target, source, min(slen,tlen)*dlen);
        rc = slen*dlen;
    }

    return rc;
}

static INT CopyCompAttrIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE ssource, INT sslen,
                                   LPBYTE target, INT tlen, BOOL unicode )
{
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
    INT rc;

    if (is_himc_ime_unicode(data) && !unicode)
    {
        rc = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)ssource, sslen, NULL, 0, NULL, NULL);
        if (tlen)
        {
            const BYTE *src = source;
            LPBYTE dst = target;
            int i, j = 0, k = 0;

            if (rc < tlen)
                tlen = rc;
            for (i = 0; i < sslen; ++i)
            {
                int len;

                len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)ssource + i, 1,
                                          NULL, 0, NULL, NULL);
                for (; len > 0; --len)
                {
                    dst[j++] = src[k];

                    if (j >= tlen)
                        goto end;
                }
                ++k;
            }
        end:
            rc = j;
        }
    }
    else if (!is_himc_ime_unicode(data) && unicode)
    {
        rc = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ssource, sslen, NULL, 0);
        if (tlen)
        {
            const BYTE *src = source;
            LPBYTE dst = target;
            int i, j = 0;

            if (rc < tlen)
                tlen = rc;
            for (i = 0; i < sslen; ++i)
            {
                if (IsDBCSLeadByte(((LPSTR)ssource)[i]))
                    continue;

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

                if (j >= tlen)
                    break;
            }
            rc = j;
        }
    }
    else
    {
        memcpy( target, source, min(slen,tlen));
        rc = slen;
    }
1153

1154
    return rc;
1155 1156
}

1157
static INT CopyCompClauseIMEtoClient(InputContextData *data, LPBYTE source, INT slen, LPBYTE ssource,
1158 1159
                                     LPBYTE target, INT tlen, BOOL unicode )
{
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    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;
    }
1208

1209
    return rc;
1210 1211 1212 1213
}

static INT CopyCompOffsetIMEtoClient(InputContextData *data, DWORD offset, LPBYTE ssource, BOOL unicode)
{
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
    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;
1228 1229 1230 1231
}

static LONG ImmGetCompositionStringT( HIMC hIMC, DWORD dwIndex, LPVOID lpBuf,
                                      DWORD dwBufLen, BOOL unicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
1232
{
1233
    LONG rc = 0;
1234
    InputContextData *data = hIMC;
1235 1236
    LPCOMPOSITIONSTRING compstr;
    LPBYTE compdata;
1237

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

1240 1241
    if (!data)
       return FALSE;
1242

1243 1244 1245 1246 1247 1248
    if (!data->IMC.hCompStr)
       return FALSE;

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

1249
    switch (dwIndex)
1250
    {
1251
    case GCS_RESULTSTR:
1252 1253
        TRACE("GCS_RESULTSTR\n");
        rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwResultStrOffset, compstr->dwResultStrLen, lpBuf, dwBufLen, unicode);
1254 1255
        break;
    case GCS_COMPSTR:
1256 1257
        TRACE("GCS_COMPSTR\n");
        rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen, lpBuf, dwBufLen, unicode);
1258 1259
        break;
    case GCS_COMPATTR:
1260 1261 1262 1263
        TRACE("GCS_COMPATTR\n");
        rc = CopyCompAttrIMEtoClient(data, compdata + compstr->dwCompAttrOffset, compstr->dwCompAttrLen,
                                     compdata + compstr->dwCompStrOffset, compstr->dwCompStrLen,
                                     lpBuf, dwBufLen, unicode);
1264 1265
        break;
    case GCS_COMPCLAUSE:
1266 1267
        TRACE("GCS_COMPCLAUSE\n");
        rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwCompClauseOffset,compstr->dwCompClauseLen,
1268
                                       compdata + compstr->dwCompStrOffset,
1269
                                       lpBuf, dwBufLen, unicode);
1270 1271
        break;
    case GCS_RESULTCLAUSE:
1272 1273
        TRACE("GCS_RESULTCLAUSE\n");
        rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwResultClauseOffset,compstr->dwResultClauseLen,
1274
                                       compdata + compstr->dwResultStrOffset,
1275
                                       lpBuf, dwBufLen, unicode);
1276
        break;
1277
    case GCS_RESULTREADSTR:
1278 1279
        TRACE("GCS_RESULTREADSTR\n");
        rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwResultReadStrOffset, compstr->dwResultReadStrLen, lpBuf, dwBufLen, unicode);
1280 1281
        break;
    case GCS_RESULTREADCLAUSE:
1282 1283
        TRACE("GCS_RESULTREADCLAUSE\n");
        rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwResultReadClauseOffset,compstr->dwResultReadClauseLen,
1284
                                       compdata + compstr->dwResultStrOffset,
1285
                                       lpBuf, dwBufLen, unicode);
1286 1287
        break;
    case GCS_COMPREADSTR:
1288 1289
        TRACE("GCS_COMPREADSTR\n");
        rc = CopyCompStringIMEtoClient(data, compdata + compstr->dwCompReadStrOffset, compstr->dwCompReadStrLen, lpBuf, dwBufLen, unicode);
1290 1291
        break;
    case GCS_COMPREADATTR:
1292 1293 1294 1295
        TRACE("GCS_COMPREADATTR\n");
        rc = CopyCompAttrIMEtoClient(data, compdata + compstr->dwCompReadAttrOffset, compstr->dwCompReadAttrLen,
                                     compdata + compstr->dwCompReadStrOffset, compstr->dwCompReadStrLen,
                                     lpBuf, dwBufLen, unicode);
1296 1297
        break;
    case GCS_COMPREADCLAUSE:
1298 1299
        TRACE("GCS_COMPREADCLAUSE\n");
        rc = CopyCompClauseIMEtoClient(data, compdata + compstr->dwCompReadClauseOffset,compstr->dwCompReadClauseLen,
1300
                                       compdata + compstr->dwCompStrOffset,
1301
                                       lpBuf, dwBufLen, unicode);
1302
        break;
1303
    case GCS_CURSORPOS:
Kusanagi Kouichi's avatar
Kusanagi Kouichi committed
1304
        TRACE("GCS_CURSORPOS\n");
1305
        rc = CopyCompOffsetIMEtoClient(data, compstr->dwCursorPos, compdata + compstr->dwCompStrOffset, unicode);
1306 1307
        break;
    case GCS_DELTASTART:
1308
        TRACE("GCS_DELTASTART\n");
1309
        rc = CopyCompOffsetIMEtoClient(data, compstr->dwDeltaStart, compdata + compstr->dwCompStrOffset, unicode);
1310 1311
        break;
    default:
1312
        FIXME("Unhandled index 0x%x\n",dwIndex);
1313
        break;
1314
    }
1315

1316 1317
    ImmUnlockIMCC(data->IMC.hCompStr);

1318
    return rc;
Alexandre Julliard's avatar
Alexandre Julliard committed
1319 1320
}

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
/***********************************************************************
 *		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
1331
/***********************************************************************
1332
 *		ImmGetCompositionStringW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1333
 */
1334
LONG WINAPI ImmGetCompositionStringW(
1335
  HIMC hIMC, DWORD dwIndex,
Alexandre Julliard's avatar
Alexandre Julliard committed
1336
  LPVOID lpBuf, DWORD dwBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1337
{
1338
    return ImmGetCompositionStringT(hIMC, dwIndex, lpBuf, dwBufLen, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1339 1340 1341
}

/***********************************************************************
1342
 *		ImmGetCompositionWindow (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1343
 */
1344
BOOL WINAPI ImmGetCompositionWindow(HIMC hIMC, LPCOMPOSITIONFORM lpCompForm)
Alexandre Julliard's avatar
Alexandre Julliard committed
1345
{
1346
    InputContextData *data = hIMC;
1347 1348 1349 1350 1351 1352

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

    if (!data)
        return FALSE;

1353
    *lpCompForm = data->IMC.cfCompForm;
1354
    return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1355 1356 1357
}

/***********************************************************************
1358
 *		ImmGetContext (IMM32.@)
1359
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1360
 */
1361
HIMC WINAPI ImmGetContext(HWND hWnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
1362
{
1363
    HIMC rc;
1364

1365
    TRACE("%p\n", hWnd);
1366 1367 1368 1369 1370 1371

    if (!IsWindow(hWnd))
    {
        SetLastError(ERROR_INVALID_WINDOW_HANDLE);
        return NULL;
    }
1372 1373
    if (!IMM_GetThreadData()->defaultContext)
        IMM_GetThreadData()->defaultContext = ImmCreateContext();
1374

1375
    rc = GetPropW(hWnd,szwWineIMCProperty);
1376 1377 1378
    if (rc == (HIMC)-1)
        rc = NULL;
    else if (rc == NULL)
1379
        rc = IMM_GetThreadData()->defaultContext;
1380

1381 1382
    if (rc)
    {
1383
        InputContextData *data = rc;
1384 1385 1386 1387 1388
        data->IMC.hWnd = hWnd;
    }
    TRACE("returning %p\n", rc);

    return rc;
Alexandre Julliard's avatar
Alexandre Julliard committed
1389 1390 1391
}

/***********************************************************************
1392
 *		ImmGetConversionListA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1393
 */
1394 1395
DWORD WINAPI ImmGetConversionListA(
  HKL hKL, HIMC hIMC,
Alexandre Julliard's avatar
Alexandre Julliard committed
1396
  LPCSTR pSrc, LPCANDIDATELIST lpDst,
1397
  DWORD dwBufLen, UINT uFlag)
Alexandre Julliard's avatar
Alexandre Julliard committed
1398
{
1399 1400 1401 1402 1403 1404 1405 1406 1407
    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
        {
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
            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;
1423 1424 1425 1426
        }
    }
    else
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1427 1428 1429
}

/***********************************************************************
1430
 *		ImmGetConversionListW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1431
 */
1432 1433
DWORD WINAPI ImmGetConversionListW(
  HKL hKL, HIMC hIMC,
Alexandre Julliard's avatar
Alexandre Julliard committed
1434
  LPCWSTR pSrc, LPCANDIDATELIST lpDst,
1435
  DWORD dwBufLen, UINT uFlag)
Alexandre Julliard's avatar
Alexandre Julliard committed
1436
{
1437 1438 1439 1440 1441 1442 1443 1444 1445
    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
        {
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
            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;
1461 1462 1463 1464
        }
    }
    else
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1465 1466 1467
}

/***********************************************************************
1468
 *		ImmGetConversionStatus (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1469
 */
1470 1471
BOOL WINAPI ImmGetConversionStatus(
  HIMC hIMC, LPDWORD lpfdwConversion, LPDWORD lpfdwSentence)
Alexandre Julliard's avatar
Alexandre Julliard committed
1472
{
1473
    InputContextData *data = hIMC;
1474 1475 1476 1477 1478 1479

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

    if (!data)
        return FALSE;

1480
    if (lpfdwConversion)
1481
        *lpfdwConversion = data->IMC.fdwConversion;
1482
    if (lpfdwSentence)
1483 1484
        *lpfdwSentence = data->IMC.fdwSentence;

1485
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1486 1487 1488
}

/***********************************************************************
1489
 *		ImmGetDefaultIMEWnd (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1490
 */
1491
HWND WINAPI ImmGetDefaultIMEWnd(HWND hWnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
1492
{
1493
    TRACE("Default is %p\n",IMM_GetThreadData()->hwndDefault);
1494
    return IMM_GetThreadData()->hwndDefault;
Alexandre Julliard's avatar
Alexandre Julliard committed
1495 1496 1497
}

/***********************************************************************
1498
 *		ImmGetDescriptionA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1499
 */
1500 1501
UINT WINAPI ImmGetDescriptionA(
  HKL hKL, LPSTR lpszDescription, UINT uBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1502
{
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
  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 );

  /* allocate a buffer of that size */
  buf = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof (WCHAR) );
  if( !buf )
Alexandre Julliard's avatar
Alexandre Julliard committed
1514
  return 0;
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525

  /* 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 );

  return len;
Alexandre Julliard's avatar
Alexandre Julliard committed
1526 1527 1528
}

/***********************************************************************
1529
 *		ImmGetDescriptionW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1530
 */
1531
UINT WINAPI ImmGetDescriptionW(HKL hKL, LPWSTR lpszDescription, UINT uBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1532
{
1533 1534 1535 1536 1537 1538 1539
  static const WCHAR name[] = { 'W','i','n','e',' ','X','I','M',0 };

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

  if (!uBufLen) return lstrlenW( name );
  lstrcpynW( lpszDescription, name, uBufLen );
  return lstrlenW( lpszDescription );
Alexandre Julliard's avatar
Alexandre Julliard committed
1540 1541 1542
}

/***********************************************************************
1543
 *		ImmGetGuideLineA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1544
 */
1545 1546
DWORD WINAPI ImmGetGuideLineA(
  HIMC hIMC, DWORD dwIndex, LPSTR lpBuf, DWORD dwBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1547
{
1548
  FIXME("(%p, %d, %s, %d): stub\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1549 1550 1551 1552
    hIMC, dwIndex, debugstr_a(lpBuf), dwBufLen
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1553 1554 1555
}

/***********************************************************************
1556
 *		ImmGetGuideLineW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1557
 */
1558
DWORD WINAPI ImmGetGuideLineW(HIMC hIMC, DWORD dwIndex, LPWSTR lpBuf, DWORD dwBufLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
1559
{
1560
  FIXME("(%p, %d, %s, %d): stub\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1561 1562 1563 1564
    hIMC, dwIndex, debugstr_w(lpBuf), dwBufLen
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1565 1566
}

1567 1568 1569
/***********************************************************************
 *		ImmGetIMEFileNameA (IMM32.@)
 */
1570
UINT WINAPI ImmGetIMEFileNameA( HKL hKL, LPSTR lpszFileName, UINT uBufLen)
1571
{
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
    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;
1598 1599 1600 1601 1602
}

/***********************************************************************
 *		ImmGetIMEFileNameW (IMM32.@)
 */
1603
UINT WINAPI ImmGetIMEFileNameW(HKL hKL, LPWSTR lpszFileName, UINT uBufLen)
1604
{
1605 1606 1607
    HKEY hkey;
    DWORD length;
    DWORD rc;
1608
    WCHAR regKey[sizeof(szImeRegFmt)/sizeof(WCHAR)+8];
1609

1610
    wsprintfW( regKey, szImeRegFmt, (ULONG_PTR)hKL );
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
    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);
1644 1645
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1646
/***********************************************************************
1647
 *		ImmGetOpenStatus (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1648
 */
1649
BOOL WINAPI ImmGetOpenStatus(HIMC hIMC)
Alexandre Julliard's avatar
Alexandre Julliard committed
1650
{
1651
  InputContextData *data = hIMC;
1652
  static int i;
1653

1654 1655
    if (!data)
        return FALSE;
1656 1657 1658 1659 1660

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

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

1662
  return data->IMC.fOpen;
Alexandre Julliard's avatar
Alexandre Julliard committed
1663 1664
}

1665 1666 1667 1668 1669
/***********************************************************************
 *		ImmGetProperty (IMM32.@)
 */
DWORD WINAPI ImmGetProperty(HKL hKL, DWORD fdwIndex)
{
1670
    DWORD rc = 0;
1671 1672
    ImmHkl *kbd;

1673
    TRACE("(%p, %d)\n", hKL, fdwIndex);
1674
    kbd = IMM_GetImmHkl(hKL);
1675

1676
    if (kbd && kbd->hIME)
1677
    {
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
        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;
        }
1689 1690
    }
    return rc;
1691 1692 1693 1694 1695 1696 1697 1698
}

/***********************************************************************
 *		ImmGetRegisterWordStyleA (IMM32.@)
 */
UINT WINAPI ImmGetRegisterWordStyleA(
  HKL hKL, UINT nItem, LPSTYLEBUFA lpStyleBuf)
{
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
    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;
1719 1720 1721 1722 1723 1724 1725 1726
}

/***********************************************************************
 *		ImmGetRegisterWordStyleW (IMM32.@)
 */
UINT WINAPI ImmGetRegisterWordStyleW(
  HKL hKL, UINT nItem, LPSTYLEBUFW lpStyleBuf)
{
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
    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;
1747 1748
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1749
/***********************************************************************
1750
 *		ImmGetStatusWindowPos (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1751
 */
1752
BOOL WINAPI ImmGetStatusWindowPos(HIMC hIMC, LPPOINT lpptPos)
Alexandre Julliard's avatar
Alexandre Julliard committed
1753
{
1754
    InputContextData *data = hIMC;
1755 1756 1757 1758 1759 1760 1761 1762 1763

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

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

    *lpptPos = data->IMC.ptStatusWndPos;

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1764 1765 1766
}

/***********************************************************************
1767
 *		ImmGetVirtualKey (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1768
 */
1769
UINT WINAPI ImmGetVirtualKey(HWND hWnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
1770
{
1771
  OSVERSIONINFOA version;
1772
  InputContextData *data = ImmGetContext( hWnd );
1773 1774 1775 1776 1777
  TRACE("%p\n", hWnd);

  if ( data )
      return data->lastVK;

1778
  version.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
1779 1780 1781 1782
  GetVersionExA( &version );
  switch(version.dwPlatformId)
  {
  case VER_PLATFORM_WIN32_WINDOWS:
Alexandre Julliard's avatar
Alexandre Julliard committed
1783
      return VK_PROCESSKEY;
1784
  case VER_PLATFORM_WIN32_NT:
Alexandre Julliard's avatar
Alexandre Julliard committed
1785
      return 0;
1786
  default:
1787
      FIXME("%d not supported\n",version.dwPlatformId);
1788 1789
      return VK_PROCESSKEY;
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
1790 1791
}

1792 1793 1794 1795 1796 1797
/***********************************************************************
 *		ImmInstallIMEA (IMM32.@)
 */
HKL WINAPI ImmInstallIMEA(
  LPCSTR lpszIMEFileName, LPCSTR lpszLayoutText)
{
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
    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;
1813 1814 1815 1816 1817 1818 1819 1820
}

/***********************************************************************
 *		ImmInstallIMEW (IMM32.@)
 */
HKL WINAPI ImmInstallIMEW(
  LPCWSTR lpszIMEFileName, LPCWSTR lpszLayoutText)
{
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
    INT lcid = GetUserDefaultLCID();
    INT count;
    HKL hkl;
    DWORD rc;
    HKEY hkey;
    WCHAR regKey[sizeof(szImeRegFmt)/sizeof(WCHAR)+8];

    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;

1838 1839
        hkl = (HKL)MAKELPARAM( lcid, 0xe000 | count );
        wsprintfW( regKey, szImeRegFmt, (ULONG_PTR)hkl);
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857

        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)
    {
1858
        rc = RegSetValueExW(hkey, szImeFileW, 0, REG_SZ, (const BYTE*)lpszIMEFileName,
1859 1860
                            (lstrlenW(lpszIMEFileName) + 1) * sizeof(WCHAR));
        if (rc == ERROR_SUCCESS)
1861
            rc = RegSetValueExW(hkey, szLayoutTextW, 0, REG_SZ, (const BYTE*)lpszLayoutText,
1862 1863 1864 1865 1866 1867 1868 1869 1870
                                (lstrlenW(lpszLayoutText) + 1) * sizeof(WCHAR));
        RegCloseKey(hkey);
        return hkl;
    }
    else
    {
        WARN("Unable to set IME registry values\n");
        return 0;
    }
1871 1872 1873 1874 1875 1876 1877
}

/***********************************************************************
 *		ImmIsIME (IMM32.@)
 */
BOOL WINAPI ImmIsIME(HKL hKL)
{
1878 1879 1880 1881
    ImmHkl *ptr;
    TRACE("(%p):\n", hKL);
    ptr = IMM_GetImmHkl(hKL);
    return (ptr && ptr->hIME);
1882 1883 1884 1885 1886 1887 1888 1889
}

/***********************************************************************
 *		ImmIsUIMessageA (IMM32.@)
 */
BOOL WINAPI ImmIsUIMessageA(
  HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam)
{
1890 1891
    BOOL rc = FALSE;

1892
    TRACE("(%p, %x, %ld, %ld)\n", hWndIME, msg, wParam, lParam);
1893
    if ((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
            (msg == WM_IME_SETCONTEXT) ||
            (msg == WM_IME_NOTIFY) ||
            (msg == WM_IME_COMPOSITIONFULL) ||
            (msg == WM_IME_SELECT) ||
            (msg == 0x287 /* FIXME: WM_IME_SYSTEM */) ||
            (msg == WM_MSIME_RECONVERTOPTIONS) ||
            (msg == WM_MSIME_MOUSE) ||
            (msg == WM_MSIME_RECONVERTREQUEST) ||
            (msg == WM_MSIME_RECONVERT) ||
            (msg == WM_MSIME_QUERYPOSITION) ||
            (msg == WM_MSIME_DOCUMENTFEED))
1905
    {
1906 1907
        if (hWndIME)
            SendMessageA(hWndIME, msg, wParam, lParam);
1908 1909 1910 1911

        rc = TRUE;
    }
    return rc;
1912 1913 1914 1915 1916 1917 1918 1919
}

/***********************************************************************
 *		ImmIsUIMessageW (IMM32.@)
 */
BOOL WINAPI ImmIsUIMessageW(
  HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam)
{
1920
    BOOL rc = FALSE;
1921 1922

    TRACE("(%p, %x, %ld, %ld)\n", hWndIME, msg, wParam, lParam);
1923
    if ((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
            (msg == WM_IME_SETCONTEXT) ||
            (msg == WM_IME_NOTIFY) ||
            (msg == WM_IME_COMPOSITIONFULL) ||
            (msg == WM_IME_SELECT) ||
            (msg == 0x287 /* FIXME: WM_IME_SYSTEM */) ||
            (msg == WM_MSIME_RECONVERTOPTIONS) ||
            (msg == WM_MSIME_MOUSE) ||
            (msg == WM_MSIME_RECONVERTREQUEST) ||
            (msg == WM_MSIME_RECONVERT) ||
            (msg == WM_MSIME_QUERYPOSITION) ||
            (msg == WM_MSIME_DOCUMENTFEED))
1935
    {
1936 1937
        if (hWndIME)
            SendMessageW(hWndIME, msg, wParam, lParam);
1938

1939
        rc = TRUE;
1940
    }
1941
    return rc;
1942 1943
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1944
/***********************************************************************
1945
 *		ImmNotifyIME (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1946
 */
1947 1948
BOOL WINAPI ImmNotifyIME(
  HIMC hIMC, DWORD dwAction, DWORD dwIndex, DWORD dwValue)
Alexandre Julliard's avatar
Alexandre Julliard committed
1949
{
1950
    InputContextData *data = hIMC;
1951

1952
    TRACE("(%p, %d, %d, %d)\n",
1953 1954
        hIMC, dwAction, dwIndex, dwValue);

1955 1956
    if (!data || ! data->immKbd->pNotifyIME)
        return FALSE;
1957

1958
    return data->immKbd->pNotifyIME(hIMC,dwAction,dwIndex,dwValue);
Alexandre Julliard's avatar
Alexandre Julliard committed
1959 1960 1961
}

/***********************************************************************
1962
 *		ImmRegisterWordA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1963
 */
1964 1965
BOOL WINAPI ImmRegisterWordA(
  HKL hKL, LPCSTR lpszReading, DWORD dwStyle, LPCSTR lpszRegister)
Alexandre Julliard's avatar
Alexandre Julliard committed
1966
{
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
    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
1989 1990 1991
}

/***********************************************************************
1992
 *		ImmRegisterWordW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1993
 */
1994 1995
BOOL WINAPI ImmRegisterWordW(
  HKL hKL, LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszRegister)
Alexandre Julliard's avatar
Alexandre Julliard committed
1996
{
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
    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
2019 2020 2021
}

/***********************************************************************
2022
 *		ImmReleaseContext (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2023
 */
2024
BOOL WINAPI ImmReleaseContext(HWND hWnd, HIMC hIMC)
Alexandre Julliard's avatar
Alexandre Julliard committed
2025
{
2026
  static int shown = 0;
2027

2028 2029 2030 2031 2032
  if (!shown) {
     FIXME("(%p, %p): stub\n", hWnd, hIMC);
     shown = 1;
  }
  return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2033 2034
}

2035 2036 2037 2038 2039
/***********************************************************************
*              ImmRequestMessageA(IMM32.@)
*/
LRESULT WINAPI ImmRequestMessageA(HIMC hIMC, WPARAM wParam, LPARAM lParam)
{
2040
    InputContextData *data = hIMC;
2041 2042 2043 2044 2045 2046 2047

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

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

     return 0;
2048 2049 2050 2051 2052 2053 2054
}

/***********************************************************************
*              ImmRequestMessageW(IMM32.@)
*/
LRESULT WINAPI ImmRequestMessageW(HIMC hIMC, WPARAM wParam, LPARAM lParam)
{
2055
    InputContextData *data = hIMC;
2056 2057 2058 2059 2060 2061 2062

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

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

     return 0;
2063 2064
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2065
/***********************************************************************
2066
 *		ImmSetCandidateWindow (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2067
 */
2068 2069
BOOL WINAPI ImmSetCandidateWindow(
  HIMC hIMC, LPCANDIDATEFORM lpCandidate)
Alexandre Julliard's avatar
Alexandre Julliard committed
2070
{
2071
    InputContextData *data = hIMC;
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091

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

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

    TRACE("\t%x, %x, (%i,%i), (%i,%i - %i,%i)\n",
            lpCandidate->dwIndex, lpCandidate->dwStyle,
            lpCandidate->ptCurrentPos.x, lpCandidate->ptCurrentPos.y,
            lpCandidate->rcArea.top, lpCandidate->rcArea.left,
            lpCandidate->rcArea.bottom, lpCandidate->rcArea.right);

    if ( lpCandidate->dwIndex >= (sizeof(data->IMC.cfCandForm) / sizeof(CANDIDATEFORM)) )
        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
2092 2093 2094
}

/***********************************************************************
2095
 *		ImmSetCompositionFontA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2096
 */
2097
BOOL WINAPI ImmSetCompositionFontA(HIMC hIMC, LPLOGFONTA lplf)
Alexandre Julliard's avatar
Alexandre Julliard committed
2098
{
2099
    InputContextData *data = hIMC;
2100 2101
    TRACE("(%p, %p)\n", hIMC, lplf);

2102
    if (!data || !lplf)
2103 2104
        return FALSE;

2105 2106
    memcpy(&data->IMC.lfFont.W,lplf,sizeof(LOGFONTA));
    MultiByteToWideChar(CP_ACP, 0, lplf->lfFaceName, -1, data->IMC.lfFont.W.lfFaceName,
2107
                        LF_FACESIZE);
2108
    ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCOMPOSITIONFONT);
2109
    ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONFONT, 0);
2110 2111

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2112 2113 2114
}

/***********************************************************************
2115
 *		ImmSetCompositionFontW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2116
 */
2117
BOOL WINAPI ImmSetCompositionFontW(HIMC hIMC, LPLOGFONTW lplf)
Alexandre Julliard's avatar
Alexandre Julliard committed
2118
{
2119
    InputContextData *data = hIMC;
2120 2121
    TRACE("(%p, %p)\n", hIMC, lplf);

2122
    if (!data || !lplf)
2123 2124
        return FALSE;

2125
    data->IMC.lfFont.W = *lplf;
2126
    ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, 0, IMC_SETCOMPOSITIONFONT);
2127
    ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONFONT, 0);
2128 2129

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2130 2131 2132
}

/***********************************************************************
2133
 *		ImmSetCompositionStringA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2134
 */
2135
BOOL WINAPI ImmSetCompositionStringA(
2136 2137
  HIMC hIMC, DWORD dwIndex,
  LPCVOID lpComp, DWORD dwCompLen,
Alexandre Julliard's avatar
Alexandre Julliard committed
2138
  LPCVOID lpRead, DWORD dwReadLen)
Alexandre Julliard's avatar
Alexandre Julliard committed
2139
{
2140 2141 2142 2143 2144
    DWORD comp_len;
    DWORD read_len;
    WCHAR *CompBuffer = NULL;
    WCHAR *ReadBuffer = NULL;
    BOOL rc;
2145
    InputContextData *data = hIMC;
2146

2147
    TRACE("(%p, %d, %p, %d, %p, %d):\n",
2148 2149
            hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);

2150 2151 2152
    if (!data)
        return FALSE;

2153 2154 2155 2156 2157 2158 2159
    if (!(dwIndex == SCS_SETSTR ||
          dwIndex == SCS_CHANGEATTR ||
          dwIndex == SCS_CHANGECLAUSE ||
          dwIndex == SCS_SETRECONVERTSTRING ||
          dwIndex == SCS_QUERYRECONVERTSTRING))
        return FALSE;

2160 2161 2162 2163
    if (!is_himc_ime_unicode(data))
        return data->immKbd->pImeSetCompositionString(hIMC, dwIndex, lpComp,
                        dwCompLen, lpRead, dwReadLen);

2164 2165 2166
    comp_len = MultiByteToWideChar(CP_ACP, 0, lpComp, dwCompLen, NULL, 0);
    if (comp_len)
    {
2167
        CompBuffer = HeapAlloc(GetProcessHeap(),0,comp_len * sizeof(WCHAR));
2168 2169 2170 2171 2172 2173
        MultiByteToWideChar(CP_ACP, 0, lpComp, dwCompLen, CompBuffer, comp_len);
    }

    read_len = MultiByteToWideChar(CP_ACP, 0, lpRead, dwReadLen, NULL, 0);
    if (read_len)
    {
2174
        ReadBuffer = HeapAlloc(GetProcessHeap(),0,read_len * sizeof(WCHAR));
2175 2176 2177 2178 2179 2180
        MultiByteToWideChar(CP_ACP, 0, lpRead, dwReadLen, ReadBuffer, read_len);
    }

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

2181 2182
    HeapFree(GetProcessHeap(), 0, CompBuffer);
    HeapFree(GetProcessHeap(), 0, ReadBuffer);
2183 2184

    return rc;
Alexandre Julliard's avatar
Alexandre Julliard committed
2185 2186 2187
}

/***********************************************************************
2188
 *		ImmSetCompositionStringW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2189
 */
2190 2191
BOOL WINAPI ImmSetCompositionStringW(
	HIMC hIMC, DWORD dwIndex,
Alexandre Julliard's avatar
Alexandre Julliard committed
2192 2193 2194
	LPCVOID lpComp, DWORD dwCompLen,
	LPCVOID lpRead, DWORD dwReadLen)
{
2195 2196 2197 2198 2199
    DWORD comp_len;
    DWORD read_len;
    CHAR *CompBuffer = NULL;
    CHAR *ReadBuffer = NULL;
    BOOL rc;
2200
    InputContextData *data = hIMC;
2201

2202 2203
    TRACE("(%p, %d, %p, %d, %p, %d):\n",
            hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
2204

2205
    if (!data)
2206
        return FALSE;
2207

2208 2209 2210 2211 2212 2213 2214
    if (!(dwIndex == SCS_SETSTR ||
          dwIndex == SCS_CHANGEATTR ||
          dwIndex == SCS_CHANGECLAUSE ||
          dwIndex == SCS_SETRECONVERTSTRING ||
          dwIndex == SCS_QUERYRECONVERTSTRING))
        return FALSE;

2215
    if (is_himc_ime_unicode(data))
2216 2217
        return data->immKbd->pImeSetCompositionString(hIMC, dwIndex, lpComp,
                        dwCompLen, lpRead, dwReadLen);
2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243

    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
2244 2245 2246
}

/***********************************************************************
2247
 *		ImmSetCompositionWindow (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2248
 */
2249 2250
BOOL WINAPI ImmSetCompositionWindow(
  HIMC hIMC, LPCOMPOSITIONFORM lpCompForm)
Alexandre Julliard's avatar
Alexandre Julliard committed
2251
{
2252
    BOOL reshow = FALSE;
2253
    InputContextData *data = hIMC;
2254 2255

    TRACE("(%p, %p)\n", hIMC, lpCompForm);
2256
    TRACE("\t%x, (%i,%i), (%i,%i - %i,%i)\n",lpCompForm->dwStyle,
2257 2258 2259 2260 2261 2262
          lpCompForm->ptCurrentPos.x, lpCompForm->ptCurrentPos.y, lpCompForm->rcArea.top,
          lpCompForm->rcArea.left, lpCompForm->rcArea.bottom, lpCompForm->rcArea.right);

    if (!data)
        return FALSE;

2263
    data->IMC.cfCompForm = *lpCompForm;
2264

2265
    if (IsWindowVisible(IMM_GetThreadData()->hwndDefault))
2266 2267
    {
        reshow = TRUE;
2268
        ShowWindow(IMM_GetThreadData()->hwndDefault,SW_HIDE);
2269 2270
    }

2271
    /* FIXME: this is a partial stub */
2272 2273

    if (reshow)
2274
        ShowWindow(IMM_GetThreadData()->hwndDefault,SW_SHOWNOACTIVATE);
2275

2276
    ImmInternalSendIMENotify(data, IMN_SETCOMPOSITIONWINDOW, 0);
2277
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2278 2279 2280
}

/***********************************************************************
2281
 *		ImmSetConversionStatus (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2282
 */
2283 2284
BOOL WINAPI ImmSetConversionStatus(
  HIMC hIMC, DWORD fdwConversion, DWORD fdwSentence)
Alexandre Julliard's avatar
Alexandre Julliard committed
2285
{
2286
    DWORD oldConversion, oldSentence;
2287
    InputContextData *data = hIMC;
2288

2289 2290 2291 2292 2293 2294 2295
    TRACE("%p %d %d\n", hIMC, fdwConversion, fdwSentence);

    if (!data)
        return FALSE;

    if ( fdwConversion != data->IMC.fdwConversion )
    {
2296
        oldConversion = data->IMC.fdwConversion;
2297
        data->IMC.fdwConversion = fdwConversion;
2298
        ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, oldConversion, IMC_SETCONVERSIONMODE);
2299 2300 2301 2302
        ImmInternalSendIMENotify(data, IMN_SETCONVERSIONMODE, 0);
    }
    if ( fdwSentence != data->IMC.fdwSentence )
    {
2303
        oldSentence = data->IMC.fdwSentence;
2304
        data->IMC.fdwSentence = fdwSentence;
2305
        ImmNotifyIME(hIMC, NI_CONTEXTUPDATED, oldSentence, IMC_SETSENTENCEMODE);
2306 2307 2308 2309
        ImmInternalSendIMENotify(data, IMN_SETSENTENCEMODE, 0);
    }

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2310 2311 2312
}

/***********************************************************************
2313
 *		ImmSetOpenStatus (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2314
 */
2315
BOOL WINAPI ImmSetOpenStatus(HIMC hIMC, BOOL fOpen)
Alexandre Julliard's avatar
Alexandre Julliard committed
2316
{
2317
    InputContextData *data = hIMC;
2318 2319

    TRACE("%p %d\n", hIMC, fOpen);
2320 2321

    if (!data)
2322
        return FALSE;
2323

2324
    if (data->imeWnd == NULL)
2325
    {
2326 2327
        /* create the ime window */
        data->imeWnd = CreateWindowExW( WS_EX_TOOLWINDOW,
2328 2329
                    data->immKbd->imeClassName, NULL, WS_POPUP, 0, 0, 1, 1, 0,
                    0, data->immKbd->hIME, 0);
2330
        SetWindowLongPtrW(data->imeWnd, IMMGWL_IMC, (LONG_PTR)data);
2331
        IMM_GetThreadData()->hwndDefault = data->imeWnd;
2332
    }
2333

2334 2335 2336 2337
    if (!fOpen != !data->IMC.fOpen)
    {
        data->IMC.fOpen = fOpen;
        ImmNotifyIME( hIMC, NI_CONTEXTUPDATED, 0, IMC_SETOPENSTATUS);
ByeongSik Jeon's avatar
ByeongSik Jeon committed
2338
        ImmInternalSendIMENotify(data, IMN_SETOPENSTATUS, 0);
2339 2340 2341
    }

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

/***********************************************************************
2345
 *		ImmSetStatusWindowPos (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2346
 */
2347
BOOL WINAPI ImmSetStatusWindowPos(HIMC hIMC, LPPOINT lpptPos)
Alexandre Julliard's avatar
Alexandre Julliard committed
2348
{
2349
    InputContextData *data = hIMC;
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362

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

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

    TRACE("\t(%i,%i)\n", lpptPos->x, lpptPos->y);

    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
2363 2364
}

2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
/***********************************************************************
 *              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
2395
/***********************************************************************
2396
 *		ImmSimulateHotKey (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2397
 */
2398
BOOL WINAPI ImmSimulateHotKey(HWND hWnd, DWORD dwHotKeyID)
Alexandre Julliard's avatar
Alexandre Julliard committed
2399
{
2400
  FIXME("(%p, %d): stub\n", hWnd, dwHotKeyID);
Alexandre Julliard's avatar
Alexandre Julliard committed
2401 2402
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2403 2404 2405
}

/***********************************************************************
2406
 *		ImmUnregisterWordA (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2407
 */
2408 2409
BOOL WINAPI ImmUnregisterWordA(
  HKL hKL, LPCSTR lpszReading, DWORD dwStyle, LPCSTR lpszUnregister)
Alexandre Julliard's avatar
Alexandre Julliard committed
2410
{
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
    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
2433 2434 2435
}

/***********************************************************************
2436
 *		ImmUnregisterWordW (IMM32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2437
 */
2438 2439
BOOL WINAPI ImmUnregisterWordW(
  HKL hKL, LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszUnregister)
Alexandre Julliard's avatar
Alexandre Julliard committed
2440
{
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
    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
2463
}
2464

2465 2466 2467 2468 2469 2470 2471
/***********************************************************************
 *		ImmGetImeMenuItemsA (IMM32.@)
 */
DWORD WINAPI ImmGetImeMenuItemsA( HIMC hIMC, DWORD dwFlags, DWORD dwType,
   LPIMEMENUITEMINFOA lpImeParentMenu, LPIMEMENUITEMINFOA lpImeMenu,
    DWORD dwSize)
{
2472
    InputContextData *data = hIMC;
2473 2474 2475 2476 2477 2478 2479 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 2506 2507 2508 2509 2510
    TRACE("(%p, %i, %i, %p, %p, %i):\n", hIMC, dwFlags, dwType,
        lpImeParentMenu, lpImeMenu, dwSize);
    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)
            {
2511
                unsigned int i;
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
                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;
2527 2528 2529 2530 2531 2532 2533 2534 2535
}

/***********************************************************************
*		ImmGetImeMenuItemsW (IMM32.@)
*/
DWORD WINAPI ImmGetImeMenuItemsW( HIMC hIMC, DWORD dwFlags, DWORD dwType,
   LPIMEMENUITEMINFOW lpImeParentMenu, LPIMEMENUITEMINFOW lpImeMenu,
   DWORD dwSize)
{
2536
    InputContextData *data = hIMC;
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
    TRACE("(%p, %i, %i, %p, %p, %i):\n", hIMC, dwFlags, dwType,
        lpImeParentMenu, lpImeMenu, dwSize);
    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)
            {
2574
                unsigned int i;
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
                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;
2589
}
2590

2591 2592 2593 2594 2595
/***********************************************************************
*		ImmLockIMC(IMM32.@)
*/
LPINPUTCONTEXT WINAPI ImmLockIMC(HIMC hIMC)
{
2596
    InputContextData *data = hIMC;
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608

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

/***********************************************************************
*		ImmUnlockIMC(IMM32.@)
*/
BOOL WINAPI ImmUnlockIMC(HIMC hIMC)
{
2609
    InputContextData *data = hIMC;
2610 2611 2612 2613 2614 2615 2616 2617 2618
    data->dwLock--;
    return (data->dwLock!=0);
}

/***********************************************************************
*		ImmGetIMCLockCount(IMM32.@)
*/
DWORD WINAPI ImmGetIMCLockCount(HIMC hIMC)
{
2619
    InputContextData *data = hIMC;
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
    return data->dwLock;
}

/***********************************************************************
*		ImmCreateIMCC(IMM32.@)
*/
HIMCC  WINAPI ImmCreateIMCC(DWORD size)
{
    IMCCInternal *internal;
    int real_size = size + sizeof(IMCCInternal);

    internal = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, real_size);
    if (internal == NULL)
        return NULL;

    internal->dwSize = size;
2636
    return  internal;
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653
}

/***********************************************************************
*       ImmDestroyIMCC(IMM32.@)
*/
HIMCC WINAPI ImmDestroyIMCC(HIMCC block)
{
    HeapFree(GetProcessHeap(),0,block);
    return NULL;
}

/***********************************************************************
*		ImmLockIMCC(IMM32.@)
*/
LPVOID WINAPI ImmLockIMCC(HIMCC imcc)
{
    IMCCInternal *internal;
2654
    internal = imcc;
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665

    internal->dwLock ++;
    return internal + 1;
}

/***********************************************************************
*		ImmUnlockIMCC(IMM32.@)
*/
BOOL WINAPI ImmUnlockIMCC(HIMCC imcc)
{
    IMCCInternal *internal;
2666
    internal = imcc;
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677

    internal->dwLock --;
    return (internal->dwLock!=0);
}

/***********************************************************************
*		ImmGetIMCCLockCount(IMM32.@)
*/
DWORD WINAPI ImmGetIMCCLockCount(HIMCC imcc)
{
    IMCCInternal *internal;
2678
    internal = imcc;
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690

    return internal->dwLock;
}

/***********************************************************************
*		ImmReSizeIMCC(IMM32.@)
*/
HIMCC  WINAPI ImmReSizeIMCC(HIMCC imcc, DWORD size)
{
    IMCCInternal *internal,*newone;
    int real_size = size + sizeof(IMCCInternal);

2691
    internal = imcc;
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704

    newone = HeapReAlloc(GetProcessHeap(), 0, internal, real_size);
    newone->dwSize = size;

    return newone;
}

/***********************************************************************
*		ImmGetIMCCSize(IMM32.@)
*/
DWORD WINAPI ImmGetIMCCSize(HIMCC imcc)
{
    IMCCInternal *internal;
2705
    internal = imcc;
2706 2707 2708 2709

    return internal->dwSize;
}

2710 2711 2712 2713 2714
/***********************************************************************
*		ImmGenerateMessage(IMM32.@)
*/
BOOL WINAPI ImmGenerateMessage(HIMC hIMC)
{
2715
    InputContextData *data = hIMC;
2716 2717 2718 2719 2720

    TRACE("%i messages queued\n",data->IMC.dwNumMsgBuf);
    if (data->IMC.dwNumMsgBuf > 0)
    {
        LPTRANSMSG lpTransMsg;
2721
        DWORD i;
2722

2723
        lpTransMsg = ImmLockIMCC(data->IMC.hMsgBuf);
2724
        for (i = 0; i < data->IMC.dwNumMsgBuf; i++)
2725
            ImmInternalPostIMEMessage(data, lpTransMsg[i].message, lpTransMsg[i].wParam, lpTransMsg[i].lParam);
2726 2727 2728 2729 2730 2731 2732 2733

        ImmUnlockIMCC(data->IMC.hMsgBuf);

        data->IMC.dwNumMsgBuf = 0;
    }

    return TRUE;
}
2734 2735 2736 2737 2738

/***********************************************************************
*       ImmTranslateMessage(IMM32.@)
*       ( Undocumented, call internally and from user32.dll )
*/
2739
BOOL WINAPI ImmTranslateMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lKeyData)
2740 2741 2742 2743 2744 2745 2746 2747
{
    InputContextData *data;
    HIMC imc = ImmGetContext(hwnd);
    BYTE state[256];
    UINT scancode;
    LPVOID list = 0;
    UINT msg_count;
    UINT uVirtKey;
2748
    static const DWORD list_count = 10;
2749

2750
    TRACE("%p %x %x %x\n",hwnd, msg, (UINT)wParam, (UINT)lKeyData);
2751 2752

    if (imc)
2753
        data = imc;
2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
    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)
    {
2768 2769
        WCHAR chr;

2770 2771
        if (!is_himc_ime_unicode(data))
            ToAscii(data->lastVK, scancode, state, &chr, 0);
2772 2773
        else
            ToUnicodeEx(data->lastVK, scancode, state, &chr, 1, 0, GetKeyboardLayout(0));
2774 2775 2776 2777 2778 2779 2780 2781 2782
        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)
    {
2783
        UINT i;
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
        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);

2794 2795
    data->lastVK = VK_PROCESSKEY;

2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
    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)
2812
        data = imc;
2813 2814 2815 2816 2817 2818 2819 2820 2821
    else
        return FALSE;

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

    GetKeyboardState(state);
    if (data->immKbd->pImeProcessKey(imc, vKey, lKeyData, state))
    {
2822 2823
        data->lastVK = vKey;
        return TRUE;
2824 2825
    }

2826
    data->lastVK = VK_PROCESSKEY;
2827 2828
    return FALSE;
}
2829 2830 2831 2832 2833 2834 2835 2836 2837

/***********************************************************************
*		ImmDisableTextFrameService(IMM32.@)
*/
BOOL WINAPI ImmDisableTextFrameService(DWORD idThread)
{
    FIXME("Stub\n");
    return FALSE;
}
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847

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

BOOL WINAPI ImmEnumInputContext(DWORD idThread, IMCENUMPROC lpfn, LPARAM lParam)
{
    FIXME("Stub\n");
    return FALSE;
}
2848 2849 2850 2851 2852 2853 2854 2855 2856 2857

/***********************************************************************
 *              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;
}