summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-02-28 11:55:35 -0500
committerPietro Gagliardi <[email protected]>2014-02-28 11:55:35 -0500
commit428d3529a86f35c8a109118ceab76092c0af0f65 (patch)
tree70f28b72cfab1a93108c8493678af368353f7b57
parent0fdfc4c7a6770799368ce943d510c51341a0227b (diff)
Added a button to the darwin test and had it respond to clicks. Committing as is because without a bezelStyle ther esult is just... wow. 1990s Windows look and feel for the win?
-rw-r--r--darwintest/newtypes.go19
-rw-r--r--darwintest/runtimetest.go28
2 files changed, 40 insertions, 7 deletions
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 <objc/runtime.h>
// 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"))
}