41 ctx->mempool = mp; |
41 ctx->mempool = mp; |
42 |
42 |
43 ctx->toplevel = toplevel; |
43 ctx->toplevel = toplevel; |
44 ctx->vars = ucx_map_new_a(mp->allocator, 16); |
44 ctx->vars = ucx_map_new_a(mp->allocator, 16); |
45 ctx->groups = NULL; |
45 ctx->groups = NULL; |
|
46 ctx->group_widgets = NULL; |
46 |
47 |
47 #ifdef UI_GTK |
48 #ifdef UI_GTK |
48 ctx->accel_group = gtk_accel_group_new(); |
49 ctx->accel_group = gtk_accel_group_new(); |
49 gtk_window_add_accel_group(GTK_WINDOW(toplevel->widget), ctx->accel_group); |
50 gtk_window_add_accel_group(GTK_WINDOW(toplevel->widget), ctx->accel_group); |
50 #endif |
51 #endif |
156 |
157 |
157 void ui_set_group(UiContext *ctx, int group) { |
158 void ui_set_group(UiContext *ctx, int group) { |
158 if(ucx_list_find(ctx->groups, (void*)(intptr_t)group, NULL, NULL) == -1) { |
159 if(ucx_list_find(ctx->groups, (void*)(intptr_t)group, NULL, NULL) == -1) { |
159 ctx->groups = ucx_list_append_a(ctx->mempool->allocator, ctx->groups, (void*)(intptr_t)group); |
160 ctx->groups = ucx_list_append_a(ctx->mempool->allocator, ctx->groups, (void*)(intptr_t)group); |
160 } |
161 } |
|
162 |
|
163 // enable/disable group widgets |
|
164 uic_check_group_widgets(ctx); |
161 } |
165 } |
162 |
166 |
163 void ui_unset_group(UiContext *ctx, int group) { |
167 void ui_unset_group(UiContext *ctx, int group) { |
164 int i = ucx_list_find(ctx->groups, (void*)(intptr_t)group, NULL, NULL); |
168 int i = ucx_list_find(ctx->groups, (void*)(intptr_t)group, NULL, NULL); |
165 if(i != -1) { |
169 if(i != -1) { |
166 UcxList *elm = ucx_list_get(ctx->groups, i); |
170 UcxList *elm = ucx_list_get(ctx->groups, i); |
167 ctx->groups = ucx_list_remove_a(ctx->mempool->allocator, ctx->groups, elm); |
171 ctx->groups = ucx_list_remove_a(ctx->mempool->allocator, ctx->groups, elm); |
168 } |
172 } |
|
173 |
|
174 // enable/disable group widgets |
|
175 uic_check_group_widgets(ctx); |
169 } |
176 } |
170 |
177 |
171 int* ui_active_groups(UiContext *ctx, int *ngroups) { |
178 int* ui_active_groups(UiContext *ctx, int *ngroups) { |
172 int nelm = ucx_list_size(ctx->groups); |
179 int nelm = ucx_list_size(ctx->groups); |
173 int *groups = calloc(sizeof(int), nelm); |
180 int *groups = calloc(sizeof(int), nelm); |
178 } |
185 } |
179 |
186 |
180 *ngroups = nelm; |
187 *ngroups = nelm; |
181 return groups; |
188 return groups; |
182 } |
189 } |
|
190 |
|
191 void uic_check_group_widgets(UiContext *ctx) { |
|
192 int ngroups = 0; |
|
193 int *groups = ui_active_groups(ctx, &ngroups); |
|
194 |
|
195 UCX_FOREACH(elm, ctx->group_widgets) { |
|
196 UiGroupWidget *gw = elm->data; |
|
197 char *check = calloc(1, gw->numgroups); |
|
198 |
|
199 for(int i=0;i<ngroups;i++) { |
|
200 for(int k=0;k<gw->numgroups;k++) { |
|
201 if(groups[i] == gw->groups[k]) { |
|
202 check[k] = 1; |
|
203 } |
|
204 } |
|
205 } |
|
206 |
|
207 int enable = 1; |
|
208 for(int i=0;i<gw->numgroups;i++) { |
|
209 if(check[i] == 0) { |
|
210 enable = 0; |
|
211 break; |
|
212 } |
|
213 } |
|
214 ui_set_enabled(gw->widget, enable); |
|
215 } |
|
216 |
|
217 free(groups); |
|
218 } |
|
219 |
|
220 void uic_add_group_widget(UiContext *ctx, UIWIDGET widget, UcxList *groups) { |
|
221 UcxMempool *mp = ctx->mempool; |
|
222 UiGroupWidget *gw = ucx_mempool_malloc(mp, sizeof(UiGroupWidget)); |
|
223 |
|
224 gw->widget = widget; |
|
225 gw->numgroups = ucx_list_size(groups); |
|
226 gw->groups = ucx_mempool_calloc(mp, gw->numgroups, sizeof(int)); |
|
227 int i = 0; |
|
228 UCX_FOREACH(elm, groups) { |
|
229 gw->groups[i++] = (intptr_t)elm->data; |
|
230 } |
|
231 |
|
232 ctx->group_widgets = ucx_list_append_a( |
|
233 mp->allocator, |
|
234 ctx->group_widgets, |
|
235 gw); |
|
236 } |
|
237 |