]> git.mxchange.org Git - flightgear.git/commitdiff
Refactor Cocoa helpers.
authorJames Turner <zakalawe@mac.com>
Sat, 16 Nov 2013 10:05:09 +0000 (10:05 +0000)
committerJames Turner <zakalawe@mac.com>
Sat, 16 Nov 2013 10:58:17 +0000 (10:58 +0000)
Change away from using deprecated FSFindFolder.

14 files changed:
src/GUI/CMakeLists.txt
src/GUI/CocoaAutoreleasePool.hxx [deleted file]
src/GUI/CocoaFileDialog.hxx
src/GUI/CocoaFileDialog.mm
src/GUI/CocoaHelpers.h [new file with mode: 0644]
src/GUI/CocoaHelpers.mm [new file with mode: 0644]
src/GUI/CocoaHelpers_private.h [new file with mode: 0644]
src/GUI/CocoaMessageBox.mm [deleted file]
src/GUI/CocoaMouseCursor.mm
src/GUI/FGCocoaMenuBar.hxx
src/GUI/FGCocoaMenuBar.mm
src/GUI/gui_funcs.cxx
src/Main/fg_init.cxx
src/Scripting/ClipboardCocoa.mm

index 573bbf452d3e8cf1f0e9416594decc78e0bb31cc..239faf28477e247d27e895e86127bea9256e2ae9 100644 (file)
@@ -56,11 +56,12 @@ if (APPLE)
     list(APPEND HEADERS FGCocoaMenuBar.hxx 
         CocoaFileDialog.hxx 
         CocoaMouseCursor.hxx
-        CocoaAutoreleasePool.hxx)
+        CocoaHelpers.h
+        CocoaHelpers_private.h)
     list(APPEND SOURCES FGCocoaMenuBar.mm 
         CocoaFileDialog.mm
         CocoaMouseCursor.mm
-        CocoaMessageBox.mm)
+        CocoaHelpers.mm)
 endif()
                
 flightgear_component(GUI "${SOURCES}" "${HEADERS}")
diff --git a/src/GUI/CocoaAutoreleasePool.hxx b/src/GUI/CocoaAutoreleasePool.hxx
deleted file mode 100644 (file)
index bd283df..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-#ifndef FG_GUI_COCOA_AUTORELEASE_POOL_HXX
-#define FG_GUI_COCOA_AUTORELEASE_POOL_HXX
-
-#include <Foundation/NSAutoreleasePool.h>
-
-class CocoaAutoreleasePool
-{
-public:
-    CocoaAutoreleasePool()
-    {
-      pool = [[NSAutoreleasePool alloc] init];
-    }
-
-    ~CocoaAutoreleasePool()
-    {
-      [pool release];
-    }
-
-private:
-    NSAutoreleasePool* pool;
-};
-  
-  
-#endif // of FG_GUI_COCOA_AUTORELEASE_POOL_HXX
\ No newline at end of file
index 2636d0488d7bb56aa8379f78839a521ae1ca4045..71965215bec4272cd6278f50436422f48ca8d046 100644 (file)
@@ -1,4 +1,22 @@
-// CocoaFileDialog.hxx - file dialog implemented using Cocoa
+// CocoaFileDialog.h - Cocoa implementation of file-dialog interface
+
+// Copyright (C) 2013 James Turner <zakalawe@mac.com>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+
 
 #ifndef FG_COCOA_FILE_DIALOG_HXX
 #define FG_COCOA_FILE_DIALOG_HXX 1
index a16a7892f77c4440de099335529df7875a72da66..6f81bc0c1e2b5ddc002431ed48967da2bffff8d2 100644 (file)
@@ -1,3 +1,21 @@
+// CocoaFileDialog.mm - Cocoa implementation of file-dialog interface
+
+// Copyright (C) 2013 James Turner <zakalawe@mac.com>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
 
 
 #include "CocoaFileDialog.hxx"
 #include <simgear/debug/logstream.hxx>
 #include <simgear/misc/strutils.hxx>
 
