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.
|
|
|
|
//
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
// 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>
|
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
|
|
|
|
informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
|
|
|
|
[[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];
|
|
|
|
}
|