Ticket #3763: introduce run_listbox_with_data().

Signed-off-by: Mooffie <mooffie@gmail.com>
This commit is contained in:
Mooffie 2017-01-25 14:07:18 +02:00
parent a4f05e5123
commit 11c1aff4d3
4 changed files with 65 additions and 0 deletions

View File

@ -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;
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -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 ****************************************************************************/

View File

@ -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)

View File

@ -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);