Disable wxWebView::Find and associated functions under MinGW and VC6 to fix compilation
[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, 2011 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 && 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 #include "wx/msw/registry.h"
27 #include "wx/msw/missing.h"
28 #include "wx/filesys.h"
29 #include "wx/dynlib.h"
30 #include <initguid.h>
31
32 /* These GUID definitions are our own implementation to support interfaces
33 * normally in urlmon.h. See include/wx/msw/webview_ie.h
34 */
35
36 namespace {
37
38 DEFINE_GUID(wxIID_IInternetProtocolRoot,0x79eac9e3,0xbaf9,0x11ce,0x8c,0x82,0,0xaa,0,0x4b,0xa9,0xb);
39 DEFINE_GUID(wxIID_IInternetProtocol,0x79eac9e4,0xbaf9,0x11ce,0x8c,0x82,0,0xaa,0,0x4b,0xa9,0xb);
40 DEFINE_GUID(wxIID_IDocHostUIHandler, 0xbd3f23c0, 0xd43e, 0x11cf, 0x89, 0x3b, 0x00, 0xaa, 0x00, 0xbd, 0xce, 0x1a);
41
42 enum //Internal find flags
43 {
44 wxWEB_VIEW_FIND_ADD_POINTERS = 0x0001,
45 wxWEB_VIEW_FIND_REMOVE_HIGHLIGHT = 0x0002
46 };
47
48 }
49
50 wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewIE, wxWebView);
51
52 BEGIN_EVENT_TABLE(wxWebViewIE, wxControl)
53 EVT_ACTIVEX(wxID_ANY, wxWebViewIE::onActiveXEvent)
54 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg)
55 END_EVENT_TABLE()
56
57 bool wxWebViewIE::Create(wxWindow* parent,
58 wxWindowID id,
59 const wxString& url,
60 const wxPoint& pos,
61 const wxSize& size,
62 long style,
63 const wxString& name)
64 {
65 if (!wxControl::Create(parent, id, pos, size, style,
66 wxDefaultValidator, name))
67 {
68 return false;
69 }
70
71 m_webBrowser = NULL;
72 m_isBusy = false;
73 m_historyLoadingFromList = false;
74 m_historyEnabled = true;
75 m_historyPosition = -1;
76 m_zoomType = wxWEB_VIEW_ZOOM_TYPE_TEXT;
77 FindClear();
78
79 if (::CoCreateInstance(CLSID_WebBrowser, NULL,
80 CLSCTX_INPROC_SERVER, // CLSCTX_INPROC,
81 IID_IWebBrowser2 , (void**)&m_webBrowser) != 0)
82 {
83 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
84 return false;
85 }
86
87 m_ie.SetDispatchPtr(m_webBrowser); // wxAutomationObject will release itself
88
89 m_webBrowser->put_RegisterAsBrowser(VARIANT_TRUE);
90 m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
91
92 m_uiHandler = new DocHostUIHandler;
93
94 m_container = new wxIEContainer(this, IID_IWebBrowser2, m_webBrowser, m_uiHandler);
95
96 EnableControlFeature(21 /* FEATURE_DISABLE_NAVIGATION_SOUNDS */);
97
98 LoadURL(url);
99 return true;
100 }
101
102 wxWebViewIE::~wxWebViewIE()
103 {
104 for(unsigned int i = 0; i < m_factories.size(); i++)
105 {
106 m_factories[i]->Release();
107 }
108 FindClear();
109 }
110
111 void wxWebViewIE::LoadURL(const wxString& url)
112 {
113 m_ie.CallMethod("Navigate", wxConvertStringToOle(url));
114 }
115
116 void wxWebViewIE::DoSetPage(const wxString& html, const wxString& baseUrl)
117 {
118 BSTR bstr = SysAllocString(OLESTR(""));
119 SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
120 if (psaStrings != NULL)
121 {
122 VARIANT *param;
123 HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
124 param->vt = VT_BSTR;
125 param->bstrVal = bstr;
126
127 hr = SafeArrayUnaccessData(psaStrings);
128
129 wxCOMPtr<IHTMLDocument2> document(GetDocument());
130
131 if(!document)
132 return;
133
134 document->write(psaStrings);
135 document->close();
136
137 SafeArrayDestroy(psaStrings);
138
139 bstr = SysAllocString(html.wc_str());
140
141 // Creates a new one-dimensional array
142 psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
143 if (psaStrings != NULL)
144 {
145 hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
146 param->vt = VT_BSTR;
147 param->bstrVal = bstr;
148 hr = SafeArrayUnaccessData(psaStrings);
149
150 document = GetDocument();
151
152 if(!document)
153 return;
154
155 document->write(psaStrings);
156
157 // SafeArrayDestroy calls SysFreeString for each BSTR
158 SafeArrayDestroy(psaStrings);
159
160 //We send the events when we are done to mimic webkit
161 //Navigated event
162 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
163 GetId(), baseUrl, "");
164 event.SetEventObject(this);
165 HandleWindowEvent(event);
166
167 //Document complete event
168 event.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED);
169 event.SetEventObject(this);
170 HandleWindowEvent(event);
171 }
172 else
173 {
174 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
175 }
176 }
177 else
178 {
179 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL during clear");
180 }
181 }
182
183 wxString wxWebViewIE::GetPageSource() const
184 {
185 wxCOMPtr<IHTMLDocument2> document(GetDocument());
186
187 if(document)
188 {
189 wxCOMPtr<IHTMLElement> bodyTag;
190 wxCOMPtr<IHTMLElement> htmlTag;
191 wxString source;
192 HRESULT hr = document->get_body(&bodyTag);
193 if(SUCCEEDED(hr))
194 {
195 hr = bodyTag->get_parentElement(&htmlTag);
196 if(SUCCEEDED(hr))
197 {
198 BSTR bstr;
199 htmlTag->get_outerHTML(&bstr);
200 source = wxString(bstr);
201 }
202 }
203 return source;
204 }
205 else
206 {
207 return "";
208 }
209 }
210
211 wxWebViewZoom wxWebViewIE::GetZoom() const
212 {
213 switch( m_zoomType )
214 {
215 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT:
216 return GetIEOpticalZoom();
217 case wxWEB_VIEW_ZOOM_TYPE_TEXT:
218 return GetIETextZoom();
219 default:
220 wxFAIL;
221 }
222
223 //Dummy return to stop compiler warnings
224 return wxWEB_VIEW_ZOOM_MEDIUM;
225
226 }
227
228 void wxWebViewIE::SetZoom(wxWebViewZoom zoom)
229 {
230 switch( m_zoomType )
231 {
232 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT:
233 SetIEOpticalZoom(zoom);
234 break;
235 case wxWEB_VIEW_ZOOM_TYPE_TEXT:
236 SetIETextZoom(zoom);
237 break;
238 default:
239 wxFAIL;
240 }
241 }
242
243 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level)
244 {
245 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
246 //is 0 to 4 so the check is unnecessary, these match exactly with the
247 //enum values
248 VARIANT zoomVariant;
249 VariantInit (&zoomVariant);
250 V_VT(&zoomVariant) = VT_I4;
251 V_I4(&zoomVariant) = level;
252
253 #if wxDEBUG_LEVEL
254 HRESULT result =
255 #endif
256 m_webBrowser->ExecWB(OLECMDID_ZOOM,
257 OLECMDEXECOPT_DONTPROMPTUSER,
258 &zoomVariant, NULL);
259 wxASSERT(result == S_OK);
260 }
261
262 wxWebViewZoom wxWebViewIE::GetIETextZoom() const
263 {
264 VARIANT zoomVariant;
265 VariantInit (&zoomVariant);
266 V_VT(&zoomVariant) = VT_I4;
267
268 #if wxDEBUG_LEVEL
269 HRESULT result =
270 #endif
271 m_webBrowser->ExecWB(OLECMDID_ZOOM,
272 OLECMDEXECOPT_DONTPROMPTUSER,
273 NULL, &zoomVariant);
274 wxASSERT(result == S_OK);
275
276 //We can safely cast here as we know that the range matches our enum
277 return static_cast<wxWebViewZoom>(V_I4(&zoomVariant));
278 }
279
280 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level)
281 {
282 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
283 //is 10 to 1000 so the check is unnecessary
284 VARIANT zoomVariant;
285 VariantInit (&zoomVariant);
286 V_VT(&zoomVariant) = VT_I4;
287
288 //We make a somewhat arbitray map here, taken from values used by webkit
289 switch(level)
290 {
291 case wxWEB_VIEW_ZOOM_TINY:
292 V_I4(&zoomVariant) = 60;
293 break;
294 case wxWEB_VIEW_ZOOM_SMALL:
295 V_I4(&zoomVariant) = 80;
296 break;
297 case wxWEB_VIEW_ZOOM_MEDIUM:
298 V_I4(&zoomVariant) = 100;
299 break;
300 case wxWEB_VIEW_ZOOM_LARGE:
301 V_I4(&zoomVariant) = 130;
302 break;
303 case wxWEB_VIEW_ZOOM_LARGEST:
304 V_I4(&zoomVariant) = 160;
305 break;
306 default:
307 wxFAIL;
308 }
309
310 #if wxDEBUG_LEVEL
311 HRESULT result =
312 #endif
313 m_webBrowser->ExecWB((OLECMDID)63 /*OLECMDID_OPTICAL_ZOOM*/,
314 OLECMDEXECOPT_DODEFAULT,
315 &zoomVariant,
316 NULL);
317 wxASSERT(result == S_OK);
318 }
319
320 wxWebViewZoom wxWebViewIE::GetIEOpticalZoom() const
321 {
322 VARIANT zoomVariant;
323 VariantInit (&zoomVariant);
324 V_VT(&zoomVariant) = VT_I4;
325
326 #if wxDEBUG_LEVEL
327 HRESULT result =
328 #endif
329 m_webBrowser->ExecWB((OLECMDID)63 /*OLECMDID_OPTICAL_ZOOM*/,
330 OLECMDEXECOPT_DODEFAULT, NULL,
331 &zoomVariant);
332 wxASSERT(result == S_OK);
333
334 const int zoom = V_I4(&zoomVariant);
335
336 //We make a somewhat arbitray map here, taken from values used by webkit
337 if (zoom <= 65)
338 {
339 return wxWEB_VIEW_ZOOM_TINY;
340 }
341 else if (zoom > 65 && zoom <= 90)
342 {
343 return wxWEB_VIEW_ZOOM_SMALL;
344 }
345 else if (zoom > 90 && zoom <= 115)
346 {
347 return wxWEB_VIEW_ZOOM_MEDIUM;
348 }
349 else if (zoom > 115 && zoom <= 145)
350 {
351 return wxWEB_VIEW_ZOOM_LARGE;
352 }
353 else /*if (zoom > 145) */ //Using else removes a compiler warning
354 {
355 return wxWEB_VIEW_ZOOM_LARGEST;
356 }
357 }
358
359 void wxWebViewIE::SetZoomType(wxWebViewZoomType type)
360 {
361 m_zoomType = type;
362 }
363
364 wxWebViewZoomType wxWebViewIE::GetZoomType() const
365 {
366 return m_zoomType;
367 }
368
369 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type) const
370 {
371 //IE 6 and below only support text zoom, so check the registry to see what
372 //version we actually have
373 wxRegKey key(wxRegKey::HKLM, "Software\\Microsoft\\Internet Explorer");
374 wxString value;
375 key.QueryValue("Version", value);
376
377 long version = wxAtoi(value.Left(1));
378 if(version <= 6 && type == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
379 return false;
380 else
381 return true;
382 }
383
384 void wxWebViewIE::Print()
385 {
386 m_webBrowser->ExecWB(OLECMDID_PRINTPREVIEW,
387 OLECMDEXECOPT_DODEFAULT, NULL, NULL);
388 }
389
390 bool wxWebViewIE::CanGoBack() const
391 {
392 if(m_historyEnabled)
393 return m_historyPosition > 0;
394 else
395 return false;
396 }
397
398 bool wxWebViewIE::CanGoForward() const
399 {
400 if(m_historyEnabled)
401 return m_historyPosition != static_cast<int>(m_historyList.size()) - 1;
402 else
403 return false;
404 }
405
406 void wxWebViewIE::LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item)
407 {
408 int pos = -1;
409 for(unsigned int i = 0; i < m_historyList.size(); i++)
410 {
411 //We compare the actual pointers to find the correct item
412 if(m_historyList[i].get() == item.get())
413 pos = i;
414 }
415 wxASSERT_MSG(pos != static_cast<int>(m_historyList.size()),
416 "invalid history item");
417 m_historyLoadingFromList = true;
418 LoadURL(item->GetUrl());
419 m_historyPosition = pos;
420 }
421
422 wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewIE::GetBackwardHistory()
423 {
424 wxVector<wxSharedPtr<wxWebViewHistoryItem> > backhist;
425 //As we don't have std::copy or an iterator constructor in the wxwidgets
426 //native vector we construct it by hand
427 for(int i = 0; i < m_historyPosition; i++)
428 {
429 backhist.push_back(m_historyList[i]);
430 }
431 return backhist;
432 }
433
434 wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewIE::GetForwardHistory()
435 {
436 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forwardhist;
437 //As we don't have std::copy or an iterator constructor in the wxwidgets
438 //native vector we construct it by hand
439 for(int i = m_historyPosition + 1; i < static_cast<int>(m_historyList.size()); i++)
440 {
441 forwardhist.push_back(m_historyList[i]);
442 }
443 return forwardhist;
444 }
445
446 void wxWebViewIE::GoBack()
447 {
448 LoadHistoryItem(m_historyList[m_historyPosition - 1]);
449 }
450
451 void wxWebViewIE::GoForward()
452 {
453 LoadHistoryItem(m_historyList[m_historyPosition + 1]);
454 }
455
456 void wxWebViewIE::Stop()
457 {
458 m_ie.CallMethod("Stop");
459 }
460
461 void wxWebViewIE::ClearHistory()
462 {
463 m_historyList.clear();
464 m_historyPosition = -1;
465 }
466
467 void wxWebViewIE::EnableHistory(bool enable)
468 {
469 m_historyEnabled = enable;
470 m_historyList.clear();
471 m_historyPosition = -1;
472 }
473
474 void wxWebViewIE::Reload(wxWebViewReloadFlags flags)
475 {
476 VARIANTARG level;
477 VariantInit(&level);
478 V_VT(&level) = VT_I2;
479
480 switch(flags)
481 {
482 case wxWEB_VIEW_RELOAD_DEFAULT:
483 V_I2(&level) = REFRESH_NORMAL;
484 break;
485 case wxWEB_VIEW_RELOAD_NO_CACHE:
486 V_I2(&level) = REFRESH_COMPLETELY;
487 break;
488 default:
489 wxFAIL_MSG("Unexpected reload type");
490 }
491
492 m_webBrowser->Refresh2(&level);
493 }
494
495 bool wxWebViewIE::IsOfflineMode()
496 {
497 wxVariant out = m_ie.GetProperty("Offline");
498
499 wxASSERT(out.GetType() == "bool");
500
501 return out.GetBool();
502 }
503
504 void wxWebViewIE::SetOfflineMode(bool offline)
505 {
506 // FIXME: the wxWidgets docs do not really document what the return
507 // parameter of PutProperty is
508 #if wxDEBUG_LEVEL
509 const bool success =
510 #endif
511 m_ie.PutProperty("Offline", (offline ?
512 VARIANT_TRUE :
513 VARIANT_FALSE));
514 wxASSERT(success);
515 }
516
517 bool wxWebViewIE::IsBusy() const
518 {
519 if (m_isBusy) return true;
520
521 wxVariant out = m_ie.GetProperty("Busy");
522
523 wxASSERT(out.GetType() == "bool");
524
525 return out.GetBool();
526 }
527
528 wxString wxWebViewIE::GetCurrentURL() const
529 {
530 wxVariant out = m_ie.GetProperty("LocationURL");
531
532 wxASSERT(out.GetType() == "string");
533 return out.GetString();
534 }
535
536 wxString wxWebViewIE::GetCurrentTitle() const
537 {
538 wxCOMPtr<IHTMLDocument2> document(GetDocument());
539
540 if(document)
541 {
542 BSTR title;
543 document->get_nameProp(&title);
544 return wxString(title);
545 }
546 else
547 {
548 return "";
549 }
550 }
551
552 bool wxWebViewIE::CanCut() const
553 {
554 return CanExecCommand("Cut");
555 }
556
557 bool wxWebViewIE::CanCopy() const
558 {
559 return CanExecCommand("Copy");
560 }
561
562 bool wxWebViewIE::CanPaste() const
563 {
564 return CanExecCommand("Paste");
565 }
566
567 void wxWebViewIE::Cut()
568 {
569 ExecCommand("Cut");
570 }
571
572 void wxWebViewIE::Copy()
573 {
574 ExecCommand("Copy");
575 }
576
577 void wxWebViewIE::Paste()
578 {
579 ExecCommand("Paste");
580 }
581
582 bool wxWebViewIE::CanUndo() const
583 {
584 return CanExecCommand("Undo");
585 }
586
587 bool wxWebViewIE::CanRedo() const
588 {
589 return CanExecCommand("Redo");
590 }
591
592 void wxWebViewIE::Undo()
593 {
594 ExecCommand("Undo");
595 }
596
597 void wxWebViewIE::Redo()
598 {
599 ExecCommand("Redo");
600 }
601
602 long wxWebViewIE::Find(const wxString& text, int flags)
603 {
604 #if !defined(__MINGW32__) && !defined(__VISUALC6__)
605 //If the text is empty then we clear.
606 if(text.IsEmpty())
607 {
608 ClearSelection();
609 if(m_findFlags & wxWEB_VIEW_FIND_HIGHLIGHT_RESULT)
610 {
611 FindInternal(m_findText, (m_findFlags &~ wxWEB_VIEW_FIND_HIGHLIGHT_RESULT), wxWEB_VIEW_FIND_REMOVE_HIGHLIGHT);
612 }
613 FindClear();
614 return wxNOT_FOUND;
615 }
616 //Have we done this search before?
617 if(m_findText == text)
618 {
619 //Just do a highlight?
620 if((flags & wxWEB_VIEW_FIND_HIGHLIGHT_RESULT) != (m_findFlags & wxWEB_VIEW_FIND_HIGHLIGHT_RESULT))
621 {
622 m_findFlags = flags;
623 if(!m_findPointers.empty())
624 {
625 FindInternal(m_findText, m_findFlags, ((flags & wxWEB_VIEW_FIND_HIGHLIGHT_RESULT) == 0 ? wxWEB_VIEW_FIND_REMOVE_HIGHLIGHT : 0));
626 }
627 return m_findPosition;
628 }
629 else if(((m_findFlags & wxWEB_VIEW_FIND_ENTIRE_WORD) == (flags & wxWEB_VIEW_FIND_ENTIRE_WORD)) && ((m_findFlags & wxWEB_VIEW_FIND_MATCH_CASE) == (flags&wxWEB_VIEW_FIND_MATCH_CASE)))
630 {
631 m_findFlags = flags;
632 return FindNext(((flags & wxWEB_VIEW_FIND_BACKWARDS) ? -1 : 1));
633 }
634 }
635 //Remove old highlight if any.
636 if(m_findFlags & wxWEB_VIEW_FIND_HIGHLIGHT_RESULT)
637 {
638 FindInternal(m_findText, (m_findFlags &~ wxWEB_VIEW_FIND_HIGHLIGHT_RESULT), wxWEB_VIEW_FIND_REMOVE_HIGHLIGHT);
639 }
640 //Reset find variables.
641 FindClear();
642 ClearSelection();
643 m_findText = text;
644 m_findFlags = flags;
645 //find the text and return count.
646 FindInternal(text, flags, wxWEB_VIEW_FIND_ADD_POINTERS);
647 return m_findPointers.empty() ? wxNOT_FOUND : m_findPointers.size();
648 #else
649 return wxNOT_FOUND;
650 #endif
651 }
652
653 void wxWebViewIE::SetEditable(bool enable)
654 {
655 wxCOMPtr<IHTMLDocument2> document(GetDocument());
656
657 if(document)
658 {
659 if( enable )
660 document->put_designMode(SysAllocString(L"On"));
661 else
662 document->put_designMode(SysAllocString(L"Off"));
663
664 }
665 }
666
667 bool wxWebViewIE::IsEditable() const
668 {
669 wxCOMPtr<IHTMLDocument2> document(GetDocument());
670
671 if(document)
672 {
673 BSTR mode;
674 document->get_designMode(&mode);
675 if(wxString(mode) == "On")
676 return true;
677 else
678 return false;
679 }
680 else
681 {
682 return false;
683 }
684 }
685
686 void wxWebViewIE::SelectAll()
687 {
688 ExecCommand("SelectAll");
689 }
690
691 bool wxWebViewIE::HasSelection() const
692 {
693 wxCOMPtr<IHTMLDocument2> document(GetDocument());
694
695 if(document)
696 {
697 wxCOMPtr<IHTMLSelectionObject> selection;
698 wxString sel;
699 HRESULT hr = document->get_selection(&selection);
700 if(SUCCEEDED(hr))
701 {
702 BSTR type;
703 selection->get_type(&type);
704 sel = wxString(type);
705 }
706 return sel != "None";
707 }
708 else
709 {
710 return false;
711 }
712 }
713
714 void wxWebViewIE::DeleteSelection()
715 {
716 ExecCommand("Delete");
717 }
718
719 wxString wxWebViewIE::GetSelectedText() const
720 {
721 wxCOMPtr<IHTMLDocument2> document(GetDocument());
722
723 if(document)
724 {
725 wxCOMPtr<IHTMLSelectionObject> selection;
726 wxString selected;
727 HRESULT hr = document->get_selection(&selection);
728 if(SUCCEEDED(hr))
729 {
730 wxCOMPtr<IDispatch> disrange;
731 hr = selection->createRange(&disrange);
732 if(SUCCEEDED(hr))
733 {
734 wxCOMPtr<IHTMLTxtRange> range;
735 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
736 if(SUCCEEDED(hr))
737 {
738 BSTR text;
739 range->get_text(&text);
740 selected = wxString(text);
741 }
742 }
743 }
744 return selected;
745 }
746 else
747 {
748 return "";
749 }
750 }
751
752 wxString wxWebViewIE::GetSelectedSource() const
753 {
754 wxCOMPtr<IHTMLDocument2> document(GetDocument());
755
756 if(document)
757 {
758 wxCOMPtr<IHTMLSelectionObject> selection;
759 wxString selected;
760 HRESULT hr = document->get_selection(&selection);
761 if(SUCCEEDED(hr))
762 {
763 wxCOMPtr<IDispatch> disrange;
764 hr = selection->createRange(&disrange);
765 if(SUCCEEDED(hr))
766 {
767 wxCOMPtr<IHTMLTxtRange> range;
768 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
769 if(SUCCEEDED(hr))
770 {
771 BSTR text;
772 range->get_htmlText(&text);
773 selected = wxString(text);
774 }
775 }
776 }
777 return selected;
778 }
779 else
780 {
781 return "";
782 }
783 }
784
785 void wxWebViewIE::ClearSelection()
786 {
787 wxCOMPtr<IHTMLDocument2> document(GetDocument());
788
789 if(document)
790 {
791 wxCOMPtr<IHTMLSelectionObject> selection;
792 wxString selected;
793 HRESULT hr = document->get_selection(&selection);
794 if(SUCCEEDED(hr))
795 {
796 selection->empty();
797 }
798 }
799 }
800
801 wxString wxWebViewIE::GetPageText() const
802 {
803 wxCOMPtr<IHTMLDocument2> document(GetDocument());
804
805 if(document)
806 {
807 wxString text;
808 wxCOMPtr<IHTMLElement> body;
809 HRESULT hr = document->get_body(&body);
810 if(SUCCEEDED(hr))
811 {
812 BSTR out;
813 body->get_innerText(&out);
814 text = wxString(out);
815 }
816 return text;
817 }
818 else
819 {
820 return "";
821 }
822 }
823
824 void wxWebViewIE::RunScript(const wxString& javascript)
825 {
826 wxCOMPtr<IHTMLDocument2> document(GetDocument());
827
828 if(document)
829 {
830 wxCOMPtr<IHTMLWindow2> window;
831 wxString language = "javascript";
832 HRESULT hr = document->get_parentWindow(&window);
833 if(SUCCEEDED(hr))
834 {
835 VARIANT level;
836 VariantInit(&level);
837 V_VT(&level) = VT_EMPTY;
838 window->execScript(SysAllocString(javascript.wc_str()),
839 SysAllocString(language.wc_str()),
840 &level);
841 }
842 }
843 }
844
845 void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
846 {
847 wxDynamicLibrary urlMon(wxT("urlmon.dll"));
848 if(urlMon.HasSymbol(wxT("CoInternetGetSession")))
849 {
850 typedef HRESULT (WINAPI *CoInternetGetSession_t)(DWORD, wxIInternetSession**, DWORD);
851 wxDYNLIB_FUNCTION(CoInternetGetSession_t, CoInternetGetSession, urlMon);
852
853 ClassFactory* cf = new ClassFactory(handler);
854 wxIInternetSession* session;
855 HRESULT res = (*pfnCoInternetGetSession)(0, &session, 0);
856 if(FAILED(res))
857 {
858 wxFAIL_MSG("Could not retrive internet session");
859 }
860
861 HRESULT hr = session->RegisterNameSpace(cf, CLSID_FileProtocol,
862 handler->GetName().wc_str(),
863 0, NULL, 0);
864 if(FAILED(hr))
865 {
866 wxFAIL_MSG("Could not register protocol");
867 }
868 m_factories.push_back(cf);
869 }
870 else
871 {
872 wxFAIL_MSG("urlmon does not contain CoInternetGetSession");
873 }
874 }
875
876 bool wxWebViewIE::CanExecCommand(wxString command) const
877 {
878 wxCOMPtr<IHTMLDocument2> document(GetDocument());
879
880 if(document)
881 {
882 VARIANT_BOOL enabled;
883
884 document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled);
885
886 return (enabled == VARIANT_TRUE);
887 }
888 else
889 {
890 return false;
891 }
892
893 }
894
895 void wxWebViewIE::ExecCommand(wxString command)
896 {
897 wxCOMPtr<IHTMLDocument2> document(GetDocument());
898
899 if(document)
900 {
901 document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL);
902 }
903 }
904
905 wxCOMPtr<IHTMLDocument2> wxWebViewIE::GetDocument() const
906 {
907 wxCOMPtr<IDispatch> dispatch;
908 wxCOMPtr<IHTMLDocument2> document;
909 HRESULT result = m_webBrowser->get_Document(&dispatch);
910 if(dispatch && SUCCEEDED(result))
911 {
912 //document is set to null automatically if the interface isn't supported
913 dispatch->QueryInterface(IID_IHTMLDocument2, (void**)&document);
914 }
915 return document;
916 }
917
918 bool wxWebViewIE::IsElementVisible(IHTMLElement* elm)
919 {
920 #if !defined(__MINGW32__) && !defined(__VISUALC6__)
921 IHTMLCurrentStyle* style;
922 IHTMLElement *elm1 = elm;
923 IHTMLElement2 *elm2;
924 BSTR tmp_bstr;
925 bool is_visible = true;
926 //This method is not perfect but it does discover most of the hidden elements.
927 //so if a better solution is found, then please do improve.
928 while(elm1)
929 {
930 if(SUCCEEDED(elm1->QueryInterface(IID_IHTMLElement2, (void**) &elm2)))
931 {
932 if(SUCCEEDED(elm2->get_currentStyle(&style)))
933 {
934 //Check if the object has the style display:none.
935 if((style->get_display(&tmp_bstr) != S_OK) ||
936 (tmp_bstr != NULL && (_wcsicmp(tmp_bstr, L"none") == 0)))
937 {
938 is_visible = false;
939 }
940 //Check if the object has the style visibility:hidden.
941 if(is_visible && (style->get_visibility(&tmp_bstr) != S_OK) ||
942 (tmp_bstr != NULL && _wcsicmp(tmp_bstr, L"hidden") == 0))
943 {
944 is_visible = false;
945 }
946 style->Release();
947 }
948 elm2->Release();
949 }
950
951 //Lets check the object's parent element.
952 IHTMLElement* parent;
953 if(is_visible && SUCCEEDED(elm1->get_parentElement(&parent)))
954 {
955 elm1->Release();
956 elm1 = parent;
957 }
958 else
959 {
960 elm1->Release();
961 break;
962 }
963 }
964 return is_visible;
965 #else
966 return false;
967 #endif
968 }
969
970 void wxWebViewIE::FindInternal(const wxString& text, int flags, int internal_flag)
971 {
972 #if !defined(__MINGW32__) && !defined(__VISUALC6__)
973 IMarkupServices *pIMS;
974 IMarkupContainer *pIMC;
975 IMarkupPointer *ptrBegin, *ptrEnd;
976 IHTMLElement* elm;
977 long find_flag = 0;
978 IHTMLDocument2 *document = GetDocument();
979 //This function does the acutal work.
980 if(SUCCEEDED(document->QueryInterface(IID_IMarkupServices, (void **)&pIMS)))
981 {
982 if(SUCCEEDED(document->QueryInterface(IID_IMarkupContainer, (void **)&pIMC)))
983 {
984 BSTR attr_bstr = SysAllocString(L"style=\"background-color:#ffff00\"");
985 BSTR text_bstr = SysAllocString(text.wc_str());
986 pIMS->CreateMarkupPointer(&ptrBegin);
987 pIMS->CreateMarkupPointer(&ptrEnd);
988
989 ptrBegin->SetGravity(POINTER_GRAVITY_Right);
990 ptrBegin->MoveToContainer(pIMC, TRUE);
991 //Create the find flag from the wx one.
992 if(flags & wxWEB_VIEW_FIND_ENTIRE_WORD)
993 {
994 find_flag |= FINDTEXT_WHOLEWORD;
995 }
996 if(flags & wxWEB_VIEW_FIND_MATCH_CASE)
997 {
998 find_flag |= FINDTEXT_MATCHCASE;
999 }
1000
1001 //A little speed-up to avoid to re-alloc in the positions vector.
1002 if(text.Len() < 3 && m_findPointers.capacity() < 500)
1003 {
1004 m_findPointers.reserve(text.Len() == 1 ? 1000 : 500);
1005 }
1006
1007 while(ptrBegin->FindText(text_bstr, find_flag, ptrEnd, NULL) == S_OK)
1008 {
1009 if(ptrBegin->CurrentScope(&elm) == S_OK)
1010 {
1011 if(IsElementVisible(elm))
1012 {
1013 //Highlight the word if the flag was set.
1014 if(flags & wxWEB_VIEW_FIND_HIGHLIGHT_RESULT)
1015 {
1016 IHTMLElement* pFontEl;
1017 pIMS->CreateElement(TAGID_FONT, attr_bstr, &pFontEl);
1018 pIMS->InsertElement(pFontEl, ptrBegin, ptrEnd);
1019 }
1020 if(internal_flag & wxWEB_VIEW_FIND_REMOVE_HIGHLIGHT)
1021 {
1022 IHTMLElement* pFontEl;
1023 ptrBegin->CurrentScope(&pFontEl);
1024 pIMS->RemoveElement(pFontEl);
1025 pFontEl->Release();
1026 }
1027 if(internal_flag & wxWEB_VIEW_FIND_ADD_POINTERS)
1028 {
1029 IMarkupPointer *cptrBegin, *cptrEnd;
1030 pIMS->CreateMarkupPointer(&cptrBegin);
1031 pIMS->CreateMarkupPointer(&cptrEnd);
1032 cptrBegin->MoveToPointer(ptrBegin);
1033 cptrEnd->MoveToPointer(ptrEnd);
1034 m_findPointers.push_back(wxFindPointers(cptrBegin,cptrEnd));
1035 }
1036 }
1037 elm->Release();
1038 }
1039 ptrBegin->MoveToPointer(ptrEnd);
1040 }
1041 //Clean up.
1042 SysFreeString(text_bstr);
1043 SysFreeString(attr_bstr);
1044 pIMC->Release();
1045 ptrBegin->Release();
1046 ptrEnd->Release();
1047 }
1048 pIMS->Release();
1049 }
1050 document->Release();
1051 #endif
1052 }
1053
1054 long wxWebViewIE::FindNext(int direction)
1055 {
1056 #if !defined(__MINGW32__) && !defined(__VISUALC6__)
1057 //Don't bother if we have no pointers set.
1058 if(m_findPointers.empty())
1059 {
1060 return wxNOT_FOUND;
1061 }
1062 //Manage the find position and do some checks.
1063 if(direction > 0)
1064 {
1065 m_findPosition++;
1066 }
1067 else
1068 {
1069 m_findPosition--;
1070 }
1071
1072 if(m_findPosition >= (signed)m_findPointers.size())
1073 {
1074 if(m_findFlags & wxWEB_VIEW_FIND_WRAP)
1075 {
1076 m_findPosition = 0;
1077 }
1078 else
1079 {
1080 m_findPosition--;
1081 return wxNOT_FOUND;
1082 }
1083 }
1084 else if(m_findPosition < 0)
1085 {
1086 if(m_findFlags & wxWEB_VIEW_FIND_WRAP)
1087 {
1088 m_findPosition = m_findPointers.size()-1;
1089 }
1090 else
1091 {
1092 m_findPosition++;
1093 return wxNOT_FOUND;
1094 }
1095 }
1096 //some variables to use later on.
1097 IHTMLElement *body_element;
1098 IHTMLBodyElement *body;
1099 IHTMLTxtRange *range = NULL;
1100 IMarkupServices *pIMS;
1101 IHTMLDocument2 *document = GetDocument();
1102 long ret = -1;
1103 //Now try to create a range from the body.
1104 if(SUCCEEDED(document->get_body(&body_element)))
1105 {
1106 if(SUCCEEDED(body_element->QueryInterface(IID_IHTMLBodyElement,(void**)&body)))
1107 {
1108 if(SUCCEEDED(body->createTextRange(&range)))
1109 {
1110 //So far so good, now we try to position our find pointers.
1111 if(SUCCEEDED(document->QueryInterface(IID_IMarkupServices,(void **)&pIMS)))
1112 {
1113 IMarkupPointer *begin = m_findPointers[m_findPosition].begin, *end = m_findPointers[m_findPosition].end;
1114 if(pIMS->MoveRangeToPointers(begin,end,range) == S_OK && range->select() == S_OK)
1115 {
1116 ret = m_findPosition;
1117 }
1118 pIMS->Release();
1119 }
1120 range->Release();
1121 }
1122 body->Release();
1123 }
1124 body_element->Release();
1125 }
1126 document->Release();
1127 return ret;
1128 #else
1129 return wxNOT_FOUND;
1130 #endif
1131 }
1132
1133 void wxWebViewIE::FindClear()
1134 {
1135 #if !defined(__MINGW32__) && !defined(__VISUALC6__)
1136 //Reset find variables.
1137 m_findText.Empty();
1138 m_findFlags = wxWEB_VIEW_FIND_DEFAULT;
1139 m_findPosition = -1;
1140
1141 //The m_findPointers contains pointers for the found text.
1142 //Since it uses ref counting we call release on the pointers first
1143 //before we remove them from the vector. In other words do not just
1144 //remove elements from m_findPointers without calling release first
1145 //or you will get a memory leak.
1146 size_t count = m_findPointers.size();
1147 for(size_t i = 0; i < count; i++)
1148 {
1149 m_findPointers[i].begin->Release();
1150 m_findPointers[i].end->Release();
1151 }
1152 m_findPointers.clear();
1153 #endif
1154 }
1155
1156 bool wxWebViewIE::EnableControlFeature(long flag, bool enable)
1157 {
1158 #if wxUSE_DYNLIB_CLASS
1159
1160 wxDynamicLibrary urlMon(wxT("urlmon.dll"));
1161 if( urlMon.IsLoaded() &&
1162 urlMon.HasSymbol("CoInternetSetFeatureEnabled") &&
1163 urlMon.HasSymbol("CoInternetIsFeatureEnabled"))
1164 {
1165 typedef HRESULT (WINAPI *CoInternetSetFeatureEnabled_t)(DWORD, DWORD, BOOL);
1166 typedef HRESULT (WINAPI *CoInternetIsFeatureEnabled_t)(DWORD, DWORD);
1167
1168 wxDYNLIB_FUNCTION(CoInternetSetFeatureEnabled_t, CoInternetSetFeatureEnabled, urlMon);
1169 wxDYNLIB_FUNCTION(CoInternetIsFeatureEnabled_t, CoInternetIsFeatureEnabled, urlMon);
1170
1171 HRESULT hr = (*pfnCoInternetIsFeatureEnabled)(flag,
1172 0x2 /* SET_FEATURE_ON_PROCESS */);
1173 if((hr == S_OK && enable) || (hr == S_FALSE && !enable))
1174 return true;
1175
1176 hr = (*pfnCoInternetSetFeatureEnabled)(flag,
1177 0x2/* SET_FEATURE_ON_PROCESS */,
1178 (enable ? TRUE : FALSE));
1179 if ( FAILED(hr) )
1180 {
1181 wxLogApiError(wxT("CoInternetSetFeatureEnabled"), hr);
1182 return false;
1183 }
1184 return true;
1185 }
1186 return false;
1187 #else
1188 wxUnusedVar(flag);
1189 wxUnusedVar(enable);
1190 return false;
1191 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
1192 }
1193
1194 void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
1195 {
1196 if (m_webBrowser == NULL) return;
1197
1198 switch (evt.GetDispatchId())
1199 {
1200 case DISPID_BEFORENAVIGATE2:
1201 {
1202 m_isBusy = true;
1203
1204 wxString url = evt[1].GetString();
1205 wxString target = evt[3].GetString();
1206
1207 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
1208 GetId(), url, target);
1209
1210 //skip empty javascript events.
1211 if(url == "javascript:\"\"" && target.IsEmpty())
1212 {
1213 event.Veto();
1214 }
1215 else
1216 {
1217 event.SetEventObject(this);
1218 HandleWindowEvent(event);
1219 }
1220
1221 if (!event.IsAllowed())
1222 {
1223 wxActiveXEventNativeMSW* nativeParams =
1224 evt.GetNativeParameters();
1225 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[0]) = VARIANT_TRUE;
1226 }
1227
1228 // at this point, either the navigation event has been cancelled
1229 // and we're not busy, either it was accepted and IWebBrowser2's
1230 // Busy property will be true; so we don't need our override
1231 // flag anymore.
1232 m_isBusy = false;
1233
1234 break;
1235 }
1236
1237 case DISPID_NAVIGATECOMPLETE2:
1238 {
1239 wxString url = evt[1].GetString();
1240 // TODO: set target parameter if possible
1241 wxString target = wxEmptyString;
1242 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
1243 GetId(), url, target);
1244 event.SetEventObject(this);
1245 HandleWindowEvent(event);
1246 break;
1247 }
1248
1249 case DISPID_PROGRESSCHANGE:
1250 {
1251 // download progress
1252 break;
1253 }
1254
1255 case DISPID_DOCUMENTCOMPLETE:
1256 {
1257 //Only send a complete even if we are actually finished, this brings
1258 //the event in to line with webkit
1259 READYSTATE rs;
1260 m_webBrowser->get_ReadyState( &rs );
1261 if(rs != READYSTATE_COMPLETE)
1262 break;
1263
1264 wxString url = evt[1].GetString();
1265
1266 //As we are complete we also add to the history list, but not if the
1267 //page is not the main page, ie it is a subframe
1268 //We also have to check if we are loading a file:// url, if so we
1269 //need to change the comparison as ie passes back a different style
1270 //of url
1271 if(m_historyEnabled && !m_historyLoadingFromList &&
1272 (url == GetCurrentURL() ||
1273 (GetCurrentURL().substr(0, 4) == "file" &&
1274 wxFileSystem::URLToFileName(GetCurrentURL()).GetFullPath() == url)))
1275 {
1276 //If we are not at the end of the list, then erase everything
1277 //between us and the end before adding the new page
1278 if(m_historyPosition != static_cast<int>(m_historyList.size()) - 1)
1279 {
1280 m_historyList.erase(m_historyList.begin() + m_historyPosition + 1,
1281 m_historyList.end());
1282 }
1283 wxSharedPtr<wxWebViewHistoryItem> item(new wxWebViewHistoryItem(url, GetCurrentTitle()));
1284 m_historyList.push_back(item);
1285 m_historyPosition++;
1286 }
1287 //Reset as we are done now
1288 m_historyLoadingFromList = false;
1289 //Reset the find values.
1290 FindClear();
1291 // TODO: set target parameter if possible
1292 wxString target = wxEmptyString;
1293 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED, GetId(),
1294 url, target);
1295 event.SetEventObject(this);
1296 HandleWindowEvent(event);
1297 break;
1298 }
1299
1300 case DISPID_STATUSTEXTCHANGE:
1301 {
1302 break;
1303 }
1304
1305 case DISPID_TITLECHANGE:
1306 {
1307 wxString title = evt[0].GetString();
1308
1309 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
1310 GetId(), GetCurrentURL(), "");
1311 event.SetString(title);
1312 event.SetEventObject(this);
1313 HandleWindowEvent(event);
1314 break;
1315 }
1316
1317 case DISPID_NAVIGATEERROR:
1318 {
1319 wxWebViewNavigationError errorType = wxWEB_NAV_ERR_OTHER;
1320 wxString errorCode = "?";
1321 switch (evt[3].GetLong())
1322 {
1323 case INET_E_INVALID_URL: // (0x800C0002L or -2146697214)
1324 errorCode = "INET_E_INVALID_URL";
1325 errorType = wxWEB_NAV_ERR_REQUEST;
1326 break;
1327 case INET_E_NO_SESSION: // (0x800C0003L or -2146697213)
1328 errorCode = "INET_E_NO_SESSION";
1329 errorType = wxWEB_NAV_ERR_CONNECTION;
1330 break;
1331 case INET_E_CANNOT_CONNECT: // (0x800C0004L or -2146697212)
1332 errorCode = "INET_E_CANNOT_CONNECT";
1333 errorType = wxWEB_NAV_ERR_CONNECTION;
1334 break;
1335 case INET_E_RESOURCE_NOT_FOUND: // (0x800C0005L or -2146697211)
1336 errorCode = "INET_E_RESOURCE_NOT_FOUND";
1337 errorType = wxWEB_NAV_ERR_NOT_FOUND;
1338 break;
1339 case INET_E_OBJECT_NOT_FOUND: // (0x800C0006L or -2146697210)
1340 errorCode = "INET_E_OBJECT_NOT_FOUND";
1341 errorType = wxWEB_NAV_ERR_NOT_FOUND;
1342 break;
1343 case INET_E_DATA_NOT_AVAILABLE: // (0x800C0007L or -2146697209)
1344 errorCode = "INET_E_DATA_NOT_AVAILABLE";
1345 errorType = wxWEB_NAV_ERR_NOT_FOUND;
1346 break;
1347 case INET_E_DOWNLOAD_FAILURE: // (0x800C0008L or -2146697208)
1348 errorCode = "INET_E_DOWNLOAD_FAILURE";
1349 errorType = wxWEB_NAV_ERR_CONNECTION;
1350 break;
1351 case INET_E_AUTHENTICATION_REQUIRED: // (0x800C0009L or -2146697207)
1352 errorCode = "INET_E_AUTHENTICATION_REQUIRED";
1353 errorType = wxWEB_NAV_ERR_AUTH;
1354 break;
1355 case INET_E_NO_VALID_MEDIA: // (0x800C000AL or -2146697206)
1356 errorCode = "INET_E_NO_VALID_MEDIA";
1357 errorType = wxWEB_NAV_ERR_REQUEST;
1358 break;
1359 case INET_E_CONNECTION_TIMEOUT: // (0x800C000BL or -2146697205)
1360 errorCode = "INET_E_CONNECTION_TIMEOUT";
1361 errorType = wxWEB_NAV_ERR_CONNECTION;
1362 break;
1363 case INET_E_INVALID_REQUEST: // (0x800C000CL or -2146697204)
1364 errorCode = "INET_E_INVALID_REQUEST";
1365 errorType = wxWEB_NAV_ERR_REQUEST;
1366 break;
1367 case INET_E_UNKNOWN_PROTOCOL: // (0x800C000DL or -2146697203)
1368 errorCode = "INET_E_UNKNOWN_PROTOCOL";
1369 errorType = wxWEB_NAV_ERR_REQUEST;
1370 break;
1371 case INET_E_SECURITY_PROBLEM: // (0x800C000EL or -2146697202)
1372 errorCode = "INET_E_SECURITY_PROBLEM";
1373 errorType = wxWEB_NAV_ERR_SECURITY;
1374 break;
1375 case INET_E_CANNOT_LOAD_DATA: // (0x800C000FL or -2146697201)
1376 errorCode = "INET_E_CANNOT_LOAD_DATA";
1377 errorType = wxWEB_NAV_ERR_OTHER;
1378 break;
1379 case INET_E_CANNOT_INSTANTIATE_OBJECT:
1380 // CoCreateInstance will return an error code if this happens,
1381 // we'll handle this above.
1382 return;
1383 break;
1384 case INET_E_REDIRECT_FAILED: // (0x800C0014L or -2146697196)
1385 errorCode = "INET_E_REDIRECT_FAILED";
1386 errorType = wxWEB_NAV_ERR_OTHER;
1387 break;
1388 case INET_E_REDIRECT_TO_DIR: // (0x800C0015L or -2146697195)
1389 errorCode = "INET_E_REDIRECT_TO_DIR";
1390 errorType = wxWEB_NAV_ERR_REQUEST;
1391 break;
1392 case INET_E_CANNOT_LOCK_REQUEST: // (0x800C0016L or -2146697194)
1393 errorCode = "INET_E_CANNOT_LOCK_REQUEST";
1394 errorType = wxWEB_NAV_ERR_OTHER;
1395 break;
1396 case INET_E_USE_EXTEND_BINDING: // (0x800C0017L or -2146697193)
1397 errorCode = "INET_E_USE_EXTEND_BINDING";
1398 errorType = wxWEB_NAV_ERR_OTHER;
1399 break;
1400 case INET_E_TERMINATED_BIND: // (0x800C0018L or -2146697192)
1401 errorCode = "INET_E_TERMINATED_BIND";
1402 errorType = wxWEB_NAV_ERR_OTHER;
1403 break;
1404 case INET_E_INVALID_CERTIFICATE: // (0x800C0019L or -2146697191)
1405 errorCode = "INET_E_INVALID_CERTIFICATE";
1406 errorType = wxWEB_NAV_ERR_CERTIFICATE;
1407 break;
1408 case INET_E_CODE_DOWNLOAD_DECLINED: // (0x800C0100L or -2146696960)
1409 errorCode = "INET_E_CODE_DOWNLOAD_DECLINED";
1410 errorType = wxWEB_NAV_ERR_USER_CANCELLED;
1411 break;
1412 case INET_E_RESULT_DISPATCHED: // (0x800C0200L or -2146696704)
1413 // cancel request cancelled...
1414 errorCode = "INET_E_RESULT_DISPATCHED";
1415 errorType = wxWEB_NAV_ERR_OTHER;
1416 break;
1417 case INET_E_CANNOT_REPLACE_SFP_FILE: // (0x800C0300L or -2146696448)
1418 errorCode = "INET_E_CANNOT_REPLACE_SFP_FILE";
1419 errorType = wxWEB_NAV_ERR_SECURITY;
1420 break;
1421 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY:
1422 errorCode = "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
1423 errorType = wxWEB_NAV_ERR_SECURITY;
1424 break;
1425 case INET_E_CODE_INSTALL_SUPPRESSED:
1426 errorCode = "INET_E_CODE_INSTALL_SUPPRESSED";
1427 errorType = wxWEB_NAV_ERR_SECURITY;
1428 break;
1429 }
1430
1431 wxString url = evt[1].GetString();
1432 wxString target = evt[2].GetString();
1433 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, GetId(),
1434 url, target);
1435 event.SetEventObject(this);
1436 event.SetInt(errorType);
1437 event.SetString(errorCode);
1438 HandleWindowEvent(event);
1439 break;
1440 }
1441 case DISPID_NEWWINDOW3:
1442 {
1443 wxString url = evt[4].GetString();
1444
1445 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
1446 GetId(), url, wxEmptyString);
1447 event.SetEventObject(this);
1448 HandleWindowEvent(event);
1449
1450 //We always cancel this event otherwise an Internet Exporer window
1451 //is opened for the url
1452 wxActiveXEventNativeMSW* nativeParams = evt.GetNativeParameters();
1453 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[3]) = VARIANT_TRUE;
1454 break;
1455 }
1456 }
1457
1458 evt.Skip();
1459 }
1460
1461 VirtualProtocol::VirtualProtocol(wxSharedPtr<wxWebViewHandler> handler)
1462 {
1463 m_file = NULL;
1464 m_handler = handler;
1465 }
1466
1467 BEGIN_IID_TABLE(VirtualProtocol)
1468 ADD_IID(Unknown)
1469 ADD_RAW_IID(wxIID_IInternetProtocolRoot)
1470 ADD_RAW_IID(wxIID_IInternetProtocol)
1471 END_IID_TABLE;
1472
1473 IMPLEMENT_IUNKNOWN_METHODS(VirtualProtocol)
1474
1475 HRESULT STDMETHODCALLTYPE VirtualProtocol::Start(LPCWSTR szUrl, wxIInternetProtocolSink *pOIProtSink,
1476 wxIInternetBindInfo *pOIBindInfo, DWORD grfPI,
1477 HANDLE_PTR dwReserved)
1478 {
1479 wxUnusedVar(szUrl);
1480 wxUnusedVar(pOIBindInfo);
1481 wxUnusedVar(grfPI);
1482 wxUnusedVar(dwReserved);
1483 m_protocolSink = pOIProtSink;
1484
1485 //We get the file itself from the protocol handler
1486 m_file = m_handler->GetFile(szUrl);
1487
1488
1489 if(!m_file)
1490 return INET_E_RESOURCE_NOT_FOUND;
1491
1492 //We return the stream length for current and total size as we can always
1493 //read the whole file from the stream
1494 wxFileOffset length = m_file->GetStream()->GetLength();
1495 m_protocolSink->ReportData(wxBSCF_FIRSTDATANOTIFICATION |
1496 wxBSCF_DATAFULLYAVAILABLE |
1497 wxBSCF_LASTDATANOTIFICATION,
1498 length, length);
1499 return S_OK;
1500 }
1501
1502 HRESULT STDMETHODCALLTYPE VirtualProtocol::Read(void *pv, ULONG cb, ULONG *pcbRead)
1503 {
1504 //If the file is null we return false to indicte it is finished
1505 if(!m_file)
1506 return S_FALSE;
1507
1508 wxStreamError err = m_file->GetStream()->Read(pv, cb).GetLastError();
1509 *pcbRead = m_file->GetStream()->LastRead();
1510
1511 if(err == wxSTREAM_NO_ERROR)
1512 {
1513 if(*pcbRead < cb)
1514 {
1515 wxDELETE(m_file);
1516 m_protocolSink->ReportResult(S_OK, 0, NULL);
1517 }
1518 //As we are not eof there is more data
1519 return S_OK;
1520 }
1521 else if(err == wxSTREAM_EOF)
1522 {
1523 wxDELETE(m_file);
1524 m_protocolSink->ReportResult(S_OK, 0, NULL);
1525 //We are eof and so finished
1526 return S_OK;
1527 }
1528 else if(err == wxSTREAM_READ_ERROR)
1529 {
1530 wxDELETE(m_file);
1531 return INET_E_DOWNLOAD_FAILURE;
1532 }
1533 else
1534 {
1535 //Dummy return to surpress a compiler warning
1536 wxFAIL;
1537 return INET_E_DOWNLOAD_FAILURE;
1538 }
1539 }
1540
1541 BEGIN_IID_TABLE(ClassFactory)
1542 ADD_IID(Unknown)
1543 ADD_IID(ClassFactory)
1544 END_IID_TABLE;
1545
1546 IMPLEMENT_IUNKNOWN_METHODS(ClassFactory)
1547
1548 HRESULT STDMETHODCALLTYPE ClassFactory::CreateInstance(IUnknown* pUnkOuter, REFIID riid,
1549 void ** ppvObject)
1550 {
1551 if (pUnkOuter)
1552 return CLASS_E_NOAGGREGATION;
1553 VirtualProtocol* vp = new VirtualProtocol(m_handler);
1554 vp->AddRef();
1555 HRESULT hr = vp->QueryInterface(riid, ppvObject);
1556 vp->Release();
1557 return hr;
1558
1559 }
1560
1561 STDMETHODIMP ClassFactory::LockServer(BOOL fLock)
1562 {
1563 wxUnusedVar(fLock);
1564 return S_OK;
1565 }
1566
1567 wxIEContainer::wxIEContainer(wxWindow *parent, REFIID iid, IUnknown *pUnk,
1568 DocHostUIHandler* uiHandler) :
1569 wxActiveXContainer(parent,iid,pUnk)
1570 {
1571 m_uiHandler = uiHandler;
1572 }
1573
1574 wxIEContainer::~wxIEContainer()
1575 {
1576 }
1577
1578 bool wxIEContainer::QueryClientSiteInterface(REFIID iid, void **_interface,
1579 const char *&desc)
1580 {
1581 if (m_uiHandler && IsEqualIID(iid, wxIID_IDocHostUIHandler))
1582 {
1583 *_interface = (IUnknown *) (wxIDocHostUIHandler *) m_uiHandler;
1584 desc = "IDocHostUIHandler";
1585 return true;
1586 }
1587 return false;
1588 }
1589
1590 HRESULT wxSTDCALL DocHostUIHandler::ShowContextMenu(DWORD dwID, POINT *ppt,
1591 IUnknown *pcmdtReserved,
1592 IDispatch *pdispReserved)
1593 {
1594 wxUnusedVar(dwID);
1595 wxUnusedVar(ppt);
1596 wxUnusedVar(pcmdtReserved);
1597 wxUnusedVar(pdispReserved);
1598 return E_NOTIMPL;
1599 }
1600
1601 HRESULT wxSTDCALL DocHostUIHandler::GetHostInfo(DOCHOSTUIINFO *pInfo)
1602 {
1603 //don't show 3d border and enable themes.
1604 pInfo->dwFlags = pInfo->dwFlags | DOCHOSTUIFLAG_NO3DBORDER | DOCHOSTUIFLAG_THEME;
1605 return S_OK;
1606 }
1607
1608 HRESULT wxSTDCALL DocHostUIHandler::ShowUI(DWORD dwID,
1609 IOleInPlaceActiveObject *pActiveObject,
1610 IOleCommandTarget *pCommandTarget,
1611 IOleInPlaceFrame *pFrame,
1612 IOleInPlaceUIWindow *pDoc)
1613 {
1614 wxUnusedVar(dwID);
1615 wxUnusedVar(pActiveObject);
1616 wxUnusedVar(pCommandTarget);
1617 wxUnusedVar(pFrame);
1618 wxUnusedVar(pDoc);
1619 return S_FALSE;
1620 }
1621
1622 HRESULT wxSTDCALL DocHostUIHandler::HideUI(void)
1623 {
1624 return E_NOTIMPL;
1625 }
1626
1627 HRESULT wxSTDCALL DocHostUIHandler::UpdateUI(void)
1628 {
1629 return E_NOTIMPL;
1630 }
1631
1632 HRESULT wxSTDCALL DocHostUIHandler::EnableModeless(BOOL fEnable)
1633 {
1634 wxUnusedVar(fEnable);
1635 return E_NOTIMPL;
1636 }
1637
1638 HRESULT wxSTDCALL DocHostUIHandler::OnDocWindowActivate(BOOL fActivate)
1639 {
1640 wxUnusedVar(fActivate);
1641 return E_NOTIMPL;
1642 }
1643
1644 HRESULT wxSTDCALL DocHostUIHandler::OnFrameWindowActivate(BOOL fActivate)
1645 {
1646 wxUnusedVar(fActivate);
1647 return E_NOTIMPL;
1648 }
1649
1650 HRESULT wxSTDCALL DocHostUIHandler::ResizeBorder(LPCRECT prcBorder,
1651 IOleInPlaceUIWindow *pUIWindow,
1652 BOOL fFrameWindow)
1653 {
1654 wxUnusedVar(prcBorder);
1655 wxUnusedVar(pUIWindow);
1656 wxUnusedVar(fFrameWindow);
1657 return E_NOTIMPL;
1658 }
1659
1660 HRESULT wxSTDCALL DocHostUIHandler::TranslateAccelerator(LPMSG lpMsg,
1661 const GUID *pguidCmdGroup,
1662 DWORD nCmdID)
1663 {
1664 if(lpMsg && lpMsg->message == WM_KEYDOWN)
1665 {
1666 //control is down?
1667 if((GetKeyState(VK_CONTROL) & 0x8000 ))
1668 {
1669 //skip the accelerators used by the control
1670 switch(lpMsg->wParam)
1671 {
1672 case 'F':
1673 case 'L':
1674 case 'N':
1675 case 'O':
1676 case 'P':
1677 return S_OK;
1678 }
1679 }
1680 //skip F5
1681 if(lpMsg->wParam == VK_F5)
1682 {
1683 return S_OK;
1684 }
1685 }
1686
1687 wxUnusedVar(pguidCmdGroup);
1688 wxUnusedVar(nCmdID);
1689 return E_NOTIMPL;
1690 }
1691
1692 HRESULT wxSTDCALL DocHostUIHandler::GetOptionKeyPath(LPOLESTR *pchKey,DWORD dw)
1693 {
1694 wxUnusedVar(pchKey);
1695 wxUnusedVar(dw);
1696 return E_NOTIMPL;
1697 }
1698
1699 HRESULT wxSTDCALL DocHostUIHandler::GetDropTarget(IDropTarget *pDropTarget,
1700 IDropTarget **ppDropTarget)
1701 {
1702 wxUnusedVar(pDropTarget);
1703 wxUnusedVar(ppDropTarget);
1704 return E_NOTIMPL;
1705 }
1706
1707 HRESULT wxSTDCALL DocHostUIHandler::GetExternal(IDispatch **ppDispatch)
1708 {
1709 wxUnusedVar(ppDispatch);
1710 return E_NOTIMPL;
1711 }
1712
1713 HRESULT wxSTDCALL DocHostUIHandler::TranslateUrl(DWORD dwTranslate,
1714 OLECHAR *pchURLIn,
1715 OLECHAR **ppchURLOut)
1716 {
1717 wxUnusedVar(dwTranslate);
1718 wxUnusedVar(pchURLIn);
1719 wxUnusedVar(ppchURLOut);
1720 return E_NOTIMPL;
1721 }
1722
1723 HRESULT wxSTDCALL DocHostUIHandler::FilterDataObject(IDataObject *pDO, IDataObject **ppDORet)
1724 {
1725 wxUnusedVar(pDO);
1726 wxUnusedVar(ppDORet);
1727 return E_NOTIMPL;
1728 }
1729
1730 BEGIN_IID_TABLE(DocHostUIHandler)
1731 ADD_IID(Unknown)
1732 ADD_RAW_IID(wxIID_IDocHostUIHandler)
1733 END_IID_TABLE;
1734
1735 IMPLEMENT_IUNKNOWN_METHODS(DocHostUIHandler)
1736
1737 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE