Added parenting/unparenting of controls to the GTK+ backend and the test program.
This commit is contained in:
parent
474436e9f6
commit
5ebd89984a
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue