aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/darwin/checkbox.m
blob: dd1ce09357a85ab8bbac7c3379e2c241ca5d70a3 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 14 august 2015
#import "uipriv_darwin.h"

struct uiCheckbox {
	uiDarwinControl c;
	NSButton *button;
	void (*onToggled)(uiCheckbox *, void *);
	void *onToggledData;
};

@interface checkboxDelegateClass : NSObject {
	struct mapTable *buttons;
}
- (IBAction)onToggled:(id)sender;
- (void)registerCheckbox:(uiCheckbox *)c;
- (void)unregisterCheckbox:(uiCheckbox *)c;
@end

@implementation checkboxDelegateClass

- (id)init
{
	self = [super init];
	if (self)
		self->buttons = newMap();
	return self;
}

- (void)dealloc
{
	mapDestroy(self->buttons);
	[super dealloc];
}

- (IBAction)onToggled:(id)sender
{
	uiCheckbox *c;

	c = (uiCheckbox *) mapGet(self->buttons, sender);
	(*(c->onToggled))(c, c->onToggledData);
}

- (void)registerCheckbox:(uiCheckbox *)c
{
	mapSet(self->buttons, c->button, c);
	[c->button setTarget:self];
	[c->button setAction:@selector(onToggled:)];
}

- (void)unregisterCheckbox:(uiCheckbox *)c
{
	[c->button setTarget:nil];
	mapDelete(self->buttons, c->button);
}

@end

static checkboxDelegateClass *checkboxDelegate = nil;

uiDarwinControlAllDefaultsExceptDestroy(uiCheckbox, button)

static void uiCheckboxDestroy(uiControl *cc)
{
	uiCheckbox *c = uiCheckbox(cc);

	[checkboxDelegate unregisterCheckbox:c];
	[c->button release];
	uiFreeControl(uiControl(c));
}

char *uiCheckboxText(uiCheckbox *c)
{
	return uiDarwinNSStringToText([c->button title]);
}

void uiCheckboxSetText(uiCheckbox *c, const char *text)
{
	[c->button setTitle:toNSString(text)];
}

void uiCheckboxOnToggled(uiCheckbox *c, void (*f)(uiCheckbox *, void *), void *data)
{
	c->onToggled = f;
	c->onToggledData = data;
}

int uiCheckboxChecked(uiCheckbox *c)
{
	return [c->button state] == NSOnState;
}

void uiCheckboxSetChecked(uiCheckbox *c, int checked)
{
	NSInteger state;

	state = NSOnState;
	if (!checked)
		state = NSOffState;
	[c->button setState:state];
}

static void defaultOnToggled(uiCheckbox *c, void *data)
{
	// do nothing
}

uiCheckbox *uiNewCheckbox(const char *text)
{
	uiCheckbox *c;

	uiDarwinNewControl(uiCheckbox, c);

	c->button = [[NSButton alloc] initWithFrame:NSZeroRect];
	[c->button setTitle:toNSString(text)];
	[c->button setButtonType:NSSwitchButton];
	// doesn't seem to have an associated bezel style
	[c->button setBordered:NO];
	[c->button setTransparent:NO];
	uiDarwinSetControlFont(c->button, NSRegularControlSize);

	if (checkboxDelegate == nil) {
		checkboxDelegate = [[checkboxDelegateClass new] autorelease];
		[delegates addObject:checkboxDelegate];
	}
	[checkboxDelegate registerCheckbox:c];
	uiCheckboxOnToggled(c, defaultOnToggled, NULL);

	return c;
}