implement radio button (Cocoa)

12 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 12 Apr 2025 19:21:15 +0200 (12 days ago)
changeset 562
88ed463e5433
parent 561
5798e3a28c59
child 564
5c08f99b2a09
child 566
0e0e9035c6c7

implement radio button (Cocoa)

make/xcode/toolkit/toolkit/main.m file | annotate | diff | comparison | revisions
ui/cocoa/button.h file | annotate | diff | comparison | revisions
ui/cocoa/button.m file | annotate | diff | comparison | revisions
--- a/make/xcode/toolkit/toolkit/main.m	Thu Apr 10 21:18:14 2025 +0200
+++ b/make/xcode/toolkit/toolkit/main.m	Sat Apr 12 19:21:15 2025 +0200
@@ -44,9 +44,13 @@
     
     
     ui_vbox(obj, .spacing = 0, .fill = UI_OFF) {
-        ui_button(obj, .label = "V Button 1");
-        ui_button(obj, .label = "V Button 2");
-        ui_button(obj, .label = "V Button 3");
+        ui_radiobutton(obj, .label = "V Button 1", .varname = "radio1");
+        ui_radiobutton(obj, .label = "V Button 2", .varname = "radio1");
+        ui_radiobutton(obj, .label = "V Button 3", .varname = "radio1");
+        
+        ui_radiobutton(obj, .label = "V Button 1 R2", .varname = "radio2");
+        ui_radiobutton(obj, .label = "V Button 2 R2", .varname = "radio2");
+        ui_radiobutton(obj, .label = "V Button 3 R2", .varname = "radio2");
     }
     
     ui_show(obj);
--- a/ui/cocoa/button.h	Thu Apr 10 21:18:14 2025 +0200
+++ b/ui/cocoa/button.h	Sat Apr 12 19:21:15 2025 +0200
@@ -30,6 +30,17 @@
 
 #import "../ui/button.h"
 
+@interface UiRadioButton : NSButton
+
+@property UiVar *var;
+@property Boolean direct_state;
+
+- (UiRadioButton*)init;
+- (void)setState:(NSControlStateValue)state;
+
+@end
+
+
 int64_t ui_togglebutton_get(UiInteger *i);
 void ui_togglebutton_set(UiInteger *i, int64_t value);
 
--- a/ui/cocoa/button.m	Thu Apr 10 21:18:14 2025 +0200
+++ b/ui/cocoa/button.m	Sat Apr 12 19:21:15 2025 +0200
@@ -174,17 +174,45 @@
     button.state = state;
 }
 
+
+@implementation UiRadioButton
+
+- (UiRadioButton*)init {
+    self = [super init];
+    _direct_state = NO;
+    [self setButtonType:NSButtonTypeRadio];
+    return self;
+}
+
+- (void)setState:(NSControlStateValue)state {
+    // NOOP
+}
+
+@end
+
 static void radiobutton_eventdata(id button, UiVar *var, void **eventdata, int *value) {
     if(var) {
-        printf("switch radiobutton\n");
+        UiInteger *value = var->value;
+        NSMutableArray *buttons = (__bridge NSMutableArray*)value->obj;
+        for(UiRadioButton *b in buttons) {
+            if(b != button) {
+                b.direct_state = YES;
+                [[b cell] setState:0];
+                b.direct_state = NO;
+            }
+        }
     }
 }
 
 UIWIDGET ui_radiobutton_create(UiObject* obj, UiToggleArgs args) {
-    NSButton *button = [[NSButton alloc] init];
-    [button setButtonType:NSButtonTypeRadio];
+    UiRadioButton *button = [[UiRadioButton alloc] init];
+    
+    if(args.label) {
+        button.title = [[NSString alloc] initWithUTF8String:args.label];
+    }
     
     UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER);
+    button.var = var;
     NSMutableArray *buttons = nil;
     if(var) {
         UiInteger *i = var->value;
@@ -220,9 +248,28 @@
 }
 
 int64_t ui_radiobuttons_get(UiInteger *i) {
-    return 0;
+    NSMutableArray *buttons = (__bridge NSMutableArray*)i->obj;
+    int64_t index = 0;
+    for(UiRadioButton *b in buttons) {
+        if([b cell].state != 0) {
+            i->value = index;
+            break;
+        }
+        index++;
+    }
+    return index;
 }
 
 void ui_radiobuttons_set(UiInteger *i, int64_t value) {
-    
+    NSMutableArray *buttons = (__bridge NSMutableArray*)i->obj;
+    int64_t index = 0;
+    for(UiRadioButton *b in buttons) {
+        if(index == value) {
+            [b cell].state = NSControlStateValueOn;
+        } else {
+            [b cell].state = NSControlStateValueOff;
+        }
+        index++;
+    }
+    return index;
 }

mercurial