From 11c1aff4d343096ef555b7fcec05a625f2e020c2 Mon Sep 17 00:00:00 2001 From: Mooffie Date: Wed, 25 Jan 2017 14:07:18 +0200 Subject: [PATCH] Ticket #3763: introduce run_listbox_with_data(). Signed-off-by: Mooffie --- lib/widget/listbox-window.c | 35 +++++++++++++++++++++++++++++++++++ lib/widget/listbox-window.h | 1 + lib/widget/listbox.c | 28 ++++++++++++++++++++++++++++ lib/widget/listbox.h | 1 + 4 files changed, 65 insertions(+) diff --git a/lib/widget/listbox-window.c b/lib/widget/listbox-window.c index 23f690c91..bbf1dc5fa 100644 --- a/lib/widget/listbox-window.c +++ b/lib/widget/listbox-window.c @@ -138,3 +138,38 @@ run_listbox (Listbox * l) } /* --------------------------------------------------------------------------------------------- */ + +/** + * A variant of run_listbox() which is more convenient to use when we + * need to select arbitrary 'data'. + * + * @param select the item to select initially, by its 'data'. Optional. + * @return the 'data' of the item selected, or NULL if none selected. + */ +void * +run_listbox_with_data (Listbox * l, const void *select) +{ + void *val = NULL; + + if (select != NULL) + listbox_select_entry (l->list, listbox_search_data (l->list, select)); + + if (dlg_run (l->dlg) != B_CANCEL) + { + WLEntry *e; + e = listbox_get_nth_item (l->list, l->list->pos); + if (e != NULL) + { + /* The assert guards against returning a soon-to-be deallocated + * pointer (as in listbox_add_item(..., TRUE)). */ + g_assert (!e->free_data); + val = e->data; + } + } + + dlg_destroy (l->dlg); + g_free (l); + return val; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/widget/listbox-window.h b/lib/widget/listbox-window.h index f1a3ece89..5a6082956 100644 --- a/lib/widget/listbox-window.h +++ b/lib/widget/listbox-window.h @@ -29,6 +29,7 @@ Listbox *create_listbox_window_centered (int center_y, int center_x, int lines, const char *title, const char *help); Listbox *create_listbox_window (int lines, int cols, const char *title, const char *help); int run_listbox (Listbox * l); +void *run_listbox_with_data (Listbox * l, const void *select); /*** inline functions ****************************************************************************/ diff --git a/lib/widget/listbox.c b/lib/widget/listbox.c index b9f294b91..378a3af8a 100644 --- a/lib/widget/listbox.c +++ b/lib/widget/listbox.c @@ -570,6 +570,9 @@ listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn /* --------------------------------------------------------------------------------------------- */ +/** + * Finds item by its label. + */ int listbox_search_text (WListbox * l, const char *text) { @@ -592,6 +595,31 @@ listbox_search_text (WListbox * l, const char *text) /* --------------------------------------------------------------------------------------------- */ +/** + * Finds item by its 'data' slot. + */ +int +listbox_search_data (WListbox * l, const void *data) +{ + 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); + + if (e->data == data) + return i; + } + } + + return (-1); +} + +/* --------------------------------------------------------------------------------------------- */ + /* Selects the first entry and scrolls the list to the top */ void listbox_select_first (WListbox * l) diff --git a/lib/widget/listbox.h b/lib/widget/listbox.h index 6bb5269cf..396678736 100644 --- a/lib/widget/listbox.h +++ b/lib/widget/listbox.h @@ -64,6 +64,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_data (WListbox * l, const void *data); void listbox_select_first (WListbox * l); void listbox_select_last (WListbox * l); void listbox_select_entry (WListbox * l, int dest);