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