]> Pileus Git - ~andy/gtk/blobdiff - modules/input/gtkimcontextxim.c
printing: Don't load custom paper sizes in file backend
[~andy/gtk] / modules / input / gtkimcontextxim.c
index f115174774b9dc0709c50079ae0ca55c233ec191..1a32e5abfd5671efb4bed7e9e354567381c964ec 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "config.h"
 #include "locale.h"
 #include <string.h>
+#include <stdlib.h>
 
-#include "gtk/gtklabel.h"
-#include "gtk/gtksignal.h"
-#include "gtk/gtkwindow.h"
 #include "gtkimcontextxim.h"
 
+#include "gtk/gtkintl.h"
+
 typedef struct _StatusWindow StatusWindow;
+typedef struct _GtkXIMInfo GtkXIMInfo;
+
+struct _GtkIMContextXIM
+{
+  GtkIMContext object;
+
+  GtkXIMInfo *im_info;
+
+  gchar *locale;
+  gchar *mb_charset;
+
+  GdkWindow *client_window;
+  GtkWidget *client_widget;
+
+  /* The status window for this input context; we claim the
+   * status window when we are focused and have created an XIC
+   */
+  StatusWindow *status_window;
+
+  gint preedit_size;
+  gint preedit_length;
+  gunichar *preedit_chars;
+  XIMFeedback *feedbacks;
+
+  gint preedit_cursor;
+  
+  XIMCallback preedit_start_callback;
+  XIMCallback preedit_done_callback;
+  XIMCallback preedit_draw_callback;
+  XIMCallback preedit_caret_callback;
+
+  XIMCallback status_start_callback;
+  XIMCallback status_done_callback;
+  XIMCallback status_draw_callback;
+
+  XIMCallback string_conversion_callback;
+
+  XIC ic;
+
+  guint filter_key_release : 1;
+  guint use_preedit : 1;
+  guint finalizing : 1;
+  guint in_toplevel : 1;
+  guint has_focus : 1;
+};
 
 struct _GtkXIMInfo
 {
-  GdkDisplay *display;
+  GdkScreen *screen;
   XIM im;
   char *locale;
+  XIMStyle preedit_style_setting;
+  XIMStyle status_style_setting;
   XIMStyle style;
+  GtkSettings *settings;
+  gulong status_set;
+  gulong preedit_set;
+  gulong display_closed_cb;
+  XIMStyles *xim_styles;
+  GSList *ics;
+
+  guint reconnecting :1;
+  guint supports_string_conversion;
 };
 
 /* A context status window; these are kept in the status_windows list. */
@@ -42,10 +97,9 @@ struct _StatusWindow
   
   /* Toplevel window to which the status window corresponds */
   GtkWidget *toplevel;
-  
-  /* Signal connection ids; we connect to the toplevel */
-  gulong destroy_handler_id;
-  gulong configure_handler_id;
+
+  /* Currently focused GtkIMContextXIM for the toplevel, if any */
+  GtkIMContextXIM *context;
 };
 
 static void     gtk_im_context_xim_class_init         (GtkIMContextXIMClass  *class);
@@ -67,17 +121,34 @@ static void     gtk_im_context_xim_get_preedit_string (GtkIMContext          *co
                                                       PangoAttrList        **attrs,
                                                       gint                  *cursor_pos);
 
-static void status_window_show     (GtkIMContextXIM *context_xim);
-static void status_window_hide     (GtkIMContextXIM *context_xim);
-static void status_window_set_text (GtkIMContextXIM *context_xim,
-                                   const gchar     *text);
+static void reinitialize_ic      (GtkIMContextXIM *context_xim);
+static void set_ic_client_window (GtkIMContextXIM *context_xim,
+                                 GdkWindow       *client_window);
+
+static void setup_styles (GtkXIMInfo *info);
+
+static void update_client_widget   (GtkIMContextXIM *context_xim);
+static void update_status_window   (GtkIMContextXIM *context_xim);
+
+static StatusWindow *status_window_get      (GtkWidget    *toplevel);
+static void          status_window_free     (StatusWindow *status_window);
+static void          status_window_set_text (StatusWindow *status_window,
+                                            const gchar  *text);
+
+static void xim_destroy_callback   (XIM      xim,
+                                   XPointer client_data,
+                                   XPointer call_data);
 
 static XIC       gtk_im_context_xim_get_ic            (GtkIMContextXIM *context_xim);
+static void           xim_info_display_closed (GdkDisplay *display,
+                                              gboolean    is_error,
+                                              GtkXIMInfo *info);
+
 static GObjectClass *parent_class;
 
 GType gtk_type_im_context_xim = 0;
 
-GSList *open_ims = NULL;
+static GSList *open_ims = NULL;
 
 /* List of status windows for different toplevels */
 static GSList *status_windows = NULL;
@@ -85,7 +156,7 @@ static GSList *status_windows = NULL;
 void
 gtk_im_context_xim_register_type (GTypeModule *type_module)
 {
-  static const GTypeInfo im_context_xim_info =
+  const GTypeInfo im_context_xim_info =
   {
     sizeof (GtkIMContextXIMClass),
     (GBaseInitFunc) NULL,
@@ -95,7 +166,7 @@ gtk_im_context_xim_register_type (GTypeModule *type_module)
     NULL,           /* class_data */
     sizeof (GtkIMContextXIM),
     0,
-    (GtkObjectInitFunc) gtk_im_context_xim_init,
+    (GInstanceInitFunc) gtk_im_context_xim_init,
   };
 
   gtk_type_im_context_xim = 
@@ -135,6 +206,8 @@ choose_better_style (XIMStyle style1, XIMStyle style2)
       return (s1 == XIMPreeditArea) ? style1 : style2;
     else if (u & XIMPreeditNothing)
       return (s1 == XIMPreeditNothing) ? style1 : style2;
+    else if (u & XIMPreeditNone)
+      return (s1 == XIMPreeditNone) ? style1 : style2;
   } else {
     s1 = style1 & STATUS_MASK;
     s2 = style2 & STATUS_MASK;
@@ -152,87 +225,298 @@ choose_better_style (XIMStyle style1, XIMStyle style2)
 }
 
 static void
