forked from andlabs/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textfield_unix.go
93 lines (77 loc) · 2.13 KB
/
textfield_unix.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// +build !windows,!darwin
// 7 july 2014
package ui
import (
"unsafe"
)
// #include "gtk_unix.h"
// extern void textfieldChanged(GtkEditable *, gpointer);
// /* 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);
// }
import "C"
type textfield struct {
*controlSingleWidget
editable *C.GtkEditable
entry *C.GtkEntry
changed *event
}
func startNewTextField() *textfield {
widget := C.gtk_entry_new()
t := &textfield{
controlSingleWidget: newControlSingleWidget(widget),
editable: (*C.GtkEditable)(unsafe.Pointer(widget)),
entry: (*C.GtkEntry)(unsafe.Pointer(widget)),
changed: newEvent(),
}
g_signal_connect(
C.gpointer(unsafe.Pointer(t.widget)),
"changed",
C.GCallback(C.textfieldChanged),
C.gpointer(unsafe.Pointer(t)))
return t
}
func newTextField() *textfield {
return startNewTextField()
}
func newPasswordField() *textfield {
t := startNewTextField()
C.gtk_entry_set_visibility(t.entry, C.FALSE)
return t
}
func (t *textfield) Text() string {
return fromgstr(C.gtk_entry_get_text(t.entry))
}
func (t *textfield) SetText(text string) {
ctext := togstr(text)
defer freegstr(ctext)
C.gtk_entry_set_text(t.entry, ctext)
}
func (t *textfield) OnChanged(f func()) {
t.changed.set(f)
}
func (t *textfield) Invalid(reason string) {
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)
C.gtk_widget_error_bell(t.widget)
}
// note that the property here is editable, which is the opposite of read-only
func (t *textfield) ReadOnly() bool {
return !fromgbool(C.gtk_editable_get_editable(t.editable))
}
func (t *textfield) SetReadOnly(readonly bool) {
C.gtk_editable_set_editable(t.editable, togbool(!readonly))
}
//export textfieldChanged
func textfieldChanged(editable *C.GtkEditable, data C.gpointer) {
t := (*textfield)(unsafe.Pointer(data))
t.changed.fire()
}