psapi/tests: Adjust margin for win10.
[wine/multimedia.git] / dlls / winemac.drv / ime.c
blob7f989142479cf040b9f4793362cdf1bdfdfefd75
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(DWORD);
182 needed_size += lpcs->dwResultReadClauseLen;
183 needed_size += lpcs->dwResultReadStrLen * sizeof(DWORD);
184 needed_size += lpcs->dwResultClauseLen;
185 needed_size += lpcs->dwResultStrLen * sizeof(DWORD);
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);
275 /* CompStr */
276 new_one->dwCompStrLen = len;
277 if (len > 0)
279 new_one->dwCompStrOffset = current_offset;
280 memcpy(&newdata[current_offset], compstr, len * sizeof(WCHAR));
284 ImmUnlockIMCC(rc);
285 if (lpcs)
286 ImmUnlockIMCC(old);
288 return rc;
291 static HIMCC updateResultStr(HIMCC old, LPWSTR resultstr, DWORD len)
293 /* we need to make sure the ResultStr and ResultClause fields are all
294 * set and correct */
295 int needed_size;
296 HIMCC rc;
297 LPBYTE newdata = NULL;
298 LPBYTE olddata = NULL;
299 LPCOMPOSITIONSTRING new_one;
300 LPCOMPOSITIONSTRING lpcs = NULL;
301 INT current_offset = 0;
303 TRACE("%s, %i\n", debugstr_wn(resultstr, len), len);
305 if (old == NULL && resultstr == NULL && len == 0)
306 return NULL;
308 if (resultstr == NULL && len != 0)
310 ERR("resultstr is NULL however we have a len! Please report\n");
311 len = 0;
314 if (old != NULL)
316 olddata = ImmLockIMCC(old);
317 lpcs = (LPCOMPOSITIONSTRING)olddata;
320 needed_size = sizeof(COMPOSITIONSTRING) + len * sizeof(WCHAR) +
321 sizeof(DWORD) * 2;
323 if (lpcs != NULL)
325 needed_size += lpcs->dwCompReadAttrLen;
326 needed_size += lpcs->dwCompReadClauseLen;
327 needed_size += lpcs->dwCompReadStrLen * sizeof(DWORD);
328 needed_size += lpcs->dwCompAttrLen;
329 needed_size += lpcs->dwCompClauseLen;
330 needed_size += lpcs->dwCompStrLen * sizeof(DWORD);
331 needed_size += lpcs->dwResultReadClauseLen;
332 needed_size += lpcs->dwResultReadStrLen * sizeof(DWORD);
333 needed_size += lpcs->dwPrivateSize;
335 rc = ImmCreateIMCC(needed_size);
336 newdata = ImmLockIMCC(rc);
337 new_one = (LPCOMPOSITIONSTRING)newdata;
339 new_one->dwSize = needed_size;
340 current_offset = sizeof(COMPOSITIONSTRING);
341 if (lpcs != NULL)
343 current_offset = updateField(lpcs->dwCompReadAttrLen,
344 lpcs->dwCompReadAttrOffset,
345 current_offset, newdata, olddata,
346 &new_one->dwCompReadAttrLen,
347 &new_one->dwCompReadAttrOffset, FALSE);
349 current_offset = updateField(lpcs->dwCompReadClauseLen,
350 lpcs->dwCompReadClauseOffset,
351 current_offset, newdata, olddata,
352 &new_one->dwCompReadClauseLen,
353 &new_one->dwCompReadClauseOffset, FALSE);
355 current_offset = updateField(lpcs->dwCompReadStrLen,
356 lpcs->dwCompReadStrOffset,
357 current_offset, newdata, olddata,
358 &new_one->dwCompReadStrLen,
359 &new_one->dwCompReadStrOffset, TRUE);
361 current_offset = updateField(lpcs->dwCompAttrLen,
362 lpcs->dwCompAttrOffset,
363 current_offset, newdata, olddata,
364 &new_one->dwCompAttrLen,
365 &new_one->dwCompAttrOffset, FALSE);
367 current_offset = updateField(lpcs->dwCompClauseLen,
368 lpcs->dwCompClauseOffset,
369 current_offset, newdata, olddata,
370 &new_one->dwCompClauseLen,
371 &new_one->dwCompClauseOffset, FALSE);
373 current_offset = updateField(lpcs->dwCompStrLen,
374 lpcs->dwCompStrOffset,
375 current_offset, newdata, olddata,
376 &new_one->dwCompStrLen,
377 &new_one->dwCompStrOffset, TRUE);
379 new_one->dwCursorPos = lpcs->dwCursorPos;
380 new_one->dwDeltaStart = 0;
382 current_offset = updateField(lpcs->dwResultReadClauseLen,
383 lpcs->dwResultReadClauseOffset,
384 current_offset, newdata, olddata,
385 &new_one->dwResultReadClauseLen,
386 &new_one->dwResultReadClauseOffset, FALSE);
388 current_offset = updateField(lpcs->dwResultReadStrLen,
389 lpcs->dwResultReadStrOffset,
390 current_offset, newdata, olddata,
391 &new_one->dwResultReadStrLen,
392 &new_one->dwResultReadStrOffset, TRUE);
394 /* new ResultClause , ResultStr */
396 current_offset = updateField(lpcs->dwPrivateSize,
397 lpcs->dwPrivateOffset,
398 current_offset, newdata, olddata,
399 &new_one->dwPrivateSize,
400 &new_one->dwPrivateOffset, FALSE);
403 /* set new data */
404 /* ResultClause */
405 if (len > 0)
407 new_one->dwResultClauseLen = sizeof(DWORD) * 2;
408 new_one->dwResultClauseOffset = current_offset;
409 *(DWORD*)&newdata[current_offset] = 0;
410 current_offset += sizeof(DWORD);
411 *(DWORD*)&newdata[current_offset] = len;
412 current_offset += sizeof(DWORD);
415 /* ResultStr */
416 new_one->dwResultStrLen = len;
417 if (len > 0)
419 new_one->dwResultStrOffset = current_offset;
420 memcpy(&newdata[current_offset], resultstr, len * sizeof(WCHAR));
422 ImmUnlockIMCC(rc);
423 if (lpcs)
424 ImmUnlockIMCC(old);
426 return rc;
429 static void GenerateIMEMessage(HIMC hIMC, UINT msg, WPARAM wParam, LPARAM lParam)
431 LPINPUTCONTEXT lpIMC;
432 LPTRANSMSG lpTransMsg;
434 lpIMC = LockRealIMC(hIMC);
435 if (lpIMC == NULL)
436 return;
438 lpIMC->hMsgBuf = ImmReSizeIMCC(lpIMC->hMsgBuf, (lpIMC->dwNumMsgBuf + 1) * sizeof(TRANSMSG));
439 if (!lpIMC->hMsgBuf)
440 return;
442 lpTransMsg = ImmLockIMCC(lpIMC->hMsgBuf);
443 if (!lpTransMsg)
444 return;
446 lpTransMsg += lpIMC->dwNumMsgBuf;
447 lpTransMsg->message = msg;
448 lpTransMsg->wParam = wParam;
449 lpTransMsg->lParam = lParam;
451 ImmUnlockIMCC(lpIMC->hMsgBuf);
452 lpIMC->dwNumMsgBuf++;
454 ImmGenerateMessage(RealIMC(hIMC));
455 UnlockRealIMC(hIMC);
458 static void GenerateIMECHARMessages(HIMC hIMC, LPWSTR String, DWORD length)
460 LPINPUTCONTEXT lpIMC;
461 LPTRANSMSG lpTransMsg;
462 DWORD i;
464 if (length <= 0)
465 return;
467 lpIMC = LockRealIMC(hIMC);
468 if (lpIMC == NULL)
469 return;
471 lpIMC->hMsgBuf = ImmReSizeIMCC(lpIMC->hMsgBuf, (lpIMC->dwNumMsgBuf + length) * sizeof(TRANSMSG));
472 if (!lpIMC->hMsgBuf)
473 return;
475 lpTransMsg = ImmLockIMCC(lpIMC->hMsgBuf);
476 if (!lpTransMsg)
477 return;
479 lpTransMsg += lpIMC->dwNumMsgBuf;
480 for (i = 0; i < length; i++)
482 lpTransMsg->message = WM_IME_CHAR;
483 lpTransMsg->wParam = String[i];
484 lpTransMsg->lParam = 1;
485 lpTransMsg++;
488 ImmUnlockIMCC(lpIMC->hMsgBuf);
489 lpIMC->dwNumMsgBuf += length;
491 ImmGenerateMessage(RealIMC(hIMC));
492 UnlockRealIMC(hIMC);
495 static BOOL GenerateMessageToTransKey(LPDWORD lpTransBuf, UINT *uNumTranMsgs,
496 UINT msg, WPARAM wParam, LPARAM lParam)
498 LPTRANSMSG ptr;
500 if (*uNumTranMsgs + 1 >= (UINT)*lpTransBuf)
501 return FALSE;
503 ptr = (LPTRANSMSG)(lpTransBuf + 1 + *uNumTranMsgs * 3);
504 ptr->message = msg;
505 ptr->wParam = wParam;
506 ptr->lParam = lParam;
507 (*uNumTranMsgs)++;
509 return TRUE;
513 static BOOL IME_RemoveFromSelected(HIMC hIMC)
515 int i;
516 for (i = 0; i < hSelectedCount; i++)
518 if (hSelectedFrom[i] == hIMC)
520 if (i < hSelectedCount - 1)
521 memmove(&hSelectedFrom[i], &hSelectedFrom[i + 1], (hSelectedCount - i - 1) * sizeof(HIMC));
522 hSelectedCount--;
523 return TRUE;
526 return FALSE;
529 static void IME_AddToSelected(HIMC hIMC)
531 hSelectedCount++;
532 if (hSelectedFrom)
533 hSelectedFrom = HeapReAlloc(GetProcessHeap(), 0, hSelectedFrom, hSelectedCount * sizeof(HIMC));
534 else
535 hSelectedFrom = HeapAlloc(GetProcessHeap(), 0, sizeof(HIMC));
536 hSelectedFrom[hSelectedCount - 1] = hIMC;
539 static void UpdateDataInDefaultIMEWindow(HIMC hIMC, HWND hwnd, BOOL showable)
541 LPCOMPOSITIONSTRING compstr;
542 LPINPUTCONTEXT lpIMC;
544 lpIMC = LockRealIMC(hIMC);
545 if (lpIMC == NULL)
546 return;
548 if (lpIMC->hCompStr)
549 compstr = ImmLockIMCC(lpIMC->hCompStr);
550 else
551 compstr = NULL;
553 if (compstr == NULL || compstr->dwCompStrLen == 0)
554 ShowWindow(hwnd, SW_HIDE);
555 else if (showable)
556 ShowWindow(hwnd, SW_SHOWNOACTIVATE);
558 RedrawWindow(hwnd, NULL, NULL, RDW_ERASENOW | RDW_INVALIDATE);
560 if (compstr != NULL)
561 ImmUnlockIMCC(lpIMC->hCompStr);
563 UnlockRealIMC(hIMC);
566 BOOL WINAPI ImeConfigure(HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
568 FIXME("(%p, %p, %d, %p): stub\n", hKL, hWnd, dwMode, lpData);
569 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
570 return FALSE;
573 DWORD WINAPI ImeConversionList(HIMC hIMC, LPCWSTR lpSource, LPCANDIDATELIST lpCandList,
574 DWORD dwBufLen, UINT uFlag)
577 FIXME("(%p, %s, %p, %d, %d): stub\n", hIMC, debugstr_w(lpSource), lpCandList,
578 dwBufLen, uFlag);
579 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
580 return 0;
583 BOOL WINAPI ImeDestroy(UINT uForce)
585 TRACE("\n");
586 HeapFree(GetProcessHeap(), 0, hSelectedFrom);
587 hSelectedFrom = NULL;
588 hSelectedCount = 0;
589 return TRUE;
592 LRESULT WINAPI ImeEscape(HIMC hIMC, UINT uSubFunc, LPVOID lpData)
594 TRACE("%x %p\n", uSubFunc, lpData);
595 return 0;
598 BOOL WINAPI ImeProcessKey(HIMC hIMC, UINT vKey, LPARAM lKeyData, const LPBYTE lpbKeyState)
600 LPINPUTCONTEXT lpIMC;
601 BOOL inIME;
603 TRACE("hIMC %p vKey 0x%04x lKeyData 0x%08lx lpbKeyState %p\n", hIMC, vKey, lKeyData, lpbKeyState);
605 switch (vKey)
607 case VK_SHIFT:
608 case VK_CONTROL:
609 case VK_CAPITAL:
610 case VK_MENU:
611 return FALSE;
614 inIME = macdrv_using_input_method();
615 lpIMC = LockRealIMC(hIMC);
616 if (lpIMC)
618 LPIMEPRIVATE myPrivate;
619 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
621 if (inIME && !myPrivate->bInternalState)
622 ImmSetOpenStatus(RealIMC(FROM_MACDRV), TRUE);
623 else if (!inIME && myPrivate->bInternalState)
625 ShowWindow(myPrivate->hwndDefault, SW_HIDE);
626 ImmDestroyIMCC(lpIMC->hCompStr);
627 lpIMC->hCompStr = ImeCreateBlankCompStr();
628 ImmSetOpenStatus(RealIMC(FROM_MACDRV), FALSE);
631 myPrivate->repeat = (lKeyData >> 30) & 0x1;
633 myPrivate->bInternalState = inIME;
634 ImmUnlockIMCC(lpIMC->hPrivate);
636 UnlockRealIMC(hIMC);
638 return inIME;
641 BOOL WINAPI ImeSelect(HIMC hIMC, BOOL fSelect)
643 LPINPUTCONTEXT lpIMC;
644 TRACE("%p %s\n", hIMC, fSelect ? "TRUE" : "FALSE");
646 if (hIMC == FROM_MACDRV)
648 ERR("ImeSelect should never be called from Cocoa\n");
649 return FALSE;
652 if (!hIMC)
653 return TRUE;
655 /* not selected */
656 if (!fSelect)
657 return IME_RemoveFromSelected(hIMC);
659 IME_AddToSelected(hIMC);
661 /* Initialize our structures */
662 lpIMC = LockRealIMC(hIMC);
663 if (lpIMC != NULL)
665 LPIMEPRIVATE myPrivate;
666 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
667 myPrivate->bInComposition = FALSE;
668 myPrivate->bInternalState = FALSE;
669 myPrivate->textfont = NULL;
670 myPrivate->hwndDefault = NULL;
671 myPrivate->repeat = 0;
672 ImmUnlockIMCC(lpIMC->hPrivate);
673 UnlockRealIMC(hIMC);
676 return TRUE;
679 BOOL WINAPI ImeSetActiveContext(HIMC hIMC, BOOL fFlag)
681 FIXME("(%p, %x): stub\n", hIMC, fFlag);
682 return TRUE;
685 UINT WINAPI ImeToAsciiEx(UINT uVKey, UINT uScanCode, const LPBYTE lpbKeyState,
686 LPDWORD lpdwTransKey, UINT fuState, HIMC hIMC)
688 UINT vkey;
689 LPINPUTCONTEXT lpIMC;
690 LPIMEPRIVATE myPrivate;
691 HWND hwndDefault;
692 UINT repeat;
693 INT rc;
695 TRACE("uVKey 0x%04x uScanCode 0x%04x fuState %u hIMC %p\n", uVKey, uScanCode, fuState, hIMC);
697 vkey = LOWORD(uVKey);
699 if (vkey == VK_KANA || vkey == VK_KANJI || vkey == VK_MENU)
701 TRACE("Skipping metakey\n");
702 return 0;
705 lpIMC = LockRealIMC(hIMC);
706 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
707 if (!myPrivate->bInternalState)
709 ImmUnlockIMCC(lpIMC->hPrivate);
710 UnlockRealIMC(hIMC);
711 return 0;
714 repeat = myPrivate->repeat;
715 hwndDefault = myPrivate->hwndDefault;
716 ImmUnlockIMCC(lpIMC->hPrivate);
717 UnlockRealIMC(hIMC);
719 TRACE("Processing Mac 0x%04x\n", vkey);
720 rc = macdrv_process_text_input(uVKey, uScanCode, repeat, lpbKeyState, hIMC);
722 if (!rc)
724 UINT msgs = 0;
725 UINT msg = (uScanCode & 0x8000) ? WM_KEYUP : WM_KEYDOWN;
727 /* KeyStroke not processed by the IME
728 * so we need to rebuild the KeyDown message and pass it on to WINE
730 if (!GenerateMessageToTransKey(lpdwTransKey, &msgs, msg, vkey, MAKELONG(0x0001, uScanCode)))
731 GenerateIMEMessage(hIMC, msg, vkey, MAKELONG(0x0001, uScanCode));
733 return msgs;
735 else
736 UpdateDataInDefaultIMEWindow(hIMC, hwndDefault, FALSE);
737 return 0;
740 BOOL WINAPI NotifyIME(HIMC hIMC, DWORD dwAction, DWORD dwIndex, DWORD dwValue)
742 BOOL bRet = FALSE;
743 LPINPUTCONTEXT lpIMC;
745 TRACE("%p %i %i %i\n", hIMC, dwAction, dwIndex, dwValue);
747 lpIMC = LockRealIMC(hIMC);
748 if (lpIMC == NULL)
749 return FALSE;
751 switch (dwAction)
753 case NI_OPENCANDIDATE: FIXME("NI_OPENCANDIDATE\n"); break;
754 case NI_CLOSECANDIDATE: FIXME("NI_CLOSECANDIDATE\n"); break;
755 case NI_SELECTCANDIDATESTR: FIXME("NI_SELECTCANDIDATESTR\n"); break;
756 case NI_CHANGECANDIDATELIST: FIXME("NI_CHANGECANDIDATELIST\n"); break;
757 case NI_SETCANDIDATE_PAGESTART: FIXME("NI_SETCANDIDATE_PAGESTART\n"); break;
758 case NI_SETCANDIDATE_PAGESIZE: FIXME("NI_SETCANDIDATE_PAGESIZE\n"); break;
759 case NI_CONTEXTUPDATED:
760 switch (dwValue)
762 case IMC_SETCOMPOSITIONWINDOW: FIXME("NI_CONTEXTUPDATED: IMC_SETCOMPOSITIONWINDOW\n"); break;
763 case IMC_SETCONVERSIONMODE: FIXME("NI_CONTEXTUPDATED: IMC_SETCONVERSIONMODE\n"); break;
764 case IMC_SETSENTENCEMODE: FIXME("NI_CONTEXTUPDATED: IMC_SETSENTENCEMODE\n"); break;
765 case IMC_SETCANDIDATEPOS: FIXME("NI_CONTEXTUPDATED: IMC_SETCANDIDATEPOS\n"); break;
766 case IMC_SETCOMPOSITIONFONT:
768 LPIMEPRIVATE myPrivate;
769 TRACE("NI_CONTEXTUPDATED: IMC_SETCOMPOSITIONFONT\n");
771 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
772 if (myPrivate->textfont)
774 DeleteObject(myPrivate->textfont);
775 myPrivate->textfont = NULL;
777 myPrivate->textfont = CreateFontIndirectW(&lpIMC->lfFont.W);
778 ImmUnlockIMCC(lpIMC->hPrivate);
780 break;
781 case IMC_SETOPENSTATUS:
783 LPIMEPRIVATE myPrivate;
784 TRACE("NI_CONTEXTUPDATED: IMC_SETOPENSTATUS\n");
786 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
787 if (lpIMC->fOpen != myPrivate->bInternalState && myPrivate->bInComposition)
789 if(lpIMC->fOpen == FALSE)
791 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
792 myPrivate->bInComposition = FALSE;
794 else
796 GenerateIMEMessage(hIMC, WM_IME_STARTCOMPOSITION, 0, 0);
797 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, 0, 0);
800 myPrivate->bInternalState = lpIMC->fOpen;
801 bRet = TRUE;
803 break;
804 default: FIXME("NI_CONTEXTUPDATED: Unknown\n"); break;
806 break;
807 case NI_COMPOSITIONSTR:
808 switch (dwIndex)
810 case CPS_COMPLETE:
812 HIMCC newCompStr;
813 DWORD cplen = 0;
814 LPWSTR cpstr;
815 LPCOMPOSITIONSTRING cs = NULL;
816 LPBYTE cdata = NULL;
817 LPIMEPRIVATE myPrivate;
819 TRACE("NI_COMPOSITIONSTR: CPS_COMPLETE\n");
821 /* clear existing result */
822 newCompStr = updateResultStr(lpIMC->hCompStr, NULL, 0);
824 ImmDestroyIMCC(lpIMC->hCompStr);
825 lpIMC->hCompStr = newCompStr;
827 if (lpIMC->hCompStr)
829 cdata = ImmLockIMCC(lpIMC->hCompStr);
830 cs = (LPCOMPOSITIONSTRING)cdata;
831 cplen = cs->dwCompStrLen;
832 cpstr = (LPWSTR)&cdata[cs->dwCompStrOffset];
833 ImmUnlockIMCC(lpIMC->hCompStr);
835 if (cplen > 0)
837 WCHAR param = cpstr[0];
838 DWORD flags = GCS_COMPSTR;
840 newCompStr = updateResultStr(lpIMC->hCompStr, cpstr, cplen);
841 ImmDestroyIMCC(lpIMC->hCompStr);
842 lpIMC->hCompStr = newCompStr;
843 newCompStr = updateCompStr(lpIMC->hCompStr, NULL, 0, &flags);
844 ImmDestroyIMCC(lpIMC->hCompStr);
845 lpIMC->hCompStr = newCompStr;
847 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, 0, flags);
849 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, param,
850 GCS_RESULTSTR | GCS_RESULTCLAUSE);
853 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
855 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
856 myPrivate->bInComposition = FALSE;
857 ImmUnlockIMCC(lpIMC->hPrivate);
859 bRet = TRUE;
861 break;
862 case CPS_CONVERT: FIXME("NI_COMPOSITIONSTR: CPS_CONVERT\n"); break;
863 case CPS_REVERT: FIXME("NI_COMPOSITIONSTR: CPS_REVERT\n"); break;
864 case CPS_CANCEL:
866 LPIMEPRIVATE myPrivate;
868 TRACE("NI_COMPOSITIONSTR: CPS_CANCEL\n");
870 if (lpIMC->hCompStr)
871 ImmDestroyIMCC(lpIMC->hCompStr);
873 lpIMC->hCompStr = ImeCreateBlankCompStr();
875 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
876 if (myPrivate->bInComposition)
878 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
879 myPrivate->bInComposition = FALSE;
881 ImmUnlockIMCC(lpIMC->hPrivate);
882 bRet = TRUE;
884 break;
885 default: FIXME("NI_COMPOSITIONSTR: Unknown\n"); break;
887 break;
888 default: FIXME("Unknown Message\n"); break;
891 UnlockRealIMC(hIMC);
892 return bRet;
895 BOOL WINAPI ImeRegisterWord(LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszRegister)
897 FIXME("(%s, %d, %s): stub\n", debugstr_w(lpszReading), dwStyle, debugstr_w(lpszRegister));
898 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
899 return FALSE;
902 BOOL WINAPI ImeUnregisterWord(LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszUnregister)
904 FIXME("(%s, %d, %s): stub\n", debugstr_w(lpszReading), dwStyle, debugstr_w(lpszUnregister));
905 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
906 return FALSE;
909 UINT WINAPI ImeGetRegisterWordStyle(UINT nItem, LPSTYLEBUFW lpStyleBuf)
911 FIXME("(%d, %p): stub\n", nItem, lpStyleBuf);
912 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
913 return 0;
916 UINT WINAPI ImeEnumRegisterWord(REGISTERWORDENUMPROCW lpfnEnumProc, LPCWSTR lpszReading,
917 DWORD dwStyle, LPCWSTR lpszRegister, LPVOID lpData)
919 FIXME("(%p, %s, %d, %s, %p): stub\n", lpfnEnumProc, debugstr_w(lpszReading), dwStyle,
920 debugstr_w(lpszRegister), lpData);
921 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
922 return 0;
925 static BOOL IME_SetCompositionString(void* hIMC, DWORD dwIndex, LPCVOID lpComp, DWORD dwCompLen, DWORD cursor_pos, BOOL cursor_valid)
927 LPINPUTCONTEXT lpIMC;
928 DWORD flags = 0;
929 WCHAR wParam = 0;
930 LPIMEPRIVATE myPrivate;
932 TRACE("(%p, %d, %p, %d):\n", hIMC, dwIndex, lpComp, dwCompLen);
935 * Explanation:
936 * this sets the composition string in the imm32.dll level
937 * of the composition buffer.
938 * TODO: set the Cocoa window's marked text string and tell text input context
941 lpIMC = LockRealIMC(hIMC);
943 if (lpIMC == NULL)
944 return FALSE;
946 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
948 if (dwIndex == SCS_SETSTR)
950 HIMCC newCompStr;
952 if (!myPrivate->bInComposition)
954 GenerateIMEMessage(hIMC, WM_IME_STARTCOMPOSITION, 0, 0);
955 myPrivate->bInComposition = TRUE;
958 flags = GCS_COMPSTR;
960 if (dwCompLen && lpComp)
962 newCompStr = updateCompStr(lpIMC->hCompStr, (LPCWSTR)lpComp, dwCompLen / sizeof(WCHAR), &flags);
963 ImmDestroyIMCC(lpIMC->hCompStr);
964 lpIMC->hCompStr = newCompStr;
966 wParam = ((const WCHAR*)lpComp)[0];
967 flags |= GCS_COMPCLAUSE | GCS_COMPATTR | GCS_DELTASTART;
969 else
971 newCompStr = updateCompStr(lpIMC->hCompStr, NULL, 0, &flags);
972 ImmDestroyIMCC(lpIMC->hCompStr);
973 lpIMC->hCompStr = newCompStr;
976 if (cursor_valid)
978 LPCOMPOSITIONSTRING compstr;
979 compstr = ImmLockIMCC(lpIMC->hCompStr);
980 compstr->dwCursorPos = cursor_pos;
981 ImmUnlockIMCC(lpIMC->hCompStr);
982 flags |= GCS_CURSORPOS;
987 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, wParam, flags);
988 ImmUnlockIMCC(lpIMC->hPrivate);
989 UnlockRealIMC(hIMC);
991 return TRUE;
994 BOOL WINAPI ImeSetCompositionString(HIMC hIMC, DWORD dwIndex, LPCVOID lpComp, DWORD dwCompLen,
995 LPCVOID lpRead, DWORD dwReadLen)
997 TRACE("(%p, %d, %p, %d, %p, %d):\n", hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
999 if (lpRead && dwReadLen)
1000 FIXME("Reading string unimplemented\n");
1002 return IME_SetCompositionString(hIMC, dwIndex, lpComp, dwCompLen, 0, FALSE);
1005 DWORD WINAPI ImeGetImeMenuItems(HIMC hIMC, DWORD dwFlags, DWORD dwType, LPIMEMENUITEMINFOW lpImeParentMenu,
1006 LPIMEMENUITEMINFOW lpImeMenu, DWORD dwSize)
1008 FIXME("(%p, %x %x %p %p %x): stub\n", hIMC, dwFlags, dwType, lpImeParentMenu, lpImeMenu, dwSize);
1009 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1010 return 0;
1013 static void IME_NotifyComplete(void* hIMC)
1015 NotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
1018 /*****
1019 * Internal functions to help with IME window management
1021 static void PaintDefaultIMEWnd(HIMC hIMC, HWND hwnd)
1023 PAINTSTRUCT ps;
1024 RECT rect;
1025 HDC hdc;
1026 LPCOMPOSITIONSTRING compstr;
1027 LPBYTE compdata = NULL;
1028 HMONITOR monitor;
1029 MONITORINFO mon_info;
1030 INT offX = 0, offY = 0;
1031 LPINPUTCONTEXT lpIMC;
1033 lpIMC = LockRealIMC(hIMC);
1034 if (lpIMC == NULL)
1035 return;
1037 hdc = BeginPaint(hwnd, &ps);
1039 GetClientRect(hwnd, &rect);
1040 FillRect(hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
1042 compdata = ImmLockIMCC(lpIMC->hCompStr);
1043 compstr = (LPCOMPOSITIONSTRING)compdata;
1045 if (compstr->dwCompStrLen && compstr->dwCompStrOffset)
1047 SIZE size;
1048 POINT pt;
1049 HFONT oldfont = NULL;
1050 LPWSTR CompString;
1051 LPIMEPRIVATE myPrivate;
1053 CompString = (LPWSTR)(compdata + compstr->dwCompStrOffset);
1054 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
1056 if (myPrivate->textfont)
1057 oldfont = SelectObject(hdc, myPrivate->textfont);
1059 ImmUnlockIMCC(lpIMC->hPrivate);
1061 GetTextExtentPoint32W(hdc, CompString, compstr->dwCompStrLen, &size);
1062 pt.x = size.cx;
1063 pt.y = size.cy;
1064 LPtoDP(hdc, &pt, 1);
1067 * How this works based on tests on windows:
1068 * CFS_POINT: then we start our window at the point and grow it as large
1069 * as it needs to be for the string.
1070 * CFS_RECT: we still use the ptCurrentPos as a starting point and our
1071 * window is only as large as we need for the string, but we do not
1072 * grow such that our window exceeds the given rect. Wrapping if
1073 * needed and possible. If our ptCurrentPos is outside of our rect
1074 * then no window is displayed.
1075 * CFS_FORCE_POSITION: appears to behave just like CFS_POINT
1076 * maybe because the default MSIME does not do any IME adjusting.
1078 if (lpIMC->cfCompForm.dwStyle != CFS_DEFAULT)
1080 POINT cpt = lpIMC->cfCompForm.ptCurrentPos;
1081 ClientToScreen(lpIMC->hWnd, &cpt);
1082 rect.left = cpt.x;
1083 rect.top = cpt.y;
1084 rect.right = rect.left + pt.x;
1085 rect.bottom = rect.top + pt.y;
1086 monitor = MonitorFromPoint(cpt, MONITOR_DEFAULTTOPRIMARY);
1088 else /* CFS_DEFAULT */
1090 /* Windows places the default IME window in the bottom left */
1091 HWND target = lpIMC->hWnd;
1092 if (!target) target = GetFocus();
1094 GetWindowRect(target, &rect);
1095 rect.top = rect.bottom;
1096 rect.right = rect.left + pt.x + 20;
1097 rect.bottom = rect.top + pt.y + 20;
1098 offX=offY=10;
1099 monitor = MonitorFromWindow(target, MONITOR_DEFAULTTOPRIMARY);
1102 if (lpIMC->cfCompForm.dwStyle == CFS_RECT)
1104 RECT client;
1105 client =lpIMC->cfCompForm.rcArea;
1106 MapWindowPoints(lpIMC->hWnd, 0, (POINT *)&client, 2);
1107 IntersectRect(&rect, &rect, &client);
1108 /* TODO: Wrap the input if needed */
1111 if (lpIMC->cfCompForm.dwStyle == CFS_DEFAULT)
1113 /* make sure we are on the desktop */
1114 mon_info.cbSize = sizeof(mon_info);
1115 GetMonitorInfoW(monitor, &mon_info);
1117 if (rect.bottom > mon_info.rcWork.bottom)
1119 int shift = rect.bottom - mon_info.rcWork.bottom;
1120 rect.top -= shift;
1121 rect.bottom -= shift;
1123 if (rect.left < 0)
1125 rect.right -= rect.left;
1126 rect.left = 0;
1128 if (rect.right > mon_info.rcWork.right)
1130 int shift = rect.right - mon_info.rcWork.right;
1131 rect.left -= shift;
1132 rect.right -= shift;
1136 SetWindowPos(hwnd, HWND_TOPMOST, rect.left, rect.top, rect.right - rect.left,
1137 rect.bottom - rect.top, SWP_NOACTIVATE);
1139 TextOutW(hdc, offX, offY, CompString, compstr->dwCompStrLen);
1141 if (oldfont)
1142 SelectObject(hdc, oldfont);
1145 ImmUnlockIMCC(lpIMC->hCompStr);
1147 EndPaint(hwnd, &ps);
1148 UnlockRealIMC(hIMC);
1151 static void DefaultIMEComposition(HIMC hIMC, HWND hwnd, LPARAM lParam)
1153 TRACE("IME message WM_IME_COMPOSITION 0x%lx\n", lParam);
1154 if (lParam & GCS_RESULTSTR)
1156 LPCOMPOSITIONSTRING compstr;
1157 LPBYTE compdata;
1158 LPWSTR ResultStr;
1159 HIMCC newCompStr;
1160 LPINPUTCONTEXT lpIMC;
1162 lpIMC = LockRealIMC(hIMC);
1163 if (lpIMC == NULL)
1164 return;
1166 TRACE("Posting result as IME_CHAR\n");
1167 compdata = ImmLockIMCC(lpIMC->hCompStr);
1168 compstr = (LPCOMPOSITIONSTRING)compdata;
1169 ResultStr = (LPWSTR)(compdata + compstr->dwResultStrOffset);
1170 GenerateIMECHARMessages(hIMC, ResultStr, compstr->dwResultStrLen);
1171 ImmUnlockIMCC(lpIMC->hCompStr);
1173 /* clear the buffer */
1174 newCompStr = updateResultStr(lpIMC->hCompStr, NULL, 0);
1175 ImmDestroyIMCC(lpIMC->hCompStr);
1176 lpIMC->hCompStr = newCompStr;
1177 UnlockRealIMC(hIMC);
1179 else
1180 UpdateDataInDefaultIMEWindow(hIMC, hwnd, TRUE);
1183 static void DefaultIMEStartComposition(HIMC hIMC, HWND hwnd)
1185 LPINPUTCONTEXT lpIMC;
1187 lpIMC = LockRealIMC(hIMC);
1188 if (lpIMC == NULL)
1189 return;
1191 TRACE("IME message WM_IME_STARTCOMPOSITION\n");
1192 lpIMC->hWnd = GetFocus();
1193 ShowWindow(hwnd, SW_SHOWNOACTIVATE);
1194 UnlockRealIMC(hIMC);
1197 static LRESULT ImeHandleNotify(HIMC hIMC, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1199 switch (wParam)
1201 case IMN_OPENSTATUSWINDOW:
1202 FIXME("WM_IME_NOTIFY:IMN_OPENSTATUSWINDOW\n");
1203 break;
1204 case IMN_CLOSESTATUSWINDOW:
1205 FIXME("WM_IME_NOTIFY:IMN_CLOSESTATUSWINDOW\n");
1206 break;
1207 case IMN_OPENCANDIDATE:
1208 FIXME("WM_IME_NOTIFY:IMN_OPENCANDIDATE\n");
1209 break;
1210 case IMN_CHANGECANDIDATE:
1211 FIXME("WM_IME_NOTIFY:IMN_CHANGECANDIDATE\n");
1212 break;
1213 case IMN_CLOSECANDIDATE:
1214 FIXME("WM_IME_NOTIFY:IMN_CLOSECANDIDATE\n");
1215 break;
1216 case IMN_SETCONVERSIONMODE:
1217 FIXME("WM_IME_NOTIFY:IMN_SETCONVERSIONMODE\n");
1218 break;
1219 case IMN_SETSENTENCEMODE:
1220 FIXME("WM_IME_NOTIFY:IMN_SETSENTENCEMODE\n");
1221 break;
1222 case IMN_SETOPENSTATUS:
1223 FIXME("WM_IME_NOTIFY:IMN_SETOPENSTATUS\n");
1224 break;
1225 case IMN_SETCANDIDATEPOS:
1226 FIXME("WM_IME_NOTIFY:IMN_SETCANDIDATEPOS\n");
1227 break;
1228 case IMN_SETCOMPOSITIONFONT:
1229 FIXME("WM_IME_NOTIFY:IMN_SETCOMPOSITIONFONT\n");
1230 break;
1231 case IMN_SETCOMPOSITIONWINDOW:
1232 FIXME("WM_IME_NOTIFY:IMN_SETCOMPOSITIONWINDOW\n");
1233 break;
1234 case IMN_GUIDELINE:
1235 FIXME("WM_IME_NOTIFY:IMN_GUIDELINE\n");
1236 break;
1237 case IMN_SETSTATUSWINDOWPOS:
1238 FIXME("WM_IME_NOTIFY:IMN_SETSTATUSWINDOWPOS\n");
1239 break;
1240 default:
1241 FIXME("WM_IME_NOTIFY:<Unknown 0x%lx>\n", wParam);
1242 break;
1244 return 0;
1247 static LRESULT WINAPI IME_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1249 LRESULT rc = 0;
1250 HIMC hIMC;
1252 TRACE("Incoming Message 0x%x (0x%08lx, 0x%08lx)\n", msg, wParam, lParam);
1255 * Each UI window contains the current Input Context.
1256 * This Input Context can be obtained by calling GetWindowLong
1257 * with IMMGWL_IMC when the UI window receives a WM_IME_xxx message.
1258 * The UI window can refer to this Input Context and handles the
1259 * messages.
1262 hIMC = (HIMC)GetWindowLongPtrW(hwnd, IMMGWL_IMC);
1263 if (!hIMC)
1264 hIMC = RealIMC(FROM_MACDRV);
1266 /* if we have no hIMC there are many messages we cannot process */
1267 if (hIMC == NULL)
1269 switch (msg) {
1270 case WM_IME_STARTCOMPOSITION:
1271 case WM_IME_ENDCOMPOSITION:
1272 case WM_IME_COMPOSITION:
1273 case WM_IME_NOTIFY:
1274 case WM_IME_CONTROL:
1275 case WM_IME_COMPOSITIONFULL:
1276 case WM_IME_SELECT:
1277 case WM_IME_CHAR:
1278 return 0L;
1279 default:
1280 break;
1284 switch (msg)
1286 case WM_CREATE:
1288 LPIMEPRIVATE myPrivate;
1289 LPINPUTCONTEXT lpIMC;
1291 SetWindowTextA(hwnd, "Wine Ime Active");
1293 lpIMC = LockRealIMC(hIMC);
1294 if (lpIMC)
1296 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
1297 myPrivate->hwndDefault = hwnd;
1298 ImmUnlockIMCC(lpIMC->hPrivate);
1300 UnlockRealIMC(hIMC);
1302 return TRUE;
1304 case WM_PAINT:
1305 PaintDefaultIMEWnd(hIMC, hwnd);
1306 return FALSE;
1308 case WM_NCCREATE:
1309 return TRUE;
1311 case WM_SETFOCUS:
1312 if (wParam)
1313 SetFocus((HWND)wParam);
1314 else
1315 FIXME("Received focus, should never have focus\n");
1316 break;
1317 case WM_IME_COMPOSITION:
1318 DefaultIMEComposition(hIMC, hwnd, lParam);
1319 break;
1320 case WM_IME_STARTCOMPOSITION:
1321 DefaultIMEStartComposition(hIMC, hwnd);
1322 break;
1323 case WM_IME_ENDCOMPOSITION:
1324 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_IME_ENDCOMPOSITION", wParam, lParam);
1325 ShowWindow(hwnd, SW_HIDE);
1326 break;
1327 case WM_IME_SELECT:
1328 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_IME_SELECT", wParam, lParam);
1329 break;
1330 case WM_IME_CONTROL:
1331 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_IME_CONTROL", wParam, lParam);
1332 rc = 1;
1333 break;
1334 case WM_IME_NOTIFY:
1335 rc = ImeHandleNotify(hIMC, hwnd, msg, wParam, lParam);
1336 break;
1337 default:
1338 TRACE("Non-standard message 0x%x\n", msg);
1340 /* check the MSIME messages */
1341 if (msg == WM_MSIME_SERVICE)
1343 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_SERVICE", wParam, lParam);
1344 rc = FALSE;
1346 else if (msg == WM_MSIME_RECONVERTOPTIONS)
1348 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_RECONVERTOPTIONS", wParam, lParam);
1350 else if (msg == WM_MSIME_MOUSE)
1352 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_MOUSE", wParam, lParam);
1354 else if (msg == WM_MSIME_RECONVERTREQUEST)
1356 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_RECONVERTREQUEST", wParam, lParam);
1358 else if (msg == WM_MSIME_RECONVERT)
1360 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_RECONVERT", wParam, lParam);
1362 else if (msg == WM_MSIME_QUERYPOSITION)
1364 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_QUERYPOSITION", wParam, lParam);
1366 else if (msg == WM_MSIME_DOCUMENTFEED)
1368 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_DOCUMENTFEED", wParam, lParam);
1370 /* DefWndProc if not an IME message */
1371 if (!rc && !((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
1372 (msg >= WM_IME_SETCONTEXT && msg <= WM_IME_KEYUP)))
1373 rc = DefWindowProcW(hwnd, msg, wParam, lParam);
1375 return rc;
1378 static BOOL WINAPI register_classes( INIT_ONCE *once, void *param, void **context )
1380 WNDCLASSW wndClass;
1381 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
1382 wndClass.style = CS_GLOBALCLASS | CS_IME | CS_HREDRAW | CS_VREDRAW;
1383 wndClass.lpfnWndProc = (WNDPROC) IME_WindowProc;
1384 wndClass.cbClsExtra = 0;
1385 wndClass.cbWndExtra = 2 * sizeof(LONG_PTR);
1386 wndClass.hInstance = macdrv_module;
1387 wndClass.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
1388 wndClass.hIcon = LoadIconW(NULL, (LPWSTR)IDI_APPLICATION);
1389 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1390 wndClass.lpszMenuName = 0;
1391 wndClass.lpszClassName = UI_CLASS_NAME;
1393 RegisterClassW(&wndClass);
1395 WM_MSIME_SERVICE = RegisterWindowMessageA("MSIMEService");
1396 WM_MSIME_RECONVERTOPTIONS = RegisterWindowMessageA("MSIMEReconvertOptions");
1397 WM_MSIME_MOUSE = RegisterWindowMessageA("MSIMEMouseOperation");
1398 WM_MSIME_RECONVERTREQUEST = RegisterWindowMessageA("MSIMEReconvertRequest");
1399 WM_MSIME_RECONVERT = RegisterWindowMessageA("MSIMEReconvert");
1400 WM_MSIME_QUERYPOSITION = RegisterWindowMessageA("MSIMEQueryPosition");
1401 WM_MSIME_DOCUMENTFEED = RegisterWindowMessageA("MSIMEDocumentFeed");
1402 return TRUE;
1405 BOOL WINAPI ImeInquire(LPIMEINFO lpIMEInfo, LPWSTR lpszUIClass, LPCWSTR lpszOption)
1407 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
1409 TRACE("\n");
1410 InitOnceExecuteOnce( &init_once, register_classes, NULL, NULL );
1411 lpIMEInfo->dwPrivateDataSize = sizeof(IMEPRIVATE);
1412 lpIMEInfo->fdwProperty = IME_PROP_UNICODE | IME_PROP_AT_CARET;
1413 lpIMEInfo->fdwConversionCaps = IME_CMODE_NATIVE | IME_CMODE_FULLSHAPE;
1414 lpIMEInfo->fdwSentenceCaps = IME_SMODE_AUTOMATIC;
1415 lpIMEInfo->fdwUICaps = UI_CAP_2700;
1416 /* Tell App we cannot accept ImeSetCompositionString calls */
1417 /* FIXME: Can we? */
1418 lpIMEInfo->fdwSCSCaps = 0;
1419 lpIMEInfo->fdwSelectCaps = SELECT_CAP_CONVERSION;
1421 lstrcpyW(lpszUIClass, UI_CLASS_NAME);
1423 return TRUE;
1426 /* Interfaces to other parts of the Mac driver */
1428 /***********************************************************************
1429 * macdrv_im_set_text
1431 void macdrv_im_set_text(const macdrv_event *event)
1433 HWND hwnd = macdrv_get_window_hwnd(event->window);
1434 void *himc = event->im_set_text.data;
1436 TRACE("win %p/%p himc %p text %s complete %u\n", hwnd, event->window, himc,
1437 debugstr_cf(event->im_set_text.text), event->im_set_text.complete);
1439 if (!himc) himc = RealIMC(FROM_MACDRV);
1441 if (event->im_set_text.text)
1443 CFIndex length = CFStringGetLength(event->im_set_text.text);
1444 const UniChar *chars = CFStringGetCharactersPtr(event->im_set_text.text);
1445 UniChar *buffer = NULL;
1447 if (!chars)
1449 buffer = HeapAlloc(GetProcessHeap(), 0, length * sizeof(*buffer));
1450 CFStringGetCharacters(event->im_set_text.text, CFRangeMake(0, length), buffer);
1451 chars = buffer;
1454 if (himc)
1455 IME_SetCompositionString(himc, SCS_SETSTR, chars, length * sizeof(*chars),
1456 event->im_set_text.cursor_pos, !event->im_set_text.complete);
1457 else
1459 INPUT input;
1460 CFIndex i;
1462 input.type = INPUT_KEYBOARD;
1463 input.ki.wVk = 0;
1464 input.ki.time = 0;
1465 input.ki.dwExtraInfo = 0;
1467 for (i = 0; i < length; i++)
1469 input.ki.wScan = chars[i];
1470 input.ki.dwFlags = KEYEVENTF_UNICODE;
1471 __wine_send_input(hwnd, &input);
1473 input.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
1474 __wine_send_input(hwnd, &input);
1478 HeapFree(GetProcessHeap(), 0, buffer);
1481 if (event->im_set_text.complete)
1482 IME_NotifyComplete(himc);
1486 /**************************************************************************
1487 * query_ime_char_rect
1489 BOOL query_ime_char_rect(macdrv_query* query)
1491 HWND hwnd = macdrv_get_window_hwnd(query->window);
1492 void *himc = query->ime_char_rect.data;
1493 CFRange* range = &query->ime_char_rect.range;
1494 CGRect* rect = &query->ime_char_rect.rect;
1495 IMECHARPOSITION charpos;
1496 BOOL ret = FALSE;
1498 TRACE("win %p/%p himc %p range %ld-%ld\n", hwnd, query->window, himc, range->location,
1499 range->length);
1501 if (!himc) himc = RealIMC(FROM_MACDRV);
1503 charpos.dwSize = sizeof(charpos);
1504 charpos.dwCharPos = range->location;
1505 if (ImmRequestMessageW(himc, IMR_QUERYCHARPOSITION, (ULONG_PTR)&charpos))
1507 int i;
1509 *rect = CGRectMake(charpos.pt.x, charpos.pt.y, 0, charpos.cLineHeight);
1511 /* iterate over rest of length to extend rect */
1512 for (i = 1; i < range->length; i++)
1514 charpos.dwSize = sizeof(charpos);
1515 charpos.dwCharPos = range->location + i;
1516 if (!ImmRequestMessageW(himc, IMR_QUERYCHARPOSITION, (ULONG_PTR)&charpos) ||
1517 charpos.pt.y != rect->origin.y)
1519 range->length = i;
1520 break;
1523 rect->size.width = charpos.pt.x - rect->origin.x;
1526 ret = TRUE;
1529 if (!ret)
1531 LPINPUTCONTEXT ic = ImmLockIMC(himc);
1533 if (ic)
1535 LPIMEPRIVATE private = ImmLockIMCC(ic->hPrivate);
1536 LPBYTE compdata = ImmLockIMCC(ic->hCompStr);
1537 LPCOMPOSITIONSTRING compstr = (LPCOMPOSITIONSTRING)compdata;
1538 LPWSTR str = (LPWSTR)(compdata + compstr->dwCompStrOffset);
1540 if (private->hwndDefault && compstr->dwCompStrOffset &&
1541 IsWindowVisible(private->hwndDefault))
1543 HDC dc = GetDC(private->hwndDefault);
1544 HFONT oldfont = NULL;
1545 SIZE size;
1547 if (private->textfont)
1548 oldfont = SelectObject(dc, private->textfont);
1550 if (range->location > compstr->dwCompStrLen)
1551 range->location = compstr->dwCompStrLen;
1552 if (range->location + range->length > compstr->dwCompStrLen)
1553 range->length = compstr->dwCompStrLen - range->location;
1555 GetTextExtentPoint32W(dc, str, range->location, &size);
1556 charpos.rcDocument.left = size.cx;
1557 charpos.rcDocument.top = 0;
1558 GetTextExtentPoint32W(dc, str, range->location + range->length, &size);
1559 charpos.rcDocument.right = size.cx;
1560 charpos.rcDocument.bottom = size.cy;
1562 if (ic->cfCompForm.dwStyle == CFS_DEFAULT)
1563 OffsetRect(&charpos.rcDocument, 10, 10);
1565 LPtoDP(dc, (POINT*)&charpos.rcDocument, 2);
1566 MapWindowPoints(private->hwndDefault, 0, (POINT*)&charpos.rcDocument, 2);
1567 *rect = cgrect_from_rect(charpos.rcDocument);
1568 ret = TRUE;
1570 if (oldfont)
1571 SelectObject(dc, oldfont);
1572 ReleaseDC(private->hwndDefault, dc);
1575 ImmUnlockIMCC(ic->hCompStr);
1576 ImmUnlockIMCC(ic->hPrivate);
1579 ImmUnlockIMC(himc);
1582 if (!ret)
1584 GUITHREADINFO gti;
1585 gti.cbSize = sizeof(gti);
1586 if (GetGUIThreadInfo(0, &gti))
1588 MapWindowPoints(gti.hwndCaret, 0, (POINT*)&gti.rcCaret, 2);
1589 *rect = cgrect_from_rect(gti.rcCaret);
1590 ret = TRUE;
1594 if (ret && range->length && !rect->size.width)
1595 rect->size.width = 1;
1597 TRACE(" -> %s range %ld-%ld rect %s\n", ret ? "TRUE" : "FALSE", range->location,
1598 range->length, wine_dbgstr_cgrect(*rect));
1600 return ret;