Added Finnish resources.
[wine] / dlls / x11drv / xim.c
1 /*
2  * Functions for further XIM control
3  *
4  * Copyright 2003 CodeWeavers, Aric Stewart
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25
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"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
36
37 /* this must match with imm32/imm.c */
38 #define FROM_IME 0xcafe1337
39
40 BOOL ximInComposeMode=FALSE;
41
42 static HIMC root_context;
43 static XIMStyle ximStyle = 0;
44 static XIMStyle ximStyleRoot = 0;
45
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;
52
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);
60
61 /* WINE specific messages from the xim in x11drv level */
62
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)
70
71 /*
72  * here are the functions that sort of marshall calls into IMM32.DLL
73  */
74 static void LoadImmDll(void)
75 {
76     hImmDll = LoadLibraryA("imm32.dll");
77
78     pImmAssociateContext = (void *)GetProcAddress(hImmDll, "ImmAssociateContext");
79     if (!pImmAssociateContext)
80         WARN("IMM: pImmAssociateContext not found in DLL\n");
81
82     pImmCreateContext = (void *)GetProcAddress(hImmDll, "ImmCreateContext");
83     if (!pImmCreateContext)
84         WARN("IMM: pImmCreateContext not found in DLL\n");
85
86     pImmSetOpenStatus = (void *)GetProcAddress( hImmDll, "ImmSetOpenStatus");
87     if (!pImmSetOpenStatus)
88         WARN("IMM: pImmSetOpenStatus not found in DLL\n");
89
90     pImmSetCompositionString =(void *)GetProcAddress(hImmDll, "ImmSetCompositionStringW");
91
92     if (!pImmSetCompositionString)
93         WARN("IMM: pImmSetCompositionStringW not found in DLL\n");
94
95     pImmNotifyIME = (void *)GetProcAddress( hImmDll, "ImmNotifyIME");
96
97     if (!pImmNotifyIME)
98         WARN("IMM: pImmNotifyIME not found in DLL\n");
99 }
100
101 static BOOL X11DRV_ImmSetInternalString(DWORD dwIndex, DWORD dwOffset,
102                                         DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
103 {
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);
108     BOOL rc = FALSE;
109
110     TRACE("( %li, %li, %ld, %p, %ld):\n", dwOffset, selLength, dwIndex, lpComp,
111                                         dwCompLen );
112
113     if (dwIndex == GCS_COMPSTR)
114     {
115         unsigned int i,j;
116         LPBYTE ptr_new;
117         LPBYTE ptr_old;
118
119         if ((dwCompLen == 0) && (selLength == 0))
120         {
121             /* DO Nothing */
122         }
123         /* deletion occurred */
124         else if ((dwCompLen== 0) && (selLength != 0))
125         {
126             if (dwCompStringLength)
127             {
128                 for (i = 0; i < byte_selection; i++)
129                 {
130                     if (byte_offset+byte_selection+i <
131                         dwCompStringLength)
132                     {
133                         CompositionString[byte_offset + i] =
134                         CompositionString[byte_offset + byte_selection + i];
135                     }
136                     else
137                         CompositionString[byte_offset + i] = 0;
138                 }
139                 /* clean up the end */
140                 dwCompStringLength -= byte_selection;
141
142                 i = dwCompStringLength;
143                 while (i < dwCompStringSize)
144                 {
145                     CompositionString[i++] = 0;
146                 }
147             }
148         }
149         else
150         {
151             int byte_expansion = byte_length - byte_selection;
152
153             if (byte_expansion + dwCompStringLength >= dwCompStringSize)
154             {
155                 if (CompositionString)
156                     CompositionString =
157                         HeapReAlloc(GetProcessHeap(), 0,
158                                     CompositionString,
159                                     dwCompStringSize +
160                                     byte_expansion);
161                 else
162                      CompositionString =
163                         HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
164                                     byte_expansion);
165
166                 memset(&(CompositionString[dwCompStringSize]), byte_expansion,
167                         0);
168
169                 dwCompStringSize += byte_expansion;
170             }
171
172             ptr_new =  ((LPBYTE)lpComp);
173             ptr_old = CompositionString + byte_offset + byte_selection;
174
175             dwCompStringLength += byte_expansion;
176
177             for (j=0,i = byte_offset; i < dwCompStringSize; i++)
178             {
179                 if (j < byte_length)
180                 {
181                     CompositionString[i] = ptr_new[j++];
182                 }
183                 else
184                 {
185                     if (ptr_old < CompositionString + dwCompStringSize)
186                     {
187                         CompositionString[i] = *ptr_old;
188                         ptr_old++;
189                             }
190                     else
191                         CompositionString[i] = 0;
192                 }
193             }
194         }
195
196         if (pImmSetCompositionString)
197             rc = pImmSetCompositionString((HIMC)FROM_IME, SCS_SETSTR,
198                                  (LPWSTR)CompositionString, dwCompStringLength,
199                                   NULL, 0);
200     }
201     else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
202     {
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);
208
209         if (pImmSetCompositionString)
210             rc = pImmSetCompositionString((HIMC)FROM_IME, SCS_SETSTR,
211                                  (LPWSTR)ResultString, dwResultStringSize,
212                                   NULL, 0);
213
214         if (pImmNotifyIME)
215             pImmNotifyIME((HIMC)FROM_IME, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
216     }
217
218     return rc;
219 }
220
221 void X11DRV_XIMLookupChars( const char *str, DWORD count )
222 {
223     DWORD dwOutput;
224     WCHAR wcOutput[64];
225     HWND focus;
226
227     dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, sizeof(wcOutput)/sizeof(WCHAR));
228
229     if (pImmAssociateContext && (focus = GetFocus()))
230         pImmAssociateContext(focus,root_context);
231
232     X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
233 }
234
235 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
236 {
237     if (fOpen == FALSE)
238     {
239         if (dwCompStringSize)
240             HeapFree(GetProcessHeap(),0,CompositionString);
241
242         dwCompStringSize = 0;
243         dwCompStringLength = 0;
244         CompositionString = NULL;
245
246         if (dwResultStringSize)
247             HeapFree(GetProcessHeap(),0,ResultString);
248
249         dwResultStringSize = 0;
250         ResultString = NULL;
251     }
252
253     if (pImmSetOpenStatus)
254         pImmSetOpenStatus((HIMC)FROM_IME,fOpen);
255 }
256
257 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
258 {
259     TRACE("PreEditStartCallback %p\n",ic);
260     X11DRV_ImmSetOpenStatus(TRUE);
261     ximInComposeMode = TRUE;
262     return -1;
263 }
264
265 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
266 {
267     TRACE("PreeditDoneCallback %p\n",ic);
268     ximInComposeMode = FALSE;
269     X11DRV_ImmSetOpenStatus(FALSE);
270 }
271
272 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
273                                    XIMPreeditDrawCallbackStruct *P_DR)
274 {
275     DWORD dwOutput;
276     WCHAR wcOutput[64];
277
278     TRACE("PreEditDrawCallback %p\n",ic);
279
280     if (P_DR)
281     {
282         int sel = P_DR->chg_first;
283         int len = P_DR->chg_length;
284         if (P_DR->text)
285         {
286             if (! P_DR->text->encoding_is_wchar)
287             {
288                 TRACE("multibyte\n");
289                 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
290                            P_DR->text->string.multi_byte, -1,
291                            wcOutput, 64);
292
293                 /* ignore null */
294                 dwOutput --;
295                 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
296             }
297             else
298             {
299                 FIXME("wchar PROBIBILY WRONG\n");
300                 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
301                                              (LPWSTR)P_DR->text->string.wide_char,
302                                              P_DR->text->length);
303             }
304         }
305         else
306             X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
307     }
308     TRACE("Finished\n");
309 }
310
311 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
312                                     XIMPreeditCaretCallbackStruct *P_C)
313 {
314     FIXME("PreeditCaretCalback %p\n",ic);
315 }
316
317 void X11DRV_ForceXIMReset(HWND hwnd)
318 {
319     XIC ic = X11DRV_get_ic(hwnd);
320     if (ic)
321     {
322         char* leftover;
323         TRACE("Forcing Reset %p\n",ic);
324         wine_tsx11_lock();
325         leftover = XmbResetIC(ic);
326         XFree(leftover);
327         wine_tsx11_unlock();
328     }
329 }
330
331 /***********************************************************************
332 *           X11DRV Ime creation
333 */
334 XIM X11DRV_SetupXIM(Display *display, const char *input_style)
335 {
336     XIMStyle ximStyleRequest, ximStyleCallback, ximStyleNone;
337     XIMStyles *ximStyles = NULL;
338     INT i;
339     XIM xim;
340
341     ximStyleRequest = STYLE_CALLBACK;
342
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;
349
350     wine_tsx11_lock();
351
352     if(!XSupportsLocale())
353     {
354         WARN("X does not support locale.\n");
355         goto err;
356     }
357     if(XSetLocaleModifiers("") == NULL)
358     {
359         WARN("Could not set locale modifiers.\n");
360         goto err;
361     }
362
363     xim = XOpenIM(display, NULL, NULL, NULL);
364     if (xim == NULL)
365     {
366         WARN("Could not open input method.\n");
367         goto err;
368     }
369
370     TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
371     TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
372
373     XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
374     if (ximStyles == 0)
375     {
376         WARN("Could not find supported input style.\n");
377     }
378     else
379     {
380         TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
381
382         ximStyleRoot = 0;
383         ximStyleNone = 0;
384         ximStyleCallback = 0;
385
386         for (i = 0; i < ximStyles->count_styles; ++i)
387         {
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] ==
396                                 ximStyleRequest))
397             {
398                 ximStyle = ximStyleRequest;
399                 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
400             }
401             else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
402                      STYLE_ROOT))
403             {
404                 ximStyleRoot = STYLE_ROOT;
405                 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
406             }
407             else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
408                      STYLE_CALLBACK))
409             {
410                 ximStyleCallback = STYLE_CALLBACK;
411                 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
412             }
413             else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
414                      STYLE_NONE))
415             {
416                 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
417                 ximStyleNone = STYLE_NONE;
418             }
419         }
420         XFree(ximStyles);
421
422         if (ximStyle == 0)
423             ximStyle = ximStyleRoot;
424
425         if (ximStyle == 0)
426             ximStyle = ximStyleNone;
427
428         if (ximStyleCallback == 0)
429         {
430             TRACE("No callback style avalable\n");
431             ximStyleCallback = ximStyle;
432         }
433
434         }
435
436     wine_tsx11_unlock();
437
438     LoadImmDll();
439
440     if (pImmCreateContext)
441     {
442         root_context = pImmCreateContext();
443         if (pImmAssociateContext)
444             pImmAssociateContext(0,root_context);
445     }
446
447     return xim;
448
449 err:
450     wine_tsx11_unlock();
451     return NULL;
452 }
453
454
455 XIC X11DRV_CreateIC(XIM xim, Display *display, Window win)
456 {
457     XFontSet fontSet;
458     char **list;
459     int count;
460     XPoint spot = {0};
461     XVaNestedList preedit = NULL;
462     XVaNestedList status = NULL;
463     XIC xic;
464     XIMCallback P_StartCB;
465     XIMCallback P_DoneCB;
466     XIMCallback P_DrawCB;
467     XIMCallback P_CaretCB;
468     LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
469
470     wine_tsx11_lock();
471
472     /* use complex and slow XIC initialization method only for CJK */
473     if (langid != LANG_CHINESE &&
474         langid != LANG_JAPANESE &&
475         langid != LANG_KOREAN)
476     {
477         xic = XCreateIC(xim,
478                         XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
479                         XNClientWindow, win,
480                         XNFocusWindow, win,
481                         NULL);
482         wine_tsx11_unlock();
483         return xic;
484     }
485
486     fontSet = XCreateFontSet(display,
487                       "*", /*FIXME*/
488                       &list, &count, NULL);
489
490     TRACE("ximFontSet = %p\n", fontSet);
491     TRACE("list = %p, count = %d\n", list, count);
492
493     if (list != NULL)
494     {
495         int i;
496
497         for (i = 0; i < count; ++i)
498         {
499             TRACE("list[%d] = %s\n", i, list[i]);
500         }
501         XFreeStringList(list);
502     }
503
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;
513
514     if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
515     {
516         preedit = XVaCreateNestedList(0,
517                         XNFontSet, fontSet,
518                         XNSpotLocation, &spot,
519                         XNPreeditStartCallback, &P_StartCB,
520                         XNPreeditDoneCallback, &P_DoneCB,
521                         XNPreeditDrawCallback, &P_DrawCB,
522                         XNPreeditCaretCallback, &P_CaretCB,
523                         NULL);
524         TRACE("preedit = %p\n", preedit);
525     }
526     else
527     {
528         preedit = XVaCreateNestedList(0,
529                         XNPreeditStartCallback, &P_StartCB,
530                         XNPreeditDoneCallback, &P_DoneCB,
531                         XNPreeditDrawCallback, &P_DrawCB,
532                         XNPreeditCaretCallback, &P_CaretCB,
533                         NULL);
534
535         TRACE("preedit = %p\n", preedit);
536     }
537
538     if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
539     {
540         status = XVaCreateNestedList(0,
541             XNFontSet, fontSet,
542             NULL);
543         TRACE("status = %p\n", status);
544      }
545
546     if (preedit != NULL && status != NULL)
547     {
548         xic = XCreateIC(xim,
549               XNInputStyle, ximStyle,
550               XNPreeditAttributes, preedit,
551               XNStatusAttributes, status,
552               XNClientWindow, win,
553               XNFocusWindow, win,
554               NULL);
555      }
556     else if (preedit != NULL)
557     {
558         xic = XCreateIC(xim,
559               XNInputStyle, ximStyle,
560               XNPreeditAttributes, preedit,
561               XNClientWindow, win,
562               XNFocusWindow, win,
563               NULL);
564     }
565     else if (status != NULL)
566     {
567         xic = XCreateIC(xim,
568               XNInputStyle, ximStyle,
569               XNStatusAttributes, status,
570               XNClientWindow, win,
571               XNFocusWindow, win,
572               NULL);
573     }
574     else
575     {
576         xic = XCreateIC(xim,
577               XNInputStyle, ximStyle,
578               XNClientWindow, win,
579               XNFocusWindow, win,
580               NULL);
581     }
582
583     TRACE("xic = %p\n", xic);
584
585     if (preedit != NULL)
586         XFree(preedit);
587     if (status != NULL)
588         XFree(status);
589
590     wine_tsx11_unlock();
591
592     return xic;
593 }