Fri, 17 Apr 2026 14:50:31 +0200
add action to button/text widget args
| 1066 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2026 Olaf Wintermann. All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | ||
| 29 | #include "action.h" | |
| 30 | #include "context.h" | |
| 31 | ||
| 32 | #include <cx/string.h> | |
| 33 | ||
| 34 | void uic_add_action( | |
| 35 | UiContext *ctx, | |
| 36 | const char *name, | |
| 37 | ui_callback callback, | |
| 38 | void *userdata, | |
| 39 | const char *accelerator, | |
| 40 | const char *accelerator_text) | |
| 41 | { | |
| 42 | if(!name) { | |
| 43 | return; | |
| 44 | } | |
| 45 | ||
| 46 | UiAction action; | |
| 47 | action.name = ui_strdup(ctx, name); | |
| 48 | action.callback = callback; | |
| 49 | action.userdata = userdata; | |
| 50 | action.accelerator = accelerator ? ui_strdup(ctx, accelerator) : NULL; | |
| 51 | action.accelerator_text = accelerator_text ? ui_strdup(ctx, accelerator_text) : NULL; | |
|
1092
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
52 | action.ctx = ctx; |
| 1066 | 53 | cxMapPut(ctx->actions, name, &action); |
| 54 | cxMapRehash(ctx->actions); | |
| 55 | } | |
| 56 | ||
| 57 | void uic_bind_action( | |
| 58 | UiContext *ctx, | |
| 59 | const char *action, | |
| 60 | void *bind_obj, | |
|
1092
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
61 | ui_enablefunc set_enabled) |
| 1066 | 62 | { |
| 63 | if(!action) { | |
| 64 | return; | |
| 65 | } | |
| 66 | ||
| 67 | UiActionBinding binding; | |
| 68 | binding.action = ui_strdup(ctx, action); | |
| 69 | binding.userdata = bind_obj; | |
| 70 | binding.set_enabled = set_enabled; | |
| 71 | cxListAdd(ctx->action_bindings, &binding); | |
| 72 | } | |
|
1092
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
73 | |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
74 | UiAction* uic_resolve_action(UiContext *ctx, const char *action) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
75 | UiAction *a = NULL; |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
76 | if(ctx->actions) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
77 | a = cxMapGet(ctx->actions, action); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
78 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
79 | // check if any sub-document defines this action |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
80 | // sub-document actions have precedence, the most specific action will |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
81 | // be returned |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
82 | CxIterator i = cxListIterator(ctx->documents); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
83 | cx_foreach(void *, doc, i) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
84 | UiContext *doc_ctx = ui_document_context(doc); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
85 | UiAction *sub_action = uic_resolve_action(doc_ctx, action); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
86 | if(sub_action) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
87 | a = sub_action; |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
88 | // if one sub-tree has an action, we don't care about other |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
89 | // subtrees |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
90 | break; |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
91 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
92 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
93 | |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
94 | if(!a && ctx->parent) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
95 | // check parents |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
96 | a = uic_resolve_action_from_parents(ctx, action); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
97 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
98 | |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
99 | return a; |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
100 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
101 | |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
102 | UiAction* uic_resolve_action_from_parents(UiContext *ctx, const char *action) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
103 | UiContext *parent = ctx->parent; |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
104 | if(parent == NULL) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
105 | return NULL; |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
106 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
107 | if(parent->actions) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
108 | UiAction *a = cxMapGet(parent->actions, action); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
109 | if(a) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
110 | return a; |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
111 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
112 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
113 | return uic_resolve_action_from_parents(parent, action); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
114 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
115 | |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
116 | |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
117 | |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
118 | void ui_update_action_bindings(UiContext *ctx) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
119 | CxIterator i = cxListIterator(ctx->action_bindings); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
120 | cx_foreach(UiActionBinding*, binding, i) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
121 | UiAction *action = uic_resolve_action(ctx, binding->action); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
122 | if(binding->set_enabled) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
123 | binding->set_enabled(binding->userdata, action != NULL); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
124 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
125 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
126 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
127 | |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
128 | void uic_action_callback(UiEvent *event, const char *action_name) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
129 | UiContext *ctx = ui_global_context(); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
130 | if(event->obj) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
131 | ctx = event->obj->ctx; |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
132 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
133 | |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
134 | UiAction *action = uic_resolve_action(ctx, action_name); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
135 | if(action) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
136 | // override event document: for actions we know that the event is |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
137 | // for a specific document |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
138 | event->document = action->ctx->self_doc; |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
139 | if(action->callback) { |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
140 | action->callback(event, action->userdata); |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
141 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
142 | } |
|
0accf125a65f
add actions, implement action binding for menu items (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1066
diff
changeset
|
143 | } |