+- (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()) ;
+}
+
+/*
+ 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;
+}
+
+@end
+
+/*
+ allows ShowModal to work when using sheets.
+ see include/wx/osx/cocoa/private.h for more info
+*/
+@implementation ModalDialogDelegate
+- (id)init
+{
+ [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();
+ }
+}
+
+- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
+{
+ wxUnusedVar(contextInfo);
+ resultCode = returnCode;
+ sheetFinished = YES;
+ // NSAlerts don't need nor respond to orderOut
+ if ([sheet respondsToSelector:@selector(orderOut:)])
+ [sheet orderOut: self];
+
+ if (impl)
+ impl->ModalFinishedCallback(sheet, returnCode);
+}
+@end
+
+bool wxApp::DoInitGui()
+{
+ wxMacAutoreleasePool pool;
+ [NSApplication sharedApplication];
+
+ if (!sm_isEmbedded)
+ {
+ wxNSAppController* controller = [[wxNSAppController alloc] init];
+ [NSApp setDelegate:controller];
+
+ NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
+ [appleEventManager setEventHandler:controller andSelector:@selector(handleGetURLEvent:withReplyEvent:)
+ forEventClass:kInternetEventClass andEventID:kAEGetURL];
+
+ // calling finishLaunching so early before running the loop seems to trigger some 'MenuManager compatibility' which leads
+ // to the duplication of menus under 10.5 and a warning under 10.6
+#if 0
+ [NSApp finishLaunching];
+#endif
+ }
+ return true;
+}
+
+void wxApp::DoCleanUp()
+{
+}