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-08-03 15:28:21 -05:00
|
|
|
_widget *C.GtkWidget
|
2014-08-02 21:35:58 -05:00
|
|
|
entry *C.GtkEntry
|
2014-08-20 00:08:24 -05:00
|
|
|
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-08-03 15:28:21 -05:00
|
|
|
_widget: widget,
|
|
|
|
entry: (*C.GtkEntry)(unsafe.Pointer(widget)),
|
2014-08-20 00:08:24 -05:00
|
|
|
changed: newEvent(),
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
2014-08-20 00:08:24 -05:00
|
|
|
g_signal_connect(
|
|
|
|
C.gpointer(unsafe.Pointer(t._widget)),
|
|
|
|
"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-08-24 00:14:17 -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))
|
|
|
|
println("changed")
|
|
|
|
t.changed.fire()
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func (t *textfield) widget() *C.GtkWidget {
|
2014-08-03 15:28:21 -05:00
|
|
|
return t._widget
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func (t *textfield) setParent(p *controlParent) {
|
2014-08-03 15:28:21 -05:00
|
|
|
basesetParent(t, p)
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func (t *textfield) allocate(x int, y int, width int, height int, d *sizing) []*allocation {
|
2014-08-03 15:28:21 -05:00
|
|
|
return baseallocate(t, x, y, width, height, d)
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func (t *textfield) preferredSize(d *sizing) (width, height int) {
|
2014-08-03 15:28:21 -05:00
|
|
|
return basepreferredSize(t, d)
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func (t *textfield) commitResize(a *allocation, d *sizing) {
|
2014-08-03 15:28:21 -05:00
|
|
|
basecommitResize(t, a, d)
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:46:42 -05:00
|
|
|
func (t *textfield) getAuxResizeInfo(d *sizing) {
|
2014-08-03 19:33:19 -05:00
|
|
|
basegetAuxResizeInfo(t, d)
|
2014-08-03 15:28:21 -05:00
|
|
|
}
|