mac_support_cocoa.m 2.0 KB

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