]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/webview_ie.cpp
In Refresh(), allow for possibility that child is mapped and parent is not.
[wxWidgets.git] / src / msw / webview_ie.cpp
index 92edb4ecd5e8a1d86f7faa0c35036bad56cc1344..80941967d225da50a524facb2a5f76bae38793e8 100644 (file)
@@ -16,7 +16,7 @@
 
 #include "wx/msw/webview_ie.h"
 
-#if wxUSE_WEBVIEW_IE
+#if wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE
 
 #include <olectl.h>
 #include <oleidl.h>
 #include "wx/msw/registry.h"
 #include "wx/msw/missing.h"
 #include "wx/filesys.h"
+#include "wx/dynlib.h"
+#include <initguid.h>
 
-wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewIE, wxWebView);
+/* These GUID definitions are our own implementation to support interfaces
+ * normally in urlmon.h. See include/wx/msw/webview_ie.h
+ */
+
+namespace {
 
-//We link to urlmon as it is required for CoInternetGetSession
-#pragma comment(lib, "urlmon")
+DEFINE_GUID(wxIID_IInternetProtocolRoot,0x79eac9e3,0xbaf9,0x11ce,0x8c,0x82,0,0xaa,0,0x4b,0xa9,0xb);
+DEFINE_GUID(wxIID_IInternetProtocol,0x79eac9e4,0xbaf9,0x11ce,0x8c,0x82,0,0xaa,0,0x4b,0xa9,0xb);
+
+}
+
+wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewIE, wxWebView);
 
 BEGIN_EVENT_TABLE(wxWebViewIE, wxControl)
     EVT_ACTIVEX(wxID_ANY, wxWebViewIE::onActiveXEvent)
@@ -73,16 +83,21 @@ bool wxWebViewIE::Create(wxWindow* parent,
 
     m_container = new wxActiveXContainer(this, IID_IWebBrowser2, m_webBrowser);
 
-    SetBackgroundStyle(wxBG_STYLE_PAINT);
-    SetDoubleBuffered(true);
     LoadURL(url);
     return true;
 }
 
+wxWebViewIE::~wxWebViewIE()
+{
+    for(unsigned int i = 0; i < m_factories.size(); i++)
+    {
+        m_factories[i]->Release();
+    }
+}
 
 void wxWebViewIE::LoadURL(const wxString& url)
 {
-    m_ie.CallMethod("Navigate", (BSTR) url.wc_str(), NULL, NULL, NULL, NULL);
+    m_ie.CallMethod("Navigate", wxConvertStringToOle(url));
 }
 
 void wxWebViewIE::SetPage(const wxString& html, const wxString& baseUrl)
@@ -152,12 +167,15 @@ wxString wxWebViewIE::GetPageSource() const
 
 wxWebViewZoom wxWebViewIE::GetZoom() const
 {
-    if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
-        return GetIEOpticalZoom();
-    else if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT)
-        return GetIETextZoom();
-    else
-        wxFAIL;
+    switch( m_zoomType )
+    {
+        case wxWEB_VIEW_ZOOM_TYPE_LAYOUT:
+            return GetIEOpticalZoom();
+        case wxWEB_VIEW_ZOOM_TYPE_TEXT:
+            return GetIETextZoom();
+        default:
+            wxFAIL;
+    }
 
     //Dummy return to stop compiler warnings
     return wxWEB_VIEW_ZOOM_MEDIUM;
@@ -166,12 +184,17 @@ wxWebViewZoom wxWebViewIE::GetZoom() const
 
 void wxWebViewIE::SetZoom(wxWebViewZoom zoom)
 {
-    if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
-        SetIEOpticalZoom(zoom);
-    else if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT)
-        SetIETextZoom(zoom);
-    else
-        wxFAIL;
+    switch( m_zoomType )
+    {
+        case wxWEB_VIEW_ZOOM_TYPE_LAYOUT:
+            SetIEOpticalZoom(zoom);
+            break;
+        case wxWEB_VIEW_ZOOM_TYPE_TEXT:
+            SetIETextZoom(zoom);
+            break;
+        default:
+            wxFAIL;
+    }
 }
 
 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level)
@@ -184,9 +207,12 @@ void wxWebViewIE::SetIETextZoom(wxWebViewZoom level)
     V_VT(&zoomVariant) = VT_I4;
     V_I4(&zoomVariant) = level;
 
-    HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM,
-                                          OLECMDEXECOPT_DONTPROMPTUSER,
-                                          &zoomVariant, NULL);
+#if wxDEBUG_LEVEL
+    HRESULT result =
+#endif
+            m_webBrowser->ExecWB(OLECMDID_ZOOM,
+                                 OLECMDEXECOPT_DONTPROMPTUSER,
+                                 &zoomVariant, NULL);
     wxASSERT(result == S_OK);
 }
 
@@ -196,9 +222,12 @@ wxWebViewZoom wxWebViewIE::GetIETextZoom() const
     VariantInit (&zoomVariant);
     V_VT(&zoomVariant) = VT_I4;
 
