ui/motif/button.c

changeset 101
7b3a3130be44
parent 100
d2bd73d28ff1
--- a/ui/motif/button.c	Thu Dec 12 20:01:43 2024 +0100
+++ b/ui/motif/button.c	Mon Jan 06 22:22:55 2025 +0100
@@ -49,7 +49,7 @@
     UI_APPLY_LAYOUT(ctn->layout, args);
     
     Widget parent = ctn->prepare(ctn, xargs, &n);
-    
+       
     XmString label = NULL;
     if(args.label) {
         label = XmStringCreateLocalized((char*)args.label);
@@ -239,7 +239,7 @@
 }
 
 static void destroy_list(Widget w, CxList *list, XtPointer d) {
-    cxListDestroy(list);
+    cxListFree(list);
 }
 
 static void radiobutton_changed(Widget w, UiVarEventData *event, XmToggleButtonCallbackStruct *tb) {
@@ -291,6 +291,55 @@
     }
 }
 
+void ui_bind_radiobutton(UiObject *obj, Widget rbutton, UiInteger *value, const char *varname, ui_callback onchange, void *onchangedata, int enable_group) {
+    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, value, varname, UI_VAR_INTEGER);
+    if(var) {
+        UiInteger *value = var->value;
+        CxList *rb = value->obj;
+        if(!rb) {
+            // first button in the radiobutton group
+            // create a list for all buttons and use the list as value obj
+            rb = cxArrayListCreateSimple(CX_STORE_POINTERS, 4);
+            value->obj = rb;
+            value->get = ui_radiobutton_get;
+            value->set = ui_radiobutton_set;
+            
+            // the first radio button is also responsible for cleanup
+            XtAddCallback(
+                    rbutton,
+                    XmNdestroyCallback,
+                    (XtCallbackProc)destroy_list,
+                    rb);
+        }
+        cxListAdd(rb, rbutton);
+        
+        // set the radiobutton state, if the value is already set
+        if(cxListSize(rb) == value->value) {
+            XmToggleButtonSetState(rbutton, True, False);
+        }
+    }
+    
+    // the radio button needs to handle change events to update all
+    // other buttons in the radio button group
+    UiVarEventData *event = malloc(sizeof(UiVarEventData));
+    event->obj = obj;
+    event->callback = onchange;
+    event->userdata = onchangedata;
+    event->observers = NULL;
+    event->var = var;
+    event->value = enable_group;
+    XtAddCallback(
+            rbutton,
+            XmNvalueChangedCallback,
+            (XtCallbackProc)radiobutton_changed,
+            event);
+    XtAddCallback(
+            rbutton,
+            XmNdestroyCallback,
+            (XtCallbackProc)ui_destroy_eventdata,
+            event);
+}
+
 UIWIDGET ui_radiobutton_create(UiObject* obj, UiToggleArgs args) {
     Arg xargs[16];
     int n = 0;

mercurial