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