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