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 #ifndef HAVE_XICCALLBACK_CALLBACK
38 #define XICCallback XIMCallback
39 #define XICProc XIMProc
42 BOOL ximInComposeMode=FALSE;
44 /* moved here from imm32 for dll separation */
45 static DWORD dwCompStringLength = 0;
46 static LPBYTE CompositionString = NULL;
47 static DWORD dwCompStringSize = 0;
48 static LPBYTE ResultString = NULL;
49 static DWORD dwResultStringSize = 0;
51 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
52 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
53 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
54 /* this uses all the callbacks to utilize full IME support */
55 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
56 /* inorder to enable deadkey support */
57 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
59 static XIMStyle ximStyle = 0;
60 static XIMStyle ximStyleRoot = 0;
61 static XIMStyle ximStyleRequest = STYLE_CALLBACK;
63 static BOOL X11DRV_ImmSetInternalString(DWORD dwIndex, DWORD dwOffset,
64 DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
66 /* Composition strings are edited in chunks */
67 unsigned int byte_length = dwCompLen * sizeof(WCHAR);
68 unsigned int byte_offset = dwOffset * sizeof(WCHAR);
69 unsigned int byte_selection = selLength * sizeof(WCHAR);
72 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset, selLength, dwIndex, lpComp, dwCompLen );
74 if (dwIndex == GCS_COMPSTR)
80 if ((dwCompLen == 0) && (selLength == 0))
84 /* deletion occurred */
85 else if ((dwCompLen== 0) && (selLength != 0))
87 if (dwCompStringLength)
89 for (i = 0; i < byte_selection; i++)
91 if (byte_offset+byte_selection+i <
94 CompositionString[byte_offset + i] =
95 CompositionString[byte_offset + byte_selection + i];
98 CompositionString[byte_offset + i] = 0;
100 /* clean up the end */
101 dwCompStringLength -= byte_selection;
103 i = dwCompStringLength;
104 while (i < dwCompStringSize)
106 CompositionString[i++] = 0;
112 int byte_expansion = byte_length - byte_selection;
114 if (byte_expansion + dwCompStringLength >= dwCompStringSize)
116 if (CompositionString)
118 HeapReAlloc(GetProcessHeap(), 0,
124 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
127 memset(&(CompositionString[dwCompStringSize]), 0,
130 dwCompStringSize += byte_expansion;
133 ptr_new = ((LPBYTE)lpComp);
134 ptr_old = CompositionString + byte_offset + byte_selection;
136 dwCompStringLength += byte_expansion;
138 for (j=0,i = byte_offset; i < dwCompStringSize; i++)
142 CompositionString[i] = ptr_new[j++];
146 if (ptr_old < CompositionString + dwCompStringSize)
148 CompositionString[i] = *ptr_old;
152 CompositionString[i] = 0;
157 rc = IME_SetCompositionString(SCS_SETSTR, (LPWSTR)CompositionString,
158 dwCompStringLength, NULL, 0);
160 else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
162 if (dwResultStringSize)
163 HeapFree(GetProcessHeap(),0,ResultString);
164 dwResultStringSize= byte_length;
165 ResultString= HeapAlloc(GetProcessHeap(),0,byte_length);
166 memcpy(ResultString,lpComp,byte_length);
168 rc = IME_SetCompositionString(SCS_SETSTR, (LPWSTR)ResultString,
169 dwResultStringSize, NULL, 0);
171 IME_NotifyIME( NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
177 void X11DRV_XIMLookupChars( const char *str, DWORD count )
183 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, sizeof(wcOutput)/sizeof(WCHAR));
185 if ((focus = GetFocus()))
186 IME_UpdateAssociation(focus);
188 X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
191 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
195 if (dwCompStringSize)
196 HeapFree(GetProcessHeap(),0,CompositionString);
198 dwCompStringSize = 0;
199 dwCompStringLength = 0;
200 CompositionString = NULL;
202 if (dwResultStringSize)
203 HeapFree(GetProcessHeap(),0,ResultString);
205 dwResultStringSize = 0;
209 IME_SetOpenStatus(fOpen);
212 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
214 TRACE("PreEditStartCallback %p\n",ic);
215 X11DRV_ImmSetOpenStatus(TRUE);
216 ximInComposeMode = TRUE;
220 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
222 TRACE("PreeditDoneCallback %p\n",ic);
223 ximInComposeMode = FALSE;
224 X11DRV_ImmSetOpenStatus(FALSE);
227 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
228 XIMPreeditDrawCallbackStruct *P_DR)
230 TRACE("PreEditDrawCallback %p\n",ic);
234 int sel = P_DR->chg_first;
235 int len = P_DR->chg_length;
238 if (! P_DR->text->encoding_is_wchar)
243 TRACE("multibyte\n");
244 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
245 P_DR->text->string.multi_byte, -1,
247 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
250 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
251 P_DR->text->string.multi_byte, -1,
256 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
257 HeapFree(GetProcessHeap(), 0, wcOutput);
262 FIXME("wchar PROBIBILY WRONG\n");
263 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
264 (LPWSTR)P_DR->text->string.wide_char,
269 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
270 IME_SetCursorPos(P_DR->caret);
275 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
276 XIMPreeditCaretCallbackStruct *P_C)
278 TRACE("PreeditCaretCallback %p\n",ic);
282 int pos = IME_GetCursorPos();
283 TRACE("pos: %d\n", pos);
284 switch(P_C->direction)
290 case XIMBackwardChar:
291 case XIMBackwardWord:
297 case XIMAbsolutePosition:
305 case XIMPreviousLine:
308 FIXME("Not implemented\n");
311 IME_SetCursorPos(pos);
317 void X11DRV_ForceXIMReset(HWND hwnd)
319 XIC ic = X11DRV_get_ic(hwnd);
323 TRACE("Forcing Reset %p\n",ic);
325 leftover = XmbResetIC(ic);
331 /***********************************************************************
334 * Process-wide XIM initialization.
336 BOOL X11DRV_InitXIM( const char *input_style )
340 if (!strcasecmp(input_style, "offthespot"))
341 ximStyleRequest = STYLE_OFFTHESPOT;
342 else if (!strcasecmp(input_style, "overthespot"))
343 ximStyleRequest = STYLE_OVERTHESPOT;
344 else if (!strcasecmp(input_style, "root"))
345 ximStyleRequest = STYLE_ROOT;
348 if (!(ret = XSupportsLocale()))
350 WARN("X does not support locale.\n");
352 else if (XSetLocaleModifiers("") == NULL)
354 WARN("Could not set locale modifiers.\n");
362 static void X11DRV_OpenIM(Display *display, XPointer p, XPointer data);
364 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
366 struct x11drv_thread_data *thread_data = x11drv_thread_data();
368 TRACE("xim = %p, p = %p\n", xim, p);
369 thread_data->xim = NULL;
372 XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, X11DRV_OpenIM, NULL );
376 /***********************************************************************
377 * X11DRV Ime creation
379 static void X11DRV_OpenIM(Display *display, XPointer ptr, XPointer data)
381 struct x11drv_thread_data *thread_data = x11drv_thread_data();
382 XIMStyle ximStyleCallback, ximStyleNone;
383 XIMStyles *ximStyles = NULL;
390 xim = XOpenIM(display, NULL, NULL, NULL);
393 WARN("Could not open input method.\n");
398 destroy.client_data = NULL;
399 destroy.callback = X11DRV_DestroyIM;
400 if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
402 WARN("Could not set destroy callback.\n");
405 TRACE("xim = %p\n", xim);
406 TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
407 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
409 XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
412 WARN("Could not find supported input style.\n");
419 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
423 ximStyleCallback = 0;
425 for (i = 0; i < ximStyles->count_styles; ++i)
427 int style = ximStyles->supported_styles[i];
428 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
429 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
430 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
431 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
432 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
433 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
434 if (!ximStyle && (ximStyles->supported_styles[i] ==
437 ximStyle = ximStyleRequest;
438 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
440 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
443 ximStyleRoot = STYLE_ROOT;
444 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
446 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
449 ximStyleCallback = STYLE_CALLBACK;
450 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
452 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
455 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
456 ximStyleNone = STYLE_NONE;
462 ximStyle = ximStyleRoot;
465 ximStyle = ximStyleNone;
467 if (ximStyleCallback == 0)
469 TRACE("No callback style avalable\n");
470 ximStyleCallback = ximStyle;
475 thread_data->xim = xim;
476 XUnregisterIMInstantiateCallback(display, NULL, NULL, NULL, X11DRV_OpenIM, NULL);
478 IME_UpdateAssociation(NULL);
482 void X11DRV_SetupXIM(void)
485 XRegisterIMInstantiateCallback(thread_display(), NULL, NULL, NULL, X11DRV_OpenIM, NULL);
489 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
491 struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
492 TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
493 win_data->xic = NULL;
498 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
501 XVaNestedList preedit = NULL;
502 XVaNestedList status = NULL;
504 XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
505 XICCallback P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
506 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
507 Window win = data->whole_window;
509 TRACE("xim = %p\n", xim);
513 /* use complex and slow XIC initialization method only for CJK */
514 if (langid != LANG_CHINESE &&
515 langid != LANG_JAPANESE &&
516 langid != LANG_KOREAN)
519 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
522 XNDestroyCallback, &destroy,
529 /* create callbacks */
530 P_StartCB.client_data = NULL;
531 P_DoneCB.client_data = NULL;
532 P_DrawCB.client_data = NULL;
533 P_CaretCB.client_data = NULL;
534 P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
535 P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
536 P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
537 P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
539 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
541 preedit = XVaCreateNestedList(0,
542 XNSpotLocation, &spot,
543 XNPreeditStartCallback, &P_StartCB,
544 XNPreeditDoneCallback, &P_DoneCB,
545 XNPreeditDrawCallback, &P_DrawCB,
546 XNPreeditCaretCallback, &P_CaretCB,
548 TRACE("preedit = %p\n", preedit);
552 preedit = XVaCreateNestedList(0,
553 XNPreeditStartCallback, &P_StartCB,
554 XNPreeditDoneCallback, &P_DoneCB,
555 XNPreeditDrawCallback, &P_DrawCB,
556 XNPreeditCaretCallback, &P_CaretCB,
559 TRACE("preedit = %p\n", preedit);
562 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
564 status = XVaCreateNestedList(0,
566 TRACE("status = %p\n", status);
569 if (preedit != NULL && status != NULL)
572 XNInputStyle, ximStyle,
573 XNPreeditAttributes, preedit,
574 XNStatusAttributes, status,
577 XNDestroyCallback, &destroy,
580 else if (preedit != NULL)
583 XNInputStyle, ximStyle,
584 XNPreeditAttributes, preedit,
587 XNDestroyCallback, &destroy,
590 else if (status != NULL)
593 XNInputStyle, ximStyle,
594 XNStatusAttributes, status,
597 XNDestroyCallback, &destroy,
603 XNInputStyle, ximStyle,
606 XNDestroyCallback, &destroy,
610 TRACE("xic = %p\n", xic);