2015-04-07 15:12:28 -05:00
|
|
|
// 7 april 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
@interface uipButtonDelegate : NSObject {
|
|
|
|
uiButton *b;
|
|
|
|
void (*onClicked)(uiButton *, void *);
|
|
|
|
void *onClickedData;
|
|
|
|
}
|
|
|
|
- (IBAction)buttonClicked:(id)sender;
|
|
|
|
- (void)setOnClicked:(void (*)(uiButton *, void *))f data:(void *)data;
|
2015-04-08 01:28:42 -05:00
|
|
|
@end
|
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
@implementation uipButtonDelegate
|
|
|
|
|
|
|
|
uiLogObjCClassAllocations
|
2015-04-08 01:28:42 -05:00
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
- (IBAction)buttonClicked:(id)sender
|
2015-04-08 03:35:50 -05:00
|
|
|
{
|
2015-04-17 17:20:14 -05:00
|
|
|
(*(self->onClicked))(self->b, self->onClickedData);
|
2015-04-08 03:35:50 -05:00
|
|
|
}
|
2015-04-08 01:28:42 -05:00
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
- (void)setOnClicked:(void (*)(uiButton *, void *))f data:(void *)data
|
2015-04-07 15:12:28 -05:00
|
|
|
{
|
2015-04-17 17:20:14 -05:00
|
|
|
self->onClicked = f;
|
|
|
|
self->onClickedData = data;
|
2015-04-07 15:12:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
struct button {
|
|
|
|
uiButton b;
|
|
|
|
NSButton *button;
|
|
|
|
uipButtonDelegate *delegate;
|
|
|
|
};
|
|
|
|
|
2015-04-16 12:19:43 -05:00
|
|
|
static void defaultOnClicked(uiButton *c, void *data)
|
2015-04-07 15:12:28 -05:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
static void destroy(void *data)
|
|
|
|
{
|
|
|
|
struct button *b = (struct button *) bb;
|
|
|
|
|
|
|
|
[b->button setTarget:nil];
|
|
|
|
[b->delegate release];
|
|
|
|
uiFree(b);
|
|
|
|
}
|
|
|
|
|
2015-04-16 12:19:43 -05:00
|
|
|
static char *buttonText(uiButton *bb)
|
2015-04-07 15:12:28 -05:00
|
|
|
{
|
2015-04-17 17:20:14 -05:00
|
|
|
struct button *b = (struct button *) bb;
|
2015-04-07 15:12:28 -05:00
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
return uiDarwinNSStringToText([b->button title]);
|
2015-04-09 10:54:02 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 12:19:43 -05:00
|
|
|
static void buttonSetText(uiButton *bb, const char *text)
|
2015-04-09 10:54:02 -05:00
|
|
|
{
|
2015-04-17 17:20:14 -05:00
|
|
|
struct button *b = (struct button *) bb;
|
2015-04-09 10:54:02 -05:00
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
[b->button setTitle:toNSString(text)];
|
2015-04-09 10:54:02 -05:00
|
|
|
}
|
2015-04-07 15:12:28 -05:00
|
|
|
|
2015-04-16 12:19:43 -05:00
|
|
|
static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data)
|
2015-04-07 15:12:28 -05:00
|
|
|
{
|
2015-04-17 17:20:14 -05:00
|
|
|
struct button *b = (struct button *) bb;
|
2015-04-07 15:12:28 -05:00
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
[b->delegate setOnClicked:f data:data];
|
2015-04-07 15:12:28 -05:00
|
|
|
}
|
2015-04-16 12:19:43 -05:00
|
|
|
|
|
|
|
uiButton *uiNewButton(const char *text)
|
|
|
|
{
|
|
|
|
uiButton *b;
|
|
|
|
|
|
|
|
b = uiNew(uiButton);
|
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
uiDarwinNewControl(uiControl(b), [uiNSButton class], NO, NO, destroy, b);
|
2015-04-16 12:19:43 -05:00
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
b->button = (NSButton *) VIEW(b);
|
2015-04-16 12:19:43 -05:00
|
|
|
|
2015-04-17 17:20:14 -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-04-17 17:20:14 -05:00
|
|
|
b->delegate = [uipButtonDelegate new];
|
|
|
|
[b->button setTarget:b->delegate];
|
|
|
|
[b->button setAction:@selector(buttonClicked:)];
|
|
|
|
[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;
|
|
|
|
|
2015-04-17 17:20:14 -05:00
|
|
|
return uiButton(b);
|
2015-04-16 12:19:43 -05:00
|
|
|
}
|