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