-setup_im (GtkXIMInfo *info)
+reinitialize_all_ics (GtkXIMInfo *info)
 {
-  XIMStyles *xim_styles = NULL;
-  XIMValuesList *ic_values = NULL;
-  int i;
+  GSList *tmp_list;
+
+  for (tmp_list = info->ics; tmp_list; tmp_list = tmp_list->next)
+    reinitialize_ic (tmp_list->data);
+}
+
+static void
+status_style_change (GtkXIMInfo *info)
+{
+  GtkIMStatusStyle status_style;
   
-  XGetIMValues (info->im,
-               XNQueryInputStyle, &xim_styles,
-               XNQueryICValuesList, &ic_values,
+  g_object_get (info->settings,
+               "gtk-im-status-style", &status_style,
+               NULL);
+  if (status_style == GTK_IM_STATUS_CALLBACK)
+    info->status_style_setting = XIMStatusCallbacks;
+  else if (status_style == GTK_IM_STATUS_NOTHING)
+    info->status_style_setting = XIMStatusNothing;
+  else if (status_style == GTK_IM_STATUS_NONE)
+    info->status_style_setting = XIMStatusNone;
+  else
+    return;
+
+  setup_styles (info);
+  
+  reinitialize_all_ics (info);
+}
+
+static void
+preedit_style_change (GtkXIMInfo *info)
+{
+  GtkIMPreeditStyle preedit_style;
+  g_object_get (info->settings,
+               "gtk-im-preedit-style", &preedit_style,
                NULL);
+  if (preedit_style == GTK_IM_PREEDIT_CALLBACK)
+    info->preedit_style_setting = XIMPreeditCallbacks;
+  else if (preedit_style == GTK_IM_PREEDIT_NOTHING)
+    info->preedit_style_setting = XIMPreeditNothing;
+  else if (preedit_style == GTK_IM_PREEDIT_NONE)
+    info->preedit_style_setting = XIMPreeditNone;
+  else
+    return;
+
+  setup_styles (info);
+  
+  reinitialize_all_ics (info);
+}
+
+static void
+setup_styles (GtkXIMInfo *info)
+{
+  int i;
+  unsigned long settings_preference;
+  XIMStyles *xim_styles = info->xim_styles;
 
+  settings_preference = info->status_style_setting|info->preedit_style_setting;
   info->style = 0;
   if (xim_styles)
     {
       for (i = 0; i < xim_styles->count_styles; i++)
        if ((xim_styles->supported_styles[i] & ALLOWED_MASK) == xim_styles->supported_styles[i])
-         info->style = choose_better_style (info->style,
-                                            xim_styles->supported_styles[i]);
+         {
+           if (settings_preference == xim_styles->supported_styles[i])
+             {
+               info->style = settings_preference;
+               break;
+             }
+           info->style = choose_better_style (info->style,
+                                              xim_styles->supported_styles[i]);
+         }
     }
+  if (info->style == 0)
+    info->style = XIMPreeditNothing | XIMStatusNothing;
+}
 
+static void
+setup_im (GtkXIMInfo *info)
+{
+  XIMValuesList *ic_values = NULL;
+  XIMCallback im_destroy_callback;
+  GdkDisplay *display;
 
-#if 0
+  if (info->im == NULL)
+    return;
+
+  im_destroy_callback.client_data = (XPointer)info;
+  im_destroy_callback.callback = (XIMProc)xim_destroy_callback;
+  XSetIMValues (info->im,
+               XNDestroyCallback, &im_destroy_callback,
+               NULL);
+
+  XGetIMValues (info->im,
+               XNQueryInputStyle, &info->xim_styles,
+               XNQueryICValuesList, &ic_values,
+               NULL);
+
+  info->settings = gtk_settings_get_for_screen (info->screen);
+  info->status_set = g_signal_connect_swapped (info->settings,
+                                              "notify::gtk-im-status-style",
+                                              G_CALLBACK (status_style_change),
+                                              info);
+  info->preedit_set = g_signal_connect_swapped (info->settings,
+                                               "notify::gtk-im-preedit-style",
+                                               G_CALLBACK (preedit_style_change),
+                                               info);
+
+  info->supports_string_conversion = FALSE;
   if (ic_values)
     {
+      int i;
+      
+      for (i = 0; i < ic_values->count_values; i++)
+       if (strcmp (ic_values->supported_values[i],
+                   XNStringConversionCallback) == 0)
+         {
+           info->supports_string_conversion = TRUE;
+           break;
+         }
+
+#if 0
       for (i = 0; i < ic_values->count_values; i++)
        g_print ("%s\n", ic_values->supported_values[i]);
       for (i = 0; i < xim_styles->count_styles; i++)
        g_print ("%#x\n", xim_styles->supported_styles[i]);
-    }
 #endif
+      
+      XFree (ic_values);
+    }
 
-  if (xim_styles)
-    XFree (xim_styles);
-  if (ic_values)
-    XFree (ic_values);
+  status_style_change (info);
+  preedit_style_change (info);
+
+  display = gdk_screen_get_display (info->screen);
+  info->display_closed_cb = g_signal_connect (display, "closed",
+                                             G_CALLBACK (xim_info_display_closed), info);
 }
 
-static GtkXIMInfo *
-get_im (GdkDisplay *display,
-       const char *locale)
+static void
+xim_info_display_closed (GdkDisplay *display,
+                        gboolean    is_error,
+                        GtkXIMInfo *info)
 {
-  GSList *tmp_list;
-  GtkXIMInfo *info;
+  GSList *ics, *tmp_list;
+
+  open_ims = g_slist_remove (open_ims, info);
+
+  ics = info->ics;
+  info->ics = NULL;
+
+  for (tmp_list = ics; tmp_list; tmp_list = tmp_list->next)
+    set_ic_client_window (tmp_list->data, NULL);
+
+  g_slist_free (ics);
+
+  if (info->status_set)
+    g_signal_handler_disconnect (info->settings, info->status_set);
+  if (info->preedit_set)
+    g_signal_handler_disconnect (info->settings, info->preedit_set);
+  if (info->display_closed_cb)
+    g_signal_handler_disconnect (display, info->display_closed_cb);
+
+  if (info->xim_styles)
+    XFree (info->xim_styles);
+  g_free (info->locale);
+
+  if (info->im)
+    XCloseIM (info->im);
+
+  g_free (info);
+}
+
+static void
+xim_instantiate_callback (Display *display, XPointer client_data,
+                         XPointer call_data)
+{
+  GtkXIMInfo *info = (GtkXIMInfo*)client_data;
   XIM im = NULL;
 
-  tmp_list = open_ims;
-  while (tmp_list)
-    {
-      info = tmp_list->data;
-      if (info->display == display &&
-         strcmp (info->locale, locale) == 0)
-       return info;
+  im = XOpenIM (display, NULL, NULL, NULL);
 
-      tmp_list = tmp_list->next;
-    }
+  if (!im)
+    return;
 
-  info = NULL;
+  info->im = im;
+  setup_im (info);
+
+  XUnregisterIMInstantiateCallback (display, NULL, NULL, NULL,
+                                   xim_instantiate_callback,
+                                   (XPointer)info);
+  info->reconnecting = FALSE;
+}
+
+/* initialize info->im */
+static void
+xim_info_try_im (GtkXIMInfo *info)
+{
+  GdkScreen *screen = info->screen;
+  GdkDisplay *display = gdk_screen_get_display (screen);
+
+  g_assert (info->im == NULL);
+  if (info->reconnecting)
+    return;
 
   if (XSupportsLocale ())
     {
       if (!XSetLocaleModifiers (""))
        g_warning ("Unable to set locale modifiers with XSetLocaleModifiers()");
-      
-      im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
-      
-      if (!im)
-       g_warning ("Unable to open XIM input method, falling back to XLookupString()");
-
-      if (im)
+      info->im = XOpenIM (GDK_DISPLAY_XDISPLAY (display), NULL, NULL, NULL);
+      if (!info->im)
        {
-         info = g_new (GtkXIMInfo, 1);
-         open_ims = g_slist_prepend (open_ims, im);
+         XRegisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY(display),
+                                         NULL, NULL, NULL,
+                                         xim_instantiate_callback,
+                                         (XPointer)info);
+         info->reconnecting = TRUE;
+         return;
+       }
+      setup_im (info);
+    }
+}
 
