-
Notifications
You must be signed in to change notification settings - Fork 17
/
button.m
105 lines (89 loc) · 3.64 KB
/
button.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
#import "button.h"
#include "_cgo_export.h"
@implementation ButtonHandler
-(void) buttonClicked:(id) sender
{
onButtonClicked([self goButtonID]);
}
@end
ButtonPtr Button_New(int goButtonID, int x, int y, int w, int h) {
id nsButton = [[[NSButton alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease];
ButtonHandler* handler = [[ButtonHandler alloc] init];
[handler setGoButtonID:goButtonID];
[handler autorelease];
[nsButton setTarget:handler];
[nsButton setAction:@selector(buttonClicked:)];
[nsButton setButtonType:NSButtonTypeMomentaryLight];
[nsButton setBezelStyle:NSBezelStyleRounded];
return (ButtonPtr)nsButton;
}
void Button_SetTitle(ButtonPtr btnPtr, const char* title) {
NSButton* button = (NSButton*)btnPtr;
[button setTitle:[NSString stringWithUTF8String:title]];
}
const char* Button_Title(ButtonPtr btnPtr) {
NSButton* button = (NSButton*)btnPtr;
return [[button title] cStringUsingEncoding:NSASCIIStringEncoding];
}
void Button_SetButtonType(ButtonPtr btnPtr, int buttonType) {
NSButton* button = (NSButton*)btnPtr;
[button setButtonType:buttonType];
}
void Button_SetBezelStyle(ButtonPtr btnPtr, int bezelStyle) {
NSButton* button = (NSButton*)btnPtr;
[button setBezelStyle:bezelStyle];
}
void Button_SetFontFamily(ButtonPtr btnPtr, const char* fontFamily) {
NSButton* button = (NSButton*)btnPtr;
button.font = [NSFont fontWithName:[NSString stringWithUTF8String:fontFamily] size:button.font.pointSize];
}
void Button_SetFontSize(ButtonPtr btnPtr, int fontSize) {
NSButton* button = (NSButton*)btnPtr;
button.font = [NSFont fontWithName:button.font.fontName size:fontSize];
}
void Button_SetColor(ButtonPtr btnPtr, int r, int g, int b, int a) {
NSButton* button = (NSButton*)btnPtr;
NSMutableAttributedString *attrTitle =[[NSMutableAttributedString alloc] initWithString:[NSString stringWithUTF8String:Button_Title(btnPtr)]];
NSUInteger len = [attrTitle length];
NSRange range = NSMakeRange(0, len);
[attrTitle addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithCalibratedRed:r/255.f green:g/255.f blue:b/255.f alpha:a/255.f] range:range];
[attrTitle addAttribute:NSFontAttributeName value:button.font range:range];
[attrTitle fixAttributesInRange:range];
[button setAttributedTitle:attrTitle];
}
void Button_SetBackgroundColor(ButtonPtr btnPtr, int r, int g, int b, int a) {
NSButton* button = (NSButton*)btnPtr;
[button setBordered:false]; // required, otherwise can't set background
[[button cell] setBackgroundColor:[NSColor colorWithCalibratedRed:r/255.f green:g/255.f blue:b/255.f alpha:a/255.f]];
}
void Button_SetBorderColor(ButtonPtr btnPtr, int r, int g, int b, int a) {
NSButton* button = (NSButton*)btnPtr;
button.wantsLayer = true;
button.layer.borderColor = [[NSColor colorWithCalibratedRed:r/255.f green:g/255.f blue:b/255.f alpha:a/255.f] CGColor];
}
void Button_SetBorderWidth(ButtonPtr btnPtr, int borderWidth) {
NSButton* button = (NSButton*)btnPtr;
button.wantsLayer = true;
button.layer.borderWidth = borderWidth;
}
void Button_SetState(ButtonPtr btnPtr, int state) {
NSButton* button = (NSButton*)btnPtr;
button.wantsLayer = true;
if(state == 0) {
button.state = NSControlStateValueOff;
} else if(state == 1) {
button.state = NSControlStateValueOn;
} else {
button.state = NSControlStateValueMixed;
}
}
int Button_State(ButtonPtr btnPtr) {
NSButton* button = (NSButton*)btnPtr;
if(button.state == NSControlStateValueOn) {
return 1;
} else if(button.state == NSControlStateValueOff) {
return 0;
} else {
return 3;
}
}