editor: support commit on reset

Commit pending pre-edit text when focus-out or changing the cursor
location.

Signed-off-by: Jan Arne Petersen <jpetersen@openismus.com>
dev
Jan Arne Petersen 12 years ago committed by Kristian Høgsberg
parent c7d2a9839b
commit 4a17fae8e7
  1. 54
      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, static void text_entry_delete_text(struct text_entry *entry,
uint32_t index, uint32_t length); uint32_t index, uint32_t length);
static void text_entry_delete_selected_text(struct text_entry *entry); 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 static void
text_model_commit_string(void *data, text_model_commit_string(void *data,
@ -310,6 +312,8 @@ text_model_commit_string(void *data,
if (index > strlen(text)) if (index > strlen(text))
fprintf(stderr, "Invalid cursor index %d\n", index); fprintf(stderr, "Invalid cursor index %d\n", index);
text_entry_reset_preedit(entry);
text_entry_delete_selected_text(entry); text_entry_delete_selected_text(entry);
text_entry_insert_at_cursor(entry, text); 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_delete_selected_text(entry);
text_entry_set_preedit(entry, text, entry->preedit_info.cursor); 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); widget_schedule_redraw(entry->widget);
} }
@ -484,6 +491,8 @@ text_model_leave(void *data,
{ {
struct text_entry *entry = data; struct text_entry *entry = data;
text_entry_commit_and_reset(entry);
entry->active = 0; entry->active = 0;
widget_schedule_redraw(entry->widget); widget_schedule_redraw(entry->widget);
@ -665,15 +674,38 @@ text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
} }
static void static void
text_entry_set_preedit(struct text_entry *entry, text_entry_reset_preedit(struct text_entry *entry)
const char *preedit_text,
int preedit_cursor)
{ {
if (entry->preedit.text) { entry->preedit.cursor = 0;
free(entry->preedit.text); free(entry->preedit.text);
entry->preedit.text = NULL; entry->preedit.text = NULL;
entry->preedit.cursor = 0;
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)
{
text_entry_reset_preedit(entry);
if (!preedit_text) if (!preedit_text)
return; return;
@ -688,6 +720,8 @@ static void
text_entry_set_cursor_position(struct text_entry *entry, text_entry_set_cursor_position(struct text_entry *entry,
int32_t x, int32_t y) int32_t x, int32_t y)
{ {
text_entry_commit_and_reset(entry);
entry->cursor = text_layout_xy_to_index(entry->layout, x, y); entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
entry->serial++; entry->serial++;
@ -979,6 +1013,8 @@ key_handler(struct window *window,
switch (sym) { switch (sym) {
case XKB_KEY_BackSpace: case XKB_KEY_BackSpace:
text_entry_commit_and_reset(entry);
start = utf8_prev_char(entry->text, entry->text + entry->cursor); start = utf8_prev_char(entry->text, entry->text + entry->cursor);
if (start == NULL) if (start == NULL)
@ -990,6 +1026,8 @@ key_handler(struct window *window,
end - start); end - start);
break; break;
case XKB_KEY_Delete: case XKB_KEY_Delete:
text_entry_commit_and_reset(entry);
start = utf8_start_char(entry->text, entry->text + entry->cursor); start = utf8_start_char(entry->text, entry->text + entry->cursor);
if (start == NULL) if (start == NULL)
@ -1005,6 +1043,8 @@ key_handler(struct window *window,
end - start); end - start);
break; break;
case XKB_KEY_Left: case XKB_KEY_Left:
text_entry_commit_and_reset(entry);
new_char = utf8_prev_char(entry->text, entry->text + entry->cursor); new_char = utf8_prev_char(entry->text, entry->text + entry->cursor);
if (new_char != NULL) { if (new_char != NULL) {
entry->cursor = new_char - entry->text; entry->cursor = new_char - entry->text;
@ -1013,6 +1053,8 @@ key_handler(struct window *window,
} }
break; break;
case XKB_KEY_Right: case XKB_KEY_Right:
text_entry_commit_and_reset(entry);
new_char = utf8_next_char(entry->text + entry->cursor); new_char = utf8_next_char(entry->text + entry->cursor);
if (new_char != NULL) { if (new_char != NULL) {
entry->cursor = new_char - entry->text; entry->cursor = new_char - entry->text;
@ -1024,6 +1066,8 @@ key_handler(struct window *window,
if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0)
break; break;
text_entry_commit_and_reset(entry);
text_entry_insert_at_cursor(entry, text); text_entry_insert_at_cursor(entry, text);
break; break;
} }

Loading…
Cancel
Save