-         info->display = display;
-         info->locale = g_strdup (locale);
-         info->im = im;
+static void
+xim_destroy_callback (XIM      xim,
+                     XPointer client_data,
+                     XPointer call_data)
+{
+  GtkXIMInfo *info = (GtkXIMInfo*)client_data;
+
+  info->im = NULL;
+
+  g_signal_handler_disconnect (info->settings, info->status_set);
+  info->status_set = 0;
+  g_signal_handler_disconnect (info->settings, info->preedit_set);
+  info->preedit_set = 0;
+
+  reinitialize_all_ics (info);
+  xim_info_try_im (info);
+  return;
+} 
+
+static GtkXIMInfo *
+get_im (GdkWindow *client_window,
+       const char *locale)
+{
+  GSList *tmp_list;
+  GtkXIMInfo *info;
+  GdkScreen *screen = gdk_window_get_screen (client_window);
 
-         setup_im (info);
+  info = NULL;
+  tmp_list = open_ims;
+  while (tmp_list)
+    {
+      GtkXIMInfo *tmp_info = tmp_list->data;
+      if (tmp_info->screen == screen &&
+         strcmp (tmp_info->locale, locale) == 0)
+       {
+         if (tmp_info->im)
+           {
+             return tmp_info;
+           }
+         else
+           {
+             tmp_info = tmp_info;
+             break;
+           }
        }
+      tmp_list = tmp_list->next;
+    }
+
+  if (info == NULL)
+    {
+      info = g_new (GtkXIMInfo, 1);
+      open_ims = g_slist_prepend (open_ims, info);
+
+      info->screen = screen;
+      info->locale = g_strdup (locale);
+      info->xim_styles = NULL;
+      info->preedit_style_setting = 0;
+      info->status_style_setting = 0;
+      info->settings = NULL;
+      info->preedit_set = 0;
+      info->status_set = 0;
+      info->display_closed_cb = 0;
+      info->ics = NULL;
+      info->reconnecting = FALSE;
+      info->im = NULL;
     }
 
+  xim_info_try_im (info);
   return info;
 }
 
@@ -260,6 +544,9 @@ gtk_im_context_xim_init (GtkIMContextXIM *im_context_xim)
 {
   im_context_xim->use_preedit = TRUE;
   im_context_xim->filter_key_release = FALSE;
+  im_context_xim->finalizing = FALSE;
+  im_context_xim->has_focus = FALSE;
+  im_context_xim->in_toplevel = FALSE;
 }
 
 static void
@@ -267,14 +554,38 @@ gtk_im_context_xim_finalize (GObject *obj)
 {
   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (obj);
 
-  if (context_xim->ic)
+  context_xim->finalizing = TRUE;
+
+  if (context_xim->im_info && !context_xim->im_info->ics->next) 
     {
-      XDestroyIC (context_xim->ic);
-      context_xim->ic = NULL;
+      if (context_xim->im_info->reconnecting)
+       {
+         GdkDisplay *display;
+
+         display = gdk_screen_get_display (context_xim->im_info->screen);
+         XUnregisterIMInstantiateCallback (GDK_DISPLAY_XDISPLAY (display),
+                                           NULL, NULL, NULL,
+                                           xim_instantiate_callback,
+                                           (XPointer)context_xim->im_info);
+       }
+      else if (context_xim->im_info->im)
+       {
+         XIMCallback im_destroy_callback;
+
+         im_destroy_callback.client_data = NULL;
+         im_destroy_callback.callback = NULL;
+         XSetIMValues (context_xim->im_info->im,
+                       XNDestroyCallback, &im_destroy_callback,
+                       NULL);
+       }
     }
+
+  set_ic_client_window (context_xim, NULL);
+
   g_free (context_xim->locale);
   g_free (context_xim->mb_charset);
+
+  G_OBJECT_CLASS (parent_class)->finalize (obj);
 }
 
 static void
@@ -284,28 +595,51 @@ reinitialize_ic (GtkIMContextXIM *context_xim)
     {
       XDestroyIC (context_xim->ic);
       context_xim->ic = NULL;
+      update_status_window (context_xim);
 
       if (context_xim->preedit_length)
        {
          context_xim->preedit_length = 0;
-         g_signal_emit_by_name (context_xim, "preedit_changed");
+         if (!context_xim->finalizing)
+           g_signal_emit_by_name (context_xim, "preedit-changed");
        }
     }
+  /* 
+     reset filter_key_release flag, otherwise keystrokes will be doubled
+     until reconnecting to XIM.
+  */
+  context_xim->filter_key_release = FALSE;
 }
 
 static void
-gtk_im_context_xim_set_client_window (GtkIMContext          *context,
-                                     GdkWindow             *client_window)
+set_ic_client_window (GtkIMContextXIM *context_xim,
+                     GdkWindow       *client_window)
 {
-  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
-
   reinitialize_ic (context_xim);
+  if (context_xim->client_window)
+    {
+      context_xim->im_info->ics = g_slist_remove (context_xim->im_info->ics, context_xim);
+      context_xim->im_info = NULL;
+    }
+  
   context_xim->client_window = client_window;
 
   if (context_xim->client_window)
-    context_xim->im_info = get_im (gdk_drawable_get_display (context_xim->client_window), context_xim->locale);
-  else
-    context_xim->im_info = NULL;
+    {
+      context_xim->im_info = get_im (context_xim->client_window, context_xim->locale);
+      context_xim->im_info->ics = g_slist_prepend (context_xim->im_info->ics, context_xim);
+    }
+  
+  update_client_widget (context_xim);
+}
+
+static void
+gtk_im_context_xim_set_client_window (GtkIMContext          *context,
+                                     GdkWindow             *client_window)
+{
+  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
+
+  set_ic_client_window (context_xim, client_window);
 }
 
 GtkIMContext *
@@ -314,7 +648,9 @@ gtk_im_context_xim_new (void)
   GtkIMContextXIM *result;
   const gchar *charset;
 
-  result = GTK_IM_CONTEXT_XIM (g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL));
+  if (!GDK_IS_X11_DISPLAY(gdk_display_get_default()))
+    return NULL;
+  result = g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL);
 
   result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
   
