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