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