summaryrefslogtreecommitdiff
path: root/BBB_GOFILES/radiobuttons.go
blob: 2413544b09a336dab50c9255a4d041adc72ac878 (plain)
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
// 12 december 2015

package ui

import (
	"unsafe"
)

// #include "ui.h"
// extern void doRadioButtonsOnSelected(uiRadioButtons *, void *);
// // see golang/go#19835
// typedef void (*radioButtonsCallback)(uiRadioButtons *, void *);
import "C"

// RadioButtons is a Control that represents a set of checkable
// buttons from which exactly one may be chosen by the user.
type RadioButtons struct {
	ControlBase
	r	*C.uiRadioButtons
	onSelected	func(*RadioButtons)
}

// NewRadioButtons creates a new RadioButtons.
func NewRadioButtons() *RadioButtons {
	r := new(RadioButtons)

	r.r = C.uiNewRadioButtons()

	C.uiRadioButtonsOnSelected(r.r, C.radioButtonsCallback(C.doRadioButtonsOnSelected), nil)

	r.ControlBase = NewControlBase(r, uintptr(unsafe.Pointer(r.r)))
	return r
}

// Append adds the named button to the end of the RadioButtons.
func (r *RadioButtons) Append(text string) {
	ctext := C.CString(text)
	C.uiRadioButtonsAppend(r.r, ctext)
	freestr(ctext)
}

// Selected returns the index of the currently selected option in the
// RadioButtons, or -1 if no item is selected.
func (r *RadioButtons) Selected() int {
	return int(C.uiRadioButtonsSelected(r.r))
}

// SetSelected sets the currently selected option in the RadioButtons
// to index.
func (r *RadioButtons) SetSelected(index int) {
	C.uiRadioButtonsSetSelected(r.r, C.int(index))
}

// OnSelected registers f to be run when the user selects an option in
// the RadioButtons. Only one function can be registered at a time.
func (r *RadioButtons) OnSelected(f func(*RadioButtons)) {
	r.onSelected = f
}

//export doRadioButtonsOnSelected
func doRadioButtonsOnSelected(rr *C.uiRadioButtons, data unsafe.Pointer) {
	r := ControlFromLibui(uintptr(unsafe.Pointer(rr))).(*RadioButtons)
	if r.onSelected != nil {
		r.onSelected(r)
	}
}