ReactOS  0.4.13-dev-443-g10f00f8
webchild.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2004, 2005 Martin Fuchs
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 
20  //
21  // Explorer clone
22  //
23  // webchild.cpp
24  //
25  // Martin Fuchs, 08.02.2004
26  //
27 
28 #include <precomp.h>
29 
30 #include "webchild.h"
31 
32 #include <comutil.h>
33 
34 Variant::Variant(const VARIANT& var)
35 {
36  VariantInit(this);
37  CheckError(VariantCopy(this, const_cast<VARIANT*>(&var)));
38 }
39 
40 Variant::Variant(const VARIANT* var)
41 {
42  VariantInit(this);
43  CheckError(VariantCopy(this, const_cast<VARIANT*>(var)));
44 }
45 
46 Variant::~Variant()
47 {
48  VariantClear(this);
49 }
50 
51 
52 Variant::operator long() const
53 {
54  Variant v;
56  return V_I4(&v);
57 }
58 
59 Variant::operator bool() const
60 {
61  Variant v;
63  return V_BOOL(&v)? true: false;
64 }
65 
67 {
68  Variant v;
70  return V_DISPATCH(&v);
71 }
72 
74 {
75  Variant v;
77  return V_BOOL(&v);
78 }
79 
80 
81 void BStr::assign(BSTR s)
82 {
83  if (!SysReAllocString(&_p, s))
85 }
86 
87 void BStr::assign(const VARIANT& var)
88 {
89  if (V_VT(&var) == VT_BSTR)
90  assign(V_BSTR(&var));
91  else {
92  Variant v;
93  CheckError(VariantChangeType(&v, const_cast<VARIANT*>(&var), 0, VT_BSTR));
94  assign(V_BSTR(&v));
95  }
96 }
97 
98 
100  : _browser_initialized(false)
101 {
102 }
103 
104 void BrowserNavigator::attach(IWebBrowser* browser)
105 {
106  _browser = browser;
107 }
108 
110 {
112  _browser->Navigate(BStr(url), NULL, NULL, NULL, NULL);
113  else {
114  _new_url = url;
115 
116  _browser->Navigate(BStr(L"about:blank"), NULL, NULL, NULL, NULL);
117  }
118 }
119 
121 {
122  _new_html_txt = html_txt;
123 
124  goto_url(TEXT("about:blank"));
125 }
126 
128 {
129  while(len-- > 0)
130  *d++ = (unsigned char)*s++;
131 }
132 
134 {
135  _browser_initialized = true;
136 
137  bool nav = false;
138 
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);
142  ++nav;
143  }
144 
145  _new_url.erase();
146  }
147 
148  if (!nav && !_new_html_txt.empty()) {
149  int len = _new_html_txt.length();
150  HGLOBAL hHtmlText = GlobalAlloc(GPTR, len);
151 
152  if (!hHtmlText) {
153  T2nA_binary(_new_html_txt, (char*)hHtmlText, len);
154  _new_html_txt.erase();
155 
156  SIfacePtr<IStream> pStream;
157  HRESULT hr = CreateStreamOnHGlobal(hHtmlText, TRUE, &pStream);
158 
159  if (SUCCEEDED(hr)) {
160  SIfacePtr<IDispatch> pHtmlDoc;
161  CheckError(_browser->get_Document(&pHtmlDoc));
162 
163  SIfacePtr<IPersistStreamInit> pPersistStreamInit;
164  pHtmlDoc.QueryInterface(IID_IPersistStreamInit, &pPersistStreamInit);
165 
166  CheckError(pPersistStreamInit->InitNew());
167  CheckError(pPersistStreamInit->Load(pStream));
168  } else
169  GlobalFree(hHtmlText);
170  }
171  }
172 }
173 
174 
176 {
178 
179  if (!pWnd)
180  return 0;
181 
182  return *pWnd;
183 }
184 
186  {0x1339B54C, 0x3453, 0x11D2, {0x93, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
187 
188 
190  : super(hwnd, info),
191  web_super(_navigator)
192 {
193  // first try to create a web control with MS IE's CLASSID
194  HRESULT hr = create_control(hwnd, CLSID_WebBrowser, IID_IWebBrowser2);
195 
196  // If this failed, try to use Mozilla's web control
197  if (FAILED(hr))
198  hr = create_control(hwnd, CLSID_MozillaBrowser, IID_IWebBrowser2);
199 
200  if (SUCCEEDED(hr)) {
202 
203  _connector = auto_ptr<EventConnector>(new EventConnector(_control, DIID_DWebBrowserEvents2, this));
204 
205  _control->Navigate(BStr(info._path), &vtMissing, &vtMissing, &vtMissing, &vtMissing);
206  //browser->Navigate2(&Variant(info._path), &vtMissing, &vtMissing, &vtMissing, &vtMissing);
207  }
208 }
209 
211 {
212  try {
213  switch(nmsg) {
214  case WM_ERASEBKGND:
215  if (!_control) {
216  HDC hdc = (HDC)wparam;
217  ClientRect rect(_hwnd);
218 
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);
225  }
226 
227  return TRUE;
228 
229  case PM_DISPATCH_COMMAND: {
230  if (!_control)
231  return FALSE;
232 
233  HRESULT hr = E_FAIL;
234 
235  switch(LOWORD(wparam)) {
236  case ID_GO_BACK:
237  hr = _control->GoBack();
238  break;
239 
240  case ID_GO_FORWARD:
241  hr = _control->GoForward();
242  break;
243 
244  case ID_GO_UP:
246  break;
247 
248  case ID_GO_HOME:
249  hr = _control->GoHome();
250  break;
251 
252  case ID_GO_SEARCH:
253  hr = _control->GoSearch();
254  break;
255 
256  case ID_REFRESH:
257  hr = _control->Refresh();
258  break;
259 
260  case ID_STOP:
261  hr = _control->Stop();
262  break;
263 
264  default:
265  return super::WndProc(nmsg, wparam, lparam);
266  }
267 
268  if (FAILED(hr) && hr!=E_FAIL)
270 
271  return TRUE;}
272 
273  default:
274  return super::WndProc(nmsg, wparam, lparam);
275  }
276  } catch(COMException& e) {
277  HandleException(e, _hwnd);
278  }
279 
280  return 0;
281 }
282 
283 
285 {
287 
288  return url;
289 }
INT WINAPI SysReAllocString(LPBSTR old, LPCOLESTR str)
Definition: oleaut.c:470
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
#define THROW_EXCEPTION(hr)
Definition: shellclasses.h:161
HRESULT WINAPI DECLSPEC_HOTPATCH VariantChangeType(VARIANTARG *pvargDest, VARIANTARG *pvargSrc, USHORT wFlags, VARTYPE vt)
Definition: variant.c:965
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
HGLOBAL NTAPI GlobalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:368
#define ID_REFRESH
Definition: resource.h:18
Definition: compat.h:1939
static HBRUSH hbrush
void navigated(LPCTSTR url)
Definition: webchild.cpp:133
HRESULT hr
Definition: shlfolder.c:183
#define GPTR
Definition: winbase.h:293
int _tcscmp(const _TCHAR *s1, const _TCHAR *s2)
Definition: tcscmp.h:8
HWND create_webchildwindow(const WebChildWndInfo &info)
Definition: webchild.cpp:175
void T2nA_binary(LPCTSTR s, LPSTR d, int len)
Definition: webchild.cpp:127
void goto_url(LPCTSTR url)
Definition: webchild.cpp:109
String _new_url
Definition: webchild.h:519
static HDC
Definition: imagelist.c:92
void attach(IWebBrowser *browser)
Definition: webchild.cpp:104
operator
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
HRESULT WINAPI CreateStreamOnHGlobal(HGLOBAL hGlobal, BOOL fDeleteOnRelease, LPSTREAM *ppstm)
static WCHAR String[]
Definition: stringtable.c:55
UINT_PTR WPARAM
Definition: windef.h:207
OLECHAR * BSTR
Definition: compat.h:1934
void set_html_page(const String &html_txt)
Definition: webchild.cpp:120
void WINAPI VariantInit(VARIANTARG *pVarg)
Definition: variant.c:571
char * LPSTR
Definition: xmlstorage.h:182
#define E_FAIL
Definition: ddrawi.h:102
#define ID_GO_UP
Definition: resource.h:210
short VARIANT_BOOL
Definition: compat.h:1931
& rect
Definition: startmenu.cpp:1413
#define DrawText
Definition: winuser.h:5637
#define V_I4(A)
Definition: oleauto.h:247
Exception with context information.
Definition: shellclasses.h:113
#define V_DISPATCH(A)
Definition: oleauto.h:239
SIfacePtr< IWebBrowser > _browser
Definition: webchild.h:518
LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
Definition: webchild.cpp:210
_variant_t vtMissing(DISP_E_PARAMNOTFOUND, VT_ERROR)
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
GLuint color
Definition: glext.h:6243
#define e
Definition: ke_i.h:82
static UINT WPARAM LPARAM lparam
Definition: combo.c:716
HRESULT create_control(HWND hwnd, REFIID clsid, REFIID riid)
Definition: webchild.h:335
#define TRANSPARENT
Definition: wingdi.h:949
smooth NULL
Definition: ftsmooth.c:416
unsigned char
Definition: typeof.h:29
LONG_PTR LPARAM
Definition: windef.h:208
static const CLSID CLSID_MozillaBrowser
Definition: webchild.cpp:185
#define DT_VCENTER
Definition: winuser.h:543
virtual String jump_to_int(LPCTSTR url)
Definition: webchild.cpp:284
typedef bool(CARDLIBPROC *pCanDragProc)(CardRegion &stackobj
void HandleException(COMException &e, HWND hwnd)
Exception Handler for COM exceptions.
#define d
Definition: ke_i.h:81
BrowserNavigator _navigator
Definition: webchild.h:1012
LONG HRESULT
Definition: typedefs.h:77
static const WCHAR url[]
Definition: encode.c:1432
#define RGB(r, g, b)
Definition: wingdi.h:2918
#define V_BOOL(A)
Definition: oleauto.h:224
information structure for creation of WebChildWindow
Definition: filechild.h:79
HGLOBAL NTAPI GlobalFree(HGLOBAL hMem)
Definition: heapmem.c:611
#define false
Definition: stdbool.h:38
HRESULT QueryInterface(REFIID riid, I *p)
Definition: shellclasses.h:417
HRESULT WINAPI DECLSPEC_HOTPATCH VariantClear(VARIANTARG *pVarg)
Definition: variant.c:651
static const WCHAR L[]
Definition: oid.c:1250
const GUID IID_IPersistStreamInit
HDC hdc
Definition: main.c:9
#define V_VT(A)
Definition: oleauto.h:211
void CheckError(HRESULT hr)
Definition: shellclasses.h:167
auto_ptr< EventConnector > _connector
Definition: webchild.h:1013
GLenum GLsizei len
Definition: glext.h:6722
GLdouble s
Definition: gl.h:2039
GLenum mode
Definition: glext.h:6217
#define V_BSTR(A)
Definition: oleauto.h:226
#define ID_GO_SEARCH
Definition: resource.h:209
bool _browser_initialized
Definition: webchild.h:521
#define DT_SINGLELINE
Definition: winuser.h:540
String _new_html_txt
Definition: webchild.h:520
#define TEXT(s)
Definition: k32.h:26
const GLdouble * v
Definition: gl.h:2040
LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
Definition: webchild.h:354
#define ID_GO_HOME
Definition: resource.h:208
#define long
Definition: qsort.c:33
unsigned int UINT
Definition: ndis.h:50
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
wrapper class for COM interface pointers
Definition: shellclasses.h:332
#define ID_GO_FORWARD
Definition: resource.h:207
#define DT_CENTER
Definition: winuser.h:527
static WebChildWindow * create(const FileChildWndInfo &info)
Definition: webchild.h:842
encapsulation of Web control in MDI child windows
Definition: webchild.h:834
#define WM_ERASEBKGND
Definition: winuser.h:1607
LONG_PTR LRESULT
Definition: windef.h:209
HRESULT WINAPI VariantCopy(VARIANTARG *pvargDest, VARIANTARG *pvargSrc)
Definition: variant.c:751
int WINAPI FillRect(HDC, LPCRECT, HBRUSH)
#define PM_DISPATCH_COMMAND
Definition: window.h:265
#define LOWORD(l)
Definition: pedump.c:82
#define ID_STOP
Definition: resource.h:21
#define SUCCEEDED(hr)
Definition: intsafe.h:57
static UINT WPARAM wparam
Definition: combo.c:716
#define ID_GO_BACK
Definition: resource.h:206
WebChildWindow(HWND hwnd, const WebChildWndInfo &info)
Definition: webchild.cpp:189