func main() { gtk.Init(nil) win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL) if err != nil { log.Fatal("Unable to create window:", err) } win.Connect("destroy", func() { gtk.MainQuit() }) win.Add(windowWidget()) // Native GTK is not thread safe, and thus, gotk3's GTK bindings may not // be used from other goroutines. Instead, glib.IdleAdd() must be used // to add a function to run in the GTK main loop when it is in an idle // state. // // Two examples of using glib.IdleAdd() are shown below. The first runs // a user created function, LabelSetTextIdle, and passes it two // arguments for a label and the text to set it with. The second calls // (*gtk.Label).SetText directly, passing in only the text as an // argument. // // If the function passed to glib.IdleAdd() returns one argument, and // that argument is a bool, this return value will be used in the same // manner as a native g_idle_add() call. If this return value is false, // the function will be removed from executing in the GTK main loop's // idle state. If the return value is true, the function will continue // to execute when the GTK main loop is in this state. go func() { for { time.Sleep(time.Second) s := fmt.Sprintf("Set a label %d time(s)!", nSets) _, err := glib.IdleAdd(LabelSetTextIdle, topLabel, s) if err != nil { log.Fatal("IdleAdd() failed:", err) } nSets++ s = fmt.Sprintf("Set a label %d time(s)!", nSets) _, err = glib.IdleAdd(bottomLabel.SetText, s) if err != nil { log.Fatal("IdleAdd() failed:", err) } nSets++ } }() win.ShowAll() gtk.Main() }
// TestConnectNotifySignal ensures that property notification signals (those // whose name begins with "notify::") are queried by the name "notify" (with the // "::" and the property name omitted). This is because the signal is "notify" // and the characters after the "::" are not recognized by the signal system. // // See // https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html#GObject-notify // for background, and // https://developer.gnome.org/gobject/stable/gobject-Signals.html#g-signal-new // for the specification of valid signal names. func TestConnectNotifySignal(t *testing.T) { runtime.LockOSThread() // Create any GObject that has defined properties. spacing := 0 box, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, spacing) // Connect to a "notify::" signal to listen on property changes. box.Connect("notify::spacing", func() { gtk.MainQuit() }) glib.IdleAdd(func(s string) bool { t.Log(s) spacing++ box.SetSpacing(spacing) return true }, "IdleAdd executed") gtk.Main() }