From 0fdfc4c7a6770799368ce943d510c51341a0227b Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 28 Feb 2014 11:06:20 -0500 Subject: [PATCH] Built a window delegate and ran the event loop. It works! --- darwintest/newtypes.go | 22 ++++++++++++++-------- darwintest/runtimetest.go | 30 +++++++++++++++++++----------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/darwintest/newtypes.go b/darwintest/newtypes.go index 8fa0147..a136d16 100644 --- a/darwintest/newtypes.go +++ b/darwintest/newtypes.go @@ -12,7 +12,8 @@ import ( // #include // #include // #include -// extern void ourMethod(id, SEL); +// extern void windowShouldClose(id, SEL, id); +// extern id objc_msgSend_id(id, SEL, id); // /* cgo doesn't like Nil */ // extern Class NilClass; /* in runtimetest.go because of cgo limitations */ import "C" @@ -30,20 +31,25 @@ func newClass(name string) C.Class { return c } -//export ourMethod -func ourMethod(self C.id, sel C.SEL) { - fmt.Println("hello, world") +//export windowShouldClose +func windowShouldClose(self C.id, sel C.SEL, sender C.id) { + fmt.Println("-[hello windowShouldClose:]") + C.objc_msgSend_id(NSApp, + sel_getUid("stop:"), + sender) } func addOurMethod(class C.Class, sel C.SEL) { - ty := []C.char{'v', '@', ':', 0} // according to the example for class_addMethod() +// ty := []C.char{'v', '@', ':', 0} // according to the example for class_addMethod() + ty := []C.char{'v', '@', ':', '@', 0} // clas methods get stored in the metaclass; the objc_allocateClassPair() docs say this will work - metaclass := C.object_getClass(C.id(unsafe.Pointer(class))) - ok := C.class_addMethod(metaclass, +// metaclass := C.object_getClass(C.id(unsafe.Pointer(class))) +// ok := C.class_addMethod(metaclass, + ok := C.class_addMethod(class, sel, // using &C.ourMethod causes faults for some reason - C.IMP(unsafe.Pointer(C.ourMethod)), + C.IMP(unsafe.Pointer(C.windowShouldClose)), &ty[0]) if ok == C.BOOL(C.NO) { panic("unable to add ourMethod") diff --git a/darwintest/runtimetest.go b/darwintest/runtimetest.go index f86398d..79138ef 100644 --- a/darwintest/runtimetest.go +++ b/darwintest/runtimetest.go @@ -38,10 +38,16 @@ func sel_getUid(sel string) C.SEL { return C.sel_getUid(csel) } -func main() { - sel := sel_getUid("ourMethod") - C.objc_msgSend_noargs(mk("hello", sel), - sel) +var NSApp C.id + +func init() { + // need an NSApplication first - see https://github.com/TooTallNate/NodObjC/issues/21 + NSApplication := objc_getClass("NSApplication") + sharedApplication := sel_getUid("sharedApplication") + NSApp = C.objc_msgSend_noargs(NSApplication, sharedApplication) + + sel := sel_getUid("windowShouldClose:") + mk("hello", sel) } const ( @@ -61,17 +67,13 @@ const ( var alloc = sel_getUid("alloc") -func wintest() { +func main() { NSWindow := objc_getClass("NSWindow") NSWindowinit := sel_getUid("initWithContentRect:styleMask:backing:defer:") + setDelegate := sel_getUid("setDelegate:") makeKeyAndOrderFront := sel_getUid("makeKeyAndOrderFront:") - // need an NSApplication first - see https://github.com/TooTallNate/NodObjC/issues/21 - NSApplication := objc_getClass("NSApplication") - sharedApplication := sel_getUid("sharedApplication") - C.objc_msgSend_noargs(NSApplication, sharedApplication) - rect := C.CGRect{ origin: C.CGPoint{100, 100}, size: C.CGSize{320, 240}, @@ -82,7 +84,13 @@ func wintest() { window := C.objc_msgSend_noargs(NSWindow, alloc) window = C.objc_msgSend_NSRect_uint_uint_bool(window, NSWindowinit, rect, style, backing, deferx) C.objc_msgSend_id(window, makeKeyAndOrderFront, window) - select{} + delegate := C.objc_msgSend_noargs( + objc_getClass("hello"), + alloc) + C.objc_msgSend_id(window, setDelegate, + delegate) + C.objc_msgSend_noargs(NSApp, + sel_getUid("run")) } func helloworld() {