Ticket #4161: 0001-Add-support-for-fast-select-in-listbox-by-key.patch

File 0001-Add-support-for-fast-select-in-listbox-by-key.patch, 2.5 KB (added by jedi7, 3 years ago)

patch

  • lib/widget/listbox.c

    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 
    339339listbox_key (WListbox * l, int key) 
    340340{ 
    341341    long command; 
     342    char text[2] = {0}; 
     343    int idx = 0; 
    342344 
    343345    if (l->list == NULL) 
    344346        return MSG_NOT_HANDLED; 
    listbox_key (WListbox * l, int key) 
    348350    { 
    349351        listbox_select_entry (l, key - '0'); 
    350352        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; 
    351358    } 
    352359 
     360 
    353361    command = widget_lookup_key (WIDGET (l), key); 
    354362    if (command == CK_IgnoreKey) 
    355363        return MSG_NOT_HANDLED; 
    listbox_search_text (WListbox * l, const char *text) 
    594602    return (-1); 
    595603} 
    596604 
     605/** 
     606 * Finds item by its label. 
     607 */ 
     608int 
     609listbox_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 
    597631/* --------------------------------------------------------------------------------------------- */ 
    598632 
    599633/** 
  • lib/widget/listbox.h

    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; 
    6262 
    6363WListbox *listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback); 
    6464int listbox_search_text (WListbox * l, const char *text); 
     65int listbox_search_first_text (WListbox * l, const char *text); 
    6566int listbox_search_data (WListbox * l, const void *data); 
    6667void listbox_select_first (WListbox * l); 
    6768void listbox_select_last (WListbox * l);