libui/darwin/button.m

103 lines
2.1 KiB
Mathematica
Raw Normal View History

// 7 april 2015
#import "uipriv_darwin.h"
2015-05-07 12:48:41 -05:00
@interface buttonDelegate : NSObject {
uiButton *b;
void (*onClicked)(uiButton *, void *);
void *onClickedData;
}
- (IBAction)buttonClicked:(id)sender;
- (void)setButton:(uiButton *)b;
- (void)setOnClicked:(void (*)(uiButton *, void *))f data:(void *)data;
@end
2015-05-07 12:48:41 -05:00
@implementation buttonDelegate
- (IBAction)buttonClicked:(id)sender
{
(*(self->onClicked))(self->b, self->onClickedData);
}
- (void)setButton:(uiButton *)b
{
self->b = b;
}
- (void)setOnClicked:(void (*)(uiButton *, void *))f data:(void *)data
{
self->onClicked = f;
self->onClickedData = data;
}
@end
struct button {
uiButton b;
NSButton *button;
2015-05-07 12:48:41 -05:00
buttonDelegate *delegate;
};
static void defaultOnClicked(uiButton *b, void *data)
{
// do nothing
}
static void destroy(void *data)
{
struct button *b = (struct button *) data;
[b->button setTarget:nil];
[b->delegate release];
uiFree(b);
}
2015-04-16 12:19:43 -05:00
static char *buttonText(uiButton *bb)
{
struct button *b = (struct button *) bb;
return uiDarwinNSStringToText([b->button title]);
}
2015-04-16 12:19:43 -05:00
static void buttonSetText(uiButton *bb, const char *text)
{
struct button *b = (struct button *) bb;
[b->button setTitle:toNSString(text)];
}
2015-04-16 12:19:43 -05:00
static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data)
{
struct button *b = (struct button *) bb;
[b->delegate setOnClicked:f data:data];
}
2015-04-16 12:19:43 -05:00
uiButton *uiNewButton(const char *text)
{
struct button *b;
2015-04-16 12:19:43 -05:00
b = uiNew(struct button);
2015-04-16 12:19:43 -05:00
uiDarwinMakeControl(uiControl(b), [NSButton class], NO, NO, destroy, b);
2015-04-16 12:19:43 -05:00
b->button = (NSButton *) uiControlHandle(uiControl(b));
2015-04-16 12:19:43 -05:00
[b->button setTitle:toNSString(text)];
[b->button setButtonType:NSMomentaryPushInButton];
[b->button setBordered:YES];
[b->button setBezelStyle:NSRoundedBezelStyle];
setStandardControlFont(b->button);
2015-04-16 12:19:43 -05:00
2015-05-07 12:48:41 -05:00
b->delegate = [buttonDelegate new];
[b->button setTarget:b->delegate];
[b->button setAction:@selector(buttonClicked:)];
[b->delegate setButton:uiButton(b)];
[b->delegate setOnClicked:defaultOnClicked data:NULL];
2015-04-16 12:19:43 -05:00
uiButton(b)->Text = buttonText;
uiButton(b)->SetText = buttonSetText;
uiButton(b)->OnClicked = buttonOnClicked;
return uiButton(b);
2015-04-16 12:19:43 -05:00
}