From 3e1258cc62ee39aab7145df192d2a9eb8b2157a3 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 6 Jun 2016 20:12:17 -0400 Subject: [PATCH] Implemented the new radio button stuff on OS X. --- README.md | 3 ++ darwin/radiobuttons.m | 81 +++++++++++++++++++++++++++++++++++++++++-- unix/radiobuttons.c | 3 +- 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 751ec05c..a2c0b1ef 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ This README is being written.
*Note that today's entry may be updated later today.* +* **6 June 2016** + * Added `uiRadioButtonsSelected()`, `uiRadioButtonsSetSelected()`, and `uiRadioButtonsOnSelected()` to control selection of a radio button and catch an event when such a thing happens. + * **5 June 2016** * Added `uiNewPasswordEntry()`, which creates a new `uiEntry` suitable for entering passwords. * Added `uiNewSearchEntry()`, which creates a new `uiEntry` suitable for searching. On some systems, the `OnChanged()` event will be slightly delayed and/or combined, to produce a more natural feel when searching. diff --git a/darwin/radiobuttons.m b/darwin/radiobuttons.m index b6c663e0..c9bcfe9f 100644 --- a/darwin/radiobuttons.m +++ b/darwin/radiobuttons.m @@ -7,16 +7,50 @@ // LONGTERM 6 units of spacing between buttons, as suggested by Interface Builder? +@interface radioButtonsDelegate : NSObject { + uiRadioButtons *libui_r; +} +- (id)initWithR:(uiRadioButtons *)r; +- (IBAction)onClicked:(id)sender; +@end + struct uiRadioButtons { uiDarwinControl c; NSView *view; NSMutableArray *buttons; NSMutableArray *constraints; NSLayoutConstraint *lastv; + radioButtonsDelegate *delegate; + void (*onSelected)(uiRadioButtons *, void *); + void *onSelectedData; }; +@implementation radioButtonsDelegate + +- (id)initWithR:(uiRadioButtons *)r +{ + self = [super init]; + if (self) + self->libui_r = r; + return self; +} + +- (IBAction)onClicked:(id)sender +{ + uiRadioButtons *r = self->libui_r; + + (*(r->onSelected))(r, r->onSelectedData); +} + +@end + uiDarwinControlAllDefaultsExceptDestroy(uiRadioButtons, view) +static void defaultOnSelected(uiRadioButtons *r, void *data) +{ + // do nothing +} + static void uiRadioButtonsDestroy(uiControl *c) { uiRadioButtons *r = uiRadioButtons(c); @@ -28,9 +62,13 @@ static void uiRadioButtonsDestroy(uiControl *c) if (r->lastv != nil) [r->lastv release]; // destroy the buttons - for (b in r->buttons) + for (b in r->buttons) { + [b setTarget:nil]; [b removeFromSuperview]; + } [r->buttons release]; + // destroy the delegate + [r->delegate release]; // and destroy ourselves [r->view release]; uiFreeControl(uiControl(r)); @@ -55,7 +93,7 @@ void uiRadioButtonsAppend(uiRadioButtons *r, const char *text) uiDarwinSetControlFont(b, NSRegularControlSize); [b setTranslatesAutoresizingMaskIntoConstraints:NO]; - // TODO set target + [b setTarget:r->delegate]; [b setAction:@selector(onClicked:)]; [r->buttons addObject:b]; @@ -114,6 +152,41 @@ void uiRadioButtonsAppend(uiRadioButtons *r, const char *text) [r->lastv retain]; } +intmax_t uiRadioButtonsSelected(uiRadioButtons *r) +{ + NSButton *b; + NSUInteger i; + + for (i = 0; i < [r->buttons count]; i++) { + b = (NSButton *) [r->buttons objectAtIndex:i]; + if ([b state] == NSOnState) + return i; + } + return -1; +} + +void uiRadioButtonsSetSelected(uiRadioButtons *r, intmax_t n) +{ + NSButton *b; + NSInteger state; + + state = NSOnState; + if (n == -1) { + n = uiRadioButtonsSelected(r); + if (n == -1) // from nothing to nothing; do nothing + return; + state = NSOffState; + } + b = (NSButton *) [r->buttons objectAtIndex:n]; + [b setState:state]; +} + +void uiRadioButtonsOnSelected(uiRadioButtons *r, void (*f)(uiRadioButtons *, void *), void *data) +{ + r->onSelected = f; + r->onSelectedData = data; +} + uiRadioButtons *uiNewRadioButtons(void) { uiRadioButtons *r; @@ -124,5 +197,9 @@ uiRadioButtons *uiNewRadioButtons(void) r->buttons = [NSMutableArray new]; r->constraints = [NSMutableArray new]; + r->delegate = [[radioButtonsDelegate alloc] initWithR:r]; + + uiRadioButtonsOnSelected(r, defaultOnSelected, NULL); + return r; } diff --git a/unix/radiobuttons.c b/unix/radiobuttons.c index 24bc80b1..4f6e24e5 100644 --- a/unix/radiobuttons.c +++ b/unix/radiobuttons.c @@ -80,8 +80,9 @@ intmax_t uiRadioButtonsSelected(uiRadioButtons *r) void uiRadioButtonsSetSelected(uiRadioButtons *r, intmax_t n) { GtkToggleButton *tb; - gboolean active = TRUE; + gboolean active; + active = TRUE; // TODO this doesn't work if (n == -1) { n = uiRadioButtonsSelected(r);