ui/common/types.c

changeset 108
77254bd6dccb
parent 103
6606616eca9f
child 109
c3dfcb8f0be7
--- a/ui/common/types.c	Sat Apr 05 17:57:04 2025 +0200
+++ b/ui/common/types.c	Sun Jul 20 22:04:39 2025 +0200
@@ -36,8 +36,10 @@
 #include "../ui/tree.h"
 #include "types.h"
 #include "context.h"
+#include "../ui/image.h"
 
-
+static ui_list_init_func default_list_init;
+static void *default_list_init_userdata;
 
 UiObserver* ui_observer_new(ui_callback f, void *data) {
     UiObserver *observer = malloc(sizeof(UiObserver));
@@ -75,6 +77,7 @@
     evt.window = NULL;
     evt.document = NULL;
     evt.eventdata = data;
+    evt.eventdatatype = UI_EVENT_DATA_POINTER;
     evt.intval = 0;
     
     while(observer) {
@@ -94,20 +97,22 @@
 
 /* --------------------------- UiList --------------------------- */
 
-UiList* ui_list_new(UiContext *ctx, char *name) {
-    UiList *list = malloc(sizeof(UiList));
+void uic_ucx_list_init(UiContext *ctx, UiList *list, void *unused) {
+    list->data = cxArrayListCreate(ctx->mp->allocator, NULL, CX_STORE_POINTERS, 32);
     list->first = ui_list_first;
     list->next = ui_list_next;
     list->get = ui_list_get;
     list->count = ui_list_count;
-    list->observers = NULL;
-    
-    list->data = cxArrayListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS, 32);
-    list->iter = NULL;
-    
-    list->update = NULL;
-    list->getselection = NULL;
-    list->obj = NULL;
+}
+
+UiList* ui_list_new(UiContext *ctx, const char *name) {
+    return ui_list_new2(ctx, name, default_list_init ? default_list_init : uic_ucx_list_init, default_list_init_userdata);
+}
+
+UiList* ui_list_new2(UiContext *ctx, const char *name, ui_list_init_func listinit, void *userdata) {
+    UiList *list = cxMalloc(ctx->mp->allocator, sizeof(UiList));
+    memset(list, 0, sizeof(UiList));
+    listinit(ctx, list, userdata);
     
     if(name) {
         uic_reg_var(ctx, name, UI_VAR_LIST, list);
@@ -248,7 +253,7 @@
 // types
 
 // public functions
-UiInteger* ui_int_new(UiContext *ctx, char *name) {
+UiInteger* ui_int_new(UiContext *ctx, const char *name) {
     UiInteger *i = ui_malloc(ctx, sizeof(UiInteger));
     memset(i, 0, sizeof(UiInteger));
     if(name) {
@@ -257,7 +262,7 @@
     return i;
 }
 
-UiDouble* ui_double_new(UiContext *ctx, char *name) {
+UiDouble* ui_double_new(UiContext *ctx, const char *name) {
     UiDouble *d = ui_malloc(ctx, sizeof(UiDouble));
     memset(d, 0, sizeof(UiDouble));
     if(name) {
@@ -266,7 +271,7 @@
     return d;
 }
 
-UiString* ui_string_new(UiContext *ctx, char *name) {
+UiString* ui_string_new(UiContext *ctx, const char *name) {
     UiString *s = ui_malloc(ctx, sizeof(UiString));
     memset(s, 0, sizeof(UiString));
     if(name) {
@@ -275,7 +280,7 @@
     return s;
 }
 
-UiText* ui_text_new(UiContext *ctx, char *name) {
+UiText* ui_text_new(UiContext *ctx, const char *name) {
     UiText *t = ui_malloc(ctx, sizeof(UiText));
     memset(t, 0, sizeof(UiText));
     if(name) {
@@ -284,7 +289,7 @@
     return t;
 }
 
-UiRange* ui_range_new(UiContext *ctx, char *name) {
+UiRange* ui_range_new(UiContext *ctx, const char *name) {
     UiRange *r = ui_malloc(ctx, sizeof(UiRange));
     memset(r, 0, sizeof(UiRange));
     if(name) {
@@ -293,7 +298,7 @@
     return r;
 }
 
-UIEXPORT UiGeneric* ui_generic_new(UiContext *ctx, char *name) {
+UIEXPORT UiGeneric* ui_generic_new(UiContext *ctx, const char *name) {
     UiGeneric *g = ui_malloc(ctx, sizeof(UiGeneric));
     memset(g, 0, sizeof(UiGeneric));
     if(name) {
@@ -404,6 +409,30 @@
     }
 }
 
+void ui_generic_set_image(UiGeneric *g, void *img) {
+    if(g->set) {
+        g->set(g, img, UI_IMAGE_OBJECT_TYPE);
+    } else {
+        if(g->value) {
+            ui_image_unref(g->value);
+        }
+        ui_image_ref(img);
+        g->value = img;
+        g->type = UI_IMAGE_OBJECT_TYPE;
+    }
+}
+
+void* ui_generic_get_image(UiGeneric *g) {
+    if(g->type) {
+        if(!strcmp(g->type, UI_IMAGE_OBJECT_TYPE)) {
+            return g->value;
+        } else {
+            return NULL;
+        }
+    }
+    return g->value;
+}
+
 
 // private functions
 void uic_int_copy(UiInteger *from, UiInteger *to) {
@@ -452,6 +481,8 @@
 
 void uic_list_copy(UiList *from, UiList *to) {
     to->update = from->update;
+    to->getselection = from->getselection;
+    to->setselection = from->setselection;
     to->obj = from->obj;
 }
 
@@ -634,3 +665,34 @@
 int ui_get_setop(void) {
     return ui_set_op;
 }
+
+/* ---------------- List initializers and wrapper functions ---------------- */
+
+void ui_global_list_initializer(ui_list_init_func func, void *userdata) {
+    default_list_init = func;
+    default_list_init_userdata = userdata;
+}
+
+void ui_list_class_set_first(UiList *list, void*(*first)(UiList *list)) {
+    list->first = first;
+}
+
+void ui_list_class_set_next(UiList *list, void*(*next)(UiList *list)) {
+    list->next = next;
+}
+
+void ui_list_class_set_get(UiList *list, void*(*get)(UiList *list, int i)) {
+    list->get = get;
+}
+
+void ui_list_class_set_count(UiList *list, int(*count)(UiList *list)) {
+    list->count = count;
+}
+
+void ui_list_class_set_data(UiList *list, void *data) {
+    list->data = data;
+}
+
+void ui_list_class_set_iter(UiList *list, void *iter) {
+    list->iter = iter;
+}

mercurial