editor: Add support for editing text using the keyboard
This simple change allows you to drive the editor using the keyboard
(supporting backspace and delete and left and right arrow keys.) The idea
behind this change is to allow the testing of the interoperation between a
virtual keyboard and real one.
Signed-off-by: Rob Bradford <rob@linux.intel.com>
Signed-off-by: Jan Arne Petersen <jpetersen@openismus.com>
diff --git a/clients/editor.c b/clients/editor.c
index abb8d03..b42b5a1 100644
--- a/clients/editor.c
+++ b/clients/editor.c
@@ -67,6 +67,7 @@
struct widget *widget;
struct text_entry *entry;
struct text_entry *editor;
+ struct text_entry *active_entry;
};
static const char *
@@ -876,11 +877,14 @@
{
struct text_entry *entry = data;
struct rectangle allocation;
+ struct editor *editor;
int32_t x, y;
widget_get_allocation(entry->widget, &allocation);
input_get_position(input, &x, &y);
+ editor = window_get_user_data(entry->window);
+
if (button != BTN_LEFT) {
return;
}
@@ -893,6 +897,7 @@
struct wl_seat *seat = input_get_seat(input);
text_entry_activate(entry, seat);
+ editor->active_entry = entry;
text_entry_set_anchor_position(entry,
x - allocation.x - text_offset_left,
@@ -921,10 +926,84 @@
text_entry_deactivate(editor->entry, seat);
text_entry_deactivate(editor->editor, seat);
+ editor->active_entry = NULL;
}
}
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);
+}
+
+static void
global_handler(struct display *display, uint32_t name,
const char *interface, uint32_t version, void *data)
{
@@ -960,6 +1039,8 @@
editor.editor = text_entry_create(&editor, "Editor");
window_set_title(editor.window, "Text Editor");
+ window_set_key_handler(editor.window, key_handler);
+ window_set_user_data(editor.window, &editor);
widget_set_redraw_handler(editor.widget, redraw_handler);
widget_set_resize_handler(editor.widget, resize_handler);