Permalink
Browse files

Merge pull request #1637 from luyikei/fix-im-problem

Support inline input (pre-edit,surrounding-text) when using input-method, Fix #1071
  • Loading branch information...
2 parents dc5dc5f + d57a386 commit c6f9ccb9a9dd3abdf92eba16c0343f2b030acbca @clefebvre clefebvre committed Feb 27, 2013
Showing with 72 additions and 0 deletions.
  1. +72 −0 src/st/st-im-text.c
View
@@ -140,6 +140,72 @@ st_im_text_commit_cb (GtkIMContext *context,
}
}
+static void
+st_im_text_preedit_changed_cb (GtkIMContext *context,
+ StIMText *imtext)
+{
+ ClutterText *clutter_text = CLUTTER_TEXT (imtext);
+ gchar *preedit_str = NULL;
+ PangoAttrList *preedit_attrs = NULL;
+ gint cursor_pos = 0;
+
+ gtk_im_context_get_preedit_string (context,
+ &preedit_str,
+ &preedit_attrs,
+ &cursor_pos);
+
+ clutter_text_set_preedit_string (clutter_text,
+ preedit_str,
+ preedit_attrs,
+ cursor_pos);
+
+ g_free (preedit_str);
+ pango_attr_list_unref (preedit_attrs);
+}
+
+static gboolean
+st_im_text_retrieve_surrounding_cb (GtkIMContext *context,
+ StIMText *imtext)
+{
+ ClutterText *clutter_text = CLUTTER_TEXT (imtext);
+ ClutterTextBuffer *buffer;
+ const gchar *text;
+ gint cursor_pos;
+
+ buffer = clutter_text_get_buffer (clutter_text);
+ text = clutter_text_buffer_get_text (buffer);
+
+ cursor_pos = clutter_text_get_cursor_position (clutter_text);
+ if (cursor_pos < 0)
+ cursor_pos = clutter_text_buffer_get_length (buffer);
+
+ gtk_im_context_set_surrounding (context, text,
+ /* length and cursor_index are in bytes */
+ clutter_text_buffer_get_bytes (buffer),
+ g_utf8_offset_to_pointer (text, cursor_pos) - text);
+
+ return TRUE;
+}
+
+static gboolean
+st_im_text_delete_surrounding_cb (GtkIMContext *context,
+ gint offset,
+ gint n_chars,
+ StIMText *imtext)
+{
+ ClutterText *clutter_text = CLUTTER_TEXT (imtext);
+
+ if (clutter_text_get_editable (clutter_text))
+ {
+ gint cursor_pos = clutter_text_get_cursor_position (clutter_text);
+ clutter_text_delete_text (clutter_text,
+ cursor_pos + offset,
+ cursor_pos + offset + n_chars);
+ }
+
+ return TRUE;
+}
+
static void
reset_im_context (StIMText *self)
{
@@ -445,6 +511,12 @@ st_im_text_init (StIMText *self)
priv->im_context = gtk_im_multicontext_new ();
g_signal_connect (priv->im_context, "commit",
G_CALLBACK (st_im_text_commit_cb), self);
+ g_signal_connect (priv->im_context, "preedit-changed",
+ G_CALLBACK (st_im_text_preedit_changed_cb), self);
+ g_signal_connect (priv->im_context, "retrieve-surrounding",
+ G_CALLBACK (st_im_text_retrieve_surrounding_cb), self);
+ g_signal_connect (priv->im_context, "delete-surrounding",
+ G_CALLBACK (st_im_text_delete_surrounding_cb), self);
}
/**

0 comments on commit c6f9ccb

Please sign in to comment.