]> Pileus Git - ~andy/gtk/blobdiff - modules/input/gtkimcontextxim.c
Cleanups
[~andy/gtk] / modules / input / gtkimcontextxim.c
index 2be36e2026795d6f02ca28756f1e9df785dbba86..68f6ec4f9d91abf6e0d36c0efaf0d3b892b80a78 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+#include <config.h>
 #include "locale.h"
 #include <string.h>
+#include <stdlib.h>
 
 #include "gtk/gtkintl.h"
 #include "gtk/gtklabel.h"
@@ -62,6 +64,8 @@ struct _GtkIMContextXIM
   XIMCallback status_done_callback;
   XIMCallback status_draw_callback;
 
+  XIMCallback string_conversion_callback;
+
   XIC ic;
 
   guint filter_key_release : 1;
@@ -86,6 +90,7 @@ struct _GtkXIMInfo
   GSList *ics;
 
   guint reconnecting :1;
+  guint supports_string_conversion;
 };
 
 /* A context status window; these are kept in the status_windows list. */
@@ -142,7 +147,7 @@ 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;
@@ -322,8 +327,8 @@ setup_im (GtkXIMInfo *info)
   if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
                                     "gtk-im-preedit-style"))
     gtk_settings_install_property (g_param_spec_enum ("gtk-im-preedit-style",
-                                                     _("IM Preedit style"),
-                                                     _("How to draw the input method preedit string"),
+                                                     P_("IM Preedit style"),
+                                                     P_("How to draw the input method preedit string"),
                                                      GTK_TYPE_IM_PREEDIT_STYLE,
                                                      GTK_IM_PREEDIT_CALLBACK,
                                                      G_PARAM_READWRITE));
@@ -331,8 +336,8 @@ setup_im (GtkXIMInfo *info)
   if (!g_object_class_find_property (G_OBJECT_GET_CLASS (info->settings),
                                     "gtk-im-status-style"))
     gtk_settings_install_property (g_param_spec_enum ("gtk-im-status-style",
-                                                     _("IM Status style"),
-                                                     _("How to draw the input method statusbar"),
+                                                     P_("IM Status style"),
+                                                     P_("How to draw the input method statusbar"),
                                                      GTK_TYPE_IM_STATUS_STYLE,
                                                      GTK_IM_STATUS_CALLBACK,
                                                      G_PARAM_READWRITE));
@@ -346,21 +351,31 @@ setup_im (GtkXIMInfo *info)
                                                G_CALLBACK (preedit_style_change),
                                                info);
 
-  status_style_change (info);
-  preedit_style_change (info);
-
-#if 0
+  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 (ic_values)
-    XFree (ic_values);
+  status_style_change (info);
+  preedit_style_change (info);
 }
 
 static void
@@ -550,10 +565,36 @@ gtk_im_context_xim_finalize (GObject *obj)
 
   context_xim->finalizing = TRUE;
 
+  if (context_xim->im_info && !context_xim->im_info->ics->next) 
+    {
+      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
+       {
+         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
@@ -616,7 +657,7 @@ 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));
+  result = g_object_new (GTK_TYPE_IM_CONTEXT_XIM, NULL);
 
   result->locale = g_strdup (setlocale (LC_CTYPE, NULL));
   
@@ -793,7 +834,7 @@ gtk_im_context_xim_set_cursor_location (GtkIMContext *context,
 
   preedit_attr = XVaCreateNestedList (0,
                                      XNSpotLocation, &spot,
-                                     0);
+                                     NULL);
   XSetICValues (ic,
                XNPreeditAttributes, preedit_attr,
                NULL);
@@ -840,7 +881,7 @@ gtk_im_context_xim_reset (GtkIMContext *context)
 
   preedit_attr = XVaCreateNestedList(0,
                                      XNPreeditState, &preedit_state,
-                                     0);
+                                     NULL);
   if (!XGetICValues(ic,
                     XNPreeditAttributes, preedit_attr,
                     NULL))
@@ -852,7 +893,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,
@@ -966,7 +1007,7 @@ 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)
@@ -976,6 +1017,8 @@ preedit_start_callback (XIC      xic,
   
   if (!context_xim->finalizing)
     g_signal_emit_by_name (context, "preedit_start");
+
+  return -1;                   /* No length limit */
 }                   
 
 static void
@@ -986,6 +1029,13 @@ preedit_done_callback (XIC      xic,
   GtkIMContext *context = GTK_IM_CONTEXT (client_data);
   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");  
 }                   
@@ -1174,6 +1224,120 @@ status_draw_callback (XIC      xic,
     }
 }
 
+static void
+string_conversion_callback (XIC xic, XPointer client_data, XPointer call_data)
+{
+  GtkIMContextXIM *context_xim;
+  XIMStringConversionCallbackStruct *conv_data;
+  gchar *surrounding;
+  gint  cursor_index;
+
+  context_xim = (GtkIMContextXIM *)client_data;
+  conv_data = (XIMStringConversionCallbackStruct *)call_data;
+
+  if (gtk_im_context_get_surrounding ((GtkIMContext *)context_xim,
+                                      &surrounding, &cursor_index))
+    {
+      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);
+    }
+}
+
+
 static XVaNestedList
 set_preedit_callback (GtkIMContextXIM *context_xim)
 {
@@ -1211,6 +1375,21 @@ set_status_callback (GtkIMContextXIM *context_xim)
 }
 
 
+static void
+set_string_conversion_callback (GtkIMContextXIM *context_xim, XIC xic)
+{
+  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)
 {
@@ -1224,7 +1403,7 @@ gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
       const char *name2 = NULL;
       XVaNestedList list2 = NULL;
       XIMStyle im_style = 0;
-      XIC xic = 0;
+      XIC xic = NULL;
 
       if (context_xim->use_preedit &&
          (context_xim->im_info->style & PREEDIT_MASK) == XIMPreeditCallbacks)
@@ -1276,11 +1455,12 @@ gtk_im_context_xim_get_ic (GtkIMContextXIM *context_xim)
           * stroke if both key pressed and released events are filtered.
           * (bugzilla #81759)
           */
-         guint32 mask = 0;
+         gulong mask = 0xaaaaaaaa;
          XGetICValues (xic,
                        XNFilterEvents, &mask,
                        NULL);
          context_xim->filter_key_release = (mask & KeyReleaseMask) != 0;
+         set_string_conversion_callback (context_xim, xic);
        }
       
       context_xim->ic = xic;