2015-08-14 20:46:35 -05:00
|
|
|
// 14 august 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
|
|
|
struct uiCheckbox {
|
|
|
|
uiDarwinControl c;
|
2015-08-17 11:57:45 -05:00
|
|
|
NSButton *button;
|
2015-08-14 20:46:35 -05:00
|
|
|
void (*onToggled)(uiCheckbox *, void *);
|
|
|
|
void *onToggledData;
|
|
|
|
};
|
|
|
|
|
|
|
|
@interface checkboxDelegateClass : NSObject {
|
2016-01-07 13:41:20 -06:00
|
|
|
struct mapTable *buttons;
|
2015-08-14 20:46:35 -05:00
|
|
|
}
|
|
|
|
- (IBAction)onToggled:(id)sender;
|
|
|
|
- (void)registerCheckbox:(uiCheckbox *)c;
|
|
|
|
- (void)unregisterCheckbox:(uiCheckbox *)c;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation checkboxDelegateClass
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
2015-08-17 00:29:34 -05:00
|
|
|
self->buttons = newMap();
|
2015-08-14 20:46:35 -05:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2016-01-07 13:41:20 -06:00
|
|
|
mapDestroy(self->buttons);
|
2015-08-14 20:46:35 -05:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)onToggled:(id)sender
|
|
|
|
{
|
|
|
|
uiCheckbox *c;
|
|
|
|
|
2015-08-17 00:29:34 -05:00
|
|
|
c = (uiCheckbox *) mapGet(self->buttons, sender);
|
2015-08-14 20:46:35 -05:00
|
|
|
(*(c->onToggled))(c, c->onToggledData);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerCheckbox:(uiCheckbox *)c
|
|
|
|
{
|
2015-08-17 00:29:34 -05:00
|
|
|
mapSet(self->buttons, c->button, c);
|
2015-08-14 20:46:35 -05:00
|
|
|
[c->button setTarget:self];
|
|
|
|
[c->button setAction:@selector(onToggled:)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)unregisterCheckbox:(uiCheckbox *)c
|
|
|
|
{
|
|
|
|
[c->button setTarget:nil];
|
2016-01-07 13:41:20 -06:00
|
|
|
mapDelete(self->buttons, c->button);
|
2015-08-14 20:46:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static checkboxDelegateClass *checkboxDelegate = nil;
|
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinControlAllDefaultsExceptDestroy(uiCheckbox, button)
|
|
|
|
|
2016-04-25 11:52:54 -05:00
|
|
|
static void uiCheckboxDestroy(uiControl *cc)
|
2016-04-25 10:54:09 -05:00
|
|
|
{
|
2016-04-25 11:52:54 -05:00
|
|
|
uiCheckbox *c = uiCheckbox(cc);
|
|
|
|
|
|
|
|
[checkboxDelegate unregisterCheckbox:c];
|
|
|
|
[c->button release];
|
|
|
|
uiFreeControl(uiControl(c));
|
2016-04-25 10:54:09 -05:00
|
|
|
}
|
2015-08-14 20:46:35 -05:00
|
|
|
|
|
|
|
char *uiCheckboxText(uiCheckbox *c)
|
|
|
|
{
|
|
|
|
return uiDarwinNSStringToText([c->button title]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiCheckboxSetText(uiCheckbox *c, const char *text)
|
|
|
|
{
|
|
|
|
[c->button setTitle:toNSString(text)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiCheckboxOnToggled(uiCheckbox *c, void (*f)(uiCheckbox *, void *), void *data)
|
|
|
|
{
|
|
|
|
c->onToggled = f;
|
|
|
|
c->onToggledData = data;
|
|
|
|
}
|
|
|
|
|
2015-08-16 22:20:53 -05:00
|
|
|
int uiCheckboxChecked(uiCheckbox *c)
|
2015-08-14 20:46:35 -05:00
|
|
|
{
|
|
|
|
return [c->button state] == NSOnState;
|
|
|
|
}
|
|
|
|
|
2015-08-16 22:20:53 -05:00
|
|
|
void uiCheckboxSetChecked(uiCheckbox *c, int checked)
|
2015-08-14 20:46:35 -05:00
|
|
|
{
|
|
|
|
NSInteger state;
|
|
|
|
|
|
|
|
state = NSOnState;
|
|
|
|
if (!checked)
|
|
|
|
state = NSOffState;
|
|
|
|
[c->button setState:state];
|
|
|
|
}
|
|
|
|
|
2015-08-16 22:20:53 -05:00
|
|
|
static void defaultOnToggled(uiCheckbox *c, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2015-08-14 20:46:35 -05:00
|
|
|
uiCheckbox *uiNewCheckbox(const char *text)
|
|
|
|
{
|
|
|
|
uiCheckbox *c;
|
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinNewControl(uiCheckbox, c);
|
2015-08-14 20:46:35 -05:00
|
|
|
|
2016-05-21 01:00:08 -05:00
|
|
|
// TODO bezel style? transparent?
|
2015-08-14 20:46:35 -05:00
|
|
|
c->button = [[NSButton alloc] initWithFrame:NSZeroRect];
|
|
|
|
[c->button setTitle:toNSString(text)];
|
|
|
|
[c->button setButtonType:NSSwitchButton];
|
|
|
|
[c->button setBordered:NO];
|
2015-08-14 21:50:20 -05:00
|
|
|
uiDarwinSetControlFont(c->button, NSRegularControlSize);
|
2015-08-14 20:46:35 -05:00
|
|
|
|
2015-08-17 11:57:45 -05:00
|
|
|
if (checkboxDelegate == nil) {
|
2016-05-24 22:17:08 -05:00
|
|
|
checkboxDelegate = [[checkboxDelegateClass new] autorelease];
|
2015-08-17 11:57:45 -05:00
|
|
|
[delegates addObject:checkboxDelegate];
|
|
|
|
}
|
2015-08-14 20:46:35 -05:00
|
|
|
[checkboxDelegate registerCheckbox:c];
|
|
|
|
uiCheckboxOnToggled(c, defaultOnToggled, NULL);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|