Browse Source

(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 <aborodin@vmail.ru>
Andrew Borodin 10 months ago
parent
commit
401cd4a16c
2 changed files with 38 additions and 1 deletions
  1. 36 1
      lib/widget/listbox.c
  2. 2 0
      lib/widget/listbox.h

+ 36 - 1
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;

+ 2 - 0
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 ****************************************************************************/