[PATCH v2 14/17] text: Add support for pre-edit string

Jan Arne Petersen jpetersen at openismus.com
Sun Sep 9 14:08:43 PDT 2012


From: Jan Arne Petersen <jpetersen at openismus.com>

Add support of preedit-string to the example editor client. Also add a
preedit_string request to the input_method_context interface and use
that in the example weston keyboard to first create a pre-edit string
when entering keys and commit it on space.

Signed-off-by: Jan Arne Petersen <jpetersen at openismus.com>
---
 clients/editor.c          | 13 +++++++++++++
 clients/keyboard.c        | 24 +++++++++++++++++++++---
 protocol/input-method.xml |  7 +++++++
 src/text-backend.c        | 14 +++++++++++++-
 4 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/clients/editor.c b/clients/editor.c
index b0b400e..b6a1742 100644
--- a/clients/editor.c
+++ b/clients/editor.c
@@ -209,6 +209,9 @@ static void text_entry_button_handler(struct widget *widget,
 				      uint32_t button,
 				      enum wl_pointer_button_state state, void *data);
 static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text);
+static void text_entry_set_preedit(struct text_entry *entry,
+				   const char *preedit_text,
+				   int preedit_cursor);
 
 static void
 text_model_commit_string(void *data,
@@ -234,6 +237,16 @@ text_model_preedit_string(void *data,
 			  const char *text,
 			  uint32_t index)
 {
+	struct text_entry *entry = data;
+
+	if (index > strlen(text)) {
+		fprintf(stderr, "Invalid cursor index %d\n", index);
+		index = strlen(text);
+	}
+
+	text_entry_set_preedit(entry, text, index);
+
+	widget_schedule_redraw(entry->widget);
 }
 
 static void
diff --git a/clients/keyboard.c b/clients/keyboard.c
index 6e73b33..4bc7d24 100644
--- a/clients/keyboard.c
+++ b/clients/keyboard.c
@@ -37,6 +37,7 @@ struct virtual_keyboard {
 	struct input_method *input_method;
 	struct input_method_context *context;
 	struct display *display;
+	char *preedit_string;
 };
 
 enum key_type {
@@ -214,16 +215,27 @@ keyboard_handle_key(struct keyboard *keyboard, const struct key *key)
 
 	switch (key->key_type) {
 		case keytype_default:
-			input_method_context_commit_string(keyboard->keyboard->context,
-							   label, -1);
+			keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
+								    label);
+			input_method_context_preedit_string(keyboard->keyboard->context,
+							    keyboard->keyboard->preedit_string,
+							    strlen(keyboard->keyboard->preedit_string));
 			break;
 		case keytype_backspace:
 			break;
 		case keytype_enter:
 			break;
 		case keytype_space:
+			keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
+								    " ");
+			input_method_context_preedit_string(keyboard->keyboard->context,
+							    "",
+							    0);
 			input_method_context_commit_string(keyboard->keyboard->context,
-							   " ", -1);
+							   keyboard->keyboard->preedit_string,
+							   strlen(keyboard->keyboard->preedit_string));
+			free(keyboard->keyboard->preedit_string);
+			keyboard->keyboard->preedit_string = strdup("");
 			break;
 		case keytype_switch:
 			if (keyboard->state == keyboardstate_default)
@@ -297,6 +309,11 @@ input_method_activate(void *data,
 	if (keyboard->context)
 		input_method_context_destroy(keyboard->context);
 
+	if (keyboard->preedit_string)
+		free(keyboard->preedit_string);
+
+	keyboard->preedit_string = strdup("");
+
 	keyboard->context = context;
 	input_method_context_add_listener(context,
 					  &input_method_context_listener,
@@ -390,6 +407,7 @@ main(int argc, char *argv[])
 	}
 
 	virtual_keyboard.context = NULL;
+	virtual_keyboard.preedit_string = NULL;
 
 	wl_display_add_global_listener(display_get_display(virtual_keyboard.display),
 				       global_handler, &virtual_keyboard);
diff --git a/protocol/input-method.xml b/protocol/input-method.xml
index be68d85..9baff62 100644
--- a/protocol/input-method.xml
+++ b/protocol/input-method.xml
@@ -42,6 +42,13 @@
       <arg name="text" type="string"/>
       <arg name="index" type="uint"/>
     </request>
+    <request name="preedit_string">
+      <description summary="pre-edit string">
+        Send the pre-edit string text to the applications text model.
+      </description>
+      <arg name="text" type="string"/>
+      <arg name="index" type="uint"/>
+    </request>
     <event name="surrounding_text">
       <description summary="surrounding text event">
         The plain surrounding text around the input position. Cursor is the
diff --git a/src/text-backend.c b/src/text-backend.c
index 63b6b57..4fb4d95 100644
--- a/src/text-backend.c
+++ b/src/text-backend.c
@@ -294,9 +294,21 @@ input_method_context_commit_string(struct wl_client *client,
 	text_model_send_commit_string(&context->model->resource, text, index);
 }
 
+static void
+input_method_context_preedit_string(struct wl_client *client,
+				   struct wl_resource *resource,
+				   const char *text,
+				   uint32_t index)
+{
+	struct input_method_context *context = resource->data;
+
+	text_model_send_preedit_string(&context->model->resource, text, index);
+}
+
 static const struct input_method_context_interface input_method_context_implementation = {
 	input_method_context_destroy,
-	input_method_context_commit_string
+	input_method_context_commit_string,
+	input_method_context_preedit_string,
 };
 
 static void
-- 
1.7.11.4



More information about the wayland-devel mailing list