Something went wrong. Try again.
Reactos
Something went wrong. Try again.
6.3 kB · 289 lines
C++
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290/* * Copyright 2004, 2005 Martin Fuchs * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
// // Explorer clone // // webchild.cpp // // Martin Fuchs, 08.02.2004 //
#include <precomp.h>
#include "webchild.h"
#include <comutil.h>
Variant::Variant(const VARIANT& var){ VariantInit(this); CheckError(VariantCopy(this, const_cast<VARIANT*>(&var)));}
Variant::Variant(const VARIANT* var){ VariantInit(this); CheckError(VariantCopy(this, const_cast<VARIANT*>(var)));}
Variant::~Variant(){ VariantClear(this);}
Variant::operator long() const{ Variant v; CheckError(VariantChangeType(&v, (VARIANT*)this, 0, VT_I4)); return V_I4(&v);}
Variant::operator bool() const{ Variant v; CheckError(VariantChangeType(&v, (VARIANT*)this, 0, VT_BOOL)); return V_BOOL(&v)? true: false;}
Variant::operator IDispatch*() const{ Variant v; CheckError(VariantChangeType(&v, (VARIANT*)this, 0, VT_DISPATCH)); return V_DISPATCH(&v);}
Variant::operator VARIANT_BOOL() const{ Variant v; CheckError(VariantChangeType(&v, (VARIANT*)this, 0, VT_BOOL)); return V_BOOL(&v);}
void BStr::assign(BSTR s){ if (!SysReAllocString(&_p, s)) THROW_EXCEPTION(E_OUTOFMEMORY);}
void BStr::assign(const VARIANT& var){ if (V_VT(&var) == VT_BSTR) assign(V_BSTR(&var)); else { Variant v; CheckError(VariantChangeType(&v, const_cast<VARIANT*>(&var), 0, VT_BSTR)); assign(V_BSTR(&v)); }}
BrowserNavigator::BrowserNavigator() : _browser_initialized(false){}
void BrowserNavigator::attach(IWebBrowser* browser){ _browser = browser;}
void BrowserNavigator::goto_url(LPCTSTR url){ if (_browser_initialized) _browser->Navigate(BStr(url), NULL, NULL, NULL, NULL); else { _new_url = url;
_browser->Navigate(BStr(L"about:blank"), NULL, NULL, NULL, NULL); }}
void BrowserNavigator::set_html_page(const String& html_txt){ _new_html_txt = html_txt;
goto_url(TEXT("about:blank"));}
void T2nA_binary(LPCTSTR s, LPSTR d, int len){ while(len-- > 0) *d++ = (unsigned char)*s++;}
void BrowserNavigator::navigated(LPCTSTR url){ _browser_initialized = true;
bool nav = false;
if (!_new_url.empty()) { if (!_tcscmp(url,TEXT("about:blank")) && _new_url!=TEXT("about:blank")) { _browser->Navigate(BStr(_new_url), NULL, NULL, NULL, NULL); ++nav; }
_new_url.erase(); }
if (!nav && !_new_html_txt.empty()) { ///@todo move this into DocumentComplete() ? int len = _new_html_txt.length(); HGLOBAL hHtmlText = GlobalAlloc(GPTR, len);
if (!hHtmlText) { T2nA_binary(_new_html_txt, (char*)hHtmlText, len); _new_html_txt.erase();
SIfacePtr<IStream> pStream; HRESULT hr = CreateStreamOnHGlobal(hHtmlText, TRUE, &pStream);
if (SUCCEEDED(hr)) { SIfacePtr<IDispatch> pHtmlDoc; CheckError(_browser->get_Document(&pHtmlDoc));
SIfacePtr<IPersistStreamInit> pPersistStreamInit; pHtmlDoc.QueryInterface(IID_IPersistStreamInit, &pPersistStreamInit);
CheckError(pPersistStreamInit->InitNew()); CheckError(pPersistStreamInit->Load(pStream)); } else GlobalFree(hHtmlText); } }}
HWND create_webchildwindow(const WebChildWndInfo& info){ WebChildWindow* pWnd = WebChildWindow::create(info);
if (!pWnd) return 0;
return *pWnd;}
static const CLSID CLSID_MozillaBrowser = {0x1339B54C, 0x3453, 0x11D2, {0x93, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
WebChildWindow::WebChildWindow(HWND hwnd, const WebChildWndInfo& info) : super(hwnd, info), web_super(_navigator){ // first try to create a web control with MS IE's CLASSID HRESULT hr = create_control(hwnd, CLSID_WebBrowser, IID_IWebBrowser2);
// If this failed, try to use Mozilla's web control if (FAILED(hr)) hr = create_control(hwnd, CLSID_MozillaBrowser, IID_IWebBrowser2);
if (SUCCEEDED(hr)) { _navigator.attach(_control);
_connector = auto_ptr<EventConnector>(new EventConnector(_control, DIID_DWebBrowserEvents2, this));
_control->Navigate(BStr(info._path), &vtMissing, &vtMissing, &vtMissing, &vtMissing); //browser->Navigate2(&Variant(info._path), &vtMissing, &vtMissing, &vtMissing, &vtMissing); }}
LRESULT WebChildWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam){ try { switch(nmsg) { case WM_ERASEBKGND: if (!_control) { HDC hdc = (HDC)wparam; ClientRect rect(_hwnd);
HBRUSH hbrush = CreateSolidBrush(RGB(200,200,235)); BkMode mode(hdc, TRANSPARENT); TextColor color(hdc, RGB(200,40,40)); FillRect(hdc, &rect, hbrush); DrawText(hdc, TEXT("Sorry - no web browser control could be loaded."), -1, &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE); DeleteObject(hbrush); }
return TRUE;
case PM_DISPATCH_COMMAND: { if (!_control) return FALSE;
HRESULT hr = E_FAIL;
switch(LOWORD(wparam)) { case ID_GO_BACK: hr = _control->GoBack(); break;
case ID_GO_FORWARD: hr = _control->GoForward(); break;
case ID_GO_UP: ///@todo break;
case ID_GO_HOME: hr = _control->GoHome(); break;
case ID_GO_SEARCH: hr = _control->GoSearch(); break;
case ID_REFRESH: hr = _control->Refresh(); break;
case ID_STOP: hr = _control->Stop(); break;
default: return super::WndProc(nmsg, wparam, lparam); }
if (FAILED(hr) && hr!=E_FAIL) THROW_EXCEPTION(hr);
return TRUE;}
default: return super::WndProc(nmsg, wparam, lparam); } } catch(COMException& e) { HandleException(e, _hwnd); }
return 0;}
String WebChildWindow::jump_to_int(LPCTSTR url){ _navigator.goto_url(url);
return url;}