2014-08-02 21:35:58 -05:00
|
|
|
// 16 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #include "objc_darwin.h"
|
|
|
|
import "C"
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
type textfield struct {
|
2014-10-18 16:03:07 -05:00
|
|
|
*controlSingleObject
|
2014-10-02 09:05:53 -05:00
|
|
|
changed *event
|
|
|
|
invalid C.id
|
2014-10-18 16:03:07 -05:00
|
|
|
chainpreferredSize func(d *sizing) (int, int)
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
|
|
|
|
2014-08-20 14:53:34 -05:00
|
|
|
func finishNewTextField(id C.id) *textfield {
|
2014-08-20 14:26:48 -05:00
|
|
|
t := &textfield{
|
2014-10-18 16:03:07 -05:00
|
|
|
controlSingleObject: newControlSingleObject(id),
|
2014-10-02 09:05:53 -05:00
|
|
|
changed: newEvent(),
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
2014-10-18 16:03:07 -05:00
|
|
|
C.textfieldSetDelegate(t.id, unsafe.Pointer(t))
|
|
|
|
t.chainpreferredSize = t.fpreferredSize
|
|
|
|
t.fpreferredSize = t.xpreferredSize
|
2014-08-20 14:26:48 -05:00
|
|
|
return t
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
|
|
|
|
2014-08-20 14:53:34 -05:00
|
|
|
func newTextField() *textfield {
|
2014-08-20 16:08:47 -05:00
|
|
|
return finishNewTextField(C.newTextField())
|
2014-08-20 14:53:34 -05:00
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func newPasswordField() *textfield {
|
2014-08-20 14:53:34 -05:00
|
|
|
return finishNewTextField(C.newPasswordField())
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func (t *textfield) Text() string {
|
2014-10-18 16:03:07 -05:00
|
|
|
return C.GoString(C.textfieldText(t.id))
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func (t *textfield) SetText(text string) {
|
2014-08-02 21:35:58 -05:00
|
|
|
ctext := C.CString(text)
|
|
|
|
defer C.free(unsafe.Pointer(ctext))
|
2014-10-18 16:03:07 -05:00
|
|
|
C.textfieldSetText(t.id, ctext)
|
2014-08-03 19:08:25 -05:00
|
|
|
}
|
|
|
|
|
2014-08-20 14:26:48 -05:00
|
|
|
func (t *textfield) OnChanged(f func()) {
|
|
|
|
t.changed.set(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *textfield) Invalid(reason string) {
|
2014-08-20 16:08:47 -05:00
|
|
|
if t.invalid != nil {
|
|
|
|
C.textfieldCloseInvalidPopover(t.invalid)
|
|
|
|
t.invalid = nil
|
|
|
|
}
|
|
|
|
if reason == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
creason := C.CString(reason)
|
|
|
|
defer C.free(unsafe.Pointer(creason))
|
2014-10-18 16:03:07 -05:00
|
|
|
t.invalid = C.textfieldOpenInvalidPopover(t.id, creason)
|
2014-08-20 14:26:48 -05:00
|
|
|
}
|
|
|
|
|
2014-11-05 12:35:39 -06:00
|
|
|
// note that the property here is editable, which is the opposite of read-only
|
|
|
|
|
|
|
|
func (t *textfield) ReadOnly() bool {
|
|
|
|
return !fromBOOL(C.textfieldEditable(t.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *textfield) SetReadOnly(readonly bool) {
|
|
|
|
C.textfieldSetEditable(t.id, toBOOL(!readonly))
|
|
|
|
}
|
|
|
|
|
2014-08-20 14:26:48 -05:00
|
|
|
//export textfieldChanged
|
|
|
|
func textfieldChanged(data unsafe.Pointer) {
|
|
|
|
t := (*textfield)(data)
|
|
|
|
t.changed.fire()
|
|
|
|
}
|
|
|
|
|
2014-10-18 16:03:07 -05:00
|
|
|
func (t *textfield) xpreferredSize(d *sizing) (width, height int) {
|
|
|
|
_, height = t.chainpreferredSize(d)
|
2014-08-25 15:07:35 -05:00
|
|
|
// the returned width is based on the contents; use this instead
|
|
|
|
return C.textfieldWidth, height
|
2014-08-03 19:08:25 -05:00
|
|
|
}
|