4 * Copyright 2000 Hidenori Takeshima
16 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(imm);
19 #include "imm_private.h"
21 static HIMC IMM32_CreateIMC( HKL hkl );
22 static BOOL IMM32_DestroyIMC( HIMC hIMC );
24 IMM32_IMC* IMM32_LockIMC( HIMC hIMC )
28 if ( hIMC == NULLIMC )
30 SetLastError( ERROR_INVALID_HANDLE );
34 pIMC = (IMM32_IMC*)IMM32_MoveableLock( (IMM32_MOVEABLEMEM*)hIMC );
35 if ( !pIMC->fSelected )
37 (void)IMM32_MoveableUnlock( (IMM32_MOVEABLEMEM*)hIMC );
38 SetLastError( ERROR_ACCESS_DENIED );
45 BOOL IMM32_UnlockIMC( HIMC hIMC )
47 if ( hIMC == NULLIMC )
49 SetLastError( ERROR_INVALID_HANDLE );
53 return IMM32_MoveableUnlock( (IMM32_MOVEABLEMEM*)hIMC );
56 static HIMC IMM32_CreateIMC( HKL hkl )
58 IMM32_MOVEABLEMEM* hIMC;
60 LPCOMPOSITIONSTRING lpCompStr;
61 LPCANDIDATEINFO lpCandInfo;
62 LPGUIDELINE lpGuideLine;
64 hIMC = IMM32_MoveableAlloc( 0, sizeof( IMM32_IMC ) );
67 SetLastError( ERROR_OUTOFMEMORY );
71 pIMC = (IMM32_IMC*)IMM32_MoveableLock( hIMC );
73 /* Initialize some members of IMC. */
74 pIMC->context.hWnd = (HWND)NULL;
75 pIMC->context.fOpen = FALSE;
76 pIMC->context.hCompStr = (HIMCC)NULL;
77 pIMC->context.hCandInfo = (HIMCC)NULL;
78 pIMC->context.hGuideLine = (HIMCC)NULL;
79 pIMC->context.hPrivate = (HIMCC)NULL;
80 pIMC->context.dwNumMsgBuf = 0;
81 pIMC->context.hMsgBuf = (HIMCC)NULL;
82 pIMC->context.fdwInit = 0;
84 pIMC->fSelected = FALSE;
86 /* hCompStr, hCandInfo, hGuideLine, hMsgBuf must be allocated. */
87 pIMC->context.hCompStr = ImmCreateIMCC( sizeof(COMPOSITIONSTRING) );
88 if ( pIMC->context.hCompStr == (HIMCC)NULL )
90 lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC( pIMC->context.hCompStr );
91 if ( lpCompStr == NULL )
93 lpCompStr->dwSize = sizeof(COMPOSITIONSTRING);
94 (void)ImmUnlockIMCC( pIMC->context.hCompStr );
96 pIMC->context.hCandInfo = ImmCreateIMCC( sizeof(CANDIDATEINFO) );
97 if ( pIMC->context.hCandInfo == (HIMCC)NULL )
99 lpCandInfo = (LPCANDIDATEINFO)ImmLockIMCC( pIMC->context.hCandInfo );
100 if ( lpCandInfo == NULL )
102 lpCandInfo->dwSize = sizeof(CANDIDATEINFO);
103 (void)ImmUnlockIMCC( pIMC->context.hCandInfo );
105 pIMC->context.hGuideLine = ImmCreateIMCC( sizeof(GUIDELINE) );
106 if ( pIMC->context.hGuideLine == (HIMCC)NULL )
108 lpGuideLine = (LPGUIDELINE)ImmLockIMCC( pIMC->context.hGuideLine );
109 if ( lpGuideLine == NULL )
111 lpGuideLine->dwSize = sizeof(GUIDELINE);
112 (void)ImmUnlockIMCC( pIMC->context.hGuideLine );
114 pIMC->context.hMsgBuf = ImmCreateIMCC( 0 );
115 if ( pIMC->context.hMsgBuf == (HIMCC)NULL )
118 pIMC->pkl = IMM32_GetIME( hkl );
119 if ( pIMC->pkl != NULL )
121 /* The current HKL is IME.
122 * Initialize IME's private context.
124 if ( pIMC->pkl->imeinfo.dwPrivateDataSize > 0 )
126 pIMC->context.hPrivate = ImmCreateIMCC(
127 pIMC->pkl->imeinfo.dwPrivateDataSize );
128 if ( pIMC->context.hPrivate == (HIMCC)NULL )
132 pIMC->fSelected = TRUE;
133 if ( !pIMC->pkl->handlers.pImeSelect( (HIMC)hIMC, TRUE ) )
135 pIMC->fSelected = FALSE;
140 (void)IMM32_MoveableUnlock( hIMC );
145 (void)IMM32_DestroyIMC( (HIMC)hIMC );
146 SetLastError( ERROR_OUTOFMEMORY );
150 static BOOL IMM32_DestroyIMC( HIMC hIMC )
154 if ( hIMC == NULLIMC )
156 SetLastError( ERROR_INVALID_HANDLE );
160 pIMC = (IMM32_IMC*)IMM32_MoveableLock( (IMM32_MOVEABLEMEM*)hIMC );
162 if ( pIMC->context.hWnd != (HWND)NULL )
164 FIXME( "please release lock of the context.hWnd!\n" );
167 if ( pIMC->fSelected )
169 (void)pIMC->pkl->handlers.pImeSelect( hIMC, FALSE );
170 pIMC->fSelected = FALSE;
173 if ( pIMC->context.hCompStr != (HIMCC)NULL )
174 (void)ImmDestroyIMCC(pIMC->context.hCompStr);
175 if ( pIMC->context.hCandInfo != (HIMCC)NULL )
176 (void)ImmDestroyIMCC(pIMC->context.hCandInfo);
177 if ( pIMC->context.hGuideLine != (HIMCC)NULL )
178 (void)ImmDestroyIMCC(pIMC->context.hGuideLine);
179 if ( pIMC->context.hPrivate != (HIMCC)NULL )
180 (void)ImmDestroyIMCC(pIMC->context.hPrivate);
181 if ( pIMC->context.hMsgBuf != (HIMCC)NULL )
182 (void)ImmDestroyIMCC(pIMC->context.hMsgBuf);
184 IMM32_MoveableFree( (IMM32_MOVEABLEMEM*)hIMC );
193 /***********************************************************************
194 * ImmCreateContext (IMM32.@)
196 HIMC WINAPI ImmCreateContext( void )
200 return IMM32_CreateIMC( GetKeyboardLayout(0) );
203 /***********************************************************************
204 * ImmDestroyContext (IMM32.@)
206 BOOL WINAPI ImmDestroyContext( HIMC hIMC )
208 TRACE("(0x%08x)\n",hIMC);
210 return IMM32_DestroyIMC( hIMC );
213 /***********************************************************************
214 * ImmLockIMC (IMM32.@)
216 LPINPUTCONTEXT WINAPI ImmLockIMC(HIMC hIMC)
220 TRACE("(0x%08x)\n", (unsigned)hIMC);
222 pIMC = IMM32_LockIMC( hIMC );
225 return &(pIMC->context);
228 /***********************************************************************
229 * ImmUnlockIMC (IMM32.@)
231 BOOL WINAPI ImmUnlockIMC(HIMC hIMC)
233 TRACE("(0x%08x)\n", (unsigned)hIMC);
235 return IMM32_UnlockIMC( hIMC );
238 /***********************************************************************
239 * ImmGetIMCLockCount (IMM32.@)
241 DWORD WINAPI ImmGetIMCLockCount(HIMC hIMC)
243 TRACE("(0x%08x)\n", (unsigned)hIMC);
245 if ( hIMC == NULLIMC )
247 SetLastError( ERROR_INVALID_HANDLE );
251 return IMM32_MoveableGetLockCount( (IMM32_MOVEABLEMEM*)hIMC );