From ac1b8e3cca971c38c8091f60fedfe093e6fe78df Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 20 Aug 2014 01:35:05 -0400 Subject: [PATCH] Added a test for TextField.Invalid() and implemented it on the GTK+ backend. --- redo/textfield_unix.go | 14 +++++++++++++- redo/zz_test.go | 13 +++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/redo/textfield_unix.go b/redo/textfield_unix.go index c4ce8b4..e9624ee 100644 --- a/redo/textfield_unix.go +++ b/redo/textfield_unix.go @@ -10,6 +10,11 @@ import ( // #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 { @@ -58,7 +63,14 @@ func (t *textfield) OnChanged(f func()) { } func (t *textfield) Invalid(reason string) { - // TODO + 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) } //export textfieldChanged diff --git a/redo/zz_test.go b/redo/zz_test.go index 2477de0..e1eb8fc 100644 --- a/redo/zz_test.go +++ b/redo/zz_test.go @@ -13,6 +13,7 @@ import ( "image/color" "image/draw" "time" + "strings" ) var closeOnClick = flag.Bool("close", false, "close on click") @@ -38,6 +39,7 @@ type testwin struct { festart Button felabel Label festop Button + vedit TextField openbtn Button fnlabel Label icons []icon @@ -91,6 +93,14 @@ func (tw *testwin) addfe() { tw.fe = nil } }) + tw.vedit = NewTextField() + tw.vedit.OnChanged(func() { + if strings.Contains(tw.vedit.Text(), "bad") { + tw.vedit.Invalid("bad entered") + } else { + tw.vedit.Invalid("") + } + }) tw.openbtn = NewButton("Open") tw.openbtn.OnClicked(func() { fn := OpenFile() @@ -101,9 +111,12 @@ func (tw *testwin) addfe() { }) tw.fnlabel = NewStandaloneLabel("") tw.festack = NewVerticalStack(tw.festart, tw.felabel, tw.festop, + Space(), + tw.vedit, Space(), tw.openbtn, tw.fnlabel) tw.festack.SetStretchy(3) + tw.festack.SetStretchy(5) tw.t.Append("Foreign Events", tw.festack) }