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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
;
42 static HIMC root_context
;
43 static XIMStyle ximStyle
= 0;
44 static XIMStyle ximStyleRoot
= 0;
46 /* moved here from imm32 for dll separation */
47 static DWORD dwCompStringLength
= 0;
48 static LPBYTE CompositionString
= NULL
;
49 static DWORD dwCompStringSize
= 0;
50 static LPBYTE ResultString
= NULL
;
51 static DWORD dwResultStringSize
= 0;
53 static HMODULE hImmDll
= NULL
;
54 static HIMC (WINAPI
*pImmAssociateContext
)(HWND
,HIMC
);
55 static HIMC (WINAPI
*pImmCreateContext
)(void);
56 static VOID (WINAPI
*pImmSetOpenStatus
)(HIMC
,BOOL
);
57 static BOOL (WINAPI
*pImmSetCompositionString
)(HIMC
, DWORD
, LPWSTR
,
58 DWORD
, LPWSTR
, DWORD
);
59 static VOID (WINAPI
*pImmNotifyIME
)(HIMC
, DWORD
, DWORD
, DWORD
);
61 /* WINE specific messages from the xim in x11drv level */
63 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
64 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
65 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
66 /* this uses all the callbacks to utilize full IME support */
67 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
68 /* inorder to enable deadkey support */
69 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
72 * here are the functions that sort of marshall calls into IMM32.DLL
74 static void LoadImmDll()
76 hImmDll
= LoadLibraryA("imm32.dll");
78 pImmAssociateContext
= (void *)GetProcAddress(hImmDll
, "ImmAssociateContext");
79 if (!pImmAssociateContext
)
80 WARN("IMM: pImmAssociateContext not found in DLL\n");
82 pImmCreateContext
= (void *)GetProcAddress(hImmDll
, "ImmCreateContext");
83 if (!pImmCreateContext
)
84 WARN("IMM: pImmCreateContext not found in DLL\n");
86 pImmSetOpenStatus
= (void *)GetProcAddress( hImmDll
, "ImmSetOpenStatus");
87 if (!pImmSetOpenStatus
)
88 WARN("IMM: pImmSetOpenStatus not found in DLL\n");
90 pImmSetCompositionString
=(void *)GetProcAddress(hImmDll
, "ImmSetCompositionStringW");
92 if (!pImmSetCompositionString
)
93 WARN("IMM: pImmSetCompositionStringW not found in DLL\n");
95 pImmNotifyIME
= (void *)GetProcAddress( hImmDll
, "ImmNotifyIME");
98 WARN("IMM: pImmNotifyIME not found in DLL\n");
101 static BOOL
X11DRV_ImmSetInternalString(DWORD dwIndex
, DWORD dwOffset
,
102 DWORD selLength
, LPWSTR lpComp
, DWORD dwCompLen
)
104 /* Composition strings are edited in chunks */
105 unsigned int byte_length
= dwCompLen
* sizeof(WCHAR
);
106 unsigned int byte_offset
= dwOffset
* sizeof(WCHAR
);
107 unsigned int byte_selection
= selLength
* sizeof(WCHAR
);
110 TRACE("( %li, %li, %ld, %p, %ld):\n", dwOffset
, selLength
, dwIndex
, lpComp
,
113 if (dwIndex
== GCS_COMPSTR
)
119 if ((dwCompLen
== 0) && (selLength
== 0))
123 /* deletion occurred */
124 else if ((dwCompLen
== 0) && (selLength
!= 0))
126 if (dwCompStringLength
)
128 for (i
= 0; i
< byte_selection
; i
++)
130 if (byte_offset
+byte_selection
+i
<
133 CompositionString
[byte_offset
+ i
] =
134 CompositionString
[byte_offset
+ byte_selection
+ i
];
137 CompositionString
[byte_offset
+ i
] = 0;
139 /* clean up the end */
140 dwCompStringLength
-= byte_selection
;
142 i
= dwCompStringLength
;
143 while (i
< dwCompStringSize
)
145 CompositionString
[i
++] = 0;
151 int byte_expansion
= byte_length
- byte_selection
;
153 if (byte_expansion
+ dwCompStringLength
>= dwCompStringSize
)
155 if (CompositionString
)
157 HeapReAlloc(GetProcessHeap(), 0,
163 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize
+
166 memset(&(CompositionString
[dwCompStringSize
]), byte_expansion
,
169 dwCompStringSize
+= byte_expansion
;
172 ptr_new
= ((LPBYTE
)lpComp
);
173 ptr_old
= CompositionString
+ byte_offset
+ byte_selection
;
175 dwCompStringLength
+= byte_expansion
;
177 for (j
=0,i
= byte_offset
; i
< dwCompStringSize
; i
++)
181 CompositionString
[i
] = ptr_new
[j
++];
185 if (ptr_old
< CompositionString
+ dwCompStringSize
)
187 CompositionString
[i
] = *ptr_old
;
191 CompositionString
[i
] = 0;
196 if (pImmSetCompositionString
)
197 rc
= pImmSetCompositionString((HIMC
)FROM_IME
, SCS_SETSTR
,
198 (LPWSTR
)CompositionString
, dwCompStringLength
,
201 else if ((dwIndex
== GCS_RESULTSTR
) && (lpComp
) && (dwCompLen
))
203 if (dwResultStringSize
)
204 HeapFree(GetProcessHeap(),0,ResultString
);
205 dwResultStringSize
= byte_length
;
206 ResultString
= HeapAlloc(GetProcessHeap(),0,byte_length
);
207 memcpy(ResultString
,lpComp
,byte_length
);
209 if (pImmSetCompositionString
)
210 rc
= pImmSetCompositionString((HIMC
)FROM_IME
, SCS_SETSTR
,
211 (LPWSTR
)ResultString
, dwResultStringSize
,
215 pImmNotifyIME((HIMC
)FROM_IME
, NI_COMPOSITIONSTR
, CPS_COMPLETE
, 0);
221 void X11DRV_XIMLookupChars( const char *str
, DWORD count
)
227 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0, str
, count
, wcOutput
, sizeof(wcOutput
));
229 if (pImmAssociateContext
&& (focus
= GetFocus()))
230 pImmAssociateContext(focus
,root_context
);
232 X11DRV_ImmSetInternalString(GCS_RESULTSTR
,0,0,wcOutput
,dwOutput
);
235 static void X11DRV_ImmSetOpenStatus(BOOL fOpen
)
239 if (dwCompStringSize
)
240 HeapFree(GetProcessHeap(),0,CompositionString
);
242 dwCompStringSize
= 0;
243 dwCompStringLength
= 0;
244 CompositionString
= NULL
;
246 if (dwResultStringSize
)
247 HeapFree(GetProcessHeap(),0,ResultString
);
249 dwResultStringSize
= 0;
253 if (pImmSetOpenStatus
)
254 pImmSetOpenStatus((HIMC
)FROM_IME
,fOpen
);
257 static int XIMPreEditStartCallback(XIC ic
, XPointer client_data
, XPointer call_data
)
259 TRACE("PreEditStartCallback %p\n",ic
);
260 X11DRV_ImmSetOpenStatus(TRUE
);
261 ximInComposeMode
= TRUE
;
265 static void XIMPreEditDoneCallback(XIC ic
, XPointer client_data
, XPointer call_data
)
267 TRACE("PreeditDoneCallback %p\n",ic
);
268 ximInComposeMode
= FALSE
;
269 X11DRV_ImmSetOpenStatus(FALSE
);
272 static void XIMPreEditDrawCallback(XIM ic
, XPointer client_data
,
273 XIMPreeditDrawCallbackStruct
*P_DR
)
278 TRACE("PreEditDrawCallback %p\n",ic
);
282 int sel
= P_DR
->chg_first
;
283 int len
= P_DR
->chg_length
;
286 if (! P_DR
->text
->encoding_is_wchar
)
288 TRACE("multibyte\n");
289 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0,
290 P_DR
->text
->string
.multi_byte
, -1,
295 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
, wcOutput
, dwOutput
);
299 FIXME("wchar PROBIBILY WRONG\n");
300 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
,
301 (LPWSTR
)P_DR
->text
->string
.wide_char
,
306 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
, NULL
, 0);
311 static void XIMPreEditCaretCallback(XIC ic
, XPointer client_data
,
312 XIMPreeditCaretCallbackStruct
*P_C
)
314 FIXME("PreeditCaretCalback %p\n",ic
);
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 /***********************************************************************
332 * X11DRV Ime creation
334 XIM
X11DRV_SetupXIM(Display
*display
, const char *input_style
)
336 XIMStyle ximStyleRequest
, ximStyleCallback
, ximStyleNone
;
337 XIMStyles
*ximStyles
= NULL
;
341 ximStyleRequest
= STYLE_CALLBACK
;
343 if (!strcasecmp(input_style
, "offthespot"))
344 ximStyleRequest
= STYLE_OFFTHESPOT
;
345 else if (!strcasecmp(input_style
, "overthespot"))
346 ximStyleRequest
= STYLE_OVERTHESPOT
;
347 else if (!strcasecmp(input_style
, "root"))
348 ximStyleRequest
= STYLE_ROOT
;
352 if(!XSupportsLocale())
354 WARN("X does not support locale.\n");
357 if(XSetLocaleModifiers("") == NULL
)
359 WARN("Could not set locale modifiers.\n");
363 xim
= XOpenIM(display
, NULL
, NULL
, NULL
);
366 WARN("Could not open input method.\n");
370 TRACE("X display of IM = %p\n", XDisplayOfIM(xim
));
371 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim
));
373 XGetIMValues(xim
, XNQueryInputStyle
, &ximStyles
, NULL
);
376 WARN("Could not find supported input style.\n");
380 TRACE("ximStyles->count_styles = %d\n", ximStyles
->count_styles
);
384 ximStyleCallback
= 0;
386 for (i
= 0; i
< ximStyles
->count_styles
; ++i
)
388 int style
= ximStyles
->supported_styles
[i
];
389 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i
,
390 (style
&XIMPreeditArea
)?"XIMPreeditArea ":"",
391 (style
&XIMPreeditCallbacks
)?"XIMPreeditCallbacks ":"",
392 (style
&XIMPreeditPosition
)?"XIMPreeditPosition ":"",
393 (style
&XIMPreeditNothing
)?"XIMPreeditNothing ":"",
394 (style
&XIMPreeditNone
)?"XIMPreeditNone ":"");
395 if (!ximStyle
&& (ximStyles
->supported_styles
[i
] ==
398 ximStyle
= ximStyleRequest
;
399 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
401 else if (!ximStyleRoot
&&(ximStyles
->supported_styles
[i
] ==
404 ximStyleRoot
= STYLE_ROOT
;
405 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
407 else if (!ximStyleCallback
&&(ximStyles
->supported_styles
[i
] ==
410 ximStyleCallback
= STYLE_CALLBACK
;
411 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
413 else if (!ximStyleNone
&& (ximStyles
->supported_styles
[i
] ==
416 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
417 ximStyleNone
= STYLE_NONE
;
423 ximStyle
= ximStyleRoot
;
426 ximStyle
= ximStyleNone
;
428 if (ximStyleCallback
== 0)
430 TRACE("No callback style avalable\n");
431 ximStyleCallback
= ximStyle
;
440 if (pImmCreateContext
)
442 root_context
= pImmCreateContext();
443 if (pImmAssociateContext
)
444 pImmAssociateContext(0,root_context
);
455 XIC
X11DRV_CreateIC(XIM xim
, Display
*display
, Window win
)
461 XVaNestedList preedit
= NULL
;
462 XVaNestedList status
= NULL
;
464 XIMCallback P_StartCB
;
465 XIMCallback P_DoneCB
;
466 XIMCallback P_DrawCB
;
467 XIMCallback P_CaretCB
;
468 LANGID langid
= PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
472 /* use complex and slow XIC initialization method only for CJK */
473 if (langid
!= LANG_CHINESE
&&
474 langid
!= LANG_JAPANESE
&&
475 langid
!= LANG_KOREAN
)
478 XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
486 fontSet
= XCreateFontSet(display
,
488 &list
, &count
, NULL
);
490 TRACE("ximFontSet = 0x%x\n", (unsigned int) fontSet
);
491 TRACE("list = 0x%x, count = %d\n", (unsigned int) list
, count
);
497 for (i
= 0; i
< count
; ++i
)
499 TRACE("list[%d] = %s\n", i
, list
[i
]);
501 XFreeStringList(list
);
504 /* create callbacks */
505 P_StartCB
.client_data
= NULL
;
506 P_StartCB
.callback
= (XIMProc
)XIMPreEditStartCallback
;
507 P_DoneCB
.client_data
= NULL
;
508 P_DoneCB
.callback
= (XIMProc
)XIMPreEditDoneCallback
;
509 P_DrawCB
.client_data
= NULL
;
510 P_DrawCB
.callback
= (XIMProc
)XIMPreEditDrawCallback
;
511 P_CaretCB
.client_data
= NULL
;
512 P_CaretCB
.callback
= (XIMProc
)XIMPreEditCaretCallback
;
514 if ((ximStyle
& (XIMPreeditNothing
| XIMPreeditNone
)) == 0)
516 preedit
= XVaCreateNestedList(0,
518 XNSpotLocation
, &spot
,
519 XNPreeditStartCallback
, &P_StartCB
,
520 XNPreeditDoneCallback
, &P_DoneCB
,
521 XNPreeditDrawCallback
, &P_DrawCB
,
522 XNPreeditCaretCallback
, &P_CaretCB
,
524 TRACE("preedit = 0x%x\n", (unsigned int) preedit
);
528 preedit
= XVaCreateNestedList(0,
529 XNPreeditStartCallback
, &P_StartCB
,
530 XNPreeditDoneCallback
, &P_DoneCB
,
531 XNPreeditDrawCallback
, &P_DrawCB
,
532 XNPreeditCaretCallback
, &P_CaretCB
,
535 TRACE("preedit = 0x%x\n", (unsigned int) preedit
);
538 if ((ximStyle
& (XIMStatusNothing
| XIMStatusNone
)) == 0)
540 status
= XVaCreateNestedList(0,
543 TRACE("status = 0x%x\n", (unsigned int) status
);
546 if (preedit
!= NULL
&& status
!= NULL
)
549 XNInputStyle
, ximStyle
,
550 XNPreeditAttributes
, preedit
,
551 XNStatusAttributes
, status
,
556 else if (preedit
!= NULL
)
559 XNInputStyle
, ximStyle
,
560 XNPreeditAttributes
, preedit
,
565 else if (status
!= NULL
)
568 XNInputStyle
, ximStyle
,
569 XNStatusAttributes
, status
,
577 XNInputStyle
, ximStyle
,
583 TRACE("xic = 0x%x\n", (unsigned int) xic
);