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