-
Notifications
You must be signed in to change notification settings - Fork 50
/
checkbox.m
105 lines (80 loc) · 2.04 KB
/
checkbox.m
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
// 14 august 2015
#import "uipriv_darwin.h"
struct uiCheckbox {
uiDarwinControl c;
NSButton *button;
void (*onToggled)(uiCheckbox *, void *);
void *onToggledData;
};
@interface uiprivCheckbox : NSButton<NSDraggingDestination> {
uiCheckbox *checkbox;
}
- (id)initWithFrame:(NSRect)frame uiCheckbox:(uiCheckbox *)c;
- (IBAction)onToggled:(id)sender;
@end
@implementation uiprivCheckbox
uiDarwinDragDestinationMethods(checkbox)
- (id)initWithFrame:(NSRect)frame uiCheckbox:(uiCheckbox *)c
{
self = [super initWithFrame:frame];
if (self) {
self->checkbox = c;
[self setButtonType:NSSwitchButton];
[self setBordered:NO];
// doesn't seem to have an associated bezel style
[self setTransparent:NO];
[self setTarget:self];
[self setAction:@selector(onToggled:)];
}
return self;
}
- (IBAction)onToggled:(id)sender
{
uiCheckbox *c = self->checkbox;
(*(c->onToggled))(c, c->onToggledData);
}
@end
uiDarwinControlAllDefaultsExceptDestroy(uiCheckbox, button)
static void uiCheckboxDestroy(uiControl *cc)
{
uiCheckbox *c = uiCheckbox(cc);
[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:uiprivToNSString(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 = (checked) ? NSOnState : 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 = [[uiprivCheckbox alloc] initWithFrame:NSZeroRect uiCheckbox:c];
uiCheckboxSetText(c, text);
uiDarwinSetControlFont(c->button, NSRegularControlSize);
uiCheckboxOnToggled(c, defaultOnToggled, NULL);
return c;
}