-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcocoa.nim
171 lines (128 loc) · 5.06 KB
/
cocoa.nim
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import objc, foundation, strutils, macros, typetraits, math
type
NSObject = object of RootObj
id: ID
NSWindow = object of NSObject
NSWindowController = object of NSObject
NSView = object of NSObject
NSTextView = object of NSView
NSString = object of NSObject
NSApplication = object of NSObject
proc `@`*(a: string): NSString =
result.id = objc_msgSend(getClass("NSString").ID, $$"stringWithUTF8String:", a.cstring)
proc objc_alloc(cls: string): ID =
objc_msgSend(getClass(cls).ID, $$"alloc")
proc autorelease(obj: NSObject) =
discard objc_msgSend(obj.id, $$"autorelease")
proc init(x: typedesc[NSWindow], rect: CMRect, mask: int, backing: int, xdefer: BOOL): NSWindow =
var wnd = objc_alloc("NSWindow")
var cmd = $$"initWithContentRect:styleMask:backing:defer:"
result.id = wnd.objc_msgSend(cmd, rect, mask.uint64, backing.uint64, xdefer)
proc init(x: typedesc[NSWindowController], window: NSWindow): NSWindowController =
var ctrl = objc_alloc("NSWindowController")
result.id = ctrl.objc_msgSend($$"initWithWindow:", window.id)
proc contentView(self: NSWindow, view: NSView) =
discard objc_msgSend(self.id, $$"setContentView:", view.id)
proc init(x: typedesc[NSTextView], rect: CMRect): NSTextView =
var view = objc_alloc("NSTextView")
result.id = view.objc_msgSend($$"initWithFrame:", rect)
proc insertText(self: NSTextView, text: string) =
discard objc_msgSend(self.id, $$"insertText:", @text.id)
proc call(cls: typedesc, cmd: SEL) =
discard objc_msgSend(getClass(cls.name).ID, cmd)
proc `[]`(obj: NSObject, cmd: SEL) =
discard objc_msgSend(obj.id, cmd)
macro `[]`(id: ID, cmd: SEL, args: varargs[untyped]): untyped =
if args.len > 0:
let p = "discard objc_msgSend($1, $2, $3)"
var z = ""
for a in args:
z.add(a.toStrLit().strVal)
var w = p % [id.toStrLit().strVal, cmd.toStrLit().strVal, z]
result = parseStmt(w)
else:
let p = "discard objc_msgSend($1, $2)"
var w = p % [id.toStrLit().strVal, cmd.toStrLit().strVal]
result = parseStmt(w)
type
AppDelegate = object
isa: Class
window: ID
proc shouldTerminate(self: ID, cmd: SEL, notification: ID): BOOL {.cdecl.} =
var cls = self.getClass()
var ivar = cls.getIvar("apple")
var res = cast[int](self.getIvar(ivar))
echo res
result = YES
proc makeDelegate(): Class =
result = allocateClassPair(getClass("NSObject"), "AppDelegate", 0)
discard result.addMethod($$"applicationShouldTerminateAfterLastWindowClosed:", cast[IMP](shouldTerminate), "c@:@")
echo result.addIvar("apple", sizeof(int), log2(sizeof(int).float64).int, "q")
result.registerClassPair()
proc getSuperMethod(id: ID, sel: SEL): Method =
var superClass = getSuperClass(id.getClass)
result = getInstanceMethod(superClass, sel)
macro callSuper(id: ID, cmd: SEL, args: varargs[untyped]): untyped =
let sid = id.toStrLit().strVal
let scmd = cmd.toStrLit().strVal
let mm = "getSuperMethod($1, $2)" % [sid, scmd]
if args.len > 0:
let p = "discard method_invoke($1, $2, $3)"
var z = ""
for a in args:
z.add(a.toStrLit().strVal)
var w = p % [sid, mm, z]
echo w
result = parseStmt(w)
else:
let p = "discard method_invoke($1, $2)"
var w = p % [sid, mm]
result = parseStmt(w)
proc canBe(self: ID, cmd: SEL): BOOL {.cdecl.} =
result = YES
proc canBecome(id: ID) =
var cls = getClass(id)
var sel = $$"showsResizeIndicator"
var im = getInstanceMethod(cls, sel)
var types = getTypeEncoding(im)
discard replaceMethod(cls, sel, cast[IMP](canBe), types)
#sel = $$"canBecomeMainWindow"
#im = getInstanceMethod(cls, sel)
#types = getTypeEncoding(im)
#discard replaceMethod(cls, sel, cast[IMP](canBe), types)
proc main() =
var pool = newClass("NSAutoReleasePool")
NSApplication.call $$"sharedApplication"
if NSApp.isNil:
echo "Failed to initialized NSApplication... terminating..."
return
NSApp[$$"setActivationPolicy:", NSApplicationActivationPolicyRegular]
var windowStyle = NSTitledWindowMask or NSClosableWindowMask or
NSMiniaturizableWindowMask or NSResizableWindowMask
var windowRect = NSMakeRect(100,100,400,400)
var window = NSWindow.init(windowRect, windowStyle, NSBackingStoreBuffered, NO)
window.autorelease()
#canBecome(window.id)
#var windowController = NSWindowController.init(window)
#windowController.autorelease()
#var textView = NSTextView.init(windowRect)
#textView.autorelease()
#textView.insertText("Hello OSX/Cocoa World!")
#window.contentView(textView)
#window[$$"orderFrontRegardless"]
#window.id[$$"makeKeyAndOrderFront:", window.id]
window.id[$$"setTitle:", @"Hello".id]
#window.id[$$"setShowsResizeIndicator:", true]
#echo cast[int](objc_msgSend(window.id, $$"showsResizeIndicator"))
#echo cast[int](objc_msgSend(window.id, $$"resizeFlags"))
var AppDelegate = makeDelegate()
var appDel = newClass("AppDelegate")
var ivar = AppDelegate.getIvar("apple")
setIvar(appDel, ivar, cast[ID](123))
NSApp[$$"setDelegate:", appDel]
window.id[$$"display"]
window.id[$$"orderFrontRegardless"]
NSApp[$$"run"]
pool[$$"drain"]
AppDelegate.disposeClassPair()
main()