From fecb475145ba5b733f273354154beb59e4e94fe6 Mon Sep 17 00:00:00 2001 From: James Turner Date: Sat, 16 Nov 2013 10:05:09 +0000 Subject: [PATCH] Refactor Cocoa helpers. Change away from using deprecated FSFindFolder. --- src/GUI/CMakeLists.txt | 5 +- src/GUI/CocoaAutoreleasePool.hxx | 24 ------ src/GUI/CocoaFileDialog.hxx | 20 ++++- src/GUI/CocoaFileDialog.mm | 29 +++++--- src/GUI/CocoaHelpers.h | 37 +++++++++ src/GUI/CocoaHelpers.mm | 124 +++++++++++++++++++++++++++++++ src/GUI/CocoaHelpers_private.h | 49 ++++++++++++ src/GUI/CocoaMessageBox.mm | 42 ----------- src/GUI/CocoaMouseCursor.mm | 2 +- src/GUI/FGCocoaMenuBar.hxx | 5 -- src/GUI/FGCocoaMenuBar.mm | 13 +--- src/GUI/gui_funcs.cxx | 4 +- src/Main/fg_init.cxx | 25 ++----- src/Scripting/ClipboardCocoa.mm | 29 +------- 14 files changed, 262 insertions(+), 146 deletions(-) delete mode 100644 src/GUI/CocoaAutoreleasePool.hxx create mode 100644 src/GUI/CocoaHelpers.h create mode 100644 src/GUI/CocoaHelpers.mm create mode 100644 src/GUI/CocoaHelpers_private.h delete mode 100644 src/GUI/CocoaMessageBox.mm diff --git a/src/GUI/CMakeLists.txt b/src/GUI/CMakeLists.txt index 573bbf452..239faf284 100644 --- a/src/GUI/CMakeLists.txt +++ b/src/GUI/CMakeLists.txt @@ -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 index bd283df50..000000000 --- a/src/GUI/CocoaAutoreleasePool.hxx +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef FG_GUI_COCOA_AUTORELEASE_POOL_HXX -#define FG_GUI_COCOA_AUTORELEASE_POOL_HXX - -#include - -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 diff --git a/src/GUI/CocoaFileDialog.hxx b/src/GUI/CocoaFileDialog.hxx index 2636d0488..71965215b 100644 --- a/src/GUI/CocoaFileDialog.hxx +++ b/src/GUI/CocoaFileDialog.hxx @@ -1,4 +1,22 @@ -// CocoaFileDialog.hxx - file dialog implemented using Cocoa +// CocoaFileDialog.h - Cocoa implementation of file-dialog interface + +// Copyright (C) 2013 James Turner +// +// 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 diff --git a/src/GUI/CocoaFileDialog.mm b/src/GUI/CocoaFileDialog.mm index a16a7892f..6f81bc0c1 100644 --- a/src/GUI/CocoaFileDialog.mm +++ b/src/GUI/CocoaFileDialog.mm @@ -1,3 +1,21 @@ +// CocoaFileDialog.mm - Cocoa implementation of file-dialog interface + +// Copyright (C) 2013 James Turner +// +// 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" @@ -13,20 +31,11 @@ #include #include +#include #include
#include
#include -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 index 000000000..9b4802a28 --- /dev/null +++ b/src/GUI/CocoaHelpers.h @@ -0,0 +1,37 @@ +// CocoaHelpers.h - C++ interface to Cocoa/AppKit helpers + +// Copyright (C) 2013 James Turner +// +// 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 + +#include + +/** + * 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 index 000000000..6ea15e2d5 --- /dev/null +++ b/src/GUI/CocoaHelpers.mm @@ -0,0 +1,124 @@ +// CocoaHelpers.mm - C++ implementation of Cocoa/AppKit helpers + +// Copyright (C) 2013 James Turner +// +// 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 + +// OS +#include +#include + +// simgear +#include + +// flightgear +#include + +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 index 000000000..3f6522184 --- /dev/null +++ b/src/GUI/CocoaHelpers_private.h @@ -0,0 +1,49 @@ +// CocoaHelpers_private.h - common Objective-C/Cocoa helper functions + +// Copyright (C) 2013 James Turner +// +// 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 + +// 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 index cc521685c..000000000 --- a/src/GUI/CocoaMessageBox.mm +++ /dev/null @@ -1,42 +0,0 @@ - -#include - -#include -#include -#include - -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; -} - diff --git a/src/GUI/CocoaMouseCursor.mm b/src/GUI/CocoaMouseCursor.mm index 53dcccf00..3a31fffc2 100644 --- a/src/GUI/CocoaMouseCursor.mm +++ b/src/GUI/CocoaMouseCursor.mm @@ -23,7 +23,7 @@ #include #include
-#include +#include class CocoaMouseCursor::CocoaMouseCursorPrivate { diff --git a/src/GUI/FGCocoaMenuBar.hxx b/src/GUI/FGCocoaMenuBar.hxx index 3ddc30cb4..694561aae 100644 --- a/src/GUI/FGCocoaMenuBar.hxx +++ b/src/GUI/FGCocoaMenuBar.hxx @@ -62,9 +62,4 @@ private: }; -/** - * open a URL using the system's web-browser - */ -void cocoaOpenUrl(const std::string& url); - #endif // __MENUBAR_HXX diff --git a/src/GUI/FGCocoaMenuBar.mm b/src/GUI/FGCocoaMenuBar.mm index d70baa5d5..b0e7fa442 100644 --- a/src/GUI/FGCocoaMenuBar.mm +++ b/src/GUI/FGCocoaMenuBar.mm @@ -11,7 +11,7 @@ #include #include
-#include +#include #include @@ -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]; -} diff --git a/src/GUI/gui_funcs.cxx b/src/GUI/gui_funcs.cxx index 2a084dad6..8aff7f465 100644 --- a/src/GUI/gui_funcs.cxx +++ b/src/GUI/gui_funcs.cxx @@ -64,8 +64,8 @@ # include #endif -#ifdef SG_MAC -# include "FGCocoaMenuBar.hxx" // for cocoaOpenUrl +#if defined(SG_MAC) +# include // for cocoaOpenUrl #endif #include "gui.h" diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index f24c0f528..8905a6a1d 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -124,6 +124,10 @@ #include "positioninit.hxx" #include "util.hxx" +#if defined(SG_MAC) +#include // 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 - -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() { diff --git a/src/Scripting/ClipboardCocoa.mm b/src/Scripting/ClipboardCocoa.mm index d6bb63897..7279cd208 100644 --- a/src/Scripting/ClipboardCocoa.mm +++ b/src/Scripting/ClipboardCocoa.mm @@ -24,34 +24,7 @@ #include -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 /** */ -- 2.39.5