-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0001-Add-support-for-fast-select-in-listbox-by-key.patch
87 lines (79 loc) · 2.54 KB
/
0001-Add-support-for-fast-select-in-listbox-by-key.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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/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 8b2236eff..0d2fbc7df 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);
--
2.29.2