PageRenderTime 37ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/rosapps/applications/explorer-old/shell/webchild.cpp

https://gitlab.com/dj-tech/reactos
C++ | 289 lines | 196 code | 65 blank | 28 comment | 20 complexity | a35875b9d15e358519c88addeaa1315a MD5 | raw 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. // Explorer clone
  20. //
  21. // webchild.cpp
  22. //
  23. // Martin Fuchs, 08.02.2004
  24. //
  25. #include <precomp.h>
  26. #include "webchild.h"
  27. #include <comutil.h>
  28. Variant::Variant(const VARIANT& var)
  29. {
  30. VariantInit(this);
  31. CheckError(VariantCopy(this, const_cast<VARIANT*>(&var)));
  32. }
  33. Variant::Variant(const VARIANT* var)
  34. {
  35. VariantInit(this);
  36. CheckError(VariantCopy(this, const_cast<VARIANT*>(var)));
  37. }
  38. Variant::~Variant()
  39. {
  40. VariantClear(this);
  41. }
  42. Variant::operator long() const
  43. {
  44. Variant v;
  45. CheckError(VariantChangeType(&v, (VARIANT*)this, 0, VT_I4));
  46. return V_I4(&v);
  47. }
  48. Variant::operator bool() const
  49. {
  50. Variant v;
  51. CheckError(VariantChangeType(&v, (VARIANT*)this, 0, VT_BOOL));
  52. return V_BOOL(&v)? true: false;
  53. }
  54. Variant::operator IDispatch*() const
  55. {
  56. Variant v;
  57. CheckError(VariantChangeType(&v, (VARIANT*)this, 0, VT_DISPATCH));
  58. return V_DISPATCH(&v);
  59. }
  60. Variant::operator VARIANT_BOOL() const
  61. {
  62. Variant v;
  63. CheckError(VariantChangeType(&v, (VARIANT*)this, 0, VT_BOOL));
  64. return V_BOOL(&v);
  65. }
  66. void BStr::assign(BSTR s)
  67. {
  68. if (!SysReAllocString(&_p, s))
  69. THROW_EXCEPTION(E_OUTOFMEMORY);
  70. }
  71. void BStr::assign(const VARIANT& var)
  72. {
  73. if (V_VT(&var) == VT_BSTR)
  74. assign(V_BSTR(&var));
  75. else {
  76. Variant v;
  77. CheckError(VariantChangeType(&v, const_cast<VARIANT*>(&var), 0, VT_BSTR));
  78. assign(V_BSTR(&v));
  79. }
  80. }
  81. BrowserNavigator::BrowserNavigator()
  82. : _browser_initialized(false)
  83. {
  84. }
  85. void BrowserNavigator::attach(IWebBrowser* browser)
  86. {
  87. _browser = browser;
  88. }
  89. void BrowserNavigator::goto_url(LPCTSTR url)
  90. {
  91. if (_browser_initialized)
  92. _browser->Navigate(BStr(url), NULL, NULL, NULL, NULL);
  93. else {
  94. _new_url = url;
  95. _browser->Navigate(BStr(L"about:blank"), NULL, NULL, NULL, NULL);
  96. }
  97. }
  98. void BrowserNavigator::set_html_page(const String& html_txt)
  99. {
  100. _new_html_txt = html_txt;
  101. goto_url(TEXT("about:blank"));
  102. }
  103. void T2nA_binary(LPCTSTR s, LPSTR d, int len)
  104. {
  105. while(len-- > 0)
  106. *d++ = (unsigned char)*s++;
  107. }
  108. void BrowserNavigator::navigated(LPCTSTR url)
  109. {
  110. _browser_initialized = true;
  111. bool nav = false;
  112. if (!_new_url.empty()) {
  113. if (!_tcscmp(url,TEXT("about:blank")) && _new_url!=TEXT("about:blank")) {
  114. _browser->Navigate(BStr(_new_url), NULL, NULL, NULL, NULL);
  115. ++nav;
  116. }
  117. _new_url.erase();
  118. }
  119. if (!nav && !_new_html_txt.empty()) { ///@todo move this into DocumentComplete() ?
  120. int len = _new_html_txt.length();
  121. HGLOBAL hHtmlText = GlobalAlloc(GPTR, len);
  122. if (!hHtmlText) {
  123. T2nA_binary(_new_html_txt, (char*)hHtmlText, len);
  124. _new_html_txt.erase();
  125. SIfacePtr<IStream> pStream;
  126. HRESULT hr = CreateStreamOnHGlobal(hHtmlText, TRUE, &pStream);
  127. if (SUCCEEDED(hr)) {
  128. SIfacePtr<IDispatch> pHtmlDoc;
  129. CheckError(_browser->get_Document(&pHtmlDoc));
  130. SIfacePtr<IPersistStreamInit> pPersistStreamInit;
  131. pHtmlDoc.QueryInterface(IID_IPersistStreamInit, &pPersistStreamInit);
  132. CheckError(pPersistStreamInit->InitNew());
  133. CheckError(pPersistStreamInit->Load(pStream));
  134. } else
  135. GlobalFree(hHtmlText);
  136. }
  137. }
  138. }
  139. HWND create_webchildwindow(const WebChildWndInfo& info)
  140. {
  141. WebChildWindow* pWnd = WebChildWindow::create(info);
  142. if (!pWnd)
  143. return 0;
  144. return *pWnd;
  145. }
  146. static const CLSID CLSID_MozillaBrowser =
  147. {0x1339B54C, 0x3453, 0x11D2, {0x93, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
  148. WebChildWindow::WebChildWindow(HWND hwnd, const WebChildWndInfo& info)
  149. : super(hwnd, info),
  150. web_super(_navigator)
  151. {
  152. // first try to create a web control with MS IE's CLASSID
  153. HRESULT hr = create_control(hwnd, CLSID_WebBrowser, IID_IWebBrowser2);
  154. // If this failed, try to use Mozilla's web control
  155. if (FAILED(hr))
  156. hr = create_control(hwnd, CLSID_MozillaBrowser, IID_IWebBrowser2);
  157. if (SUCCEEDED(hr)) {
  158. _navigator.attach(_control);
  159. _connector = auto_ptr<EventConnector>(new EventConnector(_control, DIID_DWebBrowserEvents2, this));
  160. _control->Navigate(BStr(info._path), &vtMissing, &vtMissing, &vtMissing, &vtMissing);
  161. //browser->Navigate2(&Variant(info._path), &vtMissing, &vtMissing, &vtMissing, &vtMissing);
  162. }
  163. }
  164. LRESULT WebChildWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
  165. {
  166. try {
  167. switch(nmsg) {
  168. case WM_ERASEBKGND:
  169. if (!_control) {
  170. HDC hdc = (HDC)wparam;
  171. ClientRect rect(_hwnd);
  172. HBRUSH hbrush = CreateSolidBrush(RGB(200,200,235));
  173. BkMode mode(hdc, TRANSPARENT);
  174. TextColor color(hdc, RGB(200,40,40));
  175. FillRect(hdc, &rect, hbrush);
  176. DrawText(hdc, TEXT("Sorry - no web browser control could be loaded."), -1, &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
  177. DeleteObject(hbrush);
  178. }
  179. return TRUE;
  180. case PM_DISPATCH_COMMAND: {
  181. if (!_control)
  182. return FALSE;
  183. HRESULT hr = E_FAIL;
  184. switch(LOWORD(wparam)) {
  185. case ID_GO_BACK:
  186. hr = _control->GoBack();
  187. break;
  188. case ID_GO_FORWARD:
  189. hr = _control->GoForward();
  190. break;
  191. case ID_GO_UP:
  192. ///@todo
  193. break;
  194. case ID_GO_HOME:
  195. hr = _control->GoHome();
  196. break;
  197. case ID_GO_SEARCH:
  198. hr = _control->GoSearch();
  199. break;
  200. case ID_REFRESH:
  201. hr = _control->Refresh();
  202. break;
  203. case ID_STOP:
  204. hr = _control->Stop();
  205. break;
  206. default:
  207. return super::WndProc(nmsg, wparam, lparam);
  208. }
  209. if (FAILED(hr) && hr!=E_FAIL)
  210. THROW_EXCEPTION(hr);
  211. return TRUE;}
  212. default:
  213. return super::WndProc(nmsg, wparam, lparam);
  214. }
  215. } catch(COMException& e) {
  216. HandleException(e, _hwnd);
  217. }
  218. return 0;
  219. }
  220. String WebChildWindow::jump_to_int(LPCTSTR url)
  221. {
  222. _navigator.goto_url(url);
  223. return url;
  224. }