Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fast select in listbox by key #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/widget/listbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

/* --------------------------------------------------------------------------------------------- */

/**
Expand Down
1 change: 1 addition & 0 deletions lib/widget/listbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down