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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 // Martin Fuchs, 08.02.2004
31 #include "../resource.h"
38 #if _MSC_VER>=1300 // vtMissing for VS.Net
40 #pragma comment(lib, "comsupp")
45 #ifdef __MINGW32__ // MinGW is lacking vtMissing (as of 07.02.2004)
46 static Variant vtMissing
;
54 Variant::Variant(const VARIANT
& var
)
57 CheckError(VariantCopy(this, const_cast<VARIANT
*>(&var
)));
60 Variant::Variant(const VARIANT
* var
)
63 CheckError(VariantCopy(this, const_cast<VARIANT
*>(var
)));
72 Variant::operator long() const
75 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_I4
));
79 Variant::operator bool() const
82 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_BOOL
));
83 return V_BOOL(&v
)? true: false;
86 Variant::operator IDispatch
*() const
89 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_DISPATCH
));
90 return V_DISPATCH(&v
);
93 Variant::operator VARIANT_BOOL() const
96 CheckError(VariantChangeType(&v
, (VARIANT
*)this, 0, VT_BOOL
));
101 void BStr::assign(BSTR s
)
103 if (!SysReAllocString(&_p
, s
))
104 THROW_EXCEPTION(E_OUTOFMEMORY
);
107 void BStr::assign(const VARIANT
& var
)
109 if (V_VT(&var
) == VT_BSTR
)
110 assign(V_BSTR(&var
));
113 CheckError(VariantChangeType(&v
, const_cast<VARIANT
*>(&var
), 0, VT_BSTR
));
119 BrowserNavigator::BrowserNavigator()
120 : _browser_initialized(false)
124 void BrowserNavigator::attach(IWebBrowser
* browser
)
129 void BrowserNavigator::goto_url(LPCTSTR url
)
131 if (_browser_initialized
)
132 _browser
->Navigate(BStr(url
), NULL
, NULL
, NULL
, NULL
);
136 _browser
->Navigate(L
"about:blank", NULL
, NULL
, NULL
, NULL
);
140 void BrowserNavigator::set_html_page(const String
& html_txt
)
142 _new_html_txt
= html_txt
;
144 goto_url(TEXT("about:blank"));
147 void T2nA_binary(LPCTSTR s
, LPSTR d
, int len
)
150 *d
++ = (unsigned char)*s
++;
153 void BrowserNavigator::navigated(LPCTSTR url
)
155 _browser_initialized
= true;
159 if (!_new_url
.empty()) {
160 if (!_tcscmp(url
,TEXT("about:blank")) && _new_url
!=TEXT("about:blank")) {
161 _browser
->Navigate(BStr(_new_url
), NULL
, NULL
, NULL
, NULL
);
168 if (!nav
&& !_new_html_txt
.empty()) { ///@todo move this into DocumentComplete() ?
169 int len
= _new_html_txt
.length();
170 HGLOBAL hHtmlText
= GlobalAlloc(GPTR
, len
);
173 T2nA_binary(_new_html_txt
, (char*)hHtmlText
, len
);
174 _new_html_txt
.erase();
176 SIfacePtr
<IStream
> pStream
;
177 HRESULT hr
= CreateStreamOnHGlobal(hHtmlText
, TRUE
, &pStream
);
180 SIfacePtr
<IDispatch
> pHtmlDoc
;
181 CheckError(_browser
->get_Document(&pHtmlDoc
));
183 SIfacePtr
<IPersistStreamInit
> pPersistStreamInit
;
184 pHtmlDoc
.QueryInterface(IID_IPersistStreamInit
, &pPersistStreamInit
);
186 CheckError(pPersistStreamInit
->InitNew());
187 CheckError(pPersistStreamInit
->Load(pStream
));
189 GlobalFree(hHtmlText
);
195 HWND
create_webchildwindow(const WebChildWndInfo
& info
)
197 WebChildWindow
* pWnd
= WebChildWindow::create(info
);
205 static const CLSID CLSID_MozillaBrowser
=
206 {0x1339B54C, 0x3453, 0x11D2, {0x93, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
209 WebChildWindow::WebChildWindow(HWND hwnd
, const WebChildWndInfo
& info
)
211 web_super(_navigator
)
213 // first try to create a web control with MS IE's CLASSID
214 HRESULT hr
= create_control(hwnd
, CLSID_WebBrowser
, IID_IWebBrowser2
);
216 // If this failed, try to use Mozilla's web control
218 hr
= create_control(hwnd
, CLSID_MozillaBrowser
, IID_IWebBrowser2
);
221 _navigator
.attach(_control
);
223 _connector
= auto_ptr
<EventConnector
>(new EventConnector(_control
, DIID_DWebBrowserEvents2
, this));
225 _control
->Navigate(BStr(info
._path
), &vtMissing
, &vtMissing
, &vtMissing
, &vtMissing
);
226 //browser->Navigate2(&Variant(info._path), &vtMissing, &vtMissing, &vtMissing, &vtMissing);
230 LRESULT
WebChildWindow::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
236 HDC hdc
= (HDC
)wparam
;
237 ClientRect
rect(_hwnd
);
239 HBRUSH hbrush
= CreateSolidBrush(RGB(200,200,235));
240 BkMode
mode(hdc
, TRANSPARENT
);
241 TextColor
color(hdc
, RGB(200,40,40));
242 FillRect(hdc
, &rect
, hbrush
);
243 DrawText(hdc
, TEXT("Sorry - no web browser control could be loaded."), -1, &rect
, DT_CENTER
|DT_VCENTER
|DT_SINGLELINE
);
244 DeleteObject(hbrush
);
249 case PM_DISPATCH_COMMAND
: {
255 switch(LOWORD(wparam
)) {
257 hr
= _control
->GoBack();
261 hr
= _control
->GoForward();
269 hr
= _control
->GoHome();
273 hr
= _control
->GoSearch();
277 hr
= _control
->Refresh();
281 hr
= _control
->Stop();
285 return super::WndProc(nmsg
, wparam
, lparam
);
288 if (FAILED(hr
) && hr
!=E_FAIL
)
294 return super::WndProc(nmsg
, wparam
, lparam
);
296 } catch(COMException
& e
) {
297 HandleException(e
, _hwnd
);
304 String
WebChildWindow::jump_to_int(LPCTSTR url
)
306 _navigator
.goto_url(url
);