2014-02-27 22:07:45 -06:00
|
|
|
// 27 february 2014
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #cgo CFLAGS: -Dqqq
|
|
|
|
// #cgo LDFLAGS: -lobjc -framework Foundation
|
|
|
|
// #include <stdlib.h>
|
2014-02-28 17:30:06 -06:00
|
|
|
// #include "objc_darwin.h"
|
2014-02-28 10:06:20 -06:00
|
|
|
// extern void windowShouldClose(id, SEL, id);
|
2014-02-28 10:55:35 -06:00
|
|
|
// extern void buttonClicked(id, SEL, id);
|
2014-02-28 14:08:07 -06:00
|
|
|
// extern void gotNotification(id, SEL, id);
|
2014-02-28 19:53:14 -06:00
|
|
|
// /* because cgo doesn't like Nil */
|
|
|
|
// extern Class NilClass; /* defined in runtimetest.go due to cgo limitations */
|
2014-02-27 22:07:45 -06:00
|
|
|
import "C"
|
|
|
|
|
2014-02-28 19:53:14 -06:00
|
|
|
var (
|
|
|
|
_NSObject_Class = C.object_getClass(_NSObject)
|
|
|
|
)
|
2014-02-27 22:07:45 -06:00
|
|
|
|
|
|
|
func newClass(name string) C.Class {
|
|
|
|
cname := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cname))
|
|
|
|
|
2014-02-28 19:53:14 -06:00
|
|
|
c := C.objc_allocateClassPair(_NSObject_Class, cname, 0)
|
2014-02-27 22:07:45 -06:00
|
|
|
if c == C.NilClass {
|
|
|
|
panic("unable to create Objective-C class " + name)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2014-02-28 19:53:14 -06:00
|
|
|
// TODO move these around later
|
|
|
|
var (
|
|
|
|
_stop = sel_getUid("stop:")
|
|
|
|
)
|
|
|
|
|
2014-02-28 10:06:20 -06:00
|
|
|
//export windowShouldClose
|
|
|
|
func windowShouldClose(self C.id, sel C.SEL, sender C.id) {
|
|
|
|
fmt.Println("-[hello windowShouldClose:]")
|
2014-02-28 19:53:14 -06:00
|
|
|
C.objc_msgSend_id(NSApp, _stop, sender)
|
2014-02-27 22:07:45 -06:00
|
|
|
}
|
|
|
|
|
2014-02-28 10:55:35 -06:00
|
|
|
//export buttonClicked
|
|
|
|
func buttonClicked(self C.id, sel C.SEL, sender C.id) {
|
2014-02-28 14:08:07 -06:00
|
|
|
fmt.Println("button clicked; sending notification...")
|
|
|
|
notify("button")
|
|
|
|
}
|
|
|
|
|
|
|
|
//export gotNotification
|
2014-02-28 16:20:22 -06:00
|
|
|
func gotNotification(self C.id, sel C.SEL, object C.id) {
|
2014-02-28 19:53:14 -06:00
|
|
|
fmt.Printf("got notification from %s\n", fromNSString(object))
|
2014-02-28 10:55:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func addOurMethod(class C.Class, sel C.SEL, imp C.IMP) {
|
2014-02-28 10:06:20 -06:00
|
|
|
// ty := []C.char{'v', '@', ':', 0} // according to the example for class_addMethod()
|
|
|
|
ty := []C.char{'v', '@', ':', '@', 0}
|
2014-02-27 22:07:45 -06:00
|
|
|
|
|
|
|
// clas methods get stored in the metaclass; the objc_allocateClassPair() docs say this will work
|
2014-02-28 10:06:20 -06:00
|
|
|
// metaclass := C.object_getClass(C.id(unsafe.Pointer(class)))
|
|
|
|
// ok := C.class_addMethod(metaclass,
|
|
|
|
ok := C.class_addMethod(class,
|
2014-02-27 22:07:45 -06:00
|
|
|
sel,
|
2014-02-28 10:55:35 -06:00
|
|
|
imp,
|
2014-02-27 22:07:45 -06:00
|
|
|
&ty[0])
|
|
|
|
if ok == C.BOOL(C.NO) {
|
|
|
|
panic("unable to add ourMethod")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 14:08:07 -06:00
|
|
|
func mk(name string, selW C.SEL, selB C.SEL, selN C.SEL) C.id {
|
2014-02-27 22:07:45 -06:00
|
|
|
class := newClass(name)
|
2014-02-28 10:55:35 -06:00
|
|
|
addOurMethod(class, selW,
|
|
|
|
// using &C.ourMethod causes faults for some reason
|
|
|
|
C.IMP(unsafe.Pointer(C.windowShouldClose)))
|
2014-02-27 22:07:45 -06:00
|
|
|
C.objc_registerClassPair(class)
|
2014-02-28 10:55:35 -06:00
|
|
|
addOurMethod(class, selB,
|
|
|
|
C.IMP(unsafe.Pointer(C.buttonClicked)))
|
2014-02-28 14:08:07 -06:00
|
|
|
addOurMethod(class, selN,
|
|
|
|
C.IMP(unsafe.Pointer(C.gotNotification)))
|
2014-02-27 22:07:45 -06:00
|
|
|
return objc_getClass(name)
|
|
|
|
}
|