diff --git a/include/wx/private/browserhack28.h b/include/wx/private/browserhack28.h new file mode 100644 index 0000000000..7d0fc05875 --- /dev/null +++ b/include/wx/private/browserhack28.h @@ -0,0 +1,20 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/private/browserhack28.h +// Purpose: Allows GUI library to override base wxLaunchDefaultBrowser. +// Author: David Elliott +// Modified by: +// Created: 2007-08-19 +// RCS-ID: $Id$ +// Copyright: (c) David Elliott +// Licence: wxWidgets licence +///////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_PRIVATE_BROWSERHACK28_H_ +#define _WX_PRIVATE_BROWSERHACK28_H_ + +typedef bool (*wxLaunchDefaultBrowserImpl_t)(const wxString& url, int flags); + +// Function the GUI library can call to provide a better implementation +WXDLLIMPEXP_BASE void wxSetLaunchDefaultBrowserImpl(wxLaunchDefaultBrowserImpl_t newImpl); + +#endif //ndef _WX_PRIVATE_BROWSERHACK28_H_ diff --git a/src/cocoa/utils.mm b/src/cocoa/utils.mm index c2112fc552..f17bc6b675 100644 --- a/src/cocoa/utils.mm +++ b/src/cocoa/utils.mm @@ -27,6 +27,11 @@ #include #include +#include "wx/cocoa/string.h" + +#import +#import + void wxDisplaySize(int *width, int *height) { // TODO @@ -92,6 +97,28 @@ void wxBell() // TODO } +#include "wx/private/browserhack28.h" + +// Private helper method for wxLaunchDefaultBrowser +static bool wxCocoaLaunchDefaultBrowser(const wxString& url, int flags) +{ + // NOTE: We ignore the flags + return [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString:wxNSStringWithWxString(url)]] != NO; +} + +// For this to work the linker (particularly the static one) must link in this object file and the C++ +// runtime must call the static initializer. It is basically guaranteed that the linker won't throw us +// away because some of the wxGUIAppTraits virtual methods are implemented in this file. +static class DoFixLaunchDefaultBrowser +{ +public: + DoFixLaunchDefaultBrowser() + { + // Tell the base library we can launch a default browser better. + wxSetLaunchDefaultBrowserImpl(&wxCocoaLaunchDefaultBrowser); + } +} s_launchDefaultBrowserFixer; + #if 0 // DFE: These aren't even implemented by wxGTK, and no wxWidgets code calls // them. If someone needs them, then they'll get a link error diff --git a/src/common/utilscmn.cpp b/src/common/utilscmn.cpp index b615472858..82542e4b09 100644 --- a/src/common/utilscmn.cpp +++ b/src/common/utilscmn.cpp @@ -725,23 +725,23 @@ long wxExecute(const wxString& command, // Launch default browser // ---------------------------------------------------------------------------- -bool wxLaunchDefaultBrowser(const wxString& urlOrig, int flags) +#include "wx/private/browserhack28.h" + +static bool wxLaunchDefaultBrowserBaseImpl(const wxString& url, int flags); + +// Use wxLaunchDefaultBrowserBaseImpl by default +static wxLaunchDefaultBrowserImpl_t s_launchBrowserImpl = &wxLaunchDefaultBrowserBaseImpl; + +// Function the GUI library can call to provide a better implementation +WXDLLIMPEXP_BASE void wxSetLaunchDefaultBrowserImpl(wxLaunchDefaultBrowserImpl_t newImpl) +{ + s_launchBrowserImpl = newImpl!=NULL ? newImpl : &wxLaunchDefaultBrowserBaseImpl; +} + +static bool wxLaunchDefaultBrowserBaseImpl(const wxString& url, int flags) { wxUnusedVar(flags); - // set the scheme of url to http if it does not have one - // RR: This doesn't work if the url is just a local path - wxString url(urlOrig); - wxURI uri(url); - if ( !uri.HasScheme() ) - { - if (wxFileExists(urlOrig)) - url.Prepend( wxT("file://") ); - else - url.Prepend(wxT("http://")); - } - - #if defined(__WXMSW__) #if wxUSE_IPC @@ -923,6 +923,25 @@ bool wxLaunchDefaultBrowser(const wxString& urlOrig, int flags) wxLogError(_T("No default application configured for HTML files.")); #endif // !wxUSE_MIMETYPE && !__WXMSW__ + return false; +} + +bool wxLaunchDefaultBrowser(const wxString& urlOrig, int flags) +{ + // set the scheme of url to http if it does not have one + // RR: This doesn't work if the url is just a local path + wxString url(urlOrig); + wxURI uri(url); + if ( !uri.HasScheme() ) + { + if (wxFileExists(urlOrig)) + url.Prepend( wxT("file://") ); + else + url.Prepend(wxT("http://")); + } + + if(s_launchBrowserImpl(url, flags)) + return true; wxLogSysError(_T("Failed to open URL \"%s\" in default browser."), url.c_str()); diff --git a/version-script.in b/version-script.in index 7bb057bd73..3f5272b304 100644 --- a/version-script.in +++ b/version-script.in @@ -65,6 +65,7 @@ *wxMenuItemBase*GetLabelText*; *wxMenuItem*GetItemLabel*; *wxMetafileDC*DoGetTextExtent*; + *wxSetLaunchDefaultBrowserImpl*; *wxWindowMSW*GetThemedBorderStyle*; *wxWizard*GetBitmap*; *wxWizard*SetBitmap*;