]> 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
37 NSString* stdStringToCocoa(const std::string& s)
38 {
39     return [NSString stringWithUTF8String:s.c_str()];
40 }
41
42 std::string stdStringFromCocoa(NSString* s)
43 {
44     return std::string([s UTF8String]);
45 }
46
47 NSURL* pathToNSURL(const SGPath& aPath)
48 {
49     return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
50 }
51
52 SGPath URLToPath(NSURL* url)
53 {
54     if (!url) {
55         return SGPath();
56     }
57     
58     return SGPath([[url path] UTF8String]);
59 }
60
61 flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
62                                              const std::string& text)
63 {
64     CocoaAutoreleasePool pool;
65     NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
66                                      defaultButton:nil /* localized 'ok' */
67                                    alternateButton:nil
68                                        otherButton:nil
69                          informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
70     [[alert retain] autorelease];
71     [alert runModal];
72     return flightgear::MSG_BOX_OK;
73 }
74
75
76
77 flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
78                                                const std::string& text)
79 {
80     CocoaAutoreleasePool pool;
81     NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
82                                      defaultButton:@"Quit FlightGear"
83                                    alternateButton:nil
84                                        otherButton:nil
85                          informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
86     [[alert retain] autorelease];
87     [alert runModal];
88     return flightgear::MSG_BOX_OK;
89 }
90
91 void cocoaOpenUrl(const std::string& url)
92 {
93   CocoaAutoreleasePool pool;
94   NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
95   [[NSWorkspace sharedWorkspace] openURL:nsu];
96 }
97
98 CocoaAutoreleasePool::CocoaAutoreleasePool()
99 {
100     pool = [[NSAutoreleasePool alloc] init];
101 }
102
103 CocoaAutoreleasePool::~CocoaAutoreleasePool()
104 {
105     [pool release];
106 }
107
108 SGPath platformDefaultDataPath()
109 {
110     CocoaAutoreleasePool ap;
111     NSFileManager* fm = [NSFileManager defaultManager];
112     
113     NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
114                                      inDomain:NSUserDomainMask
115                              appropriateForURL:Nil
116                                        create:YES
117                                          error:nil];
118     if (!appSupportUrl) {
119         return SGPath();
120     }
121     
122     SGPath appData(URLToPath(appSupportUrl));
123     appData.append("FlightGear");
124     return appData;
125 }
126
127 namespace flightgear
128 {
129     
130 std::string Options::platformDefaultRoot() const
131 {
132     CocoaAutoreleasePool ap;
133     
134     NSURL* url = [[NSBundle mainBundle] resourceURL];
135     SGPath dataDir(URLToPath(url));
136     dataDir.append("data");
137     return dataDir.str();
138 }
139     
140 } // of namespace flightgear