]> Pileus Git - ~andy/gtk/blob - modules/input/gtkimcontextxim.c
Patch to add support for string conversion callbacks 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 <config.h>
21 #include "locale.h"
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include "gtk/gtkintl.h"
26 #include "gtk/gtklabel.h"
27 #include "gtk/gtksignal.h"
28 #include "gtk/gtkwindow.h"
29 #include "gtkimcontextxim.h"
30
31 typedef struct _StatusWindow StatusWindow;
32 typedef struct _GtkXIMInfo GtkXIMInfo;
33
34 struct _GtkIMContextXIM
35 {
36   GtkIMContext object;
37
38   GtkXIMInfo *im_info;
39
40   gchar *locale;
41   gchar *mb_charset;
42
43   GdkWindow *client_window;
44   GtkWidget *client_widget;
45
46   /* The status window for this input context; we claim the
47    * status window when we are focused and have created an XIC
48    */
49   StatusWindow *status_window;
50
51   gint preedit_size;
52   gint preedit_length;
53   gunichar *preedit_chars;
54   XIMFeedback *feedbacks;
55
56   gint preedit_cursor;
57   
58   XIMCallback preedit_start_callback;
59   XIMCallback preedit_done_callback;
60   XIMCallback preedit_draw_callback;
61   XIMCallback preedit_caret_callback;
62
63   XIMCallback status_start_callback;
64   XIMCallback status_done_callback;
65   XIMCallback status_draw_callback;
66
67   XICCallback string_conversion_callback;
68
69   XIC ic;
70
71   guint filter_key_release : 1;
72   guint use_preedit : 1;
73   guint finalizing : 1;
74   guint in_toplevel : 1;
75   guint has_focus : 1;
76 };
77
78 struct _GtkXIMInfo
79 {
80   GdkScreen *screen;
81   XIM im;
82   char *locale;
83   XIMStyle preedit_style_setting;
84   XIMStyle status_style_setting;
85   XIMStyle style;
86   GtkSettings *settings;
87   gulong status_set;
88   gulong preedit_set;
89   XIMStyles *xim_styles;
90   GSList *ics;
91
92   guint reconnecting :1;
93   guint supports_string_conversion;
94 };
95
96 /* A context status window; these are kept in the status_windows list. */
97 struct _StatusWindow
98 {
99   GtkWidget *window;
100   
101   /* Toplevel window to which the status window corresponds */
102   GtkWidget *toplevel;
103
104   /* Currently focused GtkIMContextXIM for the toplevel, if any */
105   GtkIMContextXIM *context;
106 };
107
108 static void     gtk_im_context_xim_class_init         (GtkIMContextXIMClass  *class);
109 static void     gtk_im_context_xim_init               (GtkIMContextXIM       *im_context_xim);
110 static void     gtk_im_context_xim_finalize           (GObject               *obj);
111 static void     gtk_im_context_xim_set_client_window  (GtkIMContext          *context,
112                                                        GdkWindow             *client_window);
113 static gboolean gtk_im_context_xim_filter_keypress    (GtkIMContext          *context,
114                                                        GdkEventKey           *key);
115 static void     gtk_im_context_xim_reset              (GtkIMContext          *context);
116 static void     gtk_im_context_xim_focus_in           (GtkIMContext          *context);
117 static void     gtk_im_context_xim_focus_out          (GtkIMContext          *context);
118 static void     gtk_im_context_xim_set_cursor_location (GtkIMContext          *context,
119                                                        GdkRectangle             *area);
120 static void     gtk_im_context_xim_set_use_preedit    (GtkIMContext          *context,
121                                                        gboolean               use_preedit);
122 static void     gtk_im_context_xim_get_preedit_string (GtkIMContext          *context,
123                                                        gchar                **str,
124                                                        PangoAttrList        **attrs,
125                                                        gint                  *cursor_pos);
126
127 static void reinitialize_ic      (GtkIMContextXIM *context_xim);
128 static void set_ic_client_window (GtkIMContextXIM *context_xim,
129                                   GdkWindow       *client_window);
130
131 static void setup_styles (GtkXIMInfo *info);
132
133 static void update_client_widget   (GtkIMContextXIM *context_xim);
134 static void update_status_window   (GtkIMContextXIM *context_xim);
135
136 static StatusWindow *status_window_get      (GtkWidget    *toplevel);
137 static void          status_window_free     (StatusWindow *status_window);
138 static void          status_window_set_text (StatusWindow *status_window,
139                                              const gchar  *text);
140
141 static void xim_destroy_callback   (XIM      xim,
142                                     XPointer client_data,
143                                     XPointer call_data);
144
145 static XIC       gtk_im_context_xim_get_ic            (GtkIMContextXIM *context_xim);
146 static GObjectClass *parent_class;
147
148 GType gtk_type_im_context_xim = 0;
149
150 GSList *open_ims = NULL;
151
152 /* List of status windows for different toplevels */
153 static GSList *status_windows = NULL;
154
155 void
156 gtk_im_context_xim_register_type (GTypeModule *type_module)
157 {
158   static const GTypeInfo im_context_xim_info =
159   {
160     sizeof (GtkIMContextXIMClass),
161     (GBaseInitFunc) NULL,
162     (GBaseFinalizeFunc) NULL,
163     (GClassInitFunc) gtk_im_context_xim_class_init,
164     NULL,           /* class_finalize */    
165     NULL,           /* class_data */
166     sizeof (GtkIMContextXIM),
167     0,
168     (GInstanceInitFunc) gtk_im_context_xim_init,
169   };
170
171   gtk_type_im_context_xim = 
172     g_type_module_register_type (type_module,
173                                  GTK_TYPE_IM_CONTEXT,
174                                  "GtkIMContextXIM",
175                                  &im_context_xim_info, 0);
176 }
177
178 #define PREEDIT_MASK (XIMPreeditCallbacks | XIMPreeditPosition | \
179                       XIMPreeditArea | XIMPreeditNothing | XIMPreeditNone)
180 #define STATUS_MASK (XIMStatusCallbacks | XIMStatusArea | \
181                       XIMStatusNothing | XIMStatusNone)
182 #define ALLOWED_MASK (XIMPreeditCallbacks | XIMPreeditNothing | XIMPreeditNone | \
183                       XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone)
184
185 static XIMStyle 
186 choose_better_style (XIMStyle style1, XIMStyle style2) 
187 {
188   XIMStyle s1, s2, u; 
189   
190   if (style1 == 0) return style2;
191   if (style2 == 0) return style1;
192   if ((style1 & (PREEDIT_MASK | STATUS_MASK))
193         == (style2 & (PREEDIT_MASK | STATUS_MASK)))
194     return style1;
195
196   s1 = style1 & PREEDIT_MASK;
197   s2 = style2 & PREEDIT_MASK;
198   u = s1 | s2;
199   if (s1 != s2) {
200     if (u & XIMPreeditCallbacks)
201       return (s1 == XIMPreeditCallbacks) ? style1 : style2;
202     else if (u & XIMPreeditPosition)
203       return (s1 == XIMPreeditPosition) ? style1 :style2;
204     else if (u & XIMPreeditArea)
205       return (s1 == XIMPreeditArea) ? style1 : style2;
206     else if (u & XIMPreeditNothing)
207       return (s1 == XIMPreeditNothing) ? style1 : style2;
208     else if (u & XIMPreeditNone)
209       return (s1 == XIMPreeditNone) ? style1 : style2;
210   } else {
211     s1 = style1 & STATUS_MASK;
212     s2 = style2 & STATUS_MASK;
213     u = s1 | s2;
214     if (u & XIMStatusCallbacks)
215       return (s1 == XIMStatusCallbacks) ? style1 : style2;
216     else if (u & XIMStatusArea)
217       return (s1 == XIMStatusArea) ? style1 : style2;
218     else if (u & XIMStatusNothing)
219       return (s1 == XIMStatusNothing) ? style1 : style2;
220     else if (u & XIMStatusNone)
221       return (s1 == XIMStatusNone) ? style1 : style2;
222   }
223   return 0; /* Get rid of stupid warning */
224 }
225
226 static void
227 reinitialize_all_ics (GtkXIMInfo *info)
228 {
229   GSList *tmp_list;
230
231   for (tmp_list = info->ics; tmp_list; tmp_list = tmp_list->next)
232     reinitialize_ic (tmp_list->data);
233 }
234
235 static void
236 status_style_change (GtkXIMInfo *info)
237 {
238   GtkIMStatusStyle status_style;
239   
240   g_object_get (info->settings,
241                 "gtk-im-status-style", &status_style,
242                 NULL);
243   if (status_style == GTK_IM_STATUS_CALLBACK)
244     info->status_style_setting = XIMStatusCallbacks;
245   else if (status_style == GTK_IM_STATUS_NOTHING)
246     info->status_style_setting = XIMStatusNothing;
247   else if (status_style == GTK_IM_STATUS_NONE)
248     info->status_style_setting = XIMStatusNone;
249   else
250     return;
251
252   setup_styles (info);
253   
254   reinitialize_all_ics (info);
255 }
256
257 static void
258 preedit_style_change (GtkXIMInfo *info)
259 {
260   GtkIMPreeditStyle preedit_style;
261   g_object_get (info->settings,
262                 "gtk-im-preedit-style", &preedit_style,
263                 NULL);
264   if (preedit_style == GTK_IM_PREEDIT_CALLBACK)
265     info->preedit_style_setting = XIMPreeditCallbacks;
266   else if (preedit_style == GTK_IM_PREEDIT_NOTHING)
267     info->preedit_style_setting = XIMPreeditNothing;
268   else if (preedit_style == GTK_IM_PREEDIT_NONE)
269     info->preedit_style_setting = XIMPreeditNone;
270   else
271     return;
272
273   setup_styles (info);
274   
275   reinitialize_all_ics (info);
276 }
277
278 static void
279 setup_styles (GtkXIMInfo *info)
280 {
281   int i;
282   unsigned long settings_preference;
283   XIMStyles *xim_styles = info->xim_styles;
284
285   settings_preference = info->status_style_setting|info->preedit_style_setting;
286   info->style = 0;
287   if (xim_styles)
288     {
289       for (i = 0; i < xim_styles->count_styles; i++)
290         if ((xim_styles->supported_styles[i] & ALLOWED_MASK) == xim_styles->supported_styles[i])
291           {
292             if (settings_preference == xim_styles->supported_styles[i])
293               {
294                 info->style = settings_preference;
295                 break;
296               }
297             info->style = choose_better_style (info->style,
298                                                xim_styles->supported_styles[i]);
299           }
300     }
301   if (info->style == 0)
302     info->style = XIMPreeditNothing | XIMStatusNothing;
303 }
304
305 static void
306 setup_im (GtkXIMInfo *info)
307 {
308   XIMValuesList *ic_values = NULL;
309   XIMCallback im_destroy_callback;
310
311   if (info->im == NULL)
312     return;
313
314   im_destroy_callback.client_data = (XPointer)info;
315   im_destroy_callback.callback = (XIMProc)xim_destroy_callback;
316   XSetIMValues (info->im,
317                 XNDestroyCallback, &im_destroy_callback,
318                 NULL);
319
320   XGetIMValues (info->im,
321                 XNQueryInputStyle, &info->xim_styles,
322                 XNQueryICValuesList, &ic_values,
323                 NULL);
324
325   info->settings = gtk_settings_get_for_screen (info->screen);
326
327   if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
328                                      "gtk-im-preedit-style"))
329     gtk_settings_install_property (g_param_spec_enum ("gtk-im-preedit-style",
330                                                       P_("IM Preedit style"),
331                                                       P_("How to draw the input method preedit string"),
332                                                       GTK_TYPE_IM_PREEDIT_STYLE,
333                                                       GTK_IM_PREEDIT_CALLBACK,
334                                                       G_PARAM_READWRITE));
335
336   if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
337                                      "gtk-im-status-style"))
338     gtk_settings_install_property (g_param_spec_enum ("gtk-im-status-style",
339                                                       P_("IM Status style"),
340                                                       P_("How to draw the input method statusbar"),
341                                                       GTK_TYPE_IM_STATUS_STYLE,
342                                                       GTK_IM_STATUS_CALLBACK,
343                                                       G_PARAM_READWRITE));
344
345   info->status_set = g_signal_connect_swapped (info->settings,
346                                                "notify::gtk-im-status-style",
347                                                G_CALLBACK (status_style_change),
348                                                info);
349   info->preedit_set = g_signal_connect_swapped (info->settings,
350                                                 "notify::gtk-im-preedit-style",
351                                                 G_CALLBACK (preedit_style_change),
352                                                 info);
353
354   info->supports_string_conversion = FALSE;
355   if (ic_values)
356     {
357       int i;
358       
359       for (i = 0; i < ic_values->count_values; i++)
360         if (strcmp (ic_values->supported_values[i],
361                     XNStringConversionCallback) == 0)
362           {
363             info->supports_string_conversion = TRUE;
364             break;
365           }
366
367 #if 0
368       for (i = 0; i < ic_values->count_values; i++)
369         g_print ("%s\n", ic_values->supported_values[i]);
370       for (i = 0; i < xim_styles->count_styles; i++)
371         g_print ("%#x\n", xim_styles->supported_styles[i]);
372 #endif
373       
374       XFree (ic_values);
375     }
376
377   status_style_change (info);
378   preedit_style_change (info);
379 }
380
381 static void
382 xim_info_display_closed (GdkDisplay *display,
383                          gboolean    is_error,
384                          GtkXIMInfo *info)
385 {
386   GSList *ics, *tmp_list;
387
388   open_ims = g_slist_remove (open_ims, info);
389
390   ics = info->ics;
391   info->ics = NULL;
392
393   for (tmp_list = ics; tmp_list; tmp_list = tmp_list->next)
394     set_ic_client_window (tmp_list->data, NULL);
395
396   g_slist_free (ics);
397   
398   g_signal_handler_disconnect (info->settings, info->status_set);
399   g_signal_handler_disconnect (info->settings, info->preedit_set);
400   
401   XFree (info->xim_styles->supported_styles);
402   XFree (info->xim_styles);
403   g_free (info->locale);
404
405   if (info->im)
406     XCloseIM (info->im);
407
408   g_free (info);
409 }
410
411 static void
412 xim_instantiate_callback (Display *display, XPointer client_data,
413                           XPointer call_data)
414 {
415   GtkXIMInfo *info = (GtkXIMInfo*)client_data;
416   XIM im = NULL;
417
418   im = XOpenIM (display, NULL, NULL, NULL);
419
420   if (!im)
421     return;
422
423   info->im = im;
424   setup_im (info);
425
426   XUnregisterIMInstantiateCallback (display, NULL, NULL, NULL,
427                                     xim_instantiate_callback,
428                                     (XPointer)info);
429   info->reconnecting = FALSE;
430 }
431
432 /* initialize info->im */
433 static void
434 xim_info_try_im (GtkXIMInfo *info)
435 {
436   GdkScreen *screen = info->screen;
437   GdkDisplay *display = gdk_screen_get_display (screen);
438
439   g_assert (info->im == NULL);
440   if (info->reconnecting)
441     return;
442
443   if (XSupportsLocale ())
444     {
445       if (!XSetLocaleModifiers (""))
446         g_warning ("Unable to set locale modifiers with XSetLocaleModifiers()");
447       info->im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
448       if (!info->im)
449         {
450           XRegisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY(display),
451                                           NULL, NULL, NULL,
452                                           xim_instantiate_callback,
453                                           (XPointer)info);
454           info->reconnecting = TRUE;
455           return;
456         }
457       setup_im (info);
458
459       g_signal_connect (display, "closed",
460                         G_CALLBACK (xim_info_display_closed), info);
461     }
462 }
463
464 static void
465 xim_destroy_callback (XIM      xim,
466                       XPointer client_data,
467                       XPointer call_data)
468 {
469   GtkXIMInfo *info = (GtkXIMInfo*)client_data;
470
471   info->im = NULL;
472
473   g_signal_handler_disconnect (info->settings, info->status_set);
474   g_signal_handler_disconnect (info->settings, info->preedit_set);
475
476   reinitialize_all_ics (info);
477   xim_info_try_im (info);
478   return;
479
480
481 static GtkXIMInfo *
482 get_im (GdkWindow *client_window,
483         const char *locale)
484 {
485   GSList *tmp_list;
486   GtkXIMInfo *info;
487   GdkScreen *screen = gdk_drawable_get_screen (client_window);
488
489   info = NULL;
490   tmp_list = open_ims;
491   while (tmp_list)
492     {
493       GtkXIMInfo *tmp_info = tmp_list->data;
494       if (tmp_info->screen == screen &&
495           strcmp (tmp_info->locale, locale) == 0)
496         {
497           if (tmp_info->im)
498             {
499               return tmp_info;
500             }
501           else
502             {
503               tmp_info = tmp_info;
504               break;
505             }
506         }
507       tmp_list = tmp_list->next;
508     }
509
510   if (info == NULL)
511     {
512       info = g_new (GtkXIMInfo, 1);
513       open_ims = g_slist_prepend (open_ims, info);
514
515       info->screen = screen;
516       info->locale = g_strdup (locale);
517       info->xim_styles = NULL;
518       info->preedit_style_setting = 0;
519       info->status_style_setting = 0;
520       info->settings = NULL;
521       info->preedit_set = 0;
522       info->status_set = 0;
523       info->ics = NULL;
524       info->reconnecting = FALSE;
525       info->im = NULL;
526     }
527
528   xim_info_try_im (info);
529   return info;
530 }
531
532 static void
533 gtk_im_context_xim_class_init (GtkIMContextXIMClass *class)
534 {
535   GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
536   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
537
538   parent_class = g_type_class_peek_parent (class);
539
540   im_context_class->set_client_window = gtk_im_context_xim_set_client_window;
541   im_context_class->filter_keypress = gtk_im_context_xim_filter_keypress;
542   im_context_class->reset = gtk_im_context_xim_reset;
543   im_context_class->get_preedit_string = gtk_im_context_xim_get_preedit_string;
544   im_context_class->focus_in = gtk_im_context_xim_focus_in;
545   im_context_class->focus_out = gtk_im_context_xim_focus_out;
546   im_context_class->set_cursor_location = gtk_im_context_xim_set_cursor_location;
547   im_context_class->set_use_preedit = gtk_im_context_xim_set_use_preedit;
548   gobject_class->finalize = gtk_im_context_xim_finalize;
549 }
550
551 static void
552 gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
553 {
554   im_context_xim->use_preedit = TRUE;
555   im_context_xim->filter_key_release = FALSE;
556   im_context_xim->finalizing = FALSE;
557   im_context_xim->has_focus = FALSE;
558   im_context_xim->in_toplevel = FALSE;
559 }
560
561 static void
562 gtk_im_context_xim_finalize (GObject *obj)
563 {
564   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (obj);
565
566   context_xim->finalizing = TRUE;
567
568   set_ic_client_window (context_xim, NULL);
569
570   g_free (context_xim->locale);
571   g_free (context_xim->mb_charset);
572
573   G_OBJECT_CLASS (parent_class)->finalize (obj);
574 }
575
576 static void
577 reinitialize_ic (GtkIMContextXIM *context_xim)
578 {
579   if (context_xim->ic)
580     {
581       XDestroyIC (context_xim->ic);
582       context_xim->ic = NULL;
583       update_status_window (context_xim);
584
585       if (context_xim->preedit_length)
586         {
587           context_xim->preedit_length = 0;
588           if (!context_xim->finalizing)
589             g_signal_emit_by_name (context_xim, "preedit_changed");
590         }
591     }
592   /* 
593      reset filter_key_release flag, otherwise keystrokes will be doubled
594      until reconnecting to XIM.
595   */
596   context_xim->filter_key_release = FALSE;
597 }
598
599 static void
600 set_ic_client_window (GtkIMContextXIM *context_xim,
601                       GdkWindow       *client_window)
602 {
603   reinitialize_ic (context_xim);
604   if (context_xim->client_window)
605     {
606       context_xim->im_info->ics = g_slist_remove (context_xim->im_info->ics, context_xim);
607       context_xim->im_info = NULL;
608     }
609   
610   context_xim->client_window = client_window;
611
612   if (context_xim->client_window)
613     {
614       context_xim->im_info = get_im (context_xim->client_window, context_xim->locale);
615       context_xim->im_info->ics = g_slist_prepend (context_xim->im_info->ics, context_xim);
616     }
617   
618   update_client_widget (context_xim);
619 }
620
621 static void
622 gtk_im_context_xim_set_client_window (GtkIMContext          *context,
623                                       GdkWindow             *client_window)
624 {
625   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
626
627   set_ic_client_window (context_xim, client_window);
628 }
629
630 GtkIMContext *
631 gtk_im_context_xim_new (void)
632 {
633   GtkIMContextXIM *result;
634   const gchar *charset;
635
636   result = GTK_IM_CONTEXT_XIM (g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL));
637
638   result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
639   
640   g_get_charset (&charset);
641   result->mb_charset = g_strdup (charset);
642
643   return GTK_IM_CONTEXT (result);
644 }
645
646 static char *
647 mb_to_utf8 (GtkIMContextXIM *context_xim,
648             const char      *str)
649 {
650   GError *error = NULL;
651   gchar *result;
652
653   if (strcmp (context_xim->mb_charset, "UTF-8") == 0)
654     result = g_strdup (str);
655   else
656     {
657       result = g_convert (str, -1,
658                           "UTF-8", context_xim->mb_charset,
659                           NULL, NULL, &error);
660       if (!result)
661         {
662           g_warning ("Error converting text from IM to UTF-8: %s\n", error->message);
663           g_error_free (error);
664         }
665     }
666   
667   return result;
668 }
669
670 static gboolean
671 gtk_im_context_xim_filter_keypress (GtkIMContext *context,
672                                     GdkEventKey  *event)
673 {
674   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
675   XIC ic = gtk_im_context_xim_get_ic (context_xim);
676   gchar static_buffer[256];
677   gchar *buffer = static_buffer;
678   gint buffer_size = sizeof(static_buffer) - 1;
679   gint num_bytes = 0;
680   KeySym keysym;
681   Status status;
682   gboolean result = FALSE;
683   GdkWindow *root_window = gdk_screen_get_root_window (gdk_drawable_get_screen (event->window));
684
685   XKeyPressedEvent xevent;
686
687   if (event->type == GDK_KEY_RELEASE && !context_xim->filter_key_release)
688     return FALSE;
689
690   xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
691   xevent.serial = 0;            /* hope it doesn't matter */
692   xevent.send_event = event->send_event;
693   xevent.display = GDK_DRAWABLE_XDISPLAY (event->window);
694   xevent.window = GDK_DRAWABLE_XID (event->window);
695   xevent.root = GDK_DRAWABLE_XID (root_window);
696   xevent.subwindow = xevent.window;
697   xevent.time = event->time;
698   xevent.x = xevent.x_root = 0;
699   xevent.y = xevent.y_root = 0;
700   xevent.state = event->state;
701   xevent.keycode = event->hardware_keycode;
702   xevent.same_screen = True;
703   
704   if (XFilterEvent ((XEvent *)&xevent, GDK_DRAWABLE_XID (context_xim->client_window)))
705     return TRUE;
706   
707  again:
708   if (ic)
709     num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
710   else
711     {
712       num_bytes = XLookupString (&xevent, buffer, buffer_size, &keysym, NULL);
713       status = XLookupBoth;
714     }
715
716   if (status == XBufferOverflow)
717     {
718       buffer_size = num_bytes;
719       if (buffer != static_buffer) 
720         g_free (buffer);
721       buffer = g_malloc (num_bytes + 1);
722       goto again;
723     }
724
725   /* I don't know how we should properly handle XLookupKeysym or XLookupBoth
726    * here ... do input methods actually change the keysym? we can't really
727    * feed it back to accelerator processing at this point...
728    */
729   if (status == XLookupChars || status == XLookupBoth)
730     {
731       char *result_utf8;
732
733       buffer[num_bytes] = '\0';
734
735       result_utf8 = mb_to_utf8 (context_xim, buffer);
736       if (result_utf8)
737         {
738           if ((guchar)result_utf8[0] >= 0x20 &&
739               result_utf8[0] != 0x7f) /* Some IM have a nasty habit of converting
740                                        * control characters into strings
741                                        */
742             {
743               g_signal_emit_by_name (context, "commit", result_utf8);
744               result = TRUE;
745             }
746           
747           g_free (result_utf8);
748         }
749     }
750
751   if (buffer != static_buffer) 
752     g_free (buffer);
753
754   return result;
755 }
756
757 static void
758 gtk_im_context_xim_focus_in (GtkIMContext *context)
759 {
760   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
761
762   if (!context_xim->has_focus)
763     {
764       XIC ic = gtk_im_context_xim_get_ic (context_xim);
765
766       context_xim->has_focus = TRUE;
767       update_status_window (context_xim);
768       
769       if (ic)
770         XSetICFocus (ic);
771     }
772
773   return;
774 }
775
776 static void
777 gtk_im_context_xim_focus_out (GtkIMContext *context)
778 {
779   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
780
781   if (context_xim->has_focus)
782     {
783       XIC ic = gtk_im_context_xim_get_ic (context_xim);
784       
785       context_xim->has_focus = FALSE;
786       update_status_window (context_xim);
787   
788       if (ic)
789         XUnsetICFocus (ic);
790     }
791
792   return;
793 }
794
795 static void
796 gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
797                                         GdkRectangle *area)
798 {
799   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
800   XIC ic = gtk_im_context_xim_get_ic (context_xim);
801
802   XVaNestedList preedit_attr;
803   XPoint          spot;
804
805   if (!ic)
806     return;
807
808   spot.x = area->x;
809   spot.y = area->y;
810
811   preedit_attr = XVaCreateNestedList (0,
812                                       XNSpotLocation, &spot,
813                                       0);
814   XSetICValues (ic,
815                 XNPreeditAttributes, preedit_attr,
816                 NULL);
817   XFree(preedit_attr);
818
819   return;
820 }
821
822 static void
823 gtk_im_context_xim_set_use_preedit (GtkIMContext *context,
824                                     gboolean      use_preedit)
825 {
826   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
827
828   use_preedit = use_preedit != FALSE;
829
830   if (context_xim->use_preedit != use_preedit)
831     {
832       context_xim->use_preedit = use_preedit;
833       reinitialize_ic (context_xim);
834     }
835
836   return;
837 }
838
839 static void
840 gtk_im_context_xim_reset (GtkIMContext *context)
841 {
842   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
843   XIC ic = gtk_im_context_xim_get_ic (context_xim);
844   gchar *result;
845
846   /* restore conversion state after resetting ic later */
847   XIMPreeditState preedit_state = XIMPreeditUnKnown;
848   XVaNestedList preedit_attr;
849   gboolean have_preedit_state = FALSE;
850
851   if (!ic)
852     return;
853   
854
855   if (context_xim->preedit_length == 0)
856     return;
857
858   preedit_attr = XVaCreateNestedList(0,
859                                      XNPreeditState, &preedit_state,
860                                      0);
861   if (!XGetICValues(ic,
862                     XNPreeditAttributes, preedit_attr,
863                     NULL))
864     have_preedit_state = TRUE;
865
866   XFree(preedit_attr);
867
868   result = XmbResetIC (ic);
869
870   preedit_attr = XVaCreateNestedList(0,
871                                      XNPreeditState, preedit_state,
872                                      0);
873   if (have_preedit_state)
874     XSetICValues(ic,
875                  XNPreeditAttributes, preedit_attr,
876                  NULL);
877
878   XFree(preedit_attr);
879
880   if (result)
881     {
882       char *result_utf8 = mb_to_utf8 (context_xim, result);
883       if (result_utf8)
884         {
885           g_signal_emit_by_name (context, "commit", result_utf8);
886           g_free (result_utf8);
887         }
888     }
889
890   if (context_xim->preedit_length)
891     {
892       context_xim->preedit_length = 0;
893       g_signal_emit_by_name (context, "preedit_changed");
894     }
895
896   XFree (result);
897 }
898
899 /* Mask of feedback bits that we render
900  */
901 #define FEEDBACK_MASK (XIMReverse | XIMUnderline)
902
903 static void
904 add_feedback_attr (PangoAttrList *attrs,
905                    const gchar   *str,
906                    XIMFeedback    feedback,
907                    gint           start_pos,
908                    gint           end_pos)
909 {
910   PangoAttribute *attr;
911   
912   gint start_index = g_utf8_offset_to_pointer (str, start_pos) - str;
913   gint end_index = g_utf8_offset_to_pointer (str, end_pos) - str;
914
915   if (feedback & XIMUnderline)
916     {
917       attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
918       attr->start_index = start_index;
919       attr->end_index = end_index;
920
921       pango_attr_list_change (attrs, attr);
922     }
923
924   if (feedback & XIMReverse)
925     {
926       attr = pango_attr_foreground_new (0xffff, 0xffff, 0xffff);
927       attr->start_index = start_index;
928       attr->end_index = end_index;
929
930       pango_attr_list_change (attrs, attr);
931
932       attr = pango_attr_background_new (0, 0, 0);
933       attr->start_index = start_index;
934       attr->end_index = end_index;
935
936       pango_attr_list_change (attrs, attr);
937     }
938
939   if (feedback & ~FEEDBACK_MASK)
940     g_warning ("Unrendered feedback style: %#lx", feedback & ~FEEDBACK_MASK);
941 }
942
943 static void     
944 gtk_im_context_xim_get_preedit_string (GtkIMContext   *context,
945                                        gchar         **str,
946                                        PangoAttrList **attrs,
947                                        gint           *cursor_pos)
948 {
949   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
950   gchar *utf8 = g_ucs4_to_utf8 (context_xim->preedit_chars, context_xim->preedit_length, NULL, NULL, NULL);
951
952   if (attrs)
953     {
954       int i;
955       XIMFeedback last_feedback = 0;
956       gint start = -1;
957       
958       *attrs = pango_attr_list_new ();
959
960       for (i = 0; i < context_xim->preedit_length; i++)
961         {
962           XIMFeedback new_feedback = context_xim->feedbacks[i] & FEEDBACK_MASK;
963           if (new_feedback != last_feedback)
964             {
965               if (start >= 0)
966                 add_feedback_attr (*attrs, utf8, last_feedback, start, i);
967               
968               last_feedback = new_feedback;
969               start = i;
970             }
971         }
972
973       if (start >= 0)
974         add_feedback_attr (*attrs, utf8, last_feedback, start, i);
975     }
976
977   if (str)
978     *str = utf8;
979   else
980     g_free (utf8);
981
982   if (cursor_pos)
983     *cursor_pos = context_xim->preedit_cursor;
984 }
985
986 static int
987 preedit_start_callback (XIC      xic,
988                         XPointer client_data,
989                         XPointer call_data)
990 {
991   GtkIMContext *context = GTK_IM_CONTEXT (client_data);
992   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
993   
994   if (!context_xim->finalizing)
995     g_signal_emit_by_name (context, "preedit_start");
996
997   return -1;                    /* No length limit */
998 }                    
999
1000 static void
1001 preedit_done_callback (XIC      xic,
1002                      XPointer client_data,
1003                      XPointer call_data)
1004 {
1005   GtkIMContext *context = GTK_IM_CONTEXT (client_data);
1006   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
1007
1008   if (context_xim->preedit_length)
1009     {
1010       context_xim->preedit_length = 0;
1011       if (!context_xim->finalizing)
1012         g_signal_emit_by_name (context_xim, "preedit_changed");
1013     }
1014
1015   if (!context_xim->finalizing)
1016     g_signal_emit_by_name (context, "preedit_end");  
1017 }                    
1018
1019 static gint
1020 xim_text_to_utf8 (GtkIMContextXIM *context, XIMText *xim_text, gchar **text)
1021 {
1022   gint text_length = 0;
1023   GError *error = NULL;
1024   gchar *result = NULL;
1025
1026   if (xim_text && xim_text->string.multi_byte)
1027     {
1028       if (xim_text->encoding_is_wchar)
1029         {
1030           g_warning ("Wide character return from Xlib not currently supported");
1031           *text = NULL;
1032           return 0;
1033         }
1034
1035       if (strcmp (context->mb_charset, "UTF-8") == 0)
1036         result = g_strdup (xim_text->string.multi_byte);
1037       else
1038         result = g_convert (xim_text->string.multi_byte,
1039                             -1,
1040                             "UTF-8",
1041                             context->mb_charset,
1042                             NULL, NULL, &error);
1043       
1044       if (result)
1045         {
1046           text_length = g_utf8_strlen (result, -1);
1047           
1048           if (text_length != xim_text->length)
1049             {
1050               g_warning ("Size mismatch when converting text from input method: supplied length = %d\n, result length = %d", xim_text->length, text_length);
1051             }
1052         }
1053       else
1054         {
1055           g_warning ("Error converting text from IM to UCS-4: %s", error->message);
1056           g_error_free (error);
1057
1058           *text = NULL;
1059           return 0;
1060         }
1061
1062       *text = result;
1063       return text_length;
1064     }
1065   else
1066     {
1067       *text = NULL;
1068       return 0;
1069     }
1070 }
1071
1072 static void
1073 preedit_draw_callback (XIC                           xic, 
1074                        XPointer                      client_data,
1075                        XIMPreeditDrawCallbackStruct *call_data)
1076 {
1077   GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1078
1079   XIMText *new_xim_text = call_data->text;
1080   gint new_text_length;
1081   gunichar *new_text = NULL;
1082   gint i;
1083   gint diff;
1084   gint new_length;
1085   gchar *tmp;
1086   
1087   gint chg_first = CLAMP (call_data->chg_first, 0, context->preedit_length);
1088   gint chg_length = CLAMP (call_data->chg_length, 0, context->preedit_length - chg_first);
1089
1090   context->preedit_cursor = call_data->caret;
1091   
1092   if (chg_first != call_data->chg_first || chg_length != call_data->chg_length)
1093     g_warning ("Invalid change to preedit string, first=%d length=%d (orig length == %d)",
1094                call_data->chg_first, call_data->chg_length, context->preedit_length);
1095
1096   new_text_length = xim_text_to_utf8 (context, new_xim_text, &tmp);
1097   if (tmp)
1098     {
1099       new_text = g_utf8_to_ucs4_fast (tmp, -1, NULL);
1100       g_free (tmp);
1101     }
1102   
1103   diff = new_text_length - chg_length;
1104   new_length = context->preedit_length + diff;
1105
1106   if (new_length > context->preedit_size)
1107     {
1108       context->preedit_size = new_length;
1109       context->preedit_chars = g_renew (gunichar, context->preedit_chars, new_length);
1110       context->feedbacks = g_renew (XIMFeedback, context->feedbacks, new_length);
1111     }
1112
1113   if (diff < 0)
1114     {
1115       for (i = chg_first + chg_length ; i < context->preedit_length; i++)
1116         {
1117           context->preedit_chars[i + diff] = context->preedit_chars[i];
1118           context->feedbacks[i + diff] = context->feedbacks[i];
1119         }
1120     }
1121   else
1122     {
1123       for (i = context->preedit_length - 1; i >= chg_first + chg_length ; i--)
1124         {
1125           context->preedit_chars[i + diff] = context->preedit_chars[i];
1126           context->feedbacks[i + diff] = context->feedbacks[i];
1127         }
1128     }
1129
1130   for (i = 0; i < new_text_length; i++)
1131     {
1132       context->preedit_chars[chg_first + i] = new_text[i];
1133       context->feedbacks[chg_first + i] = new_xim_text->feedback[i];
1134     }
1135
1136   context->preedit_length += diff;
1137
1138   if (new_text)
1139     g_free (new_text);
1140
1141   if (!context->finalizing)
1142     g_signal_emit_by_name (context, "preedit_changed");
1143 }
1144     
1145
1146 static void
1147 preedit_caret_callback (XIC                            xic,
1148                         XPointer                       client_data,
1149                         XIMPreeditCaretCallbackStruct *call_data)
1150 {
1151   GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1152   
1153   if (call_data->direction == XIMAbsolutePosition)
1154     {
1155       context->preedit_cursor = call_data->position;
1156       if (!context->finalizing)
1157         g_signal_emit_by_name (context, "preedit_changed");
1158     }
1159   else
1160     {
1161       g_warning ("Caret movement command: %d %d %d not supported",
1162                  call_data->position, call_data->direction, call_data->style);
1163     }
1164 }            
1165
1166 static void
1167 status_start_callback (XIC      xic,
1168                        XPointer client_data,
1169                        XPointer call_data)
1170 {
1171   return;
1172
1173
1174 static void
1175 status_done_callback (XIC      xic,
1176                       XPointer client_data,
1177                       XPointer call_data)
1178 {
1179   return;
1180 }
1181
1182 static void
1183 status_draw_callback (XIC      xic,
1184                       XPointer client_data,
1185                       XIMStatusDrawCallbackStruct *call_data)
1186 {
1187   GtkIMContextXIM *context = GTK_IM_CONTEXT_XIM (client_data);
1188
1189   if (call_data->type == XIMTextType)
1190     {
1191       gchar *text;
1192       xim_text_to_utf8 (context, call_data->data.text, &text);
1193
1194       if (context->status_window)
1195         status_window_set_text (context->status_window, text ? text : "");
1196     }
1197   else                          /* bitmap */
1198     {
1199       g_print ("Status drawn with bitmap - id = %#lx\n", call_data->data.bitmap);
1200     }
1201 }
1202
1203 static void
1204 string_conversion_callback (XIC xic, XPointer client_data, XPointer call_data)
1205 {
1206   GtkIMContextXIM *context_xim;
1207   XIMStringConversionCallbackStruct *conv_data;
1208   gchar *surrounding;
1209   gint  cursor_index;
1210
1211   context_xim = (GtkIMContextXIM *)client_data;
1212   conv_data = (XIMStringConversionCallbackStruct *)call_data;
1213
1214   if (gtk_im_context_get_surrounding ((GtkIMContext *)context_xim,
1215                                       &surrounding, &cursor_index))
1216     {
1217       gchar *text = NULL;
1218       gsize text_len = 0;
1219       gint  subst_offset = 0, subst_nchars = 0;
1220       gint  i;
1221       gchar *p = surrounding + cursor_index, *q;
1222       gshort position = (gshort)conv_data->position;
1223
1224       if (position > 0)
1225         {
1226           for (i = position; i > 0 && *p; --i)
1227             p = g_utf8_next_char (p);
1228           if (i > 0)
1229             return;
1230         }
1231       /* According to X11R6.4 Xlib - C Library Reference Manual
1232        * section 13.5.7.3 String Conversion Callback,
1233        * XIMStringConversionPosition is starting position _relative_
1234        * to current client's cursor position. So it should be able
1235        * to be negative, or referring to a position before the cursor
1236        * would be impossible. But current X protocol defines this as
1237        * unsigned short. So, compiler may warn about the value range
1238        * here. We hope the X protocol is fixed soon.
1239        */
1240       else if (position < 0)
1241         {
1242           for (i = position; i < 0 && p > surrounding; ++i)
1243             p = g_utf8_prev_char (p);
1244           if (i < 0)
1245             return;
1246         }
1247
1248       switch (conv_data->direction)
1249         {
1250         case XIMForwardChar:
1251           for (i = conv_data->factor, q = p; i > 0 && *q; --i)
1252             q = g_utf8_next_char (q);
1253           if (i > 0)
1254             break;
1255           text = g_locale_from_utf8 (p, q - p, NULL, &text_len, NULL);
1256           subst_offset = position;
1257           subst_nchars = conv_data->factor;
1258           break;
1259
1260         case XIMBackwardChar:
1261           for (i = conv_data->factor, q = p; i > 0 && q > surrounding; --i)
1262             q = g_utf8_prev_char (q);
1263           if (i > 0)
1264             break;
1265           text = g_locale_from_utf8 (q, p - q, NULL, &text_len, NULL);
1266           subst_offset = position - conv_data->factor;
1267           subst_nchars = conv_data->factor;
1268           break;
1269
1270         case XIMForwardWord:
1271         case XIMBackwardWord:
1272         case XIMCaretUp:
1273         case XIMCaretDown:
1274         case XIMNextLine:
1275         case XIMPreviousLine:
1276         case XIMLineStart:
1277         case XIMLineEnd:
1278         case XIMAbsolutePosition:
1279         case XIMDontChange:
1280         default:
1281           break;
1282         }
1283       /* block out any failure happenning to "text", including conversion */
1284       if (text)
1285         {
1286           conv_data->text = (XIMStringConversionText *)
1287                               malloc (sizeof (XIMStringConversionText));
1288           if (conv_data->text)
1289             {
1290               conv_data->text->length = text_len;
1291               conv_data->text->feedback = NULL;
1292               conv_data->text->encoding_is_wchar = False;
1293               conv_data->text->string.mbs = (char *)malloc (text_len);
1294               if (conv_data->text->string.mbs)
1295                 memcpy (conv_data->text->string.mbs, text, text_len);
1296               else
1297                 {
1298                   free (conv_data->text);
1299                   conv_data->text = NULL;
1300                 }
1301             }
1302
1303           g_free (text);
1304         }
1305       if (conv_data->operation == XIMStringConversionSubstitution
1306           && subst_nchars > 0)
1307         {
1308           gtk_im_context_delete_surrounding ((GtkIMContext *)context_xim,
1309                                             subst_offset, subst_nchars);
1310         }
1311
1312       g_free (surrounding);
1313     }
1314 }
1315
1316
1317 static XVaNestedList
1318 set_preedit_callback (GtkIMContextXIM *context_xim)
1319 {
1320   context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
1321   context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
1322   context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
1323   context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
1324   context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
1325   context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
1326   context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
1327   context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
1328   return XVaCreateNestedList (0,
1329                               XNPreeditStartCallback, &context_xim->preedit_start_callback,
1330                               XNPreeditDoneCallback, &context_xim->preedit_done_callback,
1331                               XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
1332                               XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
1333                               NULL);
1334 }
1335
1336 static XVaNestedList
1337 set_status_callback (GtkIMContextXIM *context_xim)
1338 {
1339   context_xim->status_start_callback.client_data = (XPointer)context_xim;
1340   context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
1341   context_xim->status_done_callback.client_data = (XPointer)context_xim;
1342   context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
1343   context_xim->status_draw_callback.client_data = (XPointer)context_xim;
1344   context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
1345           
1346   return XVaCreateNestedList (0,
1347                               XNStatusStartCallback, &context_xim->status_start_callback,
1348                               XNStatusDoneCallback, &context_xim->status_done_callback,
1349                               XNStatusDrawCallback, &context_xim->status_draw_callback,
1350                               NULL);
1351 }
1352
1353
1354 static void
1355 set_string_conversion_callback (GtkIMContextXIM *context_xim, XIC xic)
1356 {
1357   if (!context_xim->im_info->supports_string_conversion)
1358     return;
1359   
1360   context_xim->string_conversion_callback.client_data = (XPointer)context_xim;
1361   context_xim->string_conversion_callback.callback = (XICProc)string_conversion_callback;
1362   
1363   XSetICValues (xic,
1364                 XNStringConversionCallback,
1365                 (XPointer)&context_xim->string_conversion_callback,
1366                 NULL);
1367 }
1368
1369 static XIC
1370 gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
1371 {
1372   if (context_xim->im_info == NULL || context_xim->im_info->im == NULL)
1373     return NULL;
1374
1375   if (!context_xim->ic)
1376     {
1377       const char *name1 = NULL;
1378       XVaNestedList list1 = NULL;
1379       const char *name2 = NULL;
1380       XVaNestedList list2 = NULL;
1381       XIMStyle im_style = 0;
1382       XIC xic = 0;
1383
1384       if (context_xim->use_preedit &&
1385           (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
1386         {
1387           im_style |= XIMPreeditCallbacks;
1388           name1 = XNPreeditAttributes;
1389           list1 = set_preedit_callback (context_xim);
1390         }
1391       else if ((context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditNone)
1392         im_style |= XIMPreeditNone;
1393       else
1394         im_style |= XIMPreeditNothing;
1395
1396       if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
1397         {
1398           im_style |= XIMStatusCallbacks;
1399           if (name1 == NULL)
1400             {
1401               name1 = XNStatusAttributes;
1402               list1 = set_status_callback (context_xim);
1403             }
1404           else
1405             {
1406               name2 = XNStatusAttributes;
1407               list2 = set_status_callback (context_xim);
1408             }
1409         }
1410       else if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusNone)
1411         im_style |= XIMStatusNone;
1412       else
1413         im_style |= XIMStatusNothing;
1414
1415       xic = XCreateIC (context_xim->im_info->im,
1416                        XNInputStyle, im_style,
1417                        XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
1418                        name1, list1,
1419                        name2, list2,
1420                        NULL);
1421       if (list1)
1422         XFree (list1);
1423       if (list2)
1424         XFree (list2);
1425
1426       if (xic)
1427         {
1428           /* Don't filter key released events with XFilterEvents unless
1429            * input methods ask for. This is a workaround for Solaris input
1430            * method bug in C and European locales. It doubles each key
1431            * stroke if both key pressed and released events are filtered.
1432            * (bugzilla #81759)
1433            */
1434           gulong mask = 0xaaaaaaaa;
1435           XGetICValues (xic,
1436                         XNFilterEvents, &mask,
1437                         NULL);
1438           context_xim->filter_key_release = (mask & KeyReleaseMask) != 0;
1439           set_string_conversion_callback (context_xim, xic);
1440         }
1441       
1442       context_xim->ic = xic;
1443
1444       update_status_window (context_xim);
1445       
1446       if (xic && context_xim->has_focus)
1447         XSetICFocus (xic);
1448     }
1449   return context_xim->ic;
1450 }
1451
1452 /*****************************************************************
1453  * Status Window handling
1454  *
1455  * A status window is a small window attached to the toplevel
1456  * that is used to display information to the user about the
1457  * current input operation.
1458  *
1459  * We claim the toplevel's status window for an input context if:
1460  *
1461  * A) The input context has a toplevel
1462  * B) The input context has the focus
1463  * C) The input context has an XIC associated with it
1464  *
1465  * Tracking A) and C) is pretty reliable since we
1466  * compute A) and create the XIC for C) ourselves.
1467  * For B) we basically have to depend on our callers
1468  * calling ::focus-in and ::focus-out at the right time.
1469  *
1470  * The toplevel is computed by walking up the GdkWindow
1471  * hierarchy from context->client_window until we find a
1472  * window that is owned by some widget, and then calling
1473  * gtk_widget_get_toplevel() on that widget. This should
1474  * handle both cases where we might have GdkWindows without widgets,
1475  * and cases where GtkWidgets have strange window hierarchies
1476  * (like a torn off GtkHandleBox.)
1477  *
1478  * The status window is visible if and only if there is text
1479  * for it; whenever a new GtkIMContextXIM claims the status
1480  * window, we blank out any existing text. We actually only
1481  * create a GtkWindow for the status window the first time
1482  * it is shown; this is an important optimization when we are
1483  * using XIM with something like a simple compose-key input
1484  * method that never needs a status window.
1485  *****************************************************************/
1486
1487 /* Called when we no longer need a status window
1488 */
1489 static void
1490 disclaim_status_window (GtkIMContextXIM *context_xim)
1491 {
1492   if (context_xim->status_window)
1493     {
1494       g_assert (context_xim->status_window->context == context_xim);
1495
1496       status_window_set_text (context_xim->status_window, "");
1497       
1498       context_xim->status_window->context = NULL;
1499       context_xim->status_window = NULL;
1500     }
1501 }
1502
1503 /* Called when we need a status window
1504  */
1505 static void
1506 claim_status_window (GtkIMContextXIM *context_xim)
1507 {
1508   if (!context_xim->status_window && context_xim->client_widget)
1509     {
1510       GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
1511       if (toplevel && GTK_WIDGET_TOPLEVEL (toplevel))
1512         {
1513           StatusWindow *status_window = status_window_get (toplevel);
1514
1515           if (status_window->context)
1516             disclaim_status_window (status_window->context);
1517
1518           status_window->context = context_xim;
1519           context_xim->status_window = status_window;
1520         }
1521     }
1522 }
1523
1524 /* Basic call made whenever something changed that might cause
1525  * us to need, or not to need a status window.
1526  */
1527 static void
1528 update_status_window (GtkIMContextXIM *context_xim)
1529 {
1530   if (context_xim->ic && context_xim->in_toplevel && context_xim->has_focus)
1531     claim_status_window (context_xim);
1532   else
1533     disclaim_status_window (context_xim);
1534 }
1535
1536 /* Updates the in_toplevel flag for @context_xim
1537  */
1538 static void
1539 update_in_toplevel (GtkIMContextXIM *context_xim)
1540 {
1541   if (context_xim->client_widget)
1542     {
1543       GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
1544       
1545       context_xim->in_toplevel = (toplevel && GTK_WIDGET_TOPLEVEL (toplevel));
1546     }
1547   else
1548     context_xim->in_toplevel = FALSE;
1549
1550   /* Some paranoia, in case we don't get a focus out */
1551   if (!context_xim->in_toplevel)
1552     context_xim->has_focus = FALSE;
1553   
1554   update_status_window (context_xim);
1555 }
1556
1557 /* Callback when @widget's toplevel changes. It will always
1558  * change from NULL to a window, or a window to NULL;
1559  * we use that intermediate NULL state to make sure
1560  * that we disclaim the toplevel status window for the old
1561  * window.
1562  */
1563 static void
1564 on_client_widget_hierarchy_changed (GtkWidget       *widget,
1565                                     GtkWidget       *old_toplevel,
1566                                     GtkIMContextXIM *context_xim)
1567 {
1568   update_in_toplevel (context_xim);
1569 }
1570
1571 /* Finds the GtkWidget that owns the window, or if none, the
1572  * widget owning the nearest parent that has a widget.
1573  */
1574 static GtkWidget *
1575 widget_for_window (GdkWindow *window)
1576 {
1577   while (window)
1578     {
1579       gpointer user_data;
1580       gdk_window_get_user_data (window, &user_data);
1581       if (user_data)
1582         return user_data;
1583
1584       window = gdk_window_get_parent (window);
1585     }
1586
1587   return NULL;
1588 }
1589
1590 /* Called when context_xim->client_window changes; takes care of
1591  * removing and/or setting up our watches for the toplevel
1592  */
1593 static void
1594 update_client_widget (GtkIMContextXIM *context_xim)
1595 {
1596   GtkWidget *new_client_widget = widget_for_window (context_xim->client_window);
1597
1598   if (new_client_widget != context_xim->client_widget)
1599     {
1600       if (context_xim->client_widget)
1601         {
1602           g_signal_handlers_disconnect_by_func (context_xim->client_widget,
1603                                                 G_CALLBACK (on_client_widget_hierarchy_changed),
1604                                                 context_xim);
1605         }
1606       context_xim->client_widget = new_client_widget;
1607       if (context_xim->client_widget)
1608         {
1609           g_signal_connect (context_xim->client_widget, "hierarchy-changed",
1610                             G_CALLBACK (on_client_widget_hierarchy_changed),
1611                             context_xim);
1612         }
1613
1614       update_in_toplevel (context_xim);
1615     }
1616 }
1617
1618 /* Called when the toplevel is destroyed; frees the status window
1619  */
1620 static void
1621 on_status_toplevel_destroy (GtkWidget    *toplevel,
1622                             StatusWindow *status_window)
1623 {
1624   status_window_free (status_window);
1625 }
1626
1627 /* Called when the screen for the toplevel changes; updates the
1628  * screen for the status window to match.
1629  */
1630 static void
1631 on_status_toplevel_notify_screen (GtkWindow    *toplevel,
1632                                   GParamSpec   *pspec,
1633                                   StatusWindow *status_window)
1634 {
1635   if (status_window->window)
1636     gtk_window_set_screen (GTK_WINDOW (status_window->window),
1637                            gtk_widget_get_screen (GTK_WIDGET (toplevel)));
1638 }
1639
1640 /* Called when the toplevel window is moved; updates the position of
1641  * the status window to follow it.
1642  */
1643 static gboolean
1644 on_status_toplevel_configure (GtkWidget         *toplevel,
1645                               GdkEventConfigure *event,
1646                               StatusWindow      *status_window)
1647 {
1648   GdkRectangle rect;
1649   GtkRequisition requisition;
1650   gint y;
1651   gint height;
1652
1653   if (status_window->window)
1654     {
1655       height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
1656   
1657       gdk_window_get_frame_extents (toplevel->window, &rect);
1658       gtk_widget_size_request (status_window->window, &requisition);
1659       
1660       if (rect.y + rect.height + requisition.height < height)
1661         y = rect.y + rect.height;
1662       else
1663         y = height - requisition.height;
1664       
1665       gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
1666     }
1667
1668   return FALSE;
1669 }
1670
1671 /* Frees a status window and removes its link from the status_windows list
1672  */
1673 static void
1674 status_window_free (StatusWindow *status_window)
1675 {
1676   status_windows = g_slist_remove (status_windows, status_window);
1677
1678   if (status_window->context)
1679     status_window->context->status_window = NULL;
1680  
1681   g_signal_handlers_disconnect_by_func (status_window->toplevel,
1682                                         G_CALLBACK (on_status_toplevel_destroy),
1683                                         status_window);
1684   g_signal_handlers_disconnect_by_func (status_window->toplevel,
1685                                         G_CALLBACK (on_status_toplevel_notify_screen),
1686                                         status_window);
1687   g_signal_handlers_disconnect_by_func (status_window->toplevel,
1688                                         G_CALLBACK (on_status_toplevel_configure),
1689                                         status_window);
1690
1691   if (status_window->window)
1692     gtk_widget_destroy (status_window->window);
1693   
1694   g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
1695  
1696   g_free (status_window);
1697 }
1698
1699 /* Finds the status window object for a toplevel, creating it if necessary.
1700  */
1701 static StatusWindow *
1702 status_window_get (GtkWidget *toplevel)
1703 {
1704   StatusWindow *status_window;
1705
1706   status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
1707   if (status_window)
1708     return status_window;
1709   
1710   status_window = g_new0 (StatusWindow, 1);
1711   status_window->toplevel = toplevel;
1712
1713   status_windows = g_slist_prepend (status_windows, status_window);
1714
1715   g_signal_connect (toplevel, "destroy",
1716                     G_CALLBACK (on_status_toplevel_destroy),
1717                     status_window);
1718   g_signal_connect (toplevel, "configure_event",
1719                     G_CALLBACK (on_status_toplevel_configure),
1720                     status_window);
1721   g_signal_connect (toplevel, "notify::screen",
1722                     G_CALLBACK (on_status_toplevel_notify_screen),
1723                     status_window);
1724   
1725   g_object_set_data (G_OBJECT (toplevel), "gtk-im-xim-status-window", status_window);
1726
1727   return status_window;
1728 }
1729
1730 /* Draw the background (normally white) and border for the status window
1731  */
1732 static gboolean
1733 on_status_window_expose_event (GtkWidget      *widget,
1734                                GdkEventExpose *event)
1735 {
1736   gdk_draw_rectangle (widget->window,
1737                       widget->style->base_gc [GTK_STATE_NORMAL],
1738                       TRUE,
1739                       0, 0,
1740                       widget->allocation.width, widget->allocation.height);
1741   gdk_draw_rectangle (widget->window,
1742                       widget->style->text_gc [GTK_STATE_NORMAL],
1743                       FALSE,
1744                       0, 0,
1745                       widget->allocation.width - 1, widget->allocation.height - 1);
1746
1747   return FALSE;
1748 }
1749
1750 /* We watch the ::style-set signal for our label widget
1751  * and use that to change it's foreground color to match
1752  * the 'text' color of the toplevel window. The text/base
1753  * pair of colors might be reversed from the fg/bg pair
1754  * that are normally used for labels.
1755  */
1756 static void
1757 on_status_window_style_set (GtkWidget *toplevel,
1758                             GtkStyle  *previous_style,
1759                             GtkWidget *label)
1760 {
1761   gint i;
1762   
1763   for (i = 0; i < 5; i++)
1764     gtk_widget_modify_fg (label, i, &toplevel->style->text[i]);
1765 }
1766
1767 /* Creates the widgets for the status window; called when we
1768  * first need to show text for the status window.
1769  */
1770 static void
1771 status_window_make_window (StatusWindow *status_window)
1772 {
1773   GtkWidget *window;
1774   GtkWidget *status_label;
1775   
1776   status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
1777   window = status_window->window;
1778
1779   gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
1780   gtk_widget_set_app_paintable (window, TRUE);
1781
1782   status_label = gtk_label_new ("");
1783   gtk_misc_set_padding (GTK_MISC (status_label), 1, 1);
1784   gtk_widget_show (status_label);
1785   
1786   g_signal_connect (window, "style_set",
1787                     G_CALLBACK (on_status_window_style_set), status_label);
1788   gtk_container_add (GTK_CONTAINER (window), status_label);
1789   
1790   g_signal_connect (window, "expose_event",
1791                     G_CALLBACK (on_status_window_expose_event), NULL);
1792   
1793   gtk_window_set_screen (GTK_WINDOW (status_window->window),
1794                          gtk_widget_get_screen (status_window->toplevel));
1795
1796   on_status_toplevel_configure (status_window->toplevel, NULL, status_window);
1797 }
1798
1799 /* Updates the text in the status window, hiding or
1800  * showing the window as necessary.
1801  */
1802 static void
1803 status_window_set_text (StatusWindow *status_window,
1804                         const gchar  *text)
1805 {
1806   if (text[0])
1807     {
1808       GtkWidget *label;
1809       
1810       if (!status_window->window)
1811         status_window_make_window (status_window);
1812       
1813       label = GTK_BIN (status_window->window)->child;
1814       gtk_label_set_text (GTK_LABEL (label), text);
1815   
1816       gtk_widget_show (status_window->window);
1817     }
1818   else
1819     {
1820       if (status_window->window)
1821         gtk_widget_hide (status_window->window);
1822     }
1823 }
1824
1825 /**
1826  * gtk_im_context_xim_shutdown:
1827  * 
1828  * Destroys all the status windows that are kept by the XIM contexts.  This
1829  * function should only be called by the XIM module exit routine.
1830  **/
1831 void
1832 gtk_im_context_xim_shutdown (void)
1833 {
1834   while (status_windows)
1835     status_window_free (status_windows->data);
1836 }