libui/redo/reredo/darwin/checkbox.m

129 lines
2.7 KiB
Mathematica
Raw Normal View History

2015-08-14 20:46:35 -05:00
// 14 august 2015
#import "uipriv_darwin.h"
struct uiCheckbox {
uiDarwinControl c;
NSButton *button; // TODO rename to checkbox?
void (*onToggled)(uiCheckbox *, void *);
void *onToggledData;
};
@interface checkboxDelegateClass : NSObject {
NSMapTable *buttons; // TODO rename to checkboxes?
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)
self->buttons = newMap();
2015-08-14 20:46:35 -05:00
return self;
}
// TODO have this called
- (void)dealloc
{
if ([self->buttons count] != 0)
complain("attempt to destroy shared checkbox delegate but checkboxes are still registered to it");
[self->buttons release];
[super dealloc];
}
- (IBAction)onToggled:(id)sender
{
uiCheckbox *c;
c = (uiCheckbox *) mapGet(self->buttons, sender);
2015-08-14 20:46:35 -05:00
(*(c->onToggled))(c, c->onToggledData);
}
- (void)registerCheckbox:(uiCheckbox *)c
{
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];
[self->buttons removeObjectForKey:c->button];
}
@end
static checkboxDelegateClass *checkboxDelegate = nil;
uiDarwinDefineControlWithOnDestroy(
uiCheckbox, // type name
uiCheckboxType, // type function
button, // handle
[checkboxDelegate unregisterCheckbox:this]; // on destroy
)
char *uiCheckboxText(uiCheckbox *c)
{
return uiDarwinNSStringToText([c->button title]);
}
void uiCheckboxSetText(uiCheckbox *c, const char *text)
{
[c->button setTitle:toNSString(text)];
// this may result in the size of the checkbox changing
//TODO uiControlQueueResize(uiControl(c));
2015-08-14 20:46:35 -05:00
}
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;
c = (uiCheckbox *) uiNewControl(uiCheckboxType());
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
if (checkboxDelegate == nil)
checkboxDelegate = [checkboxDelegateClass new];
[checkboxDelegate registerCheckbox:c];
uiCheckboxOnToggled(c, defaultOnToggled, NULL);
uiDarwinFinishNewControl(c, uiCheckbox);
return c;
}