add functions for storing custom data in an UiObject

Sun, 23 Nov 2025 11:16:13 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 23 Nov 2025 11:16:13 +0100
changeset 932
28fc967f74ef
parent 931
6ca1ef6c8107
child 933
70e14fa98ab4

add functions for storing custom data in an UiObject

ui/common/object.c file | annotate | diff | comparison | revisions
ui/common/object.h file | annotate | diff | comparison | revisions
ui/motif/toolkit.c file | annotate | diff | comparison | revisions
ui/motif/window.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/ui/common/object.c	Sun Nov 23 10:49:24 2025 +0100
+++ b/ui/common/object.c	Sun Nov 23 11:16:13 2025 +0100
@@ -32,6 +32,7 @@
 #include "context.h"
 
 #include <cx/linked_list.h>
+#include <cx/hash_map.h>
 
 #include "../ui/container.h"
 
@@ -108,7 +109,7 @@
 UiObject* uic_object_new_toplevel(void) {
     fflush(stdout);
     CxMempool *mp = cxMempoolCreateSimple(256);
-    UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject));
+    UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObjectPrivate));
     fflush(stdout);
     obj->ctx = uic_context(obj, mp);
     obj->ctx->parent = ui_global_context();
@@ -119,7 +120,7 @@
 }
 
 UiObject* uic_ctx_object_new(UiContext *ctx, UIWIDGET widget) {
-    UiObject *newobj = cxCalloc(ctx->allocator, 1, sizeof(UiObject));
+    UiObject *newobj = cxCalloc(ctx->allocator, 1, sizeof(UiObjectPrivate));
     newobj->ctx = ctx;
     newobj->widget = widget;
     uic_object_created(newobj);
@@ -170,3 +171,24 @@
         fprintf(stderr, "Error: uic_object_remove_second_last_container expected at least 2 containers\n");
     }
 }
+
+// public API
+
+void ui_object_set(UiObject *obj, const char *key, void *data) {
+    UiObjectPrivate *p = (UiObjectPrivate*)obj;
+    if(!p->ext) {
+        p->ext = cxHashMapCreate(obj->ctx->mp->allocator, CX_STORE_POINTERS, 4);
+    }
+    if(data) {
+        cxMapPut(p->ext, key, data);
+    } else {
+        cxMapRemove(p->ext, key);
+    }
+}
+
+void* ui_object_get(UiObject *obj, const char *key) {
+    UiObjectPrivate *p = (UiObjectPrivate*)obj;
+    if(p->ext) {
+        return cxMapGet(p->ext, key);
+    }
+}
--- a/ui/common/object.h	Sun Nov 23 10:49:24 2025 +0100
+++ b/ui/common/object.h	Sun Nov 23 11:16:13 2025 +0100
@@ -30,10 +30,16 @@
 #define	UIC_OBJECT_H
 
 #include "../ui/toolkit.h"
+#include <cx/map.h>
 
 #ifdef	__cplusplus
 extern "C" {
 #endif
+    
+typedef struct UiObjectPrivate {
+    UiObject obj;
+    CxMap *ext;
+} UiObjectPrivate;
 
 typedef void (*ui_object_callback)(UiObject *obj, void *userdata);
 
--- a/ui/motif/toolkit.c	Sun Nov 23 10:49:24 2025 +0100
+++ b/ui/motif/toolkit.c	Sun Nov 23 11:16:13 2025 +0100
@@ -202,7 +202,7 @@
     }
 }
 
-void ui_set_visible(UIWIDGET widget, UiBool visible) {
+void ui_set_visible(UIWIDGET widget, int visible) {
     if(visible) {
         XtManageChild(widget);
     } else {
--- a/ui/motif/window.h	Sun Nov 23 10:49:24 2025 +0100
+++ b/ui/motif/window.h	Sun Nov 23 11:16:13 2025 +0100
@@ -30,6 +30,7 @@
 #define WINDOW_H
 
 #include "../ui/window.h"
+#include "../ui/widget.h"
 
 #ifdef __cplusplus
 extern "C" {
--- a/ui/ui/toolkit.h	Sun Nov 23 10:49:24 2025 +0100
+++ b/ui/ui/toolkit.h	Sun Nov 23 11:16:13 2025 +0100
@@ -562,8 +562,6 @@
 UIEXPORT void ui_attach_document(UiContext *ctx, void *document);
 UIEXPORT void ui_detach_document(UiContext *ctx, void *document);
 
-UIEXPORT void ui_set_visible(UIWIDGET widget, UiBool visible);
-
 UIEXPORT void ui_widget_set_groups(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...);
 UIEXPORT void ui_widget_set_groups2(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, const int *groups, int ngroups);
 UIEXPORT void ui_widget_set_visibility_states(UiContext *ctx, UIWIDGET widget, const int *states, int nstates);
@@ -697,6 +695,9 @@
 UIEXPORT void ui_list_class_set_data(UiList *list, void *data);
 UIEXPORT void ui_list_class_set_iter(UiList *list, void *iter);
 
+UIEXPORT void ui_object_set(UiObject *obj, const char *key, void *data);
+UIEXPORT void* ui_object_get(UiObject *obj, const char *key);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial