]> Pileus Git - ~andy/gtk/blob - modules/input/gtkimcontextxim.c
New function for XIM instantiate callback. New function where call to
[~andy/gtk] / modules / input / gtkimcontextxim.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "locale.h"
21 #include <string.h>
22
23 #include "gtk/gtkintl.h"
24 #include "gtk/gtklabel.h"
25 #include "gtk/gtksignal.h"
26 #include "gtk/gtkwindow.h"
27 #include "gtkimcontextxim.h"
28
29 typedef struct _StatusWindow StatusWindow;
30
31 struct _GtkXIMInfo
32 {
33   GdkScreen *screen;
34   XIM im;
35   char *locale;
36   XIMStyle preedit_style_setting;
37   XIMStyle status_style_setting;
38   XIMStyle style;
39   GtkSettings *settings;
40   gulong status_set;
41   gulong preedit_set;
42   XIMStyles *xim_styles;
43   GSList *ics;
44
45   guint reconnecting :1;
46 };
47
48 /* A context status window; these are kept in the status_windows list. */
49 struct _StatusWindow
50 {
51   GtkWidget *window;
52   
53   /* Toplevel window to which the status window corresponds */
54   GtkWidget *toplevel;
55   
56   /* Signal connection ids; we connect to the toplevel */
57   gulong destroy_handler_id;
58   gulong configure_handler_id;
59 };
60
61 static void     gtk_im_context_xim_class_init         (GtkIMContextXIMClass  *class);
62 static void     gtk_im_context_xim_init               (GtkIMContextXIM       *im_context_xim);
63 static void     gtk_im_context_xim_finalize           (GObject               *obj);
64 static void     gtk_im_context_xim_set_client_window  (GtkIMContext          *context,
65                                                        GdkWindow             *client_window);
66 static gboolean gtk_im_context_xim_filter_keypress    (GtkIMContext          *context,
67                                                        GdkEventKey           *key);
68 static void     gtk_im_context_xim_reset              (GtkIMContext          *context);
69 static void     gtk_im_context_xim_focus_in           (GtkIMContext          *context);
70 static void     gtk_im_context_xim_focus_out          (GtkIMContext          *context);
71 static void     gtk_im_context_xim_set_cursor_location (GtkIMContext          *context,
72                                                        GdkRectangle             *area);
73 static void     gtk_im_context_xim_set_use_preedit    (GtkIMContext          *context,
74                                                        gboolean               use_preedit);
75 static void     gtk_im_context_xim_get_preedit_string (GtkIMContext          *context,
76                                                        gchar                **str,
77                                                        PangoAttrList        **attrs,
78                                                        gint                  *cursor_pos);
79
80 static void reinitialize_ic      (GtkIMContextXIM *context_xim);
81 static void set_ic_client_window (GtkIMContextXIM *context_xim,
82                                   GdkWindow       *client_window);
83
84 static void setup_styles (GtkXIMInfo *info);
85
86 static void status_window_show     (GtkIMContextXIM *context_xim);
87 static void status_window_hide     (GtkIMContextXIM *context_xim);
88 static void status_window_set_text (GtkIMContextXIM *context_xim,
89                                     const gchar     *text);
90
91 static void xim_destroy_callback   (XIM      xim,
92                                     XPointer client_data,
93                                     XPointer call_data);
94
95 static XIC       gtk_im_context_xim_get_ic            (GtkIMContextXIM *context_xim);
96 static GObjectClass *parent_class;
97
98 GType gtk_type_im_context_xim = 0;
99
100 GSList *open_ims = NULL;
101
102 /* List of status windows for different toplevels */
103 static GSList *status_windows = NULL;
104
105 void
106 gtk_im_context_xim_register_type (GTypeModule *type_module)
107 {
108   static const GTypeInfo im_context_xim_info =
109   {
110     sizeof (GtkIMContextXIMClass),
111     (GBaseInitFunc) NULL,
112     (GBaseFinalizeFunc) NULL,
113     (GClassInitFunc) gtk_im_context_xim_class_init,
114     NULL,           /* class_finalize */    
115     NULL,           /* class_data */
116     sizeof (GtkIMContextXIM),
117     0,
118     (GInstanceInitFunc) gtk_im_context_xim_init,
119   };
120
121   gtk_type_im_context_xim = 
122     g_type_module_register_type (type_module,
123                                  GTK_TYPE_IM_CONTEXT,
124                                  "GtkIMContextXIM",
125                                  &im_context_xim_info, 0);
126 }
127
128 #define PREEDIT_MASK (XIMPreeditCallbacks | XIMPreeditPosition | \
129                       XIMPreeditArea | XIMPreeditNothing | XIMPreeditNone)
130 #define STATUS_MASK (XIMStatusCallbacks | XIMStatusArea | \
131                       XIMStatusNothing | XIMStatusNone)
132 #define ALLOWED_MASK (XIMPreeditCallbacks | XIMPreeditNothing | XIMPreeditNone | \
133                       XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone)
134
135 static XIMStyle 
136 choose_better_style (XIMStyle style1, XIMStyle style2) 
137 {
138   XIMStyle s1, s2, u; 
139   
140   if (style1 == 0) return style2;
141   if (style2 == 0) return style1;
142   if ((style1 & (PREEDIT_MASK | STATUS_MASK))
143         == (style2 & (PREEDIT_MASK | STATUS_MASK)))
144     return style1;
145
146   s1 = style1 & PREEDIT_MASK;
147   s2 = style2 & PREEDIT_MASK;
148   u = s1 | s2;
149   if (s1 != s2) {
150     if (u & XIMPreeditCallbacks)
151       return (s1 == XIMPreeditCallbacks) ? style1 : style2;
152     else if (u & XIMPreeditPosition)
153       return (s1 == XIMPreeditPosition) ? style1 :style2;
154     else if (u & XIMPreeditArea)
155       return (s1 == XIMPreeditArea) ? style1 : style2;
156     else if (u & XIMPreeditNothing)
157       return (s1 == XIMPreeditNothing) ? style1 : style2;
158     else if (u & XIMPreeditNone)
159       return (s1 == XIMPreeditNone) ? style1 : style2;
160   } else {
161     s1 = style1 & STATUS_MASK;
162     s2 = style2 & STATUS_MASK;
163     u = s1 | s2;
164     if (u & XIMStatusCallbacks)
165       return (s1 == XIMStatusCallbacks) ? style1 : style2;
166     else if (u & XIMStatusArea)
167       return (s1 == XIMStatusArea) ? style1 : style2;
168     else if (u & XIMStatusNothing)
169       return (s1 == XIMStatusNothing) ? style1 : style2;
170     else if (u & XIMStatusNone)
171       return (s1 == XIMStatusNone) ? style1 : style2;
172   }
173   return 0; /* Get rid of stupid warning */
174 }
175
176 static void
177 reinitialize_all_ics (GtkXIMInfo *info)
178 {
179   GSList *tmp_list;
180
181   for (tmp_list = info->ics; tmp_list; tmp_list = tmp_list->next)
182     reinitialize_ic (tmp_list->data);
183 }
184
185 static void
186 status_style_change (GtkXIMInfo *info)
187 {
188   GtkIMStatusStyle status_style;
189   
190   g_object_get (info->settings,
191                 "gtk-im-status-style", &status_style,
192                 NULL);
193   if (status_style == GTK_IM_STATUS_CALLBACK)
194     info->status_style_setting = XIMStatusCallbacks;
195   else if (status_style == GTK_IM_STATUS_NOTHING)
196     info->status_style_setting = XIMStatusNothing;
197   else if (status_style == GTK_IM_STATUS_NONE)
198     info->status_style_setting = XIMStatusNone;
199   else
200     return;
201
202   setup_styles (info);
203   
204   reinitialize_all_ics (info);
205 }
206
207 static void
208 preedit_style_change (GtkXIMInfo *info)
209 {
210   GtkIMPreeditStyle preedit_style;
211   g_object_get (info->settings,
212                 "gtk-im-preedit-style", &preedit_style,
213                 NULL);
214   if (preedit_style == GTK_IM_PREEDIT_CALLBACK)
215     info->preedit_style_setting = XIMPreeditCallbacks;
216   else if (preedit_style == GTK_IM_PREEDIT_NOTHING)
217     info->preedit_style_setting = XIMPreeditNothing;
218   else if (preedit_style == GTK_IM_PREEDIT_NONE)
219     info->preedit_style_setting = XIMPreeditNone;
220   else
221     return;
222
223   setup_styles (info);
224   
225   reinitialize_all_ics (info);
226 }
227
228 static void
229 setup_styles (GtkXIMInfo *info)
230 {
231   int i;
232   unsigned long settings_preference;
233   XIMStyles *xim_styles = info->xim_styles;
234
235   settings_preference = info->status_style_setting|info->preedit_style_setting;
236   info->style = 0;
237   if (xim_styles)
238     {
239       for (i = 0; i < xim_styles->count_styles; i++)
240         if ((xim_styles->supported_styles[i] & ALLOWED_MASK) == xim_styles->supported_styles[i])
241           {
242             if (settings_preference == xim_styles->supported_styles[i])
243               {
244                 info->style = settings_preference;
245                 break;
246               }
247             info->style = choose_better_style (info->style,
248                                                xim_styles->supported_styles[i]);
249           }
250     }
251   if (info->style == 0)
252     info->style = XIMPreeditNothing | XIMStatusNothing;
253 }
254
255 static void
256 setup_im (GtkXIMInfo *info)
257 {
258   XIMValuesList *ic_values = NULL;
259   XIMCallback im_destroy_callback;
260
261   if (info->im == NULL)
262     return;
263
264   im_destroy_callback.client_data = (XPointer)info;
265   im_destroy_callback.callback = (XIMProc)xim_destroy_callback;
266   XSetIMValues (info->im,
267                 XNDestroyCallback, &im_destroy_callback,
268                 NULL);
269
270   XGetIMValues (info->im,
271                 XNQueryInputStyle, &info->xim_styles,
272                 XNQueryICValuesList, &ic_values,
273                 NULL);
274
275   info->settings = gtk_settings_get_for_screen (info->screen);
276
277   if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
278                                      "gtk-im-preedit-style"))
279     gtk_settings_install_property (g_param_spec_enum ("gtk-im-preedit-style",
280                                                       _("IM Preedit style"),
281                                                       _("How to draw the input method preedit string"),
282                                                       GTK_TYPE_IM_PREEDIT_STYLE,
283                                                       GTK_IM_PREEDIT_CALLBACK,
284                                                       G_PARAM_READWRITE));
285
286   if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
287                                      "gtk-im-status-style"))
288     gtk_settings_install_property (g_param_spec_enum ("gtk-im-status-style",
289                                                       _("IM Status style"),
290                                                       _("How to draw the input method statusbar"),
291                                                       GTK_TYPE_IM_STATUS_STYLE,
292                                                       GTK_IM_STATUS_CALLBACK,
293                                                       G_PARAM_READWRITE));
294
295   info->status_set = g_signal_connect_swapped (info->settings,
296                                                "notify::gtk-im-status-style",
297                                                G_CALLBACK (status_style_change),
298                                                info);
299   info->preedit_set = g_signal_connect_swapped (info->settings,
300                                                 "notify::gtk-im-preedit-style",
301                                                 G_CALLBACK (preedit_style_change),
302                                                 info);
303
304   status_style_change (info);
305   preedit_style_change (info);
306
307 #if 0
308   if (ic_values)
309     {
310       for (i = 0; i < ic_values->count_values; i++)
311         g_print ("%s\n", ic_values->supported_values[i]);
312       for (i = 0; i < xim_styles->count_styles; i++)
313         g_print ("%#x\n", xim_styles->supported_styles[i]);
314     }
315 #endif
316
317   if (ic_values)
318     XFree (ic_values);
319 }
320
321 static void
322 xim_info_display_closed (GdkDisplay *display,
323                          gboolean    is_error,
324                          GtkXIMInfo *info)
325 {
326   GSList *ics, *tmp_list;
327
328   open_ims = g_slist_remove (open_ims, info);
329
330   ics = info->ics;
331   info->ics = NULL;
332
333   for (tmp_list = ics; tmp_list; tmp_list = tmp_list->next)
334     set_ic_client_window (tmp_list->data, NULL);
335
336   g_slist_free (ics);
337   
338   g_signal_handler_disconnect (info->settings, info->status_set);
339   g_signal_handler_disconnect (info->settings, info->preedit_set);
340   
341   XFree (info->xim_styles->supported_styles);
342   XFree (info->xim_styles);
343   g_free (info->locale);
344
345   if (info->im)
346     XCloseIM (info->im);
347
348   g_free (info);
349 }
350
351 static void
352 xim_instantiate_callback (Display *display, XPointer client_data,
353                           XPointer call_data)
354 {
355   GtkXIMInfo *info = (GtkXIMInfo*)client_data;
356   XIM im = NULL;
357
358   im = XOpenIM (display, NULL, NULL, NULL);
359
360   if (!im)
361     return;
362
363   info->im = im;
364   setup_im (info);
365
366   XUnregisterIMInstantiateCallback (display, NULL, NULL, NULL,
367                                     xim_instantiate_callback,
368                                     (XPointer)info);
369   info->reconnecting = FALSE;
370 }
371
372 /* initialize info->im */
373 static void
374 xim_info_try_im (GtkXIMInfo *info)
375 {
376   GdkScreen *screen = info->screen;
377   GdkDisplay *display = gdk_screen_get_display (screen);
378
379   g_assert (info->im == NULL);
380   if (info->reconnecting)
381     return;
382
383   if (XSupportsLocale ())
384     {
385       if (!XSetLocaleModifiers (""))
386         g_warning ("Unable to set locale modifiers with XSetLocaleModifiers()");
387       info->im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
388       if (!info->im)
389         {
390           XRegisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY(display),
391                                           NULL, NULL, NULL,
392                                           xim_instantiate_callback,
393                                           (XPointer)info);
394           info->reconnecting = TRUE;
395           return;
396         }
397       setup_im (info);
398
399       g_signal_connect (display, "closed",
400                         G_CALLBACK (xim_info_display_closed), info);
401     }
402 }
403
404 static void
405 xim_destroy_callback (XIM      xim,
406                       XPointer client_data,
407                       XPointer call_data)
408 {
409   GtkXIMInfo *info = (GtkXIMInfo*)client_data;
410
411   info->im = NULL;
412
413   g_signal_handler_disconnect (info->settings, info->status_set);
414   g_signal_handler_disconnect (info->settings, info->preedit_set);
415
416   reinitialize_all_ics (info);
417   xim_info_try_im (info);
418   return;
419
420
421 static GtkXIMInfo *
422 get_im (GdkWindow *client_window,
423         const char *locale)
424 {
425   GSList *tmp_list;
426   GtkXIMInfo *info;
427   GdkScreen *screen = gdk_drawable_get_screen (client_window);
428
429   info = NULL;
430   tmp_list = open_ims;
431   while (tmp_list)
432     {
433       info = tmp_list->data;
434       if (info->screen == screen &&
435           strcmp (info->locale, locale) == 0)
436         {
437           if (info->im)
438             return info;
439           else
440             break;
441         }
442       tmp_list = tmp_list->next;
443     }
444
445   if (info == NULL)
446     {
447       info = g_new (GtkXIMInfo, 1);
448       open_ims = g_slist_prepend (open_ims, info);
449
450       info->screen = screen;
451       info->locale = g_strdup (locale);
452       info->xim_styles = NULL;
453       info->preedit_style_setting = 0;
454       info->status_style_setting = 0;
455       info->settings = NULL;
456       info->preedit_set = 0;
457       info->status_set = 0;
458       info->ics = NULL;
459       info->reconnecting = FALSE;
460       info->im = NULL;
461     }
462
463   xim_info_try_im (info);
464   return info;
465 }
466
467 static void
468 gtk_im_context_xim_class_init (GtkIMContextXIMClass *class)
469 {
470   GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
471   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
472
473   parent_class = g_type_class_peek_parent (class);
474
475   im_context_class->set_client_window = gtk_im_context_xim_set_client_window;
476   im_context_class->filter_keypress = gtk_im_context_xim_filter_keypress;
477   im_context_class->reset = gtk_im_context_xim_reset;
478   im_context_class->get_preedit_string = gtk_im_context_xim_get_preedit_string;
479   im_context_class->focus_in = gtk_im_context_xim_focus_in;
480   im_context_class->focus_out = gtk_im_context_xim_focus_out;
481   im_context_class->set_cursor_location = gtk_im_context_xim_set_cursor_location;
482   im_context_class->set_use_preedit = gtk_im_context_xim_set_use_preedit;
483   gobject_class->finalize = gtk_im_context_xim_finalize;
484 }
485
486 static void
487 gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
488 {
489   im_context_xim->use_preedit = TRUE;
490   im_context_xim->filter_key_release = FALSE;
491   im_context_xim->status_visible = FALSE;
492   im_context_xim->finalizing = FALSE;
493 }
494
495 static void
496 gtk_im_context_xim_finalize (GObject *obj)
497 {
498   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (obj);
499
500   context_xim->finalizing = TRUE;
501
502   set_ic_client_window (context_xim, NULL);
503
504   g_free (context_xim->locale);
505   g_free (context_xim->mb_charset);
506 }
507
508 static void
509 reinitialize_ic (GtkIMContextXIM *context_xim)
510 {
511   if (context_xim->ic)
512     {
513       XDestroyIC (context_xim->ic);
514       status_window_hide (context_xim);
515       context_xim->ic = NULL;
516
517       if (context_xim->preedit_length)
518         {
519           context_xim->preedit_length = 0;
520           if (!context_xim->finalizing)
521             g_signal_emit_by_name (context_xim, "preedit_changed");
522         }
523     }
524   /* 
525      reset filter_key_release flag, otherwise keystrokes will be doubled
526      until reconnecting to XIM.
527   */
528   context_xim->filter_key_release = FALSE;
529 }
530
531 static void
532 set_ic_client_window (GtkIMContextXIM *context_xim,
533                       GdkWindow       *client_window)
534 {
535   reinitialize_ic (context_xim);
536   if (context_xim->client_window)
537     {
538       context_xim->im_info->ics = g_slist_remove (context_xim->im_info->ics, context_xim);
539       context_xim->im_info = NULL;
540     }
541   
542   context_xim->client_window = client_window;
543
544   if (context_xim->client_window)
545     {
546       context_xim->im_info = get_im (context_xim->client_window, context_xim->locale);
547       context_xim->im_info->ics = g_slist_prepend (context_xim->im_info->ics, context_xim);
548     }
549 }
550
551 static void
552 gtk_im_context_xim_set_client_window (GtkIMContext          *context,
553                                       GdkWindow             *client_window)
554 {
555   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
556
557   set_ic_client_window (context_xim, client_window);
558 }
559
560 GtkIMContext *
561 gtk_im_context_xim_new (void)
562 {
563   GtkIMContextXIM *result;
564   const gchar *charset;
565
566   result = GTK_IM_CONTEXT_XIM (g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL));
567
568   result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
569   
570   g_get_charset (&charset);
571   result->mb_charset = g_strdup (charset);
572
573   return GTK_IM_CONTEXT (result);
574 }
575
576 static char *
577 mb_to_utf8 (GtkIMContextXIM *context_xim,
578             const char      *str)
579 {
580   GError *error = NULL;
581   gchar *result;
582
583   if (strcmp (context_xim->mb_charset, "UTF-8") == 0)
584     result = g_strdup (str);
585   else
586     {
587       result = g_convert (str, -1,
588                           "UTF-8", context_xim->mb_charset,
589                           NULL, NULL, &error);
590       if (!result)
591         {
592           g_warning ("Error converting text from IM to UTF-8: %s\n", error->message);
593           g_error_free (error);
594         }
595     }
596   
597   return result;
598 }
599
600 static gboolean
601 gtk_im_context_xim_filter_keypress (GtkIMContext *context,
602                                     GdkEventKey  *event)
603 {
604   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
605   XIC ic = gtk_im_context_xim_get_ic (context_xim);
606   gchar static_buffer[256];
607   gchar *buffer = static_buffer;
608   gint buffer_size = sizeof(static_buffer) - 1;
609   gint num_bytes = 0;
610   KeySym keysym;
611   Status status;
612   gboolean result = FALSE;
613   GdkWindow *root_window = gdk_screen_get_root_window (gdk_drawable_get_screen (event->window));
614
615   XKeyPressedEvent xevent;
616
617   if (event->type == GDK_KEY_RELEASE && !context_xim->filter_key_release)
618     return FALSE;
619
620   xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
621   xevent.serial = 0;            /* hope it doesn't matter */
622   xevent.send_event = event->send_event;
623   xevent.display = GDK_DRAWABLE_XDISPLAY (event->window);
624   xevent.window = GDK_DRAWABLE_XID (event->window);
625   xevent.root = GDK_DRAWABLE_XID (root_window);
626   xevent.subwindow = xevent.window;
627   xevent.time = event->time;
628   xevent.x = xevent.x_root = 0;
629   xevent.y = xevent.y_root = 0;
630   xevent.state = event->state;
631   xevent.keycode = event->hardware_keycode;
632   xevent.same_screen = True;
633   
634   if (XFilterEvent ((XEvent *)&xevent, GDK_DRAWABLE_XID (context_xim->client_window)))
635     return TRUE;
636   
637  again:
638   if (ic)
639     num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
640   else
641     {
642       num_bytes = XLookupString (&xevent, buffer, buffer_size, &keysym, NULL);
643       status = XLookupBoth;
644     }
645
646   if (status == XBufferOverflow)
647     {
648       buffer_size = num_bytes;
649       if (buffer != static_buffer) 
650         g_free (buffer);
651       buffer = g_malloc (num_bytes + 1);
652       goto again;
653     }
654
655   /* I don't know how we should properly handle XLookupKeysym or XLookupBoth
656    * here ... do input methods actually change the keysym? we can't really
657    * feed it back to accelerator processing at this point...
658    */
659   if (status == XLookupChars || status == XLookupBoth)
660     {
661       char *result_utf8;
662
663       buffer[num_bytes] = '\0';
664
665       result_utf8 = mb_to_utf8 (context_xim, buffer);
666       if (result_utf8)
667         {
668           if ((guchar)result_utf8[0] >= 0x20 &&
669               result_utf8[0] != 0x7f) /* Some IM have a nasty habit of converting
670                                        * control characters into strings
671                                        */
672             {
673               g_signal_emit_by_name (context, "commit", result_utf8);
674               result = TRUE;
675             }
676           
677           g_free (result_utf8);
678         }
679     }
680
681   if (buffer != static_buffer) 
682     g_free (buffer);
683
684   return result;
685 }
686
687 static void
688 gtk_im_context_xim_focus_in (GtkIMContext *context)
689 {
690   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
691   XIC ic = gtk_im_context_xim_get_ic (context_xim);
692
693   if (!ic)
694     return;
695
696   XSetICFocus (ic);
697
698   status_window_show (context_xim);
699
700   return;
701 }
702
703 static void
704 gtk_im_context_xim_focus_out (GtkIMContext *context)
705 {
706   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
707   XIC ic = gtk_im_context_xim_get_ic (context_xim);
708
709   if (!ic)
710     return;
711
712   XUnsetICFocus (ic);
713
714   status_window_hide (context_xim);
715
716   return;
717 }
718
719 static void
720 gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
721                                         GdkRectangle *area)
722 {
723   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
724   XIC ic = gtk_im_context_xim_get_ic (context_xim);
725
726   XVaNestedList preedit_attr;
727   XPoint          spot;
728
729   if (!ic)
730     return;
731
732   spot.x = area->x;
733   spot.y = area->y;
734
735   preedit_attr = XVaCreateNestedList (0,
736                                       XNSpotLocation, &spot,
737                                       0);
738   XSetICValues (ic,
739                 XNPreeditAttributes, preedit_attr,
740                 NULL);
741   XFree(preedit_attr);
742
743   return;
744 }
745
746 static void
747 gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
748                                     gboolean      use_preedit)
749 {
750   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
751
752   use_preedit = use_preedit != FALSE;
753
754   if (context_xim->use_preedit != use_preedit)
755     {
756       context_xim->use_preedit = use_preedit;
757       reinitialize_ic (context_xim);
758     }
759
760   return;
761 }
762
763 static void
764 gtk_im_context_xim_reset (GtkIMContext *context)
765 {
766   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
767   XIC ic = gtk_im_context_xim_get_ic (context_xim);
768   gchar *result;
769
770   /* restore conversion state after resetting ic later */
771   XIMPreeditState preedit_state = XIMPreeditUnKnown;
772   XVaNestedList preedit_attr;
773   gboolean have_preedit_state = FALSE;
774
775   if (!ic)
776     return;
777   
778
779   if (context_xim->preedit_length == 0)
780     return;
781
782   preedit_attr = XVaCreateNestedList(0,
783                                      XNPreeditState, &preedit_state,
784                                      0);
785   if (!XGetICValues(ic,
786                     XNPreeditAttributes, preedit_attr,
787                     NULL))
788     have_preedit_state = TRUE;
789
790   XFree(preedit_attr);
791
792   result = XmbResetIC (ic);
793
794   preedit_attr = XVaCreateNestedList(0,
795                                      XNPreeditState, preedit_state,
796                                      0);
797   if (have_preedit_state)
798     XSetICValues(ic,
799                  XNPreeditAttributes, preedit_attr,
800                  NULL);
801
802   XFree(preedit_attr);
803
804   if (result)
805     {
806       char *result_utf8 = mb_to_utf8 (context_xim, result);
807       if (result_utf8)
808         {
809           g_signal_emit_by_name (context, "commit", result_utf8);
810           g_free (result_utf8);
811         }
812     }
813
814   if (context_xim->preedit_length)
815     {
816       context_xim->preedit_length = 0;
817       g_signal_emit_by_name (context, "preedit_changed");
818     }
819
820   XFree (result);
821 }
822
823 /* Mask of feedback bits that we render
824  */
825 #define FEEDBACK_MASK (XIMReverse | XIMUnderline)
826
827 static void
828 add_feedback_attr (PangoAttrList *attrs,
829                    const gchar   *str,
830                    XIMFeedback    feedback,
831                    gint           start_pos,
832                    gint           end_pos)
833 {
834   PangoAttribute *attr;
835   
836   gint start_index = g_utf8_offset_to_pointer (str, start_pos) - str;
837   gint end_index = g_utf8_offset_to_pointer (str, end_pos) - str;
838
839   if (feedback & XIMUnderline)
840     {
841       attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
842       attr->start_index = start_index;
843       attr->end_index = end_index;
844
845       pango_attr_list_change (attrs, attr);
846     }
847
848   if (feedback & XIMReverse)
849     {
850       attr = pango_attr_foreground_new (0xffff, 0xffff, 0xffff);
851       attr->start_index = start_index;
852       attr->end_index = end_index;
853
854       pango_attr_list_change (attrs, attr);
855
856       attr = pango_attr_background_new (0, 0, 0);
857       attr->start_index = start_index;
858       attr->end_index = end_index;
859
860       pango_attr_list_change (attrs, attr);
861     }
862
863   if (feedback & ~FEEDBACK_MASK)
864     g_warning ("Unrendered feedback style: %#lx", feedback & ~FEEDBACK_MASK);
865 }
866
867 static void     
868 gtk_im_context_xim_get_preedit_string (GtkIMContext   *context,
869                                        gchar         **str,
870                                        PangoAttrList **attrs,
871                                        gint           *cursor_pos)
872 {
873   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
874   gchar *utf8 = g_ucs4_to_utf8 (context_xim->preedit_chars, context_xim->preedit_length, NULL, NULL, NULL);
875
876   if (attrs)
877     {
878       int i;
879       XIMFeedback last_feedback = 0;
880       gint start = -1;
881       
882       *attrs = pango_attr_list_new ();
883
884       for (i = 0; i < context_xim->preedit_length; i++)
885         {
886           XIMFeedback new_feedback = context_xim->feedbacks[i] & FEEDBACK_MASK;
887           if (new_feedback != last_feedback)
888             {
889               if (start >= 0)
890                 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
891               
892               last_feedback = new_feedback;
893               start = i;
894             }
895         }
896
897       if (start >= 0)
898         add_feedback_attr (*attrs, utf8, last_feedback, start, i);
899     }
900
901   if (str)
902     *str = utf8;
903   else
904     g_free (utf8);
905
906   if (cursor_pos)
907     *cursor_pos = context_xim->preedit_cursor;
908 }
909
910 static void
911 preedit_start_callback (XIC      xic,
912                         XPointer client_data,
913                         XPointer call_data)
914 {
915   GtkIMContext *context = GTK_IM_CONTEXT (client_data);
916   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
917   
918   if (!context_xim->finalizing)
919     g_signal_emit_by_name (context, "preedit_start");
920 }                    
921
922 static void
923 preedit_done_callback (XIC      xic,
924                      XPointer client_data,
925                      XPointer call_data)
926 {
927   GtkIMContext *context = GTK_IM_CONTEXT (client_data);
928   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
929
930   if (!context_xim->finalizing)
931     g_signal_emit_by_name (context, "preedit_end");  
932 }                    
933
934 static gint
935 xim_text_to_utf8 (GtkIMContextXIM *context, XIMText *xim_text, gchar **text)
936 {
937   gint text_length = 0;
938   GError *error = NULL;
939   gchar *result = NULL;
940
941   if (xim_text && xim_text->string.multi_byte)
942     {
943       if (xim_text->encoding_is_wchar)
944         {
945           g_warning ("Wide character return from Xlib not currently supported");
946           *text = NULL;
947           return 0;
948         }
949
950       if (strcmp (context->mb_charset, "UTF-8") == 0)
951         result = g_strdup (xim_text->string.multi_byte);
952       else
953         result = g_convert (xim_text->string.multi_byte,
954                             -1,
955                             "UTF-8",
956                             context->mb_charset,
957                             NULL, NULL, &error);
958       
959       if (result)
960         {
961           text_length = g_utf8_strlen (result, -1);
962           
963           if (text_length != xim_text->length)
964             {
965               g_warning ("Size mismatch when converting text from input method: supplied length = %d\n, result length = %d", xim_text->length, text_length);
966             }
967         }
968       else
969         {
970           g_warning ("Error converting text from IM to UCS-4: %s", error->message);
971           g_error_free (error);
972
973           *text = NULL;
974           return 0;
975         }
976
977       *text = result;
978       return text_length;
979     }
980   else
981     {
982       *text = NULL;
983       return 0;
984     }
985 }
986
987 static void
988 preedit_draw_callback (XIC                           xic, 
989                        XPointer                      client_data,
990                        XIMPreeditDrawCallbackStruct *call_data)
991 {
992   GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
993
994   XIMText *new_xim_text = call_data->text;
995   gint new_text_length;
996   gunichar *new_text = NULL;
997   gint i;
998   gint diff;
999   gint new_length;
1000   gchar *tmp;
1001   
1002   gint chg_first = CLAMP (call_data->chg_first, 0, context->preedit_length);
1003   gint chg_length = CLAMP (call_data->chg_length, 0, context->preedit_length - chg_first);
1004
1005   context->preedit_cursor = call_data->caret;
1006   
1007   if (chg_first != call_data->chg_first || chg_length != call_data->chg_length)
1008     g_warning ("Invalid change to preedit string, first=%d length=%d (orig length == %d)",
1009                call_data->chg_first, call_data->chg_length, context->preedit_length);
1010
1011   new_text_length = xim_text_to_utf8 (context, new_xim_text, &tmp);
1012   if (tmp)
1013     {
1014       new_text = g_utf8_to_ucs4_fast (tmp, -1, NULL);
1015       g_free (tmp);
1016     }
1017   
1018   diff = new_text_length - chg_length;
1019   new_length = context->preedit_length + diff;
1020
1021   if (new_length > context->preedit_size)
1022     {
1023       context->preedit_size = new_length;
1024       context->preedit_chars = g_renew (gunichar, context->preedit_chars, new_length);
1025       context->feedbacks = g_renew (XIMFeedback, context->feedbacks, new_length);
1026     }
1027
1028   if (diff < 0)
1029     {
1030       for (i = chg_first + chg_length ; i < context->preedit_length; i++)
1031         {
1032           context->preedit_chars[i + diff] = context->preedit_chars[i];
1033           context->feedbacks[i + diff] = context->feedbacks[i];
1034         }
1035     }
1036   else
1037     {
1038       for (i = context->preedit_length - 1; i >= chg_first + chg_length ; i--)
1039         {
1040           context->preedit_chars[i + diff] = context->preedit_chars[i];
1041           context->feedbacks[i + diff] = context->feedbacks[i];
1042         }
1043     }
1044
1045   for (i = 0; i < new_text_length; i++)
1046     {
1047       context->preedit_chars[chg_first + i] = new_text[i];
1048       context->feedbacks[chg_first + i] = new_xim_text->feedback[i];
1049     }
1050
1051   context->preedit_length += diff;
1052
1053   if (new_text)
1054     g_free (new_text);
1055
1056   if (!context->finalizing)
1057     g_signal_emit_by_name (context, "preedit_changed");
1058 }
1059     
1060
1061 static void
1062 preedit_caret_callback (XIC                            xic,
1063                         XPointer                       client_data,
1064                         XIMPreeditCaretCallbackStruct *call_data)
1065 {
1066   GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1067   
1068   if (call_data->direction == XIMAbsolutePosition)
1069     {
1070       context->preedit_cursor = call_data->position;
1071       if (!context->finalizing)
1072         g_signal_emit_by_name (context, "preedit_changed");
1073     }
1074   else
1075     {
1076       g_warning ("Caret movement command: %d %d %d not supported",
1077                  call_data->position, call_data->direction, call_data->style);
1078     }
1079 }            
1080
1081 static void
1082 status_start_callback (XIC      xic,
1083                        XPointer client_data,
1084                        XPointer call_data)
1085 {
1086   return;
1087
1088
1089 static void
1090 status_done_callback (XIC      xic,
1091                       XPointer client_data,
1092                       XPointer call_data)
1093 {
1094   return;
1095 }
1096
1097 static void
1098 status_draw_callback (XIC      xic,
1099                       XPointer client_data,
1100                       XIMStatusDrawCallbackStruct *call_data)
1101 {
1102   GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1103
1104   if (!context->status_visible)
1105     return;
1106
1107   if (call_data->type == XIMTextType)
1108     {
1109       gchar *text;
1110       xim_text_to_utf8 (context, call_data->data.text, &text);
1111
1112       if (text)
1113         status_window_set_text (context, text);
1114       else
1115         status_window_set_text (context, "");
1116     }
1117   else                          /* bitmap */
1118     {
1119       g_print ("Status drawn with bitmap - id = %#lx\n", call_data->data.bitmap);
1120     }
1121 }
1122
1123 static XVaNestedList
1124 set_preedit_callback (GtkIMContextXIM *context_xim)
1125 {
1126   context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
1127   context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
1128   context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
1129   context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
1130   context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
1131   context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
1132   context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
1133   context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
1134   return XVaCreateNestedList (0,
1135                               XNPreeditStartCallback, &context_xim->preedit_start_callback,
1136                               XNPreeditDoneCallback, &context_xim->preedit_done_callback,
1137                               XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
1138                               XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
1139                               NULL);
1140 }
1141
1142 static XVaNestedList
1143 set_status_callback (GtkIMContextXIM *context_xim)
1144 {
1145   context_xim->status_start_callback.client_data = (XPointer)context_xim;
1146   context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
1147   context_xim->status_done_callback.client_data = (XPointer)context_xim;
1148   context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
1149   context_xim->status_draw_callback.client_data = (XPointer)context_xim;
1150   context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
1151           
1152   return XVaCreateNestedList (0,
1153                               XNStatusStartCallback, &context_xim->status_start_callback,
1154                               XNStatusDoneCallback, &context_xim->status_done_callback,
1155                               XNStatusDrawCallback, &context_xim->status_draw_callback,
1156                               NULL);
1157 }
1158
1159
1160 static XIC
1161 gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
1162 {
1163   if (context_xim->im_info == NULL || context_xim->im_info->im == NULL)
1164     return NULL;
1165
1166   if (!context_xim->ic)
1167     {
1168       const char *name1 = NULL;
1169       XVaNestedList list1 = NULL;
1170       const char *name2 = NULL;
1171       XVaNestedList list2 = NULL;
1172       XIMStyle im_style = 0;
1173       XIC xic = 0;
1174
1175       if (context_xim->use_preedit &&
1176           (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
1177         {
1178           im_style |= XIMPreeditCallbacks;
1179           name1 = XNPreeditAttributes;
1180           list1 = set_preedit_callback (context_xim);
1181         }
1182       else if ((context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditNone)
1183         im_style |= XIMPreeditNone;
1184       else
1185         im_style |= XIMPreeditNothing;
1186
1187       if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
1188         {
1189           im_style |= XIMStatusCallbacks;
1190           if (name1 == NULL)
1191             {
1192               name1 = XNStatusAttributes;
1193               list1 = set_status_callback (context_xim);
1194             }
1195           else
1196             {
1197               name2 = XNStatusAttributes;
1198               list2 = set_status_callback (context_xim);
1199             }
1200         }
1201       else if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusNone)
1202         im_style |= XIMStatusNone;
1203       else
1204         im_style |= XIMStatusNothing;
1205
1206       xic = XCreateIC (context_xim->im_info->im,
1207                        XNInputStyle, im_style,
1208                        XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
1209                        name1, list1,
1210                        name2, list2,
1211                        NULL);
1212       if (list1)
1213         XFree (list1);
1214       if (list2)
1215         XFree (list2);
1216
1217       if (xic)
1218         {
1219           /* Don't filter key released events with XFilterEvents unless
1220            * input methods ask for. This is a workaround for Solaris input
1221            * method bug in C and European locales. It doubles each key
1222            * stroke if both key pressed and released events are filtered.
1223            * (bugzilla #81759)
1224            */
1225           guint32 mask = 0;
1226           XGetICValues (xic,
1227                         XNFilterEvents, &mask,
1228                         NULL);
1229           context_xim->filter_key_release = (mask & KeyReleaseMask);
1230         }
1231       context_xim->ic = xic;
1232     }
1233   return context_xim->ic;
1234 }
1235
1236 /**************************
1237  *                        *
1238  * Status Window handling *
1239  *                        *
1240  **************************/
1241
1242 static gboolean
1243 status_window_expose_event (GtkWidget      *widget,
1244                             GdkEventExpose *event)
1245 {
1246   gdk_draw_rectangle (widget->window,
1247                       widget->style->base_gc [GTK_STATE_NORMAL],
1248                       TRUE,
1249                       0, 0,
1250                       widget->allocation.width, widget->allocation.height);
1251   gdk_draw_rectangle (widget->window,
1252                       widget->style->text_gc [GTK_STATE_NORMAL],
1253                       FALSE,
1254                       0, 0,
1255                       widget->allocation.width - 1, widget->allocation.height - 1);
1256
1257   return FALSE;
1258 }
1259
1260 static void
1261 status_window_style_set (GtkWidget *toplevel,
1262                          GtkStyle  *previous_style,
1263                          GtkWidget *label)
1264 {
1265   gint i;
1266   
1267   for (i = 0; i < 5; i++)
1268     gtk_widget_modify_fg (label, i, &toplevel->style->text[i]);
1269 }
1270
1271 /* Frees a status window and removes its link from the status_windows list */
1272 static void
1273 status_window_free (StatusWindow *status_window)
1274 {
1275   status_windows = g_slist_remove (status_windows, status_window);
1276  
1277   g_signal_handler_disconnect (status_window->toplevel, status_window->destroy_handler_id);
1278   g_signal_handler_disconnect (status_window->toplevel, status_window->configure_handler_id);
1279   gtk_widget_destroy (status_window->window);
1280   g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
1281  
1282   g_free (status_window);
1283 }
1284
1285 static gboolean
1286 status_window_configure (GtkWidget         *toplevel,
1287                          GdkEventConfigure *event,
1288                          StatusWindow      *status_window)
1289 {
1290   GdkRectangle rect;
1291   GtkRequisition requisition;
1292   gint y;
1293   gint height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
1294   
1295   gdk_window_get_frame_extents (toplevel->window, &rect);
1296   gtk_widget_size_request (status_window->window, &requisition);
1297
1298   if (rect.y + rect.height + requisition.height < height)
1299     y = rect.y + rect.height;
1300   else
1301     y = height - requisition.height;
1302   
1303   gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
1304
1305   return FALSE;
1306 }
1307
1308 static GtkWidget *
1309 status_window_get (GtkIMContextXIM *context_xim,
1310                    gboolean         create)
1311 {
1312   GdkWindow *toplevel_gdk;
1313   GtkWidget *toplevel;
1314   GtkWidget *window;
1315   StatusWindow *status_window;
1316   GtkWidget *status_label;
1317   GdkScreen *screen;
1318   GdkWindow *root_window;
1319   
1320   if (!context_xim->client_window)
1321     return NULL;
1322
1323   toplevel_gdk = context_xim->client_window;
1324   screen = gdk_drawable_get_screen (toplevel_gdk);
1325   root_window = gdk_screen_get_root_window (screen);
1326   
1327   while (TRUE)
1328     {
1329       GdkWindow *parent = gdk_window_get_parent (toplevel_gdk);
1330       if (parent == root_window)
1331         break;
1332       else
1333         toplevel_gdk = parent;
1334     }
1335
1336   gdk_window_get_user_data (toplevel_gdk, (gpointer *)&toplevel);
1337   if (!toplevel)
1338     return NULL;
1339
1340   status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
1341   if (status_window)
1342     return status_window->window;
1343   else if (!create)
1344     return NULL;
1345
1346   status_window = g_new (StatusWindow, 1);
1347   status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
1348   status_window->toplevel = toplevel;
1349
1350   status_windows = g_slist_prepend (status_windows, status_window);
1351
1352   window = status_window->window;
1353
1354   gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
1355   gtk_widget_set_app_paintable (window, TRUE);
1356
1357   status_label = gtk_label_new ("");
1358   gtk_misc_set_padding (GTK_MISC (status_label), 1, 1);
1359   gtk_widget_show (status_label);
1360   
1361   gtk_container_add (GTK_CONTAINER (window), status_label);
1362
1363   status_window->destroy_handler_id = g_signal_connect_swapped (toplevel, "destroy",
1364                                                                 G_CALLBACK (status_window_free),
1365                                                                 status_window);
1366   status_window->configure_handler_id = g_signal_connect (toplevel, "configure_event",
1367                                                           G_CALLBACK (status_window_configure),
1368                                                           status_window);
1369
1370   status_window_configure (toplevel, NULL, status_window);
1371
1372   g_signal_connect (window, "style_set",
1373                     G_CALLBACK (status_window_style_set), status_label);
1374   g_signal_connect (window, "expose_event",
1375                     G_CALLBACK (status_window_expose_event), NULL);
1376
1377   g_object_set_data (G_OBJECT (toplevel), "gtk-im-xim-status-window", status_window);
1378
1379   return window;
1380 }
1381
1382 static gboolean
1383 status_window_has_text (GtkWidget *status_window)
1384 {
1385   GtkWidget *label = GTK_BIN (status_window)->child;
1386   const gchar *text = gtk_label_get_text (GTK_LABEL (label));
1387
1388   return text[0] != '\0';
1389 }
1390
1391 static void
1392 status_window_show (GtkIMContextXIM *context_xim)
1393 {
1394   context_xim->status_visible = TRUE;
1395 }
1396
1397 static void
1398 status_window_hide (GtkIMContextXIM *context_xim)
1399 {
1400   GtkWidget *status_window = status_window_get (context_xim, FALSE);
1401
1402   context_xim->status_visible = FALSE;
1403
1404   if (status_window)
1405     status_window_set_text (context_xim, "");
1406 }
1407
1408 static void
1409 status_window_set_text (GtkIMContextXIM *context_xim,
1410                         const gchar     *text)
1411 {
1412   GtkWidget *status_window = status_window_get (context_xim, TRUE);
1413
1414   if (status_window)
1415     {
1416       GtkWidget *label = GTK_BIN (status_window)->child;
1417       gtk_label_set_text (GTK_LABEL (label), text);
1418       
1419       if (context_xim->status_visible && status_window_has_text (status_window))
1420         gtk_widget_show (status_window);
1421       else
1422         gtk_widget_hide (status_window);
1423     }
1424 }
1425
1426 /**
1427  * gtk_im_context_xim_shutdown:
1428  * 
1429  * Destroys all the status windows that are kept by the XIM contexts.  This
1430  * function should only be called by the XIM module exit routine.
1431  **/
1432 void
1433 gtk_im_context_xim_shutdown (void)
1434 {
1435   while (status_windows)
1436     status_window_free (status_windows->data);
1437 }