+#include <GUI/CocoaHelpers_private.h>
 #include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
 #include <Viewer/renderer.hxx>
 
-static NSString* stdStringToCocoa(const std::string& s)
-{
-    return [NSString stringWithUTF8String:s.c_str()];
-}
-
-static NSURL* pathToNSURL(const SGPath& aPath)
-{
-    return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
-}
-
 // 10.6 compiler won't accept block-scoped locals in Objective-C++,
 // so making these globals.
 static NSString* completion_path = nil;
diff --git a/src/GUI/CocoaHelpers.h b/src/GUI/CocoaHelpers.h
new file mode 100644 (file)
index 0000000..9b4802a
--- /dev/null
@@ -0,0 +1,37 @@
+// CocoaHelpers.h - C++ interface to Cocoa/AppKit helpers
+
+// Copyright (C) 2013 James Turner <zakalawe@mac.com>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+
+#ifndef FG_GUI_COCOA_HELPERS_H
+#define FG_GUI_COCOA_HELPERS_H
+
+#include <string>
+
+#include <simgear/misc/sg_path.hxx>
+
+/**
+ * open a URL using the system's web-browser
+ */
+void cocoaOpenUrl(const std::string& url);
+
+/**
+ * Cocoa implementation so we can use NSURL
+ */
+SGPath platformDefaultDataPath();
+
+#endif // of FG_GUI_COCOA_HELPERS_H
\ No newline at end of file
diff --git a/src/GUI/CocoaHelpers.mm b/src/GUI/CocoaHelpers.mm
new file mode 100644 (file)
index 0000000..6ea15e2
--- /dev/null
@@ -0,0 +1,124 @@
+// CocoaHelpers.mm - C++ implementation of Cocoa/AppKit helpers
+
+// Copyright (C) 2013 James Turner <zakalawe@mac.com>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+
+#include "CocoaHelpers.h"
+#include "CocoaHelpers_private.h"
+
+// standard
+#include <string>
+
+// OS
+#include <Cocoa/Cocoa.h>
+#include <Foundation/NSAutoreleasePool.h>
+
+// simgear
+#include <simgear/misc/sg_path.hxx>
+
+// flightgear
+#include <GUI/MessageBox.hxx>
+
+NSString* stdStringToCocoa(const std::string& s)
+{
+    return [NSString stringWithUTF8String:s.c_str()];
+}
+
+std::string stdStringFromCocoa(NSString* s)
+{
+    return std::string([s UTF8String]);
+}
+
+NSURL* pathToNSURL(const SGPath& aPath)
+{
+    return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
+}
+
+SGPath URLToPath(NSURL* url)
+{
+    if (!url) {
+        return SGPath();
+    }
+    
+    return SGPath([[url path] UTF8String]);
+}
+
+flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
+                                             const std::string& text)
+{
+    CocoaAutoreleasePool pool;
+    NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
+                                     defaultButton:nil /* localized 'ok' */
+                                   alternateButton:nil
+                                       otherButton:nil
+                         informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
+    [[alert retain] autorelease];
+    [alert runModal];
+    return flightgear::MSG_BOX_OK;
+}
+
+
+
+flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
+                                               const std::string& text)
+{
+    CocoaAutoreleasePool pool;
+    NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
+                                     defaultButton:@"Quit FlightGear"
+                                   alternateButton:nil
+                                       otherButton:nil
+                         informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
+    [[alert retain] autorelease];
+    [alert runModal];
+    return flightgear::MSG_BOX_OK;
+}
+
+void cocoaOpenUrl(const std::string& url)
+{
+  CocoaAutoreleasePool pool;
+  NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
+  [[NSWorkspace sharedWorkspace] openURL:nsu];
+}
+
+CocoaAutoreleasePool::CocoaAutoreleasePool()
+{
+    pool = [[NSAutoreleasePool alloc] init];
+}
+
+CocoaAutoreleasePool::~CocoaAutoreleasePool()
+{
+    [pool release];
+}
+
+SGPath platformDefaultDataPath()
+{
+    CocoaAutoreleasePool ap;
+    NSFileManager* fm = [NSFileManager defaultManager];
+    
+    NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
+                                     inDomain:NSUserDomainMask
+                             appropriateForURL:Nil
+                                       create:YES
+                                         error:nil];
+    if (!appSupportUrl) {
+        return SGPath();
+    }
+    
+    SGPath appData(URLToPath(appSupportUrl));
+    appData.append("FlightGear");
+    return appData;
+}
diff --git a/src/GUI/CocoaHelpers_private.h b/src/GUI/CocoaHelpers_private.h
new file mode 100644 (file)
index 0000000..3f65221
--- /dev/null
@@ -0,0 +1,49 @@
+// CocoaHelpers_private.h - common Objective-C/Cocoa helper functions
+
+// Copyright (C) 2013 James Turner <zakalawe@mac.com>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+
+
+#ifndef FG_GUI_COCOA_HELPERS_PRIVATE_H
+#define FG_GUI_COCOA_HELPERS_PRIVATE_H
+
+#include <string>
+
+// forward decls
+class SGPath;
+
+@class NSString;
+@class NSURL;
+@class NSAutoreleasePool;
+
+NSString* stdStringToCocoa(const std::string& s);
+NSURL* pathToNSURL(const SGPath& aPath);
+SGPath URLToPath(NSURL* url);
+std::string stdStringFromCocoa(NSString* s);
+
+class CocoaAutoreleasePool
+{
+public:
+    CocoaAutoreleasePool();
+
+    ~CocoaAutoreleasePool();
+
+private:
+    NSAutoreleasePool* pool;
+};
+  
+#endif
\ No newline at end of file
diff --git a/src/GUI/CocoaMessageBox.mm b/src/GUI/CocoaMessageBox.mm
deleted file mode 100644 (file)
index cc52168..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-
-#include <string>
-
-#include <Cocoa/Cocoa.h>
-#include <GUI/CocoaAutoreleasePool.hxx>
-#include <GUI/MessageBox.hxx>
-
-static NSString* stdStringToCocoa(const std::string& s)
-{
-  return [NSString stringWithUTF8String:s.c_str()];
-}
-
-flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
-                                             const std::string& text)
-{
-    CocoaAutoreleasePool pool;
-    NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
-                                     defaultButton:nil /* localized 'ok' */
-                                   alternateButton:nil
-                                       otherButton:nil
-                         informativeTextWithFormat:@"%@",stdStringToCocoa(text)];
-    [[alert retain] autorelease];
-    [alert runModal];
-    return flightgear::MSG_BOX_OK;
-}
-
-
-
-flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
-                                               const std::string& text)
-{
-    CocoaAutoreleasePool pool;
-    NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
-                                     defaultButton:@"Quit FlightGear"
-                                   alternateButton:nil
-                                       otherButton:nil
-                         informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
-    [[alert retain] autorelease];
-    [alert runModal];
-    return flightgear::MSG_BOX_OK;
-}
-
index 53dcccf009a1637d4c5efa79c43d4366265705bc..3a31fffc2187fd9547e5143e420ac64a425ea9fd 100644 (file)
@@ -23,7 +23,7 @@
 #include <map>
 
 #include <Main/globals.hxx>
