PolycodeAppDelegate.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. [[window windowController] setShouldCascadeWindows:NO];
  30. [window setFrameAutosaveName:[window representedFilename]];
  31. app = new PolycodeIDEApp(polycodeView);
  32. app->addEventListener(eventHandler, PolycodeIDEApp::EVENT_SHOW_MENU);
  33. if(fileToOpen != "") {
  34. app->openProject(fileToOpen);
  35. }
  36. timer = [NSTimer timerWithTimeInterval:(1.0f/90.0f) target:self selector:@selector(animationTimer:) userInfo:nil repeats:YES];
  37. [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
  38. [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode]; // ensure timer fires during resize
  39. }
  40. - (void)animationTimer:(NSTimer *)timer
  41. {
  42. if(!app->Update()) {
  43. [[NSApplication sharedApplication] terminate:self];
  44. }
  45. if(mustShowProjectMenu) {
  46. NSPoint menuOrigin = NSMakePoint(polycodeView.mouseX, polycodeView.mouseY);
  47. NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown
  48. location:menuOrigin
  49. modifierFlags:NSLeftMouseDownMask // 0x100
  50. timestamp:nil
  51. windowNumber:[window windowNumber]
  52. context:[window graphicsContext]
  53. eventNumber:0
  54. clickCount:1
  55. pressure:1];
  56. [NSMenu popUpContextMenu:projectMenu withEvent:event forView:polycodeView];
  57. mustShowProjectMenu = NO;
  58. }
  59. }
  60. - (void) showProjectMenu {
  61. mustShowProjectMenu = YES;
  62. }
  63. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  64. }
  65. - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)theApplication
  66. {
  67. app->saveConfigFile();
  68. bool retVal = app->quitApp();
  69. if(retVal) {
  70. app->core->Shutdown();
  71. printf("STOPPING\n");
  72. }
  73. return retVal;
  74. }
  75. - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
  76. {
  77. return YES;
  78. }
  79. - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
  80. {
  81. // this gets called before applicationDidFinishLaunching if
  82. // user double-clicks the file to open the app
  83. if(app) {
  84. app->openProject([filename cStringUsingEncoding:NSUTF8StringEncoding]);
  85. } else {
  86. fileToOpen = [filename cStringUsingEncoding:NSUTF8StringEncoding];
  87. }
  88. return YES;
  89. }
  90. -(IBAction) refreshProject: (id) sender {
  91. app->refreshProject();
  92. }
  93. -(IBAction) renameFile: (id) sender {
  94. app->renameFile();
  95. }
  96. -(IBAction) removeFile: (id) sender {
  97. app->removeFile();
  98. }
  99. -(IBAction) newGroup: (id) sender {
  100. app->newGroup();
  101. }
  102. -(IBAction) browseExamples: (id) sender {
  103. app->browseExamples();
  104. }
  105. -(IBAction) runProject: (id) sender {
  106. app->runProject();
  107. }
  108. -(IBAction) exportProject: (id) sender {
  109. app->exportProject();
  110. }
  111. -(IBAction) newProject: (id) sender {
  112. app->newProject();
  113. }
  114. -(IBAction) newFile: (id) sender {
  115. app->newFile();
  116. }
  117. -(IBAction) openProject: (id) sender {
  118. app->openProject();
  119. }
  120. -(IBAction) closeProject: (id) sender {
  121. app->closeProject();
  122. }
  123. -(IBAction) closeFile: (id) sender {
  124. app->closeFile();
  125. }
  126. -(IBAction) saveFile: (id) sender {
  127. app->saveFile();
  128. }
  129. -(IBAction) findText: (id) sender {
  130. app->findText();
  131. }
  132. -(IBAction) openDocs: (id) sender {
  133. app->openDocs();
  134. }
  135. -(IBAction) showAbout: (id) sender {
  136. app->showAbout();
  137. }
  138. -(IBAction) toggleConsole: (id) sender {
  139. app->toggleConsole();
  140. }
  141. -(IBAction) showSettings: (id) sender {
  142. app->showSettings();
  143. }
  144. -(IBAction) createNewTab: (id) sender {
  145. app->createNewTab();
  146. }
  147. -(IBAction) showNextTab: (id) sender {
  148. app->showNextTab();
  149. }
  150. -(IBAction) showPreviousTab: (id) sender {
  151. app->showPreviousTab();
  152. }
  153. -(IBAction) closeTab: (id) sender {
  154. app->closeTab();
  155. }
  156. @end