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 <AppKit/NSAlert.h>
34 #include <simgear/misc/sg_path.hxx>
37 #include <GUI/MessageBox.hxx>
38 #include <Main/options.hxx>
39 #include <Main/locale.hxx>
41 NSString* stdStringToCocoa(const std::string& s)
43 return [NSString stringWithUTF8String:s.c_str()];
46 std::string stdStringFromCocoa(NSString* s)
48 return std::string([s UTF8String]);
51 NSURL* pathToNSURL(const SGPath& aPath)
53 return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
56 SGPath URLToPath(NSURL* url)
62 return SGPath([[url path] UTF8String]);
65 flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
66 const std::string& text)
68 CocoaAutoreleasePool pool;
69 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
70 defaultButton:nil /* localized 'ok' */
73 informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
74 [[alert retain] autorelease];
76 return flightgear::MSG_BOX_OK;
81 flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
82 const std::string& text)
84 CocoaAutoreleasePool pool;
85 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
86 defaultButton:@"Quit FlightGear"
89 informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
90 [[alert retain] autorelease];
92 return flightgear::MSG_BOX_OK;
95 void cocoaOpenUrl(const std::string& url)
97 CocoaAutoreleasePool pool;
98 NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
99 [[NSWorkspace sharedWorkspace] openURL:nsu];
102 CocoaAutoreleasePool::CocoaAutoreleasePool()
104 pool = [[NSAutoreleasePool alloc] init];
107 CocoaAutoreleasePool::~CocoaAutoreleasePool()
112 SGPath platformDefaultDataPath()
114 CocoaAutoreleasePool ap;
115 NSFileManager* fm = [NSFileManager defaultManager];
117 NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
118 inDomain:NSUserDomainMask
119 appropriateForURL:Nil
122 if (!appSupportUrl) {
126 SGPath appData(URLToPath(appSupportUrl));
127 appData.append("FlightGear");
134 std::string Options::platformDefaultRoot() const
136 CocoaAutoreleasePool ap;
138 NSURL* url = [[NSBundle mainBundle] resourceURL];
139 SGPath dataDir(URLToPath(url));
140 dataDir.append("data");
141 return dataDir.str();
144 } // of namespace flightgear
146 string_list FGLocale::getUserLanguage()
148 CocoaAutoreleasePool ap;
151 for (NSString* lang in [NSLocale preferredLanguages]) {
152 result.push_back(stdStringFromCocoa(lang));
158 void transformToForegroundApp()
160 ProcessSerialNumber sn = { 0, kCurrentProcess };
161 TransformProcessType(&sn,kProcessTransformToForegroundApplication);
164 [[NSApplication sharedApplication] activateIgnoringOtherApps: YES];