2015-04-09 11:47:34 -05:00
|
|
|
// 7 april 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
@interface uipCheckboxDelegate : NSObject {
|
|
|
|
uiCheckbox *c;
|
|
|
|
void (*onToggled)(uiCheckbox *, void *);
|
|
|
|
void *onToggledData;
|
|
|
|
}
|
|
|
|
- (IBAction)checkboxToggled:(id)sender;
|
|
|
|
- (void)setCheckbox:(uiCheckbox *)c;
|
|
|
|
- (void)setOnToggled:(void (*)(uiCheckbox *, void *))f data:(void *)data;
|
2015-04-09 11:47:34 -05:00
|
|
|
@end
|
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
@implementation uipCheckboxDelegate
|
|
|
|
|
|
|
|
- (IBAction)checkboxToggled:(id)sender
|
|
|
|
{
|
|
|
|
(*(self->onToggled))(self->c, self->onToggledData);
|
|
|
|
}
|
2015-04-09 11:47:34 -05:00
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
- (void)setCheckbox:(uiCheckbox *)c
|
2015-04-09 11:47:34 -05:00
|
|
|
{
|
2015-04-17 17:32:02 -05:00
|
|
|
self->c = c;
|
2015-04-09 11:47:34 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
- (void)setOnToggled:(void (*)(uiCheckbox *, void *))f data:(void *)data
|
2015-04-09 11:47:34 -05:00
|
|
|
{
|
2015-04-17 17:32:02 -05:00
|
|
|
self->onToggled = f;
|
|
|
|
self->onToggledData = data;
|
2015-04-09 11:47:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
struct checkbox {
|
|
|
|
uiCheckbox c;
|
|
|
|
NSButton *checkbox;
|
|
|
|
uipCheckboxDelegate *delegate;
|
|
|
|
};
|
|
|
|
|
2015-04-16 12:51:46 -05:00
|
|
|
static void defaultOnToggled(uiCheckbox *c, void *data)
|
2015-04-09 11:47:34 -05:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
static void destroy(void *data)
|
|
|
|
{
|
|
|
|
struct checkbox *c = (struct checkbox *) data;
|
|
|
|
|
|
|
|
[c->checkbox setTarget:nil];
|
|
|
|
[c->delegate release];
|
|
|
|
uiFree(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *checkboxText(uiCheckbox *cc)
|
2015-04-09 11:47:34 -05:00
|
|
|
{
|
2015-04-17 17:32:02 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 11:47:34 -05:00
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
return uiDarwinNSStringToText([c->checkbox title]);
|
2015-04-09 11:47:34 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
static void checkboxSetText(uiCheckbox *cc, const char *text)
|
2015-04-09 11:47:34 -05:00
|
|
|
{
|
2015-04-17 17:32:02 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 11:47:34 -05:00
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
[c->checkbox setTitle:toNSString(text)];
|
2015-04-09 11:47:34 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
static void checkboxOnToggled(uiCheckbox *cc, void (*f)(uiCheckbox *, void *), void *data)
|
2015-04-09 11:47:34 -05:00
|
|
|
{
|
2015-04-17 17:32:02 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 11:47:34 -05:00
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
[c->delegate setOnToggled:f data:data];
|
2015-04-09 11:47:34 -05:00
|
|
|
}
|
2015-04-09 12:01:23 -05:00
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
static int checkboxChecked(uiCheckbox *cc)
|
2015-04-09 12:01:23 -05:00
|
|
|
{
|
2015-04-17 17:32:02 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 12:01:23 -05:00
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
return [c->checkbox state] == NSOnState;
|
2015-04-09 12:01:23 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
static void checkboxSetChecked(uiCheckbox *cc, int checked)
|
2015-04-09 12:01:23 -05:00
|
|
|
{
|
2015-04-17 17:32:02 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 12:01:23 -05:00
|
|
|
NSInteger state;
|
|
|
|
|
|
|
|
state = NSOnState;
|
|
|
|
if (!checked)
|
|
|
|
state = NSOffState;
|
2015-04-17 17:32:02 -05:00
|
|
|
[c->checkbox setState:state];
|
2015-04-09 12:01:23 -05:00
|
|
|
}
|
2015-04-16 12:51:46 -05:00
|
|
|
|
|
|
|
uiCheckbox *uiNewCheckbox(const char *text)
|
|
|
|
{
|
2015-04-17 17:40:12 -05:00
|
|
|
struct checkbox *c;
|
2015-04-16 12:51:46 -05:00
|
|
|
|
2015-04-17 17:40:12 -05:00
|
|
|
c = uiNew(struct checkbox);
|
2015-04-16 12:51:46 -05:00
|
|
|
|
2015-05-02 19:51:00 -05:00
|
|
|
uiDarwinMakeControl(uiControl(c), [NSButton class], NO, NO, destroy, c);
|
2015-04-16 12:51:46 -05:00
|
|
|
|
2015-04-29 09:28:17 -05:00
|
|
|
c->checkbox = (NSButton *) uiControlHandle(uiControl(c));
|
2015-04-16 12:51:46 -05:00
|
|
|
|
2015-04-17 17:54:17 -05:00
|
|
|
[c->checkbox setTitle:toNSString(text)];
|
2015-04-17 17:32:02 -05:00
|
|
|
[c->checkbox setButtonType:NSSwitchButton];
|
|
|
|
[c->checkbox setBordered:NO];
|
|
|
|
setStandardControlFont(c->checkbox);
|
2015-04-16 12:51:46 -05:00
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
c->delegate = [uipCheckboxDelegate new];
|
|
|
|
[c->checkbox setTarget:c->delegate];
|
|
|
|
[c->checkbox setAction:@selector(checkboxToggled:)];
|
|
|
|
[c->delegate setCheckbox:uiCheckbox(c)];
|
|
|
|
[c->delegate setOnToggled:defaultOnToggled data:NULL];
|
2015-04-16 12:51:46 -05:00
|
|
|
|
|
|
|
uiCheckbox(c)->Text = checkboxText;
|
|
|
|
uiCheckbox(c)->SetText = checkboxSetText;
|
|
|
|
uiCheckbox(c)->OnToggled = checkboxOnToggled;
|
|
|
|
uiCheckbox(c)->Checked = checkboxChecked;
|
|
|
|
uiCheckbox(c)->SetChecked = checkboxSetChecked;
|
|
|
|
|
2015-04-17 17:32:02 -05:00
|
|
|
return uiCheckbox(c);
|
2015-04-16 12:51:46 -05:00
|
|
|
}
|