prepare actions default tip

Sun, 01 Feb 2026 11:42:27 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 01 Feb 2026 11:42:27 +0100
changeset 1066
afe02792303d
parent 1065
cb3c1fcecaa0

prepare actions

ui/common/action.c file | annotate | diff | comparison | revisions
ui/common/action.h file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/context.h file | annotate | diff | comparison | revisions
ui/common/objs.mk file | annotate | diff | comparison | revisions
ui/ui/menu.h file | annotate | diff | comparison | revisions
ui/ui/toolbar.h file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/common/action.c	Sun Feb 01 11:42:27 2026 +0100
@@ -0,0 +1,73 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2026 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "action.h"
+#include "context.h"
+
+#include <cx/string.h>
+
+void uic_add_action(
+        UiContext *ctx,
+        const char *name,
+        ui_callback callback,
+        void *userdata,
+        const char *accelerator,
+        const char *accelerator_text)
+{
+    if(!name) {
+        return;
+    }
+    
+    UiAction action;
+    action.name = ui_strdup(ctx, name);
+    action.callback = callback;
+    action.userdata = userdata;
+    action.accelerator = accelerator ? ui_strdup(ctx, accelerator) : NULL;
+    action.accelerator_text = accelerator_text ? ui_strdup(ctx, accelerator_text) : NULL;
+    cxMapPut(ctx->actions, name, &action);
+    cxMapRehash(ctx->actions);
+}
+
+void uic_bind_action(
+        UiContext *ctx,
+        const char *action,
+        void *bind_obj,
+        ui_action_binding_set_enabled_func set_enabled,
+        ui_action_binding_set_accelerator_text_func set_accelerator_text)
+{
+    if(!action) {
+        return;
+    }
+    
+    UiActionBinding binding;
+    binding.action = ui_strdup(ctx, action);
+    binding.userdata = bind_obj;
+    binding.set_enabled = set_enabled;
+    binding.set_accelerator_text = set_accelerator_text;
+    cxListAdd(ctx->action_bindings, &binding);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/common/action.h	Sun Feb 01 11:42:27 2026 +0100
@@ -0,0 +1,78 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2026 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef UIC_ACTION_H
+#define UIC_ACTION_H
+
+#include "../ui/toolkit.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct UiAction        UiAction;
+typedef struct UiActionBinding UiActionBinding;
+
+struct UiAction {
+    char *name;
+    char *accelerator;
+    char *accelerator_text;
+    ui_callback callback;
+    void *userdata;
+};
+
+typedef void (*ui_action_binding_set_enabled_func)(void *bind_obj, UiBool enabled);
+typedef void (*ui_action_binding_set_accelerator_text_func)(void *bind_obj, const char *text);
+struct UiActionBinding {
+    const char *action;
+    ui_action_binding_set_enabled_func set_enabled;
+    ui_action_binding_set_accelerator_text_func set_accelerator_text;
+    void *userdata;
+};
+
+void uic_add_action(
+        UiContext *ctx,
+        const char *name,
+        ui_callback callback,
+        void *userdata,
+        const char *accelerator,
+        const char *accelerator_text);
+
+void uic_bind_action(
+        UiContext *ctx,
+        const char *action,
+        void *bind_obj,
+        ui_action_binding_set_enabled_func set_enabled,
+        ui_action_binding_set_accelerator_text_func set_accelerator_text);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* UIC_ACTION_H */
+
--- a/ui/common/context.c	Thu Jan 29 20:55:59 2026 +0100
+++ b/ui/common/context.c	Sun Feb 01 11:42:27 2026 +0100
@@ -68,6 +68,9 @@
     ctx->states = cxArrayListCreate(mp->allocator, sizeof(int), 32);
     cxSetCompareFunc(ctx->states, cx_cmp_int);
     
+    ctx->actions = cxHashMapCreate(ctx->allocator, sizeof(UiAction), 8);
+    ctx->action_bindings = cxArrayListCreate(ctx->allocator, sizeof(UiActionBinding), 0);
+    
     ctx->attach_document = uic_context_attach_document;
     ctx->detach_document2 = uic_context_detach_document;
     
--- a/ui/common/context.h	Thu Jan 29 20:55:59 2026 +0100
+++ b/ui/common/context.h	Sun Feb 01 11:42:27 2026 +0100
@@ -36,6 +36,8 @@
 #include <cx/list.h>
 #include <cx/linked_list.h>
 
+#include "action.h"
+
 #ifdef	__cplusplus
 extern "C" {
 #endif
@@ -72,6 +74,9 @@
     CxList        *states; // int list
     CxList        *state_widgets; // UiGroupWidget list
     
+    CxMap         *actions; // key: action name (string), value: UiAction
+    CxList        *action_bindings; // UiActionBinding list
+    
     void (*attach_document)(UiContext *ctx, void *document);
     void (*detach_document2)(UiContext *ctx, void *document); 
     
--- a/ui/common/objs.mk	Thu Jan 29 20:55:59 2026 +0100
+++ b/ui/common/objs.mk	Sun Feb 01 11:42:27 2026 +0100
@@ -32,6 +32,7 @@
 COMMON_OBJ = context$(OBJ_EXT)
 COMMON_OBJ += document$(OBJ_EXT)
 COMMON_OBJ += object$(OBJ_EXT)
+COMMON_OBJ = action$(OBJ_EXT)
 COMMON_OBJ += container$(OBJ_EXT)
 COMMON_OBJ += types$(OBJ_EXT)
 COMMON_OBJ += app$(OBJ_EXT)
--- a/ui/ui/menu.h	Thu Jan 29 20:55:59 2026 +0100
+++ b/ui/ui/menu.h	Sun Feb 01 11:42:27 2026 +0100
@@ -37,31 +37,38 @@
 
 
 typedef struct UiMenuItemArgs {
-	const char* label;
-	const char* icon;
+	const char *label;
+	const char *icon;
 
 	ui_callback onclick;
-	void* onclickdata;
+	void *onclickdata;
+        const char *action;
+        const char *accelerator;
+        const char *accelerator_text;
 
-	const int* states;
+	const int *states;
 } UiMenuItemArgs;
 
 typedef struct UiMenuToggleItemArgs {
-	const char* label;
-	const char* icon;
+	const char *label;
+	const char *icon;
 
-	const char* varname;
+	const char *varname;
 	ui_callback onchange;
-	void* onchangedata;
+	void *onchangedata;
+        const char *action;
+        const char *accelerator;
+        const char *accelerator_text;
 
-	const int* nstates;
+	const int *nstates;
 } UiMenuToggleItemArgs;
 
 typedef struct UiMenuItemListArgs {
-	const char* varname;
+	const char *varname;
 	ui_getvaluefunc getvalue;
 	ui_callback onselect;
-	void* onselectdata;
+	void *onselectdata;
+        const char *action;
         UiBool addseparator;
 } UiMenuItemListArgs;
 
--- a/ui/ui/toolbar.h	Thu Jan 29 20:55:59 2026 +0100
+++ b/ui/ui/toolbar.h	Sun Feb 01 11:42:27 2026 +0100
@@ -42,7 +42,10 @@
     const char *tooltip;
 
     ui_callback onclick;
-    void* onclickdata;
+    void *onclickdata;
+    const char *action;
+    const char *accelerator;
+    const char *accelerator_text;
 
     const int *states;
     const int *visibility_states;
@@ -56,6 +59,9 @@
     const char *varname;
     ui_callback onchange;
     void *onchangedata;
+    const char *action;
+    const char *accelerator;
+    const char *accelerator_text;
 
     const int *states;
     const int *visibility_states;

mercurial