1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/webviewie.cpp
3 // Purpose: wxMSW wxWebViewIE class implementation for web view component
4 // Author: Marianne Gagnon
6 // Copyright: (c) 2010 Marianne Gagnon
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if defined(__BORLANDC__)
17 #include "wx/msw/webviewie.h"
19 #if wxHAVE_WEB_BACKEND_IE
28 // FIXME: Seems like MINGW does not have these, how to handle cleanly?
29 #define DISPID_COMMANDSTATECHANGE 105
30 typedef enum CommandStateChangeConstants
{
31 CSC_UPDATECOMMANDS
= (int) 0xFFFFFFFF,
32 CSC_NAVIGATEFORWARD
= 0x1,
33 CSC_NAVIGATEBACK
= 0x2
34 } CommandStateChangeConstants
;
37 // FIXME: Seems like MINGW does not have these, how to handle cleanly?
38 #define DISPID_NAVIGATECOMPLETE2 252
39 #define DISPID_NAVIGATEERROR 271
40 #define OLECMDID_OPTICAL_ZOOM 63
41 #define INET_E_ERROR_FIRST 0x800C0002L
42 #define INET_E_INVALID_URL 0x800C0002L
43 #define INET_E_NO_SESSION 0x800C0003L
44 #define INET_E_CANNOT_CONNECT 0x800C0004L
45 #define INET_E_RESOURCE_NOT_FOUND 0x800C0005L
46 #define INET_E_OBJECT_NOT_FOUND 0x800C0006L
47 #define INET_E_DATA_NOT_AVAILABLE 0x800C0007L
48 #define INET_E_DOWNLOAD_FAILURE 0x800C0008L
49 #define INET_E_AUTHENTICATION_REQUIRED 0x800C0009L
50 #define INET_E_NO_VALID_MEDIA 0x800C000AL
51 #define INET_E_CONNECTION_TIMEOUT 0x800C000BL
52 #define INET_E_INVALID_REQUEST 0x800C000CL
53 #define INET_E_UNKNOWN_PROTOCOL 0x800C000DL
54 #define INET_E_SECURITY_PROBLEM 0x800C000EL
55 #define INET_E_CANNOT_LOAD_DATA 0x800C000FL
56 #define INET_E_CANNOT_INSTANTIATE_OBJECT 0x800C0010L
57 #define INET_E_QUERYOPTION_UNKNOWN 0x800C0013L
58 #define INET_E_REDIRECT_FAILED 0x800C0014L
59 #define INET_E_REDIRECT_TO_DIR 0x800C0015L
60 #define INET_E_CANNOT_LOCK_REQUEST 0x800C0016L
61 #define INET_E_USE_EXTEND_BINDING 0x800C0017L
62 #define INET_E_TERMINATED_BIND 0x800C0018L
63 #define INET_E_INVALID_CERTIFICATE 0x800C0019L
64 #define INET_E_CODE_DOWNLOAD_DECLINED 0x800C0100L
65 #define INET_E_RESULT_DISPATCHED 0x800C0200L
66 #define INET_E_CANNOT_REPLACE_SFP_FILE 0x800C0300L
67 #define INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY 0x800C0500L
68 #define INET_E_CODE_INSTALL_SUPPRESSED 0x800C0400L
70 #define REFRESH_COMPLETELY 3
73 BEGIN_EVENT_TABLE(wxWebViewIE
, wxControl
)
74 EVT_ACTIVEX(wxID_ANY
, wxWebViewIE::onActiveXEvent
)
75 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg
)
78 bool wxWebViewIE::Create(wxWindow
* parent
,
86 if (!wxControl::Create(parent
, id
, pos
, size
, style
,
87 wxDefaultValidator
, name
))
93 m_canNavigateBack
= false;
94 m_canNavigateForward
= false;
97 if (::CoCreateInstance(CLSID_WebBrowser
, NULL
,
98 CLSCTX_INPROC_SERVER
, // CLSCTX_INPROC,
99 IID_IWebBrowser2
, (void**)&m_webBrowser
) != 0)
101 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
105 m_ie
.SetDispatchPtr(m_webBrowser
); // wxAutomationObject will release itself
107 m_webBrowser
->put_RegisterAsBrowser(VARIANT_TRUE
);
108 m_webBrowser
->put_RegisterAsDropTarget(VARIANT_TRUE
);
109 //m_webBrowser->put_Silent(VARIANT_FALSE);
111 m_container
= new wxActiveXContainer(this, IID_IWebBrowser2
, m_webBrowser
);
113 SetBackgroundStyle(wxBG_STYLE_PAINT
);
114 SetDoubleBuffered(true);
119 void wxWebViewIE::LoadUrl(const wxString
& url
)
121 wxVariant out
= m_ie
.CallMethod("Navigate", (BSTR
) url
.wc_str(),
122 NULL
, NULL
, NULL
, NULL
);
124 // FIXME: why is out value null??
125 //(HRESULT)(out.GetLong()) == S_OK;
128 void wxWebViewIE::SetPage(const wxString
& html
, const wxString
& baseUrl
)
130 LoadUrl("about:blank");
132 // Let the wx events generated for navigation events be processed, so
133 // that the underlying IE component completes its Document object.
134 // FIXME: calling wxYield is not elegant nor very reliable probably
137 wxVariant documentVariant
= m_ie
.GetProperty("Document");
138 void* documentPtr
= documentVariant
.GetVoidPtr();
140 wxASSERT (documentPtr
!= NULL
);
142 // TODO: consider the "baseUrl" parameter if possible
143 // TODO: consider encoding
144 BSTR bstr
= SysAllocString(html
.wc_str());
146 // Creates a new one-dimensional array
147 SAFEARRAY
*psaStrings
= SafeArrayCreateVector(VT_VARIANT
, 0, 1);
148 if (psaStrings
!= NULL
)
151 HRESULT hr
= SafeArrayAccessData(psaStrings
, (LPVOID
*)¶m
);
153 param
->bstrVal
= bstr
;
155 hr
= SafeArrayUnaccessData(psaStrings
);
157 IHTMLDocument2
* document
= (IHTMLDocument2
*)documentPtr
;
158 document
->write(psaStrings
);
160 // SafeArrayDestroy calls SysFreeString for each BSTR
161 SafeArrayDestroy(psaStrings
);
165 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
170 wxString
wxWebViewIE::GetPageSource()
172 wxVariant documentVariant
= m_ie
.GetProperty("Document");
173 void* documentPtr
= documentVariant
.GetVoidPtr();
175 if (documentPtr
== NULL
)
177 return wxEmptyString
;
180 IHTMLDocument2
* document
= (IHTMLDocument2
*)documentPtr
;
182 IHTMLElement
*bodyTag
= NULL
;
183 IHTMLElement
*htmlTag
= NULL
;
184 document
->get_body(&bodyTag
);
185 wxASSERT(bodyTag
!= NULL
);
188 bodyTag
->get_parentElement(&htmlTag
);
189 wxASSERT(htmlTag
!= NULL
);
192 htmlTag
->get_outerHTML(&bstr
);
197 //wxMessageBox(wxString(bstr));
199 // TODO: check encoding
200 return wxString(bstr
);
203 // FIXME? retrieve OLECMDID_GETZOOMRANGE instead of hardcoding range 0-4
204 wxWebViewZoom
wxWebViewIE::GetZoom()
206 const int zoom
= GetIETextZoom();
211 return wxWEB_VIEW_ZOOM_TINY
;
214 return wxWEB_VIEW_ZOOM_SMALL
;
217 return wxWEB_VIEW_ZOOM_MEDIUM
;
220 return wxWEB_VIEW_ZOOM_LARGE
;
223 return wxWEB_VIEW_ZOOM_LARGEST
;
227 return wxWEB_VIEW_ZOOM_MEDIUM
;
230 void wxWebViewIE::SetZoom(wxWebViewZoom zoom
)
232 // I know I could cast from enum to int since wxWebViewZoom happens to
233 // match with IE's zoom levels, but I don't like doing that, what if enum
237 case wxWEB_VIEW_ZOOM_TINY
:
240 case wxWEB_VIEW_ZOOM_SMALL
:
243 case wxWEB_VIEW_ZOOM_MEDIUM
:
246 case wxWEB_VIEW_ZOOM_LARGE
:
249 case wxWEB_VIEW_ZOOM_LARGEST
:
257 void wxWebViewIE::SetIETextZoom(int level
)
260 VariantInit (&zoomVariant
);
261 V_VT(&zoomVariant
) = VT_I4
;
262 V_I4(&zoomVariant
) = level
;
264 HRESULT result
= m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
265 OLECMDEXECOPT_DONTPROMPTUSER
,
267 wxASSERT (result
== S_OK
);
269 VariantClear (&zoomVariant
);
272 int wxWebViewIE::GetIETextZoom()
275 VariantInit (&zoomVariant
);
276 V_VT(&zoomVariant
) = VT_I4
;
277 V_I4(&zoomVariant
) = 4;
279 HRESULT result
= m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
280 OLECMDEXECOPT_DONTPROMPTUSER
,
282 wxASSERT (result
== S_OK
);
284 int zoom
= V_I4(&zoomVariant
);
285 // wxMessageBox(wxString::Format("Zoom : %i", zoom));
286 VariantClear (&zoomVariant
);
291 void wxWebViewIE::SetIEOpticalZoom(float zoom
)
293 // TODO: add support for optical zoom (IE7+ only)
295 // TODO: get range from OLECMDID_OPTICAL_GETZOOMRANGE instead of hardcoding?
296 wxASSERT(zoom
>= 10.0f
);
297 wxASSERT(zoom
<= 1000.0f
);
300 VariantInit (&zoomVariant
);
301 V_VT(&zoomVariant
) = VT_I4
;
302 V_I4(&zoomVariant
) = (zoom
* 100.0f
);
304 HRESULT result
= m_webBrowser
->ExecWB((OLECMDID
)OLECMDID_OPTICAL_ZOOM
,
305 OLECMDEXECOPT_DODEFAULT
,
308 wxASSERT (result
== S_OK
);
311 float wxWebViewIE::GetIEOpticalZoom()
313 // TODO: add support for optical zoom (IE7+ only)
316 VariantInit (&zoomVariant
);
317 V_VT(&zoomVariant
) = VT_I4
;
318 V_I4(&zoomVariant
) = -1;
320 HRESULT result
= m_webBrowser
->ExecWB((OLECMDID
)OLECMDID_OPTICAL_ZOOM
,
321 OLECMDEXECOPT_DODEFAULT
, NULL
,
323 wxASSERT (result
== S_OK
);
325 const int zoom
= V_I4(&zoomVariant
);
326 VariantClear (&zoomVariant
);
328 return zoom
/ 100.0f
;
331 void wxWebViewIE::SetZoomType(wxWebViewZoomType
)
333 // TODO: add support for optical zoom (IE7+ only)
337 wxWebViewZoomType
wxWebViewIE::GetZoomType() const
339 // TODO: add support for optical zoom (IE7+ only)
340 return wxWEB_VIEW_ZOOM_TYPE_TEXT
;
343 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType
) const
345 // both are supported
346 // TODO: IE6 only supports text zoom, check if it's IE6 first
350 void wxWebViewIE::Print()
352 m_webBrowser
->ExecWB(OLECMDID_PRINTPREVIEW
,
353 OLECMDEXECOPT_DODEFAULT
, NULL
, NULL
);
356 void wxWebViewIE::GoBack()
358 wxVariant out
= m_ie
.CallMethod("GoBack");
360 // FIXME: why is out value null??
361 //return (HRESULT)(out.GetLong()) == S_OK;
364 void wxWebViewIE::GoForward()
366 wxVariant out
= m_ie
.CallMethod("GoForward");
368 // FIXME: why is out value null??
369 //return (HRESULT)(out.GetLong()) == S_OK;
372 void wxWebViewIE::Stop()
374 wxVariant out
= m_ie
.CallMethod("Stop");
376 // FIXME: why is out value null??
377 //return (HRESULT)(out.GetLong()) == S_OK;
381 void wxWebViewIE::Reload(wxWebViewReloadFlags flags
)
385 if (flags
& wxWEB_VIEW_RELOAD_NO_CACHE
)
390 out
= m_ie
.CallMethod("Refresh2", &level
);
394 out
= m_ie
.CallMethod("Refresh");
397 if (out
.GetType() != "null")
399 wxMessageBox("Non-null return message : " + out
.GetType());
403 bool wxWebViewIE::IsOfflineMode()
405 wxVariant out
= m_ie
.GetProperty("Offline");
407 wxASSERT(out
.GetType() == "bool");
409 return out
.GetBool();
412 void wxWebViewIE::SetOfflineMode(bool offline
)
414 // FIXME: the wxWidgets docs do not really document what the return
415 // parameter of PutProperty is
416 const bool success
= m_ie
.PutProperty("Offline", (offline
?
422 bool wxWebViewIE::IsBusy()
424 if (m_isBusy
) return true;
426 wxVariant out
= m_ie
.GetProperty("Busy");
428 wxASSERT(out
.GetType() == "bool");
430 return out
.GetBool();
433 wxString
wxWebViewIE::GetCurrentURL()
435 wxVariant out
= m_ie
.GetProperty("LocationURL");
437 wxASSERT(out
.GetType() == "string");
438 return out
.GetString();
441 wxString
wxWebViewIE::GetCurrentTitle()
443 wxVariant out
= m_ie
.GetProperty("LocationName");
445 wxASSERT(out
.GetType() == "string");
446 return out
.GetString();
449 void wxWebViewIE::onActiveXEvent(wxActiveXEvent
& evt
)
451 if (m_webBrowser
== NULL
) return;
453 switch (evt
.GetDispatchId())
455 case DISPID_BEFORENAVIGATE2
:
459 wxString url
= evt
[1].GetString();
460 wxString target
= evt
[3].GetString();
462 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
463 GetId(), url
, target
, true);
464 event
.SetEventObject(this);
465 HandleWindowEvent(event
);
467 if (event
.IsVetoed())
469 wxActiveXEventNativeMSW
* nativeParams
=
470 evt
.GetNativeParameters();
471 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[0]) = VARIANT_TRUE
;
474 // at this point, either the navigation event has been cancelled
475 // and we're not busy, either it was accepted and IWebBrowser2's
476 // Busy property will be true; so we don't need our override
483 case DISPID_NAVIGATECOMPLETE2
:
485 wxString url
= evt
[1].GetString();
486 // TODO: set target parameter if possible
487 wxString target
= wxEmptyString
;
488 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
489 GetId(), url
, target
, false);
490 event
.SetEventObject(this);
491 HandleWindowEvent(event
);
495 case DISPID_PROGRESSCHANGE
:
501 case DISPID_DOCUMENTCOMPLETE
:
503 wxString url
= evt
[1].GetString();
504 // TODO: set target parameter if possible
505 wxString target
= wxEmptyString
;
506 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_LOADED
, GetId(),
508 event
.SetEventObject(this);
509 HandleWindowEvent(event
);
513 case DISPID_STATUSTEXTCHANGE
:
518 case DISPID_TITLECHANGE
:
523 case DISPID_NAVIGATEERROR
:
525 wxWebNavigationError errorType
= wxWEB_NAV_ERR_OTHER
;
526 wxString errorCode
= "?";
527 switch (evt
[3].GetLong())
529 case INET_E_INVALID_URL
: // (0x800C0002L or -2146697214)
530 errorCode
= "INET_E_INVALID_URL";
531 errorType
= wxWEB_NAV_ERR_REQUEST
;
533 case INET_E_NO_SESSION
: // (0x800C0003L or -2146697213)
534 errorCode
= "INET_E_NO_SESSION";
535 errorType
= wxWEB_NAV_ERR_CONNECTION
;
537 case INET_E_CANNOT_CONNECT
: // (0x800C0004L or -2146697212)
538 errorCode
= "INET_E_CANNOT_CONNECT";
539 errorType
= wxWEB_NAV_ERR_CONNECTION
;
541 case INET_E_RESOURCE_NOT_FOUND
: // (0x800C0005L or -2146697211)
542 errorCode
= "INET_E_RESOURCE_NOT_FOUND";
543 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
545 case INET_E_OBJECT_NOT_FOUND
: // (0x800C0006L or -2146697210)
546 errorCode
= "INET_E_OBJECT_NOT_FOUND";
547 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
549 case INET_E_DATA_NOT_AVAILABLE
: // (0x800C0007L or -2146697209)
550 errorCode
= "INET_E_DATA_NOT_AVAILABLE";
551 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
553 case INET_E_DOWNLOAD_FAILURE
: // (0x800C0008L or -2146697208)
554 errorCode
= "INET_E_DOWNLOAD_FAILURE";
555 errorType
= wxWEB_NAV_ERR_CONNECTION
;
557 case INET_E_AUTHENTICATION_REQUIRED
: // (0x800C0009L or -2146697207)
558 errorCode
= "INET_E_AUTHENTICATION_REQUIRED";
559 errorType
= wxWEB_NAV_ERR_AUTH
;
561 case INET_E_NO_VALID_MEDIA
: // (0x800C000AL or -2146697206)
562 errorCode
= "INET_E_NO_VALID_MEDIA";
563 errorType
= wxWEB_NAV_ERR_REQUEST
;
565 case INET_E_CONNECTION_TIMEOUT
: // (0x800C000BL or -2146697205)
566 errorCode
= "INET_E_CONNECTION_TIMEOUT";
567 errorType
= wxWEB_NAV_ERR_CONNECTION
;
569 case INET_E_INVALID_REQUEST
: // (0x800C000CL or -2146697204)
570 errorCode
= "INET_E_INVALID_REQUEST";
571 errorType
= wxWEB_NAV_ERR_REQUEST
;
573 case INET_E_UNKNOWN_PROTOCOL
: // (0x800C000DL or -2146697203)
574 errorCode
= "INET_E_UNKNOWN_PROTOCOL";
575 errorType
= wxWEB_NAV_ERR_REQUEST
;
577 case INET_E_SECURITY_PROBLEM
: // (0x800C000EL or -2146697202)
578 errorCode
= "INET_E_SECURITY_PROBLEM";
579 errorType
= wxWEB_NAV_ERR_SECURITY
;
581 case INET_E_CANNOT_LOAD_DATA
: // (0x800C000FL or -2146697201)
582 errorCode
= "INET_E_CANNOT_LOAD_DATA";
583 errorType
= wxWEB_NAV_ERR_OTHER
;
585 case INET_E_CANNOT_INSTANTIATE_OBJECT
:
586 // CoCreateInstance will return an error code if this happens,
587 // we'll handle this above.
590 case INET_E_REDIRECT_FAILED
: // (0x800C0014L or -2146697196)
591 errorCode
= "INET_E_REDIRECT_FAILED";
592 errorType
= wxWEB_NAV_ERR_OTHER
;
594 case INET_E_REDIRECT_TO_DIR
: // (0x800C0015L or -2146697195)
595 errorCode
= "INET_E_REDIRECT_TO_DIR";
596 errorType
= wxWEB_NAV_ERR_REQUEST
;
598 case INET_E_CANNOT_LOCK_REQUEST
: // (0x800C0016L or -2146697194)
599 errorCode
= "INET_E_CANNOT_LOCK_REQUEST";
600 errorType
= wxWEB_NAV_ERR_OTHER
;
602 case INET_E_USE_EXTEND_BINDING
: // (0x800C0017L or -2146697193)
603 errorCode
= "INET_E_USE_EXTEND_BINDING";
604 errorType
= wxWEB_NAV_ERR_OTHER
;
606 case INET_E_TERMINATED_BIND
: // (0x800C0018L or -2146697192)
607 errorCode
= "INET_E_TERMINATED_BIND";
608 errorType
= wxWEB_NAV_ERR_OTHER
;
610 case INET_E_INVALID_CERTIFICATE
: // (0x800C0019L or -2146697191)
611 errorCode
= "INET_E_INVALID_CERTIFICATE";
612 errorType
= wxWEB_NAV_ERR_CERTIFICATE
;
614 case INET_E_CODE_DOWNLOAD_DECLINED
: // (0x800C0100L or -2146696960)
615 errorCode
= "INET_E_CODE_DOWNLOAD_DECLINED";
616 errorType
= wxWEB_NAV_ERR_USER_CANCELLED
;
618 case INET_E_RESULT_DISPATCHED
: // (0x800C0200L or -2146696704)
619 // cancel request cancelled...
620 errorCode
= "INET_E_RESULT_DISPATCHED";
621 errorType
= wxWEB_NAV_ERR_OTHER
;
623 case INET_E_CANNOT_REPLACE_SFP_FILE
: // (0x800C0300L or -2146696448)
624 errorCode
= "INET_E_CANNOT_REPLACE_SFP_FILE";
625 errorType
= wxWEB_NAV_ERR_SECURITY
;
627 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY
:
628 errorCode
= "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
629 errorType
= wxWEB_NAV_ERR_SECURITY
;
631 case INET_E_CODE_INSTALL_SUPPRESSED
:
632 errorCode
= "INET_E_CODE_INSTALL_SUPPRESSED";
633 errorType
= wxWEB_NAV_ERR_SECURITY
;
637 wxString url
= evt
[1].GetString();
638 wxString target
= evt
[2].GetString();
639 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_ERROR
, GetId(),
641 event
.SetEventObject(this);
642 event
.SetInt(errorType
);
643 event
.SetString(errorCode
);
644 HandleWindowEvent(event
);
648 case DISPID_COMMANDSTATECHANGE
:
650 long commandId
= evt
[0].GetLong();
651 bool enable
= evt
[1].GetBool();
652 if (commandId
== CSC_NAVIGATEBACK
)
654 m_canNavigateBack
= enable
;
656 else if (commandId
== CSC_NAVIGATEFORWARD
)
658 m_canNavigateForward
= enable
;
663 case DISPID_NEWWINDOW2:
664 //case DISPID_NEWWINDOW3:
666 wxLogMessage("DISPID_NEWWINDOW2\n");
667 wxActiveXEventNativeMSW* nativeParams = evt.GetNativeParameters();
668 // Cancel the attempt to open a new window
669 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[0]) = VARIANT_TRUE;