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