From 7587dfa17cdc643881a3048a0935abe2f31d2936 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Wed, 4 Sep 2024 08:51:41 +0200 Subject: [PATCH] jlist: add user data pointer This allows the info and paint functions to access user data associated with the list without going through globals. --- include/justui/jlist.h | 7 +++++-- src/jlist.c | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/justui/jlist.h b/include/justui/jlist.h index cc395d7..27ffedd 100644 --- a/include/justui/jlist.h +++ b/include/justui/jlist.h @@ -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); diff --git a/src/jlist.c b/src/jlist.c index 93b65b8..c55705f 100644 --- a/src/jlist.c +++ b/src/jlist.c @@ -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)