Built a window delegate and ran the event loop. It works!
This commit is contained in:
parent
e0c351a2d7
commit
0fdfc4c7a6
|
@ -12,7 +12,8 @@ import (
|
||||||
// #include <objc/message.h>
|
// #include <objc/message.h>
|
||||||
// #include <objc/objc.h>
|
// #include <objc/objc.h>
|
||||||
// #include <objc/runtime.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 */
|
// /* cgo doesn't like Nil */
|
||||||
// extern Class NilClass; /* in runtimetest.go because of cgo limitations */
|
// extern Class NilClass; /* in runtimetest.go because of cgo limitations */
|
||||||
import "C"
|
import "C"
|
||||||
|
@ -30,20 +31,25 @@ func newClass(name string) C.Class {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
//export ourMethod
|
//export windowShouldClose
|
||||||
func ourMethod(self C.id, sel C.SEL) {
|
func windowShouldClose(self C.id, sel C.SEL, sender C.id) {
|
||||||
fmt.Println("hello, world")
|
fmt.Println("-[hello windowShouldClose:]")
|
||||||
|
C.objc_msgSend_id(NSApp,
|
||||||
|
sel_getUid("stop:"),
|
||||||
|
sender)
|
||||||
}
|
}
|
||||||
|
|
||||||
func addOurMethod(class C.Class, sel C.SEL) {
|
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
|
// clas methods get stored in the metaclass; the objc_allocateClassPair() docs say this will work
|
||||||
metaclass := C.object_getClass(C.id(unsafe.Pointer(class)))
|
// metaclass := C.object_getClass(C.id(unsafe.Pointer(class)))
|
||||||
ok := C.class_addMethod(metaclass,
|
// ok := C.class_addMethod(metaclass,
|
||||||
|
ok := C.class_addMethod(class,
|
||||||
sel,
|
sel,
|
||||||
// using &C.ourMethod causes faults for some reason
|
// using &C.ourMethod causes faults for some reason
|
||||||
C.IMP(unsafe.Pointer(C.ourMethod)),
|
C.IMP(unsafe.Pointer(C.windowShouldClose)),
|
||||||
&ty[0])
|
&ty[0])
|
||||||
if ok == C.BOOL(C.NO) {
|
if ok == C.BOOL(C.NO) {
|
||||||
panic("unable to add ourMethod")
|
panic("unable to add ourMethod")
|
||||||
|
|
|
@ -38,10 +38,16 @@ func sel_getUid(sel string) C.SEL {
|
||||||
return C.sel_getUid(csel)
|
return C.sel_getUid(csel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
var NSApp C.id
|
||||||
sel := sel_getUid("ourMethod")
|
|
||||||
C.objc_msgSend_noargs(mk("hello", sel),
|
func init() {
|
||||||
sel)
|
// 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 (
|
const (
|
||||||
|
@ -61,17 +67,13 @@ const (
|
||||||
|
|
||||||
var alloc = sel_getUid("alloc")
|
var alloc = sel_getUid("alloc")
|
||||||
|
|
||||||
func wintest() {
|
func main() {
|
||||||
NSWindow := objc_getClass("NSWindow")
|
NSWindow := objc_getClass("NSWindow")
|
||||||
NSWindowinit :=
|
NSWindowinit :=
|
||||||
sel_getUid("initWithContentRect:styleMask:backing:defer:")
|
sel_getUid("initWithContentRect:styleMask:backing:defer:")
|
||||||
|
setDelegate := sel_getUid("setDelegate:")
|
||||||
makeKeyAndOrderFront := sel_getUid("makeKeyAndOrderFront:")
|
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{
|
rect := C.CGRect{
|
||||||
origin: C.CGPoint{100, 100},
|
origin: C.CGPoint{100, 100},
|
||||||
size: C.CGSize{320, 240},
|
size: C.CGSize{320, 240},
|
||||||
|
@ -82,7 +84,13 @@ func wintest() {
|
||||||
window := C.objc_msgSend_noargs(NSWindow, alloc)
|
window := C.objc_msgSend_noargs(NSWindow, alloc)
|
||||||
window = C.objc_msgSend_NSRect_uint_uint_bool(window, NSWindowinit, rect, style, backing, deferx)
|
window = C.objc_msgSend_NSRect_uint_uint_bool(window, NSWindowinit, rect, style, backing, deferx)
|
||||||
C.objc_msgSend_id(window, makeKeyAndOrderFront, window)
|
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() {
|
func helloworld() {
|
||||||
|
|
Loading…
Reference in New Issue