@@ -361,7 +697,7 @@ gtk_im_context_xim_filter_keypress (GtkIMContext *context,
   KeySym keysym;
   Status status;
   gboolean result = FALSE;
-  GdkWindow *root_window = gdk_screen_get_root_window (gdk_drawable_get_screen (event->window));
+  GdkWindow *root_window = gdk_screen_get_root_window (gdk_window_get_screen (event->window));
 
   XKeyPressedEvent xevent;
 
@@ -371,9 +707,9 @@ gtk_im_context_xim_filter_keypress (GtkIMContext *context,
   xevent.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
   xevent.serial = 0;           /* hope it doesn't matter */
   xevent.send_event = event->send_event;
-  xevent.display = GDK_DRAWABLE_XDISPLAY (event->window);
-  xevent.window = GDK_DRAWABLE_XID (event->window);
-  xevent.root = GDK_DRAWABLE_XID (root_window);
+  xevent.display = GDK_WINDOW_XDISPLAY (event->window);
+  xevent.window = GDK_WINDOW_XID (event->window);
+  xevent.root = GDK_WINDOW_XID (root_window);
   xevent.subwindow = xevent.window;
   xevent.time = event->time;
   xevent.x = xevent.x_root = 0;
@@ -382,9 +718,13 @@ gtk_im_context_xim_filter_keypress (GtkIMContext *context,
   xevent.keycode = event->hardware_keycode;
   xevent.same_screen = True;
   
-  if (XFilterEvent ((XEvent *)&xevent, GDK_DRAWABLE_XID (context_xim->client_window)))
+  if (XFilterEvent ((XEvent *)&xevent, GDK_WINDOW_XID (context_xim->client_window)))
     return TRUE;
   
+  if (event->state &
+      (gtk_accelerator_get_default_mod_mask () & ~(GDK_SHIFT_MASK | GDK_CONTROL_MASK))) 
+    return FALSE;
+
  again:
   if (ic)
     num_bytes = XmbLookupString (ic, &xevent, buffer, buffer_size, &keysym, &status);
@@ -397,6 +737,8 @@ gtk_im_context_xim_filter_keypress (GtkIMContext *context,
   if (status == XBufferOverflow)
     {
       buffer_size = num_bytes;
+      if (buffer != static_buffer) 
+       g_free (buffer);
       buffer = g_malloc (num_bytes + 1);
       goto again;
     }
@@ -427,6 +769,9 @@ gtk_im_context_xim_filter_keypress (GtkIMContext *context,
        }
     }
 
+  if (buffer != static_buffer) 
+    g_free (buffer);
+
   return result;
 }
 
@@ -434,14 +779,17 @@ static void
 gtk_im_context_xim_focus_in (GtkIMContext *context)
 {
   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
-  XIC ic = gtk_im_context_xim_get_ic (context_xim);
 
-  if (!ic)
-    return;
-
-  XSetICFocus (ic);
+  if (!context_xim->has_focus)
+    {
+      XIC ic = gtk_im_context_xim_get_ic (context_xim);
 
-  status_window_show (context_xim);
+      context_xim->has_focus = TRUE;
+      update_status_window (context_xim);
+      
+      if (ic)
+       XSetICFocus (ic);
+    }
 
   return;
 }
@@ -450,14 +798,17 @@ static void
 gtk_im_context_xim_focus_out (GtkIMContext *context)
 {
   GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
-  XIC ic = gtk_im_context_xim_get_ic (context_xim);
-
-  if (!ic)
-    return;
 
-  XUnsetICFocus (ic);
-
-  status_window_hide (context_xim);
+  if (context_xim->has_focus)
+    {
+      XIC ic = gtk_im_context_xim_get_ic (context_xim);
+      
+      context_xim->has_focus = FALSE;
+      update_status_window (context_xim);
+  
+      if (ic)
+       XUnsetICFocus (ic);
+    }
 
   return;
 }
@@ -476,11 +827,11 @@ gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
     return;
 
   spot.x = area->x;
-  spot.y = area->y;
+  spot.y = area->y + area->height;
 
   preedit_attr = XVaCreateNestedList (0,
                                      XNSpotLocation, &spot,
-                                     0);
+                                     NULL);
   XSetICValues (ic,
                XNPreeditAttributes, preedit_attr,
                NULL);
@@ -527,7 +878,7 @@ gtk_im_context_xim_reset (GtkIMContext *context)
 
   preedit_attr = XVaCreateNestedList(0,
                                      XNPreeditState, &preedit_state,
-                                     0);
+                                     NULL);
   if (!XGetICValues(ic,
                     XNPreeditAttributes, preedit_attr,
                     NULL))
