From: Vadim Zeitlin Date: Sat, 25 Mar 2006 17:00:40 +0000 (+0000) Subject: added wxStandardPaths::GetResourcesDir() and GetLocalizedResourcesDir() X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/3af9f2de592953bec39a58cc871678fa85a3b056 added wxStandardPaths::GetResourcesDir() and GetLocalizedResourcesDir() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@38370 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index e00d297736..acb7fa8b34 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -25,6 +25,7 @@ Deprecated methods since 2.6.x and their replacements All: - wxLaunchDefaultBrowser() now supports wxBROWSER_NEW_WINDOW flag. +- Added wxStandardPaths::GetResourcesDir() and GetLocalizedResourcesDir() - Added wxStringTokenizer::GetLastDelimiter(); improved documentation. - Speed improvements to wxRegEx when matching is done in a loop such as during a search and replace. diff --git a/include/wx/mac/corefoundation/stdpaths.h b/include/wx/mac/corefoundation/stdpaths.h index 784222671a..00b1ffbdf7 100644 --- a/include/wx/mac/corefoundation/stdpaths.h +++ b/include/wx/mac/corefoundation/stdpaths.h @@ -13,6 +13,10 @@ #define _WX_MAC_STDPATHS_H_ struct __CFBundle; +struct __CFURL; + +typedef const __CFURL * wxCFURLRef; +typedef __CFBundle * wxCFBundleRef; // ---------------------------------------------------------------------------- // wxStandardPaths @@ -25,8 +29,8 @@ public: ~wxStandardPathsCF(); // wxMac specific: allow user to specify a different bundle - wxStandardPathsCF(struct __CFBundle *bundle); - void SetBundle(struct __CFBundle *bundle); + wxStandardPathsCF(wxCFBundleRef bundle); + void SetBundle(wxCFBundleRef bundle); // implement base class pure virtuals virtual wxString GetConfigDir() const; @@ -35,8 +39,16 @@ public: virtual wxString GetLocalDataDir() const; virtual wxString GetUserDataDir() const; virtual wxString GetPluginsDir() const; + virtual wxString GetResourcesDir() const; + virtual wxString GetLocalizedResourcesDir(const wxChar *lang, + ResourceCat category) const; + protected: - struct __CFBundle *m_bundle; + // this function can be called with any of CFBundleCopyXXXURL function + // pointer as parameter + wxString GetFromFunc(wxCFURLRef (*func)(wxCFBundleRef)) const; + + wxCFBundleRef m_bundle; }; // If using UNIX (i.e. darwin) then use UNIX standard paths diff --git a/include/wx/msw/stdpaths.h b/include/wx/msw/stdpaths.h index e89e80b227..c12729857e 100644 --- a/include/wx/msw/stdpaths.h +++ b/include/wx/msw/stdpaths.h @@ -26,6 +26,8 @@ public: virtual wxString GetUserDataDir() const; virtual wxString GetUserLocalDataDir() const; virtual wxString GetPluginsDir() const; + virtual wxString GetLocalizedResourcesDir(const wxChar *lang, + ResourceCat category) const; protected: // get the path corresponding to the given standard CSIDL_XXX constant diff --git a/include/wx/stdpaths.h b/include/wx/stdpaths.h index c4b3cb2b7b..233fc7f3a8 100644 --- a/include/wx/stdpaths.h +++ b/include/wx/stdpaths.h @@ -25,6 +25,20 @@ class WXDLLIMPEXP_BASE wxStandardPathsBase { public: + // possible resources categorires + enum ResourceCat + { + // no special category + ResourceCat_None, + + // message catalog resources + ResourceCat_Messages, + + // end of enum marker + ResourceCat_Max + }; + + // return the global standard paths object static wxStandardPathsBase& Get(); @@ -74,6 +88,27 @@ public: // Contents/Plugins app bundle subdirectory under Mac virtual wxString GetPluginsDir() const = 0; + // get resources directory: resources are auxiliary files used by the + // application and include things like image and sound files + // + // same as GetDataDir() for all platforms except Mac where it returns + // Contents/Resources subdirectory of the app bundle + virtual wxString GetResourcesDir() const { return GetDataDir(); } + + // get localized resources directory containing the resource files of the + // specified category for the given language + // + // in general this is just GetResourcesDir()/lang under Windows and Unix + // and GetResourcesDir()/lang.lproj under Mac but is something quite + // different under Unix for message catalog category (namely the standard + // prefix/share/locale/lang/LC_MESSAGES) + virtual wxString + GetLocalizedResourcesDir(const wxChar *lang, + ResourceCat category = ResourceCat_None) const + { + return GetResourcesDir() + wxFILE_SEP_PATH + lang; + } + // virtual dtor for the base class virtual ~wxStandardPathsBase(); diff --git a/include/wx/unix/stdpaths.h b/include/wx/unix/stdpaths.h index 5e5cb19cab..2c55751965 100644 --- a/include/wx/unix/stdpaths.h +++ b/include/wx/unix/stdpaths.h @@ -41,6 +41,8 @@ public: virtual wxString GetLocalDataDir() const; virtual wxString GetUserDataDir() const; virtual wxString GetPluginsDir() const; + virtual wxString GetLocalizedResourcesDir(const wxChar *lang, + ResourceCat category) const; private: wxString m_prefix; diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 91876f7d42..06662dde58 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -85,7 +85,7 @@ #define TEST_WCHAR #define TEST_ZIP #else // #if TEST_ALL - #define TEST_DIR + #define TEST_STDPATHS #endif // some tests are interactive, define this to run them @@ -2685,6 +2685,15 @@ static void TestStandardPaths() wxPrintf(_T("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str()); wxPrintf(_T("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str()); wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str()); + wxPrintf(_T("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str()); + wxPrintf(_T("Localized res. dir:\t%s\n"), + stdp.GetLocalizedResourcesDir(_T("fr")).c_str()); + wxPrintf(_T("Message catalogs dir:\t%s\n"), + stdp.GetLocalizedResourcesDir + ( + _T("fr"), + wxStandardPaths::ResourceCat_Messages + ).c_str()); } #endif // TEST_STDPATHS diff --git a/src/mac/corefoundation/stdpaths_cf.cpp b/src/mac/corefoundation/stdpaths_cf.cpp index 20f2c8cf3a..62725328e3 100644 --- a/src/mac/corefoundation/stdpaths_cf.cpp +++ b/src/mac/corefoundation/stdpaths_cf.cpp @@ -9,6 +9,14 @@ // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + #include "wx/wxprec.h" #if wxUSE_STDPATHS @@ -35,23 +43,22 @@ #define kDefaultPathStyle kCFURLHFSPathStyle #endif -static wxString BundleRelativeURLToPath(CFURLRef relativeURL) -{ - CFURLRef absoluteURL = CFURLCopyAbsoluteURL(relativeURL); - wxCHECK_MSG(absoluteURL, wxEmptyString, wxT("Failed to resolve relative URL to absolute URL")); - CFStringRef cfStrPath = CFURLCopyFileSystemPath(absoluteURL,kDefaultPathStyle); - CFRelease(absoluteURL); - return wxMacCFStringHolder(cfStrPath).AsString(wxLocale::GetSystemEncoding()); -} +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxStandardPathsCF ctors/dtor +// ---------------------------------------------------------------------------- wxStandardPathsCF::wxStandardPathsCF() -: m_bundle(CFBundleGetMainBundle()) + : m_bundle(CFBundleGetMainBundle()) { CFRetain(m_bundle); } -wxStandardPathsCF::wxStandardPathsCF(struct __CFBundle *bundle) -: m_bundle(bundle) +wxStandardPathsCF::wxStandardPathsCF(wxCFBundleRef bundle) + : m_bundle(bundle) { CFRetain(m_bundle); } @@ -61,13 +68,45 @@ wxStandardPathsCF::~wxStandardPathsCF() CFRelease(m_bundle); } -void wxStandardPathsCF::SetBundle(struct __CFBundle *bundle) +// ---------------------------------------------------------------------------- +// wxStandardPathsCF Mac-specific methods +// ---------------------------------------------------------------------------- + +void wxStandardPathsCF::SetBundle(wxCFBundleRef bundle) { CFRetain(bundle); CFRelease(m_bundle); m_bundle = bundle; } +// ---------------------------------------------------------------------------- +// generic functions in terms of which the other ones are implemented +// ---------------------------------------------------------------------------- + +static wxString BundleRelativeURLToPath(CFURLRef relativeURL) +{ + CFURLRef absoluteURL = CFURLCopyAbsoluteURL(relativeURL); + wxCHECK_MSG(absoluteURL, wxEmptyString, wxT("Failed to resolve relative URL to absolute URL")); + CFStringRef cfStrPath = CFURLCopyFileSystemPath(absoluteURL,kDefaultPathStyle); + CFRelease(absoluteURL); + return wxMacCFStringHolder(cfStrPath).AsString(wxLocale::GetSystemEncoding()); +} + +wxString wxStandardPathsCF::GetFromFunc(wxCFURLRef (*func)(wxCFBundleRef)) const +{ + wxCHECK_MSG(m_bundle, wxEmptyString, + wxT("wxStandardPaths for CoreFoundation only works with bundled apps")); + CFURLRef relativeURL = (*func)(m_bundle); + wxCHECK_MSG(relativeURL, wxEmptyString, wxT("Couldn't get URL")); + wxString ret(BundleRelativeURLToPath(relativeURL)); + CFRelease(relativeURL); + return ret; +} + +// ---------------------------------------------------------------------------- +// wxStandardPathsCF public API +// ---------------------------------------------------------------------------- + wxString wxStandardPathsCF::GetConfigDir() const { // TODO: What do we do for pure Carbon? @@ -82,12 +121,7 @@ wxString wxStandardPathsCF::GetUserConfigDir() const wxString wxStandardPathsCF::GetDataDir() const { - wxCHECK_MSG(m_bundle, wxEmptyString, wxT("wxStandardPaths for CoreFoundation only works with bundled apps")); - CFURLRef relativeURL = CFBundleCopySharedSupportURL(m_bundle); - wxCHECK_MSG(relativeURL, wxEmptyString, wxT("Couldn't get SharedSupport URL")); - wxString ret(BundleRelativeURLToPath(relativeURL)); - CFRelease(relativeURL); - return ret; + return GetFromFunc(CFBundleCopySharedSupportURL); } wxString wxStandardPathsCF::GetLocalDataDir() const @@ -102,12 +136,20 @@ wxString wxStandardPathsCF::GetUserDataDir() const wxString wxStandardPathsCF::GetPluginsDir() const { - wxCHECK_MSG(m_bundle, wxEmptyString, wxT("wxStandardPaths for CoreFoundation only works with bundled apps")); - CFURLRef relativeURL = CFBundleCopyBuiltInPlugInsURL(m_bundle); - wxCHECK_MSG(relativeURL, wxEmptyString, wxT("Couldn't get BuiltInPlugIns URL")); - wxString ret(BundleRelativeURLToPath(relativeURL)); - CFRelease(relativeURL); - return ret; + return GetFromFunc(CFBundleCopyBuiltInPlugInsURL); +} + +wxString wxStandardPathsCF::GetResourcesDir() const +{ + return GetFromFunc(CFBundleCopyResourcesDirectoryURL); +} + +wxString +wxStandardPathsCF::GetLocalizedResourcesDir(const wxChar *lang, + ResourceCat category) const +{ + return wxStandardPathsBase:: + GetLocalizedResourcesDir(lang, category) + _T(".lproj"); } #endif // wxUSE_STDPATHS diff --git a/src/msw/stdpaths.cpp b/src/msw/stdpaths.cpp index 3b815eac7b..2a7a046dd6 100644 --- a/src/msw/stdpaths.cpp +++ b/src/msw/stdpaths.cpp @@ -278,7 +278,6 @@ wxString wxStandardPaths::GetPluginsDir() const return wxFileName(wxGetFullModuleName()).GetPath(); } - // ============================================================================ // wxStandardPathsWin16 implementation // ============================================================================ diff --git a/src/unix/stdpaths.cpp b/src/unix/stdpaths.cpp index 543294e147..cc425db3b4 100644 --- a/src/unix/stdpaths.cpp +++ b/src/unix/stdpaths.cpp @@ -93,6 +93,13 @@ wxString wxStandardPaths::GetPluginsDir() const return wxString(); // TODO: this is wrong, it should return something } +wxString +wxStandardPaths::GetLocalizedResourcesDir(const wxChar *lang, + ResourceCat category) const +{ + return wxStandardPathsBase::GetLocalizedResourcesDir(lang, category); +} + #else // !__VMS // ============================================================================ @@ -161,6 +168,16 @@ wxString wxStandardPaths::GetPluginsDir() const return AppendAppName(GetInstallPrefix() + _T("/lib")); } +wxString +wxStandardPaths::GetLocalizedResourcesDir(const wxChar *lang, + ResourceCat category) const +{ + if ( category != ResourceCat_Messages ) + return wxStandardPathsBase::GetLocalizedResourcesDir(lang, category); + + return GetInstallPrefix() + _T("/share/locale/") + lang + _T("/LC_MESSAGES"); +} + #endif // __VMS/!__VMS #endif // wxUSE_STDPATHS