summaryrefslogtreecommitdiff
path: root/button.go
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2018-08-26 13:24:47 -0400
committerPietro Gagliardi <[email protected]>2018-08-26 13:24:47 -0400
commit2bc76219286dfe39949772ceee4dbd9560ec2c1f (patch)
tree28ae5f6f24c4a12ce349bc39490a7dda068d087a /button.go
parent809662459dcd2cbe0b42f338413b88fea0483086 (diff)
Migrated window.go, box.go, button.go, and checkbox.go back.
Diffstat (limited to 'button.go')
-rw-r--r--button.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/button.go b/button.go
new file mode 100644
index 0000000..9e39918
--- /dev/null
+++ b/button.go
@@ -0,0 +1,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)
+ }
+}