summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--redo/controls.go3
-rw-r--r--redo/controls_unix.go25
-rw-r--r--redo/window_unix.go7
-rw-r--r--redo/zz_test.go2
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()