diff --git a/darwintest/newtypes.go b/darwintest/newtypes.go index a136d16..a4e5a68 100644 --- a/darwintest/newtypes.go +++ b/darwintest/newtypes.go @@ -14,6 +14,7 @@ import ( // #include // extern void windowShouldClose(id, SEL, id); // extern id objc_msgSend_id(id, SEL, id); +// extern void buttonClicked(id, SEL, id); // /* cgo doesn't like Nil */ // extern Class NilClass; /* in runtimetest.go because of cgo limitations */ import "C" @@ -39,7 +40,12 @@ func windowShouldClose(self C.id, sel C.SEL, sender C.id) { sender) } -func addOurMethod(class C.Class, sel C.SEL) { +//export buttonClicked +func buttonClicked(self C.id, sel C.SEL, sender C.id) { + fmt.Println("button clicked") +} + +func addOurMethod(class C.Class, sel C.SEL, imp C.IMP) { // ty := []C.char{'v', '@', ':', 0} // according to the example for class_addMethod() ty := []C.char{'v', '@', ':', '@', 0} @@ -48,17 +54,20 @@ func addOurMethod(class C.Class, sel C.SEL) { // ok := C.class_addMethod(metaclass, ok := C.class_addMethod(class, sel, - // using &C.ourMethod causes faults for some reason - C.IMP(unsafe.Pointer(C.windowShouldClose)), + imp, &ty[0]) if ok == C.BOOL(C.NO) { panic("unable to add ourMethod") } } -func mk(name string, sel C.SEL) C.id { +func mk(name string, selW C.SEL, selB C.SEL) C.id { class := newClass(name) - addOurMethod(class, sel) + addOurMethod(class, selW, + // using &C.ourMethod causes faults for some reason + C.IMP(unsafe.Pointer(C.windowShouldClose))) C.objc_registerClassPair(class) + addOurMethod(class, selB, + C.IMP(unsafe.Pointer(C.buttonClicked))) return objc_getClass(name) } diff --git a/darwintest/runtimetest.go b/darwintest/runtimetest.go index 79138ef..e6854a9 100644 --- a/darwintest/runtimetest.go +++ b/darwintest/runtimetest.go @@ -21,6 +21,8 @@ import ( // id objc_msgSend_strarg(id obj, SEL sel, char *a) { return objc_msgSend(obj, sel, a); } // id objc_msgSend_NSRect_uint_uint_bool(id obj, SEL sel, CGRect a, NSUInteger b, NSUInteger c, BOOL d) { return objc_msgSend(obj, sel, a, b, c, d); } // id objc_msgSend_id(id obj, SEL sel, id a) { return objc_msgSend(obj, sel, a); } +// id objc_msgSend_NSRect(id obj, SEL sel, CGRect a) { return objc_msgSend(obj, sel, a); } +// id objc_msgSend_sel(id obj, SEL sel, SEL a) { return objc_msgSend(obj, sel, a); } // Class NilClass = Nil; /* for newtypes.go */ import "C" @@ -46,8 +48,9 @@ func init() { sharedApplication := sel_getUid("sharedApplication") NSApp = C.objc_msgSend_noargs(NSApplication, sharedApplication) - sel := sel_getUid("windowShouldClose:") - mk("hello", sel) + selW := sel_getUid("windowShouldClose:") + selB := sel_getUid("buttonClicked:") + mk("hello", selW, selB) } const ( @@ -89,6 +92,27 @@ func main() { alloc) C.objc_msgSend_id(window, setDelegate, delegate) + windowView := C.objc_msgSend_noargs(window, + sel_getUid("contentView")) + + NSButton := objc_getClass("NSButton") + button := C.objc_msgSend_noargs(NSButton, alloc) + button = C.objc_msgSend_NSRect(button, + sel_getUid("initWithFrame:"), + C.CGRect{ + origin: C.CGPoint{20, 20}, + size: C.CGSize{200, 200}, + }) + C.objc_msgSend_id(button, + sel_getUid("setTarget:"), + delegate) + C.objc_msgSend_sel(button, + sel_getUid("setAction:"), + sel_getUid("buttonClicked:")) + C.objc_msgSend_id(windowView, + sel_getUid("addSubview:"), + button) + C.objc_msgSend_noargs(NSApp, sel_getUid("run")) }