mpr: Implement WNetClearConnections().
[wine.git] / dlls / winemac.drv / ime.c
blob00fb236589caf9cf11130c1c116a993a01840631
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 BOOL GenerateMessageToTransKey(LPDWORD lpTransBuf, UINT *uNumTranMsgs,
463 UINT msg, WPARAM wParam, LPARAM lParam)
465 LPTRANSMSG ptr;
467 if (*uNumTranMsgs + 1 >= (UINT)*lpTransBuf)
468 return FALSE;
470 ptr = (LPTRANSMSG)(lpTransBuf + 1 + *uNumTranMsgs * 3);
471 ptr->message = msg;
472 ptr->wParam = wParam;
473 ptr->lParam = lParam;
474 (*uNumTranMsgs)++;
476 return TRUE;
480 static BOOL IME_RemoveFromSelected(HIMC hIMC)
482 int i;
483 for (i = 0; i < hSelectedCount; i++)
485 if (hSelectedFrom[i] == hIMC)
487 if (i < hSelectedCount - 1)
488 memmove(&hSelectedFrom[i], &hSelectedFrom[i + 1], (hSelectedCount - i - 1) * sizeof(HIMC));
489 hSelectedCount--;
490 return TRUE;
493 return FALSE;
496 static void IME_AddToSelected(HIMC hIMC)
498 hSelectedCount++;
499 if (hSelectedFrom)
500 hSelectedFrom = HeapReAlloc(GetProcessHeap(), 0, hSelectedFrom, hSelectedCount * sizeof(HIMC));
501 else
502 hSelectedFrom = HeapAlloc(GetProcessHeap(), 0, sizeof(HIMC));
503 hSelectedFrom[hSelectedCount - 1] = hIMC;
506 static void UpdateDataInDefaultIMEWindow(HIMC hIMC, HWND hwnd, BOOL showable)
508 LPCOMPOSITIONSTRING compstr;
509 LPINPUTCONTEXT lpIMC;
511 lpIMC = LockRealIMC(hIMC);
512 if (lpIMC == NULL)
513 return;
515 if (lpIMC->hCompStr)
516 compstr = ImmLockIMCC(lpIMC->hCompStr);
517 else
518 compstr = NULL;
520 if (compstr == NULL || compstr->dwCompStrLen == 0)
521 ShowWindow(hwnd, SW_HIDE);
522 else if (showable)
523 ShowWindow(hwnd, SW_SHOWNOACTIVATE);
525 RedrawWindow(hwnd, NULL, NULL, RDW_ERASENOW | RDW_INVALIDATE);
527 if (compstr != NULL)
528 ImmUnlockIMCC(lpIMC->hCompStr);
530 UnlockRealIMC(hIMC);
533 BOOL WINAPI ImeConfigure(HKL hKL, HWND hWnd, DWORD dwMode, LPVOID lpData)
535 FIXME("(%p, %p, %d, %p): stub\n", hKL, hWnd, dwMode, lpData);
536 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
537 return FALSE;
540 DWORD WINAPI ImeConversionList(HIMC hIMC, LPCWSTR lpSource, LPCANDIDATELIST lpCandList,
541 DWORD dwBufLen, UINT uFlag)
544 FIXME("(%p, %s, %p, %d, %d): stub\n", hIMC, debugstr_w(lpSource), lpCandList,
545 dwBufLen, uFlag);
546 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
547 return 0;
550 BOOL WINAPI ImeDestroy(UINT uForce)
552 TRACE("\n");
553 HeapFree(GetProcessHeap(), 0, hSelectedFrom);
554 hSelectedFrom = NULL;
555 hSelectedCount = 0;
556 return TRUE;
559 LRESULT WINAPI ImeEscape(HIMC hIMC, UINT uSubFunc, LPVOID lpData)
561 TRACE("%x %p\n", uSubFunc, lpData);
562 return 0;
565 BOOL WINAPI ImeProcessKey(HIMC hIMC, UINT vKey, LPARAM lKeyData, const LPBYTE lpbKeyState)
567 LPINPUTCONTEXT lpIMC;
568 BOOL inIME;
570 TRACE("hIMC %p vKey 0x%04x lKeyData 0x%08lx lpbKeyState %p\n", hIMC, vKey, lKeyData, lpbKeyState);
572 switch (vKey)
574 case VK_SHIFT:
575 case VK_CONTROL:
576 case VK_CAPITAL:
577 case VK_MENU:
578 return FALSE;
581 inIME = macdrv_using_input_method();
582 lpIMC = LockRealIMC(hIMC);
583 if (lpIMC)
585 LPIMEPRIVATE myPrivate;
586 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
588 if (inIME && !myPrivate->bInternalState)
589 ImmSetOpenStatus(RealIMC(FROM_MACDRV), TRUE);
590 else if (!inIME && myPrivate->bInternalState)
592 ShowWindow(myPrivate->hwndDefault, SW_HIDE);
593 ImmDestroyIMCC(lpIMC->hCompStr);
594 lpIMC->hCompStr = ImeCreateBlankCompStr();
595 ImmSetOpenStatus(RealIMC(FROM_MACDRV), FALSE);
598 myPrivate->repeat = (lKeyData >> 30) & 0x1;
600 myPrivate->bInternalState = inIME;
601 ImmUnlockIMCC(lpIMC->hPrivate);
603 UnlockRealIMC(hIMC);
605 return inIME;
608 BOOL WINAPI ImeSelect(HIMC hIMC, BOOL fSelect)
610 LPINPUTCONTEXT lpIMC;
611 TRACE("%p %s\n", hIMC, fSelect ? "TRUE" : "FALSE");
613 if (hIMC == FROM_MACDRV)
615 ERR("ImeSelect should never be called from Cocoa\n");
616 return FALSE;
619 if (!hIMC)
620 return TRUE;
622 /* not selected */
623 if (!fSelect)
624 return IME_RemoveFromSelected(hIMC);
626 IME_AddToSelected(hIMC);
628 /* Initialize our structures */
629 lpIMC = LockRealIMC(hIMC);
630 if (lpIMC != NULL)
632 LPIMEPRIVATE myPrivate;
633 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
634 myPrivate->bInComposition = FALSE;
635 myPrivate->bInternalState = FALSE;
636 myPrivate->textfont = NULL;
637 myPrivate->hwndDefault = NULL;
638 myPrivate->repeat = 0;
639 ImmUnlockIMCC(lpIMC->hPrivate);
640 UnlockRealIMC(hIMC);
643 return TRUE;
646 BOOL WINAPI ImeSetActiveContext(HIMC hIMC, BOOL fFlag)
648 FIXME("(%p, %x): stub\n", hIMC, fFlag);
649 return TRUE;
652 UINT WINAPI ImeToAsciiEx(UINT uVKey, UINT uScanCode, const LPBYTE lpbKeyState,
653 LPDWORD lpdwTransKey, UINT fuState, HIMC hIMC)
655 UINT vkey;
656 LPINPUTCONTEXT lpIMC;
657 LPIMEPRIVATE myPrivate;
658 HWND hwndDefault;
659 UINT repeat;
660 int done = 0;
662 TRACE("uVKey 0x%04x uScanCode 0x%04x fuState %u hIMC %p\n", uVKey, uScanCode, fuState, hIMC);
664 vkey = LOWORD(uVKey);
666 if (vkey == VK_KANA || vkey == VK_KANJI || vkey == VK_MENU)
668 TRACE("Skipping metakey\n");
669 return 0;
672 lpIMC = LockRealIMC(hIMC);
673 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
674 if (!myPrivate->bInternalState)
676 ImmUnlockIMCC(lpIMC->hPrivate);
677 UnlockRealIMC(hIMC);
678 return 0;
681 repeat = myPrivate->repeat;
682 hwndDefault = myPrivate->hwndDefault;
683 ImmUnlockIMCC(lpIMC->hPrivate);
684 UnlockRealIMC(hIMC);
686 TRACE("Processing Mac 0x%04x\n", vkey);
687 macdrv_process_text_input(uVKey, uScanCode, repeat, lpbKeyState, hIMC, &done);
689 while (!done)
690 MsgWaitForMultipleObjectsEx(0, NULL, INFINITE, QS_POSTMESSAGE | QS_SENDMESSAGE, 0);
692 if (done < 0)
694 UINT msgs = 0;
695 UINT msg = (uScanCode & 0x8000) ? WM_KEYUP : WM_KEYDOWN;
697 /* KeyStroke not processed by the IME
698 * so we need to rebuild the KeyDown message and pass it on to WINE
700 if (!GenerateMessageToTransKey(lpdwTransKey, &msgs, msg, vkey, MAKELONG(0x0001, uScanCode)))
701 GenerateIMEMessage(hIMC, msg, vkey, MAKELONG(0x0001, uScanCode));
703 return msgs;
705 else
706 UpdateDataInDefaultIMEWindow(hIMC, hwndDefault, FALSE);
707 return 0;
710 BOOL WINAPI NotifyIME(HIMC hIMC, DWORD dwAction, DWORD dwIndex, DWORD dwValue)
712 BOOL bRet = FALSE;
713 LPINPUTCONTEXT lpIMC;
715 TRACE("%p %i %i %i\n", hIMC, dwAction, dwIndex, dwValue);
717 lpIMC = LockRealIMC(hIMC);
718 if (lpIMC == NULL)
719 return FALSE;
721 switch (dwAction)
723 case NI_OPENCANDIDATE: FIXME("NI_OPENCANDIDATE\n"); break;
724 case NI_CLOSECANDIDATE: FIXME("NI_CLOSECANDIDATE\n"); break;
725 case NI_SELECTCANDIDATESTR: FIXME("NI_SELECTCANDIDATESTR\n"); break;
726 case NI_CHANGECANDIDATELIST: FIXME("NI_CHANGECANDIDATELIST\n"); break;
727 case NI_SETCANDIDATE_PAGESTART: FIXME("NI_SETCANDIDATE_PAGESTART\n"); break;
728 case NI_SETCANDIDATE_PAGESIZE: FIXME("NI_SETCANDIDATE_PAGESIZE\n"); break;
729 case NI_CONTEXTUPDATED:
730 switch (dwValue)
732 case IMC_SETCOMPOSITIONWINDOW: FIXME("NI_CONTEXTUPDATED: IMC_SETCOMPOSITIONWINDOW\n"); break;
733 case IMC_SETCONVERSIONMODE: FIXME("NI_CONTEXTUPDATED: IMC_SETCONVERSIONMODE\n"); break;
734 case IMC_SETSENTENCEMODE: FIXME("NI_CONTEXTUPDATED: IMC_SETSENTENCEMODE\n"); break;
735 case IMC_SETCANDIDATEPOS: FIXME("NI_CONTEXTUPDATED: IMC_SETCANDIDATEPOS\n"); break;
736 case IMC_SETCOMPOSITIONFONT:
738 LPIMEPRIVATE myPrivate;
739 TRACE("NI_CONTEXTUPDATED: IMC_SETCOMPOSITIONFONT\n");
741 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
742 if (myPrivate->textfont)
744 DeleteObject(myPrivate->textfont);
745 myPrivate->textfont = NULL;
747 myPrivate->textfont = CreateFontIndirectW(&lpIMC->lfFont.W);
748 ImmUnlockIMCC(lpIMC->hPrivate);
750 break;
751 case IMC_SETOPENSTATUS:
753 LPIMEPRIVATE myPrivate;
754 TRACE("NI_CONTEXTUPDATED: IMC_SETOPENSTATUS\n");
756 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
757 if (lpIMC->fOpen != myPrivate->bInternalState && myPrivate->bInComposition)
759 if(lpIMC->fOpen == FALSE)
761 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
762 myPrivate->bInComposition = FALSE;
764 else
766 GenerateIMEMessage(hIMC, WM_IME_STARTCOMPOSITION, 0, 0);
767 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, 0, 0);
770 myPrivate->bInternalState = lpIMC->fOpen;
771 bRet = TRUE;
773 break;
774 default: FIXME("NI_CONTEXTUPDATED: Unknown\n"); break;
776 break;
777 case NI_COMPOSITIONSTR:
778 switch (dwIndex)
780 case CPS_COMPLETE:
782 HIMCC newCompStr;
783 DWORD cplen = 0;
784 LPWSTR cpstr;
785 LPCOMPOSITIONSTRING cs = NULL;
786 LPBYTE cdata = NULL;
787 LPIMEPRIVATE myPrivate;
789 TRACE("NI_COMPOSITIONSTR: CPS_COMPLETE\n");
791 /* clear existing result */
792 newCompStr = updateResultStr(lpIMC->hCompStr, NULL, 0);
794 ImmDestroyIMCC(lpIMC->hCompStr);
795 lpIMC->hCompStr = newCompStr;
797 if (lpIMC->hCompStr)
799 cdata = ImmLockIMCC(lpIMC->hCompStr);
800 cs = (LPCOMPOSITIONSTRING)cdata;
801 cplen = cs->dwCompStrLen;
802 cpstr = (LPWSTR)&cdata[cs->dwCompStrOffset];
803 ImmUnlockIMCC(lpIMC->hCompStr);
805 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
806 if (cplen > 0)
808 WCHAR param = cpstr[0];
809 DWORD flags = GCS_COMPSTR;
811 newCompStr = updateResultStr(lpIMC->hCompStr, cpstr, cplen);
812 ImmDestroyIMCC(lpIMC->hCompStr);
813 lpIMC->hCompStr = newCompStr;
814 newCompStr = updateCompStr(lpIMC->hCompStr, NULL, 0, &flags);
815 ImmDestroyIMCC(lpIMC->hCompStr);
816 lpIMC->hCompStr = newCompStr;
818 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, 0, flags);
820 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, param,
821 GCS_RESULTSTR | GCS_RESULTCLAUSE);
823 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
825 else if (myPrivate->bInComposition)
826 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
829 myPrivate->bInComposition = FALSE;
830 ImmUnlockIMCC(lpIMC->hPrivate);
832 bRet = TRUE;
834 break;
835 case CPS_CONVERT: FIXME("NI_COMPOSITIONSTR: CPS_CONVERT\n"); break;
836 case CPS_REVERT: FIXME("NI_COMPOSITIONSTR: CPS_REVERT\n"); break;
837 case CPS_CANCEL:
839 LPIMEPRIVATE myPrivate;
841 TRACE("NI_COMPOSITIONSTR: CPS_CANCEL\n");
843 if (lpIMC->hCompStr)
844 ImmDestroyIMCC(lpIMC->hCompStr);
846 lpIMC->hCompStr = ImeCreateBlankCompStr();
848 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
849 if (myPrivate->bInComposition)
851 GenerateIMEMessage(hIMC, WM_IME_ENDCOMPOSITION, 0, 0);
852 myPrivate->bInComposition = FALSE;
854 ImmUnlockIMCC(lpIMC->hPrivate);
855 bRet = TRUE;
857 break;
858 default: FIXME("NI_COMPOSITIONSTR: Unknown\n"); break;
860 break;
861 default: FIXME("Unknown Message\n"); break;
864 UnlockRealIMC(hIMC);
865 return bRet;
868 BOOL WINAPI ImeRegisterWord(LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszRegister)
870 FIXME("(%s, %d, %s): stub\n", debugstr_w(lpszReading), dwStyle, debugstr_w(lpszRegister));
871 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
872 return FALSE;
875 BOOL WINAPI ImeUnregisterWord(LPCWSTR lpszReading, DWORD dwStyle, LPCWSTR lpszUnregister)
877 FIXME("(%s, %d, %s): stub\n", debugstr_w(lpszReading), dwStyle, debugstr_w(lpszUnregister));
878 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
879 return FALSE;
882 UINT WINAPI ImeGetRegisterWordStyle(UINT nItem, LPSTYLEBUFW lpStyleBuf)
884 FIXME("(%d, %p): stub\n", nItem, lpStyleBuf);
885 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
886 return 0;
889 UINT WINAPI ImeEnumRegisterWord(REGISTERWORDENUMPROCW lpfnEnumProc, LPCWSTR lpszReading,
890 DWORD dwStyle, LPCWSTR lpszRegister, LPVOID lpData)
892 FIXME("(%p, %s, %d, %s, %p): stub\n", lpfnEnumProc, debugstr_w(lpszReading), dwStyle,
893 debugstr_w(lpszRegister), lpData);
894 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
895 return 0;
898 static BOOL IME_SetCompositionString(void* hIMC, DWORD dwIndex, LPCVOID lpComp, DWORD dwCompLen, DWORD cursor_pos, BOOL cursor_valid)
900 LPINPUTCONTEXT lpIMC;
901 DWORD flags = 0;
902 WCHAR wParam = 0;
903 LPIMEPRIVATE myPrivate;
905 TRACE("(%p, %d, %p, %d):\n", hIMC, dwIndex, lpComp, dwCompLen);
908 * Explanation:
909 * this sets the composition string in the imm32.dll level
910 * of the composition buffer.
911 * TODO: set the Cocoa window's marked text string and tell text input context
914 lpIMC = LockRealIMC(hIMC);
916 if (lpIMC == NULL)
917 return FALSE;
919 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
921 if (dwIndex == SCS_SETSTR)
923 HIMCC newCompStr;
925 if (!myPrivate->bInComposition)
927 GenerateIMEMessage(hIMC, WM_IME_STARTCOMPOSITION, 0, 0);
928 myPrivate->bInComposition = TRUE;
931 /* clear existing result */
932 newCompStr = updateResultStr(lpIMC->hCompStr, NULL, 0);
933 ImmDestroyIMCC(lpIMC->hCompStr);
934 lpIMC->hCompStr = newCompStr;
936 flags = GCS_COMPSTR;
938 if (dwCompLen && lpComp)
940 newCompStr = updateCompStr(lpIMC->hCompStr, (LPCWSTR)lpComp, dwCompLen / sizeof(WCHAR), &flags);
941 ImmDestroyIMCC(lpIMC->hCompStr);
942 lpIMC->hCompStr = newCompStr;
944 wParam = ((const WCHAR*)lpComp)[0];
945 flags |= GCS_COMPCLAUSE | GCS_COMPATTR | GCS_DELTASTART;
947 else
949 newCompStr = updateCompStr(lpIMC->hCompStr, NULL, 0, &flags);
950 ImmDestroyIMCC(lpIMC->hCompStr);
951 lpIMC->hCompStr = newCompStr;
954 if (cursor_valid)
956 LPCOMPOSITIONSTRING compstr;
957 compstr = ImmLockIMCC(lpIMC->hCompStr);
958 compstr->dwCursorPos = cursor_pos;
959 ImmUnlockIMCC(lpIMC->hCompStr);
960 flags |= GCS_CURSORPOS;
965 GenerateIMEMessage(hIMC, WM_IME_COMPOSITION, wParam, flags);
966 ImmUnlockIMCC(lpIMC->hPrivate);
967 UnlockRealIMC(hIMC);
969 return TRUE;
972 BOOL WINAPI ImeSetCompositionString(HIMC hIMC, DWORD dwIndex, LPCVOID lpComp, DWORD dwCompLen,
973 LPCVOID lpRead, DWORD dwReadLen)
975 TRACE("(%p, %d, %p, %d, %p, %d):\n", hIMC, dwIndex, lpComp, dwCompLen, lpRead, dwReadLen);
977 if (lpRead && dwReadLen)
978 FIXME("Reading string unimplemented\n");
980 return IME_SetCompositionString(hIMC, dwIndex, lpComp, dwCompLen, 0, FALSE);
983 DWORD WINAPI ImeGetImeMenuItems(HIMC hIMC, DWORD dwFlags, DWORD dwType, LPIMEMENUITEMINFOW lpImeParentMenu,
984 LPIMEMENUITEMINFOW lpImeMenu, DWORD dwSize)
986 FIXME("(%p, %x %x %p %p %x): stub\n", hIMC, dwFlags, dwType, lpImeParentMenu, lpImeMenu, dwSize);
987 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
988 return 0;
991 static void IME_NotifyComplete(void* hIMC)
993 NotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
996 /*****
997 * Internal functions to help with IME window management
999 static void PaintDefaultIMEWnd(HIMC hIMC, HWND hwnd)
1001 PAINTSTRUCT ps;
1002 RECT rect;
1003 HDC hdc;
1004 LPCOMPOSITIONSTRING compstr;
1005 LPBYTE compdata = NULL;
1006 HMONITOR monitor;
1007 MONITORINFO mon_info;
1008 INT offX = 0, offY = 0;
1009 LPINPUTCONTEXT lpIMC;
1011 lpIMC = LockRealIMC(hIMC);
1012 if (lpIMC == NULL)
1013 return;
1015 hdc = BeginPaint(hwnd, &ps);
1017 GetClientRect(hwnd, &rect);
1018 FillRect(hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
1020 compdata = ImmLockIMCC(lpIMC->hCompStr);
1021 compstr = (LPCOMPOSITIONSTRING)compdata;
1023 if (compstr->dwCompStrLen && compstr->dwCompStrOffset)
1025 SIZE size;
1026 POINT pt;
1027 HFONT oldfont = NULL;
1028 LPWSTR CompString;
1029 LPIMEPRIVATE myPrivate;
1031 CompString = (LPWSTR)(compdata + compstr->dwCompStrOffset);
1032 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
1034 if (myPrivate->textfont)
1035 oldfont = SelectObject(hdc, myPrivate->textfont);
1037 ImmUnlockIMCC(lpIMC->hPrivate);
1039 GetTextExtentPoint32W(hdc, CompString, compstr->dwCompStrLen, &size);
1040 pt.x = size.cx;
1041 pt.y = size.cy;
1042 LPtoDP(hdc, &pt, 1);
1045 * How this works based on tests on windows:
1046 * CFS_POINT: then we start our window at the point and grow it as large
1047 * as it needs to be for the string.
1048 * CFS_RECT: we still use the ptCurrentPos as a starting point and our
1049 * window is only as large as we need for the string, but we do not
1050 * grow such that our window exceeds the given rect. Wrapping if
1051 * needed and possible. If our ptCurrentPos is outside of our rect
1052 * then no window is displayed.
1053 * CFS_FORCE_POSITION: appears to behave just like CFS_POINT
1054 * maybe because the default MSIME does not do any IME adjusting.
1056 if (lpIMC->cfCompForm.dwStyle != CFS_DEFAULT)
1058 POINT cpt = lpIMC->cfCompForm.ptCurrentPos;
1059 ClientToScreen(lpIMC->hWnd, &cpt);
1060 rect.left = cpt.x;
1061 rect.top = cpt.y;
1062 rect.right = rect.left + pt.x;
1063 rect.bottom = rect.top + pt.y;
1064 monitor = MonitorFromPoint(cpt, MONITOR_DEFAULTTOPRIMARY);
1066 else /* CFS_DEFAULT */
1068 /* Windows places the default IME window in the bottom left */
1069 HWND target = lpIMC->hWnd;
1070 if (!target) target = GetFocus();
1072 GetWindowRect(target, &rect);
1073 rect.top = rect.bottom;
1074 rect.right = rect.left + pt.x + 20;
1075 rect.bottom = rect.top + pt.y + 20;
1076 offX=offY=10;
1077 monitor = MonitorFromWindow(target, MONITOR_DEFAULTTOPRIMARY);
1080 if (lpIMC->cfCompForm.dwStyle == CFS_RECT)
1082 RECT client;
1083 client =lpIMC->cfCompForm.rcArea;
1084 MapWindowPoints(lpIMC->hWnd, 0, (POINT *)&client, 2);
1085 IntersectRect(&rect, &rect, &client);
1086 /* TODO: Wrap the input if needed */
1089 if (lpIMC->cfCompForm.dwStyle == CFS_DEFAULT)
1091 /* make sure we are on the desktop */
1092 mon_info.cbSize = sizeof(mon_info);
1093 GetMonitorInfoW(monitor, &mon_info);
1095 if (rect.bottom > mon_info.rcWork.bottom)
1097 int shift = rect.bottom - mon_info.rcWork.bottom;
1098 rect.top -= shift;
1099 rect.bottom -= shift;
1101 if (rect.left < 0)
1103 rect.right -= rect.left;
1104 rect.left = 0;
1106 if (rect.right > mon_info.rcWork.right)
1108 int shift = rect.right - mon_info.rcWork.right;
1109 rect.left -= shift;
1110 rect.right -= shift;
1114 SetWindowPos(hwnd, HWND_TOPMOST, rect.left, rect.top, rect.right - rect.left,
1115 rect.bottom - rect.top, SWP_NOACTIVATE);
1117 TextOutW(hdc, offX, offY, CompString, compstr->dwCompStrLen);
1119 if (oldfont)
1120 SelectObject(hdc, oldfont);
1123 ImmUnlockIMCC(lpIMC->hCompStr);
1125 EndPaint(hwnd, &ps);
1126 UnlockRealIMC(hIMC);
1129 static void DefaultIMEComposition(HIMC hIMC, HWND hwnd, LPARAM lParam)
1131 TRACE("IME message WM_IME_COMPOSITION 0x%lx\n", lParam);
1132 if (!(lParam & GCS_RESULTSTR))
1133 UpdateDataInDefaultIMEWindow(hIMC, hwnd, TRUE);
1136 static void DefaultIMEStartComposition(HIMC hIMC, HWND hwnd)
1138 LPINPUTCONTEXT lpIMC;
1140 lpIMC = LockRealIMC(hIMC);
1141 if (lpIMC == NULL)
1142 return;
1144 TRACE("IME message WM_IME_STARTCOMPOSITION\n");
1145 lpIMC->hWnd = GetFocus();
1146 ShowWindow(hwnd, SW_SHOWNOACTIVATE);
1147 UnlockRealIMC(hIMC);
1150 static LRESULT ImeHandleNotify(HIMC hIMC, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1152 switch (wParam)
1154 case IMN_OPENSTATUSWINDOW:
1155 FIXME("WM_IME_NOTIFY:IMN_OPENSTATUSWINDOW\n");
1156 break;
1157 case IMN_CLOSESTATUSWINDOW:
1158 FIXME("WM_IME_NOTIFY:IMN_CLOSESTATUSWINDOW\n");
1159 break;
1160 case IMN_OPENCANDIDATE:
1161 FIXME("WM_IME_NOTIFY:IMN_OPENCANDIDATE\n");
1162 break;
1163 case IMN_CHANGECANDIDATE:
1164 FIXME("WM_IME_NOTIFY:IMN_CHANGECANDIDATE\n");
1165 break;
1166 case IMN_CLOSECANDIDATE:
1167 FIXME("WM_IME_NOTIFY:IMN_CLOSECANDIDATE\n");
1168 break;
1169 case IMN_SETCONVERSIONMODE:
1170 FIXME("WM_IME_NOTIFY:IMN_SETCONVERSIONMODE\n");
1171 break;
1172 case IMN_SETSENTENCEMODE:
1173 FIXME("WM_IME_NOTIFY:IMN_SETSENTENCEMODE\n");
1174 break;
1175 case IMN_SETOPENSTATUS:
1176 FIXME("WM_IME_NOTIFY:IMN_SETOPENSTATUS\n");
1177 break;
1178 case IMN_SETCANDIDATEPOS:
1179 FIXME("WM_IME_NOTIFY:IMN_SETCANDIDATEPOS\n");
1180 break;
1181 case IMN_SETCOMPOSITIONFONT:
1182 FIXME("WM_IME_NOTIFY:IMN_SETCOMPOSITIONFONT\n");
1183 break;
1184 case IMN_SETCOMPOSITIONWINDOW:
1185 FIXME("WM_IME_NOTIFY:IMN_SETCOMPOSITIONWINDOW\n");
1186 break;
1187 case IMN_GUIDELINE:
1188 FIXME("WM_IME_NOTIFY:IMN_GUIDELINE\n");
1189 break;
1190 case IMN_SETSTATUSWINDOWPOS:
1191 FIXME("WM_IME_NOTIFY:IMN_SETSTATUSWINDOWPOS\n");
1192 break;
1193 default:
1194 FIXME("WM_IME_NOTIFY:<Unknown 0x%lx>\n", wParam);
1195 break;
1197 return 0;
1200 static LRESULT WINAPI IME_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1202 LRESULT rc = 0;
1203 HIMC hIMC;
1205 TRACE("Incoming Message 0x%x (0x%08lx, 0x%08lx)\n", msg, wParam, lParam);
1208 * Each UI window contains the current Input Context.
1209 * This Input Context can be obtained by calling GetWindowLong
1210 * with IMMGWL_IMC when the UI window receives a WM_IME_xxx message.
1211 * The UI window can refer to this Input Context and handles the
1212 * messages.
1215 hIMC = (HIMC)GetWindowLongPtrW(hwnd, IMMGWL_IMC);
1216 if (!hIMC)
1217 hIMC = RealIMC(FROM_MACDRV);
1219 /* if we have no hIMC there are many messages we cannot process */
1220 if (hIMC == NULL)
1222 switch (msg) {
1223 case WM_IME_STARTCOMPOSITION:
1224 case WM_IME_ENDCOMPOSITION:
1225 case WM_IME_COMPOSITION:
1226 case WM_IME_NOTIFY:
1227 case WM_IME_CONTROL:
1228 case WM_IME_COMPOSITIONFULL:
1229 case WM_IME_SELECT:
1230 case WM_IME_CHAR:
1231 return 0L;
1232 default:
1233 break;
1237 switch (msg)
1239 case WM_CREATE:
1241 LPIMEPRIVATE myPrivate;
1242 LPINPUTCONTEXT lpIMC;
1244 SetWindowTextA(hwnd, "Wine Ime Active");
1246 lpIMC = LockRealIMC(hIMC);
1247 if (lpIMC)
1249 myPrivate = ImmLockIMCC(lpIMC->hPrivate);
1250 myPrivate->hwndDefault = hwnd;
1251 ImmUnlockIMCC(lpIMC->hPrivate);
1253 UnlockRealIMC(hIMC);
1255 return TRUE;
1257 case WM_PAINT:
1258 PaintDefaultIMEWnd(hIMC, hwnd);
1259 return FALSE;
1261 case WM_NCCREATE:
1262 return TRUE;
1264 case WM_SETFOCUS:
1265 if (wParam)
1266 SetFocus((HWND)wParam);
1267 else
1268 FIXME("Received focus, should never have focus\n");
1269 break;
1270 case WM_IME_COMPOSITION:
1271 DefaultIMEComposition(hIMC, hwnd, lParam);
1272 break;
1273 case WM_IME_STARTCOMPOSITION:
1274 DefaultIMEStartComposition(hIMC, hwnd);
1275 break;
1276 case WM_IME_ENDCOMPOSITION:
1277 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_IME_ENDCOMPOSITION", wParam, lParam);
1278 ShowWindow(hwnd, SW_HIDE);
1279 break;
1280 case WM_IME_SELECT:
1281 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_IME_SELECT", wParam, lParam);
1282 break;
1283 case WM_IME_CONTROL:
1284 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_IME_CONTROL", wParam, lParam);
1285 rc = 1;
1286 break;
1287 case WM_IME_NOTIFY:
1288 rc = ImeHandleNotify(hIMC, hwnd, msg, wParam, lParam);
1289 break;
1290 default:
1291 TRACE("Non-standard message 0x%x\n", msg);
1293 /* check the MSIME messages */
1294 if (msg == WM_MSIME_SERVICE)
1296 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_SERVICE", wParam, lParam);
1297 rc = FALSE;
1299 else if (msg == WM_MSIME_RECONVERTOPTIONS)
1301 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_RECONVERTOPTIONS", wParam, lParam);
1303 else if (msg == WM_MSIME_MOUSE)
1305 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_MOUSE", wParam, lParam);
1307 else if (msg == WM_MSIME_RECONVERTREQUEST)
1309 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_RECONVERTREQUEST", wParam, lParam);
1311 else if (msg == WM_MSIME_RECONVERT)
1313 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_RECONVERT", wParam, lParam);
1315 else if (msg == WM_MSIME_QUERYPOSITION)
1317 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_QUERYPOSITION", wParam, lParam);
1319 else if (msg == WM_MSIME_DOCUMENTFEED)
1321 TRACE("IME message %s, 0x%lx, 0x%lx\n", "WM_MSIME_DOCUMENTFEED", wParam, lParam);
1323 /* DefWndProc if not an IME message */
1324 if (!rc && !((msg >= WM_IME_STARTCOMPOSITION && msg <= WM_IME_KEYLAST) ||
1325 (msg >= WM_IME_SETCONTEXT && msg <= WM_IME_KEYUP)))
1326 rc = DefWindowProcW(hwnd, msg, wParam, lParam);
1328 return rc;
1331 static BOOL WINAPI register_classes( INIT_ONCE *once, void *param, void **context )
1333 WNDCLASSW wndClass;
1334 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
1335 wndClass.style = CS_GLOBALCLASS | CS_IME | CS_HREDRAW | CS_VREDRAW;
1336 wndClass.lpfnWndProc = (WNDPROC) IME_WindowProc;
1337 wndClass.cbClsExtra = 0;
1338 wndClass.cbWndExtra = 2 * sizeof(LONG_PTR);
1339 wndClass.hInstance = macdrv_module;
1340 wndClass.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
1341 wndClass.hIcon = LoadIconW(NULL, (LPWSTR)IDI_APPLICATION);
1342 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1343 wndClass.lpszMenuName = 0;
1344 wndClass.lpszClassName = UI_CLASS_NAME;
1346 RegisterClassW(&wndClass);
1348 WM_MSIME_SERVICE = RegisterWindowMessageA("MSIMEService");
1349 WM_MSIME_RECONVERTOPTIONS = RegisterWindowMessageA("MSIMEReconvertOptions");
1350 WM_MSIME_MOUSE = RegisterWindowMessageA("MSIMEMouseOperation");
1351 WM_MSIME_RECONVERTREQUEST = RegisterWindowMessageA("MSIMEReconvertRequest");
1352 WM_MSIME_RECONVERT = RegisterWindowMessageA("MSIMEReconvert");
1353 WM_MSIME_QUERYPOSITION = RegisterWindowMessageA("MSIMEQueryPosition");
1354 WM_MSIME_DOCUMENTFEED = RegisterWindowMessageA("MSIMEDocumentFeed");
1355 return TRUE;
1358 BOOL WINAPI ImeInquire(LPIMEINFO lpIMEInfo, LPWSTR lpszUIClass, LPCWSTR lpszOption)
1360 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
1362 TRACE("\n");
1363 InitOnceExecuteOnce( &init_once, register_classes, NULL, NULL );
1364 lpIMEInfo->dwPrivateDataSize = sizeof(IMEPRIVATE);
1365 lpIMEInfo->fdwProperty = IME_PROP_UNICODE | IME_PROP_AT_CARET;
1366 lpIMEInfo->fdwConversionCaps = IME_CMODE_NATIVE | IME_CMODE_FULLSHAPE;
1367 lpIMEInfo->fdwSentenceCaps = IME_SMODE_AUTOMATIC;
1368 lpIMEInfo->fdwUICaps = UI_CAP_2700;
1369 /* Tell App we cannot accept ImeSetCompositionString calls */
1370 /* FIXME: Can we? */
1371 lpIMEInfo->fdwSCSCaps = 0;
1372 lpIMEInfo->fdwSelectCaps = SELECT_CAP_CONVERSION;
1374 lstrcpyW(lpszUIClass, UI_CLASS_NAME);
1376 return TRUE;
1379 /* Interfaces to other parts of the Mac driver */
1381 /***********************************************************************
1382 * macdrv_im_set_text
1384 void macdrv_im_set_text(const macdrv_event *event)
1386 HWND hwnd = macdrv_get_window_hwnd(event->window);
1387 void *himc = event->im_set_text.data;
1389 TRACE("win %p/%p himc %p text %s complete %u\n", hwnd, event->window, himc,
1390 debugstr_cf(event->im_set_text.text), event->im_set_text.complete);
1392 if (!himc) himc = RealIMC(FROM_MACDRV);
1394 if (event->im_set_text.text)
1396 CFIndex length = CFStringGetLength(event->im_set_text.text);
1397 const UniChar *chars = CFStringGetCharactersPtr(event->im_set_text.text);
1398 UniChar *buffer = NULL;
1400 if (!chars)
1402 buffer = HeapAlloc(GetProcessHeap(), 0, length * sizeof(*buffer));
1403 CFStringGetCharacters(event->im_set_text.text, CFRangeMake(0, length), buffer);
1404 chars = buffer;
1407 if (himc)
1408 IME_SetCompositionString(himc, SCS_SETSTR, chars, length * sizeof(*chars),
1409 event->im_set_text.cursor_pos, !event->im_set_text.complete);
1410 else
1412 INPUT input;
1413 CFIndex i;
1415 input.type = INPUT_KEYBOARD;
1416 input.ki.wVk = 0;
1417 input.ki.time = 0;
1418 input.ki.dwExtraInfo = 0;
1420 for (i = 0; i < length; i++)
1422 input.ki.wScan = chars[i];
1423 input.ki.dwFlags = KEYEVENTF_UNICODE;
1424 __wine_send_input(hwnd, &input);
1426 input.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
1427 __wine_send_input(hwnd, &input);
1431 HeapFree(GetProcessHeap(), 0, buffer);
1434 if (event->im_set_text.complete)
1435 IME_NotifyComplete(himc);
1438 /***********************************************************************
1439 * macdrv_sent_text_input
1441 void macdrv_sent_text_input(const macdrv_event *event)
1443 TRACE("handled: %s\n", event->sent_text_input.handled ? "TRUE" : "FALSE");
1444 *event->sent_text_input.done = event->sent_text_input.handled ? 1 : -1;
1448 /**************************************************************************
1449 * query_ime_char_rect
1451 BOOL query_ime_char_rect(macdrv_query* query)
1453 HWND hwnd = macdrv_get_window_hwnd(query->window);
1454 void *himc = query->ime_char_rect.data;
1455 CFRange* range = &query->ime_char_rect.range;
1456 CGRect* rect = &query->ime_char_rect.rect;
1457 IMECHARPOSITION charpos;
1458 BOOL ret = FALSE;
1460 TRACE("win %p/%p himc %p range %ld-%ld\n", hwnd, query->window, himc, range->location,
1461 range->length);
1463 if (!himc) himc = RealIMC(FROM_MACDRV);
1465 charpos.dwSize = sizeof(charpos);
1466 charpos.dwCharPos = range->location;
1467 if (ImmRequestMessageW(himc, IMR_QUERYCHARPOSITION, (ULONG_PTR)&charpos))
1469 int i;
1471 *rect = CGRectMake(charpos.pt.x, charpos.pt.y, 0, charpos.cLineHeight);
1473 /* iterate over rest of length to extend rect */
1474 for (i = 1; i < range->length; i++)
1476 charpos.dwSize = sizeof(charpos);
1477 charpos.dwCharPos = range->location + i;
1478 if (!ImmRequestMessageW(himc, IMR_QUERYCHARPOSITION, (ULONG_PTR)&charpos) ||
1479 charpos.pt.y != rect->origin.y)
1481 range->length = i;
1482 break;
1485 rect->size.width = charpos.pt.x - rect->origin.x;
1488 ret = TRUE;
1491 if (!ret)
1493 LPINPUTCONTEXT ic = ImmLockIMC(himc);
1495 if (ic)
1497 LPIMEPRIVATE private = ImmLockIMCC(ic->hPrivate);
1498 LPBYTE compdata = ImmLockIMCC(ic->hCompStr);
1499 LPCOMPOSITIONSTRING compstr = (LPCOMPOSITIONSTRING)compdata;
1500 LPWSTR str = (LPWSTR)(compdata + compstr->dwCompStrOffset);
1502 if (private->hwndDefault && compstr->dwCompStrOffset &&
1503 IsWindowVisible(private->hwndDefault))
1505 HDC dc = GetDC(private->hwndDefault);
1506 HFONT oldfont = NULL;
1507 SIZE size;
1509 if (private->textfont)
1510 oldfont = SelectObject(dc, private->textfont);
1512 if (range->location > compstr->dwCompStrLen)
1513 range->location = compstr->dwCompStrLen;
1514 if (range->location + range->length > compstr->dwCompStrLen)
1515 range->length = compstr->dwCompStrLen - range->location;
1517 GetTextExtentPoint32W(dc, str, range->location, &size);
1518 charpos.rcDocument.left = size.cx;
1519 charpos.rcDocument.top = 0;
1520 GetTextExtentPoint32W(dc, str, range->location + range->length, &size);
1521 charpos.rcDocument.right = size.cx;
1522 charpos.rcDocument.bottom = size.cy;
1524 if (ic->cfCompForm.dwStyle == CFS_DEFAULT)
1525 OffsetRect(&charpos.rcDocument, 10, 10);
1527 LPtoDP(dc, (POINT*)&charpos.rcDocument, 2);
1528 MapWindowPoints(private->hwndDefault, 0, (POINT*)&charpos.rcDocument, 2);
1529 *rect = cgrect_from_rect(charpos.rcDocument);
1530 ret = TRUE;
1532 if (oldfont)
1533 SelectObject(dc, oldfont);
1534 ReleaseDC(private->hwndDefault, dc);
1537 ImmUnlockIMCC(ic->hCompStr);
1538 ImmUnlockIMCC(ic->hPrivate);
1541 ImmUnlockIMC(himc);
1544 if (!ret)
1546 GUITHREADINFO gti;
1547 gti.cbSize = sizeof(gti);
1548 if (GetGUIThreadInfo(0, &gti))
1550 MapWindowPoints(gti.hwndCaret, 0, (POINT*)&gti.rcCaret, 2);
1551 *rect = cgrect_from_rect(gti.rcCaret);
1552 ret = TRUE;
1556 if (ret && range->length && !rect->size.width)
1557 rect->size.width = 1;
1559 TRACE(" -> %s range %ld-%ld rect %s\n", ret ? "TRUE" : "FALSE", range->location,
1560 range->length, wine_dbgstr_cgrect(*rect));
1562 return ret;