diff options
author | StapleButter <thetotalworm@gmail.com> | 2017-09-09 02:30:51 +0200 |
---|---|---|
committer | StapleButter <thetotalworm@gmail.com> | 2017-09-09 02:30:51 +0200 |
commit | 70e4841d311d68689724768157cc9cbfbde7a9fc (patch) | |
tree | ba9499f77d1258530a7e60aa6e1732c41d98161c /src/libui_sdl/libui/darwin/button.m | |
parent | 81747d6c34eb159481a6ca3f283d065fa3568617 (diff) |
another UI attempt, I guess.
sorry.
Diffstat (limited to 'src/libui_sdl/libui/darwin/button.m')
-rw-r--r-- | src/libui_sdl/libui/darwin/button.m | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/libui_sdl/libui/darwin/button.m b/src/libui_sdl/libui/darwin/button.m new file mode 100644 index 0000000..baccabb --- /dev/null +++ b/src/libui_sdl/libui/darwin/button.m @@ -0,0 +1,113 @@ +// 13 august 2015 +#import "uipriv_darwin.h" + +struct uiButton { + uiDarwinControl c; + NSButton *button; + void (*onClicked)(uiButton *, void *); + void *onClickedData; +}; + +@interface buttonDelegateClass : NSObject { + struct mapTable *buttons; +} +- (IBAction)onClicked:(id)sender; +- (void)registerButton:(uiButton *)b; +- (void)unregisterButton:(uiButton *)b; +@end + +@implementation buttonDelegateClass + +- (id)init +{ + self = [super init]; + if (self) + self->buttons = newMap(); + return self; +} + +- (void)dealloc +{ + mapDestroy(self->buttons); + [super dealloc]; +} + +- (IBAction)onClicked:(id)sender +{ + uiButton *b; + + b = (uiButton *) mapGet(self->buttons, sender); + (*(b->onClicked))(b, b->onClickedData); +} + +- (void)registerButton:(uiButton *)b +{ + mapSet(self->buttons, b->button, b); + [b->button setTarget:self]; + [b->button setAction:@selector(onClicked:)]; +} + +- (void)unregisterButton:(uiButton *)b +{ + [b->button setTarget:nil]; + mapDelete(self->buttons, b->button); +} + +@end + +static buttonDelegateClass *buttonDelegate = nil; + +uiDarwinControlAllDefaultsExceptDestroy(uiButton, button) + +static void uiButtonDestroy(uiControl *c) +{ + uiButton *b = uiButton(c); + + [buttonDelegate unregisterButton:b]; + [b->button release]; + uiFreeControl(uiControl(b)); +} + +char *uiButtonText(uiButton *b) +{ + return uiDarwinNSStringToText([b->button title]); +} + +void uiButtonSetText(uiButton *b, const char *text) +{ + [b->button setTitle:toNSString(text)]; +} + +void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data) +{ + b->onClicked = f; + b->onClickedData = data; +} + +static void defaultOnClicked(uiButton *b, void *data) +{ + // do nothing +} + +uiButton *uiNewButton(const char *text) +{ + uiButton *b; + + uiDarwinNewControl(uiButton, b); + + b->button = [[NSButton alloc] initWithFrame:NSZeroRect]; + [b->button setTitle:toNSString(text)]; + [b->button setButtonType:NSMomentaryPushInButton]; + [b->button setBordered:YES]; + [b->button setBezelStyle:NSRoundedBezelStyle]; + uiDarwinSetControlFont(b->button, NSRegularControlSize); + + if (buttonDelegate == nil) { + buttonDelegate = [[buttonDelegateClass new] autorelease]; + [delegates addObject:buttonDelegate]; + } + [buttonDelegate registerButton:b]; + uiButtonOnClicked(b, defaultOnClicked, NULL); + + return b; +} |