@@ -539,7 +890,7 @@ gtk_im_context_xim_reset (GtkIMContext *context)
 
   preedit_attr = XVaCreateNestedList(0,
                                      XNPreeditState, preedit_state,
-                                     0);
+                                     NULL);
   if (have_preedit_state)
     XSetICValues(ic,
                 XNPreeditAttributes, preedit_attr,
@@ -560,7 +911,7 @@ gtk_im_context_xim_reset (GtkIMContext *context)
   if (context_xim->preedit_length)
     {
       context_xim->preedit_length = 0;
-      g_signal_emit_by_name (context, "preedit_changed");
+      g_signal_emit_by_name (context, "preedit-changed");
     }
 
   XFree (result);
@@ -653,14 +1004,18 @@ gtk_im_context_xim_get_preedit_string (GtkIMContext   *context,
     *cursor_pos = context_xim->preedit_cursor;
 }
 
-static void
+static int
 preedit_start_callback (XIC      xic,
                        XPointer client_data,
                        XPointer call_data)
 {
   GtkIMContext *context = GTK_IM_CONTEXT (client_data);
+  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
   
-  g_signal_emit_by_name (context, "preedit_start");
+  if (!context_xim->finalizing)
+    g_signal_emit_by_name (context, "preedit-start");
+
+  return -1;                   /* No length limit */
 }                   
 
 static void
@@ -669,8 +1024,17 @@ preedit_done_callback (XIC      xic,
                     XPointer call_data)
 {
   GtkIMContext *context = GTK_IM_CONTEXT (client_data);
-  
-  g_signal_emit_by_name (context, "preedit_end");  
+  GtkIMContextXIM *context_xim = GTK_IM_CONTEXT_XIM (context);
+
+  if (context_xim->preedit_length)
+    {
+      context_xim->preedit_length = 0;
+      if (!context_xim->finalizing)
+       g_signal_emit_by_name (context_xim, "preedit-changed");
+    }
+
+  if (!context_xim->finalizing)
+    g_signal_emit_by_name (context, "preedit-end");
 }                   
 
 static gint
@@ -792,10 +1156,10 @@ preedit_draw_callback (XIC                           xic,
 
   context->preedit_length += diff;
 
-  if (new_text)
-    g_free (new_text);
+  g_free (new_text);
 
-  g_signal_emit_by_name (context, "preedit_changed");
+  if (!context->finalizing)
+    g_signal_emit_by_name (context, "preedit-changed");
 }
     
 
@@ -809,7 +1173,8 @@ preedit_caret_callback (XIC                            xic,
   if (call_data->direction == XIMAbsolutePosition)
     {
       context->preedit_cursor = call_data->position;
-      g_signal_emit_by_name (context, "preedit_changed");
+      if (!context->finalizing)
+       g_signal_emit_by_name (context, "preedit-changed");
     }
   else
     {
@@ -846,10 +1211,8 @@ status_draw_callback (XIC      xic,
       gchar *text;
       xim_text_to_utf8 (context, call_data->data.text, &text);
 
-      if (text)
-       status_window_set_text (context, text);
-      else
-       status_window_set_text (context, "");
+      if (context->status_window)
+       status_window_set_text (context->status_window, text ? text : "");
     }
   else                         /* bitmap */
     {
@@ -857,96 +1220,230 @@ status_draw_callback (XIC      xic,
     }
 }
 
-static XIC
-get_ic_with_preedit (GtkIMContextXIM *context_xim)
+static void
+string_conversion_callback (XIC xic, XPointer client_data, XPointer call_data)
 {
-  XIC xic = 0;
-  const char *name1 = NULL;
-  XVaNestedList list1 = NULL;
-  const char *name2 = NULL;
-  XVaNestedList list2 = NULL;
+  GtkIMContextXIM *context_xim;
+  XIMStringConversionCallbackStruct *conv_data;
+  gchar *surrounding;
+  gint  cursor_index;
 
-  if ((context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
-    {
-      context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
-      context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
-      context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
-      context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
-      context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
-      context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
-      context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
-      context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
-      name1 = XNPreeditAttributes;
-      list1 = XVaCreateNestedList (0,
-                                  XNPreeditStartCallback, &context_xim->preedit_start_callback,
-                                  XNPreeditDoneCallback, &context_xim->preedit_done_callback,
-                                  XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
-                                  XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
-                                  NULL);
-    }
+  context_xim = (GtkIMContextXIM *)client_data;
+  conv_data = (XIMStringConversionCallbackStruct *)call_data;
 
-  if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
+  if (gtk_im_context_get_surrounding ((GtkIMContext *)context_xim,
+                                      &surrounding, &cursor_index))
     {
-      XVaNestedList status_attrs;
-
-      context_xim->status_start_callback.client_data = (XPointer)context_xim;
-      context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
-      context_xim->status_done_callback.client_data = (XPointer)context_xim;
-      context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
-      context_xim->status_draw_callback.client_data = (XPointer)context_xim;
-      context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
-         
-      status_attrs = XVaCreateNestedList (0,
-                                         XNStatusStartCallback, &context_xim->status_start_callback,
-                                         XNStatusDoneCallback, &context_xim->status_done_callback,
-                                         XNStatusDrawCallback, &context_xim->status_draw_callback,
-                                         NULL);
-      if (name1 == NULL)
-       {
-         name1 = XNStatusAttributes;
-         list1 = status_attrs;
-       }
-      else
-       {
-         name2 = XNStatusAttributes;
-         list2 = status_attrs;
-       }
+      gchar *text = NULL;
+      gsize text_len = 0;
+      gint  subst_offset = 0, subst_nchars = 0;
+      gint  i;
+      gchar *p = surrounding + cursor_index, *q;
+      gshort position = (gshort)conv_data->position;
+
+      if (position > 0)
+        {
+          for (i = position; i > 0 && *p; --i)
+            p = g_utf8_next_char (p);
+          if (i > 0)
+            return;
+        }
+      /* According to X11R6.4 Xlib - C Library Reference Manual
+       * section 13.5.7.3 String Conversion Callback,
+       * XIMStringConversionPosition is starting position _relative_
+       * to current client's cursor position. So it should be able
+       * to be negative, or referring to a position before the cursor
+       * would be impossible. But current X protocol defines this as
+       * unsigned short. So, compiler may warn about the value range
+       * here. We hope the X protocol is fixed soon.
+       */
+      else if (position < 0)
+        {
+          for (i = position; i < 0 && p > surrounding; ++i)
+            p = g_utf8_prev_char (p);
+          if (i < 0)
+            return;
+        }
+
+      switch (conv_data->direction)
+        {
+        case XIMForwardChar:
+          for (i = conv_data->factor, q = p; i > 0 && *q; --i)
+            q = g_utf8_next_char (q);
+          if (i > 0)
+            break;
+          text = g_locale_from_utf8 (p, q - p, NULL, &text_len, NULL);
+          subst_offset = position;
+          subst_nchars = conv_data->factor;
+          break;
+
+        case XIMBackwardChar:
+          for (i = conv_data->factor, q = p; i > 0 && q > surrounding; --i)
+            q = g_utf8_prev_char (q);
+          if (i > 0)
+            break;
+          text = g_locale_from_utf8 (q, p - q, NULL, &text_len, NULL);
+          subst_offset = position - conv_data->factor;
+          subst_nchars = conv_data->factor;
+          break;
+
+        case XIMForwardWord:
+        case XIMBackwardWord:
+        case XIMCaretUp:
+        case XIMCaretDown:
+        case XIMNextLine:
+        case XIMPreviousLine:
+        case XIMLineStart:
+        case XIMLineEnd:
+        case XIMAbsolutePosition:
+        case XIMDontChange:
+        default:
+          break;
+        }
+      /* block out any failure happenning to "text", including conversion */
+      if (text)
+        {
+          conv_data->text = (XIMStringConversionText *)
+                              malloc (sizeof (XIMStringConversionText));
+          if (conv_data->text)
+            {
+              conv_data->text->length = text_len;
+              conv_data->text->feedback = NULL;
+              conv_data->text->encoding_is_wchar = False;
+              conv_data->text->string.mbs = (char *)malloc (text_len);
+              if (conv_data->text->string.mbs)
+                memcpy (conv_data->text->string.mbs, text, text_len);
+              else
+                {
+                  free (conv_data->text);
+                  conv_data->text = NULL;
+                }
+            }
+
+          g_free (text);
+        }
+      if (conv_data->operation == XIMStringConversionSubstitution
+          && subst_nchars > 0)
+        {
+          gtk_im_context_delete_surrounding ((GtkIMContext *)context_xim,
+                                            subst_offset, subst_nchars);
+        }
+
+      g_free (surrounding);
     }
+}
 
-  xic = XCreateIC (context_xim->im_info->im,
-                  XNInputStyle, context_xim->im_info->style,
-                  XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
-                  name1, list1,
-                  name2, list2,
-                  NULL);
-  if (list1)
-    XFree (list1);
-  if (list2)
-    XFree (list2);
-  
-  return xic;
+
+static XVaNestedList
+set_preedit_callback (GtkIMContextXIM *context_xim)
+{
+  context_xim->preedit_start_callback.client_data = (XPointer)context_xim;
+  context_xim->preedit_start_callback.callback = (XIMProc)preedit_start_callback;
+  context_xim->preedit_done_callback.client_data = (XPointer)context_xim;
+  context_xim->preedit_done_callback.callback = (XIMProc)preedit_done_callback;
+  context_xim->preedit_draw_callback.client_data = (XPointer)context_xim;
+  context_xim->preedit_draw_callback.callback = (XIMProc)preedit_draw_callback;
+  context_xim->preedit_caret_callback.client_data = (XPointer)context_xim;
+  context_xim->preedit_caret_callback.callback = (XIMProc)preedit_caret_callback;
+  return XVaCreateNestedList (0,
+                             XNPreeditStartCallback, &context_xim->preedit_start_callback,
+                             XNPreeditDoneCallback, &context_xim->preedit_done_callback,
+                             XNPreeditDrawCallback, &context_xim->preedit_draw_callback,
+                             XNPreeditCaretCallback, &context_xim->preedit_caret_callback,
+                             NULL);
 }
 
-static XIC
-get_ic_without_preedit (GtkIMContextXIM *context_xim)
+static XVaNestedList
+set_status_callback (GtkIMContextXIM *context_xim)
+{
+  context_xim->status_start_callback.client_data = (XPointer)context_xim;
+  context_xim->status_start_callback.callback = (XIMProc)status_start_callback;
+  context_xim->status_done_callback.client_data = (XPointer)context_xim;
+  context_xim->status_done_callback.callback = (XIMProc)status_done_callback;
+  context_xim->status_draw_callback.client_data = (XPointer)context_xim;
+  context_xim->status_draw_callback.callback = (XIMProc)status_draw_callback;
+         
+  return XVaCreateNestedList (0,
+                             XNStatusStartCallback, &context_xim->status_start_callback,
+                             XNStatusDoneCallback, &context_xim->status_done_callback,
+                             XNStatusDrawCallback, &context_xim->status_draw_callback,
+                             NULL);
+}
+
+
+static void
+set_string_conversion_callback (GtkIMContextXIM *context_xim, XIC xic)
 {
-  return XCreateIC (context_xim->im_info->im,
-                   XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
-                   XNClientWindow, GDK_DRAWABLE_XID (context_xim->client_window),
-                   NULL);
+  if (!context_xim->im_info->supports_string_conversion)
+    return;
+  
+  context_xim->string_conversion_callback.client_data = (XPointer)context_xim;
+  context_xim->string_conversion_callback.callback = (XIMProc)string_conversion_callback;
+  
+  XSetICValues (xic,
+               XNStringConversionCallback,
+               (XPointer)&context_xim->string_conversion_callback,
+               NULL);
 }
 
 static XIC
 gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
 {
-  if (!context_xim->ic && context_xim->im_info)
+  if (context_xim->im_info == NULL || context_xim->im_info->im == NULL)
+    return NULL;
+
+  if (!context_xim->ic)
     {
-      if (!context_xim->use_preedit)
-       context_xim->ic = get_ic_without_preedit (context_xim);
+      const char *name1 = NULL;
+      XVaNestedList list1 = NULL;
+      const char *name2 = NULL;
+      XVaNestedList list2 = NULL;
+      XIMStyle im_style = 0;
+      XIC xic = NULL;
+
+      if (context_xim->use_preedit &&
+         (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
+       {
+         im_style |= XIMPreeditCallbacks;
+         name1 = XNPreeditAttributes;
+         list1 = set_preedit_callback (context_xim);
+       }
+      else if ((context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditNone)
+       im_style |= XIMPreeditNone;
       else
-       context_xim->ic = get_ic_with_preedit (context_xim);
+       im_style |= XIMPreeditNothing;
 
-      if (context_xim->ic)
+      if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusCallbacks)
+       {
+         im_style |= XIMStatusCallbacks;
+         if (name1 == NULL)
+           {
+             name1 = XNStatusAttributes;
+             list1 = set_status_callback (context_xim);
+           }
+         else
+           {
+             name2 = XNStatusAttributes;
+             list2 = set_status_callback (context_xim);
+           }
+       }
+      else if ((context_xim->im_info->style & STATUS_MASK) == XIMStatusNone)
+       im_style |= XIMStatusNone;
+      else
+       im_style |= XIMStatusNothing;
+
+      xic = XCreateIC (context_xim->im_info->im,
+                      XNInputStyle, im_style,
+                      XNClientWindow, GDK_WINDOW_XID (context_xim->client_window),
+                      name1, list1,
+                      name2, list2,
+                      NULL);
+      if (list1)
+       XFree (list1);
+      if (list2)
+       XFree (list2);
+
+      if (xic)
        {
          /* Don't filter key released events with XFilterEvents unless
           * input methods ask for. This is a workaround for Solaris input
@@ -954,203 +1451,385 @@ gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
           * stroke if both key pressed and released events are filtered.
           * (bugzilla #81759)
           */
-         gulong mask = 0;
-         XGetICValues (context_xim->ic,
+         gulong mask = 0xaaaaaaaa;
+         XGetICValues (xic,
                        XNFilterEvents, &mask,
                        NULL);
-         context_xim->filter_key_release = (mask & KeyReleaseMask);
+         context_xim->filter_key_release = (mask & KeyReleaseMask) != 0;
+         set_string_conversion_callback (context_xim, xic);
        }
+      
+      context_xim->ic = xic;
+
+      update_status_window (context_xim);
+      
+      if (xic && context_xim->has_focus)
+       XSetICFocus (xic);
     }
   return context_xim->ic;
 }
 
-/**************************
- *                        *
- * Status Window handling *
- *                        *
- **************************/
-
-static gboolean
-status_window_expose_event (GtkWidget      *widget,
-                           GdkEventExpose *event)
+/*****************************************************************
+ * Status Window handling
+ *
+ * A status window is a small window attached to the toplevel
+ * that is used to display information to the user about the
+ * current input operation.
+ *
+ * We claim the toplevel's status window for an input context if:
+ *
+ * A) The input context has a toplevel
+ * B) The input context has the focus
+ * C) The input context has an XIC associated with it
+ *
+ * Tracking A) and C) is pretty reliable since we
+ * compute A) and create the XIC for C) ourselves.
+ * For B) we basically have to depend on our callers
+ * calling ::focus-in and ::focus-out at the right time.
+ *
+ * The toplevel is computed by walking up the GdkWindow
+ * hierarchy from context->client_window until we find a
+ * window that is owned by some widget, and then calling
+ * gtk_widget_get_toplevel() on that widget. This should
+ * handle both cases where we might have GdkWindows without widgets,
+ * and cases where GtkWidgets have strange window hierarchies
+ * (like a torn off GtkHandleBox.)
+ *
+ * The status window is visible if and only if there is text
+ * for it; whenever a new GtkIMContextXIM claims the status
+ * window, we blank out any existing text. We actually only
+ * create a GtkWindow for the status window the first time
+ * it is shown; this is an important optimization when we are
+ * using XIM with something like a simple compose-key input
+ * method that never needs a status window.
+ *****************************************************************/
+
+/* Called when we no longer need a status window
+*/
+static void
+disclaim_status_window (GtkIMContextXIM *context_xim)
 {
-  gdk_draw_rectangle (widget->window,
-                     widget->style->base_gc [GTK_STATE_NORMAL],
-                     TRUE,
-                     0, 0,
-                     widget->allocation.width, widget->allocation.height);
-  gdk_draw_rectangle (widget->window,
-                     widget->style->text_gc [GTK_STATE_NORMAL],
-                     FALSE,
-                     0, 0,
-                     widget->allocation.width - 1, widget->allocation.height - 1);
+  if (context_xim->status_window)
+    {
+      g_assert (context_xim->status_window->context == context_xim);
 
-  return FALSE;
+      status_window_set_text (context_xim->status_window, "");
+      
+      context_xim->status_window->context = NULL;
+      context_xim->status_window = NULL;
+    }
 }
 
+/* Called when we need a status window
+ */
 static void
-status_window_style_set (GtkWidget *toplevel,
-                        GtkStyle  *previous_style,
-                        GtkWidget *label)
+claim_status_window (GtkIMContextXIM *context_xim)
 {
-  gint i;
-  
-  for (i = 0; i < 5; i++)
-    gtk_widget_modify_fg (label, i, &toplevel->style->text[i]);
+  if (!context_xim->status_window && context_xim->client_widget)
+    {
+      GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
+      if (toplevel && gtk_widget_is_toplevel (toplevel))
+       {
+         StatusWindow *status_window = status_window_get (toplevel);
+
+         if (status_window->context)
+           disclaim_status_window (status_window->context);
+
+         status_window->context = context_xim;
+         context_xim->status_window = status_window;
+       }
+    }
 }
 
-/* Frees a status window and removes its link from the status_windows list */
+/* Basic call made whenever something changed that might cause
+ * us to need, or not to need a status window.
+ */
 static void
-status_window_free (StatusWindow *status_window)
+update_status_window (GtkIMContextXIM *context_xim)
 {
-  status_windows = g_slist_remove (status_windows, status_window);
-  g_signal_handler_disconnect (status_window->toplevel, status_window->destroy_handler_id);
-  g_signal_handler_disconnect (status_window->toplevel, status_window->configure_handler_id);
-  gtk_widget_destroy (status_window->window);
-  g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
-  g_free (status_window);
+  if (context_xim->ic && context_xim->in_toplevel && context_xim->has_focus)
+    claim_status_window (context_xim);
+  else
+    disclaim_status_window (context_xim);
 }
 
-static gboolean
-status_window_configure (GtkWidget         *toplevel,
-                        GdkEventConfigure *event,
-                        StatusWindow      *status_window)
+/* Updates the in_toplevel flag for @context_xim
+ */
+static void
+update_in_toplevel (GtkIMContextXIM *context_xim)
 {
-  GdkRectangle rect;
-  GtkRequisition requisition;
-  gint y;
-  gint height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
-  
-  gdk_window_get_frame_extents (toplevel->window, &rect);
-  gtk_widget_size_request (status_window->window, &requisition);
-
-  if (rect.y + rect.height + requisition.height < height)
-    y = rect.y + rect.height;
+  if (context_xim->client_widget)
+    {
+      GtkWidget *toplevel = gtk_widget_get_toplevel (context_xim->client_widget);
+      
+      context_xim->in_toplevel = (toplevel && gtk_widget_is_toplevel (toplevel));
+    }
   else
-    y = height - requisition.height;
+    context_xim->in_toplevel = FALSE;
+
+  /* Some paranoia, in case we don't get a focus out */
+  if (!context_xim->in_toplevel)
+    context_xim->has_focus = FALSE;
   
-  gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
+  update_status_window (context_xim);
+}
 
-  return FALSE;
+/* Callback when @widget's toplevel changes. It will always
+ * change from NULL to a window, or a window to NULL;
+ * we use that intermediate NULL state to make sure
+ * that we disclaim the toplevel status window for the old
+ * window.
+ */
+static void
+on_client_widget_hierarchy_changed (GtkWidget       *widget,
+                                   GtkWidget       *old_toplevel,
+                                   GtkIMContextXIM *context_xim)
+{
+  update_in_toplevel (context_xim);
 }
 
+/* Finds the GtkWidget that owns the window, or if none, the
+ * widget owning the nearest parent that has a widget.
+ */
 static GtkWidget *
-status_window_get (GtkIMContextXIM *context_xim,
-                  gboolean         create)
+widget_for_window (GdkWindow *window)
 {
-  GdkWindow *toplevel_gdk;
-  GtkWidget *toplevel;
-  GtkWidget *window;
-  StatusWindow *status_window;
-  GtkWidget *status_label;
-  
-  if (!context_xim->client_window)
-    return NULL;
+  while (window)
+    {
+      gpointer user_data;
+      gdk_window_get_user_data (window, &user_data);
+      if (user_data)
+       return user_data;
 
-  toplevel_gdk = context_xim->client_window;
-  while (TRUE)
+      window = gdk_window_get_parent (window);
+    }
+
+  return NULL;
+}
+
+/* Called when context_xim->client_window changes; takes care of
+ * removing and/or setting up our watches for the toplevel
+ */
+static void
+update_client_widget (GtkIMContextXIM *context_xim)
+{
+  GtkWidget *new_client_widget = widget_for_window (context_xim->client_window);
+
+  if (new_client_widget != context_xim->client_widget)
     {
-      GdkWindow *parent = gdk_window_get_parent (toplevel_gdk);
-      if (parent == gdk_get_default_root_window ())
-       break;
-      else
-       toplevel_gdk = parent;
+      if (context_xim->client_widget)
+       {
+         g_signal_handlers_disconnect_by_func (context_xim->client_widget,
+                                               G_CALLBACK (on_client_widget_hierarchy_changed),
+                                               context_xim);
+       }
+      context_xim->client_widget = new_client_widget;
+      if (context_xim->client_widget)
+       {
+         g_signal_connect (context_xim->client_widget, "hierarchy-changed",
+                           G_CALLBACK (on_client_widget_hierarchy_changed),
+                           context_xim);
+       }
+
+      update_in_toplevel (context_xim);
     }
+}
 
-  gdk_window_get_user_data (toplevel_gdk, (gpointer *)&toplevel);
-  if (!toplevel)
-    return NULL;
+/* Called when the toplevel is destroyed; frees the status window
+ */
+static void
+on_status_toplevel_destroy (GtkWidget    *toplevel,
+                           StatusWindow *status_window)
+{
+  status_window_free (status_window);
+}
 
-  status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
-  if (status_window)
-    return status_window->window;
-  else if (!create)
-    return NULL;
+/* Called when the screen for the toplevel changes; updates the
+ * screen for the status window to match.
+ */
+static void
+on_status_toplevel_notify_screen (GtkWindow    *toplevel,
+                                 GParamSpec   *pspec,
+                                 StatusWindow *status_window)
+{
+  if (status_window->window)
+    gtk_window_set_screen (GTK_WINDOW (status_window->window),
+                          gtk_widget_get_screen (GTK_WIDGET (toplevel)));
+}
 
-  status_window = g_new (StatusWindow, 1);
-  status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
-  status_window->toplevel = toplevel;
+/* Called when the toplevel window is moved; updates the position of
+ * the status window to follow it.
+ */
+static gboolean
+on_status_toplevel_configure (GtkWidget         *toplevel,
+                             GdkEventConfigure *event,
+                             StatusWindow      *status_window)
+{
+  GdkRectangle rect;
+  GtkRequisition requisition;
+  gint y;
+  gint height;
 
-  status_windows = g_slist_prepend (status_windows, status_window);
+  if (status_window->window)
+    {
+      height = gdk_screen_get_height (gtk_widget_get_screen (toplevel));
 
-  window = status_window->window;
+      gdk_window_get_frame_extents (gtk_widget_get_window (toplevel),
+                                    &rect);
+      gtk_widget_get_preferred_size ( (status_window->window),
+                                 &requisition, NULL);
 
-  gtk_window_set_policy (GTK_WINDOW (window), FALSE, FALSE, FALSE);
-  gtk_widget_set_app_paintable (window, TRUE);
+      if (rect.y + rect.height + requisition.height < height)
+       y = rect.y + rect.height;
+      else
+       y = height - requisition.height;
+      
+      gtk_window_move (GTK_WINDOW (status_window->window), rect.x, y);
+    }
 
-  status_label = gtk_label_new ("");
-  gtk_misc_set_padding (GTK_MISC (status_label), 1, 1);
-  gtk_widget_show (status_label);
+  return FALSE;
+}
+
+/* Frees a status window and removes its link from the status_windows list
+ */
+static void
+status_window_free (StatusWindow *status_window)
+{
+  status_windows = g_slist_remove (status_windows, status_window);
+
+  if (status_window->context)
+    status_window->context->status_window = NULL;
+  g_signal_handlers_disconnect_by_func (status_window->toplevel,
+                                       G_CALLBACK (on_status_toplevel_destroy),
+                                       status_window);
+  g_signal_handlers_disconnect_by_func (status_window->toplevel,
+                                       G_CALLBACK (on_status_toplevel_notify_screen),
+                                       status_window);
+  g_signal_handlers_disconnect_by_func (status_window->toplevel,
+                                       G_CALLBACK (on_status_toplevel_configure),
+                                       status_window);
+
+  if (status_window->window)
+    gtk_widget_destroy (status_window->window);
   
-  gtk_container_add (GTK_CONTAINER (window), status_label);
+  g_object_set_data (G_OBJECT (status_window->toplevel), "gtk-im-xim-status-window", NULL);
+  g_free (status_window);
+}
 
-  status_window->destroy_handler_id = g_signal_connect_swapped (toplevel, "destroy",
-                                                               G_CALLBACK (status_window_free),
-                                                               status_window);
-  status_window->configure_handler_id = g_signal_connect (toplevel, "configure_event",
-                                                         G_CALLBACK (status_window_configure),
-                                                         status_window);
+/* Finds the status window object for a toplevel, creating it if necessary.
+ */
+static StatusWindow *
+status_window_get (GtkWidget *toplevel)
+{
+  StatusWindow *status_window;
 
-  status_window_configure (toplevel, NULL, status_window);
+  status_window = g_object_get_data (G_OBJECT (toplevel), "gtk-im-xim-status-window");
+  if (status_window)
+    return status_window;
+  
+  status_window = g_new0 (StatusWindow, 1);
+  status_window->toplevel = toplevel;
 
-  g_signal_connect (window, "style_set",
-                   G_CALLBACK (status_window_style_set), status_label);
-  g_signal_connect (window, "expose_event",
-                   G_CALLBACK (status_window_expose_event), NULL);
+  status_windows = g_slist_prepend (status_windows, status_window);
 
+  g_signal_connect (toplevel, "destroy",
+                   G_CALLBACK (on_status_toplevel_destroy),
+                   status_window);
+  g_signal_connect (toplevel, "configure-event",
+                   G_CALLBACK (on_status_toplevel_configure),
+                   status_window);
+  g_signal_connect (toplevel, "notify::screen",
+                   G_CALLBACK (on_status_toplevel_notify_screen),
+                   status_window);
+  
   g_object_set_data (G_OBJECT (toplevel), "gtk-im-xim-status-window", status_window);
 
-  return window;
+  return status_window;
 }
 
+/* Draw the background (normally white) and border for the status window
+ */
 static gboolean
-status_window_has_text (GtkWidget *status_window)
+on_status_window_draw (GtkWidget *widget,
+                       cairo_t   *cr)
 {
-  GtkWidget *label = GTK_BIN (status_window)->child;
-  const gchar *text = gtk_label_get_text (GTK_LABEL (label));
+  GtkStyleContext *style;
+  GdkRGBA color;
 
-  return text[0] != '\0';
-}
+  style = gtk_widget_get_style_context (widget);
 
-static void
-status_window_show (GtkIMContextXIM *context_xim)
-{
-  GtkWidget *status_window = status_window_get (context_xim, TRUE);
+  gtk_style_context_get_background_color (style, 0, &color);
+  gdk_cairo_set_source_rgba (cr, &color);
+  cairo_paint (cr);
 
-  context_xim->status_visible = TRUE;
-  
-  if (status_window && status_window_has_text (status_window))
-    gtk_widget_show (status_window);
+  gtk_style_context_get_color (style, 0, &color);
+  gdk_cairo_set_source_rgba (cr, &color);
+  cairo_paint (cr);
+
+  cairo_rectangle (cr, 
+                   0, 0,
+                   gtk_widget_get_allocated_width (widget) - 1,
+                   gtk_widget_get_allocated_height (widget) - 1);
+  cairo_fill (cr);
+
+  return FALSE;
 }
 
+/* Creates the widgets for the status window; called when we
+ * first need to show text for the status window.
+ */
 static void
-status_window_hide (GtkIMContextXIM *context_xim)
+status_window_make_window (StatusWindow *status_window)
 {
-  GtkWidget *status_window = status_window_get (context_xim, FALSE);
+  GtkWidget *window;
+  GtkWidget *status_label;
+  
+  status_window->window = gtk_window_new (GTK_WINDOW_POPUP);
+  window = status_window->window;
+
+  gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
+  gtk_widget_set_app_paintable (window, TRUE);
 
-  context_xim->status_visible = FALSE;
+  status_label = gtk_label_new ("");
+  g_object_set (status_label, "margin", 1, NULL);
+  gtk_widget_show (status_label);
   
-  if (status_window)
-    gtk_widget_hide (status_window);
+  gtk_container_add (GTK_CONTAINER (window), status_label);
+  
+  g_signal_connect (window, "draw",
+                   G_CALLBACK (on_status_window_draw), NULL);
+  
+  gtk_window_set_screen (GTK_WINDOW (status_window->window),
+                        gtk_widget_get_screen (status_window->toplevel));
+
+  on_status_toplevel_configure (status_window->toplevel, NULL, status_window);
 }
 
+/* Updates the text in the status window, hiding or
+ * showing the window as necessary.
+ */
 static void
-status_window_set_text (GtkIMContextXIM *context_xim,
-                       const gchar     *text)
+status_window_set_text (StatusWindow *status_window,
+                       const gchar  *text)
 {
-  GtkWidget *status_window = status_window_get (context_xim, TRUE);
-
-  if (status_window)
+  if (text[0])
     {
-      GtkWidget *label = GTK_BIN (status_window)->child;
-      gtk_label_set_text (GTK_LABEL (label), text);
+      GtkWidget *label;
       
-      if (context_xim->status_visible && status_window_has_text (status_window))
-       gtk_widget_show (status_window);
-      else
-       gtk_widget_hide (status_window);
+      if (!status_window->window)
+       status_window_make_window (status_window);
+      
+      label = gtk_bin_get_child (GTK_BIN (status_window->window));
+      gtk_label_set_text (GTK_LABEL (label), text);
+  
+      gtk_widget_show (status_window->window);
+    }
+  else
+    {
+      if (status_window->window)
+       gtk_widget_hide (status_window->window);
     }
 }
 
@@ -1165,4 +1844,13 @@ gtk_im_context_xim_shutdown (void)
 {
   while (status_windows)
     status_window_free (status_windows->data);
+
+  while (open_ims)
+    {
+      GtkXIMInfo *info = open_ims->data;
+      GdkDisplay *display = gdk_screen_get_display (info->screen);
+
+      xim_info_display_closed (display, FALSE, info);
+      open_ims = g_slist_remove_link (open_ims, open_ims);
+    }
 }