# HG changeset patch # User Olaf Wintermann # Date 1729624621 -7200 # Node ID 54f5d7eb1335082669cbddcacd75c6e70d9edcca # Parent 99f83fbf48e9a6b8a24060a1e176de4fe6fea03c implement widget groups for buttons (GTK) diff -r 99f83fbf48e9 -r 54f5d7eb1335 application/main.c --- a/application/main.c Mon Oct 21 15:47:10 2024 +0200 +++ b/application/main.c Tue Oct 22 21:17:01 2024 +0200 @@ -152,6 +152,25 @@ ui_set(doc->tabview, 0); } + +void action_group1(UiEvent *event, void *userdata) { + UiContext *ctx = event->obj->ctx; + if(userdata) { + ui_unset_group(ctx, 1); + } else { + ui_set_group(ctx, 1); + } +} + +void action_group2(UiEvent *event, void *userdata) { + UiContext *ctx = event->obj->ctx; + if(userdata) { + ui_unset_group(ctx, 2); + } else { + ui_set_group(ctx, 2); + } +} + void application_startup(UiEvent *event, void *data) { // global list UiContext *global = ui_global_context(); @@ -175,10 +194,14 @@ ui_checkbox(obj, .label = "My Checkbox"); } ui_grid(obj, .fill = UI_OFF, .columnspacing = 15, .rowspacing = 15, .margin = 15) { - ui_button(obj, .label = "cell1", .hexpand = TRUE); - ui_button(obj, .label = "cell2"); + ui_button(obj, .label = "Activate Group 1", .hexpand = TRUE, .onclick = action_group1); + ui_button(obj, .label = "Disable Group 1", .onclick = action_group1, .onclickdata = "disable"); ui_newline(obj); - ui_button(obj, .label = "cell_colspan2", .colspan = 2); + ui_button(obj, .label = "Activate Group 2", .hexpand = TRUE, .onclick = action_group2); + ui_button(obj, .label = "Disable Group 2", .onclick = action_group2, .onclickdata = "disable"); + ui_newline(obj); + + ui_button(obj, .label = "Groups 1,2", .colspan = 2, .groups = UI_GROUPS(1, 2)); ui_newline(obj); ui_label(obj, .label = "Label Col 1", .align = UI_ALIGN_LEFT); diff -r 99f83fbf48e9 -r 54f5d7eb1335 ui/common/context.c --- a/ui/common/context.c Mon Oct 21 15:47:10 2024 +0200 +++ b/ui/common/context.c Tue Oct 22 21:17:01 2024 +0200 @@ -523,19 +523,28 @@ cxListDestroy(groups); } +size_t uic_group_array_size(const int *groups) { + int i; + for(i=0;groups[i] >= 0;i++) { } + return i; +} + void uic_add_group_widget(UiContext *ctx, void *widget, ui_enablefunc enable, CxList *groups) { + uic_add_group_widget_i(ctx, widget, enable, cxListAt(groups, 0), cxListSize(groups)); +} + +void uic_add_group_widget_i(UiContext *ctx, void *widget, ui_enablefunc enable, const int *groups, size_t numgroups) { const CxAllocator *a = ctx->allocator; UiGroupWidget gw; gw.widget = widget; gw.enable = enable; - gw.numgroups = cxListSize(groups); - gw.groups = cxCalloc(a, gw.numgroups, sizeof(int)); + gw.numgroups = numgroups; + gw.groups = cxCalloc(a, numgroups, sizeof(int)); // copy groups - int *intgroups = cxListAt(groups, 0); - if(intgroups) { - memcpy(gw.groups, intgroups, gw.numgroups * sizeof(int)); + if(groups) { + memcpy(gw.groups, groups, gw.numgroups * sizeof(int)); } cxListAdd(ctx->group_widgets, &gw); diff -r 99f83fbf48e9 -r 54f5d7eb1335 ui/common/context.h --- a/ui/common/context.h Mon Oct 21 15:47:10 2024 +0200 +++ b/ui/common/context.h Tue Oct 22 21:17:01 2024 +0200 @@ -134,8 +134,10 @@ void uic_remove_bound_var(UiContext *ctx, UiVar *var); +size_t uic_group_array_size(const int *groups); void uic_check_group_widgets(UiContext *ctx); void uic_add_group_widget(UiContext *ctx, void *widget, ui_enablefunc enable, CxList *groups); +void uic_add_group_widget_i(UiContext *ctx, void *widget, ui_enablefunc enable, const int *groups, size_t numgroups); #ifdef __cplusplus diff -r 99f83fbf48e9 -r 54f5d7eb1335 ui/gtk/button.c --- a/ui/gtk/button.c Mon Oct 21 15:47:10 2024 +0200 +++ b/ui/gtk/button.c Tue Oct 22 21:17:01 2024 +0200 @@ -91,6 +91,7 @@ UiObject* current = uic_current_obj(obj); GtkWidget *button = ui_create_button(obj, args.label, args.icon, args.onclick, args.onclickdata); ui_set_name_and_style(button, args.name, args.style_class); + ui_set_widget_groups(obj->ctx, button, args.groups); UI_APPLY_LAYOUT1(current, args); current->container->add(current->container, button, FALSE); return button; @@ -232,6 +233,7 @@ ui_setup_togglebutton(current, widget, args.label, args.icon, args.varname, args.value, args.onchange, args.onchangedata); ui_set_name_and_style(widget, args.name, args.style_class); + ui_set_widget_groups(obj->ctx, widget, args.groups); UI_APPLY_LAYOUT1(current, args); current->container->add(current->container, widget, FALSE); @@ -282,6 +284,9 @@ args.onchange, args.onchangedata); + ui_set_name_and_style(widget, args.name, args.style_class); + ui_set_widget_groups(obj->ctx, widget, args.groups); + UI_APPLY_LAYOUT1(current, args); current->container->add(current->container, widget, FALSE); @@ -343,6 +348,7 @@ GtkWidget *rbutton = RADIOBUTTON_NEW(rg, args.label); ui_set_name_and_style(rbutton, args.name, args.style_class); + ui_set_widget_groups(obj->ctx, rbutton, args.groups); if(rgroup) { #if GTK_MAJOR_VERSION >= 4 if(rg) { diff -r 99f83fbf48e9 -r 54f5d7eb1335 ui/gtk/toolkit.c --- a/ui/gtk/toolkit.c Mon Oct 21 15:47:10 2024 +0200 +++ b/ui/gtk/toolkit.c Tue Oct 22 21:17:01 2024 +0200 @@ -409,3 +409,13 @@ } } + +void ui_set_widget_groups(UiContext *ctx, GtkWidget *widget, const int *groups) { + if(!groups) { + return; + } + + size_t ngroups = uic_group_array_size(groups); + uic_add_group_widget_i(ctx, widget, (ui_enablefunc)ui_set_enabled, groups, ngroups); + ui_set_enabled(widget, FALSE); +} diff -r 99f83fbf48e9 -r 54f5d7eb1335 ui/gtk/toolkit.h --- a/ui/gtk/toolkit.h Mon Oct 21 15:47:10 2024 +0200 +++ b/ui/gtk/toolkit.h Tue Oct 22 21:17:01 2024 +0200 @@ -159,6 +159,7 @@ int ui_get_scalefactor(); void ui_set_name_and_style(GtkWidget *widget, const char *name, const char *style); +void ui_set_widget_groups(UiContext *ctx, GtkWidget *widget, const int *groups); void ui_destroy_userdata(GtkWidget *object, void *userdata); void ui_destroy_vardata(GtkWidget *object, UiVarEventData *data); diff -r 99f83fbf48e9 -r 54f5d7eb1335 ui/ui/button.h --- a/ui/ui/button.h Mon Oct 21 15:47:10 2024 +0200 +++ b/ui/ui/button.h Tue Oct 22 21:17:01 2024 +0200 @@ -50,6 +50,8 @@ UiLabelType labeltype; ui_callback onclick; void* onclickdata; + + const int* groups; } UiButtonArgs; typedef struct UiToggleArgs { @@ -69,6 +71,8 @@ const char* varname; ui_callback onchange; void* onchangedata; + + const int* groups; } UiToggleArgs; #define ui_button(obj, ...) ui_button_create(obj, (UiButtonArgs){ __VA_ARGS__ } ) diff -r 99f83fbf48e9 -r 54f5d7eb1335 ui/ui/toolkit.h --- a/ui/ui/toolkit.h Mon Oct 21 15:47:10 2024 +0200 +++ b/ui/ui/toolkit.h Tue Oct 22 21:17:01 2024 +0200 @@ -147,7 +147,7 @@ #define UI_GROUP_SELECTION 20000 -#define UI_GROUPS(...) (int[]){ __VA_ARGS__, -1 } +#define UI_GROUPS(...) (const int[]){ __VA_ARGS__, -1 } /* public types */ typedef int UiBool; @@ -202,7 +202,7 @@ typedef void(*ui_freefunc)(void*); -typedef void(*ui_enablefunc)(void*, UiBool); +typedef void(*ui_enablefunc)(void*, int); struct UiObject { /*