-
Notifications
You must be signed in to change notification settings - Fork 648
/
button.go
62 lines (50 loc) · 1.32 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
// 12 december 2015
package ui
import (
"unsafe"
)
// #include "pkgui.h"
import "C"
// Button is a Control that represents a button that the user can
// click to perform an action. A Button has a text label that should
// describe what the button does.
type Button struct {
ControlBase
b *C.uiButton
onClicked func(*Button)
}
// NewButton creates a new Button with the given text as its label.
func NewButton(text string) *Button {
b := new(Button)
ctext := C.CString(text)
b.b = C.uiNewButton(ctext)
freestr(ctext)
C.pkguiButtonOnClicked(b.b)
b.ControlBase = NewControlBase(b, uintptr(unsafe.Pointer(b.b)))
return b
}
// Text returns the Button's text.
func (b *Button) Text() string {
ctext := C.uiButtonText(b.b)
text := C.GoString(ctext)
C.uiFreeText(ctext)
return text
}
// SetText sets the Button's text to text.
func (b *Button) SetText(text string) {
ctext := C.CString(text)
C.uiButtonSetText(b.b, ctext)
freestr(ctext)
}
// OnClicked registers f to be run when the user clicks the Button.
// Only one function can be registered at a time.
func (b *Button) OnClicked(f func(*Button)) {
b.onClicked = f
}
//export pkguiDoButtonOnClicked
func pkguiDoButtonOnClicked(bb *C.uiButton, data unsafe.Pointer) {
b := ControlFromLibui(uintptr(unsafe.Pointer(bb))).(*Button)
if b.onClicked != nil {
b.onClicked(b)
}
}