winex11.drv: Private copy of a result string is unnecessary.
[wine/testsucceed.git] / dlls / winex11.drv / xim.c
blob1e5543105de54e240df57898df6c2ee353393562
1 /*
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
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "wingdi.h"
30 #include "winnls.h"
31 #include "x11drv.h"
32 #include "imm.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
37 #ifndef HAVE_XICCALLBACK_CALLBACK
38 #define XICCallback XIMCallback
39 #define XICProc XIMProc
40 #endif
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;
49 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
50 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
51 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
52 /* this uses all the callbacks to utilize full IME support */
53 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
54 /* inorder to enable deadkey support */
55 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
57 static XIMStyle ximStyle = 0;
58 static XIMStyle ximStyleRoot = 0;
59 static XIMStyle ximStyleRequest = STYLE_CALLBACK;
61 static BOOL X11DRV_ImmSetInternalString(DWORD dwIndex, DWORD dwOffset,
62 DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
64 /* Composition strings are edited in chunks */
65 unsigned int byte_length = dwCompLen * sizeof(WCHAR);
66 unsigned int byte_offset = dwOffset * sizeof(WCHAR);
67 unsigned int byte_selection = selLength * sizeof(WCHAR);
68 BOOL rc = FALSE;
70 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset, selLength, dwIndex, lpComp, dwCompLen );
72 if (dwIndex == GCS_COMPSTR)
74 unsigned int i,j;
75 LPBYTE ptr_new;
76 LPBYTE ptr_old;
78 if ((dwCompLen == 0) && (selLength == 0))
80 /* DO Nothing */
82 /* deletion occurred */
83 else if ((dwCompLen== 0) && (selLength != 0))
85 if (dwCompStringLength)
87 for (i = 0; i < byte_selection; i++)
89 if (byte_offset+byte_selection+i <
90 dwCompStringLength)
92 CompositionString[byte_offset + i] =
93 CompositionString[byte_offset + byte_selection + i];
95 else
96 CompositionString[byte_offset + i] = 0;
98 /* clean up the end */
99 dwCompStringLength -= byte_selection;
101 i = dwCompStringLength;
102 while (i < dwCompStringSize)
104 CompositionString[i++] = 0;
108 else
110 int byte_expansion = byte_length - byte_selection;
112 if (byte_expansion + dwCompStringLength >= dwCompStringSize)
114 if (CompositionString)
115 CompositionString =
116 HeapReAlloc(GetProcessHeap(), 0,
117 CompositionString,
118 dwCompStringSize +
119 byte_expansion);
120 else
121 CompositionString =
122 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
123 byte_expansion);
125 memset(&(CompositionString[dwCompStringSize]), 0,
126 byte_expansion);
128 dwCompStringSize += byte_expansion;
131 ptr_new = ((LPBYTE)lpComp);
132 ptr_old = CompositionString + byte_offset + byte_selection;
134 dwCompStringLength += byte_expansion;
136 for (j=0,i = byte_offset; i < dwCompStringSize; i++)
138 if (j < byte_length)
140 CompositionString[i] = ptr_new[j++];
142 else
144 if (ptr_old < CompositionString + dwCompStringSize)
146 CompositionString[i] = *ptr_old;
147 ptr_old++;
149 else
150 CompositionString[i] = 0;
155 rc = IME_SetCompositionString(SCS_SETSTR, CompositionString,
156 dwCompStringLength, NULL, 0);
158 else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
160 rc = IME_SetCompositionString(SCS_SETSTR, lpComp,
161 byte_length, NULL, 0);
163 IME_NotifyIME( NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
166 return rc;
169 void X11DRV_XIMLookupChars( const char *str, DWORD count )
171 DWORD dwOutput;
172 WCHAR *wcOutput;
173 HWND focus;
175 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, NULL, 0);
176 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * dwOutput);
177 if (wcOutput == NULL)
178 return;
179 MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, dwOutput);
181 if ((focus = GetFocus()))
182 IME_UpdateAssociation(focus);
184 X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
185 HeapFree(GetProcessHeap(), 0, wcOutput);
188 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
190 if (fOpen == FALSE)
192 if (dwCompStringSize)
193 HeapFree(GetProcessHeap(),0,CompositionString);
195 dwCompStringSize = 0;
196 dwCompStringLength = 0;
197 CompositionString = NULL;
200 IME_SetOpenStatus(fOpen);
203 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
205 TRACE("PreEditStartCallback %p\n",ic);
206 X11DRV_ImmSetOpenStatus(TRUE);
207 ximInComposeMode = TRUE;
208 return -1;
211 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
213 TRACE("PreeditDoneCallback %p\n",ic);
214 ximInComposeMode = FALSE;
215 X11DRV_ImmSetOpenStatus(FALSE);
218 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
219 XIMPreeditDrawCallbackStruct *P_DR)
221 TRACE("PreEditDrawCallback %p\n",ic);
223 if (P_DR)
225 int sel = P_DR->chg_first;
226 int len = P_DR->chg_length;
227 if (P_DR->text)
229 if (! P_DR->text->encoding_is_wchar)
231 DWORD dwOutput;
232 WCHAR *wcOutput;
234 TRACE("multibyte\n");
235 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
236 P_DR->text->string.multi_byte, -1,
237 NULL, 0);
238 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
239 if (wcOutput)
241 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
242 P_DR->text->string.multi_byte, -1,
243 wcOutput, dwOutput);
245 /* ignore null */
246 dwOutput --;
247 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
248 HeapFree(GetProcessHeap(), 0, wcOutput);
251 else
253 FIXME("wchar PROBIBILY WRONG\n");
254 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
255 (LPWSTR)P_DR->text->string.wide_char,
256 P_DR->text->length);
259 else
260 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
261 IME_SetCursorPos(P_DR->caret);
263 TRACE("Finished\n");
266 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
267 XIMPreeditCaretCallbackStruct *P_C)
269 TRACE("PreeditCaretCallback %p\n",ic);
271 if (P_C)
273 int pos = IME_GetCursorPos();
274 TRACE("pos: %d\n", pos);
275 switch(P_C->direction)
277 case XIMForwardChar:
278 case XIMForwardWord:
279 pos++;
280 break;
281 case XIMBackwardChar:
282 case XIMBackwardWord:
283 pos--;
284 break;
285 case XIMLineStart:
286 pos = 0;
287 break;
288 case XIMAbsolutePosition:
289 pos = P_C->position;
290 break;
291 case XIMDontChange:
292 P_C->position = pos;
293 return;
294 case XIMCaretUp:
295 case XIMCaretDown:
296 case XIMPreviousLine:
297 case XIMNextLine:
298 case XIMLineEnd:
299 FIXME("Not implemented\n");
300 break;
302 IME_SetCursorPos(pos);
303 P_C->position = pos;
305 TRACE("Finished\n");
308 void X11DRV_ForceXIMReset(HWND hwnd)
310 XIC ic = X11DRV_get_ic(hwnd);
311 if (ic)
313 char* leftover;
314 TRACE("Forcing Reset %p\n",ic);
315 wine_tsx11_lock();
316 leftover = XmbResetIC(ic);
317 XFree(leftover);
318 wine_tsx11_unlock();
322 /***********************************************************************
323 * X11DRV_InitXIM
325 * Process-wide XIM initialization.
327 BOOL X11DRV_InitXIM( const char *input_style )
329 BOOL ret;
331 if (!strcasecmp(input_style, "offthespot"))
332 ximStyleRequest = STYLE_OFFTHESPOT;
333 else if (!strcasecmp(input_style, "overthespot"))
334 ximStyleRequest = STYLE_OVERTHESPOT;
335 else if (!strcasecmp(input_style, "root"))
336 ximStyleRequest = STYLE_ROOT;
338 wine_tsx11_lock();
339 if (!(ret = XSupportsLocale()))
341 WARN("X does not support locale.\n");
343 else if (XSetLocaleModifiers("") == NULL)
345 WARN("Could not set locale modifiers.\n");
346 ret = FALSE;
348 wine_tsx11_unlock();
349 return ret;
353 static void open_xim_callback( Display *display, XPointer ptr, XPointer data );
355 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
357 struct x11drv_thread_data *thread_data = x11drv_thread_data();
359 TRACE("xim = %p, p = %p\n", xim, p);
360 thread_data->xim = NULL;
361 ximStyle = 0;
362 wine_tsx11_lock();
363 XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, open_xim_callback, NULL );
364 wine_tsx11_unlock();
367 /***********************************************************************
368 * X11DRV Ime creation
370 * Should always be called with the x11 lock held
372 static BOOL open_xim( Display *display )
374 struct x11drv_thread_data *thread_data = x11drv_thread_data();
375 XIMStyle ximStyleCallback, ximStyleNone;
376 XIMStyles *ximStyles = NULL;
377 INT i;
378 XIM xim;
379 XIMCallback destroy;
381 xim = XOpenIM(display, NULL, NULL, NULL);
382 if (xim == NULL)
384 WARN("Could not open input method.\n");
385 return FALSE;
388 destroy.client_data = NULL;
389 destroy.callback = X11DRV_DestroyIM;
390 if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
392 WARN("Could not set destroy callback.\n");
395 TRACE("xim = %p\n", xim);
396 TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
397 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
399 XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
400 if (ximStyles == 0)
402 WARN("Could not find supported input style.\n");
403 XCloseIM(xim);
404 return FALSE;
406 else
408 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
410 ximStyleRoot = 0;
411 ximStyleNone = 0;
412 ximStyleCallback = 0;
414 for (i = 0; i < ximStyles->count_styles; ++i)
416 int style = ximStyles->supported_styles[i];
417 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
418 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
419 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
420 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
421 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
422 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
423 if (!ximStyle && (ximStyles->supported_styles[i] ==
424 ximStyleRequest))
426 ximStyle = ximStyleRequest;
427 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
429 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
430 STYLE_ROOT))
432 ximStyleRoot = STYLE_ROOT;
433 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
435 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
436 STYLE_CALLBACK))
438 ximStyleCallback = STYLE_CALLBACK;
439 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
441 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
442 STYLE_NONE))
444 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
445 ximStyleNone = STYLE_NONE;
448 XFree(ximStyles);
450 if (ximStyle == 0)
451 ximStyle = ximStyleRoot;
453 if (ximStyle == 0)
454 ximStyle = ximStyleNone;
456 if (ximStyleCallback == 0)
458 TRACE("No callback style avalable\n");
459 ximStyleCallback = ximStyle;
464 thread_data->xim = xim;
466 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0 ||
467 (ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
469 char **list;
470 int count;
471 thread_data->font_set = XCreateFontSet(display, "fixed",
472 &list, &count, NULL);
473 TRACE("ximFontSet = %p\n", thread_data->font_set);
474 TRACE("list = %p, count = %d\n", list, count);
475 if (list != NULL)
477 int i;
478 for (i = 0; i < count; ++i)
479 TRACE("list[%d] = %s\n", i, list[i]);
480 XFreeStringList(list);
483 else
484 thread_data->font_set = NULL;
486 wine_tsx11_unlock();
487 IME_UpdateAssociation(NULL);
488 wine_tsx11_lock();
489 return TRUE;
492 static void open_xim_callback( Display *display, XPointer ptr, XPointer data )
494 if (open_xim( display ))
495 XUnregisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL);
498 void X11DRV_SetupXIM(void)
500 Display *display = thread_display();
502 wine_tsx11_lock();
503 if (!open_xim( display ))
504 XRegisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL );
505 wine_tsx11_unlock();
508 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
510 struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
511 TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
512 win_data->xic = NULL;
513 return TRUE;
517 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
519 XPoint spot = {0};
520 XVaNestedList preedit = NULL;
521 XVaNestedList status = NULL;
522 XIC xic;
523 XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
524 XICCallback P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
525 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
526 Window win = data->whole_window;
527 XFontSet fontSet = x11drv_thread_data()->font_set;
529 TRACE("xim = %p\n", xim);
531 wine_tsx11_lock();
533 /* use complex and slow XIC initialization method only for CJK */
534 if (langid != LANG_CHINESE &&
535 langid != LANG_JAPANESE &&
536 langid != LANG_KOREAN)
538 xic = XCreateIC(xim,
539 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
540 XNClientWindow, win,
541 XNFocusWindow, win,
542 XNDestroyCallback, &destroy,
543 NULL);
544 wine_tsx11_unlock();
545 data->xic = xic;
546 return xic;
549 /* create callbacks */
550 P_StartCB.client_data = NULL;
551 P_DoneCB.client_data = NULL;
552 P_DrawCB.client_data = NULL;
553 P_CaretCB.client_data = NULL;
554 P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
555 P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
556 P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
557 P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
559 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
561 preedit = XVaCreateNestedList(0,
562 XNFontSet, fontSet,
563 XNSpotLocation, &spot,
564 XNPreeditStartCallback, &P_StartCB,
565 XNPreeditDoneCallback, &P_DoneCB,
566 XNPreeditDrawCallback, &P_DrawCB,
567 XNPreeditCaretCallback, &P_CaretCB,
568 NULL);
569 TRACE("preedit = %p\n", preedit);
571 else
573 preedit = XVaCreateNestedList(0,
574 XNPreeditStartCallback, &P_StartCB,
575 XNPreeditDoneCallback, &P_DoneCB,
576 XNPreeditDrawCallback, &P_DrawCB,
577 XNPreeditCaretCallback, &P_CaretCB,
578 NULL);
580 TRACE("preedit = %p\n", preedit);
583 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
585 status = XVaCreateNestedList(0,
586 XNFontSet, fontSet,
587 NULL);
588 TRACE("status = %p\n", status);
591 if (preedit != NULL && status != NULL)
593 xic = XCreateIC(xim,
594 XNInputStyle, ximStyle,
595 XNPreeditAttributes, preedit,
596 XNStatusAttributes, status,
597 XNClientWindow, win,
598 XNFocusWindow, win,
599 XNDestroyCallback, &destroy,
600 NULL);
602 else if (preedit != NULL)
604 xic = XCreateIC(xim,
605 XNInputStyle, ximStyle,
606 XNPreeditAttributes, preedit,
607 XNClientWindow, win,
608 XNFocusWindow, win,
609 XNDestroyCallback, &destroy,
610 NULL);
612 else if (status != NULL)
614 xic = XCreateIC(xim,
615 XNInputStyle, ximStyle,
616 XNStatusAttributes, status,
617 XNClientWindow, win,
618 XNFocusWindow, win,
619 XNDestroyCallback, &destroy,
620 NULL);
622 else
624 xic = XCreateIC(xim,
625 XNInputStyle, ximStyle,
626 XNClientWindow, win,
627 XNFocusWindow, win,
628 XNDestroyCallback, &destroy,
629 NULL);
632 TRACE("xic = %p\n", xic);
633 data->xic = xic;
635 if (preedit != NULL)
636 XFree(preedit);
637 if (status != NULL)
638 XFree(status);
640 wine_tsx11_unlock();
642 return xic;