From dcdee4ec5e2561655745325de3f8c8e9c4c20e59 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 8 Nov 2013 20:26:32 +0000 Subject: [PATCH] fixes #15613 git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@75142 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/app.h | 4 +++ src/osx/cocoa/utils.mm | 76 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/include/wx/osx/app.h b/include/wx/osx/app.h index 4f7b9e352f..767ee9c8bb 100644 --- a/include/wx/osx/app.h +++ b/include/wx/osx/app.h @@ -143,6 +143,10 @@ public: virtual void MacNewFile() ; // in response of a reopen-application apple event virtual void MacReopenApp() ; + + // override this to return false from a non-bundled console app in order to stay in background ... + virtual bool OSXIsGUIApplication() { return true; } + #if wxOSX_USE_COCOA_OR_IPHONE // immediately before the native event loop launches virtual void OSXOnWillFinishLaunching(); diff --git a/src/osx/cocoa/utils.mm b/src/osx/cocoa/utils.mm index 55e08c7965..28437de59d 100644 --- a/src/osx/cocoa/utils.mm +++ b/src/osx/cocoa/utils.mm @@ -255,6 +255,50 @@ void wxBell() } @end + +// more on bringing non-bundled apps to the foreground +// https://devforums.apple.com/thread/203753 + +#if 0 + +// one possible solution is also quoted here +// from http://stackoverflow.com/questions/7596643/when-calling-transformprocesstype-the-app-menu-doesnt-show-up + +@interface wxNSNonBundledAppHelper : NSObject { + +} + ++ (void)transformToForegroundApplication; + +@end + +@implementation wxNSNonBundledAppHelper + ++ (void)transformToForegroundApplication { + for (NSRunningApplication * app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.finder"]) { + [app activateWithOptions:NSApplicationActivateIgnoringOtherApps]; + break; + } + [self performSelector:@selector(transformStep2) withObject:nil afterDelay:0.1]; +} + ++ (void)transformStep2 +{ + ProcessSerialNumber psn = { 0, kCurrentProcess }; + (void) TransformProcessType(&psn, kProcessTransformToForegroundApplication); + + [self performSelector:@selector(transformStep3) withObject:nil afterDelay:0.1]; +} + ++ (void)transformStep3 +{ + [[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps]; +} + +@end + +#endif + // here we subclass NSApplication, for the purpose of being able to override sendEvent. @interface wxNSApplication : NSApplication { @@ -276,6 +320,24 @@ void wxBell() return self; } +- (void) transformToForegroundApplication { + ProcessSerialNumber psn = { 0, kCurrentProcess }; + TransformProcessType(&psn, kProcessTransformToForegroundApplication); + + if ( UMAGetSystemVersion() < 0x1090 ) + { + [self deactivate]; + [self activateIgnoringOtherApps:YES]; + } + else + { + [[NSRunningApplication currentApplication] activateWithOptions: + (NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)]; + } +} + + + /* This is needed because otherwise we don't receive any key-up events for command-key combinations (an AppKit bug, apparently) */ - (void)sendEvent:(NSEvent *)anEvent @@ -311,6 +373,20 @@ bool wxApp::DoInitGui() if (!sm_isEmbedded) { [wxNSApplication sharedApplication]; + + if ( OSXIsGUIApplication() ) + { + CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ; + CFStringRef path = CFURLCopyFileSystemPath ( url , kCFURLPOSIXPathStyle ) ; + CFRelease( url ) ; + wxString app = wxCFStringRef(path).AsString(wxLocale::GetSystemEncoding()); + + // workaround is only needed for non-bundled apps + if ( !app.EndsWith(".app") ) + { + [(wxNSApplication*) [wxNSApplication sharedApplication] transformToForegroundApplication]; + } + } appcontroller = OSXCreateAppController(); [NSApp setDelegate:appcontroller];