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.
|
// Control represents a control.
|
||||||
// All Controls have event handlers that take a single argument (the Doer active during the event) and return nothing.
|
// All Controls have event handlers that take a single argument (the Doer active during the event) and return nothing.
|
||||||
type Control interface {
|
type Control interface {
|
||||||
// TODO reparent (public)
|
unparent()
|
||||||
|
parent(*window)
|
||||||
// TODO enable/disable (public)
|
// TODO enable/disable (public)
|
||||||
// TODO show/hide (public)
|
// TODO show/hide (public)
|
||||||
// TODO sizing (likely private)
|
// TODO sizing (likely private)
|
||||||
|
|
|
@ -13,6 +13,8 @@ import "C"
|
||||||
|
|
||||||
type widgetbase struct {
|
type widgetbase struct {
|
||||||
widget *C.GtkWidget
|
widget *C.GtkWidget
|
||||||
|
parentw *window
|
||||||
|
floating bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWidget(w *C.GtkWidget) *widgetbase {
|
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 {
|
type button struct {
|
||||||
*widgetbase
|
*widgetbase
|
||||||
button *C.GtkButton
|
button *C.GtkButton
|
||||||
|
|
|
@ -23,6 +23,8 @@ type window struct {
|
||||||
layoutc *C.GtkContainer
|
layoutc *C.GtkContainer
|
||||||
layout *C.GtkLayout
|
layout *C.GtkLayout
|
||||||
|
|
||||||
|
child Control
|
||||||
|
|
||||||
closing *event
|
closing *event
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,8 +68,9 @@ func (w *window) SetControl(control Control) *Request {
|
||||||
c := make(chan interface{})
|
c := make(chan interface{})
|
||||||
return &Request{
|
return &Request{
|
||||||
op: func() {
|
op: func() {
|
||||||
// TODO unparent
|
control.unparent()
|
||||||
// TODO reparent
|
control.parent(w)
|
||||||
|
w.child = control
|
||||||
c <- struct{}{}
|
c <- struct{}{}
|
||||||
},
|
},
|
||||||
resp: c,
|
resp: c,
|
||||||
|
|
|
@ -12,6 +12,8 @@ import (
|
||||||
func init() {
|
func init() {
|
||||||
go func() {
|
go func() {
|
||||||
w := GetNewWindow(Do, "Hello", 320, 240)
|
w := GetNewWindow(Do, "Hello", 320, 240)
|
||||||
|
b := GetNewButton(Do, "There")
|
||||||
|
Wait(Do, w.SetControl(b))
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
Wait(Do, w.OnClosing(func(c Doer) bool {
|
Wait(Do, w.OnClosing(func(c Doer) bool {
|
||||||
Stop()
|
Stop()
|
||||||
|
|
Loading…
Reference in New Issue