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 <AppKit/NSAlert.h>
35 #include <simgear/misc/sg_path.hxx>
38 #include <GUI/MessageBox.hxx>
39 #include <Main/options.hxx>
40 #include <Main/locale.hxx>
42 NSString* stdStringToCocoa(const std::string& s)
44 return [NSString stringWithUTF8String:s.c_str()];
47 std::string stdStringFromCocoa(NSString* s)
49 return std::string([s UTF8String]);
52 NSURL* pathToNSURL(const SGPath& aPath)
54 return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
57 SGPath URLToPath(NSURL* url)
63 return SGPath([[url path] UTF8String]);
66 flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
67 const std::string& text)
69 CocoaAutoreleasePool pool;
70 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
71 defaultButton:nil /* localized 'ok' */
74 informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
75 [[alert retain] autorelease];
77 return flightgear::MSG_BOX_OK;
82 flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
83 const std::string& text)
85 CocoaAutoreleasePool pool;
86 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
87 defaultButton:@"Quit FlightGear"
90 informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
91 [[alert retain] autorelease];
93 return flightgear::MSG_BOX_OK;
96 void cocoaOpenUrl(const std::string& url)
98 CocoaAutoreleasePool pool;
99 NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
100 [[NSWorkspace sharedWorkspace] openURL:nsu];
103 CocoaAutoreleasePool::CocoaAutoreleasePool()
105 pool = [[NSAutoreleasePool alloc] init];
108 CocoaAutoreleasePool::~CocoaAutoreleasePool()
113 SGPath platformDefaultDataPath()
115 CocoaAutoreleasePool ap;
116 NSFileManager* fm = [NSFileManager defaultManager];
118 NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
119 inDomain:NSUserDomainMask
120 appropriateForURL:Nil
123 if (!appSupportUrl) {
127 SGPath appData(URLToPath(appSupportUrl));
128 appData.append("FlightGear");
135 std::string Options::platformDefaultRoot() const
137 CocoaAutoreleasePool ap;
139 NSURL* url = [[NSBundle mainBundle] resourceURL];
140 SGPath dataDir(URLToPath(url));
141 dataDir.append("data");
142 return dataDir.str();
145 } // of namespace flightgear
147 string_list FGLocale::getUserLanguage()
149 CocoaAutoreleasePool ap;
152 for (NSString* lang in [NSLocale preferredLanguages]) {
153 result.push_back(stdStringFromCocoa(lang));
159 void transformToForegroundApp()
161 ProcessSerialNumber sn = { 0, kCurrentProcess };
162 TransformProcessType(&sn,kProcessTransformToForegroundApplication);
165 [[NSApplication sharedApplication] activateIgnoringOtherApps: YES];