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
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
38 #ifndef HAVE_XICCALLBACK_CALLBACK
39 #define XICCallback XIMCallback
40 #define XICProc XIMProc
43 BOOL ximInComposeMode=FALSE;
45 /* moved here from imm32 for dll separation */
46 static DWORD dwCompStringLength = 0;
47 static LPBYTE CompositionString = NULL;
48 static DWORD dwCompStringSize = 0;
49 static LPBYTE ResultString = NULL;
50 static DWORD dwResultStringSize = 0;
51 static DWORD dwPreeditPos = 0;
53 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
54 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
55 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
56 /* this uses all the callbacks to utilize full IME support */
57 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
58 /* inorder to enable deadkey support */
59 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
61 static XIMStyle ximStyle = 0;
62 static XIMStyle ximStyleRoot = 0;
63 static XIMStyle ximStyleRequest = STYLE_CALLBACK;
65 static BOOL X11DRV_ImmSetInternalString(DWORD dwIndex, DWORD dwOffset,
66 DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
68 /* Composition strings are edited in chunks */
69 unsigned int byte_length = dwCompLen * sizeof(WCHAR);
70 unsigned int byte_offset = dwOffset * sizeof(WCHAR);
71 unsigned int byte_selection = selLength * sizeof(WCHAR);
74 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset, selLength, dwIndex, lpComp, dwCompLen );
76 if (dwIndex == GCS_COMPSTR)
82 if ((dwCompLen == 0) && (selLength == 0))
86 /* deletion occurred */
87 else if ((dwCompLen== 0) && (selLength != 0))
89 if (dwCompStringLength)
91 for (i = 0; i < byte_selection; i++)
93 if (byte_offset+byte_selection+i <
96 CompositionString[byte_offset + i] =
97 CompositionString[byte_offset + byte_selection + i];
100 CompositionString[byte_offset + i] = 0;
102 /* clean up the end */
103 dwCompStringLength -= byte_selection;
105 i = dwCompStringLength;
106 while (i < dwCompStringSize)
108 CompositionString[i++] = 0;
114 int byte_expansion = byte_length - byte_selection;
116 if (byte_expansion + dwCompStringLength >= dwCompStringSize)
118 if (CompositionString)
120 HeapReAlloc(GetProcessHeap(), 0,
126 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
129 memset(&(CompositionString[dwCompStringSize]), 0,
132 dwCompStringSize += byte_expansion;
135 ptr_new = ((LPBYTE)lpComp);
136 ptr_old = CompositionString + byte_offset + byte_selection;
138 dwCompStringLength += byte_expansion;
140 for (j=0,i = byte_offset; i < dwCompStringSize; i++)
144 CompositionString[i] = ptr_new[j++];
148 if (ptr_old < CompositionString + dwCompStringSize)
150 CompositionString[i] = *ptr_old;
154 CompositionString[i] = 0;
159 rc = IME_SetCompositionString(SCS_SETSTR, (LPWSTR)CompositionString,
160 dwCompStringLength, NULL, 0);
162 else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
164 if (dwResultStringSize)
165 HeapFree(GetProcessHeap(),0,ResultString);
166 dwResultStringSize= byte_length;
167 ResultString= HeapAlloc(GetProcessHeap(),0,byte_length);
168 memcpy(ResultString,lpComp,byte_length);
170 rc = IME_SetCompositionString(SCS_SETSTR, (LPWSTR)ResultString,
171 dwResultStringSize, NULL, 0);
173 IME_NotifyIME( NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
179 void X11DRV_XIMLookupChars( const char *str, DWORD count )
185 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, sizeof(wcOutput)/sizeof(WCHAR));
187 if ((focus = GetFocus()))
188 IME_UpdateAssociation(focus);
190 X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
193 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
197 if (dwCompStringSize)
198 HeapFree(GetProcessHeap(),0,CompositionString);
200 dwCompStringSize = 0;
201 dwCompStringLength = 0;
202 CompositionString = NULL;
204 if (dwResultStringSize)
205 HeapFree(GetProcessHeap(),0,ResultString);
207 dwResultStringSize = 0;
211 IME_SetOpenStatus(fOpen);
214 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
216 TRACE("PreEditStartCallback %p\n",ic);
217 X11DRV_ImmSetOpenStatus(TRUE);
218 ximInComposeMode = TRUE;
219 IME_SendMessageToSelectedHWND(EM_GETSEL, 0, (LPARAM)&dwPreeditPos);
223 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
225 TRACE("PreeditDoneCallback %p\n",ic);
226 ximInComposeMode = FALSE;
227 X11DRV_ImmSetOpenStatus(FALSE);
231 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
232 XIMPreeditDrawCallbackStruct *P_DR)
234 TRACE("PreEditDrawCallback %p\n",ic);
238 int sel = P_DR->chg_first;
239 int len = P_DR->chg_length;
242 if (! P_DR->text->encoding_is_wchar)
247 TRACE("multibyte\n");
248 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
249 P_DR->text->string.multi_byte, -1,
251 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
254 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
255 P_DR->text->string.multi_byte, -1,
260 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
261 HeapFree(GetProcessHeap(), 0, wcOutput);
266 FIXME("wchar PROBIBILY WRONG\n");
267 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
268 (LPWSTR)P_DR->text->string.wide_char,
273 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
278 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
279 XIMPreeditCaretCallbackStruct *P_C)
281 TRACE("PreeditCaretCallback %p\n",ic);
285 int pos = IME_GetCursorPos();
286 TRACE("pos: %d\n", pos);
287 switch(P_C->direction)
293 case XIMBackwardChar:
294 case XIMBackwardWord:
300 case XIMAbsolutePosition:
308 case XIMPreviousLine:
311 FIXME("Not implemented\n");
314 IME_SendMessageToSelectedHWND( EM_SETSEL, dwPreeditPos + pos,
321 void X11DRV_ForceXIMReset(HWND hwnd)
323 XIC ic = X11DRV_get_ic(hwnd);
327 TRACE("Forcing Reset %p\n",ic);
329 leftover = XmbResetIC(ic);
335 /***********************************************************************
338 * Process-wide XIM initialization.
340 BOOL X11DRV_InitXIM( const char *input_style )
344 if (!strcasecmp(input_style, "offthespot"))
345 ximStyleRequest = STYLE_OFFTHESPOT;
346 else if (!strcasecmp(input_style, "overthespot"))
347 ximStyleRequest = STYLE_OVERTHESPOT;
348 else if (!strcasecmp(input_style, "root"))
349 ximStyleRequest = STYLE_ROOT;
352 if (!(ret = XSupportsLocale()))
354 WARN("X does not support locale.\n");
356 else if (XSetLocaleModifiers("") == NULL)
358 WARN("Could not set locale modifiers.\n");
366 static void X11DRV_OpenIM(Display *display, XPointer p, XPointer data);
368 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
370 struct x11drv_thread_data *thread_data = x11drv_thread_data();
372 TRACE("xim = %p, p = %p\n", xim, p);
373 thread_data->xim = NULL;
376 XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, X11DRV_OpenIM, NULL );
380 /***********************************************************************
381 * X11DRV Ime creation
383 static void X11DRV_OpenIM(Display *display, XPointer ptr, XPointer data)
385 struct x11drv_thread_data *thread_data = x11drv_thread_data();
386 XIMStyle ximStyleCallback, ximStyleNone;
387 XIMStyles *ximStyles = NULL;
394 xim = XOpenIM(display, NULL, NULL, NULL);
397 WARN("Could not open input method.\n");
402 destroy.client_data = NULL;
403 destroy.callback = X11DRV_DestroyIM;
404 if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
406 WARN("Could not set destroy callback.\n");
409 TRACE("xim = %p\n", xim);
410 TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
411 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
413 XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
416 WARN("Could not find supported input style.\n");
423 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
427 ximStyleCallback = 0;
429 for (i = 0; i < ximStyles->count_styles; ++i)
431 int style = ximStyles->supported_styles[i];
432 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
433 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
434 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
435 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
436 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
437 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
438 if (!ximStyle && (ximStyles->supported_styles[i] ==
441 ximStyle = ximStyleRequest;
442 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
444 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
447 ximStyleRoot = STYLE_ROOT;
448 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
450 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
453 ximStyleCallback = STYLE_CALLBACK;
454 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
456 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
459 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
460 ximStyleNone = STYLE_NONE;
466 ximStyle = ximStyleRoot;
469 ximStyle = ximStyleNone;
471 if (ximStyleCallback == 0)
473 TRACE("No callback style avalable\n");
474 ximStyleCallback = ximStyle;
479 thread_data->xim = xim;
480 XUnregisterIMInstantiateCallback(display, NULL, NULL, NULL, X11DRV_OpenIM, NULL);
482 IME_XIMPresent(TRUE);
483 IME_UpdateAssociation(NULL);
487 void X11DRV_SetupXIM(void)
490 XRegisterIMInstantiateCallback(thread_display(), NULL, NULL, NULL, X11DRV_OpenIM, NULL);
494 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
496 struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
497 TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
498 win_data->xic = NULL;
503 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
506 XVaNestedList preedit = NULL;
507 XVaNestedList status = NULL;
509 XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
510 XICCallback P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
511 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
512 Window win = data->whole_window;
514 TRACE("xim = %p\n", xim);
518 /* use complex and slow XIC initialization method only for CJK */
519 if (langid != LANG_CHINESE &&
520 langid != LANG_JAPANESE &&
521 langid != LANG_KOREAN)
524 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
527 XNDestroyCallback, &destroy,
534 /* create callbacks */
535 P_StartCB.client_data = NULL;
536 P_DoneCB.client_data = NULL;
537 P_DrawCB.client_data = NULL;
538 P_CaretCB.client_data = NULL;
539 P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
540 P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
541 P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
542 P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
544 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
546 preedit = XVaCreateNestedList(0,
547 XNSpotLocation, &spot,
548 XNPreeditStartCallback, &P_StartCB,
549 XNPreeditDoneCallback, &P_DoneCB,
550 XNPreeditDrawCallback, &P_DrawCB,
551 XNPreeditCaretCallback, &P_CaretCB,
553 TRACE("preedit = %p\n", preedit);
557 preedit = XVaCreateNestedList(0,
558 XNPreeditStartCallback, &P_StartCB,
559 XNPreeditDoneCallback, &P_DoneCB,
560 XNPreeditDrawCallback, &P_DrawCB,
561 XNPreeditCaretCallback, &P_CaretCB,
564 TRACE("preedit = %p\n", preedit);
567 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
569 status = XVaCreateNestedList(0,
571 TRACE("status = %p\n", status);
574 if (preedit != NULL && status != NULL)
577 XNInputStyle, ximStyle,
578 XNPreeditAttributes, preedit,
579 XNStatusAttributes, status,
582 XNDestroyCallback, &destroy,
585 else if (preedit != NULL)
588 XNInputStyle, ximStyle,
589 XNPreeditAttributes, preedit,
592 XNDestroyCallback, &destroy,
595 else if (status != NULL)
598 XNInputStyle, ximStyle,
599 XNStatusAttributes, status,
602 XNDestroyCallback, &destroy,
608 XNInputStyle, ximStyle,
611 XNDestroyCallback, &destroy,
615 TRACE("xic = %p\n", xic);