| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/msw/utilswin.cpp |
| 3 | // Purpose: Various utility functions only available in Windows GUI |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 21.06.2003 (extracted from msw/utils.cpp) |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #include "wx/wxprec.h" |
| 13 | |
| 14 | #ifdef __BORLANDC__ |
| 15 | #pragma hdrstop |
| 16 | #endif |
| 17 | |
| 18 | #ifndef WX_PRECOMP |
| 19 | #include "wx/utils.h" |
| 20 | #endif //WX_PRECOMP |
| 21 | |
| 22 | #include "wx/msw/private.h" // includes <windows.h> |
| 23 | #include "wx/msw/registry.h" |
| 24 | #include <shellapi.h> // needed for SHELLEXECUTEINFO |
| 25 | |
| 26 | // ---------------------------------------------------------------------------- |
| 27 | // Launch document with default app |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | |
| 30 | bool wxLaunchDefaultApplication(const wxString& document, int flags) |
| 31 | { |
| 32 | wxUnusedVar(flags); |
| 33 | |
| 34 | WinStruct<SHELLEXECUTEINFO> sei; |
| 35 | sei.lpFile = document.t_str(); |
| 36 | #ifdef __WXWINCE__ |
| 37 | sei.nShow = SW_SHOWNORMAL; // SW_SHOWDEFAULT not defined under CE (#10216) |
| 38 | #else |
| 39 | sei.nShow = SW_SHOWDEFAULT; |
| 40 | #endif |
| 41 | |
| 42 | // avoid Windows message box in case of error for consistency with |
| 43 | // wxLaunchDefaultBrowser() even if don't show the error ourselves in this |
| 44 | // function |
| 45 | sei.fMask = SEE_MASK_FLAG_NO_UI; |
| 46 | |
| 47 | if ( ::ShellExecuteEx(&sei) ) |
| 48 | return true; |
| 49 | |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | // Launch default browser |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | |
| 57 | bool wxDoLaunchDefaultBrowser(const wxString& url, const wxString& scheme, int flags) |
| 58 | { |
| 59 | wxUnusedVar(flags); |
| 60 | |
| 61 | #if wxUSE_IPC |
| 62 | if ( flags & wxBROWSER_NEW_WINDOW ) |
| 63 | { |
| 64 | // ShellExecuteEx() opens the URL in an existing window by default so |
| 65 | // we can't use it if we need a new window |
| 66 | wxRegKey key(wxRegKey::HKCR, scheme + wxT("\\shell\\open")); |
| 67 | if ( !key.Exists() ) |
| 68 | { |
| 69 | // try the default browser, it must be registered at least for http URLs |
| 70 | key.SetName(wxRegKey::HKCR, wxT("http\\shell\\open")); |
| 71 | } |
| 72 | |
| 73 | if ( key.Exists() ) |
| 74 | { |
| 75 | wxRegKey keyDDE(key, wxT("DDEExec")); |
| 76 | if ( keyDDE.Exists() ) |
| 77 | { |
| 78 | // we only know the syntax of WWW_OpenURL DDE request for IE, |
| 79 | // optimistically assume that all other browsers are compatible |
| 80 | // with it |
| 81 | static const wxChar *TOPIC_OPEN_URL = wxT("WWW_OpenURL"); |
| 82 | wxString ddeCmd; |
| 83 | wxRegKey keyTopic(keyDDE, wxT("topic")); |
| 84 | bool ok = keyTopic.Exists() && |
| 85 | keyTopic.QueryDefaultValue() == TOPIC_OPEN_URL; |
| 86 | if ( ok ) |
| 87 | { |
| 88 | ddeCmd = keyDDE.QueryDefaultValue(); |
| 89 | ok = !ddeCmd.empty(); |
| 90 | } |
| 91 | |
| 92 | if ( ok ) |
| 93 | { |
| 94 | // for WWW_OpenURL, the index of the window to open the URL |
| 95 | // in may be -1 (meaning "current") by default, replace it |
| 96 | // with 0 which means "new" (see KB article 160957), but |
| 97 | // don't fail if there is no -1 as at least for recent |
| 98 | // Firefox versions the default value already is 0 |
| 99 | ddeCmd.Replace(wxT("-1"), wxT("0"), |
| 100 | false /* only first occurrence */); |
| 101 | |
| 102 | // and also replace the parameters: the topic should |
| 103 | // contain a placeholder for the URL and we should fail if |
| 104 | // we didn't find it as this would mean that we have no way |
| 105 | // of passing the URL to the browser |
| 106 | ok = ddeCmd.Replace(wxT("%1"), url, false) == 1; |
| 107 | } |
| 108 | |
| 109 | if ( ok ) |
| 110 | { |
| 111 | // try to send it the DDE request now but ignore the errors |
| 112 | wxLogNull noLog; |
| 113 | |
| 114 | const wxString ddeServer = wxRegKey(keyDDE, wxT("application")); |
| 115 | if ( wxExecuteDDE(ddeServer, TOPIC_OPEN_URL, ddeCmd) ) |
| 116 | return true; |
| 117 | |
| 118 | // this is not necessarily an error: maybe browser is |
| 119 | // simply not running, but no matter, in any case we're |
| 120 | // going to launch it using ShellExecuteEx() below now and |
| 121 | // we shouldn't try to open a new window if we open a new |
| 122 | // browser anyhow |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | #endif // wxUSE_IPC |
| 128 | |
| 129 | WinStruct<SHELLEXECUTEINFO> sei; |
| 130 | sei.lpFile = url.c_str(); |
| 131 | sei.lpVerb = wxT("open"); |
| 132 | sei.nShow = SW_SHOWNORMAL; |
| 133 | sei.fMask = SEE_MASK_FLAG_NO_UI; // we give error message ourselves |
| 134 | |
| 135 | if ( ::ShellExecuteEx(&sei) ) |
| 136 | return true; |
| 137 | |
| 138 | return false; |
| 139 | } |