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
34 Variant::Variant(const VARIANT
& var
)
37 CheckError(VariantCopy(this, const_cast<VARIANT
*>(&var
)));
40 Variant::Variant(const VARIANT
* var
)
43 CheckError(VariantCopy(this, const_cast<VARIANT
*>(var
)));
52 Variant::operator long() const
55 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_I4
));
59 Variant::operator bool() const
62 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_BOOL
));
63 return V_BOOL(&v
)? true: false;
66 Variant::operator IDispatch
*() const
69 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_DISPATCH
));
70 return V_DISPATCH(&v
);
73 Variant::operator VARIANT_BOOL() const
76 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_BOOL
));
81 void BStr::assign(BSTR s
)
83 if (!SysReAllocString(&_p
, s
))
84 THROW_EXCEPTION(E_OUTOFMEMORY
);
87 void BStr::assign(const VARIANT
& var
)
89 if (V_VT(&var
) == VT_BSTR
)
93 CheckError(VariantChangeType(&v
, const_cast<VARIANT
*>(&var
), 0, VT_BSTR
));
99 BrowserNavigator::BrowserNavigator()
100 : _browser_initialized(false)
104 void BrowserNavigator::attach(IWebBrowser
* browser
)
109 void BrowserNavigator::goto_url(LPCTSTR url
)
111 if (_browser_initialized
)
112 _browser
->Navigate(BStr(url
), NULL
, NULL
, NULL
, NULL
);
116 _browser
->Navigate(BStr(L
"about:blank"), NULL
, NULL
, NULL
, NULL
);
120 void BrowserNavigator::set_html_page(const String
& html_txt
)
122 _new_html_txt
= html_txt
;
124 goto_url(TEXT("about:blank"));
127 void T2nA_binary(LPCTSTR s
, LPSTR d
, int len
)
130 *d
++ = (unsigned char)*s
++;
133 void BrowserNavigator::navigated(LPCTSTR url
)
135 _browser_initialized
= true;
139 if (!_new_url
.empty()) {
140 if (!_tcscmp(url
,TEXT("about:blank")) && _new_url
!=TEXT("about:blank")) {
141 _browser
->Navigate(BStr(_new_url
), NULL
, NULL
, NULL
, NULL
);
148 if (!nav
&& !_new_html_txt
.empty()) { ///@todo move this into DocumentComplete() ?
149 int len
= _new_html_txt
.length();
150 HGLOBAL hHtmlText
= GlobalAlloc(GPTR
, len
);
153 T2nA_binary(_new_html_txt
, (char*)hHtmlText
, len
);
154 _new_html_txt
.erase();
156 SIfacePtr
<IStream
> pStream
;
157 HRESULT hr
= CreateStreamOnHGlobal(hHtmlText
, TRUE
, &pStream
);
160 SIfacePtr
<IDispatch
> pHtmlDoc
;
161 CheckError(_browser
->get_Document(&pHtmlDoc
));
163 SIfacePtr
<IPersistStreamInit
> pPersistStreamInit
;
164 pHtmlDoc
.QueryInterface(IID_IPersistStreamInit
, &pPersistStreamInit
);
166 CheckError(pPersistStreamInit
->InitNew());
167 CheckError(pPersistStreamInit
->Load(pStream
));
169 GlobalFree(hHtmlText
);
175 HWND
create_webchildwindow(const WebChildWndInfo
& info
)
177 WebChildWindow
* pWnd
= WebChildWindow::create(info
);
185 static const CLSID CLSID_MozillaBrowser
=
186 {0x1339B54C, 0x3453, 0x11D2, {0x93, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
189 WebChildWindow::WebChildWindow(HWND hwnd
, const WebChildWndInfo
& info
)
191 web_super(_navigator
)
193 // first try to create a web control with MS IE's CLASSID
194 HRESULT hr
= create_control(hwnd
, CLSID_WebBrowser
, IID_IWebBrowser2
);
196 // If this failed, try to use Mozilla's web control
198 hr
= create_control(hwnd
, CLSID_MozillaBrowser
, IID_IWebBrowser2
);
201 _navigator
.attach(_control
);
203 _connector
= auto_ptr
<EventConnector
>(new EventConnector(_control
, DIID_DWebBrowserEvents2
, this));
205 _control
->Navigate(BStr(info
._path
), &vtMissing
, &vtMissing
, &vtMissing
, &vtMissing
);
206 //browser->Navigate2(&Variant(info._path), &vtMissing, &vtMissing, &vtMissing, &vtMissing);
210 LRESULT
WebChildWindow::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
216 HDC hdc
= (HDC
)wparam
;
217 ClientRect
rect(_hwnd
);
219 HBRUSH hbrush
= CreateSolidBrush(RGB(200,200,235));
220 BkMode
mode(hdc
, TRANSPARENT
);
221 TextColor
color(hdc
, RGB(200,40,40));
222 FillRect(hdc
, &rect
, hbrush
);
223 DrawText(hdc
, TEXT("Sorry - no web browser control could be loaded."), -1, &rect
, DT_CENTER
|DT_VCENTER
|DT_SINGLELINE
);
224 DeleteObject(hbrush
);
229 case PM_DISPATCH_COMMAND
: {
235 switch(LOWORD(wparam
)) {
237 hr
= _control
->GoBack();
241 hr
= _control
->GoForward();
249 hr
= _control
->GoHome();
253 hr
= _control
->GoSearch();
257 hr
= _control
->Refresh();
261 hr
= _control
->Stop();
265 return super::WndProc(nmsg
, wparam
, lparam
);
268 if (FAILED(hr
) && hr
!=E_FAIL
)
274 return super::WndProc(nmsg
, wparam
, lparam
);
276 } catch(COMException
& e
) {
277 HandleException(e
, _hwnd
);
284 String
WebChildWindow::jump_to_int(LPCTSTR url
)
286 _navigator
.goto_url(url
);