+@implementation wxNSAppController
+
+- (void)applicationWillFinishLaunching:(NSNotification *)application {
+ wxUnusedVar(application);
+
+ // we must install our handlers later than setting the app delegate, because otherwise our handlers
+ // get overwritten in the meantime
+
+ NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
+
+ [appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:)
+ forEventClass:kInternetEventClass andEventID:kAEGetURL];
+
+ [appleEventManager setEventHandler:self andSelector:@selector(handleOpenAppEvent:withReplyEvent:)
+ forEventClass:kCoreEventClass andEventID:kAEOpenApplication];
+
+}
+
+- (void)application:(NSApplication *)sender openFiles:(NSArray *)fileNames
+{
+ wxUnusedVar(sender);
+ wxArrayString fileList;
+ size_t i;
+ const size_t count = [fileNames count];
+ for (i = 0; i < count; i++)
+ {
+ fileList.Add( wxCFStringRef::AsStringWithNormalizationFormC([fileNames objectAtIndex:i]) );
+ }
+
+ wxTheApp->MacOpenFiles(fileList);
+}
+
+- (BOOL)application:(NSApplication *)sender printFile:(NSString *)filename
+{
+ wxUnusedVar(sender);
+ wxCFStringRef cf(wxCFRetain(filename));
+ wxTheApp->MacPrintFile(cf.AsString()) ;
+ return YES;
+}
+
+- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag
+{
+ wxUnusedVar(flag);
+ wxUnusedVar(sender);
+ wxTheApp->MacReopenApp() ;
+ return NO;
+}
+
+- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
+ withReplyEvent:(NSAppleEventDescriptor *)replyEvent
+{
+ wxUnusedVar(replyEvent);
+ NSString* url = [[event descriptorAtIndex:1] stringValue];
+ wxCFStringRef cf(wxCFRetain(url));
+ wxTheApp->MacOpenURL(cf.AsString()) ;
+}
+
+- (void)handleOpenAppEvent:(NSAppleEventDescriptor *)event
+ withReplyEvent:(NSAppleEventDescriptor *)replyEvent
+{
+ wxUnusedVar(replyEvent);
+ wxTheApp->MacNewFile() ;
+}
+
+/*
+ Allowable return values are:
+ NSTerminateNow - it is ok to proceed with termination
+ NSTerminateCancel - the application should not be terminated
+ NSTerminateLater - it may be ok to proceed with termination later. The application must call -replyToApplicationShouldTerminate: with YES or NO once the answer is known
+ this return value is for delegates who need to provide document modal alerts (sheets) in order to decide whether to quit.
+*/
+- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
+{
+ wxUnusedVar(sender);
+ wxCloseEvent event;
+ wxTheApp->OnQueryEndSession(event);
+ if ( event.GetVeto() )
+ return NSTerminateCancel;
+
+ return NSTerminateNow;
+}
+
+- (void)applicationWillTerminate:(NSNotification *)application {
+ wxUnusedVar(application);
+ wxCloseEvent event;
+ event.SetCanVeto(false);
+ wxTheApp->OnEndSession(event);
+}
+
+- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
+{
+ wxUnusedVar(sender);
+ // let wx do this, not cocoa
+ return NO;
+}
+
+- (void)applicationDidBecomeActive:(NSNotification *)notification
+{
+ wxUnusedVar(notification);
+
+ for ( wxWindowList::const_iterator i = wxTopLevelWindows.begin(),
+ end = wxTopLevelWindows.end();
+ i != end;
+ ++i )
+ {
+ wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i);
+ wxNonOwnedWindowImpl* winimpl = win ? win->GetNonOwnedPeer() : NULL;
+ WXWindow nswindow = win ? win->GetWXWindow() : nil;
+
+ if ( nswindow && [nswindow hidesOnDeactivate] == NO && winimpl)
+ winimpl->RestoreWindowLevel();
+ }
+ if ( wxTheApp )
+ wxTheApp->SetActive( true , NULL ) ;
+}
+
+- (void)applicationWillResignActive:(NSNotification *)notification
+{
+ wxUnusedVar(notification);
+ for ( wxWindowList::const_iterator i = wxTopLevelWindows.begin(),
+ end = wxTopLevelWindows.end();
+ i != end;
+ ++i )
+ {
+ wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i);
+ WXWindow nswindow = win ? win->GetWXWindow() : nil;
+
+ if ( nswindow && [nswindow level] == kCGFloatingWindowLevel && [nswindow hidesOnDeactivate] == NO )
+ [nswindow setLevel:kCGNormalWindowLevel];
+ }
+}
+
+- (void)applicationDidResignActive:(NSNotification *)notification
+{
+ wxUnusedVar(notification);
+ if ( wxTheApp )
+ wxTheApp->SetActive( false , NULL ) ;
+}
+
+@end
+
+/*
+ allows ShowModal to work when using sheets.
+ see include/wx/osx/cocoa/private.h for more info
+*/
+@implementation ModalDialogDelegate
+- (id)init
+{
+ self = [super init];
+ sheetFinished = NO;
+ resultCode = -1;
+ impl = 0;
+ return self;
+}
+
+- (void)setImplementation: (wxDialog *)dialog
+{
+ impl = dialog;
+}
+
+- (BOOL)finished
+{
+ return sheetFinished;
+}
+
+- (int)code
+{
+ return resultCode;
+}
+
+- (void)waitForSheetToFinish
+{
+ while (!sheetFinished)
+ {
+ wxSafeYield();
+ }
+}