]> git.saurik.com Git - wxWidgets.git/blame - src/msw/webview_ie.cpp
Remove some out of date comments.
[wxWidgets.git] / src / msw / webview_ie.cpp
CommitLineData
61b98a2d 1/////////////////////////////////////////////////////////////////////////////
8290e3cd 2// Name: src/msw/webview_ie.cpp
61b98a2d
SL
3// Purpose: wxMSW wxWebViewIE class implementation for web view component
4// Author: Marianne Gagnon
5// Id: $Id$
153530af 6// Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton
61b98a2d
SL
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
384b8d9f
SL
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
8290e3cd 17#include "wx/msw/webview_ie.h"
61b98a2d 18
66f2aa61 19#if wxUSE_WEBVIEW_IE
61b98a2d
SL
20
21#include <olectl.h>
22#include <oleidl.h>
23#include <exdispid.h>
24#include <exdisp.h>
25#include <mshtml.h>
cd4e4673 26#include "wx/msw/registry.h"
1d7d04d7 27#include "wx/msw/missing.h"
7d3f6b4d
SL
28#include "wx/filesys.h"
29
cddf4541
SL
30wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewIE, wxWebView);
31
aa35b9ca
SL
32//We link to urlmon as it is required for CoInternetGetSession
33#pragma comment(lib, "urlmon")
34
61b98a2d 35BEGIN_EVENT_TABLE(wxWebViewIE, wxControl)
97ad1425
SL
36 EVT_ACTIVEX(wxID_ANY, wxWebViewIE::onActiveXEvent)
37 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg)
61b98a2d
SL
38END_EVENT_TABLE()
39
40bool wxWebViewIE::Create(wxWindow* parent,
41 wxWindowID id,
42 const wxString& url,
43 const wxPoint& pos,
44 const wxSize& size,
45 long style,
46 const wxString& name)
47{
48 if (!wxControl::Create(parent, id, pos, size, style,
49 wxDefaultValidator, name))
50 {
51 return false;
52 }
53
54 m_webBrowser = NULL;
61b98a2d 55 m_isBusy = false;
74af0b13
SL
56 m_historyLoadingFromList = false;
57 m_historyEnabled = true;
58 m_historyPosition = -1;
c5f417cb 59 m_zoomType = wxWEB_VIEW_ZOOM_TYPE_TEXT;
61b98a2d
SL
60
61 if (::CoCreateInstance(CLSID_WebBrowser, NULL,
62 CLSCTX_INPROC_SERVER, // CLSCTX_INPROC,
63 IID_IWebBrowser2 , (void**)&m_webBrowser) != 0)
64 {
65 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
66 return false;
67 }
68
69 m_ie.SetDispatchPtr(m_webBrowser); // wxAutomationObject will release itself
70
71 m_webBrowser->put_RegisterAsBrowser(VARIANT_TRUE);
72 m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
61b98a2d
SL
73
74 m_container = new wxActiveXContainer(this, IID_IWebBrowser2, m_webBrowser);
75
76 SetBackgroundStyle(wxBG_STYLE_PAINT);
77 SetDoubleBuffered(true);
9ef101cd 78 LoadUrl(url);
61b98a2d
SL
79 return true;
80}
81
82
83void wxWebViewIE::LoadUrl(const wxString& url)
84{
7fbc727b 85 m_ie.CallMethod("Navigate", (BSTR) url.wc_str(), NULL, NULL, NULL, NULL);
61b98a2d
SL
86}
87
442262d4 88void wxWebViewIE::SetPage(const wxString& html, const wxString& baseUrl)
61b98a2d 89{
61b98a2d
SL
90 BSTR bstr = SysAllocString(html.wc_str());
91
92 // Creates a new one-dimensional array
93 SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
94 if (psaStrings != NULL)
95 {
96 VARIANT *param;
442262d4 97
61b98a2d
SL
98 HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
99 param->vt = VT_BSTR;
100 param->bstrVal = bstr;
61b98a2d 101 hr = SafeArrayUnaccessData(psaStrings);
442262d4 102
617227c3 103 IHTMLDocument2* document = GetDocument();
61b98a2d 104 document->write(psaStrings);
442262d4 105 document->Release();
61b98a2d
SL
106
107 // SafeArrayDestroy calls SysFreeString for each BSTR
108 SafeArrayDestroy(psaStrings);
442262d4
SL
109
110 //We send the events when we are done to mimic webkit
111 //Navigated event
112 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
113 GetId(), baseUrl, "", false);
114 event.SetEventObject(this);
115 HandleWindowEvent(event);
116
117 //Document complete event
118 event.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED);
119 event.SetEventObject(this);
120 HandleWindowEvent(event);
61b98a2d
SL
121 }
122 else
123 {
124 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
125 }
126
127}
128
129wxString wxWebViewIE::GetPageSource()
130{
617227c3 131 IHTMLDocument2* document = GetDocument();
61b98a2d
SL
132 IHTMLElement *bodyTag = NULL;
133 IHTMLElement *htmlTag = NULL;
423adfde 134 wxString source;
7fbc727b
SL
135 HRESULT hr = document->get_body(&bodyTag);
136 if(SUCCEEDED(hr))
137 {
138 hr = bodyTag->get_parentElement(&htmlTag);
139 if(SUCCEEDED(hr))
140 {
423adfde 141 BSTR bstr;
7fbc727b 142 htmlTag->get_outerHTML(&bstr);
423adfde 143 source = wxString(bstr);
7fbc727b
SL
144 htmlTag->Release();
145 }
146 bodyTag->Release();
147 }
61b98a2d
SL
148
149 document->Release();
423adfde 150 return source;
61b98a2d
SL
151}
152
61b98a2d
SL
153wxWebViewZoom wxWebViewIE::GetZoom()
154{
c5f417cb
SL
155 if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
156 return GetIEOpticalZoom();
157 else if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT)
158 return GetIETextZoom();
159 else
160 wxFAIL;
423adfde
SL
161
162 //Dummy return to stop compiler warnings
163 return wxWEB_VIEW_ZOOM_MEDIUM;
1d7d04d7 164
61b98a2d 165}
c5f417cb 166
61b98a2d
SL
167void wxWebViewIE::SetZoom(wxWebViewZoom zoom)
168{
c5f417cb
SL
169 if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
170 SetIEOpticalZoom(zoom);
171 else if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT)
172 SetIETextZoom(zoom);
173 else
174 wxFAIL;
61b98a2d
SL
175}
176
c5f417cb 177void wxWebViewIE::SetIETextZoom(wxWebViewZoom level)
61b98a2d 178{
1d7d04d7 179 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
c5f417cb
SL
180 //is 0 to 4 so the check is unnecessary, these match exactly with the
181 //enum values
61b98a2d
SL
182 VARIANT zoomVariant;
183 VariantInit (&zoomVariant);
184 V_VT(&zoomVariant) = VT_I4;
185 V_I4(&zoomVariant) = level;
186
187 HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM,
188 OLECMDEXECOPT_DONTPROMPTUSER,
189 &zoomVariant, NULL);
c5f417cb 190 wxASSERT(result == S_OK);
61b98a2d
SL
191}
192
c5f417cb 193wxWebViewZoom wxWebViewIE::GetIETextZoom()
61b98a2d
SL
194{
195 VARIANT zoomVariant;
196 VariantInit (&zoomVariant);
197 V_VT(&zoomVariant) = VT_I4;
61b98a2d
SL
198
199 HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM,
200 OLECMDEXECOPT_DONTPROMPTUSER,
201 NULL, &zoomVariant);
c5f417cb 202 wxASSERT(result == S_OK);
61b98a2d 203
c5f417cb
SL
204 //We can safely cast here as we know that the range matches our enum
205 return static_cast<wxWebViewZoom>(V_I4(&zoomVariant));
61b98a2d
SL
206}
207
c5f417cb 208void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level)
61b98a2d 209{
1d7d04d7 210 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
c5f417cb 211 //is 10 to 1000 so the check is unnecessary
61b98a2d
SL
212 VARIANT zoomVariant;
213 VariantInit (&zoomVariant);
214 V_VT(&zoomVariant) = VT_I4;
c5f417cb
SL
215
216 //We make a somewhat arbitray map here, taken from values used by webkit
217 switch(level)
218 {
219 case wxWEB_VIEW_ZOOM_TINY:
220 V_I4(&zoomVariant) = 60;
221 break;
222 case wxWEB_VIEW_ZOOM_SMALL:
223 V_I4(&zoomVariant) = 80;
224 break;
225 case wxWEB_VIEW_ZOOM_MEDIUM:
226 V_I4(&zoomVariant) = 100;
227 break;
228 case wxWEB_VIEW_ZOOM_LARGE:
229 V_I4(&zoomVariant) = 130;
230 break;
231 case wxWEB_VIEW_ZOOM_LARGEST:
232 V_I4(&zoomVariant) = 160;
233 break;
234 default:
235 wxFAIL;
236 }
61b98a2d
SL
237
238 HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM,
239 OLECMDEXECOPT_DODEFAULT,
240 &zoomVariant,
241 NULL);
c5f417cb 242 wxASSERT(result == S_OK);
61b98a2d
SL
243}
244
c5f417cb 245wxWebViewZoom wxWebViewIE::GetIEOpticalZoom()
61b98a2d 246{
61b98a2d
SL
247 VARIANT zoomVariant;
248 VariantInit (&zoomVariant);
249 V_VT(&zoomVariant) = VT_I4;
61b98a2d
SL
250
251 HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM,
252 OLECMDEXECOPT_DODEFAULT, NULL,
253 &zoomVariant);
c5f417cb 254 wxASSERT(result == S_OK);
61b98a2d
SL
255
256 const int zoom = V_I4(&zoomVariant);
61b98a2d 257
c5f417cb
SL
258 //We make a somewhat arbitray map here, taken from values used by webkit
259 if (zoom <= 65)
260 {
261 return wxWEB_VIEW_ZOOM_TINY;
262 }
263 else if (zoom > 65 && zoom <= 90)
264 {
265 return wxWEB_VIEW_ZOOM_SMALL;
266 }
267 else if (zoom > 90 && zoom <= 115)
268 {
269 return wxWEB_VIEW_ZOOM_MEDIUM;
270 }
271 else if (zoom > 115 && zoom <= 145)
272 {
273 return wxWEB_VIEW_ZOOM_LARGE;
274 }
423adfde 275 else /*if (zoom > 145) */ //Using else removes a compiler warning
c5f417cb
SL
276 {
277 return wxWEB_VIEW_ZOOM_LARGEST;
278 }
61b98a2d
SL
279}
280
c5f417cb 281void wxWebViewIE::SetZoomType(wxWebViewZoomType type)
61b98a2d 282{
c5f417cb 283 m_zoomType = type;
61b98a2d
SL
284}
285
286wxWebViewZoomType wxWebViewIE::GetZoomType() const
287{
c5f417cb 288 return m_zoomType;
61b98a2d
SL
289}
290
cd4e4673 291bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type) const
61b98a2d 292{
cd4e4673
SL
293 //IE 6 and below only support text zoom, so check the registry to see what
294 //version we actually have
295 wxRegKey key(wxRegKey::HKLM, "Software\\Microsoft\\Internet Explorer");
296 wxString value;
297 key.QueryValue("Version", value);
298
299 long version = wxAtoi(value.Left(1));
300 if(version <= 6 && type == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
301 return false;
302 else
303 return true;
61b98a2d
SL
304}
305
306void wxWebViewIE::Print()
307{
308 m_webBrowser->ExecWB(OLECMDID_PRINTPREVIEW,
309 OLECMDEXECOPT_DODEFAULT, NULL, NULL);
310}
311
74af0b13 312bool wxWebViewIE::CanGoBack()
61b98a2d 313{
74af0b13
SL
314 if(m_historyEnabled)
315 return m_historyPosition > 0;
316 else
317 return false;
318}
61b98a2d 319
74af0b13
SL
320bool wxWebViewIE::CanGoForward()
321{
322 if(m_historyEnabled)
22ca10fa 323 return m_historyPosition != static_cast<int>(m_historyList.size()) - 1;
74af0b13
SL
324 else
325 return false;
61b98a2d
SL
326}
327
3e7968c2 328void wxWebViewIE::LoadHistoryItem(wxSharedPtr<wxWebHistoryItem> item)
61b98a2d 329{
74af0b13
SL
330 int pos = -1;
331 for(unsigned int i = 0; i < m_historyList.size(); i++)
332 {
3e7968c2
SL
333 //We compare the actual pointers to find the correct item
334 if(m_historyList[i].get() == item.get())
74af0b13
SL
335 pos = i;
336 }
1d7d04d7 337 wxASSERT_MSG(pos != static_cast<int>(m_historyList.size()),
22ca10fa 338 "invalid history item");
74af0b13
SL
339 m_historyLoadingFromList = true;
340 LoadUrl(item->GetUrl());
341 m_historyPosition = pos;
342}
61b98a2d 343
5cbda74b
SL
344wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewIE::GetBackwardHistory()
345{
346 wxVector<wxSharedPtr<wxWebHistoryItem> > backhist;
347 //As we don't have std::copy or an iterator constructor in the wxwidgets
348 //native vector we construct it by hand
349 for(int i = 0; i < m_historyPosition; i++)
350 {
351 backhist.push_back(m_historyList[i]);
352 }
353 return backhist;
354}
355
356wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewIE::GetForwardHistory()
357{
358 wxVector<wxSharedPtr<wxWebHistoryItem> > forwardhist;
359 //As we don't have std::copy or an iterator constructor in the wxwidgets
360 //native vector we construct it by hand
22ca10fa 361 for(int i = m_historyPosition + 1; i < static_cast<int>(m_historyList.size()); i++)
5cbda74b
SL
362 {
363 forwardhist.push_back(m_historyList[i]);
364 }
365 return forwardhist;
366}
367
74af0b13
SL
368void wxWebViewIE::GoBack()
369{
3e7968c2 370 LoadHistoryItem(m_historyList[m_historyPosition - 1]);
74af0b13
SL
371}
372
373void wxWebViewIE::GoForward()
374{
3e7968c2 375 LoadHistoryItem(m_historyList[m_historyPosition + 1]);
61b98a2d
SL
376}
377
378void wxWebViewIE::Stop()
379{
7fbc727b 380 m_ie.CallMethod("Stop");
61b98a2d
SL
381}
382
74af0b13
SL
383void wxWebViewIE::ClearHistory()
384{
385 m_historyList.clear();
386 m_historyPosition = -1;
387}
388
389void wxWebViewIE::EnableHistory(bool enable)
390{
391 m_historyEnabled = enable;
392 m_historyList.clear();
393 m_historyPosition = -1;
394}
61b98a2d
SL
395
396void wxWebViewIE::Reload(wxWebViewReloadFlags flags)
397{
7aa18fc7
SL
398 VARIANTARG level;
399 VariantInit(&level);
400 V_VT(&level) = VT_I2;
61b98a2d 401
7aa18fc7 402 switch(flags)
61b98a2d 403 {
7aa18fc7
SL
404 case wxWEB_VIEW_RELOAD_DEFAULT:
405 V_I2(&level) = REFRESH_NORMAL;
406 break;
407 case wxWEB_VIEW_RELOAD_NO_CACHE:
408 V_I2(&level) = REFRESH_COMPLETELY;
409 break;
410 default:
411 wxFAIL_MSG("Unexpected reload type");
61b98a2d
SL
412 }
413
7aa18fc7 414 m_webBrowser->Refresh2(&level);
61b98a2d
SL
415}
416
417bool wxWebViewIE::IsOfflineMode()
418{
419 wxVariant out = m_ie.GetProperty("Offline");
420
421 wxASSERT(out.GetType() == "bool");
422
423 return out.GetBool();
424}
425
426void wxWebViewIE::SetOfflineMode(bool offline)
427{
428 // FIXME: the wxWidgets docs do not really document what the return
429 // parameter of PutProperty is
430 const bool success = m_ie.PutProperty("Offline", (offline ?
431 VARIANT_TRUE :
432 VARIANT_FALSE));
433 wxASSERT(success);
434}
435
436bool wxWebViewIE::IsBusy()
437{
438 if (m_isBusy) return true;
439
440 wxVariant out = m_ie.GetProperty("Busy");
441
442 wxASSERT(out.GetType() == "bool");
443
444 return out.GetBool();
445}
446
447wxString wxWebViewIE::GetCurrentURL()
448{
449 wxVariant out = m_ie.GetProperty("LocationURL");
450
451 wxASSERT(out.GetType() == "string");
452 return out.GetString();
453}
454
455wxString wxWebViewIE::GetCurrentTitle()
456{
617227c3 457 IHTMLDocument2* document = GetDocument();
977c5320 458 BSTR title;
7fbc727b 459
977c5320 460 document->get_nameProp(&title);
7fbc727b 461 document->Release();
977c5320 462 return wxString(title);
61b98a2d
SL
463}
464
4681a3ea
SL
465bool wxWebViewIE::CanCut()
466{
467 return CanExecCommand("Cut");
468}
469
470bool wxWebViewIE::CanCopy()
471{
472 return CanExecCommand("Copy");
473}
474bool wxWebViewIE::CanPaste()
475{
476 return CanExecCommand("Paste");
477}
478
479void wxWebViewIE::Cut()
480{
481 ExecCommand("Cut");
482}
483
484void wxWebViewIE::Copy()
485{
486 ExecCommand("Copy");
487}
488
489void wxWebViewIE::Paste()
490{
491 ExecCommand("Paste");
492}
493
97e49559
SL
494bool wxWebViewIE::CanUndo()
495{
496 return CanExecCommand("Undo");
497}
498bool wxWebViewIE::CanRedo()
499{
500 return CanExecCommand("Redo");
501}
502
503void wxWebViewIE::Undo()
504{
505 ExecCommand("Undo");
506}
507
508void wxWebViewIE::Redo()
509{
510 ExecCommand("Redo");
511}
512
c7cbe308
SL
513void wxWebViewIE::SetEditable(bool enable)
514{
515 IHTMLDocument2* document = GetDocument();
516 if( enable )
517 document->put_designMode(SysAllocString(L"On"));
518 else
519 document->put_designMode(SysAllocString(L"Off"));
7fbc727b
SL
520
521 document->Release();
c7cbe308
SL
522}
523
524bool wxWebViewIE::IsEditable()
525{
526 IHTMLDocument2* document = GetDocument();
527 BSTR mode;
528 document->get_designMode(&mode);
423adfde 529 document->Release();
c7cbe308
SL
530 if(wxString(mode) == "On")
531 return true;
532 else
533 return false;
534}
535
63a65070
SL
536void wxWebViewIE::SelectAll()
537{
538 ExecCommand("SelectAll");
539}
540
541bool wxWebViewIE::HasSelection()
542{
543 IHTMLDocument2* document = GetDocument();
544 IHTMLSelectionObject* selection;
423adfde 545 wxString sel;
7fbc727b
SL
546 HRESULT hr = document->get_selection(&selection);
547 if(SUCCEEDED(hr))
548 {
423adfde 549 BSTR type;
7fbc727b 550 selection->get_type(&type);
423adfde 551 sel = wxString(type);
7fbc727b
SL
552 selection->Release();
553 }
554 document->Release();
423adfde 555 return sel != "None";
63a65070
SL
556}
557
558void wxWebViewIE::DeleteSelection()
559{
560 ExecCommand("Delete");
561}
562
c9355a3d
SL
563wxString wxWebViewIE::GetSelectedText()
564{
565 IHTMLDocument2* document = GetDocument();
566 IHTMLSelectionObject* selection;
567 wxString selected;
568 HRESULT hr = document->get_selection(&selection);
569 if(SUCCEEDED(hr))
570 {
571 IDispatch* disrange;
572 hr = selection->createRange(&disrange);
573 if(SUCCEEDED(hr))
574 {
575 IHTMLTxtRange* range;
576 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
577 if(SUCCEEDED(hr))
578 {
579 BSTR text;
580 range->get_text(&text);
581 selected = wxString(text);
582 range->Release();
583 }
584 disrange->Release();
585 }
586 selection->Release();
587 }
588 document->Release();
589 return selected;
590}
591
97ba4d81 592wxString wxWebViewIE::GetSelectedSource()
0fe8a1b6
SL
593{
594 IHTMLDocument2* document = GetDocument();
595 IHTMLSelectionObject* selection;
596 wxString selected;
597 HRESULT hr = document->get_selection(&selection);
598 if(SUCCEEDED(hr))
599 {
600 IDispatch* disrange;
601 hr = selection->createRange(&disrange);
602 if(SUCCEEDED(hr))
603 {
604 IHTMLTxtRange* range;
605 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
606 if(SUCCEEDED(hr))
607 {
608 BSTR text;
609 range->get_htmlText(&text);
610 selected = wxString(text);
611 range->Release();
612 }
613 disrange->Release();
614 }
615 selection->Release();
616 }
617 document->Release();
618 return selected;
619}
620
41933aa5
SL
621void wxWebViewIE::ClearSelection()
622{
623 IHTMLDocument2* document = GetDocument();
624 IHTMLSelectionObject* selection;
625 wxString selected;
626 HRESULT hr = document->get_selection(&selection);
627 if(SUCCEEDED(hr))
628 {
629 selection->empty();
630 selection->Release();
631 }
632 document->Release();
633}
634
241b769f
SL
635wxString wxWebViewIE::GetPageText()
636{
637 IHTMLDocument2* document = GetDocument();
423adfde 638 wxString text;
241b769f
SL
639 IHTMLElement* body;
640 HRESULT hr = document->get_body(&body);
641 if(SUCCEEDED(hr))
642 {
423adfde 643 BSTR out;
241b769f 644 body->get_innerText(&out);
423adfde 645 text = wxString(out);
241b769f
SL
646 body->Release();
647 }
648 document->Release();
423adfde 649 return text;
241b769f
SL
650}
651
c9ccc09c
SL
652void wxWebViewIE::RunScript(const wxString& javascript)
653{
654 IHTMLDocument2* document = GetDocument();
655 IHTMLWindow2* window;
656 wxString language = "javascript";
657 HRESULT hr = document->get_parentWindow(&window);
658 if(SUCCEEDED(hr))
659 {
660 VARIANT level;
661 VariantInit(&level);
662 V_VT(&level) = VT_EMPTY;
663 window->execScript(SysAllocString(javascript), SysAllocString(language), &level);
664 }
665 document->Release();
666}
667
9e3d4a32 668void wxWebViewIE::RegisterHandler(wxWebHandler* handler)
29365629
SL
669{
670 ClassFactory* cf = new ClassFactory(handler);
671 IInternetSession* session;
672 if(FAILED(CoInternetGetSession(0, &session, 0)))
673 {
674 wxFAIL_MSG("Could not retrive internet session");
675 }
676
9e3d4a32 677 HRESULT hr = session->RegisterNameSpace(cf, CLSID_FileProtocol, handler->GetName(), 0, NULL, 0);
29365629
SL
678 if(FAILED(hr))
679 {
680 wxFAIL_MSG("Could not register protocol");
681 }
682}
683
4681a3ea
SL
684bool wxWebViewIE::CanExecCommand(wxString command)
685{
617227c3 686 IHTMLDocument2* document = GetDocument();
4681a3ea 687 VARIANT_BOOL enabled;
7fbc727b 688
4681a3ea 689 document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled);
7fbc727b 690 document->Release();
4681a3ea
SL
691
692 return (enabled == VARIANT_TRUE);
693}
694
695void wxWebViewIE::ExecCommand(wxString command)
696{
617227c3
SL
697 IHTMLDocument2* document = GetDocument();
698 document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL);
7fbc727b 699 document->Release();
617227c3 700}
4681a3ea 701
617227c3
SL
702IHTMLDocument2* wxWebViewIE::GetDocument()
703{
704 wxVariant variant = m_ie.GetProperty("Document");
705 IHTMLDocument2* document = (IHTMLDocument2*)variant.GetVoidPtr();
4681a3ea 706
617227c3
SL
707 wxASSERT(document);
708
709 return document;
4681a3ea
SL
710}
711
61b98a2d
SL
712void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
713{
714 if (m_webBrowser == NULL) return;
715
716 switch (evt.GetDispatchId())
717 {
718 case DISPID_BEFORENAVIGATE2:
719 {
720 m_isBusy = true;
721
722 wxString url = evt[1].GetString();
723 wxString target = evt[3].GetString();
724
725 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
726 GetId(), url, target, true);
727 event.SetEventObject(this);
728 HandleWindowEvent(event);
729
730 if (event.IsVetoed())
731 {
732 wxActiveXEventNativeMSW* nativeParams =
733 evt.GetNativeParameters();
734 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[0]) = VARIANT_TRUE;
735 }
736
737 // at this point, either the navigation event has been cancelled
738 // and we're not busy, either it was accepted and IWebBrowser2's
739 // Busy property will be true; so we don't need our override
740 // flag anymore.
741 m_isBusy = false;
742
743 break;
744 }
745
746 case DISPID_NAVIGATECOMPLETE2:
747 {
748 wxString url = evt[1].GetString();
749 // TODO: set target parameter if possible
750 wxString target = wxEmptyString;
751 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
752 GetId(), url, target, false);
753 event.SetEventObject(this);
754 HandleWindowEvent(event);
755 break;
756 }
757
758 case DISPID_PROGRESSCHANGE:
759 {
760 // download progress
761 break;
762 }
763
764 case DISPID_DOCUMENTCOMPLETE:
765 {
3ee442ff
SL
766 //Only send a complete even if we are actually finished, this brings
767 //the event in to line with webkit
768 READYSTATE rs;
769 m_webBrowser->get_ReadyState( &rs );
770 if(rs != READYSTATE_COMPLETE)
771 break;
772
61b98a2d 773 wxString url = evt[1].GetString();
113e0a92
SL
774
775 //As we are complete we also add to the history list, but not if the
776 //page is not the main page, ie it is a subframe
777 if(m_historyEnabled && !m_historyLoadingFromList && url == GetCurrentURL())
74af0b13
SL
778 {
779 //If we are not at the end of the list, then erase everything
780 //between us and the end before adding the new page
22ca10fa 781 if(m_historyPosition != static_cast<int>(m_historyList.size()) - 1)
74af0b13
SL
782 {
783 m_historyList.erase(m_historyList.begin() + m_historyPosition + 1,
784 m_historyList.end());
785 }
786 wxSharedPtr<wxWebHistoryItem> item(new wxWebHistoryItem(url, GetCurrentTitle()));
787 m_historyList.push_back(item);
788 m_historyPosition++;
789 }
790 //Reset as we are done now
791 m_historyLoadingFromList = false;
61b98a2d
SL
792 // TODO: set target parameter if possible
793 wxString target = wxEmptyString;
794 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED, GetId(),
795 url, target, false);
796 event.SetEventObject(this);
797 HandleWindowEvent(event);
798 break;
799 }
800
801 case DISPID_STATUSTEXTCHANGE:
802 {
803 break;
804 }
805
806 case DISPID_TITLECHANGE:
807 {
153530af
SL
808 wxString title = evt[0].GetString();
809
810 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
811 GetId(), GetCurrentURL(), wxEmptyString, true);
812 event.SetString(title);
813 event.SetEventObject(this);
814 HandleWindowEvent(event);
61b98a2d
SL
815 break;
816 }
817
818 case DISPID_NAVIGATEERROR:
819 {
820 wxWebNavigationError errorType = wxWEB_NAV_ERR_OTHER;
821 wxString errorCode = "?";
822 switch (evt[3].GetLong())
823 {
824 case INET_E_INVALID_URL: // (0x800C0002L or -2146697214)
825 errorCode = "INET_E_INVALID_URL";
826 errorType = wxWEB_NAV_ERR_REQUEST;
827 break;
828 case INET_E_NO_SESSION: // (0x800C0003L or -2146697213)
829 errorCode = "INET_E_NO_SESSION";
830 errorType = wxWEB_NAV_ERR_CONNECTION;
831 break;
832 case INET_E_CANNOT_CONNECT: // (0x800C0004L or -2146697212)
833 errorCode = "INET_E_CANNOT_CONNECT";
834 errorType = wxWEB_NAV_ERR_CONNECTION;
835 break;
836 case INET_E_RESOURCE_NOT_FOUND: // (0x800C0005L or -2146697211)
837 errorCode = "INET_E_RESOURCE_NOT_FOUND";
838 errorType = wxWEB_NAV_ERR_NOT_FOUND;
839 break;
840 case INET_E_OBJECT_NOT_FOUND: // (0x800C0006L or -2146697210)
841 errorCode = "INET_E_OBJECT_NOT_FOUND";
842 errorType = wxWEB_NAV_ERR_NOT_FOUND;
843 break;
844 case INET_E_DATA_NOT_AVAILABLE: // (0x800C0007L or -2146697209)
845 errorCode = "INET_E_DATA_NOT_AVAILABLE";
846 errorType = wxWEB_NAV_ERR_NOT_FOUND;
847 break;
848 case INET_E_DOWNLOAD_FAILURE: // (0x800C0008L or -2146697208)
849 errorCode = "INET_E_DOWNLOAD_FAILURE";
850 errorType = wxWEB_NAV_ERR_CONNECTION;
851 break;
852 case INET_E_AUTHENTICATION_REQUIRED: // (0x800C0009L or -2146697207)
853 errorCode = "INET_E_AUTHENTICATION_REQUIRED";
854 errorType = wxWEB_NAV_ERR_AUTH;
855 break;
856 case INET_E_NO_VALID_MEDIA: // (0x800C000AL or -2146697206)
857 errorCode = "INET_E_NO_VALID_MEDIA";
858 errorType = wxWEB_NAV_ERR_REQUEST;
859 break;
860 case INET_E_CONNECTION_TIMEOUT: // (0x800C000BL or -2146697205)
861 errorCode = "INET_E_CONNECTION_TIMEOUT";
862 errorType = wxWEB_NAV_ERR_CONNECTION;
863 break;
864 case INET_E_INVALID_REQUEST: // (0x800C000CL or -2146697204)
865 errorCode = "INET_E_INVALID_REQUEST";
866 errorType = wxWEB_NAV_ERR_REQUEST;
867 break;
868 case INET_E_UNKNOWN_PROTOCOL: // (0x800C000DL or -2146697203)
869 errorCode = "INET_E_UNKNOWN_PROTOCOL";
870 errorType = wxWEB_NAV_ERR_REQUEST;
871 break;
872 case INET_E_SECURITY_PROBLEM: // (0x800C000EL or -2146697202)
873 errorCode = "INET_E_SECURITY_PROBLEM";
874 errorType = wxWEB_NAV_ERR_SECURITY;
875 break;
876 case INET_E_CANNOT_LOAD_DATA: // (0x800C000FL or -2146697201)
877 errorCode = "INET_E_CANNOT_LOAD_DATA";
878 errorType = wxWEB_NAV_ERR_OTHER;
879 break;
880 case INET_E_CANNOT_INSTANTIATE_OBJECT:
881 // CoCreateInstance will return an error code if this happens,
882 // we'll handle this above.
883 return;
884 break;
885 case INET_E_REDIRECT_FAILED: // (0x800C0014L or -2146697196)
886 errorCode = "INET_E_REDIRECT_FAILED";
887 errorType = wxWEB_NAV_ERR_OTHER;
888 break;
889 case INET_E_REDIRECT_TO_DIR: // (0x800C0015L or -2146697195)
890 errorCode = "INET_E_REDIRECT_TO_DIR";
891 errorType = wxWEB_NAV_ERR_REQUEST;
892 break;
893 case INET_E_CANNOT_LOCK_REQUEST: // (0x800C0016L or -2146697194)
894 errorCode = "INET_E_CANNOT_LOCK_REQUEST";
895 errorType = wxWEB_NAV_ERR_OTHER;
896 break;
897 case INET_E_USE_EXTEND_BINDING: // (0x800C0017L or -2146697193)
898 errorCode = "INET_E_USE_EXTEND_BINDING";
899 errorType = wxWEB_NAV_ERR_OTHER;
900 break;
901 case INET_E_TERMINATED_BIND: // (0x800C0018L or -2146697192)
902 errorCode = "INET_E_TERMINATED_BIND";
903 errorType = wxWEB_NAV_ERR_OTHER;
904 break;
905 case INET_E_INVALID_CERTIFICATE: // (0x800C0019L or -2146697191)
906 errorCode = "INET_E_INVALID_CERTIFICATE";
907 errorType = wxWEB_NAV_ERR_CERTIFICATE;
908 break;
909 case INET_E_CODE_DOWNLOAD_DECLINED: // (0x800C0100L or -2146696960)
910 errorCode = "INET_E_CODE_DOWNLOAD_DECLINED";
911 errorType = wxWEB_NAV_ERR_USER_CANCELLED;
912 break;
913 case INET_E_RESULT_DISPATCHED: // (0x800C0200L or -2146696704)
914 // cancel request cancelled...
915 errorCode = "INET_E_RESULT_DISPATCHED";
916 errorType = wxWEB_NAV_ERR_OTHER;
917 break;
918 case INET_E_CANNOT_REPLACE_SFP_FILE: // (0x800C0300L or -2146696448)
919 errorCode = "INET_E_CANNOT_REPLACE_SFP_FILE";
920 errorType = wxWEB_NAV_ERR_SECURITY;
921 break;
922 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY:
923 errorCode = "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
924 errorType = wxWEB_NAV_ERR_SECURITY;
925 break;
926 case INET_E_CODE_INSTALL_SUPPRESSED:
927 errorCode = "INET_E_CODE_INSTALL_SUPPRESSED";
928 errorType = wxWEB_NAV_ERR_SECURITY;
929 break;
930 }
931
932 wxString url = evt[1].GetString();
933 wxString target = evt[2].GetString();
934 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, GetId(),
935 url, target, false);
936 event.SetEventObject(this);
937 event.SetInt(errorType);
938 event.SetString(errorCode);
939 HandleWindowEvent(event);
940 break;
941 }
853b6cd0 942 case DISPID_NEWWINDOW3:
61b98a2d 943 {
853b6cd0
SL
944 wxString url = evt[4].GetString();
945
946 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
947 GetId(), url, wxEmptyString, true);
948 event.SetEventObject(this);
949 HandleWindowEvent(event);
950
d676fb21
SL
951 //We always cancel this event otherwise an Internet Exporer window
952 //is opened for the url
953 wxActiveXEventNativeMSW* nativeParams = evt.GetNativeParameters();
954 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[3]) = VARIANT_TRUE;
be19c556
SL
955 break;
956 }
61b98a2d
SL
957 }
958
959 evt.Skip();
960}
961
9e3d4a32 962VirtualProtocol::VirtualProtocol(wxWebHandler *handler)
7d3f6b4d
SL
963{
964 m_refCount = 0;
965 m_file = NULL;
29365629 966 m_handler = handler;
7d3f6b4d
SL
967}
968
969VirtualProtocol::~VirtualProtocol()
970{
7d3f6b4d
SL
971}
972
973ULONG VirtualProtocol::AddRef()
974{
975 m_refCount++;
976 return m_refCount;
977}
978
979HRESULT VirtualProtocol::QueryInterface(REFIID riid, void **ppvObject)
980{
29365629
SL
981 if(riid == IID_IUnknown || riid == IID_IInternetProtocolRoot ||
982 riid == IID_IInternetProtocol)
7d3f6b4d 983 {
666f73c4
SL
984 *ppvObject = (IInternetProtocol*)this;
985 AddRef();
986 return S_OK;
987 }
7d3f6b4d
SL
988 else
989 {
990 *ppvObject = NULL;
991 return E_POINTER;
992 }
993}
994
995ULONG VirtualProtocol::Release()
996{
997 m_refCount--;
998 if (m_refCount > 0)
999 {
1000 return m_refCount;
1001 }
1002 else
1003 {
1004 delete this;
1005 return 0;
1006 }
1007}
1008
1009HRESULT VirtualProtocol::Start(LPCWSTR szUrl, IInternetProtocolSink *pOIProtSink,
0995b9dc
SL
1010 IInternetBindInfo *pOIBindInfo, DWORD grfPI,
1011 HANDLE_PTR dwReserved)
7d3f6b4d 1012{
0995b9dc
SL
1013 wxUnusedVar(szUrl);
1014 wxUnusedVar(pOIBindInfo);
1015 wxUnusedVar(grfPI);
1016 wxUnusedVar(dwReserved);
7d3f6b4d 1017 m_protocolSink = pOIProtSink;
29365629
SL
1018
1019 //We get the file itself from the protocol handler
1020 m_file = m_handler->GetFile(szUrl);
1021
7d3f6b4d
SL
1022
1023 if(!m_file)
1024 return INET_E_RESOURCE_NOT_FOUND;
1025
1026 //We return the stream length for current and total size as we can always
1027 //read the whole file from the stream
666f73c4 1028 wxFileOffset length = m_file->GetStream()->GetLength();
7d3f6b4d
SL
1029 m_protocolSink->ReportData(BSCF_FIRSTDATANOTIFICATION |
1030 BSCF_DATAFULLYAVAILABLE |
1031 BSCF_LASTDATANOTIFICATION,
666f73c4 1032 length, length);
7d3f6b4d
SL
1033 return S_OK;
1034}
1035
1036HRESULT VirtualProtocol::Read(void *pv, ULONG cb, ULONG *pcbRead)
1037{
1038 //If the file is null we return false to indicte it is finished
1039 if(!m_file)
1040 return S_FALSE;
1041
1042 wxStreamError err = m_file->GetStream()->Read(pv, cb).GetLastError();
1043 *pcbRead = m_file->GetStream()->LastRead();
1044
1045 if(err == wxSTREAM_NO_ERROR)
1046 {
1047 if(*pcbRead < cb)
1048 {
1049 wxDELETE(m_file);
1050 m_protocolSink->ReportResult(S_OK, 0, NULL);
1051 }
1052 //As we are not eof there is more data
1053 return S_OK;
1054 }
1055 else if(err == wxSTREAM_EOF)
1056 {
1057 wxDELETE(m_file);
1058 m_protocolSink->ReportResult(S_OK, 0, NULL);
1059 //We are eof and so finished
1060 return S_OK;
1061 }
1062 else if(err == wxSTREAM_READ_ERROR)
1063 {
1064 wxDELETE(m_file);
1065 return INET_E_DOWNLOAD_FAILURE;
1066 }
0995b9dc
SL
1067 else
1068 {
1069 //Dummy return to surpress a compiler warning
1070 wxFAIL;
1071 return INET_E_DOWNLOAD_FAILURE;
1072 }
7d3f6b4d
SL
1073}
1074
1075HRESULT ClassFactory::CreateInstance(IUnknown* pUnkOuter, REFIID riid,
1076 void ** ppvObject)
1077{
1078 if (pUnkOuter)
1079 return CLASS_E_NOAGGREGATION;
29365629 1080 VirtualProtocol* vp = new VirtualProtocol(m_handler);
7d3f6b4d
SL
1081 vp->AddRef();
1082 HRESULT hr = vp->QueryInterface(riid, ppvObject);
1083 vp->Release();
1084 return hr;
1085
1086}
1087
1088STDMETHODIMP ClassFactory::LockServer(BOOL fLock)
1089{
0995b9dc
SL
1090 wxUnusedVar(fLock);
1091 return S_OK;
7d3f6b4d
SL
1092}
1093
1094ULONG ClassFactory::AddRef(void)
1095{
1096 m_refCount++;
1097 return m_refCount;
1098}
1099
1100HRESULT ClassFactory::QueryInterface(REFIID riid, void **ppvObject)
1101{
1102 if ((riid == IID_IUnknown) || (riid == IID_IClassFactory))
1103 {
1104 *ppvObject = this;
1105 AddRef();
1106 return S_OK;
1107 }
1108 else
1109 {
1110 *ppvObject = NULL;
1111 return E_POINTER;
1112 }
1113
1114}
1115
1116ULONG ClassFactory::Release(void)
1117{
1118 m_refCount--;
1119 if (m_refCount > 0)
1120 {
1121 return m_refCount;
1122 }
1123 else
1124 {
1125 delete this;
1126 return 0;
1127 }
1128
1129}
1130
61b98a2d 1131#endif