forked from mojbro/gocoa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
button.go
159 lines (134 loc) · 4.69 KB
/
button.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gocoa
// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Cocoa
// #import "button.h"
// #import "image.h"
// #include <stdlib.h>
import "C"
import (
"fmt"
"image"
"unsafe"
)
// Button represents a button control that can trigger actions.
type Button struct {
buttonPtr C.ButtonPtr
callback func()
}
type ButtonType int32
type ButtonBezelStyle int32
type ButtonState int32
const (
ButtonTypeMomentaryPushIn ButtonType = 7
ButtonTypeMomentaryLight ButtonType = 0
ButtonTypeMomentaryChange ButtonType = 5
ButtonTypePushOnPushOff ButtonType = 1
ButtonTypeOnOff ButtonType = 6
ButtonTypeToggle ButtonType = 2
ButtonTypeSwitch ButtonType = 3
ButtonTypeRadio ButtonType = 4
ButtonTypeAccelerator ButtonType = 8
ButtonTypeMultiLevelAccelerator ButtonType = 9
)
const (
ButtonBezelStyleRounded ButtonBezelStyle = 1
ButtonBezelStyleRegularSquare ButtonBezelStyle = 2
ButtonBezelStyleShadowlessSquare ButtonBezelStyle = 6
ButtonBezelStyleSmallSquare ButtonBezelStyle = 10
ButtonBezelStyleRoundRect ButtonBezelStyle = 12
ButtonBezelStyleInline ButtonBezelStyle = 15
ButtonBezelStyleRecessed ButtonBezelStyle = 13
ButtonBezelStyleDisclosure ButtonBezelStyle = 5
ButtonBezelStyleRoundedDisclosure ButtonBezelStyle = 14
ButtonBezelStyleCircular ButtonBezelStyle = 7
ButtonBezelStyleHelpButton ButtonBezelStyle = 9
ButtonBezelStyleTexturedRounded ButtonBezelStyle = 11
ButtonBezelStyleTexturedSquare ButtonBezelStyle = 8
)
const (
ButtonStateValueOff ButtonState = 0
ButtonStateValueOn ButtonState = 1
ButtonStateValueMixed ButtonState = 2
)
var buttons []*Button
//export onButtonClicked
func onButtonClicked(id C.int) {
buttonID := int(id)
if buttonID < len(buttons) && buttons[buttonID].callback != nil {
buttons[buttonID].callback()
}
}
// NewButton constructs a new button at position (x, y) and with size (width x height).
// Gotcha! It is currently hard-coded to quit the app when the button is being pressed, until
// callbacks have been implemented.
// This func is not thread safe.
func NewButton(x int, y int, width int, height int) *Button {
buttonID := len(buttons)
buttonPtr := C.Button_New(C.int(buttonID), C.int(x), C.int(y), C.int(width), C.int(height))
btn := &Button{
buttonPtr: buttonPtr,
}
buttons = append(buttons, btn)
return btn
}
// SetTitle sets the title of the button, which is the text displayed on the button.
func (btn *Button) SetTitle(title string) {
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
C.Button_SetTitle(btn.buttonPtr, cTitle)
}
func (btn *Button) Title() string {
return C.GoString(C.Button_Title(btn.buttonPtr))
}
func (btn *Button) SetButtonType(buttonType ButtonType) {
C.Button_SetButtonType(btn.buttonPtr, C.int(buttonType))
}
func (btn *Button) SetBezelStyle(bezelStyle ButtonBezelStyle) {
C.Button_SetBezelStyle(btn.buttonPtr, C.int(bezelStyle))
}
func (btn *Button) SetFontFamily(fontFamily string) {
cText := C.CString(fontFamily)
defer C.free(unsafe.Pointer(cText))
C.Button_SetFontFamily(btn.buttonPtr, cText)
}
func (btn *Button) SetFontSize(fontSize int) {
C.Button_SetFontSize(btn.buttonPtr, C.int(fontSize))
}
func (btn *Button) SetColor(hexRGBA string) {
var r, g, b, a = 0, 0, 0, 0
fmt.Sscanf(hexRGBA, "#%02x%02x%02x%02x", &r, &g, &b, &a)
C.Button_SetColor(btn.buttonPtr, C.int(r), C.int(g), C.int(b), C.int(a))
}
func (btn *Button) SetBackgroundColor(hexRGBA string) {
var r, g, b, a = 0, 0, 0, 0
fmt.Sscanf(hexRGBA, "#%02x%02x%02x%02x", &r, &g, &b, &a)
C.Button_SetBackgroundColor(btn.buttonPtr, C.int(r), C.int(g), C.int(b), C.int(a))
}
func (btn *Button) SetBorderColor(hexRGBA string) {
var r, g, b, a = 0, 0, 0, 0
fmt.Sscanf(hexRGBA, "#%02x%02x%02x%02x", &r, &g, &b, &a)
C.Button_SetBorderColor(btn.buttonPtr, C.int(r), C.int(g), C.int(b), C.int(a))
}
func (btn *Button) SetBorderWidth(borderWidth int) {
C.Button_SetBorderWidth(btn.buttonPtr, C.int(borderWidth))
}
func (btn *Button) SetState(state ButtonState) {
C.Button_SetState(btn.buttonPtr, C.int(state))
}
func (btn *Button) State() ButtonState {
return ButtonState(int(C.Button_State(btn.buttonPtr)))
}
// OnClick - function, that will be triggered, if the button is clicked.
func (btn *Button) OnClick(fn func()) {
btn.callback = fn
}
// Remove - removes a button from the parent view
func (btn *Button) Remove() {
C.Button_Remove(btn.buttonPtr)
}
func (btn *Button) SetImage(img *image.RGBA) {
bytes := C.CBytes(img.Pix)
nsImage := C.Image_NewWithRGBA(C.int(img.Bounds().Dx()), C.int(img.Bounds().Dy()), (*C.uchar)(bytes))
C.Button_SetImage(btn.buttonPtr, nsImage)
C.free(bytes)
}