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>
36 NSString* stdStringToCocoa(const std::string& s)
38 return [NSString stringWithUTF8String:s.c_str()];
41 std::string stdStringFromCocoa(NSString* s)
43 return std::string([s UTF8String]);
46 NSURL* pathToNSURL(const SGPath& aPath)
48 return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
51 SGPath URLToPath(NSURL* url)
57 return SGPath([[url path] UTF8String]);
60 flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
61 const std::string& text)
63 CocoaAutoreleasePool pool;
64 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
65 defaultButton:nil /* localized 'ok' */
68 informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
69 [[alert retain] autorelease];
71 return flightgear::MSG_BOX_OK;
76 flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
77 const std::string& text)
79 CocoaAutoreleasePool pool;
80 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
81 defaultButton:@"Quit FlightGear"
84 informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
85 [[alert retain] autorelease];
87 return flightgear::MSG_BOX_OK;
90 void cocoaOpenUrl(const std::string& url)
92 CocoaAutoreleasePool pool;
93 NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
94 [[NSWorkspace sharedWorkspace] openURL:nsu];
97 CocoaAutoreleasePool::CocoaAutoreleasePool()
99 pool = [[NSAutoreleasePool alloc] init];
102 CocoaAutoreleasePool::~CocoaAutoreleasePool()
107 SGPath platformDefaultDataPath()
109 CocoaAutoreleasePool ap;
110 NSFileManager* fm = [NSFileManager defaultManager];
112 NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
113 inDomain:NSUserDomainMask
114 appropriateForURL:Nil
117 if (!appSupportUrl) {
121 SGPath appData(URLToPath(appSupportUrl));
122 appData.append("FlightGear");