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;
52 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
53 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
54 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
55 /* this uses all the callbacks to utilize full IME support */
56 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
57 /* inorder to enable deadkey support */
58 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
60 static XIMStyle ximStyle = 0;
61 static XIMStyle ximStyleRoot = 0;
62 static XIMStyle ximStyleRequest = STYLE_CALLBACK;
64 static BOOL X11DRV_ImmSetInternalString(DWORD dwIndex, DWORD dwOffset,
65 DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
67 /* Composition strings are edited in chunks */
68 unsigned int byte_length = dwCompLen * sizeof(WCHAR);
69 unsigned int byte_offset = dwOffset * sizeof(WCHAR);
70 unsigned int byte_selection = selLength * sizeof(WCHAR);
73 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset, selLength, dwIndex, lpComp, dwCompLen );
75 if (dwIndex == GCS_COMPSTR)
81 if ((dwCompLen == 0) && (selLength == 0))
85 /* deletion occurred */
86 else if ((dwCompLen== 0) && (selLength != 0))
88 if (dwCompStringLength)
90 for (i = 0; i < byte_selection; i++)
92 if (byte_offset+byte_selection+i <
95 CompositionString[byte_offset + i] =
96 CompositionString[byte_offset + byte_selection + i];
99 CompositionString[byte_offset + i] = 0;
101 /* clean up the end */
102 dwCompStringLength -= byte_selection;
104 i = dwCompStringLength;
105 while (i < dwCompStringSize)
107 CompositionString[i++] = 0;
113 int byte_expansion = byte_length - byte_selection;
115 if (byte_expansion + dwCompStringLength >= dwCompStringSize)
117 if (CompositionString)
119 HeapReAlloc(GetProcessHeap(), 0,
125 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
128 memset(&(CompositionString[dwCompStringSize]), 0,
131 dwCompStringSize += byte_expansion;
134 ptr_new = ((LPBYTE)lpComp);
135 ptr_old = CompositionString + byte_offset + byte_selection;
137 dwCompStringLength += byte_expansion;
139 for (j=0,i = byte_offset; i < dwCompStringSize; i++)
143 CompositionString[i] = ptr_new[j++];
147 if (ptr_old < CompositionString + dwCompStringSize)
149 CompositionString[i] = *ptr_old;
153 CompositionString[i] = 0;
158 rc = IME_SetCompositionString(SCS_SETSTR, (LPWSTR)CompositionString,
159 dwCompStringLength, NULL, 0);
161 else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
163 if (dwResultStringSize)
164 HeapFree(GetProcessHeap(),0,ResultString);
165 dwResultStringSize= byte_length;
166 ResultString= HeapAlloc(GetProcessHeap(),0,byte_length);
167 memcpy(ResultString,lpComp,byte_length);
169 rc = IME_SetCompositionString(SCS_SETSTR, (LPWSTR)ResultString,
170 dwResultStringSize, NULL, 0);
172 IME_NotifyIME( NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
178 void X11DRV_XIMLookupChars( const char *str, DWORD count )
184 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, sizeof(wcOutput)/sizeof(WCHAR));
186 if ((focus = GetFocus()))
187 IME_UpdateAssociation(focus);
189 X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
192 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
196 if (dwCompStringSize)
197 HeapFree(GetProcessHeap(),0,CompositionString);
199 dwCompStringSize = 0;
200 dwCompStringLength = 0;
201 CompositionString = NULL;
203 if (dwResultStringSize)
204 HeapFree(GetProcessHeap(),0,ResultString);
206 dwResultStringSize = 0;
210 IME_SetOpenStatus(fOpen);
213 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
215 TRACE("PreEditStartCallback %p\n",ic);
216 X11DRV_ImmSetOpenStatus(TRUE);
217 ximInComposeMode = TRUE;
221 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
223 TRACE("PreeditDoneCallback %p\n",ic);
224 ximInComposeMode = FALSE;
225 X11DRV_ImmSetOpenStatus(FALSE);
228 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
229 XIMPreeditDrawCallbackStruct *P_DR)
231 TRACE("PreEditDrawCallback %p\n",ic);
235 int sel = P_DR->chg_first;
236 int len = P_DR->chg_length;
239 if (! P_DR->text->encoding_is_wchar)
244 TRACE("multibyte\n");
245 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
246 P_DR->text->string.multi_byte, -1,
248 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
251 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
252 P_DR->text->string.multi_byte, -1,
257 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
258 HeapFree(GetProcessHeap(), 0, wcOutput);
263 FIXME("wchar PROBIBILY WRONG\n");
264 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
265 (LPWSTR)P_DR->text->string.wide_char,
270 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
271 IME_SetCursorPos(P_DR->caret);
276 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
277 XIMPreeditCaretCallbackStruct *P_C)
279 TRACE("PreeditCaretCallback %p\n",ic);
283 int pos = IME_GetCursorPos();
284 TRACE("pos: %d\n", pos);
285 switch(P_C->direction)
291 case XIMBackwardChar:
292 case XIMBackwardWord:
298 case XIMAbsolutePosition:
306 case XIMPreviousLine:
309 FIXME("Not implemented\n");
312 IME_SetCursorPos(pos);
318 void X11DRV_ForceXIMReset(HWND hwnd)
320 XIC ic = X11DRV_get_ic(hwnd);
324 TRACE("Forcing Reset %p\n",ic);
326 leftover = XmbResetIC(ic);
332 /***********************************************************************
335 * Process-wide XIM initialization.
337 BOOL X11DRV_InitXIM( const char *input_style )
341 if (!strcasecmp(input_style, "offthespot"))
342 ximStyleRequest = STYLE_OFFTHESPOT;
343 else if (!strcasecmp(input_style, "overthespot"))
344 ximStyleRequest = STYLE_OVERTHESPOT;
345 else if (!strcasecmp(input_style, "root"))
346 ximStyleRequest = STYLE_ROOT;
349 if (!(ret = XSupportsLocale()))
351 WARN("X does not support locale.\n");
353 else if (XSetLocaleModifiers("") == NULL)
355 WARN("Could not set locale modifiers.\n");
363 static void X11DRV_OpenIM(Display *display, XPointer p, XPointer data);
365 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
367 struct x11drv_thread_data *thread_data = x11drv_thread_data();
369 TRACE("xim = %p, p = %p\n", xim, p);
370 thread_data->xim = NULL;
373 XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, X11DRV_OpenIM, NULL );
377 /***********************************************************************
378 * X11DRV Ime creation
380 static void X11DRV_OpenIM(Display *display, XPointer ptr, XPointer data)
382 struct x11drv_thread_data *thread_data = x11drv_thread_data();
383 XIMStyle ximStyleCallback, ximStyleNone;
384 XIMStyles *ximStyles = NULL;
391 xim = XOpenIM(display, NULL, NULL, NULL);
394 WARN("Could not open input method.\n");
399 destroy.client_data = NULL;
400 destroy.callback = X11DRV_DestroyIM;
401 if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
403 WARN("Could not set destroy callback.\n");
406 TRACE("xim = %p\n", xim);
407 TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
408 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
410 XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
413 WARN("Could not find supported input style.\n");
420 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
424 ximStyleCallback = 0;
426 for (i = 0; i < ximStyles->count_styles; ++i)
428 int style = ximStyles->supported_styles[i];
429 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
430 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
431 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
432 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
433 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
434 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
435 if (!ximStyle && (ximStyles->supported_styles[i] ==
438 ximStyle = ximStyleRequest;
439 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
441 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
444 ximStyleRoot = STYLE_ROOT;
445 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
447 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
450 ximStyleCallback = STYLE_CALLBACK;
451 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
453 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
456 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
457 ximStyleNone = STYLE_NONE;
463 ximStyle = ximStyleRoot;
466 ximStyle = ximStyleNone;
468 if (ximStyleCallback == 0)
470 TRACE("No callback style avalable\n");
471 ximStyleCallback = ximStyle;
476 thread_data->xim = xim;
477 XUnregisterIMInstantiateCallback(display, NULL, NULL, NULL, X11DRV_OpenIM, NULL);
479 IME_XIMPresent(TRUE);
480 IME_UpdateAssociation(NULL);
484 void X11DRV_SetupXIM(void)
487 XRegisterIMInstantiateCallback(thread_display(), NULL, NULL, NULL, X11DRV_OpenIM, NULL);
491 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
493 struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
494 TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
495 win_data->xic = NULL;
500 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
503 XVaNestedList preedit = NULL;
504 XVaNestedList status = NULL;
506 XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
507 XICCallback P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
508 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
509 Window win = data->whole_window;
511 TRACE("xim = %p\n", xim);
515 /* use complex and slow XIC initialization method only for CJK */
516 if (langid != LANG_CHINESE &&
517 langid != LANG_JAPANESE &&
518 langid != LANG_KOREAN)
521 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
524 XNDestroyCallback, &destroy,
531 /* create callbacks */
532 P_StartCB.client_data = NULL;
533 P_DoneCB.client_data = NULL;
534 P_DrawCB.client_data = NULL;
535 P_CaretCB.client_data = NULL;
536 P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
537 P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
538 P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
539 P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
541 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
543 preedit = XVaCreateNestedList(0,
544 XNSpotLocation, &spot,
545 XNPreeditStartCallback, &P_StartCB,
546 XNPreeditDoneCallback, &P_DoneCB,
547 XNPreeditDrawCallback, &P_DrawCB,
548 XNPreeditCaretCallback, &P_CaretCB,
550 TRACE("preedit = %p\n", preedit);
554 preedit = XVaCreateNestedList(0,
555 XNPreeditStartCallback, &P_StartCB,
556 XNPreeditDoneCallback, &P_DoneCB,
557 XNPreeditDrawCallback, &P_DrawCB,
558 XNPreeditCaretCallback, &P_CaretCB,
561 TRACE("preedit = %p\n", preedit);
564 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
566 status = XVaCreateNestedList(0,
568 TRACE("status = %p\n", status);
571 if (preedit != NULL && status != NULL)
574 XNInputStyle, ximStyle,
575 XNPreeditAttributes, preedit,
576 XNStatusAttributes, status,
579 XNDestroyCallback, &destroy,
582 else if (preedit != NULL)
585 XNInputStyle, ximStyle,
586 XNPreeditAttributes, preedit,
589 XNDestroyCallback, &destroy,
592 else if (status != NULL)
595 XNInputStyle, ximStyle,
596 XNStatusAttributes, status,
599 XNDestroyCallback, &destroy,
605 XNInputStyle, ximStyle,
608 XNDestroyCallback, &destroy,
612 TRACE("xic = %p\n", xic);