]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/webview_ie.cpp
Implement wxDataViewModel::IsEnabled() support in generic implementation.
[wxWidgets.git] / src / msw / webview_ie.cpp
index c118ac694173b8a3fa57b49adc56e8d22ac7b11c..17e63ec3f3cf3b458e4e9c7c53e0a2571050bd07 100644 (file)
 #include "wx/msw/missing.h"
 #include "wx/filesys.h"
 #include "wx/dynlib.h"
+#include <initguid.h>
+
+/* These GUID definitions are our own implementation to support interfaces
+ * normally in urlmon.h. See include/wx/msw/webview_ie.h
+ */
+
+namespace {
+
+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);
 
@@ -71,8 +83,6 @@ bool wxWebViewIE::Create(wxWindow* parent,
 
     m_container = new wxActiveXContainer(this, IID_IWebBrowser2, m_webBrowser);
 
-    SetBackgroundStyle(wxBG_STYLE_PAINT);
-    SetDoubleBuffered(true);
     LoadURL(url);
     return true;
 }
@@ -87,48 +97,68 @@ wxWebViewIE::~wxWebViewIE()
 
 void wxWebViewIE::LoadURL(const wxString& url)
 {
-    m_ie.CallMethod("Navigate", (BSTR) url.wc_str());
+    m_ie.CallMethod("Navigate", wxConvertStringToOle(url));
 }
 
 void wxWebViewIE::SetPage(const wxString& html, const wxString& baseUrl)
 {
-    BSTR bstr = SysAllocString(html.wc_str());
-
-    // Creates a new one-dimensional array
+    BSTR bstr = SysAllocString(OLESTR(""));
     SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
     if (psaStrings != NULL)
     {
         VARIANT *param;
-
         HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
         param->vt = VT_BSTR;
         param->bstrVal = bstr;
-        hr = SafeArrayUnaccessData(psaStrings);
 
+        hr = SafeArrayUnaccessData(psaStrings);
+        
         IHTMLDocument2* document = GetDocument();
         document->write(psaStrings);
+        document->close();
         document->Release();
 
-        // SafeArrayDestroy calls SysFreeString for each BSTR
         SafeArrayDestroy(psaStrings);
 
-        //We send the events when we are done to mimic webkit
-        //Navigated event
-        wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
-                             GetId(), baseUrl, "");
-        event.SetEventObject(this);
-        HandleWindowEvent(event);
-
-        //Document complete event
-        event.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED);
-        event.SetEventObject(this);
-        HandleWindowEvent(event);
+        bstr = SysAllocString(html.wc_str());
+
+        // Creates a new one-dimensional array
+        psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
+        if (psaStrings != NULL)
+        {
+            hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
+            param->vt = VT_BSTR;
+            param->bstrVal = bstr;
+            hr = SafeArrayUnaccessData(psaStrings);
+
+            document = GetDocument();
+            document->write(psaStrings);
+            document->Release();
+
+            // SafeArrayDestroy calls SysFreeString for each BSTR
+            SafeArrayDestroy(psaStrings);
+
+            //We send the events when we are done to mimic webkit
+            //Navigated event
+            wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
+                                 GetId(), baseUrl, "");
+            event.SetEventObject(this);
+            HandleWindowEvent(event);
+
+            //Document complete event
+            event.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED);
+            event.SetEventObject(this);
+            HandleWindowEvent(event);
+        }
+        else
+        {
+            wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
+        }
     }
     else
     {
-        wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
+        wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL during clear");
     }
-
 }
 
 wxString wxWebViewIE::GetPageSource() const
@@ -157,12 +187,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;
@@ -171,12 +204,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)
@@ -189,9 +227,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);
 }
 
@@ -201,9 +242,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
@@ -240,10 +284,13 @@ void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level)
             wxFAIL;
     }
 
