| 1 |
@implementation CocoaDelegate : NSObject |
| 2 |
|
| 3 |
-(NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*)sender { |
| 4 |
using phoenix::Application; |
| 5 |
if(Application::Cocoa::onQuit) Application::Cocoa::onQuit(); |
| 6 |
else Application::quit(); |
| 7 |
return NSTerminateCancel; |
| 8 |
} |
| 9 |
|
| 10 |
-(BOOL) applicationShouldHandleReopen:(NSApplication*)application hasVisibleWindows:(BOOL)flag { |
| 11 |
using phoenix::Application; |
| 12 |
if(Application::Cocoa::onActivate) Application::Cocoa::onActivate(); |
| 13 |
return NO; |
| 14 |
} |
| 15 |
|
| 16 |
-(void) run:(NSTimer*)timer { |
| 17 |
using phoenix::Application; |
| 18 |
if(Application::main) Application::main(); |
| 19 |
} |
| 20 |
|
| 21 |
-(void) updateInDock:(NSTimer*)timer { |
| 22 |
NSArray* windows = [NSApp windows]; |
| 23 |
for(unsigned n = 0; n < [windows count]; n++) { |
| 24 |
NSWindow* window = [windows objectAtIndex:n]; |
| 25 |
if([window isMiniaturized]) { |
| 26 |
[window updateInDock]; |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
@end |
| 32 |
|
| 33 |
CocoaDelegate* cocoaDelegate = nullptr; |
| 34 |
|
| 35 |
namespace phoenix { |
| 36 |
|
| 37 |
void pApplication::run() { |
| 38 |
//NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.1667 target:cocoaDelegate selector:@selector(updateInDock:) userInfo:nil repeats:YES]; |
| 39 |
|
| 40 |
if(Application::main) { |
| 41 |
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:cocoaDelegate selector:@selector(run:) userInfo:nil repeats:YES]; |
| 42 |
|
| 43 |
//below line is needed to run application during window resize; however it has a large performance penalty on the resize smoothness |
| 44 |
//[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode]; |
| 45 |
} |
| 46 |
|
| 47 |
@autoreleasepool { |
| 48 |
[NSApp run]; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
bool pApplication::pendingEvents() { |
| 53 |
bool result = false; |
| 54 |
@autoreleasepool { |
| 55 |
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:NO]; |
| 56 |
if(event != nil) result = true; |
| 57 |
} |
| 58 |
return result; |
| 59 |
} |
| 60 |
|
| 61 |
void pApplication::processEvents() { |
| 62 |
@autoreleasepool { |
| 63 |
while(applicationState.quit == false) { |
| 64 |
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES]; |
| 65 |
if(event == nil) break; |
| 66 |
[event retain]; |
| 67 |
[NSApp sendEvent:event]; |
| 68 |
[event release]; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void pApplication::quit() { |
| 74 |
@autoreleasepool { |
| 75 |
[NSApp stop:nil]; |
| 76 |
NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined location:NSMakePoint(0, 0) modifierFlags:0 timestamp:0.0 windowNumber:0 context:nil subtype:0 data1:0 data2:0]; |
| 77 |
[NSApp postEvent:event atStart:true]; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void pApplication::initialize() { |
| 82 |
@autoreleasepool { |
| 83 |
[NSApplication sharedApplication]; |
| 84 |
cocoaDelegate = [[CocoaDelegate alloc] init]; |
| 85 |
[NSApp setDelegate:cocoaDelegate]; |
| 86 |
//every window has the default application menu; call this so it is displayed at startup |
| 87 |
[NSApp setMainMenu:[pWindow::none().p.cocoaWindow menu]]; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
} |