2015-07-11 15:56:37 -05:00
|
|
|
// 10 june 2015
|
2015-07-11 16:02:01 -05:00
|
|
|
#include "uipriv_darwin.h"
|
2015-07-11 15:56:37 -05:00
|
|
|
|
2015-07-11 21:56:43 -05:00
|
|
|
@interface checkboxDelegate : NSObject {
|
|
|
|
uiCheckbox *c;
|
2015-07-11 15:56:37 -05:00
|
|
|
void (*onToggled)(uiCheckbox *, void *);
|
|
|
|
void *onToggledData;
|
2015-07-11 21:56:43 -05:00
|
|
|
}
|
|
|
|
- (IBAction)checkboxToggled:(id)sender;
|
|
|
|
- (void)setCheckbox:(uiCheckbox *)newc;
|
|
|
|
- (void)setOnToggled:(void (*)(uiCheckbox *, void *))f data:(void *)data;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation checkboxDelegate
|
|
|
|
|
|
|
|
- (IBAction)checkboxToggled:(id)sender
|
|
|
|
{
|
|
|
|
(*(self->onToggled))(self->c, self->onToggledData);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setCheckbox:(uiCheckbox *)newc
|
|
|
|
{
|
|
|
|
self->c = newc;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setOnToggled:(void (*)(uiCheckbox *, void *))f data:(void *)data
|
|
|
|
{
|
|
|
|
self->onToggled = f;
|
|
|
|
self->onToggledData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
struct checkbox {
|
|
|
|
uiCheckbox c;
|
|
|
|
NSButton *checkbox;
|
|
|
|
checkboxDelegate *delegate;
|
2015-07-11 15:56:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
uiDefineControlType(uiCheckbox, uiTypeCheckbox, struct checkbox)
|
|
|
|
|
|
|
|
static uintptr_t checkboxHandle(uiControl *cc)
|
|
|
|
{
|
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
|
|
|
|
2015-07-11 21:56:43 -05:00
|
|
|
return (uintptr_t) (c->checkbox);
|
2015-07-11 15:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultOnToggled(uiCheckbox *c, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *checkboxText(uiCheckbox *cc)
|
|
|
|
{
|
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
|
|
|
|
2015-07-11 21:56:43 -05:00
|
|
|
return uiDarwinNSStringToText([c->checkbox title]);
|
2015-07-11 15:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void checkboxSetText(uiCheckbox *cc, const char *text)
|
|
|
|
{
|
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
|
|
|
|
2015-07-11 21:56:43 -05:00
|
|
|
[c->checkbox setTitle:toNSString(text)];
|
2015-07-11 15:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void checkboxOnToggled(uiCheckbox *cc, void (*f)(uiCheckbox *, void *), void *data)
|
|
|
|
{
|
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
|
|
|
|
2015-07-11 21:56:43 -05:00
|
|
|
[c->delegate setOnToggled:f data:data];
|
2015-07-11 15:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int checkboxChecked(uiCheckbox *cc)
|
|
|
|
{
|
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
|
|
|
|
2015-07-11 21:56:43 -05:00
|
|
|
return [c->checkbox state] == NSOnState;
|
2015-07-11 15:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void checkboxSetChecked(uiCheckbox *cc, int checked)
|
|
|
|
{
|
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-07-11 21:56:43 -05:00
|
|
|
NSInteger state;
|
2015-07-11 15:56:37 -05:00
|
|
|
|
2015-07-11 21:56:43 -05:00
|
|
|
state = NSOnState;
|
|
|
|
if (!checked)
|
|
|
|
state = NSOffState;
|
|
|
|
[c->checkbox setState:state];
|
2015-07-11 15:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uiCheckbox *uiNewCheckbox(const char *text)
|
|
|
|
{
|
|
|
|
struct checkbox *c;
|
|
|
|
|
2015-07-11 21:56:43 -05:00
|
|
|
c = (struct checkbox *) uiNewControl(uiTypeCheckbox());
|
2015-07-11 15:56:37 -05:00
|
|
|
|
2015-07-11 21:56:43 -05:00
|
|
|
// TODO make a macro for the below
|
|
|
|
c->checkbox = [[NSCheckbox alloc] initWithFrame:NSZeroRect];
|
|
|
|
[c->checkbox setTitle:toNSString(text)];
|
|
|
|
[c->checkbox setButtonType:NSSwitchButton];
|
|
|
|
[c->checkbox setBordered:NO];
|
|
|
|
uiDarwinMakeSingleViewControl(uiControl(c), c->checkbox, YES);
|
2015-07-11 15:56:37 -05:00
|
|
|
|
2015-07-11 21:56:43 -05:00
|
|
|
c->delegate = [checkboxDelegate new];
|
|
|
|
[c->checkbox setTarget:c->delegate];
|
|
|
|
[c->checkbox setAction:@selector(checkboxToggled:)];
|
|
|
|
[c->delegate setCheckbox:uiCheckbox(c)];
|
|
|
|
[c->delegate setOnToggled:defaultOnToggled data:NULL];
|
2015-07-11 15:56:37 -05:00
|
|
|
|
|
|
|
uiControl(c)->Handle = checkboxHandle;
|
|
|
|
|
|
|
|
uiCheckbox(c)->Text = checkboxText;
|
|
|
|
uiCheckbox(c)->SetText = checkboxSetText;
|
|
|
|
uiCheckbox(c)->OnToggled = checkboxOnToggled;
|
|
|
|
uiCheckbox(c)->Checked = checkboxChecked;
|
|
|
|
uiCheckbox(c)->SetChecked = checkboxSetChecked;
|
|
|
|
|
|
|
|
return uiCheckbox(c);
|
|
|
|
}
|