diff options
author | Jan Arne Petersen <jpetersen@openismus.com> | 2013-01-31 15:52:23 +0100 |
---|---|---|
committer | Kristian Høgsberg <krh@bitplanet.net> | 2013-02-15 17:08:01 -0500 |
commit | 1cc9e08d2fbd59e34cf5009acd5067a6b171c20a (patch) | |
tree | 2ca0e325d7c01c357d8d85174b15006054c26ed2 /clients | |
parent | 964b517c9b39d9655257c3968c32c0fb81be9613 (diff) |
text: Split out cursor_position
Add an extra cursor_position, which also allows to change the anchor
(for slections). Change the index type to int to allow setting it before
the beginning of a commited string.
The cursor should not be moved as a direct repsonse to this event but
atomically on the next commit_string event.
Signed-off-by: Jan Arne Petersen <jpetersen@openismus.com>
Diffstat (limited to 'clients')
-rw-r--r-- | clients/editor.c | 50 | ||||
-rw-r--r-- | clients/keyboard.c | 6 | ||||
-rw-r--r-- | clients/weston-simple-im.c | 18 |
3 files changed, 54 insertions, 20 deletions
diff --git a/clients/editor.c b/clients/editor.c index a830ead..32c3f9b 100644 --- a/clients/editor.c +++ b/clients/editor.c @@ -53,6 +53,10 @@ struct text_entry { PangoAttrList *attr_list; int32_t cursor; } preedit_info; + struct { + int32_t cursor; + int32_t anchor; + } pending_commit; struct text_model *model; PangoLayout *layout; struct { @@ -112,7 +116,8 @@ static void text_entry_button_handler(struct widget *widget, struct input *input, uint32_t time, 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_insert_at_cursor(struct text_entry *entry, const char *text, + int32_t cursor, int32_t anchor); static void text_entry_set_preedit(struct text_entry *entry, const char *preedit_text, int preedit_cursor); @@ -126,18 +131,18 @@ static void text_model_commit_string(void *data, struct text_model *text_model, uint32_t serial, - const char *text, - uint32_t index) + const char *text) { struct text_entry *entry = data; - if (index > strlen(text)) - fprintf(stderr, "Invalid cursor index %d\n", index); - text_entry_reset_preedit(entry); text_entry_delete_selected_text(entry); - text_entry_insert_at_cursor(entry, text); + text_entry_insert_at_cursor(entry, text, + entry->pending_commit.cursor, + entry->pending_commit.anchor); + + memset(&entry->pending_commit, 0, sizeof entry->pending_commit); widget_schedule_redraw(entry->widget); } @@ -195,6 +200,19 @@ text_model_delete_surrounding_text(void *data, } static void +text_model_cursor_position(void *data, + struct text_model *text_model, + uint32_t serial, + int32_t index, + int32_t anchor) +{ + struct text_entry *entry = data; + + entry->pending_commit.cursor = index; + entry->pending_commit.anchor = anchor; +} + +static void text_model_preedit_styling(void *data, struct text_model *text_model, uint32_t serial, @@ -377,6 +395,7 @@ static const struct text_model_listener text_model_listener = { text_model_commit_string, text_model_preedit_string, text_model_delete_surrounding_text, + text_model_cursor_position, text_model_preedit_styling, text_model_preedit_cursor, text_model_modifiers_map, @@ -583,7 +602,8 @@ text_entry_update(struct text_entry *entry) } static void -text_entry_insert_at_cursor(struct text_entry *entry, const char *text) +text_entry_insert_at_cursor(struct text_entry *entry, const char *text, + int32_t cursor, int32_t anchor) { char *new_text = malloc(strlen(entry->text) + strlen(text) + 1); @@ -594,8 +614,14 @@ text_entry_insert_at_cursor(struct text_entry *entry, const char *text) free(entry->text); entry->text = new_text; - entry->cursor += strlen(text); - entry->anchor += strlen(text); + if (anchor >= 0) + entry->anchor = entry->cursor + strlen(text) + anchor; + else + entry->anchor = entry->cursor + 1 + anchor; + if (cursor >= 0) + entry->cursor += strlen(text) + cursor; + else + entry->cursor += 1 + cursor; text_entry_update_layout(entry); @@ -629,7 +655,7 @@ text_entry_commit_and_reset(struct text_entry *entry) text_entry_reset_preedit(entry); if (commit) { - text_entry_insert_at_cursor(entry, commit); + text_entry_insert_at_cursor(entry, commit, 0, 0); free(commit); } } @@ -993,7 +1019,7 @@ key_handler(struct window *window, text_entry_commit_and_reset(entry); - text_entry_insert_at_cursor(entry, text); + text_entry_insert_at_cursor(entry, text, 0, 0); break; } diff --git a/clients/keyboard.c b/clients/keyboard.c index 8d94870..3760ff7 100644 --- a/clients/keyboard.c +++ b/clients/keyboard.c @@ -321,10 +321,12 @@ virtual_keyboard_commit_preedit(struct virtual_keyboard *keyboard) keyboard->serial, "", ""); + input_method_context_cursor_position(keyboard->context, + keyboard->serial, + 0, 0); input_method_context_commit_string(keyboard->context, keyboard->serial, - keyboard->preedit_string, - strlen(keyboard->preedit_string)); + keyboard->preedit_string); free(keyboard->preedit_string); keyboard->preedit_string = strdup(""); } diff --git a/clients/weston-simple-im.c b/clients/weston-simple-im.c index fab7fc5..e955d3c 100644 --- a/clients/weston-simple-im.c +++ b/clients/weston-simple-im.c @@ -409,10 +409,12 @@ simple_im_key_handler(struct simple_im *keyboard, input_method_context_preedit_string(keyboard->context, keyboard->serial, "", ""); + input_method_context_cursor_position(keyboard->context, + keyboard->serial, + 0, 0); input_method_context_commit_string(keyboard->context, keyboard->serial, - cs->text, - strlen(cs->text)); + cs->text); keyboard->compose_state = state_normal; } else { uint32_t j = 0, idx = 0; @@ -441,10 +443,12 @@ simple_im_key_handler(struct simple_im *keyboard, input_method_context_preedit_string(keyboard->context, keyboard->serial, "", ""); + input_method_context_cursor_position(keyboard->context, + keyboard->serial, + 0, 0); input_method_context_commit_string(keyboard->context, keyboard->serial, - text, - strlen(text)); + text); keyboard->compose_state = state_normal; } return; @@ -458,10 +462,12 @@ simple_im_key_handler(struct simple_im *keyboard, if (state == WL_KEYBOARD_KEY_STATE_PRESSED) return; + input_method_context_cursor_position(keyboard->context, + keyboard->serial, + 0, 0); input_method_context_commit_string(keyboard->context, keyboard->serial, - text, - strlen(text)); + text); } int |