7
# include <Foundation/Foundation.h>
8
# include <AppKit/AppKit.h>
11
#include "acocoa.h" /* Argyll Cocoa support functions and defines */
14
- (void) someMethod:...
20
// process all args with va_arg
26
/* Our static instance variables for AppDelegate */
28
id window; /* NSWindow */
32
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
34
void MyView_setCntx(id self, SEL _cmd, void *val) {
35
object_setInstanceVariable(self, "cntx", val);
38
void MyView_drawRect(id self, SEL _cmd, NSRect rect) {
39
double w = rect.size.width, h = rect.size.height;
41
id aPath = sendClassMsg("NSBezierPath", "bezierPath");
42
sendMsg(aPath, "setLineWidth:", 2.0);
43
sendMsg(aPath, "moveToPoint:", NSMakePoint(0.0, 0.0));
44
sendMsg(aPath, "lineToPoint:", NSMakePoint(0.9 * w, 0.9 * h));
45
sendMsg(aPath, "appendBezierPathWithRect:", NSMakeRect(0.5 * w, 0.5 * h,
47
sendMsg(aPath, "stroke");
50
id att = newObject("NSDictionary");
51
id str = newNSString("String");
52
sendMsg(str, "drawAtPoint:withAttributes:", NSMakePoint(0.1 * w, 0.1 * h), att);
57
void MyView_dealloc(id self, SEL _cmd) {
61
// Create our custom NSView */
63
method_info minfo[] = {
64
{ "setCntx:", (IMP)MyView_setCntx, "v@:^v" },
65
{ "drawRect:", (IMP)MyView_drawRect, "v@:@" },
66
{ "dealloc", (IMP)MyView_dealloc, "v@:" },
70
variable_info vinfo[] = {
71
{ "cntx", sizeof(void *), "^v" },
75
registerClass("MyView", "NSView", minfo, vinfo);
78
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
80
void MyWin_setCntx(id self, SEL _cmd, void *val) {
81
object_setInstanceVariable(self, "cntx", val);
84
void MyWin_keyDown(id self, SEL _cmd, id event) {
90
etype = (int)sendMsg(event, "type");
91
str = sendMsg(event, "characters");
92
cstr = cNSString(str);
93
printf("Got Window KeyDown type %d, chars '%s'\n",etype, cstr);
96
sendMsg(NSApp, "terminate:", self);
100
void MyWin_dealloc(id self, SEL _cmd) {
101
delObjectSuper(self);
104
// Create our custom NSWin */
106
method_info minfo[] = {
107
{ "setCntx:", (IMP)MyWin_setCntx, "v@:^v", },
108
{ "keyDown:", (IMP)MyWin_keyDown, "v@:@", },
109
{ "dealloc", (IMP)MyWin_dealloc, "v@:" },
113
variable_info vinfo[] = {
114
{ "cntx", sizeof(void *), "^v" },
118
registerClass("MyWin", "NSWindow", minfo, vinfo);
121
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
123
/* Create all the bits */
124
void AppDel_willFinishLaunching(id self, SEL _cmd, id notification) {
126
id label; /* NSTextField */
128
cx = calloc(1, sizeof(cntx_t));
130
// Set cntx to to allocated structure
131
object_setInstanceVariable(self, "cntx", (void *)cx);
134
cx->window = sendClassMsg("MyWin", "alloc");
135
cx->window = sendMsg(cx->window,
136
"initWithContentRect:styleMask:backing:defer:",
137
NSMakeRect(300, 300, 200, 100),
139
| NSClosableWindowMask
140
| NSMiniaturizableWindowMask
141
| NSResizableWindowMask,
142
NSBackingStoreBuffered,
145
/* Make the background white */
146
sendMsg(cx->window, "setBackgroundColor:", sendClassMsg("NSColor","whiteColor"));
149
sendMsg(cx->window, "setTitle:", newNSString("Hello World"));
153
label = sendClassMsg("NSTextField", "alloc");
154
label = sendMsg(label, "initWithFrame:", NSMakeRect(30, 30, 80, 30));
156
sendMsg(label, "setSelectable:", NO);
157
sendMsg(label, "setBezeled:", NO);
158
sendMsg(label, "setDrawsBackground:", NO);
159
sendMsg(label, "setStringValue:", newNSString("Hello World"));
161
/* Hmm. How does this work ? */
162
cx->view = sendMsg(cx->window, "contentView");
163
sendMsg(cx->view, "addSubview:", label);
165
/* Use our custom view to draw contents */
166
cx->view = newObject("MyView");
168
sendMsg(cx->view, "setCntx:", (void *)cx);
169
sendMsg(cx->window, "setContentView:", cx->view);
171
// sendMsg(cx->window, "setInitialFirstResponder:", cx->view);
176
// sendEvent: gets messages.
178
// Set above the screen saver
179
// [aWindow setLevel:NSScreenSaverWindowLevel + 1];
181
// setCollectionBehavior: NSWindowCollectionBehaviorIgnoresCycle
182
// NSWindowCollectionBehaviorFullScreenPrimary
183
// NSWindowCollectionBehaviorStationary
184
// NSWindowCollectionBehaviorIgnoresCycle
187
// mouseDownCanMoveWindow
189
// setContentMinSize: and setContentMaxSize:
191
// NSRect frame = [myWindow frame];
192
// if (frame.size.width <= MIN_WIDTH_WITH_ADDITIONS)
193
// frame.size.width = MIN_WIDTH_WITH_ADDITIONS;
194
// frame.size.height += ADDITIONS_HEIGHT;
195
// frame.origin.y -= ADDITIONS_HEIGHT;
196
// [myWindow setFrame:frame display:YES animate:YES];
197
// objc_msgSend(label, "release"));
199
// setExcludedFromWindowsMenu:YES
201
// setBackgroundColor: and setAlphaValue:
203
// WindowImage object:
207
// NSTrackingArea class, along with the cursorUpdate: method of the NSResponder class
212
void AppDel_didFinishLaunching(id self, SEL _cmd, id notification) {
215
object_getInstanceVariable(self, "cntx", (void **)&cx);
216
sendMsg(cx->window, "makeKeyAndOrderFront:", self);
218
#ifdef NEVER /* Test terminate */
220
sendMsg(NSApp, "terminate:", self);
224
/* Should the application terminate ? */
225
NSApplicationTerminateReply AppDel_shouldTerminate(id self, SEL _cmd, id notification) {
226
return NSTerminateNow;
230
void AppDel_dealloc(id self, SEL _cmd) {
233
object_getInstanceVariable(self, "cntx", (void **)&cx);
234
delObject(cx->window);
235
delObjectSuper(self);
238
// Create the delegate class that implements our window
239
void createAppDelClass() {
240
method_info minfo[] = {
241
{ "applicationWillFinishLaunching:", (IMP)AppDel_willFinishLaunching, "v@:@" },
242
{ "applicationDidFinishLaunching:", (IMP)AppDel_didFinishLaunching, "v@:@" },
243
{ "applicationShouldTerminate:", (IMP)AppDel_shouldTerminate, "i@:@" },
244
{ "dealloc", (IMP)AppDel_dealloc, "v@:" },
248
variable_info vinfo[] = {
249
{ "cntx", sizeof(void *), "^v" },
253
registerClass("AppDelegate", "NSObject", minfo, vinfo);
256
/* - - - - - - - - - - - - - - - - - - - - - - - - */
257
int main(int argc, char** argv) {
261
/* Transform process so that it interacts with desktop properly */
264
/* Create an autorelease pool */
265
pool = newObject("NSAutoreleasePool");
267
// Create all the classes we override
272
// Get our shared NSApplication and start it
273
sendClassMsg("NSApplication", "sharedApplication");
276
fprintf(stderr,"Failed to initialized NSApplication... terminating...\n");
280
/* Set a delegate to create the window */
281
appDelObj = newObject("AppDelegate");
282
sendMsg(NSApp, "setDelegate:", appDelObj);
284
// if running on 10.7:
285
// sendMsg(NSApp, "disableRelaunchOnLogin"));
287
/* Call the run loop */
288
sendMsg(NSApp, "run");
290
// detachDrawingThread:toTarget:withObject:
293
sendMsg(NSApp, "terminate:", self);
296
/* We're done with the pool */
304
- (void)drawRect:(NSRect)rect
306
NSRect r = NSMakeRect(10, 10, 50, 60);
307
NSBezierPath *bp = [NSBezierPath bezierPathWithRect:r];
308
NSColor *color = [NSColor blueColor];