From 401cd4a16c7dedc7c84dc41fb2e9eb1c11bd5e18 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sat, 20 Apr 2024 20:18:03 +0300 Subject: [PATCH] (listbox_add_item_take): new WListbox API. Add new item to a listbox taking ownerhip of item text and avoid a string duplication. (listbox_add_item): reimplement using listbox_add_item_take(). Signed-off-by: Andrew Borodin --- lib/widget/listbox.c | 37 ++++++++++++++++++++++++++++++++++++- lib/widget/listbox.h | 2 ++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/widget/listbox.c b/lib/widget/listbox.c index 76f07ad68..c20bd1865 100644 --- a/lib/widget/listbox.c +++ b/lib/widget/listbox.c @@ -806,9 +806,44 @@ listbox_remove_list (WListbox * l) /* --------------------------------------------------------------------------------------------- */ +/** + * Add new intem to the listbox. + * + * @param l WListbox object + * @param pos position of the item + * @param hotkey position of the item + * @param text item text. @l takes the copy of @text. + * @param data item data + * @param free_data if TRUE free the @data when @l is destroyed, + * + * @returns pointer to copy of @text. + */ char * listbox_add_item (WListbox * l, listbox_append_t pos, int hotkey, const char *text, void *data, gboolean free_data) +{ + return listbox_add_item_take (l, pos, hotkey, g_strdup (text), data, free_data); +} + +/* --------------------------------------------------------------------------------------------- */ + +/** + * Add new intem to the listbox. + * + * @param l WListbox object + * @param pos position of the item + * @param hotkey position of the item + * @param text item text. Ownership of the text is transferred to the @l. + * @param data item data + * @param free_data if TRUE free the @data when @l is destroyed, + * + * After this call, @text belongs to the @l and may no longer be modified by the caller. + * + * @returns pointer to @text. + */ +char * +listbox_add_item_take (WListbox * l, listbox_append_t pos, int hotkey, char *text, void *data, + gboolean free_data) { WLEntry *entry; @@ -819,7 +854,7 @@ listbox_add_item (WListbox * l, listbox_append_t pos, int hotkey, const char *te return NULL; entry = g_new (WLEntry, 1); - entry->text = g_strdup (text); + entry->text = text; entry->data = data; entry->free_data = free_data; entry->hotkey = hotkey; diff --git a/lib/widget/listbox.h b/lib/widget/listbox.h index 0a62dfdd0..37ef748ef 100644 --- a/lib/widget/listbox.h +++ b/lib/widget/listbox.h @@ -76,6 +76,8 @@ void listbox_set_list (WListbox * l, GQueue * list); void listbox_remove_list (WListbox * l); char *listbox_add_item (WListbox * l, listbox_append_t pos, int hotkey, const char *text, void *data, gboolean free_data); +char *listbox_add_item_take (WListbox * l, listbox_append_t pos, int hotkey, char *text, + void *data, gboolean free_data); /*** inline functions ****************************************************************************/