advapi32/tests: Skip some tests on Win9x and WinMe.
[wine/hramrach.git] / dlls / winex11.drv / xim.c
blob5c30d5bd5832f6adc2ca7b5b95540b8684f4354d
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;
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);
70 BOOL rc = FALSE;
72 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset, selLength, dwIndex, lpComp, dwCompLen );
74 if (dwIndex == GCS_COMPSTR)
76 unsigned int i,j;
77 LPBYTE ptr_new;
78 LPBYTE ptr_old;
80 if ((dwCompLen == 0) && (selLength == 0))
82 /* DO Nothing */
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 <
92 dwCompStringLength)
94 CompositionString[byte_offset + i] =
95 CompositionString[byte_offset + byte_selection + i];
97 else
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;
110 else
112 int byte_expansion = byte_length - byte_selection;
114 if (byte_expansion + dwCompStringLength >= dwCompStringSize)
116 if (CompositionString)
117 CompositionString =
118 HeapReAlloc(GetProcessHeap(), 0,
119 CompositionString,
120 dwCompStringSize +
121 byte_expansion);
122 else
123 CompositionString =
124 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
125 byte_expansion);
127 memset(&(CompositionString[dwCompStringSize]), 0,
128 byte_expansion);
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++)
140 if (j < byte_length)
142 CompositionString[i] = ptr_new[j++];
144 else
146 if (ptr_old < CompositionString + dwCompStringSize)
148 CompositionString[i] = *ptr_old;
149 ptr_old++;
151 else
152 CompositionString[i] = 0;
157 rc = IME_SetCompositionString(SCS_SETSTR, 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, ResultString,
169 dwResultStringSize, NULL, 0);
171 IME_NotifyIME( NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
174 return rc;
177 void X11DRV_XIMLookupChars( const char *str, DWORD count )
179 DWORD dwOutput;
180 WCHAR *wcOutput;
181 HWND focus;
183 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, NULL, 0);
184 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * dwOutput);
185 if (wcOutput == NULL)
186 return;
187 MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, dwOutput);
189 if ((focus = GetFocus()))
190 IME_UpdateAssociation(focus);
192 X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
193 HeapFree(GetProcessHeap(), 0, wcOutput);
196 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
198 if (fOpen == FALSE)
200 if (dwCompStringSize)
201 HeapFree(GetProcessHeap(),0,CompositionString);
203 dwCompStringSize = 0;
204 dwCompStringLength = 0;
205 CompositionString = NULL;
207 if (dwResultStringSize)
208 HeapFree(GetProcessHeap(),0,ResultString);
210 dwResultStringSize = 0;
211 ResultString = NULL;
214 IME_SetOpenStatus(fOpen);
217 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
219 TRACE("PreEditStartCallback %p\n",ic);
220 X11DRV_ImmSetOpenStatus(TRUE);
221 ximInComposeMode = TRUE;
222 return -1;
225 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
227 TRACE("PreeditDoneCallback %p\n",ic);
228 ximInComposeMode = FALSE;
229 X11DRV_ImmSetOpenStatus(FALSE);
232 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
233 XIMPreeditDrawCallbackStruct *P_DR)
235 TRACE("PreEditDrawCallback %p\n",ic);
237 if (P_DR)
239 int sel = P_DR->chg_first;
240 int len = P_DR->chg_length;
241 if (P_DR->text)
243 if (! P_DR->text->encoding_is_wchar)
245 DWORD dwOutput;
246 WCHAR *wcOutput;
248 TRACE("multibyte\n");
249 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
250 P_DR->text->string.multi_byte, -1,
251 NULL, 0);
252 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
253 if (wcOutput)
255 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
256 P_DR->text->string.multi_byte, -1,
257 wcOutput, dwOutput);
259 /* ignore null */
260 dwOutput --;
261 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
262 HeapFree(GetProcessHeap(), 0, wcOutput);
265 else
267 FIXME("wchar PROBIBILY WRONG\n");
268 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
269 (LPWSTR)P_DR->text->string.wide_char,
270 P_DR->text->length);
273 else
274 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
275 IME_SetCursorPos(P_DR->caret);
277 TRACE("Finished\n");
280 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
281 XIMPreeditCaretCallbackStruct *P_C)
283 TRACE("PreeditCaretCallback %p\n",ic);
285 if (P_C)
287 int pos = IME_GetCursorPos();
288 TRACE("pos: %d\n", pos);
289 switch(P_C->direction)
291 case XIMForwardChar:
292 case XIMForwardWord:
293 pos++;
294 break;
295 case XIMBackwardChar:
296 case XIMBackwardWord:
297 pos--;
298 break;
299 case XIMLineStart:
300 pos = 0;
301 break;
302 case XIMAbsolutePosition:
303 pos = P_C->position;
304 break;
305 case XIMDontChange:
306 P_C->position = pos;
307 return;
308 case XIMCaretUp:
309 case XIMCaretDown:
310 case XIMPreviousLine:
311 case XIMNextLine:
312 case XIMLineEnd:
313 FIXME("Not implemented\n");
314 break;
316 IME_SetCursorPos(pos);
317 P_C->position = pos;
319 TRACE("Finished\n");
322 void X11DRV_ForceXIMReset(HWND hwnd)
324 XIC ic = X11DRV_get_ic(hwnd);
325 if (ic)
327 char* leftover;
328 TRACE("Forcing Reset %p\n",ic);
329 wine_tsx11_lock();
330 leftover = XmbResetIC(ic);
331 XFree(leftover);
332 wine_tsx11_unlock();
336 /***********************************************************************
337 * X11DRV_InitXIM
339 * Process-wide XIM initialization.
341 BOOL X11DRV_InitXIM( const char *input_style )
343 BOOL ret;
345 if (!strcasecmp(input_style, "offthespot"))
346 ximStyleRequest = STYLE_OFFTHESPOT;
347 else if (!strcasecmp(input_style, "overthespot"))
348 ximStyleRequest = STYLE_OVERTHESPOT;
349 else if (!strcasecmp(input_style, "root"))
350 ximStyleRequest = STYLE_ROOT;
352 wine_tsx11_lock();
353 if (!(ret = XSupportsLocale()))
355 WARN("X does not support locale.\n");
357 else if (XSetLocaleModifiers("") == NULL)
359 WARN("Could not set locale modifiers.\n");
360 ret = FALSE;
362 wine_tsx11_unlock();
363 return ret;
367 static void open_xim_callback( Display *display, XPointer ptr, XPointer data );
369 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
371 struct x11drv_thread_data *thread_data = x11drv_thread_data();
373 TRACE("xim = %p, p = %p\n", xim, p);
374 thread_data->xim = NULL;
375 ximStyle = 0;
376 wine_tsx11_lock();
377 XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, open_xim_callback, NULL );
378 wine_tsx11_unlock();
381 /***********************************************************************
382 * X11DRV Ime creation
384 * Should always be called with the x11 lock held
386 static BOOL open_xim( Display *display )
388 struct x11drv_thread_data *thread_data = x11drv_thread_data();
389 XIMStyle ximStyleCallback, ximStyleNone;
390 XIMStyles *ximStyles = NULL;
391 INT i;
392 XIM xim;
393 XIMCallback destroy;
395 xim = XOpenIM(display, NULL, NULL, NULL);
396 if (xim == NULL)
398 WARN("Could not open input method.\n");
399 return FALSE;
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);
414 if (ximStyles == 0)
416 WARN("Could not find supported input style.\n");
417 XCloseIM(xim);
418 return FALSE;
420 else
422 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
424 ximStyleRoot = 0;
425 ximStyleNone = 0;
426 ximStyleCallback = 0;
428 for (i = 0; i < ximStyles->count_styles; ++i)
430 int style = ximStyles->supported_styles[i];
431 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
432 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
433 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
434 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
435 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
436 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
437 if (!ximStyle && (ximStyles->supported_styles[i] ==
438 ximStyleRequest))
440 ximStyle = ximStyleRequest;
441 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
443 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
444 STYLE_ROOT))
446 ximStyleRoot = STYLE_ROOT;
447 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
449 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
450 STYLE_CALLBACK))
452 ximStyleCallback = STYLE_CALLBACK;
453 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
455 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
456 STYLE_NONE))
458 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
459 ximStyleNone = STYLE_NONE;
462 XFree(ximStyles);
464 if (ximStyle == 0)
465 ximStyle = ximStyleRoot;
467 if (ximStyle == 0)
468 ximStyle = ximStyleNone;
470 if (ximStyleCallback == 0)
472 TRACE("No callback style avalable\n");
473 ximStyleCallback = ximStyle;
478 thread_data->xim = xim;
480 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0 ||
481 (ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
483 char **list;
484 int count;
485 thread_data->font_set = XCreateFontSet(display, "fixed",
486 &list, &count, NULL);
487 TRACE("ximFontSet = %p\n", thread_data->font_set);
488 TRACE("list = %p, count = %d\n", list, count);
489 if (list != NULL)
491 int i;
492 for (i = 0; i < count; ++i)
493 TRACE("list[%d] = %s\n", i, list[i]);
494 XFreeStringList(list);
497 else
498 thread_data->font_set = NULL;
500 wine_tsx11_unlock();
501 IME_UpdateAssociation(NULL);
502 wine_tsx11_lock();
503 return TRUE;
506 static void open_xim_callback( Display *display, XPointer ptr, XPointer data )
508 if (open_xim( display ))
509 XUnregisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL);
512 void X11DRV_SetupXIM(void)
514 Display *display = thread_display();
516 wine_tsx11_lock();
517 if (!open_xim( display ))
518 XRegisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL );
519 wine_tsx11_unlock();
522 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
524 struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
525 TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
526 win_data->xic = NULL;
527 return TRUE;
531 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
533 XPoint spot = {0};
534 XVaNestedList preedit = NULL;
535 XVaNestedList status = NULL;
536 XIC xic;
537 XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
538 XICCallback P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
539 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
540 Window win = data->whole_window;
541 XFontSet fontSet = x11drv_thread_data()->font_set;
543 TRACE("xim = %p\n", xim);
545 wine_tsx11_lock();
547 /* use complex and slow XIC initialization method only for CJK */
548 if (langid != LANG_CHINESE &&
549 langid != LANG_JAPANESE &&
550 langid != LANG_KOREAN)
552 xic = XCreateIC(xim,
553 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
554 XNClientWindow, win,
555 XNFocusWindow, win,
556 XNDestroyCallback, &destroy,
557 NULL);
558 wine_tsx11_unlock();
559 data->xic = xic;
560 return xic;
563 /* create callbacks */
564 P_StartCB.client_data = NULL;
565 P_DoneCB.client_data = NULL;
566 P_DrawCB.client_data = NULL;
567 P_CaretCB.client_data = NULL;
568 P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
569 P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
570 P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
571 P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
573 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
575 preedit = XVaCreateNestedList(0,
576 XNFontSet, fontSet,
577 XNSpotLocation, &spot,
578 XNPreeditStartCallback, &P_StartCB,
579 XNPreeditDoneCallback, &P_DoneCB,
580 XNPreeditDrawCallback, &P_DrawCB,
581 XNPreeditCaretCallback, &P_CaretCB,
582 NULL);
583 TRACE("preedit = %p\n", preedit);
585 else
587 preedit = XVaCreateNestedList(0,
588 XNPreeditStartCallback, &P_StartCB,
589 XNPreeditDoneCallback, &P_DoneCB,
590 XNPreeditDrawCallback, &P_DrawCB,
591 XNPreeditCaretCallback, &P_CaretCB,
592 NULL);
594 TRACE("preedit = %p\n", preedit);
597 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
599 status = XVaCreateNestedList(0,
600 XNFontSet, fontSet,
601 NULL);
602 TRACE("status = %p\n", status);
605 if (preedit != NULL && status != NULL)
607 xic = XCreateIC(xim,
608 XNInputStyle, ximStyle,
609 XNPreeditAttributes, preedit,
610 XNStatusAttributes, status,
611 XNClientWindow, win,
612 XNFocusWindow, win,
613 XNDestroyCallback, &destroy,
614 NULL);
616 else if (preedit != NULL)
618 xic = XCreateIC(xim,
619 XNInputStyle, ximStyle,
620 XNPreeditAttributes, preedit,
621 XNClientWindow, win,
622 XNFocusWindow, win,
623 XNDestroyCallback, &destroy,
624 NULL);
626 else if (status != NULL)
628 xic = XCreateIC(xim,
629 XNInputStyle, ximStyle,
630 XNStatusAttributes, status,
631 XNClientWindow, win,
632 XNFocusWindow, win,
633 XNDestroyCallback, &destroy,
634 NULL);
636 else
638 xic = XCreateIC(xim,
639 XNInputStyle, ximStyle,
640 XNClientWindow, win,
641 XNFocusWindow, win,
642 XNDestroyCallback, &destroy,
643 NULL);
646 TRACE("xic = %p\n", xic);
647 data->xic = xic;
649 if (preedit != NULL)
650 XFree(preedit);
651 if (status != NULL)
652 XFree(status);
654 wine_tsx11_unlock();
656 return xic;