-
Notifications
You must be signed in to change notification settings - Fork 3
/
SimpleView.go
68 lines (53 loc) · 1.83 KB
/
SimpleView.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
/*
#cgo CFLAGS: -I/System/Library/Frameworks/CoreGraphics.framework/Versions/A/Headers/
#cgo LDFLAGS: -framework AppKit
#include <objc/objc-runtime.h>
#include <CoreGraphics.h>
*/
import "C"
import (
. "gocoa"
"unsafe"
)
/*
* The following is an example of something that I don't understand well and represents a problem:
* the exported symbol names seem to determine whether the funtions can be found by dlsym.
* If all are named similarly, only one is found. If they are dissimilar enough in their first
* character, they apparently can be loaded.
*
* I have a bug open that may be resolved with some coming linker changes, remains to be seen.
*/
//export IInitWithFrame
func IInitWithFrame(self C.id, op C.SEL, aRect C.CGRect) C.id {
rect := TypeNSRect(aRect)
simpleView := (Object)(unsafe.Pointer(self))
simpleView = simpleView.Class().Instance("alloc")
simpleView = simpleView.CallSuper("initWithFrame:", rect)
return (C.id)(unsafe.Pointer(simpleView))
}
//export VDrawRect
func VDrawRect(self C.id, op C.SEL, aRect C.CGRect) {
rect := TypeNSRect(aRect)
NSColor(WhiteColor).Call("set")
ClassForName("NSBezierPath").Instance("fillRect:", rect)
NSColor(GreenColor).Call("set")
rect2 := MakeNSRect(5,5,50,50)
ClassForName("NSBezierPath").Instance("fillRect:", rect2)
}
/*
* main()
* Main function for testing
*/
func main() {
view := ClassForName("NSView").Subclass("SimpleView")
view.AddMethod("initWithFrame:", IInitWithFrame)
view.AddMethod("drawRect:", VDrawRect)
view.Register()
app := ClassForName("NSApplication").Instance("sharedApplication")
bundle := ClassForName("NSBundle").Instance("alloc")
dict := NSDictionary("NSOwner", app)
bundle = bundle.Call("initWithPath:", NSString("."))
bundle.Call("loadNibFile:externalNameTable:withZone:", NSString("SimpleView"), dict, app.Call("zone"))
app.Call("run")
}