PageRenderTime 75ms CodeModel.GetById 1ms app.highlight 69ms RepoModel.GetById 1ms app.codeStats 1ms

/platform/osx/objc/window.m

http://github.com/wilkie/djehuty
Objective C | 398 lines | 228 code | 103 blank | 67 comment | 7 complexity | 6ead61631cd29a077bee858c0f90cf67 MD5 | raw file
  1#include <Cocoa/Cocoa.h>
  2#include <Foundation/Foundation.h>
  3
  4#include "OSXView.h"
  5
  6enum OSXEvents
  7{
  8	// Window Events
  9
 10	EventResize, // parameters: (width, height) of window
 11
 12	// Mouse Events, params are (x, y) coordinates
 13
 14	EventPrimaryDown,
 15	EventSecondaryDown,
 16	EventTertiaryDown,
 17	EventPrimaryUp,
 18	EventSecondaryUp,
 19	EventTertiaryUp,
 20	EventMouseMove,
 21
 22	EventMouseExit,
 23	EventMouseEnter,
 24
 25	EventWindowClose,
 26
 27	EventOtherDown = 0xff,
 28	EventOtherUp = 0xffff,
 29};
 30
 31void OSXEventRoutine(void* window, int event, int p1, int p2);
 32void _D_OSXInitView(void* windPtr, struct _OSXViewPlatformVars* viewVars);
 33
 34@interface _OSXWindow : NSWindow {
 35	@public
 36		void* window;
 37		struct _OSXWindowPlatformVars* window_info;
 38}
 39
 40@end
 41
 42@interface _OSXView : NSView <NSWindowDelegate> {
 43	@public
 44		void* window;
 45		struct _OSXWindowPlatformVars* window_info;
 46		bool acceptMouse;
 47}
 48- (void)tick:(NSTimer*)timer;
 49- (void)OSX_HandleEvent:(NSEvent*)event;
 50@end
 51
 52@implementation _OSXWindow
 53
 54- (void)drawRect:(NSRect)rect {
 55	window_info->viewVars->nsRect = [ window_info->viewVars->viewRef bounds ];
 56
 57	[ window_info->viewVars->viewRef setNeedsDisplay:YES ];
 58
 59	OSXEventRoutine(window_info->windowClassRef, EventResize, window_info->viewVars->nsRect.size.width, window_info->viewVars->nsRect.size.height);
 60}
 61
 62-(BOOL)isFlipped {
 63	return YES;
 64}
 65
 66- (BOOL)acceptsMouseMovedEvents {
 67    return YES;
 68}
 69@end
 70
 71@implementation _OSXView
 72
 73-(BOOL)isFlipped {
 74	return YES;
 75}
 76
 77-(void)windowWillClose:(NSNotification *)notification {
 78	OSXEventRoutine(window_info->windowClassRef, EventWindowClose, 0, 0);
 79}
 80
 81- (BOOL)acceptsFirstResponder {
 82    return YES;
 83}
 84
 85- (BOOL)acceptsMouseMovedEvents {
 86    return YES;
 87}
 88/*
 89-(void)windowDidExpose:(NSNotification*)aNotification {
 90	printf("OBJ-C: done with windowDidExpose\n");
 91}
 92
 93-(void)windowDidUpdate:(NSNotification*)aNotification {
 94	window_info->viewVars->nsRect = [ window_info->viewVars->viewRef bounds ];
 95
 96	[ window_info->viewVars->viewRef setNeedsDisplay:YES ];
 97
 98	OSXEventRoutine(window_info->windowClassRef, EventResize, window_info->viewVars->nsRect.size.width, window_info->viewVars->nsRect.size.height);
 99
100	printf("OBJ-C: done with windowDidUpdate\n");
101}
102
103-(void)windowDidResize:(NSNotification*)aNotification {
104	printf("OBJ-C: done with windowDidResize\n");
105}
106*/
107- (void)drawRect:(NSRect)rect {
108	window_info->viewVars->nsRect = [ window_info->viewVars->viewRef bounds ];
109
110	[ window_info->viewVars->viewRef setNeedsDisplay:YES ];
111
112	OSXEventRoutine(window_info->windowClassRef, EventResize, window_info->viewVars->nsRect.size.width, window_info->viewVars->nsRect.size.height);
113
114//	printf("OBJ-C: done with drawRect\n");
115}
116
117- (void)mouseExited:(NSEvent*)event {
118	acceptMouse = false;
119	[ self OSX_HandleEvent:event ];
120}
121
122- (void)mouseEntered:(NSEvent*)event {
123	acceptMouse = true;
124	[ self OSX_HandleEvent:event ];
125}
126
127- (void)mouseDown:(NSEvent*)event {
128	[ self OSX_HandleEvent:event ];
129}
130
131- (void)rightMouseDown:(NSEvent*)event {
132	[ self OSX_HandleEvent:event ];
133}
134
135- (void)otherMouseDown:(NSEvent*)event {
136	[ self OSX_HandleEvent:event ];
137}
138
139- (void)mouseUp:(NSEvent*)event {
140	[ self OSX_HandleEvent:event ];
141}
142
143- (void)rightMouseUp:(NSEvent*)event {
144	[ self OSX_HandleEvent:event ];
145}
146
147- (void)otherMouseUp:(NSEvent*)event {
148	[ self OSX_HandleEvent:event ];
149}
150
151- (void)mouseMoved:(NSEvent*)event {
152	if (acceptMouse) {
153		[ self OSX_HandleEvent:event ];
154	}
155}
156
157- (void)mouseDragged:(NSEvent*)event {
158	[ self OSX_HandleEvent:event ];
159}
160
161- (void)OSX_HandleEvent:(NSEvent*)event {
162	NSPoint coord;
163
164	switch ( [ event type ] ) {
165
166	case NSMouseExited:
167		OSXEventRoutine(window_info->windowClassRef, EventMouseExit, 0, 0);
168		break;
169	
170	case NSMouseEntered:
171		OSXEventRoutine(window_info->windowClassRef, EventMouseEnter, 0, 0);
172		break;
173
174	case NSLeftMouseDown:
175	case NSRightMouseDown:
176	case NSOtherMouseDown:
177
178		coord = [ window_info->viewVars->viewRef convertPoint:[ event locationInWindow ] fromView: nil ];
179
180		int xysend;
181		xysend = coord.x;
182		xysend |= ((int)coord.y << 16);
183
184		switch ( [ event buttonNumber ] ) {
185			case 0:
186				OSXEventRoutine(window_info->windowClassRef, EventPrimaryDown, xysend, [ event clickCount ] );
187				break;
188			case 1:
189				OSXEventRoutine(window_info->windowClassRef, EventSecondaryDown, xysend, [ event clickCount ] );
190				break;
191			case 2:
192				OSXEventRoutine(window_info->windowClassRef, EventTertiaryDown, xysend, [ event clickCount ] );
193				break;
194			default:
195				OSXEventRoutine(window_info->windowClassRef, EventOtherDown + ( [ event buttonNumber ] - 3 ), xysend, [ event clickCount ] );
196				break;
197		}
198
199		break;
200
201	case NSLeftMouseUp:
202	case NSRightMouseUp:
203	case NSOtherMouseUp:
204
205		coord = [ window_info->viewVars->viewRef convertPoint:[ event locationInWindow ] fromView: nil ];
206
207		int xysendup;
208		xysendup = coord.x;
209		xysendup |= ((int)coord.y << 16);
210
211		switch ( [ event buttonNumber ] ) {
212			case 0:
213				OSXEventRoutine(window_info->windowClassRef, EventPrimaryUp, xysendup, [ event clickCount ] );
214				break;
215			case 1:
216				OSXEventRoutine(window_info->windowClassRef, EventSecondaryUp, xysendup, [ event clickCount ] );
217				break;
218			case 2:
219				OSXEventRoutine(window_info->windowClassRef, EventTertiaryUp, xysendup, [ event clickCount ] );
220				break;
221			default:
222				OSXEventRoutine(window_info->windowClassRef, EventOtherUp + ( [ event buttonNumber ] - 3 ), xysendup, [ event clickCount ] );
223				break;
224		}
225
226		break;
227
228	case NSMouseMoved:
229	case NSLeftMouseDragged:
230	case NSRightMouseDragged:
231	case NSOtherMouseDragged:
232
233		coord = [ window_info->viewVars->viewRef convertPoint:[ event locationInWindow ] fromView: nil ];
234
235		int xysendmove;
236		xysendmove = coord.x;
237		xysendmove |= ((int)coord.y << 16);
238
239		OSXEventRoutine(window_info->windowClassRef, EventMouseMove, xysendmove, [ event clickCount ] );
240
241		break;
242	}
243}
244
245
246// FOCUS //
247
248//- (void)windowDidBecomeKey:(NSNotification *)notification {
249	/*// control receives focus due to window
250
251	//fire event
252	FIRE_WINDOW_EVENT(window, EventGotFocus, 0, 0);
253
254	//currently focused control will regain focus
255	//all controls receive message ???
256
257	_internal_file_event_GotFocus(window);*/
258
259	//return;
260//}
261
262- (void)windowDidResignKey:(NSNotification *)notification {
263	/*// control loses focus due to window
264
265	//fire event
266	FIRE_WINDOW_EVENT(window, EventLostFocus, 0, 0);
267
268	//currently focused control will regain focus
269	//all controls receive message ???
270
271	_internal_file_event_LostFocus(window);*/
272}
273
274// ACTION MESSAGES //
275
276-(void)_osx_redraw {
277	//Scaffold.RequestRedraw(window_info);
278}
279
280// TIMERS //
281
282- (void)tick:(NSTimer*)timer {
283	/*NSValue* val = (NSValue*)[ timer userInfo ];
284
285	void* ptr_val = [ val pointerValue ];
286
287	_internal_timer_info* tmr_info = (_internal_timer_info*)ptr_val;
288
289	_internal_file_event_Timer(window, tmr_info);*/
290}
291
292
293@end
294
295
296
297
298
299
300
301void _OSXWindowShow(struct _OSXWindowPlatformVars* window, int bShow) {
302	NSWindow* wnd = window->windowRef;
303
304	if (bShow) {
305		[ wnd makeKeyAndOrderFront: nil ]; // displays
306	} else {
307		[ wnd orderOut: nil ]; // hides
308	}
309}
310
311void _OSXWindowSetTitle(struct _OSXWindowPlatformVars* window, char* str) {
312	NSString* nss = [[NSString alloc] initWithUTF8String: str];
313    [window->windowRef setTitle: nss];
314}
315
316struct _OSXViewPlatformVars* _OSXWindowCreate(void* windowRef, struct _OSXWindowPlatformVars* parent, struct _OSXWindowPlatformVars** window, char* initTitle, int initX, int initY, int initW, int initH) {
317
318    // initialize the rectangle variable
319	NSRect graphicsRect = NSMakeRect(initX,initY,initW,initH); //window->_initial_x,window->_initial_y,window->_initial_width,window->_initial_height);
320
321	(*window) = malloc( sizeof( struct _OSXWindowPlatformVars ) );
322	(*window)->viewVars = malloc( sizeof( struct _OSXViewPlatformVars) );
323
324	(*window)->windowClassRef = windowRef;
325
326	(*window)->viewVars->layout = [[NSLayoutManager alloc] init];
327
328	(*window)->viewVars->container = [[NSTextContainer alloc] init];
329
330	[ (*window)->viewVars->layout addTextContainer: (*window)->viewVars->container ];
331
332	(*window)->viewVars->cur_font = [ [ NSMutableDictionary alloc ] init ];
333
334	(*window)->viewVars->txtstore = [ [ NSTextStorage alloc ] init ];
335
336	[ (*window)->viewVars->txtstore addLayoutManager:(*window)->viewVars->layout ];
337
338    (*window)->viewVars->viewRef = [[[_OSXView alloc] initWithFrame:graphicsRect] autorelease];
339	
340	NSTrackingArea* tarea = [[ NSTrackingArea alloc ] initWithRect:graphicsRect options:(NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect) owner:(*window)->viewVars->viewRef userInfo:nil ];
341	[ (*window)->viewVars->viewRef addTrackingArea:tarea ];
342
343	(*window)->viewVars->viewRef->window_info = *window;
344
345	// the view is responsible for the rest of the setup
346
347	(*window)->windowRef = (*window)->viewVars->viewRef->window = [ [_OSXWindow alloc]              // create the window
348               initWithContentRect: graphicsRect
349                         styleMask:NSTitledWindowMask
350                                  |NSClosableWindowMask
351                                  |NSMiniaturizableWindowMask
352                           backing:NSBackingStoreBuffered
353                             defer:YES ];
354
355	_OSXWindowSetTitle(*window, initTitle);
356
357    [ (*window)->windowRef setContentView:(NSView*)(*window)->viewVars->viewRef ];    // set window's view
358	[ (*window)->windowRef setDelegate: (*window)->viewVars->viewRef ];
359	[ (*window)->windowRef makeFirstResponder: (*window)->viewVars->viewRef ];
360	[ (*window)->windowRef setAcceptsMouseMovedEvents:NO ];
361	[ (*window)->windowRef setAcceptsMouseMovedEvents:YES ];
362
363	return (*window)->viewVars;
364}
365
366void _OSXWindowStartDraw(struct _OSXWindowPlatformVars* windVars, struct _OSXViewPlatformVars* viewVars, int isSysColorWindow, double r, double g, double b) {
367	// This stops it from painting upside down (for some reason)
368	[ viewVars->viewRef setNeedsDisplay:YES ];
369
370	// StartDraw
371
372//	printf("start draw (OBJ-C)\n");
373
374	//Fill the background of the window with the window color
375	if (!isSysColorWindow) {
376		NSColor* clr;
377
378		clr = [ NSColor colorWithDeviceRed:r green:g blue:b alpha:1.0f ];
379
380		[ clr set ];
381		NSRectFill(viewVars->nsRect);
382	}
383//	printf("start draw (OBJ-C)\n");
384
385	[ [ NSGraphicsContext currentContext ] setShouldAntialias:YES ];
386//	printf("start draw (OBJ-C)\n");
387
388	//Get the CGContextRef
389	viewVars->cgContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
390//	printf("start draw (OBJ-C)\n");
391
392	//Set default colors
393	[ [ NSColor blackColor ] setStroke ];
394//	printf("start draw (OBJ-C)\n");
395	[ [ NSColor whiteColor ] setFill ];
396
397//	printf("start draw done\n");
398}