andlabs-ui/combobox.go

65 lines
1.6 KiB
Go
Raw Normal View History

2015-12-12 15:18:58 -06:00
// 12 december 2015
package ui
import (
"unsafe"
)
2018-08-26 12:33:54 -05:00
// #include "pkgui.h"
2015-12-12 15:18:58 -06:00
import "C"
// Combobox is a Control that represents a drop-down list of strings
// that the user can choose one of at any time. For a Combobox that
// users can type values into, see EditableCombobox.
2015-12-12 15:18:58 -06:00
type Combobox struct {
2018-08-11 18:52:29 -05:00
ControlBase
2015-12-12 15:18:58 -06:00
c *C.uiCombobox
onSelected func(*Combobox)
}
// NewCombobox creates a new Combobox.
func NewCombobox() *Combobox {
c := new(Combobox)
c.c = C.uiNewCombobox()
2018-08-26 12:33:54 -05:00
C.pkguiComboboxOnSelected(c.c)
2015-12-12 15:18:58 -06:00
2018-08-11 18:52:29 -05:00
c.ControlBase = NewControlBase(c, uintptr(unsafe.Pointer(c.c)))
2015-12-12 15:18:58 -06:00
return c
}
// Append adds the named item to the end of the Combobox.
func (c *Combobox) Append(text string) {
ctext := C.CString(text)
C.uiComboboxAppend(c.c, ctext)
freestr(ctext)
}
// Selected returns the index of the currently selected item in the
// Combobox, or -1 if nothing is selected.
func (c *Combobox) Selected() int {
return int(C.uiComboboxSelected(c.c))
}
2018-08-11 20:29:20 -05:00
// SetSelected sets the currently selected item in the Combobox
2015-12-12 15:18:58 -06:00
// to index. If index is -1 no item will be selected.
func (c *Combobox) SetSelected(index int) {
2018-08-11 20:29:20 -05:00
C.uiComboboxSetSelected(c.c, C.int(index))
2015-12-12 15:18:58 -06:00
}
// OnSelected registers f to be run when the user selects an item in
// the Combobox. Only one function can be registered at a time.
func (c *Combobox) OnSelected(f func(*Combobox)) {
c.onSelected = f
}
2018-08-26 12:33:54 -05:00
//export pkguiDoComboboxOnSelected
func pkguiDoComboboxOnSelected(cc *C.uiCombobox, data unsafe.Pointer) {
2018-08-11 18:52:29 -05:00
c := ControlFromLibui(uintptr(unsafe.Pointer(cc))).(*Combobox)
2015-12-12 15:18:58 -06:00
if c.onSelected != nil {
c.onSelected(c)
}
}