Implemented the new radio button stuff on OS X.

This commit is contained in:
Pietro Gagliardi 2016-06-06 20:12:17 -04:00
parent 9a5bc738c5
commit 3e1258cc62
3 changed files with 84 additions and 3 deletions

View File

@ -37,6 +37,9 @@ This README is being written.<br>
*Note that today's entry may be updated later today.* *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** * **5 June 2016**
* Added `uiNewPasswordEntry()`, which creates a new `uiEntry` suitable for entering passwords. * 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. * 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.

View File

@ -7,16 +7,50 @@
// LONGTERM 6 units of spacing between buttons, as suggested by Interface Builder? // 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 { struct uiRadioButtons {
uiDarwinControl c; uiDarwinControl c;
NSView *view; NSView *view;
NSMutableArray *buttons; NSMutableArray *buttons;
NSMutableArray *constraints; NSMutableArray *constraints;
NSLayoutConstraint *lastv; 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) uiDarwinControlAllDefaultsExceptDestroy(uiRadioButtons, view)
static void defaultOnSelected(uiRadioButtons *r, void *data)
{
// do nothing
}
static void uiRadioButtonsDestroy(uiControl *c) static void uiRadioButtonsDestroy(uiControl *c)
{ {
uiRadioButtons *r = uiRadioButtons(c); uiRadioButtons *r = uiRadioButtons(c);
@ -28,9 +62,13 @@ static void uiRadioButtonsDestroy(uiControl *c)
if (r->lastv != nil) if (r->lastv != nil)
[r->lastv release]; [r->lastv release];
// destroy the buttons // destroy the buttons
for (b in r->buttons) for (b in r->buttons) {
[b setTarget:nil];
[b removeFromSuperview]; [b removeFromSuperview];
}
[r->buttons release]; [r->buttons release];
// destroy the delegate
[r->delegate release];
// and destroy ourselves // and destroy ourselves
[r->view release]; [r->view release];
uiFreeControl(uiControl(r)); uiFreeControl(uiControl(r));
@ -55,7 +93,7 @@ void uiRadioButtonsAppend(uiRadioButtons *r, const char *text)
uiDarwinSetControlFont(b, NSRegularControlSize); uiDarwinSetControlFont(b, NSRegularControlSize);
[b setTranslatesAutoresizingMaskIntoConstraints:NO]; [b setTranslatesAutoresizingMaskIntoConstraints:NO];
// TODO set target [b setTarget:r->delegate];
[b setAction:@selector(onClicked:)]; [b setAction:@selector(onClicked:)];
[r->buttons addObject:b]; [r->buttons addObject:b];
@ -114,6 +152,41 @@ void uiRadioButtonsAppend(uiRadioButtons *r, const char *text)
[r->lastv retain]; [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 *uiNewRadioButtons(void)
{ {
uiRadioButtons *r; uiRadioButtons *r;
@ -124,5 +197,9 @@ uiRadioButtons *uiNewRadioButtons(void)
r->buttons = [NSMutableArray new]; r->buttons = [NSMutableArray new];
r->constraints = [NSMutableArray new]; r->constraints = [NSMutableArray new];
r->delegate = [[radioButtonsDelegate alloc] initWithR:r];
uiRadioButtonsOnSelected(r, defaultOnSelected, NULL);
return r; return r;
} }

View File

@ -80,8 +80,9 @@ intmax_t uiRadioButtonsSelected(uiRadioButtons *r)
void uiRadioButtonsSetSelected(uiRadioButtons *r, intmax_t n) void uiRadioButtonsSetSelected(uiRadioButtons *r, intmax_t n)
{ {
GtkToggleButton *tb; GtkToggleButton *tb;
gboolean active = TRUE; gboolean active;
active = TRUE;
// TODO this doesn't work // TODO this doesn't work
if (n == -1) { if (n == -1) {
n = uiRadioButtonsSelected(r); n = uiRadioButtonsSelected(r);