1/*******************************
2 Mac support for HID Test GUI
3
4 Alan Ott
5 Signal 11 Software
6*******************************/
7
8#include <fx.h>
9#import <Cocoa/Cocoa.h>
10
11extern FXMainWindow *g_main_window;
12
13
14@interface MyAppDelegate : NSObject
15{
16}
17@end
18
19@implementation MyAppDelegate
20- (void) applicationWillBecomeActive:(NSNotification*)notif
21{
22	printf("WillBecomeActive\n");
23	g_main_window->show();
24
25}
26
27- (void) applicationWillTerminate:(NSNotification*)notif
28{
29	/* Doesn't get called. Not sure why */
30	printf("WillTerminate\n");
31	FXApp::instance()->exit();
32}
33
34- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*)sender
35{
36	/* Doesn't get called. Not sure why */
37	printf("ShouldTerminate\n");
38	return YES;
39}
40
41- (void) applicationWillHide:(NSNotification*)notif
42{
43	printf("WillHide\n");
44	g_main_window->hide();
45}
46
47- (void) handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
48{
49	printf("QuitEvent\n");
50	FXApp::instance()->exit();
51}
52
53@end
54
55extern "C" {
56
57void
58init_apple_message_system()
59{
60	static MyAppDelegate *d = [MyAppDelegate new];
61
62	[[NSApplication sharedApplication] setDelegate:d];
63
64	/* Register for Apple Events. */
65	/* This is from
66	   http://stackoverflow.com/questions/1768497/application-exit-event */
67	NSAppleEventManager *aem = [NSAppleEventManager sharedAppleEventManager];
68	[aem setEventHandler:d
69	     andSelector:@selector(handleQuitEvent:withReplyEvent:)
70	     forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
71}
72
73void
74check_apple_events()
75{
76	NSApplication *app = [NSApplication sharedApplication];
77
78	NSAutoreleasePool *pool = [NSAutoreleasePool new];
79	while (1) {
80		NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
81		                        untilDate:nil
82                                        inMode:NSDefaultRunLoopMode
83                                        dequeue:YES];
84		if (event == NULL)
85			break;
86		else {
87			//printf("Event happened: Type: %d\n", event->_type);
88			[app sendEvent: event];
89		}
90	}
91	[pool release];
92}
93
94} /* extern "C" */
95