summaryrefslogtreecommitdiff
path: root/plot/osx/hellom.m
blob: b306878858585c6a0a5503dcb830bea887baab34 (plain)
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
172
173
174
175
176
177
178
179
180
181

#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>

#include <objc/objc-runtime.h>		/* For diagnostics */

typedef struct {
	id window;		/* NSWindow */
	id view;		/* NSTextField */
} cntx_t;

// - - - - - - - - - - - - - - - - - - - - - - - - - 
@interface MyView : NSView {
	void *cntx;
}
- (void)setCntx:(void *)cntx;
@end

@implementation MyView

- (void)setCntx:(void *)val {
	cntx = val;
}

- (void)drawRect:(NSRect)rect {
//	NSGraphicsContext* aContext = [NSGraphicsContext currentContext];

	[[NSColor redColor] setStroke];

	NSBezierPath* aPath = [NSBezierPath bezierPath];
	[aPath setLineWidth:2.0];
	[aPath moveToPoint:NSMakePoint(0.0, 0.0)];
	[aPath lineToPoint:NSMakePoint(100.0, 100.0)];
	[aPath appendBezierPathWithRect:NSMakeRect(20.0, 160.0, 80.0, 50.0)];

	[aPath stroke];

	NSDictionary *att = [NSDictionary new];

	[ @"String" drawAtPoint: NSMakePoint(10.0, 10.0) withAttributes: att ];

/* 

[@"Hello" drawInRect:r withAttributes:[NSDictionary
dictionaryWithObjectsAndKeys:
[NSColor redColor], NSForegroundColorAttributeName,
[NSFont systemFontOfSize:24], NSFontAttributeName,
nil]];

*/
}

@end

/* To trigger an update:

Send a setNeedsDisplayInRect: or setNeedsDisplay: message to the
view. Sending either of these messages marks part or all of the view as invalid

 */

// - - - - - - - - - - - - - - - - - - - - - - - - - 

@interface MyWin : NSWindow {
	void *cntx;
}
- (void)setCntx:(void *)cntx;
@end

@implementation MyWin

- (void)setCntx:(void *)val {
	cntx = val;
}

- (void)keyDown:(NSEvent *)event {
	printf("Got Window KeyDown type %d char %s\n",[event type],
	    [[event characters] cStringUsingEncoding:NSASCIIStringEncoding]);
}

- (BOOL)windowShouldClose:(id)sender {
	printf("Got Window windowShouldClose\n"); 
	[NSApp terminate: nil];
	return YES;
}

@end

// - - - - - - - - - - - - - - - - - - - - - - - - - 

@interface AppDelegate : NSObject {
	void *cntx;
}
@end

@implementation AppDelegate
- (void) applicationWillFinishLaunching: (NSNotification *)not {
	cntx_t *cx;

	cx = calloc(1, sizeof(cntx_t));

	cntx = (void *)cx;

	/* Create Window */
	cx->window = [[MyWin alloc] initWithContentRect: NSMakeRect(300, 300, 200, 100)
                                        styleMask: (NSTitledWindowMask |
	                                                NSClosableWindowMask |
                                                    NSMiniaturizableWindowMask |
                                                    NSResizableWindowMask)
                                          backing: NSBackingStoreBuffered
                                            defer: YES];
	[cx->window setTitle: @"Hello World"];

#ifdef NEVER
	/* Create Label */
	cx->label = [[NSTextField alloc] initWithFrame: NSMakeRect(30, 30, 80, 30)]; 
	[cx->label setSelectable: NO];
	[cx->label setBezeled: NO];
	[cx->label setDrawsBackground: NO];
	[cx->label setStringValue: @"Hello World"];

	[[cx->window contentView] addSubview: cx->label];

	[cx->label release];

#else
	cx->view = [MyView new];
	[cx->view setCntx:(void *)cx];
	[cx->window setContentView: cx->view];
#endif
	// [window setContentView:customView]
}

- (void) applicationDidFinishLaunching: (NSNotification *) not {
	cntx_t *cx = (cntx_t *)cntx;
	[cx->window makeKeyAndOrderFront: self];
}

- (void) dealloc {
	cntx_t *cx = (cntx_t *)cntx;
	[cx->window release];
	[super dealloc];
}
@end

// - - - - - - - - - - - - - - - - - - - - - - - - - 

int main (int argc, const char **argv)
{ 
	NSAutoreleasePool *pool;
	id appDelObj;
   
	if (NSApp == nil) {
		OSStatus stat;
		ProcessSerialNumber psn = { 0, 0 };

		if (GetCurrentProcess(&psn) == noErr) {
			/* Transform the process so that the desktop interacts with it properly. */
			/* We don't need resources or a bundle if we do this. */
			if (psn.lowLongOfPSN != 0 && (stat = TransformProcessType(&psn,
				               kProcessTransformToForegroundApplication)) != noErr)
				fprintf(stderr,"TransformProcess failed with code %d\n",stat);
		}

		pool = [NSAutoreleasePool new];

		[NSApplication sharedApplication];
		appDelObj = [AppDelegate new];
		[NSApp setDelegate: appDelObj];

		// Run the event loop until done
		[NSApp run];

		[pool release];
	}

	// To terminate:
	// [NSApp terminate: nil];
}