MyDocument.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #import "MyDocument.h"
  20. @implementation MyDocument
  21. @synthesize mainView;
  22. @synthesize consoleWindow;
  23. @synthesize consoleTextView;
  24. - (id)init
  25. {
  26. self = [super init];
  27. if (self) {
  28. showingConsole = NO;
  29. }
  30. return self;
  31. }
  32. - (NSString *)windowNibName
  33. {
  34. return @"MyDocument";
  35. }
  36. - (void)windowControllerDidLoadNib:(NSWindowController *) aController
  37. {
  38. [super windowControllerDidLoadNib:aController];
  39. player = new CocoaPolycodePlayer(mainView, [docFileName cStringUsingEncoding:NSASCIIStringEncoding], false);
  40. playerProxy = new PolycodeProxy();
  41. playerProxy->playerDocument = self;
  42. player->addEventListener(playerProxy, PolycodeDebugEvent::EVENT_RESIZE);
  43. player->addEventListener(playerProxy, PolycodeDebugEvent::EVENT_PRINT);
  44. player->addEventListener(playerProxy, PolycodeDebugEvent::EVENT_ERROR);
  45. player->windowData = self;
  46. player->runPlayer();
  47. timer = [NSTimer timerWithTimeInterval:(1.0f/90.0f) target:self selector:@selector(animationTimer:) userInfo:nil repeats:YES];
  48. [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
  49. [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode]; // ensure timer fires during resize
  50. }
  51. - (IBAction) showConsoleWindow: (id) sender
  52. {
  53. if(!showingConsole) {
  54. [consoleWindow makeKeyAndOrderFront:nil];
  55. } else{
  56. [consoleWindow close];
  57. }
  58. showingConsole = !showingConsole;
  59. }
  60. - (void) handleDebugError: (NSString*) error onLine:(int) lineNumber
  61. {
  62. NSTextStorage *textStorage = [consoleTextView textStorage];
  63. [consoleTextView setInsertionPointColor: [NSColor whiteColor]];
  64. [textStorage beginEditing];
  65. NSMutableString *fullText = [[NSMutableString alloc] initWithString:@"Error:\""];
  66. [fullText appendString:error];
  67. [fullText appendFormat:@"\" on line %d.", lineNumber];
  68. NSMutableAttributedString *str = [[NSMutableAttributedString alloc ]initWithString: fullText];
  69. [fullText release];
  70. [str addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0,[str length])];
  71. [str addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Menlo" size: 10] range:NSMakeRange(0,[str length])];
  72. [textStorage appendAttributedString:str];
  73. [textStorage endEditing];
  74. [str release];
  75. showingConsole = NO;
  76. [self showConsoleWindow:self];
  77. }
  78. - (void) printToConsole: (NSString*) message
  79. {
  80. NSTextStorage *textStorage = [consoleTextView textStorage];
  81. [consoleTextView setInsertionPointColor: [NSColor whiteColor]];
  82. [textStorage beginEditing];
  83. NSMutableAttributedString *str = [[NSMutableAttributedString alloc ]initWithString: message];
  84. [str addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:NSMakeRange(0,[str length])];
  85. [str addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Menlo" size: 10] range:NSMakeRange(0,[str length])];
  86. [textStorage appendAttributedString:str];
  87. [textStorage endEditing];
  88. [str release];
  89. }
  90. - (void)animationTimer:(NSTimer *)timer
  91. {
  92. if(!player->Update()) {
  93. [self close];
  94. }
  95. }
  96. - (void)close
  97. {
  98. [timer invalidate];
  99. [timer release];
  100. delete player;
  101. delete playerProxy;
  102. [super close];
  103. }
  104. - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
  105. {
  106. // Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.
  107. // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
  108. // For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
  109. if ( outError != NULL ) {
  110. *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
  111. }
  112. return nil;
  113. }
  114. - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
  115. {
  116. NSLog(@"Reading from %@\n", docFileName);
  117. docFileName = [[NSString stringWithString:[absoluteURL path]] retain];
  118. return YES;
  119. }
  120. @end