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>
36 #include <Main/locale.hxx>
38 NSString* stdStringToCocoa(const std::string& s)
40 return [NSString stringWithUTF8String:s.c_str()];
43 std::string stdStringFromCocoa(NSString* s)
45 return std::string([s UTF8String]);
48 NSURL* pathToNSURL(const SGPath& aPath)
50 return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
53 SGPath URLToPath(NSURL* url)
59 return SGPath([[url path] UTF8String]);
62 flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
63 const std::string& text)
65 CocoaAutoreleasePool pool;
66 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
67 defaultButton:nil /* localized 'ok' */
70 informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
71 [[alert retain] autorelease];
73 return flightgear::MSG_BOX_OK;
78 flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
79 const std::string& text)
81 CocoaAutoreleasePool pool;
82 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
83 defaultButton:@"Quit FlightGear"
86 informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
87 [[alert retain] autorelease];
89 return flightgear::MSG_BOX_OK;
92 void cocoaOpenUrl(const std::string& url)
94 CocoaAutoreleasePool pool;
95 NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
96 [[NSWorkspace sharedWorkspace] openURL:nsu];
99 CocoaAutoreleasePool::CocoaAutoreleasePool()
101 pool = [[NSAutoreleasePool alloc] init];
104 CocoaAutoreleasePool::~CocoaAutoreleasePool()
109 SGPath platformDefaultDataPath()
111 CocoaAutoreleasePool ap;
112 NSFileManager* fm = [NSFileManager defaultManager];
114 NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
115 inDomain:NSUserDomainMask
116 appropriateForURL:Nil
119 if (!appSupportUrl) {
123 SGPath appData(URLToPath(appSupportUrl));
124 appData.append("FlightGear");
131 std::string Options::platformDefaultRoot() const
133 CocoaAutoreleasePool ap;
135 NSURL* url = [[NSBundle mainBundle] resourceURL];
136 SGPath dataDir(URLToPath(url));
137 dataDir.append("data");
138 return dataDir.str();
141 } // of namespace flightgear
143 string_list FGLocale::getUserLanguage()
145 CocoaAutoreleasePool ap;
148 for (NSString* lang in [NSLocale preferredLanguages]) {
149 result.push_back(stdStringFromCocoa(lang));
155 void transformToForegroundApp()
157 ProcessSerialNumber sn = { 0, kCurrentProcess };
158 TransformProcessType(&sn,kProcessTransformToForegroundApplication);
161 [[NSApplication sharedApplication] activateIgnoringOtherApps: YES];