1 // CocoaHelpers.mm - C++ implementation of Cocoa/AppKit helpers
3 // Copyright (C) 2013 James Turner <zakalawe@mac.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "CocoaHelpers.h"
21 #include "CocoaHelpers_private.h"
28 #include <Foundation/NSAutoreleasePool.h>
29 #include <Foundation/NSURL.h>
30 #include <Foundation/NSFileManager.h>
31 #include <Foundation/NSBundle.h>
32 #include <Foundation/NSLocale.h>
33 #include <AppKit/NSAlert.h>
36 #include <simgear/misc/sg_path.hxx>
39 #include <GUI/MessageBox.hxx>
40 #include <Main/options.hxx>
41 #include <Main/locale.hxx>
43 NSString* stdStringToCocoa(const std::string& s)
45 return [NSString stringWithUTF8String:s.c_str()];
48 std::string stdStringFromCocoa(NSString* s)
50 return std::string([s UTF8String]);
53 NSURL* pathToNSURL(const SGPath& aPath)
55 return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
58 SGPath URLToPath(NSURL* url)
64 return SGPath([[url path] UTF8String]);
67 flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
68 const std::string& text)
70 CocoaAutoreleasePool pool;
71 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
72 defaultButton:nil /* localized 'ok' */
75 informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
76 [[alert retain] autorelease];
78 return flightgear::MSG_BOX_OK;
83 flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
84 const std::string& text)
86 CocoaAutoreleasePool pool;
87 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
88 defaultButton:@"Quit FlightGear"
91 informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
92 [[alert retain] autorelease];
94 return flightgear::MSG_BOX_OK;
97 void cocoaOpenUrl(const std::string& url)
99 CocoaAutoreleasePool pool;
100 NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
101 [[NSWorkspace sharedWorkspace] openURL:nsu];
104 CocoaAutoreleasePool::CocoaAutoreleasePool()
106 pool = [[NSAutoreleasePool alloc] init];
109 CocoaAutoreleasePool::~CocoaAutoreleasePool()
114 SGPath platformDefaultDataPath()
116 CocoaAutoreleasePool ap;
117 NSFileManager* fm = [NSFileManager defaultManager];
119 NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
120 inDomain:NSUserDomainMask
121 appropriateForURL:Nil
124 if (!appSupportUrl) {
128 SGPath appData(URLToPath(appSupportUrl));
129 appData.append("FlightGear");
136 std::string Options::platformDefaultRoot() const
138 CocoaAutoreleasePool ap;
140 NSURL* url = [[NSBundle mainBundle] resourceURL];
141 SGPath dataDir(URLToPath(url));
142 dataDir.append("data");
143 return dataDir.str();
146 } // of namespace flightgear
148 string_list FGLocale::getUserLanguage()
150 CocoaAutoreleasePool ap;
153 for (NSString* lang in [NSLocale preferredLanguages]) {
154 result.push_back(stdStringFromCocoa(lang));
160 void transformToForegroundApp()
162 ProcessSerialNumber sn = { 0, kCurrentProcess };
163 TransformProcessType(&sn,kProcessTransformToForegroundApplication);
166 [[NSApplication sharedApplication] activateIgnoringOtherApps: YES];