2 * Copyright 2004, 2005 Martin Fuchs
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 // Martin Fuchs, 08.02.2004
31 #include "../resource.h"
41 Variant::Variant(const VARIANT
& var
)
44 CheckError(VariantCopy(this, const_cast<VARIANT
*>(&var
)));
47 Variant::Variant(const VARIANT
* var
)
50 CheckError(VariantCopy(this, const_cast<VARIANT
*>(var
)));
59 Variant::operator long() const
62 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_I4
));
66 Variant::operator bool() const
69 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_BOOL
));
70 return V_BOOL(&v
)? true: false;
73 Variant::operator IDispatch
*() const
76 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_DISPATCH
));
77 return V_DISPATCH(&v
);
80 Variant::operator VARIANT_BOOL() const
83 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_BOOL
));
88 void BStr::assign(BSTR s
)
90 if (!SysReAllocString(&_p
, s
))
91 THROW_EXCEPTION(E_OUTOFMEMORY
);
94 void BStr::assign(const VARIANT
& var
)
96 if (V_VT(&var
) == VT_BSTR
)
100 CheckError(VariantChangeType(&v
, const_cast<VARIANT
*>(&var
), 0, VT_BSTR
));
106 BrowserNavigator::BrowserNavigator()
107 : _browser_initialized(false)
111 void BrowserNavigator::attach(IWebBrowser
* browser
)
116 void BrowserNavigator::goto_url(LPCTSTR url
)
118 if (_browser_initialized
)
119 _browser
->Navigate(BStr(url
), NULL
, NULL
, NULL
, NULL
);
123 _browser
->Navigate(BStr(L
"about:blank"), NULL
, NULL
, NULL
, NULL
);
127 void BrowserNavigator::set_html_page(const String
& html_txt
)
129 _new_html_txt
= html_txt
;
131 goto_url(TEXT("about:blank"));
134 void T2nA_binary(LPCTSTR s
, LPSTR d
, int len
)
137 *d
++ = (unsigned char)*s
++;
140 void BrowserNavigator::navigated(LPCTSTR url
)
142 _browser_initialized
= true;
146 if (!_new_url
.empty()) {
147 if (!_tcscmp(url
,TEXT("about:blank")) && _new_url
!=TEXT("about:blank")) {
148 _browser
->Navigate(BStr(_new_url
), NULL
, NULL
, NULL
, NULL
);
155 if (!nav
&& !_new_html_txt
.empty()) { ///@todo move this into DocumentComplete() ?
156 int len
= _new_html_txt
.length();
157 HGLOBAL hHtmlText
= GlobalAlloc(GPTR
, len
);
160 T2nA_binary(_new_html_txt
, (char*)hHtmlText
, len
);
161 _new_html_txt
.erase();
163 SIfacePtr
<IStream
> pStream
;
164 HRESULT hr
= CreateStreamOnHGlobal(hHtmlText
, TRUE
, &pStream
);
167 SIfacePtr
<IDispatch
> pHtmlDoc
;
168 CheckError(_browser
->get_Document(&pHtmlDoc
));
170 SIfacePtr
<IPersistStreamInit
> pPersistStreamInit
;
171 pHtmlDoc
.QueryInterface(IID_IPersistStreamInit
, &pPersistStreamInit
);
173 CheckError(pPersistStreamInit
->InitNew());
174 CheckError(pPersistStreamInit
->Load(pStream
));
176 GlobalFree(hHtmlText
);
182 HWND
create_webchildwindow(const WebChildWndInfo
& info
)
184 WebChildWindow
* pWnd
= WebChildWindow::create(info
);
192 static const CLSID CLSID_MozillaBrowser
=
193 {0x1339B54C, 0x3453, 0x11D2, {0x93, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
196 WebChildWindow::WebChildWindow(HWND hwnd
, const WebChildWndInfo
& info
)
198 web_super(_navigator
)
200 // first try to create a web control with MS IE's CLASSID
201 HRESULT hr
= create_control(hwnd
, CLSID_WebBrowser
, IID_IWebBrowser2
);
203 // If this failed, try to use Mozilla's web control
205 hr
= create_control(hwnd
, CLSID_MozillaBrowser
, IID_IWebBrowser2
);
208 _navigator
.attach(_control
);
210 _connector
= auto_ptr
<EventConnector
>(new EventConnector(_control
, DIID_DWebBrowserEvents2
, this));
212 _control
->Navigate(BStr(info
._path
), &vtMissing
, &vtMissing
, &vtMissing
, &vtMissing
);
213 //browser->Navigate2(&Variant(info._path), &vtMissing, &vtMissing, &vtMissing, &vtMissing);
217 LRESULT
WebChildWindow::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
223 HDC hdc
= (HDC
)wparam
;
224 ClientRect
rect(_hwnd
);
226 HBRUSH hbrush
= CreateSolidBrush(RGB(200,200,235));
227 BkMode
mode(hdc
, TRANSPARENT
);
228 TextColor
color(hdc
, RGB(200,40,40));
229 FillRect(hdc
, &rect
, hbrush
);
230 DrawText(hdc
, TEXT("Sorry - no web browser control could be loaded."), -1, &rect
, DT_CENTER
|DT_VCENTER
|DT_SINGLELINE
);
231 DeleteObject(hbrush
);
236 case PM_DISPATCH_COMMAND
: {
242 switch(LOWORD(wparam
)) {
244 hr
= _control
->GoBack();
248 hr
= _control
->GoForward();
256 hr
= _control
->GoHome();
260 hr
= _control
->GoSearch();
264 hr
= _control
->Refresh();
268 hr
= _control
->Stop();
272 return super::WndProc(nmsg
, wparam
, lparam
);
275 if (FAILED(hr
) && hr
!=E_FAIL
)
281 return super::WndProc(nmsg
, wparam
, lparam
);
283 } catch(COMException
& e
) {
284 HandleException(e
, _hwnd
);
291 String
WebChildWindow::jump_to_int(LPCTSTR url
)
293 _navigator
.goto_url(url
);