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