-    HRESULT result = m_webBrowser->ExecWB((OLECMDID)63 /*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);
 }
 
@@ -253,9 +300,12 @@ wxWebViewZoom wxWebViewIE::GetIEOpticalZoom() const
     VariantInit (&zoomVariant);
     V_VT(&zoomVariant) = VT_I4;
 
-    HRESULT result = m_webBrowser->ExecWB((OLECMDID)63 /*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);
@@ -432,14 +482,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");
@@ -665,7 +718,9 @@ 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();
 }
@@ -675,18 +730,20 @@ void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
     wxDynamicLibrary urlMon(wxT("urlmon.dll"));
     if(urlMon.HasSymbol(wxT("CoInternetGetSession")))
     {
-        typedef HRESULT (WINAPI *CoInternetGetSession_t)(DWORD, IInternetSession**, DWORD);
+        typedef HRESULT (WINAPI *CoInternetGetSession_t)(DWORD, wxIInternetSession**, DWORD);
         wxDYNLIB_FUNCTION(CoInternetGetSession_t, CoInternetGetSession, urlMon);
 
         ClassFactory* cf = new ClassFactory(handler);
-        IInternetSession* session;
+        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(), 0, NULL, 0);
+        HRESULT hr = session->RegisterNameSpace(cf, CLSID_FileProtocol,
+                                                handler->GetName().wc_str(),
+                                                0, NULL, 0);
         if(FAILED(hr))
         {
             wxFAIL_MSG("Could not register protocol");
@@ -742,8 +799,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())
             {
@@ -792,12 +858,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
@@ -985,53 +1051,20 @@ void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
 
 VirtualProtocol::VirtualProtocol(wxSharedPtr<wxWebViewHandler> handler)
 {
-    m_refCount = 0;
     m_file = NULL;
     m_handler = handler;
 }
 
-VirtualProtocol::~VirtualProtocol()
-{
-}
+BEGIN_IID_TABLE(VirtualProtocol)
+    ADD_IID(Unknown)
+    ADD_RAW_IID(wxIID_IInternetProtocolRoot)
+    ADD_RAW_IID(wxIID_IInternetProtocol)
+END_IID_TABLE;
 
-ULONG VirtualProtocol::AddRef()
-{
-    m_refCount++;
-    return m_refCount;
-}
+IMPLEMENT_IUNKNOWN_METHODS(VirtualProtocol)
 
-HRESULT VirtualProtocol::QueryInterface(REFIID riid, void **ppvObject)
-{
-    if(riid == IID_IUnknown || riid == IID_IInternetProtocolRoot || 
-       riid == IID_IInternetProtocol)
-    {
-        *ppvObject = (IInternetProtocol*)this;
-        AddRef();
-        return S_OK;
-    }
-    else
-    {
-        *ppvObject = NULL;
-        return E_POINTER;
-    }
-}
-
-ULONG VirtualProtocol::Release()
-{
-    m_refCount--;
-    if (m_refCount > 0)
-    {
-        return m_refCount;
-    }
-    else
-    {
-        delete this;
-        return 0;
-    }
-}
-
-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);
@@ -1039,7 +1072,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);
 
@@ -1050,17 +1083,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();
@@ -1096,10 +1129,17 @@ HRESULT VirtualProtocol::Read(void *pv, ULONG cb, ULONG *pcbRead)
     }
 }
 
+BEGIN_IID_TABLE(ClassFactory)
+    ADD_IID(Unknown)
+    ADD_IID(ClassFactory)
+END_IID_TABLE;
+
+IMPLEMENT_IUNKNOWN_METHODS(ClassFactory)
+
 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();
@@ -1107,7 +1147,7 @@ HRESULT ClassFactory::CreateInstance(IUnknown* pUnkOuter, REFIID riid,
     vp->Release();
     return hr;
 
-} 
+}
 
 STDMETHODIMP ClassFactory::LockServer(BOOL fLock)
 {
@@ -1115,41 +1155,4 @@ STDMETHODIMP ClassFactory::LockServer(BOOL fLock)
     return S_OK;
 }
 
-ULONG ClassFactory::AddRef(void)
-{
-    m_refCount++;
-    return m_refCount;
-}
-
-HRESULT ClassFactory::QueryInterface(REFIID riid, void **ppvObject)
-{
-    if ((riid == IID_IUnknown) || (riid == IID_IClassFactory))
-    {
-        *ppvObject = this;
-        AddRef();
-        return S_OK;
-    }
-    else
-    {
-        *ppvObject = NULL;
-        return E_POINTER;
-    }
-
-}
-
-ULONG ClassFactory::Release(void)
-{
-    m_refCount--;
-    if (m_refCount > 0)
-    {
-        return m_refCount;
-    }
-    else
-    {
-        delete this;
-        return 0;
-    }
-
-} 
-
 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE