2014-08-02 21:35:58 -05:00
|
|
|
// +build !windows,!darwin
|
|
|
|
|
|
|
|
// 7 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #include "gtk_unix.h"
|
2014-08-20 00:08:24 -05:00
|
|
|
// extern void textfieldChanged(GtkEditable *, gpointer);
|
2014-08-20 00:35:05 -05:00
|
|
|
// /* because cgo doesn't like GTK_STOCK_DIALOG_ERROR */
|
|
|
|
// static inline void setErrorIcon(GtkEntry *entry)
|
|
|
|
// {
|
|
|
|
// gtk_entry_set_icon_from_stock(entry, GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_DIALOG_ERROR);
|
|
|
|
// }
|
2014-08-02 21:35:58 -05:00
|
|
|
import "C"
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
type textfield struct {
|
2014-10-18 16:03:07 -05:00
|
|
|
*controlSingleWidget
|
2014-10-02 09:05:53 -05:00
|
|
|
entry *C.GtkEntry
|
|
|
|
changed *event
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func startNewTextField() *textfield {
|
2014-08-03 15:28:21 -05:00
|
|
|
widget := C.gtk_entry_new()
|
2014-08-20 00:08:24 -05:00
|
|
|
t := &textfield{
|
2014-10-18 16:03:07 -05:00
|
|
|
controlSingleWidget: newControlSingleWidget(widget),
|
2014-10-02 09:05:53 -05:00
|
|
|
entry: (*C.GtkEntry)(unsafe.Pointer(widget)),
|
|
|
|
changed: newEvent(),
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
2014-08-20 00:08:24 -05:00
|
|
|
g_signal_connect(
|
2014-10-18 16:03:07 -05:00
|
|
|
C.gpointer(unsafe.Pointer(t.widget)),
|
2014-08-20 00:08:24 -05:00
|
|
|
"changed",
|
|
|
|
C.GCallback(C.textfieldChanged),
|
|
|
|
C.gpointer(unsafe.Pointer(t)))
|
|
|
|
return t
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func newTextField() *textfield {
|
2014-08-02 21:35:58 -05:00
|
|
|
return startNewTextField()
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func newPasswordField() *textfield {
|
2014-08-02 21:35:58 -05:00
|
|
|
t := startNewTextField()
|
|
|
|
C.gtk_entry_set_visibility(t.entry, C.FALSE)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func (t *textfield) Text() string {
|
2014-08-02 21:35:58 -05:00
|
|
|
return fromgstr(C.gtk_entry_get_text(t.entry))
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func (t *textfield) SetText(text string) {
|
2014-08-02 21:35:58 -05:00
|
|
|
ctext := togstr(text)
|
|
|
|
defer freegstr(ctext)
|
|
|
|
C.gtk_entry_set_text(t.entry, ctext)
|
|
|
|
}
|
2014-08-03 15:28:21 -05:00
|
|
|
|
2014-08-20 00:08:24 -05:00
|
|
|
func (t *textfield) OnChanged(f func()) {
|
|
|
|
t.changed.set(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *textfield) Invalid(reason string) {
|
2014-08-20 00:35:05 -05:00
|
|
|
if reason == "" {
|
|
|
|
C.gtk_entry_set_icon_from_stock(t.entry, C.GTK_ENTRY_ICON_SECONDARY, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
C.setErrorIcon(t.entry)
|
|
|
|
creason := togstr(reason)
|
|
|
|
defer freegstr(creason)
|
|
|
|
C.gtk_entry_set_icon_tooltip_text(t.entry, C.GTK_ENTRY_ICON_SECONDARY, creason)
|
2014-10-18 16:03:07 -05:00
|
|
|
C.gtk_widget_error_bell(t.widget)
|
2014-08-20 00:08:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//export textfieldChanged
|
|
|
|
func textfieldChanged(editable *C.GtkEditable, data C.gpointer) {
|
|
|
|
t := (*textfield)(unsafe.Pointer(data))
|
|
|
|
t.changed.fire()
|
|
|
|
}
|