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