From 199d03d8f6909fabd7402ea9197acb98fb472d73 Mon Sep 17 00:00:00 2001 From: "Ing. Jaroslav Safka" Date: Tue, 22 Dec 2020 20:11:03 +0100 Subject: [PATCH] Add support for fast select in listbox by key * items in listbox are possible to select by pressing key of first item character --- lib/widget/listbox.c | 34 ++++++++++++++++++++++++++++++++++ lib/widget/listbox.h | 1 + 2 files changed, 35 insertions(+) diff --git a/lib/widget/listbox.c b/lib/widget/listbox.c index e20c1a82d0..bb25256afe 100644 --- a/lib/widget/listbox.c +++ b/lib/widget/listbox.c @@ -339,6 +339,8 @@ static cb_ret_t listbox_key (WListbox * l, int key) { long command; + char text[2] = {0}; + int idx = 0; if (l->list == NULL) return MSG_NOT_HANDLED; @@ -348,8 +350,14 @@ listbox_key (WListbox * l, int key) { listbox_select_entry (l, key - '0'); return MSG_HANDLED; + } else if(key >= 'a' && key <= 'z') { + text[0] = key; + idx = listbox_search_first_text(l, text); + listbox_select_entry(l, idx); + return MSG_HANDLED; } + command = widget_lookup_key (WIDGET (l), key); if (command == CK_IgnoreKey) return MSG_NOT_HANDLED; @@ -594,6 +602,32 @@ listbox_search_text (WListbox * l, const char *text) return (-1); } +/** + * Finds item by its label. + */ +int +listbox_search_first_text (WListbox * l, const char *text) +{ + size_t text_len = strlen(text); + + if (!listbox_is_empty (l)) + { + int i; + GList *le; + + for (i = 0, le = g_queue_peek_head_link (l->list); le != NULL; i++, le = g_list_next (le)) + { + WLEntry *e = LENTRY (le->data); + + // limit compare size, to avoid check for full string + if (strncmp (e->text, text, text_len) == 0) + return i; + } + } + + return (-1); +} + /* --------------------------------------------------------------------------------------------- */ /** diff --git a/lib/widget/listbox.h b/lib/widget/listbox.h index 8b2236eff2..0d2fbc7dfc 100644 --- a/lib/widget/listbox.h +++ b/lib/widget/listbox.h @@ -62,6 +62,7 @@ extern const global_keymap_t *listbox_map; WListbox *listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback); int listbox_search_text (WListbox * l, const char *text); +int listbox_search_first_text (WListbox * l, const char *text); int listbox_search_data (WListbox * l, const void *data); void listbox_select_first (WListbox * l); void listbox_select_last (WListbox * l);