static void text_input_leave(void *data, struct wl_text_input *text_input) { struct text_entry *entry = data; text_entry_commit_and_reset(entry); entry->active = 0; wl_text_input_hide_input_panel(text_input); widget_schedule_redraw(entry->widget); }
static void text_entry_set_cursor_position(struct text_entry *entry, int32_t x, int32_t y) { int index, trailing; const char *text; text_entry_commit_and_reset(entry); pango_layout_xy_to_index(entry->layout, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); text = pango_layout_get_text(entry->layout); entry->cursor = g_utf8_offset_to_pointer(text + index, trailing) - text; text_entry_update_layout(entry); widget_schedule_redraw(entry->widget); text_entry_update(entry); }
static void text_entry_set_cursor_position(struct text_entry *entry, int32_t x, int32_t y, bool move_anchor) { int index, trailing; const char *text; uint32_t cursor; pango_layout_xy_to_index(entry->layout, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); text = pango_layout_get_text(entry->layout); cursor = g_utf8_offset_to_pointer(text + index, trailing) - text; if (move_anchor) entry->anchor = cursor; if (text_entry_has_preedit(entry)) { text_entry_commit_and_reset(entry); assert(!text_entry_has_preedit(entry)); } if (entry->cursor == cursor) return; entry->cursor = cursor; text_entry_update_layout(entry); widget_schedule_redraw(entry->widget); text_entry_update(entry); }
static void text_input_keysym(void *data, struct wl_text_input *text_input, uint32_t serial, uint32_t time, uint32_t key, uint32_t state, uint32_t modifiers) { struct text_entry *entry = data; const char *state_label = "release"; const char *key_label = "Unknown"; const char *new_char; if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { state_label = "pressed"; } if (key == XKB_KEY_Left || key == XKB_KEY_Right) { if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return; if (key == XKB_KEY_Left) new_char = utf8_prev_char(entry->text, entry->text + entry->cursor); else new_char = utf8_next_char(entry->text + entry->cursor); if (new_char != NULL) { entry->cursor = new_char - entry->text; } if (!(modifiers & entry->keysym.shift_mask)) entry->anchor = entry->cursor; widget_schedule_redraw(entry->widget); return; } if (key == XKB_KEY_BackSpace) { const char *start, *end; if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return; text_entry_commit_and_reset(entry); start = utf8_prev_char(entry->text, entry->text + entry->cursor); end = utf8_next_char(start); if (start == NULL) return; text_entry_delete_text(entry, start - entry->text, end - start); return; } switch (key) { case XKB_KEY_Tab: key_label = "Tab"; break; case XKB_KEY_KP_Enter: case XKB_KEY_Return: key_label = "Enter"; break; } fprintf(stderr, "%s key was %s.\n", key_label, state_label); }
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 *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: text_entry_commit_and_reset(entry); new_char = utf8_prev_char(entry->text, entry->text + entry->cursor); if (new_char != NULL) text_entry_delete_text(entry, new_char - entry->text, (entry->text + entry->cursor) - new_char); break; case XKB_KEY_Delete: text_entry_commit_and_reset(entry); new_char = utf8_next_char(entry->text + entry->cursor); if (new_char != NULL) text_entry_delete_text(entry, entry->cursor, new_char - (entry->text + entry->cursor)); break; case XKB_KEY_Left: text_entry_commit_and_reset(entry); 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: text_entry_commit_and_reset(entry); 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_commit_and_reset(entry); text_entry_insert_at_cursor(entry, text, 0, 0); break; } widget_schedule_redraw(entry->widget); }