--- gtk-im/gtkimcontextgcin.c.orig	2005-09-16 13:32:08.000000000 +0800
+++ gtk-im/gtkimcontextgcin.c	2005-12-14 08:04:27.000000000 +0800
@@ -33,6 +33,14 @@
 
 #if NEW_GTK_IM
 static GtkIMContextGCIN *context_xim_queued;
+static int timeout_handle;
+static void cancel_timeout()
+{
+  if (!context_xim_queued)
+    return;
+  g_source_remove(timeout_handle);
+  context_xim_queued = NULL;
+}
 #endif
 
 struct _GtkIMContextGCIN
@@ -110,6 +118,9 @@
 			 gboolean    is_error,
                          GtkIMContextGCIN *context_xim)
 {
+#if NEW_GTK_IM
+  cancel_timeout();
+#endif
   if (!context_xim->gcin_ch)
     return;
 
@@ -147,6 +158,10 @@
   GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
 
+#if NEW_GTK_IM
+  cancel_timeout();
+#endif
+
 //  parent_class = g_type_class_peek_parent (class);
   im_context_class->set_client_window = gtk_im_context_gcin_set_client_window;
   im_context_class->filter_keypress = gtk_im_context_gcin_filter_keypress;
@@ -238,6 +253,10 @@
 		      GdkWindow       *client_window)
 {
 //  printf("set_ic_client_window\n");
+#if NEW_GTK_IM
+  if (context_xim != context_xim_queued)
+    cancel_timeout();
+#endif
 
   context_xim->client_window = client_window;
 
@@ -294,7 +313,9 @@
 {
 //  printf("gtk_im_context_gcin_new\n");
   GtkIMContextGCIN *result;
-
+#if NEW_GTK_IM
+  cancel_timeout();
+#endif
   result = GTK_IM_CONTEXT_GCIN (g_object_new (GTK_TYPE_IM_CONTEXT_GCIN, NULL));
 
   return GTK_IM_CONTEXT (result);
@@ -370,12 +391,14 @@
     }
 #endif
 
-#if NEW_GTK_IM
+#if NEW_GTK_IM && 1
     // this one is for mozilla
     if (!context_xim_queued) {
         context_xim_queued = context_xim;
-        g_timeout_add(200, update_cursor_position, NULL);
-    }
+        timeout_handle = g_timeout_add(200, update_cursor_position, NULL);
+    } else
+    if (context_xim != context_xim_queued)
+        cancel_timeout();
 #endif
 
     if (!rstr && !result && num_bytes && buffer[0]>=0x20 && buffer[0]!=0x7f) {
@@ -410,7 +433,9 @@
 
 //  printf("gtk_im_context_gcin_focus_in\n");
   if (context_xim->gcin_ch) {
+#if NEW_GTK_IM
     g_signal_emit_by_name(context_xim, "preedit_changed");
+#endif
     gcin_im_client_focus_in(context_xim->gcin_ch);
   }
 
@@ -422,6 +447,10 @@
 {
   GtkIMContextGCIN *context_xim = GTK_IM_CONTEXT_GCIN (context);
 //  printf("gtk_im_context_gcin_focus_out\n");
+#if NEW_GTK_IM
+  if (context_xim == context_xim_queued)
+      cancel_timeout(timeout_handle);
+#endif
 
   if (context_xim->gcin_ch) {
     gcin_im_client_focus_out(context_xim->gcin_ch);
@@ -496,4 +525,7 @@
 void
 gtk_im_context_gcin_shutdown (void)
 {
+#if NEW_GTK_IM
+  cancel_timeout();
+#endif
 }
