add ui_call_action/ui_broadcast_action default tip

Sun, 17 May 2026 10:46:35 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 17 May 2026 10:46:35 +0200
changeset 1127
d3a8c29ebdaa
parent 1126
bd1fe6134c1a

add ui_call_action/ui_broadcast_action

ui/common/action.c file | annotate | diff | comparison | revisions
ui/common/object.c file | annotate | diff | comparison | revisions
ui/common/object.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/ui/common/action.c	Wed May 13 11:57:43 2026 +0200
+++ b/ui/common/action.c	Sun May 17 10:46:35 2026 +0200
@@ -28,6 +28,7 @@
 
 #include "action.h"
 #include "context.h"
+#include "object.h"
 
 #include <cx/string.h>
 
@@ -141,3 +142,62 @@
         }
     }
 }
+
+void ui_call_action(UiContext *ctx, const char *action_name) {
+    ui_call_action2(ctx, action_name, NULL, 0);
+}
+
+void ui_call_action2(UiContext *ctx, const char *action_name, void *eventdata, int intval) {
+    UiAction *action = uic_resolve_action(ctx, action_name);
+    if(action && action->callback) {
+        UiEvent event;
+        memset(&event, 0, sizeof(UiEvent));
+        event.obj = ctx->obj;
+        event.window = event.obj ? event.obj->window : NULL;
+        event.document = ctx->self_doc ? ctx->self_doc : ctx->document;
+        if(eventdata) {
+            event.eventdata = eventdata;
+            event.eventdatatype = UI_EVENT_DATA_POINTER;
+        }
+        event.intval = intval;
+        action->callback(&event, action->userdata);
+    }
+}
+
+void ui_broadcast_action(const char *action_name) {
+    ui_broadcast_action2(action_name, NULL, 0);
+}
+
+void ui_broadcast_action2(const char *action_name, void *eventdata, int intval) {
+    CxList *objects = uic_object_list();
+    CxIterator i = cxListIterator(objects);
+    cx_foreach(UiObject*, obj, i) {
+        ui_call_action2(obj->ctx, action_name, eventdata, intval);
+    }
+}
+
+typedef struct UiActionBroadcast {
+    char *action;
+    void *eventdata;
+    int intval;
+} UiActionBroadcast;
+
+void ui_mainthread_broadcast(const char *action_name) {
+    ui_mainthread_broadcast2(action_name, NULL, 0);
+}
+
+static int mainthread_action_broadcast(void *data) {
+    UiActionBroadcast *broadcast = data;
+    ui_broadcast_action2(broadcast->action, broadcast->eventdata, broadcast->intval);
+    free(broadcast->action);
+    free(broadcast);
+    return 0;
+}
+
+void ui_mainthread_broadcast2(const char *action_name, void *eventdata, int intval) {
+    UiActionBroadcast *broadcast = malloc(sizeof(UiActionBroadcast));
+    broadcast->action = strdup(action_name);
+    broadcast->eventdata = eventdata;
+    broadcast->intval = intval;
+    ui_call_mainthread(mainthread_action_broadcast, broadcast);
+}
--- a/ui/common/object.c	Wed May 13 11:57:43 2026 +0200
+++ b/ui/common/object.c	Sun May 17 10:46:35 2026 +0200
@@ -39,6 +39,8 @@
 static CxList *creation_callbacks;
 static CxList *destruction_callbacks;
 
+static CxList *objects;
+
 typedef struct objcallback {
     ui_object_callback func;
     void *userdata;
@@ -67,6 +69,11 @@
 }
 
 void uic_object_created(UiObject *obj) {
+    if(!objects) {
+        objects = cxLinkedListCreate(NULL, CX_STORE_POINTERS);
+    }
+    cxListAdd(objects, obj);
+    
     CxIterator i = cxListIterator(creation_callbacks);
     cx_foreach(objcallback *, cb, i) {
         cb->func(obj, cb->userdata);
@@ -74,12 +81,20 @@
 }
 
 void uic_object_destroyed(UiObject *obj) {
+    if(objects) {
+        cxListFindRemove(objects, obj);
+    }
+    
     CxIterator i = cxListIterator(destruction_callbacks);
     cx_foreach(objcallback *, cb, i) {
         cb->func(obj, cb->userdata);
     }
 }
 
+CxList* uic_object_list(void) {
+    return objects ? objects : cxEmptyList;
+}
+
 void ui_object_ref(UiObject *obj) {
     obj->ref++;
 }
--- a/ui/common/object.h	Wed May 13 11:57:43 2026 +0200
+++ b/ui/common/object.h	Sun May 17 10:46:35 2026 +0200
@@ -50,6 +50,8 @@
 void uic_object_destroyed(UiObject *obj);
     
 void uic_object_destroy(UiObject *obj);
+
+CxList* uic_object_list(void);
     
 UiObject* uic_object_new_toplevel(void);
 UiObject* uic_object_new(UiObject *toplevel, UIWIDGET widget);
--- a/ui/ui/toolkit.h	Wed May 13 11:57:43 2026 +0200
+++ b/ui/ui/toolkit.h	Sun May 17 10:46:35 2026 +0200
@@ -575,6 +575,12 @@
         const char *accelerator,
         const char *accelerator_text);
 UIEXPORT void ui_update_action_bindings(UiContext *ctx);
+UIEXPORT void ui_call_action(UiContext *ctx, const char *action_name);
+UIEXPORT void ui_call_action2(UiContext *ctx, const char *action_name, void *eventdata, int intval);
+UIEXPORT void ui_broadcast_action(const char *action_name);
+UIEXPORT void ui_broadcast_action2(const char *action_name, void *eventdata, int intval);
+UIEXPORT void ui_mainthread_broadcast(const char *action_name);
+UIEXPORT void ui_mainthread_broadcast2(const char *action_name, void *eventdata, int intval);
 
 UIEXPORT void ui_set_state(UiContext *ctx, int state);
 UIEXPORT void ui_unset_state(UiContext *ctx, int state);

mercurial