summaryrefslogtreecommitdiff
path: root/new/darwin/button.m
blob: 3ec8a549309af2c399b74094b7464c0b06bb8458 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 7 april 2015
#import "uipriv_darwin.h"

@interface uiNSButton : NSButton
@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];
		self.uiB = NULL;
	}
	[super viewDidMoveToSuperview];
}

- (IBAction)uiButtonClicked:(id)sender
{
	(*(self.uiOnClicked))(self.uiB, self.uiOnClickedData);
}

@end

static void defaultOnClicked(uiButton *c, void *data)
{
	// do nothing
}

static char *buttonText(uiButton *bb)
{
	uiNSButton *b;

	b = (uiNSButton *) uiControlHandle(uiControl(bb));
	return uiDarwinNSStringToText([b title]);
}

static void buttonSetText(uiButton *bb, const char *text)
{
	uiNSButton *b;

	b = (uiNSButton *) uiControlHandle(uiControl(bb));
	[b setTitle:toNSString(text)];
}

static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data)
{
	uiNSButton *b;

	b = (uiNSButton *) uiControlHandle(uiControl(bb));
	b.uiOnClicked = f;
	b.uiOnClickedData = data;
}

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 bb.uiB;
}