-#include <GUI/CocoaAutoreleasePool.hxx>
+#include <GUI/CocoaHelpers_private.h>
 
 class CocoaMouseCursor::CocoaMouseCursorPrivate
 {
index 3ddc30cb46cf777b7a8c56bc5cb67faad1c4606c..694561aaec146ebcf6f63730de2e5861f8a70deb 100644 (file)
@@ -62,9 +62,4 @@ private:
   
 };
 
-/**
- * open a URL using the system's web-browser
- */
-void cocoaOpenUrl(const std::string& url);
-
 #endif // __MENUBAR_HXX
index d70baa5d5f01bed6b23486ade24e03a7cc526eba..b0e7fa442bf77b60749ea3cc2e1ca98aff0d425a 100644 (file)
@@ -11,7 +11,7 @@
 #include <simgear/misc/strutils.hxx>
 
 #include <Main/fg_props.hxx>
-#include <GUI/CocoaAutoreleasePool.hxx>
+#include <GUI/CocoaHelpers_private.h>
 
 #include <iostream>
 
@@ -65,11 +65,6 @@ public:
 
 @end
 
-static NSString* stdStringToCocoa(const string& s)
-{
-  return [NSString stringWithUTF8String:s.c_str()];
-}
-
 static void setFunctionKeyShortcut(const std::string& shortcut, NSMenuItem* item)
 {
     unichar shortcutChar = NSF1FunctionKey;
@@ -295,9 +290,3 @@ void FGCocoaMenuBar::hide()
   // no-op
 }
 
