| 1 |
/* |
| 2 |
* Copyright 2019 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#import <Cocoa/Cocoa.h> |
| 9 |
|
| 10 |
#include "tools/sk_app/Application.h" |
| 11 |
#include "tools/sk_app/mac/Window_mac.h" |
| 12 |
|
| 13 |
@interface AppDelegate : NSObject<NSApplicationDelegate, NSWindowDelegate> |
| 14 |
|
| 15 |
@property (nonatomic, assign) BOOL done; |
| 16 |
|
| 17 |
@end |
| 18 |
|
| 19 |
@implementation AppDelegate : NSObject |
| 20 |
|
| 21 |
@synthesize done = _done; |
| 22 |
|
| 23 |
- (id)init { |
| 24 |
self = [super init]; |
| 25 |
_done = FALSE; |
| 26 |
return self; |
| 27 |
} |
| 28 |
|
| 29 |
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { |
| 30 |
_done = TRUE; |
| 31 |
return NSTerminateCancel; |
| 32 |
} |
| 33 |
|
| 34 |
- (void)applicationDidFinishLaunching:(NSNotification *)notification { |
| 35 |
[NSApp stop:nil]; |
| 36 |
} |
| 37 |
|
| 38 |
@end |
| 39 |
|
| 40 |
/////////////////////////////////////////////////////////////////////////////////////////// |
| 41 |
|
| 42 |
using sk_app::Application; |
| 43 |
using sk_app::Window_mac; |
| 44 |
|
| 45 |
int main(int argc, char * argv[]) { |
| 46 |
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070 |
| 47 |
// we only run on systems that support at least Core Profile 3.2 |
| 48 |
return EXIT_FAILURE; |
| 49 |
#endif |
| 50 |
|
| 51 |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 52 |
[NSApplication sharedApplication]; |
| 53 |
|
| 54 |
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; |
| 55 |
|
| 56 |
//Create the application menu. |
| 57 |
NSMenu* menuBar=[[NSMenu alloc] initWithTitle:@"AMainMenu"]; |
| 58 |
[NSApp setMainMenu:menuBar]; |
| 59 |
|
| 60 |
NSMenuItem* item; |
| 61 |
NSMenu* subMenu; |
| 62 |
|
| 63 |
item=[[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""]; |
| 64 |
[menuBar addItem:item]; |
| 65 |
subMenu=[[NSMenu alloc] initWithTitle:@"Apple"]; |
| 66 |
[menuBar setSubmenu:subMenu forItem:item]; |
| 67 |
[item release]; |
| 68 |
item=[[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"]; |
| 69 |
[subMenu addItem:item]; |
| 70 |
[item release]; |
| 71 |
[subMenu release]; |
| 72 |
|
| 73 |
// Set AppDelegate to catch certain global events |
| 74 |
AppDelegate* appDelegate = [[AppDelegate alloc] init]; |
| 75 |
[NSApp setDelegate:appDelegate]; |
| 76 |
|
| 77 |
Application* app = Application::Create(argc, argv, nullptr); |
| 78 |
|
| 79 |
// This will run until the application finishes launching, then lets us take over |
| 80 |
[NSApp run]; |
| 81 |
|
| 82 |
// Now we process the events |
| 83 |
while (![appDelegate done]) { |
| 84 |
NSEvent* event; |
| 85 |
do { |
| 86 |
event = [NSApp nextEventMatchingMask:NSEventMaskAny |
| 87 |
untilDate:[NSDate distantPast] |
| 88 |
inMode:NSDefaultRunLoopMode |
| 89 |
dequeue:YES]; |
| 90 |
[NSApp sendEvent:event]; |
| 91 |
} while (event != nil); |
| 92 |
|
| 93 |
[pool drain]; |
| 94 |
pool = [[NSAutoreleasePool alloc] init]; |
| 95 |
|
| 96 |
// Rather than depending on a Mac event to drive this, we treat our window |
| 97 |
// invalidation flag as a separate event stream. Window::onPaint() will clear |
| 98 |
// the invalidation flag, effectively removing it from the stream. |
| 99 |
Window_mac::PaintWindows(); |
| 100 |
|
| 101 |
app->onIdle(); |
| 102 |
} |
| 103 |
|
| 104 |
delete app; |
| 105 |
|
| 106 |
[NSApp setDelegate:nil]; |
| 107 |
[appDelegate release]; |
| 108 |
|
| 109 |
[menuBar release]; |
| 110 |
[pool release]; |
| 111 |
|
| 112 |
return EXIT_SUCCESS; |
| 113 |
} |