libui/darwin/button.m

87 lines
1.7 KiB
Mathematica
Raw Normal View History

// 7 april 2015
#import "uipriv_darwin.h"
@interface uiNSButton : NSButton
2015-04-16 12:19:43 -05:00
@property uiButton *uiB;
@property void (*uiOnClicked)(uiButton *, void *);
@property void *uiOnClickedData;
@end
@implementation uiNSButton
- (void)viewDidMoveToSuperview
{
if (uiDarwinControlFreeWhenAppropriate(uiControl(self.uiB), [self superview])) {
[self setTarget:nil];
2015-04-16 12:19:43 -05:00
self.uiB = NULL;
}
[super viewDidMoveToSuperview];
}
- (IBAction)uiButtonClicked:(id)sender
{
2015-04-16 12:19:43 -05:00
(*(self.uiOnClicked))(self.uiB, self.uiOnClickedData);
}
@end
2015-04-16 12:19:43 -05:00
static void defaultOnClicked(uiButton *c, void *data)
{
// do nothing
}
2015-04-16 12:19:43 -05:00
static char *buttonText(uiButton *bb)
{
uiNSButton *b;
2015-04-16 12:19:43 -05:00
b = (uiNSButton *) uiControlHandle(uiControl(bb));
return uiDarwinNSStringToText([b title]);
}
2015-04-16 12:19:43 -05:00
static void buttonSetText(uiButton *bb, const char *text)
{
uiNSButton *b;
2015-04-16 12:19:43 -05:00
b = (uiNSButton *) uiControlHandle(uiControl(bb));
[b setTitle:toNSString(text)];
}
2015-04-16 12:19:43 -05:00
static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data)
{
uiNSButton *b;
2015-04-16 12:19:43 -05:00
b = (uiNSButton *) uiControlHandle(uiControl(bb));
b.uiOnClicked = f;
b.uiOnClickedData = data;
}
2015-04-16 12:19:43 -05:00
uiButton *uiNewButton(const char *text)
{
uiButton *b;
uiNSButton *bb;
b = uiNew(uiButton);
uiDarwinNewControl(uiControl(b), [uiNSButton class], NO, NO);
bb = (uiNSButton *) uiControlHandle(uiControl(b));
[bb setTitle:toNSString(text)];
[bb setButtonType:NSMomentaryPushInButton];
[bb setBordered:YES];
[bb setBezelStyle:NSRoundedBezelStyle];
setStandardControlFont((NSControl *) bb);
[bb setTarget:bb];
[bb setAction:@selector(uiButtonClicked:)];
bb.uiOnClicked = defaultOnClicked;
uiButton(b)->Text = buttonText;
uiButton(b)->SetText = buttonSetText;
uiButton(b)->OnClicked = buttonOnClicked;
bb.uiB = b;
return b.uiB;
}