2013-11-16 10:05:09 +00:00
|
|
|
// CocoaHelpers.mm - C++ implementation of Cocoa/AppKit helpers
|
|
|
|
|
|
|
|
// Copyright (C) 2013 James Turner <zakalawe@mac.com>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
//
|
|
|
|
|
2019-04-17 16:37:22 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2013-11-16 10:05:09 +00:00
|
|
|
#include "CocoaHelpers.h"
|
|
|
|
#include "CocoaHelpers_private.h"
|
|
|
|
|
|
|
|
// standard
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
// OS
|
2015-04-11 20:59:23 +00:00
|
|
|
|
2013-11-16 10:05:09 +00:00
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
2015-04-11 20:59:23 +00:00
|
|
|
#include <Foundation/NSURL.h>
|
|
|
|
#include <Foundation/NSFileManager.h>
|
2015-04-20 09:35:24 +00:00
|
|
|
#include <Foundation/NSBundle.h>
|
2015-06-07 15:36:07 +00:00
|
|
|
#include <Foundation/NSLocale.h>
|
2015-04-11 20:59:23 +00:00
|
|
|
#include <AppKit/NSAlert.h>
|
2013-11-16 10:05:09 +00:00
|
|
|
|
|
|
|
// simgear
|
|
|
|
#include <simgear/misc/sg_path.hxx>
|
2019-04-17 16:37:22 +00:00
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
#include <simgear/structure/commands.hxx>
|
2013-11-16 10:05:09 +00:00
|
|
|
|
|
|
|
// flightgear
|
|
|
|
#include <GUI/MessageBox.hxx>
|
2013-11-18 15:52:30 +00:00
|
|
|
#include <Main/options.hxx>
|
2013-11-19 22:01:11 +00:00
|
|
|
#include <Main/locale.hxx>
|
2019-04-17 16:37:22 +00:00
|
|
|
#include <Main/globals.hxx>
|
|
|
|
|
|
|
|
#if defined(HAVE_QT)
|
|
|
|
# include <GUI/QtLauncher.hxx>
|
|
|
|
#endif
|
2013-11-16 10:05:09 +00:00
|
|
|
|
|
|
|
NSString* stdStringToCocoa(const std::string& s)
|
|
|
|
{
|
|
|
|
return [NSString stringWithUTF8String:s.c_str()];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string stdStringFromCocoa(NSString* s)
|
|
|
|
{
|
|
|
|
return std::string([s UTF8String]);
|
|
|
|
}
|
|
|
|
|
|
|
|
NSURL* pathToNSURL(const SGPath& aPath)
|
|
|
|
{
|
2016-06-21 11:28:35 +00:00
|
|
|
return [NSURL fileURLWithPath:stdStringToCocoa(aPath.utf8Str())];
|
2013-11-16 10:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SGPath URLToPath(NSURL* url)
|
|
|
|
{
|
|
|
|
if (!url) {
|
|
|
|
return SGPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
return SGPath([[url path] UTF8String]);
|
|
|
|
}
|
|
|
|
|
|
|
|
flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
|
|
|
|
const std::string& text)
|
|
|
|
{
|
|
|
|
CocoaAutoreleasePool pool;
|
|
|
|
NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
|
|
|
|
defaultButton:nil /* localized 'ok' */
|
|
|
|
alternateButton:nil
|
|
|
|
otherButton:nil
|
2017-09-20 16:57:25 +00:00
|
|
|
informativeTextWithFormat:@" %@",stdStringToCocoa(text)];
|
2013-11-16 10:05:09 +00:00
|
|
|
[[alert retain] autorelease];
|
|
|
|
[alert runModal];
|
|
|
|
return flightgear::MSG_BOX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
|
|
|
|
const std::string& text)
|
|
|
|
{
|
|
|
|
CocoaAutoreleasePool pool;
|
|
|
|
NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
|
|
|
|
defaultButton:@"Quit FlightGear"
|
|
|
|
alternateButton:nil
|
|
|
|
otherButton:nil
|
|
|
|
informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
|
|
|
|
[[alert retain] autorelease];
|
|
|
|
[alert runModal];
|
|
|
|
return flightgear::MSG_BOX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cocoaOpenUrl(const std::string& url)
|
|
|
|
{
|
|
|
|
CocoaAutoreleasePool pool;
|
|
|
|
NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
|
|
|
|
[[NSWorkspace sharedWorkspace] openURL:nsu];
|
|
|
|
}
|
|
|
|
|
|
|
|
CocoaAutoreleasePool::CocoaAutoreleasePool()
|
|
|
|
{
|
|
|
|
pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
}
|
|
|
|
|
|
|
|
CocoaAutoreleasePool::~CocoaAutoreleasePool()
|
|
|
|
{
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
SGPath platformDefaultDataPath()
|
|
|
|
{
|
|
|
|
CocoaAutoreleasePool ap;
|
|
|
|
NSFileManager* fm = [NSFileManager defaultManager];
|
|
|
|
|
|
|
|
NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
|
|
|
|
inDomain:NSUserDomainMask
|
|
|
|
appropriateForURL:Nil
|
|
|
|
create:YES
|
|
|
|
error:nil];
|
|
|
|
if (!appSupportUrl) {
|
|
|
|
return SGPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
SGPath appData(URLToPath(appSupportUrl));
|
|
|
|
appData.append("FlightGear");
|
|
|
|
return appData;
|
|
|
|
}
|
2013-11-18 15:52:30 +00:00
|
|
|
|
|
|
|
namespace flightgear
|
|
|
|
{
|
|
|
|
|
2016-06-21 11:29:04 +00:00
|
|
|
SGPath Options::platformDefaultRoot() const
|
2013-11-18 15:52:30 +00:00
|
|
|
{
|
|
|
|
CocoaAutoreleasePool ap;
|
|
|
|
|
|
|
|
NSURL* url = [[NSBundle mainBundle] resourceURL];
|
|
|
|
SGPath dataDir(URLToPath(url));
|
|
|
|
dataDir.append("data");
|
2016-06-21 11:29:04 +00:00
|
|
|
return dataDir;
|
2013-11-18 15:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // of namespace flightgear
|
2013-11-19 22:01:11 +00:00
|
|
|
|
|
|
|
string_list FGLocale::getUserLanguage()
|
|
|
|
{
|
|
|
|
CocoaAutoreleasePool ap;
|
|
|
|
string_list result;
|
|
|
|
|
|
|
|
for (NSString* lang in [NSLocale preferredLanguages]) {
|
|
|
|
result.push_back(stdStringFromCocoa(lang));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2013-12-21 15:29:11 +00:00
|
|
|
|
|
|
|
void transformToForegroundApp()
|
|
|
|
{
|
|
|
|
ProcessSerialNumber sn = { 0, kCurrentProcess };
|
|
|
|
TransformProcessType(&sn,kProcessTransformToForegroundApplication);
|
|
|
|
|
|
|
|
|
|
|
|
[[NSApplication sharedApplication] activateIgnoringOtherApps: YES];
|
|
|
|
}
|
2019-04-17 16:37:22 +00:00
|
|
|
|
|
|
|
@interface FlightGearNSAppDelegate : NSObject <NSApplicationDelegate>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FlightGearNSAppDelegate
|
|
|
|
|
|
|
|
|
|
|
|
// it would be lovely to do the following, but the return code doesn't
|
|
|
|
// seem to work as documented - maybe it's ignored due to newer process
|
|
|
|
// management
|
|
|
|
#if 0
|
|
|
|
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *)sender
|
|
|
|
{
|
|
|
|
SGCommandMgr* mgr = SGCommandMgr::instance();
|
|
|
|
SGPropertyNode_ptr propArgs(new SGPropertyNode);
|
|
|
|
propArgs->setStringValue("dialog-name", "exit");
|
|
|
|
mgr->execute("dialog-show", propArgs, globals->get_props());
|
|
|
|
|
|
|
|
return NSTerminateCancel;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
- (void) applicationWillTerminate:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
SG_LOG(SG_GENERAL, SG_INFO, "macOS quit occuring");
|
|
|
|
#if defined(HAVE_QT)
|
|
|
|
flightgear::shutdownQtApp();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
void cocoaRegisterTerminateHandler()
|
|
|
|
{
|
|
|
|
FlightGearNSAppDelegate* delegate = [[FlightGearNSAppDelegate alloc] init];
|
|
|
|
|
|
|
|
auto *app = [NSApplication sharedApplication];
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:delegate
|
|
|
|
selector:@selector(applicationWillTerminate:)
|
|
|
|
name:NSApplicationWillTerminateNotification object:app];
|
|
|
|
#if 9
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:delegate
|
|
|
|
selector:@selector(applicationShouldTerminate:)
|
|
|
|
name:NSApplicationWillTerminateNotification object:app];
|
|
|
|
#endif
|
|
|
|
}
|