[PATCH 3/5] editor: support commit on reset
Jan Arne Petersen
jpetersen at openismus.com
Fri Nov 30 08:01:42 PST 2012
From: Jan Arne Petersen <jpetersen at openismus.com>
Commit pending pre-edit text when focus-out or changing the cursor
location.
Signed-off-by: Jan Arne Petersen <jpetersen at openismus.com>
---
clients/editor.c | 46 +++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 41 insertions(+), 5 deletions(-)
diff --git a/clients/editor.c b/clients/editor.c
index 890b32c..a10e256 100644
--- a/clients/editor.c
+++ b/clients/editor.c
@@ -297,6 +297,8 @@ static void text_entry_set_preedit(struct text_entry *entry,
static void text_entry_delete_text(struct text_entry *entry,
uint32_t index, uint32_t length);
static void text_entry_delete_selected_text(struct text_entry *entry);
+static void text_entry_reset_preedit(struct text_entry *entry);
+static void text_entry_commit_and_reset(struct text_entry *entry);
static void
text_model_commit_string(void *data,
@@ -310,6 +312,8 @@ text_model_commit_string(void *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);
@@ -327,6 +331,9 @@ text_model_preedit_string(void *data,
text_entry_delete_selected_text(entry);
text_entry_set_preedit(entry, text, entry->preedit_info.cursor);
+ entry->preedit.commit = strdup(commit);
+
+ entry->preedit_info.cursor = 0;
widget_schedule_redraw(entry->widget);
}
@@ -484,6 +491,8 @@ text_model_leave(void *data,
{
struct text_entry *entry = data;
+ text_entry_commit_and_reset(entry);
+
entry->active = 0;
widget_schedule_redraw(entry->widget);
@@ -665,15 +674,38 @@ text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
}
static void
+text_entry_reset_preedit(struct text_entry *entry)
+{
+ entry->preedit.cursor = 0;
+
+ free(entry->preedit.text);
+ entry->preedit.text = NULL;
+
+ free(entry->preedit.commit);
+ entry->preedit.commit = NULL;
+}
+
+static void
+text_entry_commit_and_reset(struct text_entry *entry)
+{
+ char *commit = NULL;
+
+ if (entry->preedit.commit)
+ commit = strdup(entry->preedit.commit);
+
+ text_entry_reset_preedit(entry);
+ if (commit) {
+ text_entry_insert_at_cursor(entry, commit);
+ free(commit);
+ }
+}
+
+static void
text_entry_set_preedit(struct text_entry *entry,
const char *preedit_text,
int preedit_cursor)
{
- if (entry->preedit.text) {
- free(entry->preedit.text);
- entry->preedit.text = NULL;
- entry->preedit.cursor = 0;
- }
+ text_entry_reset_preedit(entry);
if (!preedit_text)
return;
@@ -688,6 +720,8 @@ static void
text_entry_set_cursor_position(struct text_entry *entry,
int32_t x, int32_t y)
{
+ text_entry_commit_and_reset(entry);
+
entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
entry->serial++;
@@ -977,6 +1011,8 @@ key_handler(struct window *window,
if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
return;
+ text_entry_commit_and_reset(entry);
+
switch (sym) {
case XKB_KEY_BackSpace:
start = utf8_prev_char(entry->text, entry->text + entry->cursor);
--
1.7.11.7
More information about the wayland-devel
mailing list