2015-08-13 22:38:17 -05:00
|
|
|
// 13 august 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
|
|
|
struct uiButton {
|
|
|
|
uiDarwinControl c;
|
|
|
|
NSButton *button;
|
|
|
|
void (*onClicked)(uiButton *, void *);
|
|
|
|
void *onClickedData;
|
|
|
|
};
|
|
|
|
|
|
|
|
@interface buttonDelegateClass : NSObject {
|
2016-01-07 13:41:20 -06:00
|
|
|
struct mapTable *buttons;
|
2015-08-13 22:38:17 -05:00
|
|
|
}
|
|
|
|
- (IBAction)onClicked:(id)sender;
|
|
|
|
- (void)registerButton:(uiButton *)b;
|
|
|
|
- (void)unregisterButton:(uiButton *)b;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation buttonDelegateClass
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
2015-08-17 00:29:34 -05:00
|
|
|
self->buttons = newMap();
|
2015-08-13 22:38:17 -05:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2016-01-07 13:41:20 -06:00
|
|
|
mapDestroy(self->buttons);
|
2015-08-13 22:38:17 -05:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)onClicked:(id)sender
|
|
|
|
{
|
|
|
|
uiButton *b;
|
|
|
|
|
2015-08-17 00:29:34 -05:00
|
|
|
b = (uiButton *) mapGet(self->buttons, sender);
|
2015-08-13 22:38:17 -05:00
|
|
|
(*(b->onClicked))(b, b->onClickedData);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerButton:(uiButton *)b
|
|
|
|
{
|
2015-08-17 00:29:34 -05:00
|
|
|
mapSet(self->buttons, b->button, b);
|
2015-08-13 22:38:17 -05:00
|
|
|
[b->button setTarget:self];
|
|
|
|
[b->button setAction:@selector(onClicked:)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)unregisterButton:(uiButton *)b
|
|
|
|
{
|
|
|
|
[b->button setTarget:nil];
|
2016-01-07 13:41:20 -06:00
|
|
|
mapDelete(self->buttons, b->button);
|
2015-08-13 22:38:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static buttonDelegateClass *buttonDelegate = nil;
|
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinControlAllDefaultsExceptDestroy(uiButton, button)
|
|
|
|
|
2016-04-25 11:52:54 -05:00
|
|
|
static void uiButtonDestroy(uiControl *c)
|
2016-04-25 10:54:09 -05:00
|
|
|
{
|
2016-04-25 11:52:54 -05:00
|
|
|
uiButton *b = uiButton(c);
|
|
|
|
|
|
|
|
[buttonDelegate unregisterButton:b];
|
|
|
|
[b->button release];
|
|
|
|
uiFreeControl(uiControl(b));
|
2016-04-25 10:54:09 -05:00
|
|
|
}
|
2015-08-13 22:38:17 -05:00
|
|
|
|
|
|
|
char *uiButtonText(uiButton *b)
|
|
|
|
{
|
|
|
|
return uiDarwinNSStringToText([b->button title]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiButtonSetText(uiButton *b, const char *text)
|
|
|
|
{
|
|
|
|
[b->button setTitle:toNSString(text)];
|
|
|
|
// this may result in the size of the button changing
|
2015-08-18 19:15:09 -05:00
|
|
|
uiDarwinControlTriggerRelayout(uiDarwinControl(b));
|
2015-08-13 22:38:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data)
|
|
|
|
{
|
|
|
|
b->onClicked = f;
|
2015-08-14 20:46:35 -05:00
|
|
|
b->onClickedData = data;
|
2015-08-13 22:38:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultOnClicked(uiButton *b, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
uiButton *uiNewButton(const char *text)
|
|
|
|
{
|
|
|
|
uiButton *b;
|
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinNewControl(uiButton, b);
|
2015-08-13 22:38:17 -05:00
|
|
|
|
|
|
|
b->button = [[NSButton alloc] initWithFrame:NSZeroRect];
|
|
|
|
[b->button setTitle:toNSString(text)];
|
|
|
|
[b->button setButtonType:NSMomentaryPushInButton];
|
|
|
|
[b->button setBordered:YES];
|
|
|
|
[b->button setBezelStyle:NSRoundedBezelStyle];
|
2015-08-14 21:50:20 -05:00
|
|
|
uiDarwinSetControlFont(b->button, NSRegularControlSize);
|
2015-08-13 22:38:17 -05:00
|
|
|
|
2015-08-17 11:57:45 -05:00
|
|
|
if (buttonDelegate == nil) {
|
2015-08-13 22:38:17 -05:00
|
|
|
buttonDelegate = [buttonDelegateClass new];
|
2015-08-17 11:57:45 -05:00
|
|
|
[delegates addObject:buttonDelegate];
|
|
|
|
}
|
2015-08-13 22:38:17 -05:00
|
|
|
[buttonDelegate registerButton:b];
|
|
|
|
uiButtonOnClicked(b, defaultOnClicked, NULL);
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|