static const char * utf8_next_char(const char *p) { if (*p != 0) return utf8_end_char(++p); return NULL; }
static void text_model_delete_surrounding_text(void *data, struct text_model *text_model, int32_t index, uint32_t length) { struct text_entry *entry = data; uint32_t cursor_index = index + entry->cursor; const char *start, *end; if (cursor_index > strlen(entry->text)) { fprintf(stderr, "Invalid cursor index %d\n", index); return; } if (cursor_index + length > strlen(entry->text)) { fprintf(stderr, "Invalid length %d\n", length); return; } if (length == 0) return; start = utf8_start_char(entry->text, entry->text + cursor_index); end = utf8_end_char(entry->text + cursor_index + length); text_entry_delete_text(entry, start - entry->text, end - start); }
static void key_handler(struct window *window, struct input *input, uint32_t time, uint32_t key, uint32_t sym, enum wl_keyboard_key_state state, void *data) { struct editor *editor = data; struct text_entry *entry; const char *start, *end, *new_char; char text[16]; if (!editor->active_entry) return; entry = editor->active_entry; if (state != WL_KEYBOARD_KEY_STATE_PRESSED) return; switch (sym) { case XKB_KEY_BackSpace: start = utf8_prev_char(entry->text, entry->text + entry->cursor); if (start == NULL) break; end = utf8_end_char(entry->text + entry->cursor); text_entry_delete_text(entry, start - entry->text, end - start); break; case XKB_KEY_Delete: start = utf8_start_char(entry->text, entry->text + entry->cursor); if (start == NULL) break; end = utf8_next_char(start); if (end == NULL) break; text_entry_delete_text(entry, start - entry->text, end - start); break; case XKB_KEY_Left: new_char = utf8_prev_char(entry->text, entry->text + entry->cursor); if (new_char != NULL) { entry->cursor = new_char - entry->text; entry->anchor = entry->cursor; widget_schedule_redraw(entry->widget); } break; case XKB_KEY_Right: new_char = utf8_next_char(entry->text + entry->cursor); if (new_char != NULL) { entry->cursor = new_char - entry->text; entry->anchor = entry->cursor; widget_schedule_redraw(entry->widget); } break; default: if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) break; text_entry_insert_at_cursor(entry, text); break; } widget_schedule_redraw(entry->widget); }