andlabs-ui/BBB_GOFILES/entry.go

94 lines
2.2 KiB
Go
Raw Normal View History

2015-12-12 15:44:32 -06:00
// 12 december 2015
2015-12-13 01:26:26 -06:00
// TODO typing in entry in OS X crashes libui
// I've had similar issues with checkboxes on libui
// something's wrong with NSMapTable
2015-12-12 15:44:32 -06:00
package ui
import (
"unsafe"
)
// #include "ui.h"
// extern void doEntryOnChanged(uiEntry *, void *);
2018-08-12 10:07:33 -05:00
// // see golang/go#19835
// typedef void (*entryCallback)(uiEntry *, void *);
2015-12-12 15:44:32 -06:00
import "C"
// Entry is a Control that represents a space that the user can
// type a single line of text into.
type Entry struct {
2018-08-11 18:52:29 -05:00
ControlBase
2015-12-12 15:44:32 -06:00
e *C.uiEntry
onChanged func(*Entry)
}
2018-08-11 18:52:29 -05:00
func finishNewEntry(ee *C.uiEntry) *Entry {
2015-12-12 15:44:32 -06:00
e := new(Entry)
2018-08-11 18:52:29 -05:00
e.e = ee
2015-12-12 15:44:32 -06:00
2018-08-12 10:07:33 -05:00
C.uiEntryOnChanged(e.e, C.entryCallback(C.doEntryOnChanged), nil)
2015-12-12 15:44:32 -06:00
2018-08-11 18:52:29 -05:00
e.ControlBase = NewControlBase(e, uintptr(unsafe.Pointer(e.e)))
2015-12-12 15:44:32 -06:00
return e
}
2018-08-11 18:52:29 -05:00
// NewEntry creates a new Entry.
func NewEntry() *Entry {
return finishNewEntry(C.uiNewEntry())
2015-12-12 15:44:32 -06:00
}
2018-08-11 18:52:29 -05:00
// NewPasswordEntry creates a new Entry whose contents are
// visibly obfuscated, suitable for passwords.
func NewPasswordEntry() *Entry {
return finishNewEntry(C.uiNewPasswordEntry())
2015-12-12 15:44:32 -06:00
}
2018-08-11 18:52:29 -05:00
// NewSearchEntry creates a new Entry suitable for searching with.
// Changed events may, depending on the system, be delayed
// with a search Entry, to produce a smoother user experience.
func NewSearchEntry() *Entry {
return finishNewEntry(C.uiNewSearchEntry())
2015-12-12 15:44:32 -06:00
}
// Text returns the Entry's text.
func (e *Entry) Text() string {
ctext := C.uiEntryText(e.e)
text := C.GoString(ctext)
C.uiFreeText(ctext)
return text
}
// SetText sets the Entry's text to text.
func (e *Entry) SetText(text string) {
ctext := C.CString(text)
C.uiEntrySetText(e.e, ctext)
freestr(ctext)
}
// OnChanged registers f to be run when the user makes a change to
// the Entry. Only one function can be registered at a time.
func (e *Entry) OnChanged(f func(*Entry)) {
e.onChanged = f
}
//export doEntryOnChanged
func doEntryOnChanged(ee *C.uiEntry, data unsafe.Pointer) {
2018-08-11 18:52:29 -05:00
e := ControlFromLibui(uintptr(unsafe.Pointer(ee))).(*Entry)
2015-12-12 15:44:32 -06:00
if e.onChanged != nil {
e.onChanged(e)
}
}
// ReadOnly returns whether the Entry can be changed.
func (e *Entry) ReadOnly() bool {
return tobool(C.uiEntryReadOnly(e.e))
}
// SetReadOnly sets whether the Entry can be changed.
func (e *Entry) SetReadOnly(ro bool) {
C.uiEntrySetReadOnly(e.e, frombool(ro))
}