-void cocoaOpenUrl(const std::string& url)
-{
-  CocoaAutoreleasePool pool;
-  NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
-  [[NSWorkspace sharedWorkspace] openURL:nsu];
-}
index 2a084dad679dde1b94be538768099eaca2ef9f5b..8aff7f4658c5a4abdd8a98540e544e6bb9ede5d4 100644 (file)
@@ -64,8 +64,8 @@
 #  include <shellapi.h>
 #endif
 
-#ifdef SG_MAC
-# include "FGCocoaMenuBar.hxx" // for cocoaOpenUrl
+#if defined(SG_MAC)
+# include <GUI/CocoaHelpers.h> // for cocoaOpenUrl
 #endif
 
 #include "gui.h"
index f24c0f5288584ded740159312cfec270f7ec6144..8905a6a1deef0cbcaaaebbacf9c01c66498fb193 100644 (file)
 #include "positioninit.hxx"
 #include "util.hxx"
 
+#if defined(SG_MAC)
+#include <GUI/CocoaHelpers.h> // for Mac impl of platformDefaultDataPath()
+#endif
+
 using std::string;
 using std::endl;
 using std::cerr;
@@ -386,27 +390,10 @@ static SGPath platformDefaultDataPath()
   return config;
 }
 
-#elif __APPLE__
-#include <CoreServices/CoreServices.h>
-
-static SGPath platformDefaultDataPath()
-{
-  FSRef ref;
-  OSErr err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, false, &ref);
-  if (err) {
-    return SGPath();
-  }
+#elif defined(SG_MAC)
 
-  unsigned char path[1024];
-  if (FSRefMakePath(&ref, path, 1024) != noErr) {
-    return SGPath();
-  }
+// platformDefaultDataPath defined in GUI/CocoaHelpers.h
 
-  SGPath appData;
-  appData.set((const char*) path);
-  appData.append("FlightGear");
-  return appData;
-}
 #else
 static SGPath platformDefaultDataPath()
 {
index d6bb63897bdc85cf513c808d5801b7b5a337ba2e..7279cd208bd6e5983135ebe02287394e8789112c 100644 (file)
 
 #include <Cocoa/Cocoa.h>
 
-namespace {
-  class CocoaAutoreleasePool
-  {
-  public:
-    CocoaAutoreleasePool()
-    {
-      pool = [[NSAutoreleasePool alloc] init];
-    }
-    
-    ~CocoaAutoreleasePool()
-    {
-      [pool release];
-    }
-    
-  private:
-    NSAutoreleasePool* pool;
-  };
-}
-
-static NSString* stdStringToCocoa(const std::string& s)
-{
-  return [NSString stringWithUTF8String:s.c_str()];
-}
-
-static std::string stdStringFromCocoa(NSString* s)
-{
-  return std::string([s UTF8String]);
-}
+#include <GUI/CocoaHelpers_private.h>
 
 /**
  */