Fix compiler warnings in the IE backend.
[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 wxString wxWebViewIE::GetPageText()
659 {
660 IHTMLDocument2* document = GetDocument();
661 wxString text;
662 IHTMLElement* body;
663 HRESULT hr = document->get_body(&body);
664 if(SUCCEEDED(hr))
665 {
666 BSTR out;
667 body->get_innerText(&out);
668 text = wxString(out);
669 body->Release();
670 }
671 document->Release();
672 return text;
673 }
674
675 bool wxWebViewIE::CanExecCommand(wxString command)
676 {
677 IHTMLDocument2* document = GetDocument();
678 VARIANT_BOOL enabled;
679
680 document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled);
681 document->Release();
682
683 return (enabled == VARIANT_TRUE);
684 }
685
686 void wxWebViewIE::ExecCommand(wxString command)
687 {
688 IHTMLDocument2* document = GetDocument();
689 document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL);
690 document->Release();
691 }
692
693 IHTMLDocument2* wxWebViewIE::GetDocument()
694 {
695 wxVariant variant = m_ie.GetProperty("Document");
696 IHTMLDocument2* document = (IHTMLDocument2*)variant.GetVoidPtr();
697
698 wxASSERT(document);
699
700 return document;
701 }
702
703 void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
704 {
705 if (m_webBrowser == NULL) return;
706
707 switch (evt.GetDispatchId())
708 {
709 case DISPID_BEFORENAVIGATE2:
710 {
711 m_isBusy = true;
712
713 wxString url = evt[1].GetString();
714 wxString target = evt[3].GetString();
715
716 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
717 GetId(), url, target, true);
718 event.SetEventObject(this);
719 HandleWindowEvent(event);
720
721 if (event.IsVetoed())
722 {
723 wxActiveXEventNativeMSW* nativeParams =
724 evt.GetNativeParameters();
725 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[0]) = VARIANT_TRUE;
726 }
727
728 // at this point, either the navigation event has been cancelled
729 // and we're not busy, either it was accepted and IWebBrowser2's
730 // Busy property will be true; so we don't need our override
731 // flag anymore.
732 m_isBusy = false;
733
734 break;
735 }
736
737 case DISPID_NAVIGATECOMPLETE2:
738 {
739 wxString url = evt[1].GetString();
740 // TODO: set target parameter if possible
741 wxString target = wxEmptyString;
742 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
743 GetId(), url, target, false);
744 event.SetEventObject(this);
745 HandleWindowEvent(event);
746 break;
747 }
748
749 case DISPID_PROGRESSCHANGE:
750 {
751 // download progress
752 break;
753 }
754
755 case DISPID_DOCUMENTCOMPLETE:
756 {
757 //Only send a complete even if we are actually finished, this brings
758 //the event in to line with webkit
759 READYSTATE rs;
760 m_webBrowser->get_ReadyState( &rs );
761 if(rs != READYSTATE_COMPLETE)
762 break;
763
764 wxString url = evt[1].GetString();
765
766 //As we are complete we also add to the history list, but not if the
767 //page is not the main page, ie it is a subframe
768 if(m_historyEnabled && !m_historyLoadingFromList && url == GetCurrentURL())
769 {
770 //If we are not at the end of the list, then erase everything
771 //between us and the end before adding the new page
772 if(m_historyPosition != static_cast<int>(m_historyList.size()) - 1)
773 {
774 m_historyList.erase(m_historyList.begin() + m_historyPosition + 1,
775 m_historyList.end());
776 }
777 wxSharedPtr<wxWebHistoryItem> item(new wxWebHistoryItem(url, GetCurrentTitle()));
778 m_historyList.push_back(item);
779 m_historyPosition++;
780 }
781 //Reset as we are done now
782 m_historyLoadingFromList = false;
783 // TODO: set target parameter if possible
784 wxString target = wxEmptyString;
785 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED, GetId(),
786 url, target, false);
787 event.SetEventObject(this);
788 HandleWindowEvent(event);
789 break;
790 }
791
792 case DISPID_STATUSTEXTCHANGE:
793 {
794 break;
795 }
796
797 case DISPID_TITLECHANGE:
798 {
799 break;
800 }
801
802 case DISPID_NAVIGATEERROR:
803 {
804 wxWebNavigationError errorType = wxWEB_NAV_ERR_OTHER;
805 wxString errorCode = "?";
806 switch (evt[3].GetLong())
807 {
808 case INET_E_INVALID_URL: // (0x800C0002L or -2146697214)
809 errorCode = "INET_E_INVALID_URL";
810 errorType = wxWEB_NAV_ERR_REQUEST;
811 break;
812 case INET_E_NO_SESSION: // (0x800C0003L or -2146697213)
813 errorCode = "INET_E_NO_SESSION";
814 errorType = wxWEB_NAV_ERR_CONNECTION;
815 break;
816 case INET_E_CANNOT_CONNECT: // (0x800C0004L or -2146697212)
817 errorCode = "INET_E_CANNOT_CONNECT";
818 errorType = wxWEB_NAV_ERR_CONNECTION;
819 break;
820 case INET_E_RESOURCE_NOT_FOUND: // (0x800C0005L or -2146697211)
821 errorCode = "INET_E_RESOURCE_NOT_FOUND";
822 errorType = wxWEB_NAV_ERR_NOT_FOUND;
823 break;
824 case INET_E_OBJECT_NOT_FOUND: // (0x800C0006L or -2146697210)
825 errorCode = "INET_E_OBJECT_NOT_FOUND";
826 errorType = wxWEB_NAV_ERR_NOT_FOUND;
827 break;
828 case INET_E_DATA_NOT_AVAILABLE: // (0x800C0007L or -2146697209)
829 errorCode = "INET_E_DATA_NOT_AVAILABLE";
830 errorType = wxWEB_NAV_ERR_NOT_FOUND;
831 break;
832 case INET_E_DOWNLOAD_FAILURE: // (0x800C0008L or -2146697208)
833 errorCode = "INET_E_DOWNLOAD_FAILURE";
834 errorType = wxWEB_NAV_ERR_CONNECTION;
835 break;
836 case INET_E_AUTHENTICATION_REQUIRED: // (0x800C0009L or -2146697207)
837 errorCode = "INET_E_AUTHENTICATION_REQUIRED";
838 errorType = wxWEB_NAV_ERR_AUTH;
839 break;
840 case INET_E_NO_VALID_MEDIA: // (0x800C000AL or -2146697206)
841 errorCode = "INET_E_NO_VALID_MEDIA";
842 errorType = wxWEB_NAV_ERR_REQUEST;
843 break;
844 case INET_E_CONNECTION_TIMEOUT: // (0x800C000BL or -2146697205)
845 errorCode = "INET_E_CONNECTION_TIMEOUT";
846 errorType = wxWEB_NAV_ERR_CONNECTION;
847 break;
848 case INET_E_INVALID_REQUEST: // (0x800C000CL or -2146697204)
849 errorCode = "INET_E_INVALID_REQUEST";
850 errorType = wxWEB_NAV_ERR_REQUEST;
851 break;
852 case INET_E_UNKNOWN_PROTOCOL: // (0x800C000DL or -2146697203)
853 errorCode = "INET_E_UNKNOWN_PROTOCOL";
854 errorType = wxWEB_NAV_ERR_REQUEST;
855 break;
856 case INET_E_SECURITY_PROBLEM: // (0x800C000EL or -2146697202)
857 errorCode = "INET_E_SECURITY_PROBLEM";
858 errorType = wxWEB_NAV_ERR_SECURITY;
859 break;
860 case INET_E_CANNOT_LOAD_DATA: // (0x800C000FL or -2146697201)
861 errorCode = "INET_E_CANNOT_LOAD_DATA";
862 errorType = wxWEB_NAV_ERR_OTHER;
863 break;
864 case INET_E_CANNOT_INSTANTIATE_OBJECT:
865 // CoCreateInstance will return an error code if this happens,
866 // we'll handle this above.
867 return;
868 break;
869 case INET_E_REDIRECT_FAILED: // (0x800C0014L or -2146697196)
870 errorCode = "INET_E_REDIRECT_FAILED";
871 errorType = wxWEB_NAV_ERR_OTHER;
872 break;
873 case INET_E_REDIRECT_TO_DIR: // (0x800C0015L or -2146697195)
874 errorCode = "INET_E_REDIRECT_TO_DIR";
875 errorType = wxWEB_NAV_ERR_REQUEST;
876 break;
877 case INET_E_CANNOT_LOCK_REQUEST: // (0x800C0016L or -2146697194)
878 errorCode = "INET_E_CANNOT_LOCK_REQUEST";
879 errorType = wxWEB_NAV_ERR_OTHER;
880 break;
881 case INET_E_USE_EXTEND_BINDING: // (0x800C0017L or -2146697193)
882 errorCode = "INET_E_USE_EXTEND_BINDING";
883 errorType = wxWEB_NAV_ERR_OTHER;
884 break;
885 case INET_E_TERMINATED_BIND: // (0x800C0018L or -2146697192)
886 errorCode = "INET_E_TERMINATED_BIND";
887 errorType = wxWEB_NAV_ERR_OTHER;
888 break;
889 case INET_E_INVALID_CERTIFICATE: // (0x800C0019L or -2146697191)
890 errorCode = "INET_E_INVALID_CERTIFICATE";
891 errorType = wxWEB_NAV_ERR_CERTIFICATE;
892 break;
893 case INET_E_CODE_DOWNLOAD_DECLINED: // (0x800C0100L or -2146696960)
894 errorCode = "INET_E_CODE_DOWNLOAD_DECLINED";
895 errorType = wxWEB_NAV_ERR_USER_CANCELLED;
896 break;
897 case INET_E_RESULT_DISPATCHED: // (0x800C0200L or -2146696704)
898 // cancel request cancelled...
899 errorCode = "INET_E_RESULT_DISPATCHED";
900 errorType = wxWEB_NAV_ERR_OTHER;
901 break;
902 case INET_E_CANNOT_REPLACE_SFP_FILE: // (0x800C0300L or -2146696448)
903 errorCode = "INET_E_CANNOT_REPLACE_SFP_FILE";
904 errorType = wxWEB_NAV_ERR_SECURITY;
905 break;
906 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY:
907 errorCode = "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
908 errorType = wxWEB_NAV_ERR_SECURITY;
909 break;
910 case INET_E_CODE_INSTALL_SUPPRESSED:
911 errorCode = "INET_E_CODE_INSTALL_SUPPRESSED";
912 errorType = wxWEB_NAV_ERR_SECURITY;
913 break;
914 }
915
916 wxString url = evt[1].GetString();
917 wxString target = evt[2].GetString();
918 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, GetId(),
919 url, target, false);
920 event.SetEventObject(this);
921 event.SetInt(errorType);
922 event.SetString(errorCode);
923 HandleWindowEvent(event);
924 break;
925 }
926
927 case DISPID_COMMANDSTATECHANGE:
928 {
929 long commandId = evt[0].GetLong();
930 bool enable = evt[1].GetBool();
931 if (commandId == CSC_NAVIGATEBACK)
932 {
933 m_canNavigateBack = enable;
934 }
935 else if (commandId == CSC_NAVIGATEFORWARD)
936 {
937 m_canNavigateForward = enable;
938 }
939 break;
940 }
941 case DISPID_NEWWINDOW3:
942 {
943 wxString url = evt[4].GetString();
944
945 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
946 GetId(), url, wxEmptyString, true);
947 event.SetEventObject(this);
948 HandleWindowEvent(event);
949
950 //If we veto the event then we cancel the new window
951 if (event.IsVetoed())
952 {
953 wxActiveXEventNativeMSW* nativeParams = evt.GetNativeParameters();
954 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[3]) = VARIANT_TRUE;
955 }
956 break;
957 }
958 }
959
960 evt.Skip();
961 }
962
963 #endif