From 199d03d8f6909fabd7402ea9197acb98fb472d73 Mon Sep 17 00:00:00 2001
From: "Ing. Jaroslav Safka" <devel@safka.org>
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 e20c1a82d..bb25256af 100644
a
|
b
|
static cb_ret_t |
339 | 339 | listbox_key (WListbox * l, int key) |
340 | 340 | { |
341 | 341 | long command; |
| 342 | char text[2] = {0}; |
| 343 | int idx = 0; |
342 | 344 | |
343 | 345 | if (l->list == NULL) |
344 | 346 | return MSG_NOT_HANDLED; |
… |
… |
listbox_key (WListbox * l, int key) |
348 | 350 | { |
349 | 351 | listbox_select_entry (l, key - '0'); |
350 | 352 | return MSG_HANDLED; |
| 353 | } else if(key >= 'a' && key <= 'z') { |
| 354 | text[0] = key; |
| 355 | idx = listbox_search_first_text(l, text); |
| 356 | listbox_select_entry(l, idx); |
| 357 | return MSG_HANDLED; |
351 | 358 | } |
352 | 359 | |
| 360 | |
353 | 361 | command = widget_lookup_key (WIDGET (l), key); |
354 | 362 | if (command == CK_IgnoreKey) |
355 | 363 | return MSG_NOT_HANDLED; |
… |
… |
listbox_search_text (WListbox * l, const char *text) |
594 | 602 | return (-1); |
595 | 603 | } |
596 | 604 | |
| 605 | /** |
| 606 | * Finds item by its label. |
| 607 | */ |
| 608 | int |
| 609 | listbox_search_first_text (WListbox * l, const char *text) |
| 610 | { |
| 611 | size_t text_len = strlen(text); |
| 612 | |
| 613 | if (!listbox_is_empty (l)) |
| 614 | { |
| 615 | int i; |
| 616 | GList *le; |
| 617 | |
| 618 | for (i = 0, le = g_queue_peek_head_link (l->list); le != NULL; i++, le = g_list_next (le)) |
| 619 | { |
| 620 | WLEntry *e = LENTRY (le->data); |
| 621 | |
| 622 | // limit compare size, to avoid check for full string |
| 623 | if (strncmp (e->text, text, text_len) == 0) |
| 624 | return i; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | return (-1); |
| 629 | } |
| 630 | |
597 | 631 | /* --------------------------------------------------------------------------------------------- */ |
598 | 632 | |
599 | 633 | /** |
diff --git a/lib/widget/listbox.h b/lib/widget/listbox.h
index 8b2236eff..0d2fbc7df 100644
a
|
b
|
extern const global_keymap_t *listbox_map; |
62 | 62 | |
63 | 63 | WListbox *listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback); |
64 | 64 | int listbox_search_text (WListbox * l, const char *text); |
| 65 | int listbox_search_first_text (WListbox * l, const char *text); |
65 | 66 | int listbox_search_data (WListbox * l, const void *data); |
66 | 67 | void listbox_select_first (WListbox * l); |
67 | 68 | void listbox_select_last (WListbox * l); |