From 5ebd89984ac63c30a68a325a430038979badc3a0 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 15 Jul 2014 20:48:16 -0400 Subject: [PATCH] Added parenting/unparenting of controls to the GTK+ backend and the test program. --- redo/controls.go | 3 ++- redo/controls_unix.go | 25 +++++++++++++++++++++++++ redo/window_unix.go | 7 +++++-- redo/zz_test.go | 2 ++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/redo/controls.go b/redo/controls.go index b40256a..0500ac6 100644 --- a/redo/controls.go +++ b/redo/controls.go @@ -5,7 +5,8 @@ package ui // Control represents a control. // All Controls have event handlers that take a single argument (the Doer active during the event) and return nothing. type Control interface { - // TODO reparent (public) + unparent() + parent(*window) // TODO enable/disable (public) // TODO show/hide (public) // TODO sizing (likely private) diff --git a/redo/controls_unix.go b/redo/controls_unix.go index 1be152b..e48aab2 100644 --- a/redo/controls_unix.go +++ b/redo/controls_unix.go @@ -13,6 +13,8 @@ import "C" type widgetbase struct { widget *C.GtkWidget + parentw *window + floating bool } func newWidget(w *C.GtkWidget) *widgetbase { @@ -21,6 +23,29 @@ func newWidget(w *C.GtkWidget) *widgetbase { } } +// these few methods are embedded by all the various Controls since they all will do the same thing + +func (w *widgetbase) unparent() { + if w.parentw != nil { + // add another reference so it doesn't get removed by accident + C.g_object_ref(C.gpointer(unsafe.Pointer(w.widget))) + // we unref this in parent() below + w.floating = true + C.gtk_container_remove(w.parentw.layoutc, w.widget) + w.parentw = nil + } +} + +func (w *widgetbase) parent(win *window) { + C.gtk_container_add(win.layoutc, w.widget) + w.parentw = win + // was previously parented; unref our saved ref + if w.floating { + C.g_object_unref(C.gpointer(unsafe.Pointer(w.widget))) + w.floating = false + } +} + type button struct { *widgetbase button *C.GtkButton diff --git a/redo/window_unix.go b/redo/window_unix.go index 062c65f..3992443 100644 --- a/redo/window_unix.go +++ b/redo/window_unix.go @@ -23,6 +23,8 @@ type window struct { layoutc *C.GtkContainer layout *C.GtkLayout + child Control + closing *event } @@ -66,8 +68,9 @@ func (w *window) SetControl(control Control) *Request { c := make(chan interface{}) return &Request{ op: func() { - // TODO unparent - // TODO reparent + control.unparent() + control.parent(w) + w.child = control c <- struct{}{} }, resp: c, diff --git a/redo/zz_test.go b/redo/zz_test.go index f022ab4..fb17ef7 100644 --- a/redo/zz_test.go +++ b/redo/zz_test.go @@ -12,6 +12,8 @@ import ( func init() { go func() { w := GetNewWindow(Do, "Hello", 320, 240) + b := GetNewButton(Do, "There") + Wait(Do, w.SetControl(b)) done := make(chan struct{}) Wait(Do, w.OnClosing(func(c Doer) bool { Stop()