Cleaned up newtypes.go.

This commit is contained in:
Pietro Gagliardi 2014-02-28 20:53:14 -05:00
parent ea24130fc8
commit e5a4f32182
3 changed files with 15 additions and 15 deletions

View File

@ -13,30 +13,34 @@ import (
// extern void windowShouldClose(id, SEL, id); // extern void windowShouldClose(id, SEL, id);
// extern void buttonClicked(id, SEL, id); // extern void buttonClicked(id, SEL, id);
// extern void gotNotification(id, SEL, id); // extern void gotNotification(id, SEL, id);
// /* cgo doesn't like nil or Nil */ // /* because cgo doesn't like Nil */
// extern Class NilClass; /* in runtimetest.go because of cgo limitations */ // extern Class NilClass; /* defined in runtimetest.go due to cgo limitations */
// extern id Nilid;
import "C" import "C"
var NSObject = C.object_getClass(objc_getClass("NSObject")) var (
_NSObject_Class = C.object_getClass(_NSObject)
)
func newClass(name string) C.Class { func newClass(name string) C.Class {
cname := C.CString(name) cname := C.CString(name)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))
c := C.objc_allocateClassPair(NSObject, cname, 0) c := C.objc_allocateClassPair(_NSObject_Class, cname, 0)
if c == C.NilClass { if c == C.NilClass {
panic("unable to create Objective-C class " + name) panic("unable to create Objective-C class " + name)
} }
return c return c
} }
// TODO move these around later
var (
_stop = sel_getUid("stop:")
)
//export windowShouldClose //export windowShouldClose
func windowShouldClose(self C.id, sel C.SEL, sender C.id) { func windowShouldClose(self C.id, sel C.SEL, sender C.id) {
fmt.Println("-[hello windowShouldClose:]") fmt.Println("-[hello windowShouldClose:]")
C.objc_msgSend_id(NSApp, C.objc_msgSend_id(NSApp, _stop, sender)
sel_getUid("stop:"),
sender)
} }
//export buttonClicked //export buttonClicked
@ -47,11 +51,7 @@ func buttonClicked(self C.id, sel C.SEL, sender C.id) {
//export gotNotification //export gotNotification
func gotNotification(self C.id, sel C.SEL, object C.id) { func gotNotification(self C.id, sel C.SEL, object C.id) {
source := (*C.char)(unsafe.Pointer( fmt.Printf("got notification from %s\n", fromNSString(object))
C.objc_msgSend_noargs(object,
sel_getUid("UTF8String"))))
fmt.Println("got notification from %s",
C.GoString(source))
} }
func addOurMethod(class C.Class, sel C.SEL, imp C.IMP) { func addOurMethod(class C.Class, sel C.SEL, imp C.IMP) {

View File

@ -26,6 +26,7 @@ func sel_getUid(sel string) C.SEL {
// Common Objective-C types and selectors. // Common Objective-C types and selectors.
var ( var (
_NSObject = objc_getClass("NSObject")
_NSString = objc_getClass("NSString") _NSString = objc_getClass("NSString")
_alloc = sel_getUid("alloc") _alloc = sel_getUid("alloc")

View File

@ -9,8 +9,7 @@ import (
// #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit // #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit
// #include <stdlib.h> // #include <stdlib.h>
// #include "objc_darwin.h" // #include "objc_darwin.h"
// Class NilClass = Nil; /* for newtypes.go */ // Class NilClass = Nil; /* for newtypes.go; here due to cgo limitations */
// id Nilid = nil;
import "C" import "C"
var ( var (