From 77bf566ebbcb62acd4d08d905d9542d6ff9b6b80 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 30 Aug 2014 23:02:02 -0400 Subject: ...in with the new. --- textfield_unix.go | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 textfield_unix.go (limited to 'textfield_unix.go') diff --git a/textfield_unix.go b/textfield_unix.go new file mode 100644 index 0000000..b06da02 --- /dev/null +++ b/textfield_unix.go @@ -0,0 +1,106 @@ +// +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 { + _widget *C.GtkWidget + entry *C.GtkEntry + changed *event +} + +func startNewTextField() *textfield { + widget := C.gtk_entry_new() + t := &textfield{ + _widget: 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) +} + +//export textfieldChanged +func textfieldChanged(editable *C.GtkEditable, data C.gpointer) { + t := (*textfield)(unsafe.Pointer(data)) +println("changed") + t.changed.fire() +} + +func (t *textfield) widget() *C.GtkWidget { + return t._widget +} + +func (t *textfield) setParent(p *controlParent) { + basesetParent(t, p) +} + +func (t *textfield) allocate(x int, y int, width int, height int, d *sizing) []*allocation { + return baseallocate(t, x, y, width, height, d) +} + +func (t *textfield) preferredSize(d *sizing) (width, height int) { + return basepreferredSize(t, d) +} + +func (t *textfield) commitResize(a *allocation, d *sizing) { + basecommitResize(t, a, d) +} + +func (t *textfield) getAuxResizeInfo(d *sizing) { + basegetAuxResizeInfo(t, d) +} -- cgit v1.2.3