]> git.mxchange.org Git - flightgear.git/blob - src/GUI/CocoaHelpers.mm
One more Mac helper moved into CocoaHelpers.mm
[flightgear.git] / src / GUI / CocoaHelpers.mm
1 // CocoaHelpers.mm - C++ implementation of Cocoa/AppKit helpers
2
3 // Copyright (C) 2013 James Turner <zakalawe@mac.com>
4 //
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.
9 //
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.
14 //
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.
18 //
19
20 #include "CocoaHelpers.h"
21 #include "CocoaHelpers_private.h"
22
23 // standard
24 #include <string>
25
26 // OS
27 #include <Cocoa/Cocoa.h>
28 #include <Foundation/NSAutoreleasePool.h>
29
30 // simgear
31 #include <simgear/misc/sg_path.hxx>
32
33 // flightgear
34 #include <GUI/MessageBox.hxx>
35 #include <Main/options.hxx>
36 #include <Main/locale.hxx>
37
38 NSString* stdStringToCocoa(const std::string& s)
39 {
40     return [NSString stringWithUTF8String:s.c_str()];
41 }
42
43 std::string stdStringFromCocoa(NSString* s)
44 {
45     return std::string([s UTF8String]);
46 }
47
48 NSURL* pathToNSURL(const SGPath& aPath)
49 {
50     return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
51 }
52
53 SGPath URLToPath(NSURL* url)
54 {
55     if (!url) {
56         return SGPath();
57     }
58     
59     return SGPath([[url path] UTF8String]);
60 }
61
62 flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
63                                              const std::string& text)
64 {
65     CocoaAutoreleasePool pool;
66     NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
67                                      defaultButton:nil /* localized 'ok' */
68                                    alternateButton:nil
69                                        otherButton:nil
70                          informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
71     [[alert retain] autorelease];
72     [alert runModal];
73     return flightgear::MSG_BOX_OK;
74 }
75
76
77
78 flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
79                                                const std::string& text)
80 {
81     CocoaAutoreleasePool pool;
82     NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
83                                      defaultButton:@"Quit FlightGear"
84                                    alternateButton:nil
85                                        otherButton:nil
86                          informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
87     [[alert retain] autorelease];
88     [alert runModal];
89     return flightgear::MSG_BOX_OK;
90 }
91
92 void cocoaOpenUrl(const std::string& url)
93 {
94   CocoaAutoreleasePool pool;
95   NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
96   [[NSWorkspace sharedWorkspace] openURL:nsu];
97 }
98
99 CocoaAutoreleasePool::CocoaAutoreleasePool()
100 {
101     pool = [[NSAutoreleasePool alloc] init];
102 }
103
104 CocoaAutoreleasePool::~CocoaAutoreleasePool()
105 {
106     [pool release];
107 }
108
109 SGPath platformDefaultDataPath()
110 {
111     CocoaAutoreleasePool ap;
112     NSFileManager* fm = [NSFileManager defaultManager];
113     
114     NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
115                                      inDomain:NSUserDomainMask
116                              appropriateForURL:Nil
117                                        create:YES
118                                          error:nil];
119     if (!appSupportUrl) {
120         return SGPath();
121     }
122     
123     SGPath appData(URLToPath(appSupportUrl));
124     appData.append("FlightGear");
125     return appData;
126 }
127
128 namespace flightgear
129 {
130     
131 std::string Options::platformDefaultRoot() const
132 {
133     CocoaAutoreleasePool ap;
134     
135     NSURL* url = [[NSBundle mainBundle] resourceURL];
136     SGPath dataDir(URLToPath(url));
137     dataDir.append("data");
138     return dataDir.str();
139 }
140     
141 } // of namespace flightgear
142
143 string_list FGLocale::getUserLanguage()
144 {
145     CocoaAutoreleasePool ap;
146     string_list result;
147     
148     for (NSString* lang in [NSLocale preferredLanguages]) {
149         result.push_back(stdStringFromCocoa(lang));
150     }
151     
152     return result;
153 }