Built a window delegate and ran the event loop. It works!

This commit is contained in:
Pietro Gagliardi 2014-02-28 11:06:20 -05:00
parent e0c351a2d7
commit 0fdfc4c7a6
2 changed files with 33 additions and 19 deletions

View File

@ -12,7 +12,8 @@ import (
// #include <objc/message.h>
// #include <objc/objc.h>
// #include <objc/runtime.h>
// 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")

View File

@ -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() {