text: Add support for pre-edit string
authorJan Arne Petersen <jpetersen@openismus.com>
Sun, 9 Sep 2012 21:08:43 +0000 (23:08 +0200)
committerKristian Høgsberg <krh@bitplanet.net>
Wed, 12 Sep 2012 20:50:44 +0000 (16:50 -0400)
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@openismus.com>
clients/editor.c
clients/keyboard.c
protocol/input-method.xml
src/text-backend.c

index b0b400e..b6a1742 100644 (file)
@@ -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
index 6e73b33..4bc7d24 100644 (file)
@@ -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);
index be68d85..9baff62 100644 (file)
       <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
index 63b6b57..4fb4d95 100644 (file)
@@ -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