-    HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM,
-                                          OLECMDEXECOPT_DONTPROMPTUSER,
-                                          NULL, &zoomVariant);
+#if wxDEBUG_LEVEL
+    HRESULT result =
+#endif
+            m_webBrowser->ExecWB(OLECMDID_ZOOM,
+                                 OLECMDEXECOPT_DONTPROMPTUSER,
+                                 NULL, &zoomVariant);
     wxASSERT(result == S_OK);
 
     //We can safely cast here as we know that the range matches our enum
@@ -235,10 +264,13 @@ void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level)
             wxFAIL;
     }
 
-    HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM,
-                                          OLECMDEXECOPT_DODEFAULT,
-                                          &zoomVariant,
-                                          NULL);
+#if wxDEBUG_LEVEL
+    HRESULT result =
+#endif
+            m_webBrowser->ExecWB((OLECMDID)63 /*OLECMDID_OPTICAL_ZOOM*/,
+                                 OLECMDEXECOPT_DODEFAULT,
+                                 &zoomVariant,
+                                 NULL);
     wxASSERT(result == S_OK);
 }
 
@@ -248,9 +280,12 @@ wxWebViewZoom wxWebViewIE::GetIEOpticalZoom() const
     VariantInit (&zoomVariant);
     V_VT(&zoomVariant) = VT_I4;
 
-    HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM,
-                                          OLECMDEXECOPT_DODEFAULT, NULL,
-                                          &zoomVariant);
+#if wxDEBUG_LEVEL
+    HRESULT result =
+#endif
+            m_webBrowser->ExecWB((OLECMDID)63 /*OLECMDID_OPTICAL_ZOOM*/,
+                                 OLECMDEXECOPT_DODEFAULT, NULL,
+                                 &zoomVariant);
     wxASSERT(result == S_OK);
 
     const int zoom = V_I4(&zoomVariant);
@@ -427,14 +462,17 @@ void wxWebViewIE::SetOfflineMode(bool offline)
 {
     // FIXME: the wxWidgets docs do not really document what the return
     //        parameter of PutProperty is
-    const bool success = m_ie.PutProperty("Offline", (offline ?
-                                                      VARIANT_TRUE :
-                                                      VARIANT_FALSE));
+#if wxDEBUG_LEVEL
+    const bool success =
+#endif
+            m_ie.PutProperty("Offline", (offline ?
+                                         VARIANT_TRUE :
+                                         VARIANT_FALSE));
     wxASSERT(success);
 }
 
 bool wxWebViewIE::IsBusy() const
-{ 
+{
     if (m_isBusy) return true;
 
     wxVariant out = m_ie.GetProperty("Busy");
@@ -660,24 +698,41 @@ void wxWebViewIE::RunScript(const wxString& javascript)
         VARIANT level;
         VariantInit(&level);
         V_VT(&level) = VT_EMPTY;
-        window->execScript(SysAllocString(javascript), SysAllocString(language), &level);
+        window->execScript(SysAllocString(javascript.wc_str()),
+                           SysAllocString(language.wc_str()),
+                           &level);
     }
     document->Release();
 }
 
 void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
 {
-    ClassFactory* cf = new ClassFactory(handler);
-    IInternetSession* session;
-    if(FAILED(CoInternetGetSession(0, &session, 0)))
+    wxDynamicLibrary urlMon(wxT("urlmon.dll"));
+    if(urlMon.HasSymbol(wxT("CoInternetGetSession")))
     {
-        wxFAIL_MSG("Could not retrive internet session");
-    }
+        typedef HRESULT (WINAPI *CoInternetGetSession_t)(DWORD, wxIInternetSession**, DWORD);
+        wxDYNLIB_FUNCTION(CoInternetGetSession_t, CoInternetGetSession, urlMon);
 
-    HRESULT hr = session->RegisterNameSpace(cf, CLSID_FileProtocol, handler->GetName(), 0, NULL, 0);
-    if(FAILED(hr))
+        ClassFactory* cf = new ClassFactory(handler);
+        wxIInternetSession* session;
+        HRESULT res = (*pfnCoInternetGetSession)(0, &session, 0);
+        if(FAILED(res))
+        {
+            wxFAIL_MSG("Could not retrive internet session");
+        }
+
+        HRESULT hr = session->RegisterNameSpace(cf, CLSID_FileProtocol,
+                                                handler->GetName().wc_str(),
+                                                0, NULL, 0);
+        if(FAILED(hr))
+        {
+            wxFAIL_MSG("Could not register protocol");
+        }
+        m_factories.push_back(cf);
+    }
+    else
     {
-        wxFAIL_MSG("Could not register protocol");
+        wxFAIL_MSG("urlmon does not contain CoInternetGetSession");
     }
 }
 
@@ -724,8 +779,17 @@ void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
 
             wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
                                  GetId(), url, target);
