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"
27 #include <Cocoa/Cocoa.h>
28 #include <Foundation/NSAutoreleasePool.h>
31 #include <simgear/misc/sg_path.hxx>
34 #include <GUI/MessageBox.hxx>
35 #include <Main/options.hxx>
37 NSString* stdStringToCocoa(const std::string& s)
39 return [NSString stringWithUTF8String:s.c_str()];
42 std::string stdStringFromCocoa(NSString* s)
44 return std::string([s UTF8String]);
47 NSURL* pathToNSURL(const SGPath& aPath)
49 return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
52 SGPath URLToPath(NSURL* url)
58 return SGPath([[url path] UTF8String]);
61 flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
62 const std::string& text)
64 CocoaAutoreleasePool pool;
65 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
66 defaultButton:nil /* localized 'ok' */
69 informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
70 [[alert retain] autorelease];
72 return flightgear::MSG_BOX_OK;
77 flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
78 const std::string& text)
80 CocoaAutoreleasePool pool;
81 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
82 defaultButton:@"Quit FlightGear"
85 informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
86 [[alert retain] autorelease];
88 return flightgear::MSG_BOX_OK;
91 void cocoaOpenUrl(const std::string& url)
93 CocoaAutoreleasePool pool;
94 NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
95 [[NSWorkspace sharedWorkspace] openURL:nsu];
98 CocoaAutoreleasePool::CocoaAutoreleasePool()
100 pool = [[NSAutoreleasePool alloc] init];
103 CocoaAutoreleasePool::~CocoaAutoreleasePool()
108 SGPath platformDefaultDataPath()
110 CocoaAutoreleasePool ap;
111 NSFileManager* fm = [NSFileManager defaultManager];
113 NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
114 inDomain:NSUserDomainMask
115 appropriateForURL:Nil
118 if (!appSupportUrl) {
122 SGPath appData(URLToPath(appSupportUrl));
123 appData.append("FlightGear");
130 std::string Options::platformDefaultRoot() const
132 CocoaAutoreleasePool ap;
134 NSURL* url = [[NSBundle mainBundle] resourceURL];
135 SGPath dataDir(URLToPath(url));
136 dataDir.append("data");
137 return dataDir.str();
140 } // of namespace flightgear