2 * Functions for further XIM control
4 * Copyright 2003 CodeWeavers, Aric Stewart
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
37 /* this must match with imm32/imm.c */
38 #define FROM_IME 0xcafe1337
40 BOOL ximInComposeMode=FALSE;
42 static HIMC root_context;
43 static XIMStyle ximStyle = 0;
44 static XIMStyle ximStyleRoot = 0;
46 /* moved here from imm32 for dll separation */
47 static DWORD dwCompStringLength = 0;
48 static LPBYTE CompositionString = NULL;
49 static DWORD dwCompStringSize = 0;
50 static LPBYTE ResultString = NULL;
51 static DWORD dwResultStringSize = 0;
53 static HMODULE hImmDll = NULL;
54 static HIMC (WINAPI *pImmAssociateContext)(HWND,HIMC);
55 static HIMC (WINAPI *pImmCreateContext)(void);
56 static VOID (WINAPI *pImmSetOpenStatus)(HIMC,BOOL);
57 static BOOL (WINAPI *pImmSetCompositionString)(HIMC, DWORD, LPWSTR,
58 DWORD, LPWSTR, DWORD);
59 static VOID (WINAPI *pImmNotifyIME)(HIMC, DWORD, DWORD, DWORD);
61 /* WINE specific messages from the xim in x11drv level */
63 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
64 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
65 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
66 /* this uses all the callbacks to utilize full IME support */
67 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
68 /* inorder to enable deadkey support */
69 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
72 * here are the functions that sort of marshall calls into IMM32.DLL
74 static void LoadImmDll(void)
76 hImmDll = LoadLibraryA("imm32.dll");
78 pImmAssociateContext = (void *)GetProcAddress(hImmDll, "ImmAssociateContext");
79 if (!pImmAssociateContext)
80 WARN("IMM: pImmAssociateContext not found in DLL\n");
82 pImmCreateContext = (void *)GetProcAddress(hImmDll, "ImmCreateContext");
83 if (!pImmCreateContext)
84 WARN("IMM: pImmCreateContext not found in DLL\n");
86 pImmSetOpenStatus = (void *)GetProcAddress( hImmDll, "ImmSetOpenStatus");
87 if (!pImmSetOpenStatus)
88 WARN("IMM: pImmSetOpenStatus not found in DLL\n");
90 pImmSetCompositionString =(void *)GetProcAddress(hImmDll, "ImmSetCompositionStringW");
92 if (!pImmSetCompositionString)
93 WARN("IMM: pImmSetCompositionStringW not found in DLL\n");
95 pImmNotifyIME = (void *)GetProcAddress( hImmDll, "ImmNotifyIME");
98 WARN("IMM: pImmNotifyIME not found in DLL\n");
101 static BOOL X11DRV_ImmSetInternalString(DWORD dwIndex, DWORD dwOffset,
102 DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
104 /* Composition strings are edited in chunks */
105 unsigned int byte_length = dwCompLen * sizeof(WCHAR);
106 unsigned int byte_offset = dwOffset * sizeof(WCHAR);
107 unsigned int byte_selection = selLength * sizeof(WCHAR);
110 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset, selLength, dwIndex, lpComp, dwCompLen );
112 if (dwIndex == GCS_COMPSTR)
118 if ((dwCompLen == 0) && (selLength == 0))
122 /* deletion occurred */
123 else if ((dwCompLen== 0) && (selLength != 0))
125 if (dwCompStringLength)
127 for (i = 0; i < byte_selection; i++)
129 if (byte_offset+byte_selection+i <
132 CompositionString[byte_offset + i] =
133 CompositionString[byte_offset + byte_selection + i];
136 CompositionString[byte_offset + i] = 0;
138 /* clean up the end */
139 dwCompStringLength -= byte_selection;
141 i = dwCompStringLength;
142 while (i < dwCompStringSize)
144 CompositionString[i++] = 0;
150 int byte_expansion = byte_length - byte_selection;
152 if (byte_expansion + dwCompStringLength >= dwCompStringSize)
154 if (CompositionString)
156 HeapReAlloc(GetProcessHeap(), 0,
162 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
165 memset(&(CompositionString[dwCompStringSize]), 0,
168 dwCompStringSize += byte_expansion;
171 ptr_new = ((LPBYTE)lpComp);
172 ptr_old = CompositionString + byte_offset + byte_selection;
174 dwCompStringLength += byte_expansion;
176 for (j=0,i = byte_offset; i < dwCompStringSize; i++)
180 CompositionString[i] = ptr_new[j++];
184 if (ptr_old < CompositionString + dwCompStringSize)
186 CompositionString[i] = *ptr_old;
190 CompositionString[i] = 0;
195 if (pImmSetCompositionString)
196 rc = pImmSetCompositionString((HIMC)FROM_IME, SCS_SETSTR,
197 (LPWSTR)CompositionString, dwCompStringLength,
200 else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
202 if (dwResultStringSize)
203 HeapFree(GetProcessHeap(),0,ResultString);
204 dwResultStringSize= byte_length;
205 ResultString= HeapAlloc(GetProcessHeap(),0,byte_length);
206 memcpy(ResultString,lpComp,byte_length);
208 if (pImmSetCompositionString)
209 rc = pImmSetCompositionString((HIMC)FROM_IME, SCS_SETSTR,
210 (LPWSTR)ResultString, dwResultStringSize,
214 pImmNotifyIME((HIMC)FROM_IME, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
220 void X11DRV_XIMLookupChars( const char *str, DWORD count )
226 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, sizeof(wcOutput)/sizeof(WCHAR));
228 if (pImmAssociateContext && (focus = GetFocus()))
229 pImmAssociateContext(focus,root_context);
231 X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
234 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
238 if (dwCompStringSize)
239 HeapFree(GetProcessHeap(),0,CompositionString);
241 dwCompStringSize = 0;
242 dwCompStringLength = 0;
243 CompositionString = NULL;
245 if (dwResultStringSize)
246 HeapFree(GetProcessHeap(),0,ResultString);
248 dwResultStringSize = 0;
252 if (pImmSetOpenStatus)
253 pImmSetOpenStatus((HIMC)FROM_IME,fOpen);
256 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
258 TRACE("PreEditStartCallback %p\n",ic);
259 X11DRV_ImmSetOpenStatus(TRUE);
260 ximInComposeMode = TRUE;
264 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
266 TRACE("PreeditDoneCallback %p\n",ic);
267 ximInComposeMode = FALSE;
268 X11DRV_ImmSetOpenStatus(FALSE);
271 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
272 XIMPreeditDrawCallbackStruct *P_DR)
277 TRACE("PreEditDrawCallback %p\n",ic);
281 int sel = P_DR->chg_first;
282 int len = P_DR->chg_length;
285 if (! P_DR->text->encoding_is_wchar)
287 TRACE("multibyte\n");
288 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
289 P_DR->text->string.multi_byte, -1,
294 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
298 FIXME("wchar PROBIBILY WRONG\n");
299 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
300 (LPWSTR)P_DR->text->string.wide_char,
305 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
310 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
311 XIMPreeditCaretCallbackStruct *P_C)
313 FIXME("PreeditCaretCalback %p\n",ic);
316 void X11DRV_ForceXIMReset(HWND hwnd)
318 XIC ic = X11DRV_get_ic(hwnd);
322 TRACE("Forcing Reset %p\n",ic);
324 leftover = XmbResetIC(ic);
330 /***********************************************************************
331 * X11DRV Ime creation
333 XIM X11DRV_SetupXIM(Display *display, const char *input_style)
335 XIMStyle ximStyleRequest, ximStyleCallback, ximStyleNone;
336 XIMStyles *ximStyles = NULL;
340 ximStyleRequest = STYLE_CALLBACK;
342 if (!strcasecmp(input_style, "offthespot"))
343 ximStyleRequest = STYLE_OFFTHESPOT;
344 else if (!strcasecmp(input_style, "overthespot"))
345 ximStyleRequest = STYLE_OVERTHESPOT;
346 else if (!strcasecmp(input_style, "root"))
347 ximStyleRequest = STYLE_ROOT;
351 if(!XSupportsLocale())
353 WARN("X does not support locale.\n");
356 if(XSetLocaleModifiers("") == NULL)
358 WARN("Could not set locale modifiers.\n");
362 xim = XOpenIM(display, NULL, NULL, NULL);
365 WARN("Could not open input method.\n");
369 TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
370 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
372 XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
375 WARN("Could not find supported input style.\n");
379 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
383 ximStyleCallback = 0;
385 for (i = 0; i < ximStyles->count_styles; ++i)
387 int style = ximStyles->supported_styles[i];
388 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
389 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
390 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
391 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
392 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
393 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
394 if (!ximStyle && (ximStyles->supported_styles[i] ==
397 ximStyle = ximStyleRequest;
398 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
400 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
403 ximStyleRoot = STYLE_ROOT;
404 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
406 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
409 ximStyleCallback = STYLE_CALLBACK;
410 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
412 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
415 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
416 ximStyleNone = STYLE_NONE;
422 ximStyle = ximStyleRoot;
425 ximStyle = ximStyleNone;
427 if (ximStyleCallback == 0)
429 TRACE("No callback style avalable\n");
430 ximStyleCallback = ximStyle;
439 if (pImmCreateContext)
441 root_context = pImmCreateContext();
442 if (pImmAssociateContext)
443 pImmAssociateContext(0,root_context);
454 XIC X11DRV_CreateIC(XIM xim, Display *display, Window win)
460 XVaNestedList preedit = NULL;
461 XVaNestedList status = NULL;
463 XIMCallback P_StartCB;
464 XIMCallback P_DoneCB;
465 XIMCallback P_DrawCB;
466 XIMCallback P_CaretCB;
467 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
471 /* use complex and slow XIC initialization method only for CJK */
472 if (langid != LANG_CHINESE &&
473 langid != LANG_JAPANESE &&
474 langid != LANG_KOREAN)
477 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
485 fontSet = XCreateFontSet(display,
487 &list, &count, NULL);
489 TRACE("ximFontSet = %p\n", fontSet);
490 TRACE("list = %p, count = %d\n", list, count);
496 for (i = 0; i < count; ++i)
498 TRACE("list[%d] = %s\n", i, list[i]);
500 XFreeStringList(list);
503 /* create callbacks */
504 P_StartCB.client_data = NULL;
505 P_StartCB.callback = (XIMProc)XIMPreEditStartCallback;
506 P_DoneCB.client_data = NULL;
507 P_DoneCB.callback = (XIMProc)XIMPreEditDoneCallback;
508 P_DrawCB.client_data = NULL;
509 P_DrawCB.callback = (XIMProc)XIMPreEditDrawCallback;
510 P_CaretCB.client_data = NULL;
511 P_CaretCB.callback = (XIMProc)XIMPreEditCaretCallback;
513 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
515 preedit = XVaCreateNestedList(0,
517 XNSpotLocation, &spot,
518 XNPreeditStartCallback, &P_StartCB,
519 XNPreeditDoneCallback, &P_DoneCB,
520 XNPreeditDrawCallback, &P_DrawCB,
521 XNPreeditCaretCallback, &P_CaretCB,
523 TRACE("preedit = %p\n", preedit);
527 preedit = XVaCreateNestedList(0,
528 XNPreeditStartCallback, &P_StartCB,
529 XNPreeditDoneCallback, &P_DoneCB,
530 XNPreeditDrawCallback, &P_DrawCB,
531 XNPreeditCaretCallback, &P_CaretCB,
534 TRACE("preedit = %p\n", preedit);
537 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
539 status = XVaCreateNestedList(0,
542 TRACE("status = %p\n", status);
545 if (preedit != NULL && status != NULL)
548 XNInputStyle, ximStyle,
549 XNPreeditAttributes, preedit,
550 XNStatusAttributes, status,
555 else if (preedit != NULL)
558 XNInputStyle, ximStyle,
559 XNPreeditAttributes, preedit,
564 else if (status != NULL)
567 XNInputStyle, ximStyle,
568 XNStatusAttributes, status,
576 XNInputStyle, ximStyle,
582 TRACE("xic = %p\n", xic);