-            event.SetEventObject(this);
-            HandleWindowEvent(event);
+
+            //skip empty javascript events.
+            if(url == "javascript:\"\"" && target.IsEmpty())
+            {
+                event.Veto();
+            }
+            else
+            {
+                event.SetEventObject(this);
+                HandleWindowEvent(event);
+            }
 
             if (!event.IsAllowed())
             {
@@ -774,12 +838,12 @@ void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
 
             //As we are complete we also add to the history list, but not if the
             //page is not the main page, ie it is a subframe
-            //We also have to check if we are loading a file:// url, if so we 
+            //We also have to check if we are loading a file:// url, if so we
             //need to change the comparison as ie passes back a different style
             //of url
-            if(m_historyEnabled && !m_historyLoadingFromList && 
-              (url == GetCurrentURL() || 
-              (GetCurrentURL().substr(0, 4) == "file" && 
+            if(m_historyEnabled && !m_historyLoadingFromList &&
+              (url == GetCurrentURL() ||
+              (GetCurrentURL().substr(0, 4) == "file" &&
                wxFileSystem::URLToFileName(GetCurrentURL()).GetFullPath() == url)))
             {
                 //If we are not at the end of the list, then erase everything
@@ -984,10 +1048,10 @@ ULONG VirtualProtocol::AddRef()
 
 HRESULT VirtualProtocol::QueryInterface(REFIID riid, void **ppvObject)
 {
-    if(riid == IID_IUnknown || riid == IID_IInternetProtocolRoot || 
-       riid == IID_IInternetProtocol)
+    if(riid == IID_IUnknown || riid == wxIID_IInternetProtocolRoot ||
+       riid == wxIID_IInternetProtocol)
     {
-        *ppvObject = (IInternetProtocol*)this;
+        *ppvObject = (wxIInternetProtocol*)this;
         AddRef();
         return S_OK;
     }
@@ -1012,8 +1076,8 @@ ULONG VirtualProtocol::Release()
     }
 }
 
-HRESULT VirtualProtocol::Start(LPCWSTR szUrl, IInternetProtocolSink *pOIProtSink,
-                               IInternetBindInfo *pOIBindInfo, DWORD grfPI, 
+HRESULT VirtualProtocol::Start(LPCWSTR szUrl, wxIInternetProtocolSink *pOIProtSink,
+                               wxIInternetBindInfo *pOIBindInfo, DWORD grfPI,
                                HANDLE_PTR dwReserved)
 {
     wxUnusedVar(szUrl);
@@ -1021,7 +1085,7 @@ HRESULT VirtualProtocol::Start(LPCWSTR szUrl, IInternetProtocolSink *pOIProtSink
     wxUnusedVar(grfPI);
     wxUnusedVar(dwReserved);
     m_protocolSink = pOIProtSink;
-    
+
     //We get the file itself from the protocol handler
     m_file = m_handler->GetFile(szUrl);
 
@@ -1032,17 +1096,17 @@ HRESULT VirtualProtocol::Start(LPCWSTR szUrl, IInternetProtocolSink *pOIProtSink
     //We return the stream length for current and total size as we can always
     //read the whole file from the stream
     wxFileOffset length = m_file->GetStream()->GetLength();
-    m_protocolSink->ReportData(BSCF_FIRSTDATANOTIFICATION | 
-                               BSCF_DATAFULLYAVAILABLE |
-                               BSCF_LASTDATANOTIFICATION,
+    m_protocolSink->ReportData(wxBSCF_FIRSTDATANOTIFICATION |
+                               wxBSCF_DATAFULLYAVAILABLE |
+                               wxBSCF_LASTDATANOTIFICATION,
                                length, length);
-    return S_OK; 
+    return S_OK;
 }
 
 HRESULT VirtualProtocol::Read(void *pv, ULONG cb, ULONG *pcbRead)
 {
     //If the file is null we return false to indicte it is finished
-    if(!m_file) 
+    if(!m_file)
         return S_FALSE;
 
     wxStreamError err = m_file->GetStream()->Read(pv, cb).GetLastError();
@@ -1081,7 +1145,7 @@ HRESULT VirtualProtocol::Read(void *pv, ULONG cb, ULONG *pcbRead)
 HRESULT ClassFactory::CreateInstance(IUnknown* pUnkOuter, REFIID riid,
                                      void ** ppvObject)
 {
-    if (pUnkOuter) 
+    if (pUnkOuter)
         return CLASS_E_NOAGGREGATION;
     VirtualProtocol* vp = new VirtualProtocol(m_handler);
     vp->AddRef();
@@ -1089,7 +1153,7 @@ HRESULT ClassFactory::CreateInstance(IUnknown* pUnkOuter, REFIID riid,
     vp->Release();
     return hr;
 
-} 
+}
 
 STDMETHODIMP ClassFactory::LockServer(BOOL fLock)
 {
@@ -1132,6 +1196,6 @@ ULONG ClassFactory::Release(void)
         return 0;
     }
 
-} 
+}
 
-#endif
+#endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE