PolycodeAppDelegate.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // PolycodeAppDelegate.m
  3. // Polycode
  4. //
  5. // Created by Ivan Safrin on 11/28/10.
  6. // Copyright 2010 Local Projects. All rights reserved.
  7. //PolycodeIDEApp
  8. #import "PolycodeAppDelegate.h"
  9. void PolycodeAppEventHandler::handleEvent(Event *evt) {
  10. switch(evt->getEventCode()) {
  11. case PolycodeIDEApp::EVENT_SHOW_MENU:
  12. [appDelegate showProjectMenu];
  13. break;
  14. }
  15. }
  16. @implementation PolycodeAppDelegate
  17. @synthesize window;
  18. @synthesize polycodeView;
  19. @synthesize projectMenu;
  20. - (id) init {
  21. app = NULL;
  22. return [super init];
  23. }
  24. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  25. // Insert code here to initialize your application
  26. mustShowProjectMenu = NO;
  27. eventHandler = new PolycodeAppEventHandler();
  28. eventHandler->appDelegate = self;
  29. app = new PolycodeIDEApp(polycodeView);
  30. app->addEventListener(eventHandler, PolycodeIDEApp::EVENT_SHOW_MENU);
  31. if(fileToOpen != "") {
  32. app->openProject(fileToOpen);
  33. }
  34. timer = [NSTimer timerWithTimeInterval:(1.0f/90.0f) target:self selector:@selector(animationTimer:) userInfo:nil repeats:YES];
  35. [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
  36. [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode]; // ensure timer fires during resize
  37. }
  38. - (void)animationTimer:(NSTimer *)timer
  39. {
  40. if(!app->Update()) {
  41. [[NSApplication sharedApplication] terminate:self];
  42. }
  43. if(mustShowProjectMenu) {
  44. NSPoint menuOrigin = NSMakePoint(polycodeView.mouseX, polycodeView.mouseY);
  45. NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown
  46. location:menuOrigin
  47. modifierFlags:NSLeftMouseDownMask // 0x100
  48. timestamp:nil
  49. windowNumber:[window windowNumber]
  50. context:[window graphicsContext]
  51. eventNumber:0
  52. clickCount:1
  53. pressure:1];
  54. [NSMenu popUpContextMenu:projectMenu withEvent:event forView:polycodeView];
  55. mustShowProjectMenu = NO;
  56. }
  57. }
  58. - (void) showProjectMenu {
  59. mustShowProjectMenu = YES;
  60. }
  61. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  62. }
  63. - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)theApplication
  64. {
  65. bool retVal = app->quitApp();
  66. if(retVal) {
  67. app->saveConfigFile();
  68. app->core->Shutdown();
  69. printf("STOPPING\n");
  70. }
  71. return retVal;
  72. }
  73. - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
  74. {
  75. return YES;
  76. }
  77. - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
  78. {
  79. // this gets called before applicationDidFinishLaunching if
  80. // user double-clicks the file to open the app
  81. if(app) {
  82. app->openProject([filename cStringUsingEncoding:NSUTF8StringEncoding]);
  83. } else {
  84. fileToOpen = [filename cStringUsingEncoding:NSUTF8StringEncoding];
  85. }
  86. return YES;
  87. }
  88. -(IBAction) refreshProject: (id) sender {
  89. app->refreshProject();
  90. }
  91. -(IBAction) renameFile: (id) sender {
  92. app->renameFile();
  93. }
  94. -(IBAction) removeFile: (id) sender {
  95. app->removeFile();
  96. }
  97. -(IBAction) newGroup: (id) sender {
  98. app->newGroup();
  99. }
  100. -(IBAction) browseExamples: (id) sender {
  101. app->browseExamples();
  102. }
  103. -(IBAction) runProject: (id) sender {
  104. app->runProject();
  105. }
  106. -(IBAction) exportProject: (id) sender {
  107. app->exportProject();
  108. }
  109. -(IBAction) newProject: (id) sender {
  110. app->newProject();
  111. }
  112. -(IBAction) newFile: (id) sender {
  113. app->newFile();
  114. }
  115. -(IBAction) openProject: (id) sender {
  116. app->openProject();
  117. }
  118. -(IBAction) closeProject: (id) sender {
  119. app->closeProject();
  120. }
  121. -(IBAction) closeFile: (id) sender {
  122. app->closeFile();
  123. }
  124. -(IBAction) saveFile: (id) sender {
  125. app->saveFile();
  126. }
  127. -(IBAction) findText: (id) sender {
  128. app->findText();
  129. }
  130. -(IBAction) openDocs: (id) sender {
  131. app->openDocs();
  132. }
  133. -(IBAction) showAbout: (id) sender {
  134. app->showAbout();
  135. }
  136. -(IBAction) toggleConsole: (id) sender {
  137. app->toggleConsole();
  138. }
  139. -(IBAction) showSettings: (id) sender {
  140. app->showSettings();
  141. }
  142. @end