jlist: add user data pointer

This allows the info and paint functions to access user data associated
with the list without going through globals.
This commit is contained in:
Lephenixnoir 2024-09-04 08:51:41 +02:00
parent 12b29f8223
commit 7587dfa17c
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
2 changed files with 10 additions and 4 deletions

View file

@ -77,6 +77,8 @@ typedef struct jlist {
/* Currently selected item, -1 if none */
int cursor;
/* User data pointer */
void *user;
} jlist;
@ -91,8 +93,9 @@ jlist *jlist_create(jlist_item_info_function info_function,
/* jlist_update_model(): Update jlists's information about the model
The new model size is passed as parameter. The model is refreshed by
repeatedly calling the info function. */
void jlist_update_model(jlist *l, int item_count);
repeatedly calling the info function. The user pointer is also updated. To
keep it unchanged, pass `l->user` as third parameter. */
void jlist_update_model(jlist *l, int item_count, void *user);
/* jlist_clear(): Remove all items */
void jlist_clear(jlist *l);

View file

@ -37,6 +37,7 @@ jlist *jlist_create(jlist_item_info_function info_function,
l->info_function = info_function;
l->paint_function = paint_function;
l->cursor = -1;
l->user = NULL;
return l;
}
@ -113,8 +114,10 @@ int jlist_selected_item(jlist *l)
// Item management
//---
void jlist_update_model(jlist *l, int item_count)
void jlist_update_model(jlist *l, int item_count, void *user)
{
l->user = user;
if(l->item_count != item_count) {
l->items = realloc(l->items, item_count * sizeof *l->items);
if(!l->items) {
@ -138,7 +141,7 @@ void jlist_update_model(jlist *l, int item_count)
void jlist_clear(jlist *l)
{
jlist_update_model(l, 0);
jlist_update_model(l, 0, NULL);
}
jrect jlist_selected_region(jlist *l)