Made Window.Closing a callback closure.

This commit is contained in:
Pietro Gagliardi 2014-06-30 23:48:08 -04:00
parent ffa1bbe0b9
commit 3565889e15
7 changed files with 26 additions and 22 deletions

View File

@ -27,9 +27,7 @@ import "C"
func our_window_delete_event_callback(widget *C.GtkWidget, event *C.GdkEvent, what C.gpointer) C.gboolean {
// called when the user tries to close the window
s := (*sysData)(unsafe.Pointer(what))
b := false // TODO
s.close(&b)
return togbool(!b) // ! because TRUE means don't close
return togbool(s.close()) // ! because TRUE means don't close
}
var window_delete_event_callback = C.GCallback(C.our_window_delete_event_callback)

View File

@ -26,9 +26,7 @@ func makeAppDelegate() {
//export appDelegate_windowShouldClose
func appDelegate_windowShouldClose(win C.id) C.BOOL {
sysData := getSysData(win)
b := false // TODO
sysData.close(&b)
return toBOOL(b)
return toBOOL(sysData.close())
}
//export appDelegate_windowDidResize

View File

@ -164,9 +164,8 @@ func stdWndProc(hwnd _HWND, uMsg uint32, wParam _WPARAM, lParam _LPARAM) _LRESUL
}
return 0
case _WM_CLOSE:
close := false // TODO decide apt default
s.close(&close)
if close {
if s.close() {
// TODO destroy
s.hide()
}
return 0

View File

@ -10,7 +10,7 @@ type cSysData struct {
alternate bool // editable for Combobox, multi-select for listbox, password for lineedit
handler AreaHandler // for Areas; TODO rename to areahandler
winhandler WindowHandler // for Windows
close func(*bool) // provided by each Window
close func() bool // provided by each Window
event func() // provided by each control
}

View File

@ -75,9 +75,6 @@ func (a *keyboardArea) Key(e KeyEvent) (repaint bool) {
type kbhandler struct{}
func (kbhandler) Event(e Event, d interface{}) {
if e == Closing {
*(d.(*bool)) = true
}
}
var doKeyboard = flag.Bool("kb", false, "run keyboard test (overrides -areabounds)")
@ -85,6 +82,7 @@ func kbTest() {
wid, ht, ah := mkkbArea()
a := NewArea(wid, ht, ah)
w := NewWindow("Hi", wid, ht, kbhandler{})
w.Closing = func() bool { return true }
w.Open(a)
}

View File

@ -18,6 +18,12 @@ import (
type nullwinhandler struct{}
func (nullwinhandler) Event(Event, interface{}) {}
func die() bool {
// TODO we want the bool return to happen before the Stop...
Stop <- struct{}{}
return true
}
var prefsizetest = flag.Bool("prefsize", false, "")
func listboxPreferredSizeTest() *Window {
lb := NewListbox("xxxxx", "y", "zzz")
@ -242,6 +248,7 @@ func areaTest() {
modaltest: modaltest,
repainttest: repainttest,
})
w.Closing = die
w.Open(layout)
go func() {
for t := range timechan {
@ -264,9 +271,6 @@ type areatestwinhandler struct {
}
func (a *areatestwinhandler) Event(e Event, d interface{}) {
switch e {
case Closing:
*(d.(*bool)) = true
Stop <- struct{}{}
case Clicked:
switch d {
case a.resize:
@ -311,6 +315,7 @@ func areaboundsTest() {
w := NewWindow("Area Bounds Test", 320, 240, &areatestwinhandler{
a: a,
})
w.Closing = die
w.Open(a)
}
@ -367,6 +372,10 @@ type testwinhandler struct {
func runMainTest() {
handler := new(testwinhandler)
handler.w = NewWindow("Main Window", 320, 240, handler)
handler.w.Closing = func() bool {
println("window closed event received")
return die()
}
handler.b = NewButton("Click Me")
handler.b2 = NewButton("Or Me")
handler.bmsg = NewButton("Or Even Me!")
@ -481,9 +490,6 @@ func runMainTest() {
func (handler *testwinhandler) Event(e Event, d interface{}) {
switch e {
case Closing:
println("window closed event received")
Stop <- struct{}{}
case Clicked:
switch d {
case handler.b:

View File

@ -8,6 +8,13 @@ import (
// Window represents an on-screen window.
type Window struct {
// Closing is called when the Close button is pressed by the user, or when the application needs to be quit (should the underlying system provide a concept of application distinct from window).
// Return true to allow the window to be closed; false otherwise.
// You cannot change this field after the Window has been created.
// [TODO close vs. hide]
// TODO nil
Closing func() bool
created bool
sysData *sysData
initTitle string
@ -103,9 +110,7 @@ func (w *Window) create(control Control, show bool) {
}
w.sysData.spaced = w.spaced
w.sysData.winhandler = w.handler
w.sysData.close = func(b *bool) {
w.sysData.winhandler.Event(Closing, b)
}
w.sysData.close = w.Closing
err := w.sysData.make(nil)
if err != nil {
panic(fmt.Errorf("error opening window: %v", err))