appwiz.cpl: Add __WINE_ALLOC_SIZE attributes to heap_xxx() functions.
[wine.git] / dlls / winemac.drv / ime.c
blob846f9bd26d365485779afcb8b58673b72aa5e4d1
1 /*
2 * The IME for interfacing with Mac input methods
4 * Copyright 2008, 2013 CodeWeavers, Aric Stewart
5 * Copyright 2013 Ken Thomases for CodeWeavers Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * Notes:
24 * The normal flow for IMM/IME Processing is as follows.
25 * 1) The Keyboard Driver generates key messages which are first passed to
26 * the IMM and then to IME via ImeProcessKey. If the IME returns 0 then
27 * it does not want the key and the keyboard driver then generates the
28 * WM_KEYUP/WM_KEYDOWN messages. However if the IME is going to process the
29 * key it returns non-zero.
30 * 2) If the IME is going to process the key then the IMM calls ImeToAsciiEx to
31 * process the key. the IME modifies the HIMC structure to reflect the
32 * current state and generates any messages it needs the IMM to process.
33 * 3) IMM checks the messages and send them to the application in question. From
34 * here the IMM level deals with if the application is IME aware or not.
37 #include "config.h"
39 #include <stdarg.h>
41 #include "macdrv.h"
42 #include "winuser.h"
43 #include "imm.h"
44 #include "ddk/imm.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(imm);
48 #define FROM_MACDRV ((HIMC)0xcafe1337)
50 typedef struct _IMEPRIVATE {
51 BOOL bInComposition;
52 BOOL bInternalState;
53 HFONT textfont;
54 HWND hwndDefault;
56 UINT repeat;
57 } IMEPRIVATE, *LPIMEPRIVATE;
59 typedef struct _tagTRANSMSG {
60 UINT message;
61 WPARAM wParam;
62 LPARAM lParam;
63 } TRANSMSG, *LPTRANSMSG;
65 static const WCHAR UI_CLASS_NAME[] = {'W','i','n','e',' ','M','a','c',' ','I','M','E',0};
67 static HIMC *hSelectedFrom = NULL;
68 static INT hSelectedCount = 0;
70 /* MSIME messages */
71 static UINT WM_MSIME_SERVICE;
72 static UINT WM_MSIME_RECONVERTOPTIONS;
73 static UINT WM_MSIME_MOUSE;
74 static UINT WM_MSIME_RECONVERTREQUEST;
75 static UINT WM_MSIME_RECONVERT;
76 static UINT WM_MSIME_QUERYPOSITION;
77 static UINT WM_MSIME_DOCUMENTFEED;
80 static HIMC RealIMC(HIMC hIMC)
82 if (hIMC == FROM_MACDRV)
84 INT i;
85 HWND wnd = GetFocus();
86 HIMC winHimc = ImmGetContext(wnd);
87 for (i = 0; i < hSelectedCount; i++)
88 if (winHimc == hSelectedFrom[i])
89 return winHimc;
90 return NULL;
92 else
93 return hIMC;
96 static LPINPUTCONTEXT LockRealIMC(HIMC hIMC)
98 HIMC real_imc = RealIMC(hIMC);
99 if (real_imc)
100 return ImmLockIMC(real_imc);
101 else
102 return NULL;
105 static BOOL UnlockRealIMC(HIMC hIMC)
107 HIMC real_imc = RealIMC(hIMC);
108 if (real_imc)
109 return ImmUnlockIMC(real_imc);
110 else
111 return FALSE;
114 static HIMCC ImeCreateBlankCompStr(void)
116 HIMCC rc;
117 LPCOMPOSITIONSTRING ptr;
118 rc = ImmCreateIMCC(sizeof(COMPOSITIONSTRING));
119 ptr = ImmLockIMCC(rc);
120 memset(ptr, 0, sizeof(COMPOSITIONSTRING));
121 ptr->dwSize = sizeof(COMPOSITIONSTRING);
122 ImmUnlockIMCC(rc);
123 return rc;
126 static int updateField(DWORD origLen, DWORD origOffset, DWORD currentOffset,
127 LPBYTE target, LPBYTE source, DWORD* lenParam,
128 DWORD* offsetParam, BOOL wchars)
130 if (origLen > 0 && origOffset > 0)
132 int truelen = origLen;
133 if (wchars)
134 truelen *= sizeof(WCHAR);
136 memcpy(&target[currentOffset], &source[origOffset], truelen);
138 *lenParam = origLen;
139 *offsetParam = currentOffset;
140 currentOffset += truelen;
142 return currentOffset;
145 static HIMCC updateCompStr(HIMCC old, LPCWSTR compstr, DWORD len, DWORD *flags)
147 /* we need to make sure the CompStr, CompClaus and CompAttr fields are all
148 * set and correct */
149 int needed_size;
150 HIMCC rc;
151 LPBYTE newdata = NULL;
152 LPBYTE olddata = NULL;
153 LPCOMPOSITIONSTRING new_one;
154 LPCOMPOSITIONSTRING lpcs = NULL;
155 INT current_offset = 0;
157 TRACE("%s, %i\n", debugstr_wn(compstr, len), len);
159 if (old == NULL && compstr == NULL && len == 0)
160 return NULL;
162 if (compstr == NULL && len != 0)
164 ERR("compstr is NULL however we have a len! Please report\n");
165 len = 0;
168 if (old != NULL)
170 olddata = ImmLockIMCC(old);
171 lpcs = (LPCOMPOSITIONSTRING)olddata;
174 needed_size = sizeof(COMPOSITIONSTRING) + len * sizeof(WCHAR) +
175 len + sizeof(DWORD) * 2;
177 if (lpcs != NULL)
179 needed_size += lpcs->dwCompReadAttrLen;
180 needed_size += lpcs->dwCompReadClauseLen;
181 needed_size += lpcs->dwCompReadStrLen * sizeof(WCHAR);
182 needed_size += lpcs->dwResultReadClauseLen;
183 needed_size += lpcs->dwResultReadStrLen * sizeof(WCHAR);
184 needed_size += lpcs->dwResultClauseLen;
185 needed_size += lpcs->dwResultStrLen * sizeof(WCHAR);
186 needed_size += lpcs->dwPrivateSize;
188 rc = ImmCreateIMCC(needed_size);
189 newdata = ImmLockIMCC(rc);
190 new_one = (LPCOMPOSITIONSTRING)newdata;
192 new_one->dwSize = needed_size;
193 current_offset = sizeof(COMPOSITIONSTRING);
194 if (lpcs != NULL)
196 current_offset = updateField(lpcs->dwCompReadAttrLen,
197 lpcs->dwCompReadAttrOffset,
198 current_offset, newdata, olddata,
199 &new_one->dwCompReadAttrLen,
200 &new_one->dwCompReadAttrOffset, FALSE);
202 current_offset = updateField(lpcs->dwCompReadClauseLen,
203 lpcs->dwCompReadClauseOffset,
204 current_offset, newdata, olddata,
205 &new_one->dwCompReadClauseLen,
206 &new_one->dwCompReadClauseOffset, FALSE);
208 current_offset = updateField(lpcs->dwCompReadStrLen,
209 lpcs->dwCompReadStrOffset,
210 current_offset, newdata, olddata,
211 &new_one->dwCompReadStrLen,
212 &new_one->dwCompReadStrOffset, TRUE);
214 /* new CompAttr, CompClause, CompStr, dwCursorPos */
215 new_one->dwDeltaStart = 0;
216 new_one->dwCursorPos = lpcs->dwCursorPos;
218 current_offset = updateField(lpcs->dwResultReadClauseLen,
219 lpcs->dwResultReadClauseOffset,
220 current_offset, newdata, olddata,
221 &new_one->dwResultReadClauseLen,
222 &new_one->dwResultReadClauseOffset, FALSE);
224 current_offset = updateField(lpcs->dwResultReadStrLen,
225 lpcs->dwResultReadStrOffset,
226 current_offset, newdata, olddata,
227 &new_one->dwResultReadStrLen,
228 &new_one->dwResultReadStrOffset, TRUE);
230 current_offset = updateField(lpcs->dwResultClauseLen,
231 lpcs->dwResultClauseOffset,
232 current_offset, newdata, olddata,
233 &new_one->dwResultClauseLen,
234 &new_one->dwResultClauseOffset, FALSE);
236 current_offset = updateField(lpcs->dwResultStrLen,
237 lpcs->dwResultStrOffset,
238 current_offset, newdata, olddata,
239 &new_one->dwResultStrLen,
240 &new_one->dwResultStrOffset, TRUE);
242 current_offset = updateField(lpcs->dwPrivateSize,
243 lpcs->dwPrivateOffset,
244 current_offset, newdata, olddata,
245 &new_one->dwPrivateSize,
246 &new_one->dwPrivateOffset, FALSE);
248 else
250 new_one->dwCursorPos = len;
251 *flags |= GCS_CURSORPOS;
254 /* set new data */
255 /* CompAttr */
256 new_one->dwCompAttrLen = len;
257 if (len > 0)
259 new_one->dwCompAttrOffset = current_offset;
260 memset(&newdata[current_offset], ATTR_INPUT, len);
261 current_offset += len;
264 /* CompClause */
265 if (len > 0)
267 new_one->dwCompClauseLen = sizeof(DWORD) * 2;
268 new_one->dwCompClauseOffset = current_offset;
269 *(DWORD*)&newdata[current_offset] = 0;
270 current_offset += sizeof(DWORD);
271 *(DWORD*)&newdata[current_offset] = len;
272 current_offset += sizeof(DWORD);
274 else
275 new_one->dwCompClauseLen = 0;
277 /* CompStr */
278 new_one->dwCompStrLen = len;
279 if (len > 0)
281 new_one->dwCompStrOffset = current_offset;
282 memcpy(&newdata[current_offset], compstr, len * sizeof(WCHAR));
286 ImmUnlockIMCC(rc);
287 if (lpcs)
288 ImmUnlockIMCC(old);
290 return rc;
293 static HIMCC updateResultStr(HIMCC old, LPWSTR resultstr, DWORD len)
295 /* we need to make sure the ResultStr and ResultClause fields are all
296 * set and correct */
297 int needed_size;
298 HIMCC rc;
299 LPBYTE newdata = NULL;
300 LPBYTE olddata = NULL;
301 LPCOMPOSITIONSTRING new_one;
302 LPCOMPOSITIONSTRING lpcs = NULL;
303 INT current_offset = 0;
305 TRACE("%s, %i\n", debugstr_wn(resultstr, len), len);
307 if (old == NULL && resultstr == NULL && len == 0)
308 return NULL;
310 if (resultstr == NULL && len != 0)
312 ERR("resultstr is NULL however we have a len! Please report\n");
313 len = 0;
316 if (old != NULL)
318 olddata = ImmLockIMCC(old);
319 lpcs = (LPCOMPOSITIONSTRING)olddata;
322 needed_size = sizeof(COMPOSITIONSTRING) + len * sizeof(WCHAR) +
323 sizeof(DWORD) * 2;
325 if (lpcs != NULL)
327 needed_size += lpcs->dwCompReadAttrLen;
328 needed_size += lpcs->dwCompReadClauseLen;
329 needed_size += lpcs->dwCompReadStrLen * sizeof(WCHAR);
330 needed_size += lpcs->dwCompAttrLen;
331 needed_size += lpcs->dwCompClauseLen;
332 needed_size += lpcs->dwCompStrLen * sizeof(WCHAR);
333 needed_size += lpcs->dwResultReadClauseLen;
334 needed_size += lpcs->dwResultReadStrLen * sizeof(WCHAR);
335 needed_size += lpcs->dwPrivateSize;
337 rc = ImmCreateIMCC(needed_size);
338 newdata = ImmLockIMCC(rc);
339 new_one = (LPCOMPOSITIONSTRING)newdata;
341 new_one->dwSize = needed_size;
342 current_offset = sizeof(COMPOSITIONSTRING);
343 if (lpcs != NULL)
345 current_offset = updateField(lpcs->dwCompReadAttrLen,
346 lpcs->dwCompReadAttrOffset,
347 current_offset, newdata, olddata,
348 &new_one->dwCompReadAttrLen,
349 &new_one->dwCompReadAttrOffset, FALSE);
351 current_offset = updateField(lpcs->dwCompReadClauseLen,
352 lpcs->dwCompReadClauseOffset,
353 current_offset, newdata, olddata,
354 &new_one->dwCompReadClauseLen,
355 &new_one->dwCompReadClauseOffset, FALSE);
357 current_offset = updateField(lpcs->dwCompReadStrLen,
358 lpcs->dwCompReadStrOffset,
359 current_offset, newdata, olddata,
360 &new_one->dwCompReadStrLen,
361 &new_one->dwCompReadStrOffset, TRUE);
363 current_offset = updateField(lpcs->dwCompAttrLen,
364 lpcs->dwCompAttrOffset,
365 current_offset, newdata, olddata,
366 &new_one->dwCompAttrLen,
367 &new_one->dwCompAttrOffset, FALSE);
369 current_offset = updateField(lpcs->dwCompClauseLen,
370 lpcs->dwCompClauseOffset,
371 current_offset, newdata, olddata,
372 &new_one->dwCompClauseLen,
373 &new_one->dwCompClauseOffset, FALSE);
375 current_offset = updateField(lpcs->dwCompStrLen,
376 lpcs->dwCompStrOffset,
377 current_offset, newdata, olddata,
378 &new_one->dwCompStrLen,
379 &new_one->dwCompStrOffset, TRUE);
381 new_one->dwCursorPos = lpcs->dwCursorPos;
382 new_one->dwDeltaStart = 0;
384 current_offset = updateField(lpcs->dwResultReadClauseLen,
385 lpcs->dwResultReadClauseOffset,
386 current_offset, newdata, olddata,
387 &new_one->dwResultReadClauseLen,
388 &new_one->dwResultReadClauseOffset, FALSE);
390 current_offset = updateField(lpcs->dwResultReadStrLen,
391 lpcs->dwResultReadStrOffset,
392 current_offset, newdata, olddata,
393 &new_one->dwResultReadStrLen,
394 &new_one->dwResultReadStrOffset, TRUE);
396 /* new ResultClause , ResultStr */
398 current_offset = updateField(lpcs->dwPrivateSize,
399 lpcs->dwPrivateOffset,
400 current_offset, newdata, olddata,
401 &new_one->dwPrivateSize,
402 &new_one->dwPrivateOffset, FALSE);
405 /* set new data */
406 /* ResultClause */
407 if (len > 0)
409 new_one->dwResultClauseLen = sizeof(DWORD) * 2;
410 new_one->dwResultClauseOffset = current_offset;
411 *(DWORD*)&newdata[current_offset] = 0;
412 current_offset += sizeof(DWORD);
413 *(DWORD*)&newdata[current_offset] = len;
414 current_offset += sizeof(DWORD);
416 else
417 new_one->dwResultClauseLen = 0;
419 /* ResultStr */
420 new_one->dwResultStrLen = len;
421 if (len > 0)
423 new_one->dwResultStrOffset = current_offset;
424 memcpy(&newdata[current_offset], resultstr, len * sizeof(WCHAR));
426 ImmUnlockIMCC(rc);
427 if (lpcs)
428 ImmUnlockIMCC(old);
430 return rc;
433 static void GenerateIMEMessage(HIMC hIMC, UINT msg, WPARAM wParam, LPARAM lParam)
435 LPINPUTCONTEXT lpIMC;
436 LPTRANSMSG lpTransMsg;
438 lpIMC = LockRealIMC(hIMC);
439 if (lpIMC == NULL)
440 return;
442 lpIMC->hMsgBuf = ImmReSizeIMCC(lpIMC->hMsgBuf, (lpIMC->dwNumMsgBuf + 1) * sizeof(TRANSMSG));
443 if (!lpIMC->hMsgBuf)
444 return;
446 lpTransMsg = ImmLockIMCC(lpIMC->hMsgBuf);
447 if (!lpTransMsg)
448 return;
450 lpTransMsg += lpIMC->dwNumMsgBuf;
451 lpTransMsg->message = msg;
452 lpTransMsg->wParam = wParam;
453 lpTransMsg->lParam = lParam;
455 ImmUnlockIMCC(lpIMC->hMsgBuf);
456 lpIMC->dwNumMsgBuf++;
458 ImmGenerateMessage(RealIMC(hIMC));
459 UnlockRealIMC(hIMC);
462 static void GenerateIMECHARMessages(HIMC hIMC, LPWSTR String, DWORD length)
464 LPINPUTCONTEXT lpIMC;
465 LPTRANSMSG lpTransMsg;
466 DWORD i;
468 if (length <= 0)
469 return;
471 lpIMC = LockRealIMC(hIMC);
472 if (lpIMC == NULL)
473 return;
475 lpIMC->hMsgBuf = ImmReSizeIMCC(lpIMC->hMsgBuf, (lpIMC->dwNumMsgBuf + length) * sizeof(TRANSMSG));
476 if (!lpIMC->hMsgBuf)
477 return;
479 lpTransMsg = ImmLockIMCC(lpIMC->hMsgBuf);
480 if (!lpTransMsg)
481 return;
483 lpTransMsg += lpIMC->dwNumMsgBuf;
484 for (i = 0; i < length; i++)
486 lpTransMsg->message = WM_IME_CHAR;
487 lpTransMsg->wParam = String[i];
488 lpTransMsg->lParam = 1;
489 lpTransMsg++;
492 ImmUnlockIMCC(lpIMC->hMsgBuf);
493 lpIMC->dwNumMsgBuf += length;
495 ImmGenerateMessage(RealIMC(hIMC));
496 UnlockRealIMC(hIMC);
499 static BOOL GenerateMessageToTransKey(LPDWORD lpTransBuf, UINT *uNumTranMsgs,
500 UINT msg, WPARAM wParam, LPARAM lParam)
502 LPTRANSMSG ptr;
504 if (*uNumTranMsgs + 1 >= (UINT)*lpTransBuf)
505 return FALSE;
507 ptr = (LPTRANSMSG)(lpTransBuf + 1 + *uNumTranMsgs * 3);
508 ptr->message = msg;
509 ptr->wParam = wParam;
510 ptr->lParam = lParam;
511 (*uNumTranMsgs)++;
513 return TRUE;
517 static BOOL IME_RemoveFromSelected(HIMC hIMC)
519 int i;
520 for (i = 0; i < hSelectedCount; i++)
522 if (hSelectedFrom[i] == hIMC)
524 if (i < hSelectedCount - 1)
525 memmove(&hSelectedFrom[i], &hSelectedFrom[i + 1], (hSelectedCount - i - 1) * sizeof(HIMC));
526 hSelectedCount--;
527 return TRUE;
530 return FALSE;
533 static void IME_AddToSelected(HIMC hIMC)
535 hSelectedCount++;
536 if (hSelectedFrom)
537 hSelectedFrom = HeapReAlloc(GetProcessHeap(), 0, hSelectedFrom, hSelectedCount * sizeof(HIMC));
538 else
539 hSelectedFrom = HeapAlloc(GetProcessHeap(), 0, sizeof(HIMC));
540 hSelectedFrom[hSelectedCount - 1] = hIMC;
543 static void UpdateDataInDefaultIMEWindow(HIMC hIMC, HWND hwnd, BOOL showable)
545 LPCOMPOSITIONSTRING compstr;
546 LPINPUTCONTEXT lpIMC;
548 lpIMC = LockRealIMC(hIMC);
549 if (lpIMC == NULL)
550 return;
552 if (lpIMC->hCompStr)
553 compstr = ImmLockIMCC(lpIMC->hCompStr);
554 else
555 compstr = NULL;
557 if (compstr == NULL || compstr->dwCompStrLen == 0)
558 ShowWindow(hwnd, SW_HIDE);
559 else if (showable)
560 ShowWindow(hwnd, SW_SHOWNOACTIVATE);
562 RedrawWindow(hwnd, NULL, NULL, RDW_ERASENOW | RDW_INVALIDATE);
564 if (compstr != NULL)
565 ImmUnlockIMCC(lpIMC->hCompStr);
567 UnlockRealIMC(hIMC);
570 BOOL WINAPI ImeConfigure(HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
572 FIXME("(%p, %p, %d, %p): stub\n", hKL, hWnd, dwMode, lpData);
573 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
574 return FALSE;
577 DWORD WINAPI ImeConversionList(HIMC hIMC, LPCWSTR lpSource, LPCANDIDATELIST lpCandList,
578 DWORD dwBufLen, UINT uFlag)
581 FIXME("(%p, %s, %p, %d, %d): stub\n", hIMC, debugstr_w(lpSource), lpCandList,
582 dwBufLen, uFlag);
583 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
584 return 0;
587 BOOL WINAPI ImeDestroy(UINT uForce)
589 TRACE("\n");
590 HeapFree(GetProcessHeap(), 0, hSelectedFrom);
591 hSelectedFrom = NULL;
592 hSelectedCount = 0;
593 return TRUE;
596 LRESULT WINAPI ImeEscape(HIMC hIMC, UINT uSubFunc, LPVOID lpData)
598 TRACE("%x %p\n", uSubFunc, lpData);
599 return 0;
602 BOOL WINAPI ImeProcessKey(HIMC hIMC, UINT vKey, LPARAM lKeyData, const LPBYTE lpbKeyState)
604 LPINPUTCONTEXT lpIMC;
605 BOOL inIME;
607 TRACE("hIMC %p vKey 0x%04x lKeyData 0x%08lx lpbKeyState %p\n", hIMC, vKey, lKeyData, lpbKeyState);
609 switch (vKey)
611 case VK_SHIFT:
612 case VK_CONTROL:
613 case VK_CAPITAL:
614 case VK_MENU:
615 return FALSE;
618 inIME = macdrv_using_input_method();
619 lpIMC = LockRealIMC(hIMC);
620 if (lpIMC)
622 LPIMEPRIVATE myPrivate;
623 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
625 if (inIME && !myPrivate->bInternalState)
626 ImmSetOpenStatus(RealIMC(FROM_MACDRV), TRUE);
627 else if (!inIME && myPrivate->bInternalState)
629 ShowWindow(myPrivate->hwndDefault, SW_HIDE);
630 ImmDestroyIMCC(lpIMC->hCompStr);
631 lpIMC->hCompStr = ImeCreateBlankCompStr();
632 ImmSetOpenStatus(RealIMC(FROM_MACDRV), FALSE);
635 myPrivate->repeat = (lKeyData >> 30) & 0x1;
637 myPrivate->bInternalState = inIME;
638 ImmUnlockIMCC(lpIMC->hPrivate);
640 UnlockRealIMC(hIMC);
642 return inIME;
645 BOOL WINAPI ImeSelect(HIMC hIMC, BOOL fSelect)
647 LPINPUTCONTEXT lpIMC;
648 TRACE("%p %s\n", hIMC, fSelect ? "TRUE" : "FALSE");
650 if (hIMC == FROM_MACDRV)
652 ERR("ImeSelect should never be called from Cocoa\n");
653 return FALSE;
656 if (!hIMC)
657 return TRUE;
659 /* not selected */
660 if (!fSelect)
661 return IME_RemoveFromSelected(hIMC);
663 IME_AddToSelected(hIMC);
665 /* Initialize our structures */
666 lpIMC = LockRealIMC(hIMC);
667 if (lpIMC != NULL)
669 LPIMEPRIVATE myPrivate;
670 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
671 myPrivate->bInComposition = FALSE;
672 myPrivate->bInternalState = FALSE;
673 myPrivate->textfont = NULL;
674 myPrivate->hwndDefault = NULL;
675 myPrivate->repeat = 0;
676 ImmUnlockIMCC(lpIMC->hPrivate);
677 UnlockRealIMC(hIMC);
680 return TRUE;
683 BOOL WINAPI ImeSetActiveContext(HIMC hIMC, BOOL fFlag)
685 FIXME("(%p, %x): stub\n", hIMC, fFlag);
686 return TRUE;
689 UINT WINAPI ImeToAsciiEx(UINT uVKey, UINT uScanCode, const LPBYTE lpbKeyState,
690 LPDWORD lpdwTransKey, UINT fuState, HIMC hIMC)
692 UINT vkey;
693 LPINPUTCONTEXT lpIMC;
694 LPIMEPRIVATE myPrivate;
695 HWND hwndDefault;
696 UINT repeat;
697 int done = 0;
699 TRACE("uVKey 0x%04x uScanCode 0x%04x fuState %u hIMC %p\n", uVKey, uScanCode, fuState, hIMC);
701 vkey = LOWORD(uVKey);
703 if (vkey == VK_KANA || vkey == VK_KANJI || vkey == VK_MENU)
705 TRACE("Skipping metakey\n");
706 return 0;
709 lpIMC = LockRealIMC(hIMC);
710 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
711 if (!myPrivate->bInternalState)
713 ImmUnlockIMCC(lpIMC->hPrivate);
714 UnlockRealIMC(hIMC);
715 return 0;
718 repeat = myPrivate->repeat;
719 hwndDefault = myPrivate->hwndDefault;
720 ImmUnlockIMCC(lpIMC->hPrivate);
721 UnlockRealIMC(hIMC);
723 TRACE("Processing Mac 0x%04x\n", vkey);
724 macdrv_process_text_input(uVKey, uScanCode, repeat, lpbKeyState, hIMC, &done);
726 while (!done)
727 MsgWaitForMultipleObjectsEx(0, NULL, INFINITE, QS_POSTMESSAGE | QS_SENDMESSAGE, 0);
729 if (done < 0)
731 UINT msgs = 0;
732 UINT msg = (uScanCode & 0x8000) ? WM_KEYUP : WM_KEYDOWN;
734 /* KeyStroke not processed by the IME
735 * so we need to rebuild the KeyDown message and pass it on to WINE
737 if (!GenerateMessageToTransKey(lpdwTransKey, &msgs, msg, vkey, MAKELONG(0x0001, uScanCode)))
738 GenerateIMEMessage(hIMC, msg, vkey, MAKELONG(0x0001, uScanCode));
740 return msgs;
742 else
743 UpdateDataInDefaultIMEWindow(hIMC, hwndDefault, FALSE);
744 return 0;
747 BOOL WINAPI NotifyIME(HIMC hIMC, DWORD dwAction, DWORD dwIndex, DWORD dwValue)
749 BOOL bRet = FALSE;
750 LPINPUTCONTEXT lpIMC;
752 TRACE("%p %i %i %i\n", hIMC, dwAction, dwIndex, dwValue);
754 lpIMC = LockRealIMC(hIMC);
755 if (lpIMC == NULL)
756 return FALSE;
758 switch (dwAction)
760 case NI_OPENCANDIDATE: FIXME("NI_OPENCANDIDATE\n"); break;
761 case NI_CLOSECANDIDATE: FIXME("NI_CLOSECANDIDATE\n"); break;
762 case NI_SELECTCANDIDATESTR: FIXME("NI_SELECTCANDIDATESTR\n"); break;
763 case NI_CHANGECANDIDATELIST: FIXME("NI_CHANGECANDIDATELIST\n"); break;
764 case NI_SETCANDIDATE_PAGESTART: FIXME("NI_SETCANDIDATE_PAGESTART\n"); break;
765 case NI_SETCANDIDATE_PAGESIZE: FIXME("NI_SETCANDIDATE_PAGESIZE\n"); break;
766 case NI_CONTEXTUPDATED:
767 switch (dwValue)
769 case IMC_SETCOMPOSITIONWINDOW: FIXME("NI_CONTEXTUPDATED: IMC_SETCOMPOSITIONWINDOW\n"); break;
770 case IMC_SETCONVERSIONMODE: FIXME("NI_CONTEXTUPDATED: IMC_SETCONVERSIONMODE\n"); break;
771 case IMC_SETSENTENCEMODE: FIXME("NI_CONTEXTUPDATED: IMC_SETSENTENCEMODE\n"); break;
772 case IMC_SETCANDIDATEPOS: FIXME("NI_CONTEXTUPDATED: IMC_SETCANDIDATEPOS\n"); break;
773 case IMC_SETCOMPOSITIONFONT:
775 LPIMEPRIVATE myPrivate;
776 TRACE("NI_CONTEXTUPDATED: IMC_SETCOMPOSITIONFONT\n");
778 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
779 if (myPrivate->textfont)
781 DeleteObject(myPrivate->textfont);
782 myPrivate->textfont = NULL;
784 myPrivate->textfont = CreateFontIndirectW(&lpIMC->lfFont.W);
785 ImmUnlockIMCC(lpIMC->hPrivate);
787 break;
788 case IMC_SETOPENSTATUS:
790 LPIMEPRIVATE myPrivate;
791 TRACE("NI_CONTEXTUPDATED: IMC_SETOPENSTATUS\n");
793 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
794 if (lpIMC->fOpen != myPrivate->bInternalState && myPrivate->bInComposition)
796 if(lpIMC->fOpen == FALSE)
798 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
799 myPrivate->bInComposition = FALSE;
801 else
803 GenerateIMEMessage(hIMC, WM_IME_STARTCOMPOSITION, 0, 0);
804 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, 0, 0);
807 myPrivate->bInternalState = lpIMC->fOpen;
808 bRet = TRUE;
810 break;
811 default: FIXME("NI_CONTEXTUPDATED: Unknown\n"); break;
813 break;
814 case NI_COMPOSITIONSTR:
815 switch (dwIndex)
817 case CPS_COMPLETE:
819 HIMCC newCompStr;
820 DWORD cplen = 0;
821 LPWSTR cpstr;
822 LPCOMPOSITIONSTRING cs = NULL;
823 LPBYTE cdata = NULL;
824 LPIMEPRIVATE myPrivate;
826 TRACE("NI_COMPOSITIONSTR: CPS_COMPLETE\n");
828 /* clear existing result */
829 newCompStr = updateResultStr(lpIMC->hCompStr, NULL, 0);
831 ImmDestroyIMCC(lpIMC->hCompStr);
832 lpIMC->hCompStr = newCompStr;
834 if (lpIMC->hCompStr)
836 cdata = ImmLockIMCC(lpIMC->hCompStr);
837 cs = (LPCOMPOSITIONSTRING)cdata;
838 cplen = cs->dwCompStrLen;
839 cpstr = (LPWSTR)&cdata[cs->dwCompStrOffset];
840 ImmUnlockIMCC(lpIMC->hCompStr);
842 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
843 if (cplen > 0)
845 WCHAR param = cpstr[0];
846 DWORD flags = GCS_COMPSTR;
848 newCompStr = updateResultStr(lpIMC->hCompStr, cpstr, cplen);
849 ImmDestroyIMCC(lpIMC->hCompStr);
850 lpIMC->hCompStr = newCompStr;
851 newCompStr = updateCompStr(lpIMC->hCompStr, NULL, 0, &flags);
852 ImmDestroyIMCC(lpIMC->hCompStr);
853 lpIMC->hCompStr = newCompStr;
855 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, 0, flags);
857 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, param,
858 GCS_RESULTSTR | GCS_RESULTCLAUSE);
860 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
862 else if (myPrivate->bInComposition)
863 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
866 myPrivate->bInComposition = FALSE;
867 ImmUnlockIMCC(lpIMC->hPrivate);
869 bRet = TRUE;
871 break;
872 case CPS_CONVERT: FIXME("NI_COMPOSITIONSTR: CPS_CONVERT\n"); break;
873 case CPS_REVERT: FIXME("NI_COMPOSITIONSTR: CPS_REVERT\n"); break;
874 case CPS_CANCEL:
876 LPIMEPRIVATE myPrivate;
878 TRACE("NI_COMPOSITIONSTR: CPS_CANCEL\n");
880 if (lpIMC->hCompStr)
881 ImmDestroyIMCC(lpIMC->hCompStr);
883 lpIMC->hCompStr = ImeCreateBlankCompStr();
885 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
886 if (myPrivate->bInComposition)
888 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
889 myPrivate->bInComposition = FALSE;
891 ImmUnlockIMCC(lpIMC->hPrivate);
892 bRet = TRUE;
894 break;
895 default: FIXME("NI_COMPOSITIONSTR: Unknown\n"); break;
897 break;
898 default: FIXME("Unknown Message\n"); break;
901 UnlockRealIMC(hIMC);
902 return bRet;
905 BOOL WINAPI ImeRegisterWord(LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszRegister)
907 FIXME("(%s, %d, %s): stub\n", debugstr_w(lpszReading), dwStyle, debugstr_w(lpszRegister));
908 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
909 return FALSE;
912 BOOL WINAPI ImeUnregisterWord(LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszUnregister)
914 FIXME("(%s, %d, %s): stub\n", debugstr_w(lpszReading), dwStyle, debugstr_w(lpszUnregister));
915 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
916 return FALSE;
919 UINT WINAPI ImeGetRegisterWordStyle(UINT nItem, LPSTYLEBUFW lpStyleBuf)
921 FIXME("(%d, %p): stub\n", nItem, lpStyleBuf);
922 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
923 return 0;
926 UINT WINAPI ImeEnumRegisterWord(REGISTERWORDENUMPROCW lpfnEnumProc, LPCWSTR lpszReading,
927 DWORD dwStyle, LPCWSTR lpszRegister, LPVOID lpData)
929 FIXME("(%p, %s, %d, %s, %p): stub\n", lpfnEnumProc, debugstr_w(lpszReading), dwStyle,
930 debugstr_w(lpszRegister), lpData);
931 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
932 return 0;
935 static BOOL IME_SetCompositionString(void* hIMC, DWORD dwIndex, LPCVOID lpComp, DWORD dwCompLen, DWORD cursor_pos, BOOL cursor_valid)
937 LPINPUTCONTEXT lpIMC;
938 DWORD flags = 0;
939 WCHAR wParam = 0;
940 LPIMEPRIVATE myPrivate;
942 TRACE("(%p, %d, %p, %d):\n", hIMC, dwIndex, lpComp, dwCompLen);
945 * Explanation:
946 * this sets the composition string in the imm32.dll level
947 * of the composition buffer.
948 * TODO: set the Cocoa window's marked text string and tell text input context
951 lpIMC = LockRealIMC(hIMC);
953 if (lpIMC == NULL)
954 return FALSE;
956 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
958 if (dwIndex == SCS_SETSTR)
960 HIMCC newCompStr;
962 if (!myPrivate->bInComposition)
964 GenerateIMEMessage(hIMC, WM_IME_STARTCOMPOSITION, 0, 0);
965 myPrivate->bInComposition = TRUE;
968 flags = GCS_COMPSTR;
970 if (dwCompLen && lpComp)
972 newCompStr = updateCompStr(lpIMC->hCompStr, (LPCWSTR)lpComp, dwCompLen / sizeof(WCHAR), &flags);
973 ImmDestroyIMCC(lpIMC->hCompStr);
974 lpIMC->hCompStr = newCompStr;
976 wParam = ((const WCHAR*)lpComp)[0];
977 flags |= GCS_COMPCLAUSE | GCS_COMPATTR | GCS_DELTASTART;
979 else
981 newCompStr = updateCompStr(lpIMC->hCompStr, NULL, 0, &flags);
982 ImmDestroyIMCC(lpIMC->hCompStr);
983 lpIMC->hCompStr = newCompStr;
986 if (cursor_valid)
988 LPCOMPOSITIONSTRING compstr;
989 compstr = ImmLockIMCC(lpIMC->hCompStr);
990 compstr->dwCursorPos = cursor_pos;
991 ImmUnlockIMCC(lpIMC->hCompStr);
992 flags |= GCS_CURSORPOS;
997 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, wParam, flags);
998 ImmUnlockIMCC(lpIMC->hPrivate);
999 UnlockRealIMC(hIMC);
1001 return TRUE;
1004 BOOL WINAPI ImeSetCompositionString(HIMC hIMC, DWORD dwIndex, LPCVOID lpComp, DWORD dwCompLen,
1005 LPCVOID lpRead, DWORD dwReadLen)
1007 TRACE("(%p, %d, %p, %d, %p, %d):\n", hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
1009 if (lpRead && dwReadLen)
1010 FIXME("Reading string unimplemented\n");
1012 return IME_SetCompositionString(hIMC, dwIndex, lpComp, dwCompLen, 0, FALSE);
1015 DWORD WINAPI ImeGetImeMenuItems(HIMC hIMC, DWORD dwFlags, DWORD dwType, LPIMEMENUITEMINFOW lpImeParentMenu,
1016 LPIMEMENUITEMINFOW lpImeMenu, DWORD dwSize)
1018 FIXME("(%p, %x %x %p %p %x): stub\n", hIMC, dwFlags, dwType, lpImeParentMenu, lpImeMenu, dwSize);
1019 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1020 return 0;
1023 static void IME_NotifyComplete(void* hIMC)
1025 NotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
1028 /*****
1029 * Internal functions to help with IME window management
1031 static void PaintDefaultIMEWnd(HIMC hIMC, HWND hwnd)
1033 PAINTSTRUCT ps;
1034 RECT rect;
1035 HDC hdc;
1036 LPCOMPOSITIONSTRING compstr;
1037 LPBYTE compdata = NULL;
1038 HMONITOR monitor;
1039 MONITORINFO mon_info;
1040 INT offX = 0, offY = 0;
1041 LPINPUTCONTEXT lpIMC;
1043 lpIMC = LockRealIMC(hIMC);
1044 if (lpIMC == NULL)
1045 return;
1047 hdc = BeginPaint(hwnd, &ps);
1049 GetClientRect(hwnd, &rect);
1050 FillRect(hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
1052 compdata = ImmLockIMCC(lpIMC->hCompStr);
1053 compstr = (LPCOMPOSITIONSTRING)compdata;
1055 if (compstr->dwCompStrLen && compstr->dwCompStrOffset)
1057 SIZE size;
1058 POINT pt;
1059 HFONT oldfont = NULL;
1060 LPWSTR CompString;
1061 LPIMEPRIVATE myPrivate;
1063 CompString = (LPWSTR)(compdata + compstr->dwCompStrOffset);
1064 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
1066 if (myPrivate->textfont)
1067 oldfont = SelectObject(hdc, myPrivate->textfont);
1069 ImmUnlockIMCC(lpIMC->hPrivate);
1071 GetTextExtentPoint32W(hdc, CompString, compstr->dwCompStrLen, &size);
1072 pt.x = size.cx;
1073 pt.y = size.cy;
1074 LPtoDP(hdc, &pt, 1);
1077 * How this works based on tests on windows:
1078 * CFS_POINT: then we start our window at the point and grow it as large
1079 * as it needs to be for the string.
1080 * CFS_RECT: we still use the ptCurrentPos as a starting point and our
1081 * window is only as large as we need for the string, but we do not
1082 * grow such that our window exceeds the given rect. Wrapping if
1083 * needed and possible. If our ptCurrentPos is outside of our rect
1084 * then no window is displayed.
1085 * CFS_FORCE_POSITION: appears to behave just like CFS_POINT
1086 * maybe because the default MSIME does not do any IME adjusting.
1088 if (lpIMC->cfCompForm.dwStyle != CFS_DEFAULT)
1090 POINT cpt = lpIMC->cfCompForm.ptCurrentPos;
1091 ClientToScreen(lpIMC->hWnd, &cpt);
1092 rect.left = cpt.x;
1093 rect.top = cpt.y;
1094 rect.right = rect.left + pt.x;
1095 rect.bottom = rect.top + pt.y;
1096 monitor = MonitorFromPoint(cpt, MONITOR_DEFAULTTOPRIMARY);
1098 else /* CFS_DEFAULT */
1100 /* Windows places the default IME window in the bottom left */
1101 HWND target = lpIMC->hWnd;
1102 if (!target) target = GetFocus();
1104 GetWindowRect(target, &rect);
1105 rect.top = rect.bottom;
1106 rect.right = rect.left + pt.x + 20;
1107 rect.bottom = rect.top + pt.y + 20;
1108 offX=offY=10;
1109 monitor = MonitorFromWindow(target, MONITOR_DEFAULTTOPRIMARY);
1112 if (lpIMC->cfCompForm.dwStyle == CFS_RECT)
1114 RECT client;
1115 client =lpIMC->cfCompForm.rcArea;
1116 MapWindowPoints(lpIMC->hWnd, 0, (POINT *)&client, 2);
1117 IntersectRect(&rect, &rect, &client);
1118 /* TODO: Wrap the input if needed */
1121 if (lpIMC->cfCompForm.dwStyle == CFS_DEFAULT)
1123 /* make sure we are on the desktop */
1124 mon_info.cbSize = sizeof(mon_info);
1125 GetMonitorInfoW(monitor, &mon_info);
1127 if (rect.bottom > mon_info.rcWork.bottom)
1129 int shift = rect.bottom - mon_info.rcWork.bottom;
1130 rect.top -= shift;
1131 rect.bottom -= shift;
1133 if (rect.left < 0)
1135 rect.right -= rect.left;
1136 rect.left = 0;
1138 if (rect.right > mon_info.rcWork.right)
1140 int shift = rect.right - mon_info.rcWork.right;
1141 rect.left -= shift;
1142 rect.right -= shift;
1146 SetWindowPos(hwnd, HWND_TOPMOST, rect.left, rect.top, rect.right - rect.left,
1147 rect.bottom - rect.top, SWP_NOACTIVATE);
1149 TextOutW(hdc, offX, offY, CompString, compstr->dwCompStrLen);
1151 if (oldfont)
1152 SelectObject(hdc, oldfont);
1155 ImmUnlockIMCC(lpIMC->hCompStr);
1157 EndPaint(hwnd, &ps);
1158 UnlockRealIMC(hIMC);
1161 static void DefaultIMEComposition(HIMC hIMC, HWND hwnd, LPARAM lParam)
1163 TRACE("IME message WM_IME_COMPOSITION 0x%lx\n", lParam);
1164 if (lParam & GCS_RESULTSTR)
1166 LPCOMPOSITIONSTRING compstr;
1167 LPBYTE compdata;
1168 LPWSTR ResultStr;
1169 HIMCC newCompStr;
1170 LPINPUTCONTEXT lpIMC;
1172 lpIMC = LockRealIMC(hIMC);
1173 if (lpIMC == NULL)
1174 return;
1176 TRACE("Posting result as IME_CHAR\n");
1177 compdata = ImmLockIMCC(lpIMC->hCompStr);
1178 compstr = (LPCOMPOSITIONSTRING)compdata;
1179 ResultStr = (LPWSTR)(compdata + compstr->dwResultStrOffset);
1180 GenerateIMECHARMessages(hIMC, ResultStr, compstr->dwResultStrLen);
1181 ImmUnlockIMCC(lpIMC->hCompStr);
1183 /* clear the buffer */
1184 newCompStr = updateResultStr(lpIMC->hCompStr, NULL, 0);
1185 ImmDestroyIMCC(lpIMC->hCompStr);
1186 lpIMC->hCompStr = newCompStr;
1187 UnlockRealIMC(hIMC);
1189 else
1190 UpdateDataInDefaultIMEWindow(hIMC, hwnd, TRUE);
1193 static void DefaultIMEStartComposition(HIMC hIMC, HWND hwnd)
1195 LPINPUTCONTEXT lpIMC;
1197 lpIMC = LockRealIMC(hIMC);
1198 if (lpIMC == NULL)
1199 return;
1201 TRACE("IME message WM_IME_STARTCOMPOSITION\n");
1202 lpIMC->hWnd = GetFocus();
1203 ShowWindow(hwnd, SW_SHOWNOACTIVATE);
1204 UnlockRealIMC(hIMC);
1207 static LRESULT ImeHandleNotify(HIMC hIMC, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1209 switch (wParam)
1211 case IMN_OPENSTATUSWINDOW:
1212 FIXME("WM_IME_NOTIFY:IMN_OPENSTATUSWINDOW\n");
1213 break;
1214 case IMN_CLOSESTATUSWINDOW:
1215 FIXME("WM_IME_NOTIFY:IMN_CLOSESTATUSWINDOW\n");
1216 break;
1217 case IMN_OPENCANDIDATE:
1218 FIXME("WM_IME_NOTIFY:IMN_OPENCANDIDATE\n");
1219 break;
1220 case IMN_CHANGECANDIDATE:
1221 FIXME("WM_IME_NOTIFY:IMN_CHANGECANDIDATE\n");
1222 break;
1223 case IMN_CLOSECANDIDATE:
1224 FIXME("WM_IME_NOTIFY:IMN_CLOSECANDIDATE\n");
1225 break;
1226 case IMN_SETCONVERSIONMODE:
1227 FIXME("WM_IME_NOTIFY:IMN_SETCONVERSIONMODE\n");
1228 break;
1229 case IMN_SETSENTENCEMODE:
1230 FIXME("WM_IME_NOTIFY:IMN_SETSENTENCEMODE\n");
1231 break;
1232 case IMN_SETOPENSTATUS:
1233 FIXME("WM_IME_NOTIFY:IMN_SETOPENSTATUS\n");
1234 break;
1235 case IMN_SETCANDIDATEPOS:
1236 FIXME("WM_IME_NOTIFY:IMN_SETCANDIDATEPOS\n");
1237 break;
1238 case IMN_SETCOMPOSITIONFONT:
1239 FIXME("WM_IME_NOTIFY:IMN_SETCOMPOSITIONFONT\n");
1240 break;
1241 case IMN_SETCOMPOSITIONWINDOW:
1242 FIXME("WM_IME_NOTIFY:IMN_SETCOMPOSITIONWINDOW\n");
1243 break;
1244 case IMN_GUIDELINE:
1245 FIXME("WM_IME_NOTIFY:IMN_GUIDELINE\n");
1246 break;
1247 case IMN_SETSTATUSWINDOWPOS:
1248 FIXME("WM_IME_NOTIFY:IMN_SETSTATUSWINDOWPOS\n");
1249 break;
1250 default:
1251 FIXME("WM_IME_NOTIFY:<Unknown 0x%lx>\n", wParam);
1252 break;
1254 return 0;
1257 static LRESULT WINAPI IME_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1259 LRESULT rc = 0;
1260 HIMC hIMC;
1262 TRACE("Incoming Message 0x%x (0x%08lx, 0x%08lx)\n", msg, wParam, lParam);
1265 * Each UI window contains the current Input Context.
1266 * This Input Context can be obtained by calling GetWindowLong
1267 * with IMMGWL_IMC when the UI window receives a WM_IME_xxx message.
1268 * The UI window can refer to this Input Context and handles the
1269 * messages.
1272 hIMC = (HIMC)GetWindowLongPtrW(hwnd, IMMGWL_IMC);
1273 if (!hIMC)
1274 hIMC = RealIMC(FROM_MACDRV);
1276 /* if we have no hIMC there are many messages we cannot process */
1277 if (hIMC == NULL)
1279 switch (msg) {
1280 case WM_IME_STARTCOMPOSITION:
1281 case WM_IME_ENDCOMPOSITION:
1282 case WM_IME_COMPOSITION:
1283 case WM_IME_NOTIFY:
1284 case WM_IME_CONTROL:
1285 case WM_IME_COMPOSITIONFULL:
1286 case WM_IME_SELECT:
1287 case WM_IME_CHAR:
1288 return 0L;
1289 default:
1290 break;
1294 switch (msg)
1296 case WM_CREATE:
1298 LPIMEPRIVATE myPrivate;
1299 LPINPUTCONTEXT lpIMC;
1301 SetWindowTextA(hwnd, "Wine Ime Active");
1303 lpIMC = LockRealIMC(hIMC);
1304 if (lpIMC)
1306 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
1307 myPrivate->hwndDefault = hwnd;
1308 ImmUnlockIMCC(lpIMC->hPrivate);
1310 UnlockRealIMC(hIMC);
1312 return TRUE;
1314 case WM_PAINT:
1315 PaintDefaultIMEWnd(hIMC, hwnd);
1316 return FALSE;
1318 case WM_NCCREATE:
1319 return TRUE;
1321 case WM_SETFOCUS:
1322 if (wParam)
1323 SetFocus((HWND)wParam);
1324 else
1325 FIXME("Received focus, should never have focus\n");
1326 break;
1327 case WM_IME_COMPOSITION:
1328 DefaultIMEComposition(hIMC, hwnd, lParam);
1329 break;
1330 case WM_IME_STARTCOMPOSITION:
1331 DefaultIMEStartComposition(hIMC, hwnd);
1332 break;
1333 case WM_IME_ENDCOMPOSITION:
1334 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_IME_ENDCOMPOSITION", wParam, lParam);
1335 ShowWindow(hwnd, SW_HIDE);
1336 break;
1337 case WM_IME_SELECT:
1338 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_IME_SELECT", wParam, lParam);
1339 break;
1340 case WM_IME_CONTROL:
1341 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_IME_CONTROL", wParam, lParam);
1342 rc = 1;
1343 break;
1344 case WM_IME_NOTIFY:
1345 rc = ImeHandleNotify(hIMC, hwnd, msg, wParam, lParam);
1346 break;
1347 default:
1348 TRACE("Non-standard message 0x%x\n", msg);
1350 /* check the MSIME messages */
1351 if (msg == WM_MSIME_SERVICE)
1353 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_SERVICE", wParam, lParam);
1354 rc = FALSE;
1356 else if (msg == WM_MSIME_RECONVERTOPTIONS)
1358 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_RECONVERTOPTIONS", wParam, lParam);
1360 else if (msg == WM_MSIME_MOUSE)
1362 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_MOUSE", wParam, lParam);
1364 else if (msg == WM_MSIME_RECONVERTREQUEST)
1366 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_RECONVERTREQUEST", wParam, lParam);
1368 else if (msg == WM_MSIME_RECONVERT)
1370 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_RECONVERT", wParam, lParam);
1372 else if (msg == WM_MSIME_QUERYPOSITION)
1374 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_QUERYPOSITION", wParam, lParam);
1376 else if (msg == WM_MSIME_DOCUMENTFEED)
1378 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_DOCUMENTFEED", wParam, lParam);
1380 /* DefWndProc if not an IME message */
1381 if (!rc && !((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
1382 (msg >= WM_IME_SETCONTEXT && msg <= WM_IME_KEYUP)))
1383 rc = DefWindowProcW(hwnd, msg, wParam, lParam);
1385 return rc;
1388 static BOOL WINAPI register_classes( INIT_ONCE *once, void *param, void **context )
1390 WNDCLASSW wndClass;
1391 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
1392 wndClass.style = CS_GLOBALCLASS | CS_IME | CS_HREDRAW | CS_VREDRAW;
1393 wndClass.lpfnWndProc = (WNDPROC) IME_WindowProc;
1394 wndClass.cbClsExtra = 0;
1395 wndClass.cbWndExtra = 2 * sizeof(LONG_PTR);
1396 wndClass.hInstance = macdrv_module;
1397 wndClass.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
1398 wndClass.hIcon = LoadIconW(NULL, (LPWSTR)IDI_APPLICATION);
1399 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1400 wndClass.lpszMenuName = 0;
1401 wndClass.lpszClassName = UI_CLASS_NAME;
1403 RegisterClassW(&wndClass);
1405 WM_MSIME_SERVICE = RegisterWindowMessageA("MSIMEService");
1406 WM_MSIME_RECONVERTOPTIONS = RegisterWindowMessageA("MSIMEReconvertOptions");
1407 WM_MSIME_MOUSE = RegisterWindowMessageA("MSIMEMouseOperation");
1408 WM_MSIME_RECONVERTREQUEST = RegisterWindowMessageA("MSIMEReconvertRequest");
1409 WM_MSIME_RECONVERT = RegisterWindowMessageA("MSIMEReconvert");
1410 WM_MSIME_QUERYPOSITION = RegisterWindowMessageA("MSIMEQueryPosition");
1411 WM_MSIME_DOCUMENTFEED = RegisterWindowMessageA("MSIMEDocumentFeed");
1412 return TRUE;
1415 BOOL WINAPI ImeInquire(LPIMEINFO lpIMEInfo, LPWSTR lpszUIClass, LPCWSTR lpszOption)
1417 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
1419 TRACE("\n");
1420 InitOnceExecuteOnce( &init_once, register_classes, NULL, NULL );
1421 lpIMEInfo->dwPrivateDataSize = sizeof(IMEPRIVATE);
1422 lpIMEInfo->fdwProperty = IME_PROP_UNICODE | IME_PROP_AT_CARET;
1423 lpIMEInfo->fdwConversionCaps = IME_CMODE_NATIVE | IME_CMODE_FULLSHAPE;
1424 lpIMEInfo->fdwSentenceCaps = IME_SMODE_AUTOMATIC;
1425 lpIMEInfo->fdwUICaps = UI_CAP_2700;
1426 /* Tell App we cannot accept ImeSetCompositionString calls */
1427 /* FIXME: Can we? */
1428 lpIMEInfo->fdwSCSCaps = 0;
1429 lpIMEInfo->fdwSelectCaps = SELECT_CAP_CONVERSION;
1431 lstrcpyW(lpszUIClass, UI_CLASS_NAME);
1433 return TRUE;
1436 /* Interfaces to other parts of the Mac driver */
1438 /***********************************************************************
1439 * macdrv_im_set_text
1441 void macdrv_im_set_text(const macdrv_event *event)
1443 HWND hwnd = macdrv_get_window_hwnd(event->window);
1444 void *himc = event->im_set_text.data;
1446 TRACE("win %p/%p himc %p text %s complete %u\n", hwnd, event->window, himc,
1447 debugstr_cf(event->im_set_text.text), event->im_set_text.complete);
1449 if (!himc) himc = RealIMC(FROM_MACDRV);
1451 if (event->im_set_text.text)
1453 CFIndex length = CFStringGetLength(event->im_set_text.text);
1454 const UniChar *chars = CFStringGetCharactersPtr(event->im_set_text.text);
1455 UniChar *buffer = NULL;
1457 if (!chars)
1459 buffer = HeapAlloc(GetProcessHeap(), 0, length * sizeof(*buffer));
1460 CFStringGetCharacters(event->im_set_text.text, CFRangeMake(0, length), buffer);
1461 chars = buffer;
1464 if (himc)
1465 IME_SetCompositionString(himc, SCS_SETSTR, chars, length * sizeof(*chars),
1466 event->im_set_text.cursor_pos, !event->im_set_text.complete);
1467 else
1469 INPUT input;
1470 CFIndex i;
1472 input.type = INPUT_KEYBOARD;
1473 input.ki.wVk = 0;
1474 input.ki.time = 0;
1475 input.ki.dwExtraInfo = 0;
1477 for (i = 0; i < length; i++)
1479 input.ki.wScan = chars[i];
1480 input.ki.dwFlags = KEYEVENTF_UNICODE;
1481 __wine_send_input(hwnd, &input);
1483 input.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
1484 __wine_send_input(hwnd, &input);
1488 HeapFree(GetProcessHeap(), 0, buffer);
1491 if (event->im_set_text.complete)
1492 IME_NotifyComplete(himc);
1495 /***********************************************************************
1496 * macdrv_sent_text_input
1498 void macdrv_sent_text_input(const macdrv_event *event)
1500 TRACE("handled: %s\n", event->sent_text_input.handled ? "TRUE" : "FALSE");
1501 *event->sent_text_input.done = event->sent_text_input.handled ? 1 : -1;
1505 /**************************************************************************
1506 * query_ime_char_rect
1508 BOOL query_ime_char_rect(macdrv_query* query)
1510 HWND hwnd = macdrv_get_window_hwnd(query->window);
1511 void *himc = query->ime_char_rect.data;
1512 CFRange* range = &query->ime_char_rect.range;
1513 CGRect* rect = &query->ime_char_rect.rect;
1514 IMECHARPOSITION charpos;
1515 BOOL ret = FALSE;
1517 TRACE("win %p/%p himc %p range %ld-%ld\n", hwnd, query->window, himc, range->location,
1518 range->length);
1520 if (!himc) himc = RealIMC(FROM_MACDRV);
1522 charpos.dwSize = sizeof(charpos);
1523 charpos.dwCharPos = range->location;
1524 if (ImmRequestMessageW(himc, IMR_QUERYCHARPOSITION, (ULONG_PTR)&charpos))
1526 int i;
1528 *rect = CGRectMake(charpos.pt.x, charpos.pt.y, 0, charpos.cLineHeight);
1530 /* iterate over rest of length to extend rect */
1531 for (i = 1; i < range->length; i++)
1533 charpos.dwSize = sizeof(charpos);
1534 charpos.dwCharPos = range->location + i;
1535 if (!ImmRequestMessageW(himc, IMR_QUERYCHARPOSITION, (ULONG_PTR)&charpos) ||
1536 charpos.pt.y != rect->origin.y)
1538 range->length = i;
1539 break;
1542 rect->size.width = charpos.pt.x - rect->origin.x;
1545 ret = TRUE;
1548 if (!ret)
1550 LPINPUTCONTEXT ic = ImmLockIMC(himc);
1552 if (ic)
1554 LPIMEPRIVATE private = ImmLockIMCC(ic->hPrivate);
1555 LPBYTE compdata = ImmLockIMCC(ic->hCompStr);
1556 LPCOMPOSITIONSTRING compstr = (LPCOMPOSITIONSTRING)compdata;
1557 LPWSTR str = (LPWSTR)(compdata + compstr->dwCompStrOffset);
1559 if (private->hwndDefault && compstr->dwCompStrOffset &&
1560 IsWindowVisible(private->hwndDefault))
1562 HDC dc = GetDC(private->hwndDefault);
1563 HFONT oldfont = NULL;
1564 SIZE size;
1566 if (private->textfont)
1567 oldfont = SelectObject(dc, private->textfont);
1569 if (range->location > compstr->dwCompStrLen)
1570 range->location = compstr->dwCompStrLen;
1571 if (range->location + range->length > compstr->dwCompStrLen)
1572 range->length = compstr->dwCompStrLen - range->location;
1574 GetTextExtentPoint32W(dc, str, range->location, &size);
1575 charpos.rcDocument.left = size.cx;
1576 charpos.rcDocument.top = 0;
1577 GetTextExtentPoint32W(dc, str, range->location + range->length, &size);
1578 charpos.rcDocument.right = size.cx;
1579 charpos.rcDocument.bottom = size.cy;
1581 if (ic->cfCompForm.dwStyle == CFS_DEFAULT)
1582 OffsetRect(&charpos.rcDocument, 10, 10);
1584 LPtoDP(dc, (POINT*)&charpos.rcDocument, 2);
1585 MapWindowPoints(private->hwndDefault, 0, (POINT*)&charpos.rcDocument, 2);
1586 *rect = cgrect_from_rect(charpos.rcDocument);
1587 ret = TRUE;
1589 if (oldfont)
1590 SelectObject(dc, oldfont);
1591 ReleaseDC(private->hwndDefault, dc);
1594 ImmUnlockIMCC(ic->hCompStr);
1595 ImmUnlockIMCC(ic->hPrivate);
1598 ImmUnlockIMC(himc);
1601 if (!ret)
1603 GUITHREADINFO gti;
1604 gti.cbSize = sizeof(gti);
1605 if (GetGUIThreadInfo(0, &gti))
1607 MapWindowPoints(gti.hwndCaret, 0, (POINT*)&gti.rcCaret, 2);
1608 *rect = cgrect_from_rect(gti.rcCaret);
1609 ret = TRUE;
1613 if (ret && range->length && !rect->size.width)
1614 rect->size.width = 1;
1616 TRACE(" -> %s range %ld-%ld rect %s\n", ret ? "TRUE" : "FALSE", range->location,
1617 range->length, wine_dbgstr_cgrect(*rect));
1619 return ret;