Add RunScript and implement on all backends. Document and add a very simple unit...
[wxWidgets.git] / src / msw / webview_ie.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/webview_ie.cpp
3 // Purpose: wxMSW wxWebViewIE class implementation for web view component
4 // Author: Marianne Gagnon
5 // Id: $Id$
6 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if defined(__BORLANDC__)
14 #pragma hdrstop
15 #endif
16
17 #include "wx/msw/webview_ie.h"
18
19
20 #if wxUSE_WEBVIEW_IE
21
22 #include <olectl.h>
23 #include <oleidl.h>
24 #include <exdispid.h>
25 #include <exdisp.h>
26 #include <mshtml.h>
27 #include "wx/msw/registry.h"
28 // Various definitions are missing from mingw
29 #ifdef __MINGW32__
30 typedef enum CommandStateChangeConstants {
31 CSC_UPDATECOMMANDS = (int) 0xFFFFFFFF,
32 CSC_NAVIGATEFORWARD = 0x1,
33 CSC_NAVIGATEBACK = 0x2
34 } CommandStateChangeConstants;
35
36 #define DISPID_COMMANDSTATECHANGE 105
37 #define DISPID_NAVIGATECOMPLETE2 252
38 #define DISPID_NAVIGATEERROR 271
39 #define DISPID_NEWWINDOW3 273
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
69
70 #define REFRESH_NORMAL 0
71 #define REFRESH_COMPLETELY 3
72 #endif
73
74 BEGIN_EVENT_TABLE(wxWebViewIE, wxControl)
75 EVT_ACTIVEX(wxID_ANY, wxWebViewIE::onActiveXEvent)
76 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg)
77 END_EVENT_TABLE()
78
79 bool wxWebViewIE::Create(wxWindow* parent,
80 wxWindowID id,
81 const wxString& url,
82 const wxPoint& pos,
83 const wxSize& size,
84 long style,
85 const wxString& name)
86 {
87 if (!wxControl::Create(parent, id, pos, size, style,
88 wxDefaultValidator, name))
89 {
90 return false;
91 }
92
93 m_webBrowser = NULL;
94 m_canNavigateBack = false;
95 m_canNavigateForward = false;
96 m_isBusy = false;
97 m_historyLoadingFromList = false;
98 m_historyEnabled = true;
99 m_historyPosition = -1;
100 m_zoomType = wxWEB_VIEW_ZOOM_TYPE_TEXT;
101
102 if (::CoCreateInstance(CLSID_WebBrowser, NULL,
103 CLSCTX_INPROC_SERVER, // CLSCTX_INPROC,
104 IID_IWebBrowser2 , (void**)&m_webBrowser) != 0)
105 {
106 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
107 return false;
108 }
109
110 m_ie.SetDispatchPtr(m_webBrowser); // wxAutomationObject will release itself
111
112 m_webBrowser->put_RegisterAsBrowser(VARIANT_TRUE);
113 m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
114 //m_webBrowser->put_Silent(VARIANT_FALSE);
115
116 m_container = new wxActiveXContainer(this, IID_IWebBrowser2, m_webBrowser);
117
118 SetBackgroundStyle(wxBG_STYLE_PAINT);
119 SetDoubleBuffered(true);
120 LoadUrl(url);
121 return true;
122 }
123
124
125 void wxWebViewIE::LoadUrl(const wxString& url)
126 {
127 m_ie.CallMethod("Navigate", (BSTR) url.wc_str(), NULL, NULL, NULL, NULL);
128 }
129
130 void wxWebViewIE::SetPage(const wxString& html, const wxString&)
131 {
132 LoadUrl("about:blank");
133
134 // Let the wx events generated for navigation events be processed, so
135 // that the underlying IE component completes its Document object.
136 // FIXME: calling wxYield is not elegant nor very reliable probably
137 wxYield();
138
139 // TODO: consider the "baseUrl" parameter if possible
140 // TODO: consider encoding
141 BSTR bstr = SysAllocString(html.wc_str());
142
143 // Creates a new one-dimensional array
144 SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
145 if (psaStrings != NULL)
146 {
147 VARIANT *param;
148 HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
149 param->vt = VT_BSTR;
150 param->bstrVal = bstr;
151
152 hr = SafeArrayUnaccessData(psaStrings);
153 IHTMLDocument2* document = GetDocument();
154 document->write(psaStrings);
155
156 // SafeArrayDestroy calls SysFreeString for each BSTR
157 SafeArrayDestroy(psaStrings);
158 }
159 else
160 {
161 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
162 }
163
164 }
165
166 wxString wxWebViewIE::GetPageSource()
167 {
168 IHTMLDocument2* document = GetDocument();
169 IHTMLElement *bodyTag = NULL;
170 IHTMLElement *htmlTag = NULL;
171 wxString source;
172 HRESULT hr = document->get_body(&bodyTag);
173 if(SUCCEEDED(hr))
174 {
175 hr = bodyTag->get_parentElement(&htmlTag);
176 if(SUCCEEDED(hr))
177 {
178 BSTR bstr;
179 htmlTag->get_outerHTML(&bstr);
180 source = wxString(bstr);
181 htmlTag->Release();
182 }
183 bodyTag->Release();
184 }
185
186 document->Release();
187 return source;
188 }
189
190 wxWebViewZoom wxWebViewIE::GetZoom()
191 {
192 if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
193 return GetIEOpticalZoom();
194 else if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT)
195 return GetIETextZoom();
196 else
197 wxFAIL;
198
199 //Dummy return to stop compiler warnings
200 return wxWEB_VIEW_ZOOM_MEDIUM;
201
202 }
203
204 void wxWebViewIE::SetZoom(wxWebViewZoom zoom)
205 {
206 if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
207 SetIEOpticalZoom(zoom);
208 else if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT)
209 SetIETextZoom(zoom);
210 else
211 wxFAIL;
212 }
213
214 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level)
215 {
216 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
217 //is 0 to 4 so the check is unnecessary, these match exactly with the
218 //enum values
219 VARIANT zoomVariant;
220 VariantInit (&zoomVariant);
221 V_VT(&zoomVariant) = VT_I4;
222 V_I4(&zoomVariant) = level;
223
224 HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM,
225 OLECMDEXECOPT_DONTPROMPTUSER,
226 &zoomVariant, NULL);
227 wxASSERT(result == S_OK);
228 }
229
230 wxWebViewZoom wxWebViewIE::GetIETextZoom()
231 {
232 VARIANT zoomVariant;
233 VariantInit (&zoomVariant);
234 V_VT(&zoomVariant) = VT_I4;
235
236 HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM,
237 OLECMDEXECOPT_DONTPROMPTUSER,
238 NULL, &zoomVariant);
239 wxASSERT(result == S_OK);
240
241 //We can safely cast here as we know that the range matches our enum
242 return static_cast<wxWebViewZoom>(V_I4(&zoomVariant));
243 }
244
245 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level)
246 {
247 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
248 //is 10 to 1000 so the check is unnecessary
249 VARIANT zoomVariant;
250 VariantInit (&zoomVariant);
251 V_VT(&zoomVariant) = VT_I4;
252
253 //We make a somewhat arbitray map here, taken from values used by webkit
254 switch(level)
255 {
256 case wxWEB_VIEW_ZOOM_TINY:
257 V_I4(&zoomVariant) = 60;
258 break;
259 case wxWEB_VIEW_ZOOM_SMALL:
260 V_I4(&zoomVariant) = 80;
261 break;
262 case wxWEB_VIEW_ZOOM_MEDIUM:
263 V_I4(&zoomVariant) = 100;
264 break;
265 case wxWEB_VIEW_ZOOM_LARGE:
266 V_I4(&zoomVariant) = 130;
267 break;
268 case wxWEB_VIEW_ZOOM_LARGEST:
269 V_I4(&zoomVariant) = 160;
270 break;
271 default:
272 wxFAIL;
273 }
274
275 HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM,
276 OLECMDEXECOPT_DODEFAULT,
277 &zoomVariant,
278 NULL);
279 wxASSERT(result == S_OK);
280 }
281
282 wxWebViewZoom wxWebViewIE::GetIEOpticalZoom()
283 {
284 VARIANT zoomVariant;
285 VariantInit (&zoomVariant);
286 V_VT(&zoomVariant) = VT_I4;
287
288 HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM,
289 OLECMDEXECOPT_DODEFAULT, NULL,
290 &zoomVariant);
291 wxASSERT(result == S_OK);
292
293 const int zoom = V_I4(&zoomVariant);
294
295 //We make a somewhat arbitray map here, taken from values used by webkit
296 if (zoom <= 65)
297 {
298 return wxWEB_VIEW_ZOOM_TINY;
299 }
300 else if (zoom > 65 && zoom <= 90)
301 {
302 return wxWEB_VIEW_ZOOM_SMALL;
303 }
304 else if (zoom > 90 && zoom <= 115)
305 {
306 return wxWEB_VIEW_ZOOM_MEDIUM;
307 }
308 else if (zoom > 115 && zoom <= 145)
309 {
310 return wxWEB_VIEW_ZOOM_LARGE;
311 }
312 else /*if (zoom > 145) */ //Using else removes a compiler warning
313 {
314 return wxWEB_VIEW_ZOOM_LARGEST;
315 }
316 }
317
318 void wxWebViewIE::SetZoomType(wxWebViewZoomType type)
319 {
320 m_zoomType = type;
321 }
322
323 wxWebViewZoomType wxWebViewIE::GetZoomType() const
324 {
325 return m_zoomType;
326 }
327
328 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type) const
329 {
330 //IE 6 and below only support text zoom, so check the registry to see what
331 //version we actually have
332 wxRegKey key(wxRegKey::HKLM, "Software\\Microsoft\\Internet Explorer");
333 wxString value;
334 key.QueryValue("Version", value);
335
336 long version = wxAtoi(value.Left(1));
337 if(version <= 6 && type == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
338 return false;
339 else
340 return true;
341 }
342
343 void wxWebViewIE::Print()
344 {
345 m_webBrowser->ExecWB(OLECMDID_PRINTPREVIEW,
346 OLECMDEXECOPT_DODEFAULT, NULL, NULL);
347 }
348
349 bool wxWebViewIE::CanGoBack()
350 {
351 if(m_historyEnabled)
352 return m_historyPosition > 0;
353 else
354 return false;
355 }
356
357 bool wxWebViewIE::CanGoForward()
358 {
359 if(m_historyEnabled)
360 return m_historyPosition != static_cast<int>(m_historyList.size()) - 1;
361 else
362 return false;
363 }
364
365 void wxWebViewIE::LoadHistoryItem(wxSharedPtr<wxWebHistoryItem> item)
366 {
367 int pos = -1;
368 for(unsigned int i = 0; i < m_historyList.size(); i++)
369 {
370 //We compare the actual pointers to find the correct item
371 if(m_historyList[i].get() == item.get())
372 pos = i;
373 }
374 wxASSERT_MSG(pos != static_cast<int>(m_historyList.size()),
375 "invalid history item");
376 m_historyLoadingFromList = true;
377 LoadUrl(item->GetUrl());
378 m_historyPosition = pos;
379 }
380
381 wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewIE::GetBackwardHistory()
382 {
383 wxVector<wxSharedPtr<wxWebHistoryItem> > backhist;
384 //As we don't have std::copy or an iterator constructor in the wxwidgets
385 //native vector we construct it by hand
386 for(int i = 0; i < m_historyPosition; i++)
387 {
388 backhist.push_back(m_historyList[i]);
389 }
390 return backhist;
391 }
392
393 wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewIE::GetForwardHistory()
394 {
395 wxVector<wxSharedPtr<wxWebHistoryItem> > forwardhist;
396 //As we don't have std::copy or an iterator constructor in the wxwidgets
397 //native vector we construct it by hand
398 for(int i = m_historyPosition + 1; i < static_cast<int>(m_historyList.size()); i++)
399 {
400 forwardhist.push_back(m_historyList[i]);
401 }
402 return forwardhist;
403 }
404
405 void wxWebViewIE::GoBack()
406 {
407 LoadHistoryItem(m_historyList[m_historyPosition - 1]);
408 }
409
410 void wxWebViewIE::GoForward()
411 {
412 LoadHistoryItem(m_historyList[m_historyPosition + 1]);
413 }
414
415 void wxWebViewIE::Stop()
416 {
417 m_ie.CallMethod("Stop");
418 }
419
420 void wxWebViewIE::ClearHistory()
421 {
422 m_historyList.clear();
423 m_historyPosition = -1;
424 }
425
426 void wxWebViewIE::EnableHistory(bool enable)
427 {
428 m_historyEnabled = enable;
429 m_historyList.clear();
430 m_historyPosition = -1;
431 }
432
433 void wxWebViewIE::Reload(wxWebViewReloadFlags flags)
434 {
435 VARIANTARG level;
436 VariantInit(&level);
437 V_VT(&level) = VT_I2;
438
439 switch(flags)
440 {
441 case wxWEB_VIEW_RELOAD_DEFAULT:
442 V_I2(&level) = REFRESH_NORMAL;
443 break;
444 case wxWEB_VIEW_RELOAD_NO_CACHE:
445 V_I2(&level) = REFRESH_COMPLETELY;
446 break;
447 default:
448 wxFAIL_MSG("Unexpected reload type");
449 }
450
451 m_webBrowser->Refresh2(&level);
452 }
453
454 bool wxWebViewIE::IsOfflineMode()
455 {
456 wxVariant out = m_ie.GetProperty("Offline");
457
458 wxASSERT(out.GetType() == "bool");
459
460 return out.GetBool();
461 }
462
463 void wxWebViewIE::SetOfflineMode(bool offline)
464 {
465 // FIXME: the wxWidgets docs do not really document what the return
466 // parameter of PutProperty is
467 const bool success = m_ie.PutProperty("Offline", (offline ?
468 VARIANT_TRUE :
469 VARIANT_FALSE));
470 wxASSERT(success);
471 }
472
473 bool wxWebViewIE::IsBusy()
474 {
475 if (m_isBusy) return true;
476
477 wxVariant out = m_ie.GetProperty("Busy");
478
479 wxASSERT(out.GetType() == "bool");
480
481 return out.GetBool();
482 }
483
484 wxString wxWebViewIE::GetCurrentURL()
485 {
486 wxVariant out = m_ie.GetProperty("LocationURL");
487
488 wxASSERT(out.GetType() == "string");
489 return out.GetString();
490 }
491
492 wxString wxWebViewIE::GetCurrentTitle()
493 {
494 IHTMLDocument2* document = GetDocument();
495 BSTR title;
496
497 document->get_nameProp(&title);
498 document->Release();
499 return wxString(title);
500 }
501
502 bool wxWebViewIE::CanCut()
503 {
504 return CanExecCommand("Cut");
505 }
506
507 bool wxWebViewIE::CanCopy()
508 {
509 return CanExecCommand("Copy");
510 }
511 bool wxWebViewIE::CanPaste()
512 {
513 return CanExecCommand("Paste");
514 }
515
516 void wxWebViewIE::Cut()
517 {
518 ExecCommand("Cut");
519 }
520
521 void wxWebViewIE::Copy()
522 {
523 ExecCommand("Copy");
524 }
525
526 void wxWebViewIE::Paste()
527 {
528 ExecCommand("Paste");
529 }
530
531 bool wxWebViewIE::CanUndo()
532 {
533 return CanExecCommand("Undo");
534 }
535 bool wxWebViewIE::CanRedo()
536 {
537 return CanExecCommand("Redo");
538 }
539
540 void wxWebViewIE::Undo()
541 {
542 ExecCommand("Undo");
543 }
544
545 void wxWebViewIE::Redo()
546 {
547 ExecCommand("Redo");
548 }
549
550 void wxWebViewIE::SetEditable(bool enable)
551 {
552 IHTMLDocument2* document = GetDocument();
553 if( enable )
554 document->put_designMode(SysAllocString(L"On"));
555 else
556 document->put_designMode(SysAllocString(L"Off"));
557
558 document->Release();
559 }
560
561 bool wxWebViewIE::IsEditable()
562 {
563 IHTMLDocument2* document = GetDocument();
564 BSTR mode;
565 document->get_designMode(&mode);
566 document->Release();
567 if(wxString(mode) == "On")
568 return true;
569 else
570 return false;
571 }
572
573 void wxWebViewIE::SelectAll()
574 {
575 ExecCommand("SelectAll");
576 }
577
578 bool wxWebViewIE::HasSelection()
579 {
580 IHTMLDocument2* document = GetDocument();
581 IHTMLSelectionObject* selection;
582 wxString sel;
583 HRESULT hr = document->get_selection(&selection);
584 if(SUCCEEDED(hr))
585 {
586 BSTR type;
587 selection->get_type(&type);
588 sel = wxString(type);
589 selection->Release();
590 }
591 document->Release();
592 return sel != "None";
593 }
594
595 void wxWebViewIE::DeleteSelection()
596 {
597 ExecCommand("Delete");
598 }
599
600 wxString wxWebViewIE::GetSelectedText()
601 {
602 IHTMLDocument2* document = GetDocument();
603 IHTMLSelectionObject* selection;
604 wxString selected;
605 HRESULT hr = document->get_selection(&selection);
606 if(SUCCEEDED(hr))
607 {
608 IDispatch* disrange;
609 hr = selection->createRange(&disrange);
610 if(SUCCEEDED(hr))
611 {
612 IHTMLTxtRange* range;
613 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
614 if(SUCCEEDED(hr))
615 {
616 BSTR text;
617 range->get_text(&text);
618 selected = wxString(text);
619 range->Release();
620 }
621 disrange->Release();
622 }
623 selection->Release();
624 }
625 document->Release();
626 return selected;
627 }
628
629 wxString wxWebViewIE::GetSelectedSource()
630 {
631 IHTMLDocument2* document = GetDocument();
632 IHTMLSelectionObject* selection;
633 wxString selected;
634 HRESULT hr = document->get_selection(&selection);
635 if(SUCCEEDED(hr))
636 {
637 IDispatch* disrange;
638 hr = selection->createRange(&disrange);
639 if(SUCCEEDED(hr))
640 {
641 IHTMLTxtRange* range;
642 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
643 if(SUCCEEDED(hr))
644 {
645 BSTR text;
646 range->get_htmlText(&text);
647 selected = wxString(text);
648 range->Release();
649 }
650 disrange->Release();
651 }
652 selection->Release();
653 }
654 document->Release();
655 return selected;
656 }
657
658 void wxWebViewIE::ClearSelection()
659 {
660 IHTMLDocument2* document = GetDocument();
661 IHTMLSelectionObject* selection;
662 wxString selected;
663 HRESULT hr = document->get_selection(&selection);
664 if(SUCCEEDED(hr))
665 {
666 selection->empty();
667 selection->Release();
668 }
669 document->Release();
670 }
671
672 wxString wxWebViewIE::GetPageText()
673 {
674 IHTMLDocument2* document = GetDocument();
675 wxString text;
676 IHTMLElement* body;
677 HRESULT hr = document->get_body(&body);
678 if(SUCCEEDED(hr))
679 {
680 BSTR out;
681 body->get_innerText(&out);
682 text = wxString(out);
683 body->Release();
684 }
685 document->Release();
686 return text;
687 }
688
689 void wxWebViewIE::RunScript(const wxString& javascript)
690 {
691 IHTMLDocument2* document = GetDocument();
692 IHTMLWindow2* window;
693 wxString language = "javascript";
694 HRESULT hr = document->get_parentWindow(&window);
695 if(SUCCEEDED(hr))
696 {
697 VARIANT level;
698 VariantInit(&level);
699 V_VT(&level) = VT_EMPTY;
700 window->execScript(SysAllocString(javascript), SysAllocString(language), &level);
701 }
702 document->Release();
703 }
704
705 bool wxWebViewIE::CanExecCommand(wxString command)
706 {
707 IHTMLDocument2* document = GetDocument();
708 VARIANT_BOOL enabled;
709
710 document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled);
711 document->Release();
712
713 return (enabled == VARIANT_TRUE);
714 }
715
716 void wxWebViewIE::ExecCommand(wxString command)
717 {
718 IHTMLDocument2* document = GetDocument();
719 document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL);
720 document->Release();
721 }
722
723 IHTMLDocument2* wxWebViewIE::GetDocument()
724 {
725 wxVariant variant = m_ie.GetProperty("Document");
726 IHTMLDocument2* document = (IHTMLDocument2*)variant.GetVoidPtr();
727
728 wxASSERT(document);
729
730 return document;
731 }
732
733 void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
734 {
735 if (m_webBrowser == NULL) return;
736
737 switch (evt.GetDispatchId())
738 {
739 case DISPID_BEFORENAVIGATE2:
740 {
741 m_isBusy = true;
742
743 wxString url = evt[1].GetString();
744 wxString target = evt[3].GetString();
745
746 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
747 GetId(), url, target, true);
748 event.SetEventObject(this);
749 HandleWindowEvent(event);
750
751 if (event.IsVetoed())
752 {
753 wxActiveXEventNativeMSW* nativeParams =
754 evt.GetNativeParameters();
755 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[0]) = VARIANT_TRUE;
756 }
757
758 // at this point, either the navigation event has been cancelled
759 // and we're not busy, either it was accepted and IWebBrowser2's
760 // Busy property will be true; so we don't need our override
761 // flag anymore.
762 m_isBusy = false;
763
764 break;
765 }
766
767 case DISPID_NAVIGATECOMPLETE2:
768 {
769 wxString url = evt[1].GetString();
770 // TODO: set target parameter if possible
771 wxString target = wxEmptyString;
772 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
773 GetId(), url, target, false);
774 event.SetEventObject(this);
775 HandleWindowEvent(event);
776 break;
777 }
778
779 case DISPID_PROGRESSCHANGE:
780 {
781 // download progress
782 break;
783 }
784
785 case DISPID_DOCUMENTCOMPLETE:
786 {
787 //Only send a complete even if we are actually finished, this brings
788 //the event in to line with webkit
789 READYSTATE rs;
790 m_webBrowser->get_ReadyState( &rs );
791 if(rs != READYSTATE_COMPLETE)
792 break;
793
794 wxString url = evt[1].GetString();
795
796 //As we are complete we also add to the history list, but not if the
797 //page is not the main page, ie it is a subframe
798 if(m_historyEnabled && !m_historyLoadingFromList && url == GetCurrentURL())
799 {
800 //If we are not at the end of the list, then erase everything
801 //between us and the end before adding the new page
802 if(m_historyPosition != static_cast<int>(m_historyList.size()) - 1)
803 {
804 m_historyList.erase(m_historyList.begin() + m_historyPosition + 1,
805 m_historyList.end());
806 }
807 wxSharedPtr<wxWebHistoryItem> item(new wxWebHistoryItem(url, GetCurrentTitle()));
808 m_historyList.push_back(item);
809 m_historyPosition++;
810 }
811 //Reset as we are done now
812 m_historyLoadingFromList = false;
813 // TODO: set target parameter if possible
814 wxString target = wxEmptyString;
815 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED, GetId(),
816 url, target, false);
817 event.SetEventObject(this);
818 HandleWindowEvent(event);
819 break;
820 }
821
822 case DISPID_STATUSTEXTCHANGE:
823 {
824 break;
825 }
826
827 case DISPID_TITLECHANGE:
828 {
829 break;
830 }
831
832 case DISPID_NAVIGATEERROR:
833 {
834 wxWebNavigationError errorType = wxWEB_NAV_ERR_OTHER;
835 wxString errorCode = "?";
836 switch (evt[3].GetLong())
837 {
838 case INET_E_INVALID_URL: // (0x800C0002L or -2146697214)
839 errorCode = "INET_E_INVALID_URL";
840 errorType = wxWEB_NAV_ERR_REQUEST;
841 break;
842 case INET_E_NO_SESSION: // (0x800C0003L or -2146697213)
843 errorCode = "INET_E_NO_SESSION";
844 errorType = wxWEB_NAV_ERR_CONNECTION;
845 break;
846 case INET_E_CANNOT_CONNECT: // (0x800C0004L or -2146697212)
847 errorCode = "INET_E_CANNOT_CONNECT";
848 errorType = wxWEB_NAV_ERR_CONNECTION;
849 break;
850 case INET_E_RESOURCE_NOT_FOUND: // (0x800C0005L or -2146697211)
851 errorCode = "INET_E_RESOURCE_NOT_FOUND";
852 errorType = wxWEB_NAV_ERR_NOT_FOUND;
853 break;
854 case INET_E_OBJECT_NOT_FOUND: // (0x800C0006L or -2146697210)
855 errorCode = "INET_E_OBJECT_NOT_FOUND";
856 errorType = wxWEB_NAV_ERR_NOT_FOUND;
857 break;
858 case INET_E_DATA_NOT_AVAILABLE: // (0x800C0007L or -2146697209)
859 errorCode = "INET_E_DATA_NOT_AVAILABLE";
860 errorType = wxWEB_NAV_ERR_NOT_FOUND;
861 break;
862 case INET_E_DOWNLOAD_FAILURE: // (0x800C0008L or -2146697208)
863 errorCode = "INET_E_DOWNLOAD_FAILURE";
864 errorType = wxWEB_NAV_ERR_CONNECTION;
865 break;
866 case INET_E_AUTHENTICATION_REQUIRED: // (0x800C0009L or -2146697207)
867 errorCode = "INET_E_AUTHENTICATION_REQUIRED";
868 errorType = wxWEB_NAV_ERR_AUTH;
869 break;
870 case INET_E_NO_VALID_MEDIA: // (0x800C000AL or -2146697206)
871 errorCode = "INET_E_NO_VALID_MEDIA";
872 errorType = wxWEB_NAV_ERR_REQUEST;
873 break;
874 case INET_E_CONNECTION_TIMEOUT: // (0x800C000BL or -2146697205)
875 errorCode = "INET_E_CONNECTION_TIMEOUT";
876 errorType = wxWEB_NAV_ERR_CONNECTION;
877 break;
878 case INET_E_INVALID_REQUEST: // (0x800C000CL or -2146697204)
879 errorCode = "INET_E_INVALID_REQUEST";
880 errorType = wxWEB_NAV_ERR_REQUEST;
881 break;
882 case INET_E_UNKNOWN_PROTOCOL: // (0x800C000DL or -2146697203)
883 errorCode = "INET_E_UNKNOWN_PROTOCOL";
884 errorType = wxWEB_NAV_ERR_REQUEST;
885 break;
886 case INET_E_SECURITY_PROBLEM: // (0x800C000EL or -2146697202)
887 errorCode = "INET_E_SECURITY_PROBLEM";
888 errorType = wxWEB_NAV_ERR_SECURITY;
889 break;
890 case INET_E_CANNOT_LOAD_DATA: // (0x800C000FL or -2146697201)
891 errorCode = "INET_E_CANNOT_LOAD_DATA";
892 errorType = wxWEB_NAV_ERR_OTHER;
893 break;
894 case INET_E_CANNOT_INSTANTIATE_OBJECT:
895 // CoCreateInstance will return an error code if this happens,
896 // we'll handle this above.
897 return;
898 break;
899 case INET_E_REDIRECT_FAILED: // (0x800C0014L or -2146697196)
900 errorCode = "INET_E_REDIRECT_FAILED";
901 errorType = wxWEB_NAV_ERR_OTHER;
902 break;
903 case INET_E_REDIRECT_TO_DIR: // (0x800C0015L or -2146697195)
904 errorCode = "INET_E_REDIRECT_TO_DIR";
905 errorType = wxWEB_NAV_ERR_REQUEST;
906 break;
907 case INET_E_CANNOT_LOCK_REQUEST: // (0x800C0016L or -2146697194)
908 errorCode = "INET_E_CANNOT_LOCK_REQUEST";
909 errorType = wxWEB_NAV_ERR_OTHER;
910 break;
911 case INET_E_USE_EXTEND_BINDING: // (0x800C0017L or -2146697193)
912 errorCode = "INET_E_USE_EXTEND_BINDING";
913 errorType = wxWEB_NAV_ERR_OTHER;
914 break;
915 case INET_E_TERMINATED_BIND: // (0x800C0018L or -2146697192)
916 errorCode = "INET_E_TERMINATED_BIND";
917 errorType = wxWEB_NAV_ERR_OTHER;
918 break;
919 case INET_E_INVALID_CERTIFICATE: // (0x800C0019L or -2146697191)
920 errorCode = "INET_E_INVALID_CERTIFICATE";
921 errorType = wxWEB_NAV_ERR_CERTIFICATE;
922 break;
923 case INET_E_CODE_DOWNLOAD_DECLINED: // (0x800C0100L or -2146696960)
924 errorCode = "INET_E_CODE_DOWNLOAD_DECLINED";
925 errorType = wxWEB_NAV_ERR_USER_CANCELLED;
926 break;
927 case INET_E_RESULT_DISPATCHED: // (0x800C0200L or -2146696704)
928 // cancel request cancelled...
929 errorCode = "INET_E_RESULT_DISPATCHED";
930 errorType = wxWEB_NAV_ERR_OTHER;
931 break;
932 case INET_E_CANNOT_REPLACE_SFP_FILE: // (0x800C0300L or -2146696448)
933 errorCode = "INET_E_CANNOT_REPLACE_SFP_FILE";
934 errorType = wxWEB_NAV_ERR_SECURITY;
935 break;
936 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY:
937 errorCode = "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
938 errorType = wxWEB_NAV_ERR_SECURITY;
939 break;
940 case INET_E_CODE_INSTALL_SUPPRESSED:
941 errorCode = "INET_E_CODE_INSTALL_SUPPRESSED";
942 errorType = wxWEB_NAV_ERR_SECURITY;
943 break;
944 }
945
946 wxString url = evt[1].GetString();
947 wxString target = evt[2].GetString();
948 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, GetId(),
949 url, target, false);
950 event.SetEventObject(this);
951 event.SetInt(errorType);
952 event.SetString(errorCode);
953 HandleWindowEvent(event);
954 break;
955 }
956
957 case DISPID_COMMANDSTATECHANGE:
958 {
959 long commandId = evt[0].GetLong();
960 bool enable = evt[1].GetBool();
961 if (commandId == CSC_NAVIGATEBACK)
962 {
963 m_canNavigateBack = enable;
964 }
965 else if (commandId == CSC_NAVIGATEFORWARD)
966 {
967 m_canNavigateForward = enable;
968 }
969 break;
970 }
971 case DISPID_NEWWINDOW3:
972 {
973 wxString url = evt[4].GetString();
974
975 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
976 GetId(), url, wxEmptyString, true);
977 event.SetEventObject(this);
978 HandleWindowEvent(event);
979
980 //If we veto the event then we cancel the new window
981 if (event.IsVetoed())
982 {
983 wxActiveXEventNativeMSW* nativeParams = evt.GetNativeParameters();
984 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[3]) = VARIANT_TRUE;
985 }
986 break;
987 }
988 }
989
990 evt.Skip();
991 }
992
993 #endif