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 /* this must match with imm32/imm.c */
38 #define FROM_IME 0xcafe1337
40 BOOL ximInComposeMode
=FALSE
;
43 extern void MAC_SetupXIM();
46 static HIMC root_context
;
47 static XIMStyle ximStyle
= 0;
48 static XIMStyle ximStyleRoot
= 0;
50 /* moved here from imm32 for dll separation */
51 static DWORD dwCompStringLength
= 0;
52 static LPBYTE CompositionString
= NULL
;
53 static DWORD dwCompStringSize
= 0;
54 static LPBYTE ResultString
= NULL
;
55 static DWORD dwResultStringSize
= 0;
57 static HMODULE hImmDll
= NULL
;
58 HIMC (WINAPI
*pImmAssociateContext
)(HWND
,HIMC
);
59 HIMC (WINAPI
*pImmCreateContext
)(void);
60 VOID (WINAPI
*pImmSetOpenStatus
)(HIMC
,BOOL
);
61 BOOL (WINAPI
*pImmSetCompositionString
)(HIMC
, DWORD
, LPWSTR
,
62 DWORD
, LPWSTR
, DWORD
);
63 VOID (WINAPI
*pImmNotifyIME
)(HIMC
, DWORD
, DWORD
, DWORD
);
65 /* WINE specific messages from the xim in x11drv level */
67 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
68 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
69 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
70 /* this uses all the callbacks to utilize full IME support */
71 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
72 /* inorder to enable deadkey support */
73 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
76 * here are the functions that sort of marshall calls into IMM32.DLL
78 static void LoadImmDll(void)
80 hImmDll
= LoadLibraryA("imm32.dll");
82 pImmAssociateContext
= (void *)GetProcAddress(hImmDll
, "ImmAssociateContext");
83 if (!pImmAssociateContext
)
84 WARN("IMM: pImmAssociateContext not found in DLL\n");
86 pImmCreateContext
= (void *)GetProcAddress(hImmDll
, "ImmCreateContext");
87 if (!pImmCreateContext
)
88 WARN("IMM: pImmCreateContext not found in DLL\n");
90 pImmSetOpenStatus
= (void *)GetProcAddress( hImmDll
, "ImmSetOpenStatus");
91 if (!pImmSetOpenStatus
)
92 WARN("IMM: pImmSetOpenStatus not found in DLL\n");
94 pImmSetCompositionString
=(void *)GetProcAddress(hImmDll
, "ImmSetCompositionStringW");
96 if (!pImmSetCompositionString
)
97 WARN("IMM: pImmSetCompositionStringW not found in DLL\n");
99 pImmNotifyIME
= (void *)GetProcAddress( hImmDll
, "ImmNotifyIME");
102 WARN("IMM: pImmNotifyIME not found in DLL\n");
105 static BOOL
X11DRV_ImmSetInternalString(DWORD dwIndex
, DWORD dwOffset
,
106 DWORD selLength
, LPWSTR lpComp
, DWORD dwCompLen
)
108 /* Composition strings are edited in chunks */
109 unsigned int byte_length
= dwCompLen
* sizeof(WCHAR
);
110 unsigned int byte_offset
= dwOffset
* sizeof(WCHAR
);
111 unsigned int byte_selection
= selLength
* sizeof(WCHAR
);
114 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset
, selLength
, dwIndex
, lpComp
, dwCompLen
);
116 if (dwIndex
== GCS_COMPSTR
)
122 if ((dwCompLen
== 0) && (selLength
== 0))
126 /* deletion occurred */
127 else if ((dwCompLen
== 0) && (selLength
!= 0))
129 if (dwCompStringLength
)
131 for (i
= 0; i
< byte_selection
; i
++)
133 if (byte_offset
+byte_selection
+i
<
136 CompositionString
[byte_offset
+ i
] =
137 CompositionString
[byte_offset
+ byte_selection
+ i
];
140 CompositionString
[byte_offset
+ i
] = 0;
142 /* clean up the end */
143 dwCompStringLength
-= byte_selection
;
145 i
= dwCompStringLength
;
146 while (i
< dwCompStringSize
)
148 CompositionString
[i
++] = 0;
154 int byte_expansion
= byte_length
- byte_selection
;
156 if (byte_expansion
+ dwCompStringLength
>= dwCompStringSize
)
158 if (CompositionString
)
160 HeapReAlloc(GetProcessHeap(), 0,
166 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize
+
169 memset(&(CompositionString
[dwCompStringSize
]), 0,
172 dwCompStringSize
+= byte_expansion
;
175 ptr_new
= ((LPBYTE
)lpComp
);
176 ptr_old
= CompositionString
+ byte_offset
+ byte_selection
;
178 dwCompStringLength
+= byte_expansion
;
180 for (j
=0,i
= byte_offset
; i
< dwCompStringSize
; i
++)
184 CompositionString
[i
] = ptr_new
[j
++];
188 if (ptr_old
< CompositionString
+ dwCompStringSize
)
190 CompositionString
[i
] = *ptr_old
;
194 CompositionString
[i
] = 0;
199 if (pImmSetCompositionString
)
200 rc
= pImmSetCompositionString((HIMC
)FROM_IME
, SCS_SETSTR
,
201 (LPWSTR
)CompositionString
, dwCompStringLength
,
204 else if ((dwIndex
== GCS_RESULTSTR
) && (lpComp
) && (dwCompLen
))
206 if (dwResultStringSize
)
207 HeapFree(GetProcessHeap(),0,ResultString
);
208 dwResultStringSize
= byte_length
;
209 ResultString
= HeapAlloc(GetProcessHeap(),0,byte_length
);
210 memcpy(ResultString
,lpComp
,byte_length
);
212 if (pImmSetCompositionString
)
213 rc
= pImmSetCompositionString((HIMC
)FROM_IME
, SCS_SETSTR
,
214 (LPWSTR
)ResultString
, dwResultStringSize
,
218 pImmNotifyIME((HIMC
)FROM_IME
, NI_COMPOSITIONSTR
, CPS_COMPLETE
, 0);
224 void X11DRV_XIMLookupChars( const char *str
, DWORD count
)
230 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0, str
, count
, wcOutput
, sizeof(wcOutput
)/sizeof(WCHAR
));
232 if (pImmAssociateContext
&& (focus
= GetFocus()))
233 pImmAssociateContext(focus
,root_context
);
235 X11DRV_ImmSetInternalString(GCS_RESULTSTR
,0,0,wcOutput
,dwOutput
);
238 static void X11DRV_ImmSetOpenStatus(BOOL fOpen
)
242 if (dwCompStringSize
)
243 HeapFree(GetProcessHeap(),0,CompositionString
);
245 dwCompStringSize
= 0;
246 dwCompStringLength
= 0;
247 CompositionString
= NULL
;
249 if (dwResultStringSize
)
250 HeapFree(GetProcessHeap(),0,ResultString
);
252 dwResultStringSize
= 0;
256 if (pImmSetOpenStatus
)
257 pImmSetOpenStatus((HIMC
)FROM_IME
,fOpen
);
260 static int XIMPreEditStartCallback(XIC ic
, XPointer client_data
, XPointer call_data
)
262 TRACE("PreEditStartCallback %p\n",ic
);
263 X11DRV_ImmSetOpenStatus(TRUE
);
264 ximInComposeMode
= TRUE
;
268 static void XIMPreEditDoneCallback(XIC ic
, XPointer client_data
, XPointer call_data
)
270 TRACE("PreeditDoneCallback %p\n",ic
);
271 ximInComposeMode
= FALSE
;
272 X11DRV_ImmSetOpenStatus(FALSE
);
275 static void XIMPreEditDrawCallback(XIM ic
, XPointer client_data
,
276 XIMPreeditDrawCallbackStruct
*P_DR
)
281 TRACE("PreEditDrawCallback %p\n",ic
);
285 int sel
= P_DR
->chg_first
;
286 int len
= P_DR
->chg_length
;
289 if (! P_DR
->text
->encoding_is_wchar
)
291 TRACE("multibyte\n");
292 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0,
293 P_DR
->text
->string
.multi_byte
, -1,
298 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
, wcOutput
, dwOutput
);
302 FIXME("wchar PROBIBILY WRONG\n");
303 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
,
304 (LPWSTR
)P_DR
->text
->string
.wide_char
,
309 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
, NULL
, 0);
314 static void XIMPreEditCaretCallback(XIC ic
, XPointer client_data
,
315 XIMPreeditCaretCallbackStruct
*P_C
)
317 FIXME("PreeditCaretCalback %p\n",ic
);
320 void X11DRV_ForceXIMReset(HWND hwnd
)
322 XIC ic
= X11DRV_get_ic(hwnd
);
326 TRACE("Forcing Reset %p\n",ic
);
328 leftover
= XmbResetIC(ic
);
334 /***********************************************************************
335 * X11DRV Ime creation
337 XIM
X11DRV_SetupXIM(Display
*display
, const char *input_style
)
339 XIMStyle ximStyleRequest
, ximStyleCallback
, ximStyleNone
;
340 XIMStyles
*ximStyles
= NULL
;
344 ximStyleRequest
= STYLE_CALLBACK
;
351 if (pImmCreateContext
)
353 root_context
= pImmCreateContext();
354 if (pImmAssociateContext
)
355 pImmAssociateContext(0,root_context
);
362 if (!strcasecmp(input_style
, "offthespot"))
363 ximStyleRequest
= STYLE_OFFTHESPOT
;
364 else if (!strcasecmp(input_style
, "overthespot"))
365 ximStyleRequest
= STYLE_OVERTHESPOT
;
366 else if (!strcasecmp(input_style
, "root"))
367 ximStyleRequest
= STYLE_ROOT
;
371 if(!XSupportsLocale())
373 WARN("X does not support locale.\n");
376 if(XSetLocaleModifiers("") == NULL
)
378 WARN("Could not set locale modifiers.\n");
382 xim
= XOpenIM(display
, NULL
, NULL
, NULL
);
385 WARN("Could not open input method.\n");
389 TRACE("X display of IM = %p\n", XDisplayOfIM(xim
));
390 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim
));
392 XGetIMValues(xim
, XNQueryInputStyle
, &ximStyles
, NULL
);
395 WARN("Could not find supported input style.\n");
399 TRACE("ximStyles->count_styles = %d\n", ximStyles
->count_styles
);
403 ximStyleCallback
= 0;
405 for (i
= 0; i
< ximStyles
->count_styles
; ++i
)
407 int style
= ximStyles
->supported_styles
[i
];
408 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i
,
409 (style
&XIMPreeditArea
)?"XIMPreeditArea ":"",
410 (style
&XIMPreeditCallbacks
)?"XIMPreeditCallbacks ":"",
411 (style
&XIMPreeditPosition
)?"XIMPreeditPosition ":"",
412 (style
&XIMPreeditNothing
)?"XIMPreeditNothing ":"",
413 (style
&XIMPreeditNone
)?"XIMPreeditNone ":"");
414 if (!ximStyle
&& (ximStyles
->supported_styles
[i
] ==
417 ximStyle
= ximStyleRequest
;
418 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
420 else if (!ximStyleRoot
&&(ximStyles
->supported_styles
[i
] ==
423 ximStyleRoot
= STYLE_ROOT
;
424 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
426 else if (!ximStyleCallback
&&(ximStyles
->supported_styles
[i
] ==
429 ximStyleCallback
= STYLE_CALLBACK
;
430 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
432 else if (!ximStyleNone
&& (ximStyles
->supported_styles
[i
] ==
435 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
436 ximStyleNone
= STYLE_NONE
;
442 ximStyle
= ximStyleRoot
;
445 ximStyle
= ximStyleNone
;
447 if (ximStyleCallback
== 0)
449 TRACE("No callback style avalable\n");
450 ximStyleCallback
= ximStyle
;
461 if (pImmCreateContext
)
463 root_context
= pImmCreateContext();
464 if (pImmAssociateContext
)
465 pImmAssociateContext(0,root_context
);
477 XIC
X11DRV_CreateIC(XIM xim
, Display
*display
, Window win
)
479 XFontSet fontSet
= NULL
;
483 XVaNestedList preedit
= NULL
;
484 XVaNestedList status
= NULL
;
486 XIMCallback P_StartCB
;
487 XIMCallback P_DoneCB
;
488 XIMCallback P_DrawCB
;
489 XIMCallback P_CaretCB
;
490 LANGID langid
= PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
494 /* use complex and slow XIC initialization method only for CJK */
495 if (langid
!= LANG_CHINESE
&&
496 langid
!= LANG_JAPANESE
&&
497 langid
!= LANG_KOREAN
)
500 XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
509 if (((ximStyle
& (XIMPreeditNothing
| XIMPreeditNone
)) == 0) ||
510 ((ximStyle
& (XIMStatusNothing
| XIMStatusNone
)) == 0))
512 fontSet
= XCreateFontSet(display
,
514 &list
, &count
, NULL
);
516 TRACE("ximFontSet = %p\n", fontSet
);
517 TRACE("list = %p, count = %d\n", list
, count
);
523 for (i
= 0; i
< count
; ++i
)
525 TRACE("list[%d] = %s\n", i
, list
[i
]);
527 XFreeStringList(list
);
531 /* create callbacks */
532 P_StartCB
.client_data
= NULL
;
533 P_StartCB
.callback
= (XIMProc
)XIMPreEditStartCallback
;
534 P_DoneCB
.client_data
= NULL
;
535 P_DoneCB
.callback
= (XIMProc
)XIMPreEditDoneCallback
;
536 P_DrawCB
.client_data
= NULL
;
537 P_DrawCB
.callback
= (XIMProc
)XIMPreEditDrawCallback
;
538 P_CaretCB
.client_data
= NULL
;
539 P_CaretCB
.callback
= (XIMProc
)XIMPreEditCaretCallback
;
541 if ((ximStyle
& (XIMPreeditNothing
| XIMPreeditNone
)) == 0)
543 preedit
= XVaCreateNestedList(0,
545 XNSpotLocation
, &spot
,
546 XNPreeditStartCallback
, &P_StartCB
,
547 XNPreeditDoneCallback
, &P_DoneCB
,
548 XNPreeditDrawCallback
, &P_DrawCB
,
549 XNPreeditCaretCallback
, &P_CaretCB
,
551 TRACE("preedit = %p\n", preedit
);
555 preedit
= XVaCreateNestedList(0,
556 XNPreeditStartCallback
, &P_StartCB
,
557 XNPreeditDoneCallback
, &P_DoneCB
,
558 XNPreeditDrawCallback
, &P_DrawCB
,
559 XNPreeditCaretCallback
, &P_CaretCB
,
562 TRACE("preedit = %p\n", preedit
);
565 if ((ximStyle
& (XIMStatusNothing
| XIMStatusNone
)) == 0)
567 status
= XVaCreateNestedList(0,
570 TRACE("status = %p\n", status
);
573 if (preedit
!= NULL
&& status
!= NULL
)
576 XNInputStyle
, ximStyle
,
577 XNPreeditAttributes
, preedit
,
578 XNStatusAttributes
, status
,
583 else if (preedit
!= NULL
)
586 XNInputStyle
, ximStyle
,
587 XNPreeditAttributes
, preedit
,
592 else if (status
!= NULL
)
595 XNInputStyle
, ximStyle
,
596 XNStatusAttributes
, status
,
604 XNInputStyle
, ximStyle
,
610 TRACE("xic = %p\n", xic
);