user32: Unify handling of child and normal windows in getMinMaxInfo.
[wine] / dlls / winex11.drv / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #ifndef HAVE_XICCALLBACK_CALLBACK
38 #define XICCallback XIMCallback
39 #define XICProc XIMProc
40 #endif
41
42 BOOL ximInComposeMode=FALSE;
43
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;
50
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)
58
59 static XIMStyle ximStyle = 0;
60 static XIMStyle ximStyleRoot = 0;
61 static XIMStyle ximStyleRequest = STYLE_CALLBACK;
62
63 static BOOL X11DRV_ImmSetInternalString(DWORD dwIndex, DWORD dwOffset,
64                                         DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
65 {
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;
71
72     TRACE("( %i, %i, %d, %p, %d):\n", dwOffset, selLength, dwIndex, lpComp, dwCompLen );
73
74     if (dwIndex == GCS_COMPSTR)
75     {
76         unsigned int i,j;
77         LPBYTE ptr_new;
78         LPBYTE ptr_old;
79
80         if ((dwCompLen == 0) && (selLength == 0))
81         {
82             /* DO Nothing */
83         }
84         /* deletion occurred */
85         else if ((dwCompLen== 0) && (selLength != 0))
86         {
87             if (dwCompStringLength)
88             {
89                 for (i = 0; i < byte_selection; i++)
90                 {
91                     if (byte_offset+byte_selection+i <
92                         dwCompStringLength)
93                     {
94                         CompositionString[byte_offset + i] =
95                         CompositionString[byte_offset + byte_selection + i];
96                     }
97                     else
98                         CompositionString[byte_offset + i] = 0;
99                 }
100                 /* clean up the end */
101                 dwCompStringLength -= byte_selection;
102
103                 i = dwCompStringLength;
104                 while (i < dwCompStringSize)
105                 {
106                     CompositionString[i++] = 0;
107                 }
108             }
109         }
110         else
111         {
112             int byte_expansion = byte_length - byte_selection;
113
114             if (byte_expansion + dwCompStringLength >= dwCompStringSize)
115             {
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);
126
127                 memset(&(CompositionString[dwCompStringSize]), 0,
128                         byte_expansion);
129
130                 dwCompStringSize += byte_expansion;
131             }
132
133             ptr_new =  ((LPBYTE)lpComp);
134             ptr_old = CompositionString + byte_offset + byte_selection;
135
136             dwCompStringLength += byte_expansion;
137
138             for (j=0,i = byte_offset; i < dwCompStringSize; i++)
139             {
140                 if (j < byte_length)
141                 {
142                     CompositionString[i] = ptr_new[j++];
143                 }
144                 else
145                 {
146                     if (ptr_old < CompositionString + dwCompStringSize)
147                     {
148                         CompositionString[i] = *ptr_old;
149                         ptr_old++;
150                             }
151                     else
152                         CompositionString[i] = 0;
153                 }
154             }
155         }
156
157         rc = IME_SetCompositionString(SCS_SETSTR, CompositionString,
158                                       dwCompStringLength, NULL, 0);
159     }
160     else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
161     {
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);
167
168         rc = IME_SetCompositionString(SCS_SETSTR, ResultString,
169                                      dwResultStringSize, NULL, 0);
170
171         IME_NotifyIME( NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
172     }
173
174     return rc;
175 }
176
177 void X11DRV_XIMLookupChars( const char *str, DWORD count )
178 {
179     DWORD dwOutput;
180     WCHAR *wcOutput;
181     HWND focus;
182
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);
188
189     if ((focus = GetFocus()))
190         IME_UpdateAssociation(focus);
191
192     X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
193     HeapFree(GetProcessHeap(), 0, wcOutput);
194 }
195
196 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
197 {
198     if (fOpen == FALSE)
199     {
200         if (dwCompStringSize)
201             HeapFree(GetProcessHeap(),0,CompositionString);
202
203         dwCompStringSize = 0;
204         dwCompStringLength = 0;
205         CompositionString = NULL;
206
207         if (dwResultStringSize)
208             HeapFree(GetProcessHeap(),0,ResultString);
209
210         dwResultStringSize = 0;
211         ResultString = NULL;
212     }
213
214     IME_SetOpenStatus(fOpen);
215 }
216
217 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
218 {
219     TRACE("PreEditStartCallback %p\n",ic);
220     X11DRV_ImmSetOpenStatus(TRUE);
221     ximInComposeMode = TRUE;
222     return -1;
223 }
224
225 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
226 {
227     TRACE("PreeditDoneCallback %p\n",ic);
228     ximInComposeMode = FALSE;
229     X11DRV_ImmSetOpenStatus(FALSE);
230 }
231
232 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
233                                    XIMPreeditDrawCallbackStruct *P_DR)
234 {
235     TRACE("PreEditDrawCallback %p\n",ic);
236
237     if (P_DR)
238     {
239         int sel = P_DR->chg_first;
240         int len = P_DR->chg_length;
241         if (P_DR->text)
242         {
243             if (! P_DR->text->encoding_is_wchar)
244             {
245                 DWORD dwOutput;
246                 WCHAR *wcOutput;
247
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)
254                 {
255                     dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
256                                P_DR->text->string.multi_byte, -1,
257                                wcOutput, dwOutput);
258
259                     /* ignore null */
260                     dwOutput --;
261                     X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
262                     HeapFree(GetProcessHeap(), 0, wcOutput);
263                 }
264             }
265             else
266             {
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);
271             }
272         }
273         else
274             X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
275         IME_SetCursorPos(P_DR->caret);
276     }
277     TRACE("Finished\n");
278 }
279
280 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
281                                     XIMPreeditCaretCallbackStruct *P_C)
282 {
283     TRACE("PreeditCaretCallback %p\n",ic);
284
285     if (P_C)
286     {
287         int pos = IME_GetCursorPos();
288         TRACE("pos: %d\n", pos);
289         switch(P_C->direction)
290         {
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;
315         }
316         IME_SetCursorPos(pos);
317         P_C->position = pos;
318     }
319     TRACE("Finished\n");
320 }
321
322 void X11DRV_ForceXIMReset(HWND hwnd)
323 {
324     XIC ic = X11DRV_get_ic(hwnd);
325     if (ic)
326     {
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();
333     }
334 }
335
336 /***********************************************************************
337  *           X11DRV_InitXIM
338  *
339  * Process-wide XIM initialization.
340  */
341 BOOL X11DRV_InitXIM( const char *input_style )
342 {
343     BOOL ret;
344
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;
351
352     wine_tsx11_lock();
353     if (!(ret = XSupportsLocale()))
354     {
355         WARN("X does not support locale.\n");
356     }
357     else if (XSetLocaleModifiers("") == NULL)
358     {
359         WARN("Could not set locale modifiers.\n");
360         ret = FALSE;
361     }
362     wine_tsx11_unlock();
363     return ret;
364 }
365
366
367 static void open_xim_callback( Display *display, XPointer ptr, XPointer data );
368
369 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
370 {
371     struct x11drv_thread_data *thread_data = x11drv_thread_data();
372
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();
379 }
380
381 /***********************************************************************
382  *           X11DRV Ime creation
383  *
384  * Should always be called with the x11 lock held
385  */
386 static BOOL open_xim( Display *display )
387 {
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;
394
395     xim = XOpenIM(display, NULL, NULL, NULL);
396     if (xim == NULL)
397     {
398         WARN("Could not open input method.\n");
399         return FALSE;
400     }
401
402     destroy.client_data = NULL;
403     destroy.callback = X11DRV_DestroyIM;
404     if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
405     {
406         WARN("Could not set destroy callback.\n");
407     }
408
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));
412
413     XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
414     if (ximStyles == 0)
415     {
416         WARN("Could not find supported input style.\n");
417         XCloseIM(xim);
418         return FALSE;
419     }
420     else
421     {
422         TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
423
424         ximStyleRoot = 0;
425         ximStyleNone = 0;
426         ximStyleCallback = 0;
427
428         for (i = 0; i < ximStyles->count_styles; ++i)
429         {
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))
439             {
440                 ximStyle = ximStyleRequest;
441                 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
442             }
443             else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
444                      STYLE_ROOT))
445             {
446                 ximStyleRoot = STYLE_ROOT;
447                 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
448             }
449             else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
450                      STYLE_CALLBACK))
451             {
452                 ximStyleCallback = STYLE_CALLBACK;
453                 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
454             }
455             else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
456                      STYLE_NONE))
457             {
458                 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
459                 ximStyleNone = STYLE_NONE;
460             }
461         }
462         XFree(ximStyles);
463
464         if (ximStyle == 0)
465             ximStyle = ximStyleRoot;
466
467         if (ximStyle == 0)
468             ximStyle = ximStyleNone;
469
470         if (ximStyleCallback == 0)
471         {
472             TRACE("No callback style avalable\n");
473             ximStyleCallback = ximStyle;
474         }
475
476     }
477
478     thread_data->xim = xim;
479
480     if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0 ||
481         (ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
482     {
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)
490         {
491             int i;
492             for (i = 0; i < count; ++i)
493                 TRACE("list[%d] = %s\n", i, list[i]);
494             XFreeStringList(list);
495         }
496     }
497     else
498         thread_data->font_set = NULL;
499
500     wine_tsx11_unlock();
501     IME_UpdateAssociation(NULL);
502     wine_tsx11_lock();
503     return TRUE;
504 }
505
506 static void open_xim_callback( Display *display, XPointer ptr, XPointer data )
507 {
508     if (open_xim( display ))
509         XUnregisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL);
510 }
511
512 void X11DRV_SetupXIM(void)
513 {
514     Display *display = thread_display();
515
516     wine_tsx11_lock();
517     if (!open_xim( display ))
518         XRegisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL );
519     wine_tsx11_unlock();
520 }
521
522 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
523 {
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;
528 }
529
530
531 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
532 {
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;
542
543     TRACE("xim = %p\n", xim);
544
545     wine_tsx11_lock();
546
547     /* use complex and slow XIC initialization method only for CJK */
548     if (langid != LANG_CHINESE &&
549         langid != LANG_JAPANESE &&
550         langid != LANG_KOREAN)
551     {
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;
561     }
562
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;
572
573     if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
574     {
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);
584     }
585     else
586     {
587         preedit = XVaCreateNestedList(0,
588                         XNPreeditStartCallback, &P_StartCB,
589                         XNPreeditDoneCallback, &P_DoneCB,
590                         XNPreeditDrawCallback, &P_DrawCB,
591                         XNPreeditCaretCallback, &P_CaretCB,
592                         NULL);
593
594         TRACE("preedit = %p\n", preedit);
595     }
596
597     if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
598     {
599         status = XVaCreateNestedList(0,
600             XNFontSet, fontSet,
601             NULL);
602         TRACE("status = %p\n", status);
603      }
604
605     if (preedit != NULL && status != NULL)
606     {
607         xic = XCreateIC(xim,
608               XNInputStyle, ximStyle,
609               XNPreeditAttributes, preedit,
610               XNStatusAttributes, status,
611               XNClientWindow, win,
612               XNFocusWindow, win,
613               XNDestroyCallback, &destroy,
614               NULL);
615      }
616     else if (preedit != NULL)
617     {
618         xic = XCreateIC(xim,
619               XNInputStyle, ximStyle,
620               XNPreeditAttributes, preedit,
621               XNClientWindow, win,
622               XNFocusWindow, win,
623               XNDestroyCallback, &destroy,
624               NULL);
625     }
626     else if (status != NULL)
627     {
628         xic = XCreateIC(xim,
629               XNInputStyle, ximStyle,
630               XNStatusAttributes, status,
631               XNClientWindow, win,
632               XNFocusWindow, win,
633               XNDestroyCallback, &destroy,
634               NULL);
635     }
636     else
637     {
638         xic = XCreateIC(xim,
639               XNInputStyle, ximStyle,
640               XNClientWindow, win,
641               XNFocusWindow, win,
642               XNDestroyCallback, &destroy,
643               NULL);
644     }
645
646     TRACE("xic = %p\n", xic);
647     data->xic = xic;
648
649     if (preedit != NULL)
650         XFree(preedit);
651     if (status != NULL)
652         XFree(status);
653
654     wine_tsx11_unlock();
655
656     return xic;
657 }