SimpleBrowser.cpp
上传用户:dengkfang
上传日期:2008-12-30
资源大小:5233k
文件大小:24k
源码类别:

CA认证

开发平台:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // SimpleBrowser.cpp: Web browser control
  3. /////////////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "comdef.h"
  6. #include "mshtml.h"
  7. #include "mshtmcid.h"
  8. #include "mshtmhst.h"
  9. #include "exdispid.h"
  10. #include "SimpleBrowser.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. #define Unused(parameter) parameter // avoid compile warnings
  17. // about unused parameters
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Construction and creation
  20. /////////////////////////////////////////////////////////////////////////////
  21. SimpleBrowser::SimpleBrowser()
  22. : _Browser(NULL),
  23.   _BrowserDispatch(NULL)
  24. {
  25. }
  26. SimpleBrowser::~SimpleBrowser()
  27. {
  28.     // release browser interfaces
  29. if (_Browser != NULL) {
  30. _Browser->Release();
  31. _Browser = NULL;
  32. }
  33. if (_BrowserDispatch != NULL) {
  34. _BrowserDispatch->Release();
  35. _BrowserDispatch = NULL;
  36. }
  37. }
  38. // Standard create
  39. BOOL SimpleBrowser::Create(DWORD dwStyle, 
  40.                            const RECT& rect, 
  41.                            CWnd* pParentWnd, 
  42.    UINT nID)
  43. {
  44.     BOOL results = TRUE;
  45.     _Browser     = NULL;
  46.     // create this window
  47.     CWnd *window = this;
  48. results = window->Create(AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW),
  49.                          NULL,
  50.  dwStyle,
  51.  rect,
  52.  pParentWnd,
  53.  nID);
  54.     if (!results) return FALSE;
  55.     // create browser control window as child of this window; 
  56. // this window sinks events from control
  57. CRect browser_window_rect(0,0,(rect.right - rect.left),(rect.bottom - rect.top));
  58.     results = _BrowserWindow.CreateControl(CLSID_WebBrowser,
  59.                                            NULL,
  60.                                            (WS_VISIBLE | WS_TABSTOP),
  61.                                            browser_window_rect,
  62.                                            this,
  63.                                            AFX_IDW_PANE_FIRST);
  64.     if (!results) {
  65.         DestroyWindow();
  66.         return FALSE;
  67.     }
  68.     // get control interfaces
  69.     LPUNKNOWN unknown = _BrowserWindow.GetControlUnknown();
  70.     HRESULT hr = unknown->QueryInterface(IID_IWebBrowser2,(void **)&_Browser);
  71.     if (SUCCEEDED(hr)) {
  72.         hr = unknown->QueryInterface(IID_IDispatch,(void **)&_BrowserDispatch);
  73.     }
  74.     if (!SUCCEEDED(hr)) {
  75.         _BrowserWindow.DestroyWindow();
  76.         DestroyWindow();
  77.         return FALSE;        
  78.     }
  79. // navigate to blank page; wait for document creation
  80. if (_Browser != NULL) {
  81. Navigate(_T("about:blank"));
  82. IHTMLDocument2 *document = NULL;
  83. HRESULT hr       = S_OK;
  84. while ((document == NULL) && (hr == S_OK)) {
  85. Sleep(0);
  86. IDispatch *document_dispatch = NULL;
  87. hr = _Browser->get_Document(&document_dispatch);
  88. // if dispatch interface available, retrieve document interface
  89. if (SUCCEEDED(hr) && (document_dispatch != NULL)) {
  90. // retrieve document interface
  91. hr = document_dispatch->QueryInterface(IID_IHTMLDocument2,(void **)&document);
  92. document_dispatch->Release();
  93. }
  94. }
  95. if (document != NULL) {
  96. document->Release();
  97. }
  98. }
  99.     return TRUE;
  100. }
  101. // Create in place of dialog control
  102. BOOL SimpleBrowser::CreateFromControl(CWnd *pParentWnd,UINT nID)
  103. {
  104. BOOL result = FALSE;
  105. ASSERT(pParentWnd != NULL);
  106. CWnd *control = pParentWnd->GetDlgItem(nID);
  107. if (control != NULL) {
  108. // get control location
  109. CRect rect;
  110. control->GetWindowRect(&rect);
  111. pParentWnd->ScreenToClient(&rect);
  112. // destroy control, since the browser will take its place
  113. control->DestroyWindow();
  114. // create browser
  115. result = Create((WS_CHILD | WS_VISIBLE),
  116.                 rect,
  117. pParentWnd,
  118. nID);
  119. }
  120. return result;
  121. }
  122. // Destruction
  123. void SimpleBrowser::PostNcDestroy()
  124. {
  125.     // release browser interfaces
  126. if (_Browser != NULL) {
  127. _Browser->Release();
  128. _Browser = NULL;
  129. }
  130. if (_BrowserDispatch != NULL) {
  131. _BrowserDispatch->Release();
  132. _BrowserDispatch = NULL;
  133. }
  134. }
  135. /////////////////////////////////////////////////////////////////////////////
  136. // Controls
  137. /////////////////////////////////////////////////////////////////////////////
  138. // Navigate to URL
  139. void SimpleBrowser::Navigate(LPCTSTR URL)
  140. {
  141. if (_Browser != NULL) {
  142. CString url(URL);
  143. _variant_t flags(0L,VT_I4);
  144. _variant_t target_frame_name("");
  145. _variant_t post_data("");
  146. _variant_t headers("");
  147. _Browser->Navigate(url.AllocSysString(),
  148.    &flags,
  149.    &target_frame_name,
  150.    &post_data,
  151.    &headers);
  152. }
  153. }
  154. // Navigate to HTML document in resource
  155. void SimpleBrowser::NavigateResource(int resource_ID)
  156. {
  157. if (_Browser != NULL) {
  158. CString resource_string;
  159. // load HTML document from resource
  160. HRSRC resource_handle = FindResource(AfxGetResourceHandle(),
  161.  MAKEINTRESOURCE(resource_ID),
  162.  RT_HTML);
  163. if (resource_handle != NULL) {
  164. HGLOBAL resource = LoadResource(AfxGetResourceHandle(),
  165. resource_handle);
  166. if (resource != NULL) {
  167. LPVOID resource_memory = LockResource(resource);
  168. if (resource_memory != NULL) {
  169. DWORD resource_size = SizeofResource(AfxGetResourceHandle(),
  170.  resource_handle);
  171. // identify the resource document as MBCS (e.g. ANSI)
  172. // or UNICODE
  173. bool     UNICODE_document = false;
  174. wchar_t *UNICODE_memory   = (wchar_t *)resource_memory;
  175. int      UNICODE_size     = resource_size / sizeof(wchar_t);
  176. if (UNICODE_size >= 1) {
  177. // check for UNICODE byte order mark
  178. if (*UNICODE_memory == L'xFEFF') {
  179. UNICODE_document = true;
  180. UNICODE_memory  += 1;
  181. UNICODE_size    -= 1;
  182. }
  183. // otherwise, check for UNICODE leading tag
  184. else if (UNICODE_size >= 5) {
  185. if ((UNICODE_memory[0]           == L'<') &&
  186.     (towupper(UNICODE_memory[1]) == L'H') &&
  187.     (towupper(UNICODE_memory[2]) == L'T') &&
  188.     (towupper(UNICODE_memory[3]) == L'M') &&
  189.     (towupper(UNICODE_memory[4]) == L'L')) {
  190. UNICODE_document = true;
  191. }
  192. }
  193. // Note: This logic assumes that the UNICODE resource document is 
  194. //       in little-endian byte order, which would be typical for 
  195. //       any HTML document used as a resource in a Windows application.
  196. }
  197. // convert resource document if required
  198. #if !defined(UNICODE)
  199. if (UNICODE_document) {
  200. char *MBCS_buffer = resource_string.GetBufferSetLength(resource_size + 1);
  201. int MBCS_length = ::WideCharToMultiByte(CP_ACP,
  202.                                         0,
  203.                     UNICODE_memory,
  204.                     UNICODE_size,
  205.                     MBCS_buffer,
  206.                     resource_size + 1,
  207.                     NULL,
  208.                     NULL);
  209. resource_string.ReleaseBuffer(MBCS_length);
  210. }
  211. else {
  212. resource_string = CString((char *)resource_memory,resource_size);
  213. }
  214. #else
  215. if (UNICODE_document) {
  216. resource_string = CString(UNICODE_memory,UNICODE_size);
  217. }
  218. else {
  219. wchar_t *UNICODE_buffer = resource_string.GetBufferSetLength(resource_size + 1);
  220. int UNICODE_length = ::MultiByteToWideChar(CP_ACP,
  221.                                            0,
  222.                        (const char *)resource_memory,
  223.                        resource_size,
  224.                        UNICODE_buffer,
  225.                        (resource_size + 1));
  226. resource_string.ReleaseBuffer(UNICODE_length);
  227. }
  228. #endif
  229. }
  230. }
  231. }
  232. Clear();
  233. Write(resource_string);
  234. }
  235. }
  236. // Append string to current document
  237. void SimpleBrowser::Write(LPCTSTR string)
  238. {
  239. if (_Browser != NULL) {
  240. // get document interface
  241. IHTMLDocument2 *document = GetDocument();
  242. if (document != NULL) {
  243. // construct text to be written to browser as SAFEARRAY
  244. SAFEARRAY *safe_array = SafeArrayCreateVector(VT_VARIANT,0,1);
  245. VARIANT *variant;
  246. SafeArrayAccessData(safe_array,(LPVOID *)&variant);
  247. variant->vt      = VT_BSTR;
  248. variant->bstrVal = CString(string).AllocSysString();
  249. SafeArrayUnaccessData(safe_array);
  250. // write SAFEARRAY to browser document
  251. document->write(safe_array);
  252. document->Release();
  253. document = NULL;
  254. }
  255. }
  256. }
  257. void SimpleBrowser::Clear()
  258. {
  259. if (_Browser != NULL) {
  260. // if document interface available, close/re-open document to clear display
  261. IHTMLDocument2 *document = GetDocument();
  262. HRESULT hr = S_OK;
  263. if (document != NULL) {
  264. // close and re-open document to empty contents
  265. document->close();
  266. VARIANT open_name;
  267. VARIANT open_features;
  268. VARIANT open_replace;
  269. IDispatch *open_window = NULL;
  270. ::VariantInit(&open_name);
  271. open_name.vt      = VT_BSTR;
  272. open_name.bstrVal = ::SysAllocString(L"_self");
  273. ::VariantInit(&open_features);
  274. ::VariantInit(&open_replace);
  275. hr = document->open(::SysAllocString(L"text/html"),
  276.                     open_name,
  277.                     open_features,
  278.                     open_replace,
  279.                     &open_window);
  280. if (hr == S_OK) {
  281. Refresh();
  282. }
  283. if (open_window != NULL) {
  284. open_window->Release();
  285. }
  286. }
  287. // otherwise, navigate to about:blank and wait for document ready
  288. else {
  289. Navigate(_T("about:blank"));
  290. IHTMLDocument2 *document = NULL;
  291. HRESULT hr       = S_OK;
  292. while ((document == NULL) && (hr == S_OK)) {
  293. Sleep(0);
  294. IDispatch *document_dispatch = NULL;
  295. hr = _Browser->get_Document(&document_dispatch);
  296. // if dispatch interface available, retrieve document interface
  297. if (SUCCEEDED(hr) && (document_dispatch != NULL)) {
  298. // retrieve document interface
  299. hr = document_dispatch->QueryInterface(IID_IHTMLDocument2,(void **)&document);
  300. document_dispatch->Release();
  301. }
  302. }
  303. if (document != NULL) {
  304. document->Release();
  305. }
  306. }
  307. }
  308. }
  309. // Navigation operations
  310. void SimpleBrowser::GoBack()
  311. {
  312. if (_Browser != NULL) {
  313. _Browser->GoBack();
  314. }
  315. }
  316. void SimpleBrowser::GoForward()
  317. {
  318. if (_Browser != NULL) {
  319. _Browser->GoForward();
  320. }
  321. }
  322. void SimpleBrowser::GoHome()
  323. {
  324. if (_Browser != NULL) {
  325. _Browser->GoHome();
  326. }
  327. }
  328. void SimpleBrowser::Refresh()
  329. {
  330. if (_Browser != NULL) {
  331. _Browser->Refresh();
  332. }
  333. }
  334. void SimpleBrowser::Stop()
  335. {
  336. if (_Browser != NULL) {
  337. _Browser->Stop();
  338. }
  339. }
  340. // Print contents
  341. void SimpleBrowser::Print(LPCTSTR header,LPCTSTR footer)
  342. {
  343. if (_Browser != NULL) {
  344. // construct two element SAFEARRAY;
  345. // first element is header string, second element is footer string
  346. HRESULT hr;
  347. VARIANT header_variant;
  348. VariantInit(&header_variant);
  349. V_VT(&header_variant)   = VT_BSTR;
  350. V_BSTR(&header_variant) = CString(header).AllocSysString();
  351. VARIANT footer_variant;
  352. VariantInit(&footer_variant);
  353. V_VT(&footer_variant)   = VT_BSTR;
  354. V_BSTR(&footer_variant) = CString(footer).AllocSysString();
  355. long index;
  356. SAFEARRAYBOUND parameter_array_bound[1];
  357. SAFEARRAY *parameter_array = NULL;
  358. parameter_array_bound[0].cElements = 2;
  359. parameter_array_bound[0].lLbound   = 0;
  360. parameter_array = SafeArrayCreate(VT_VARIANT,1,parameter_array_bound);
  361. index = 0;
  362. hr    = SafeArrayPutElement(parameter_array,&index,&header_variant);
  363. index = 1;
  364. hr    = SafeArrayPutElement(parameter_array,&index,&footer_variant);
  365. VARIANT parameter;
  366. VariantInit(&parameter);
  367. V_VT(&parameter)    = VT_ARRAY | VT_BYREF;
  368. V_ARRAY(&parameter) = parameter_array;
  369. // start printing browser contents
  370. hr = _Browser->ExecWB(OLECMDID_PRINT,OLECMDEXECOPT_DODEFAULT,&parameter,NULL);
  371. // Note: There is no simple way to determine that printing has completed. 
  372. //       In fact, if the browser is destroyed while printing is in progress, 
  373. //       only part of the contents will be printed.
  374. // release SAFEARRAY
  375. if (!SUCCEEDED(hr)) {
  376. VariantClear(&header_variant);
  377. VariantClear(&footer_variant);
  378. if (parameter_array != NULL) {
  379. SafeArrayDestroy(parameter_array);
  380. }
  381. }
  382. }
  383. }
  384. // Miscellaneous
  385. bool SimpleBrowser::GetBusy()
  386. {
  387.     bool busy = false;
  388. if (_Browser != NULL) {
  389. VARIANT_BOOL    variant_bool;
  390. HRESULT hr = _Browser->get_Busy(&variant_bool);
  391. if (SUCCEEDED(hr)) {
  392. busy = (variant_bool == VARIANT_TRUE);    
  393. }
  394. }
  395.     return busy;
  396. }
  397. CString SimpleBrowser::GetLocationName()
  398. {
  399.     CString location_name = _T("");
  400. if (_Browser != NULL) {
  401. BSTR location_name_BSTR = NULL;
  402. HRESULT hr = _Browser->get_LocationName(&location_name_BSTR);
  403. if (SUCCEEDED(hr)) {
  404. location_name = location_name_BSTR;
  405. }
  406. ::SysFreeString(location_name_BSTR);
  407. }
  408.     return location_name;
  409. }
  410. CString SimpleBrowser::GetLocationURL()
  411. {
  412.     CString location_URL = _T("");
  413. if (_Browser != NULL) {
  414. BSTR location_URL_BSTR = NULL;
  415. HRESULT hr = _Browser->get_LocationURL(&location_URL_BSTR);
  416. if (SUCCEEDED(hr)) {
  417. location_URL = location_URL_BSTR;
  418. }
  419. ::SysFreeString(location_URL_BSTR);
  420. }
  421.     return location_URL;
  422. }
  423. READYSTATE SimpleBrowser::GetReadyState()
  424. {
  425.     READYSTATE readystate = READYSTATE_UNINITIALIZED;
  426.     if (_Browser != NULL) {
  427. _Browser->get_ReadyState(&readystate);
  428. }
  429.     return readystate;
  430. }
  431. bool SimpleBrowser::GetSilent()
  432. {
  433.     bool silent = false;
  434. if (_Browser != NULL) {
  435. VARIANT_BOOL silent_variant;
  436. HRESULT hr = _Browser->get_Silent(&silent_variant);
  437. if (SUCCEEDED(hr)) {
  438. silent = (silent_variant == VARIANT_TRUE);
  439. }
  440. }
  441.     return silent;
  442. }
  443. void SimpleBrowser::PutSilent(bool silent)
  444. {
  445. if (_Browser != NULL) {
  446. VARIANT_BOOL silent_variant;
  447. if (silent) silent_variant = VARIANT_TRUE;
  448. else        silent_variant = VARIANT_FALSE;
  449. _Browser->put_Silent(silent_variant);
  450. }
  451. }
  452. IHTMLDocument2 *SimpleBrowser::GetDocument()
  453. {
  454. IHTMLDocument2 *document = NULL;
  455. if (_Browser != NULL) {
  456. // get browser document's dispatch interface
  457. IDispatch *document_dispatch = NULL;
  458. HRESULT hr = _Browser->get_Document(&document_dispatch);
  459. if (SUCCEEDED(hr) && (document_dispatch != NULL)) {
  460. // get the actual document interface
  461. hr = document_dispatch->QueryInterface(IID_IHTMLDocument2,
  462.                                        (void **)&document);
  463. // release dispatch interface
  464. document_dispatch->Release();
  465. }
  466. }
  467. return document;
  468. }
  469. /////////////////////////////////////////////////////////////////////////////
  470. // Message handlers
  471. /////////////////////////////////////////////////////////////////////////////
  472. BEGIN_MESSAGE_MAP(SimpleBrowser,CWnd)
  473.     //{{AFX_MSG_MAP(SimpleBrowser)
  474. ON_WM_SIZE()
  475. //}}AFX_MSG_MAP
  476. END_MESSAGE_MAP()
  477. // Resize control window as this window is resized
  478. void SimpleBrowser::OnSize(UINT nType, int cx, int cy)
  479. {
  480. CWnd::OnSize(nType, cx, cy);
  481. if (_Browser != NULL) {
  482.         CRect rect(0,0,cx,cy);
  483. _BrowserWindow.MoveWindow(&rect);
  484. }
  485. }
  486. /////////////////////////////////////////////////////////////////////////////
  487. // Event handlers
  488. /////////////////////////////////////////////////////////////////////////////
  489. BEGIN_EVENTSINK_MAP(SimpleBrowser,CWnd)
  490.     ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
  491.          DISPID_BEFORENAVIGATE2,
  492.              _OnBeforeNavigate2, 
  493.  VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
  494.     ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
  495.          DISPID_DOCUMENTCOMPLETE,
  496.              _OnDocumentComplete, 
  497.  VTS_DISPATCH VTS_PVARIANT)
  498.     ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
  499.          DISPID_DOWNLOADBEGIN,
  500.              _OnDownloadBegin, 
  501.  VTS_NONE)
  502.     ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
  503.          DISPID_PROGRESSCHANGE,
  504.              _OnProgressChange, 
  505.  VTS_I4 VTS_I4)
  506.     ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
  507.          DISPID_DOWNLOADCOMPLETE,
  508.              _OnDownloadComplete, 
  509.  VTS_NONE)
  510.     ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
  511.          DISPID_NAVIGATECOMPLETE2,
  512.              _OnNavigateComplete2, 
  513.  VTS_DISPATCH VTS_PVARIANT)
  514.     ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
  515.          DISPID_STATUSTEXTCHANGE,
  516.              _OnStatusTextChange, 
  517.  VTS_BSTR)
  518.     ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
  519.          DISPID_TITLECHANGE,
  520.              _OnTitleChange, 
  521.  VTS_BSTR)
  522. END_EVENTSINK_MAP()
  523. SimpleBrowser::Notification::Notification(HWND hwnd,UINT ID,NotificationType type)
  524. {
  525. hdr.hwndFrom = hwnd;
  526. hdr.idFrom = ID;
  527. hdr.code = type;
  528. URL = _T("");
  529. frame = _T("");
  530. post_data = NULL;
  531. post_data_size = 0;
  532. headers = _T("");
  533. progress = 0;
  534. progress_max = 0;
  535. text = _T("");
  536. }
  537. // Called before navigation begins; application may cancel if required
  538. void SimpleBrowser::_OnBeforeNavigate2(LPDISPATCH lpDisp,
  539.                                        VARIANT FAR *URL,
  540.                                        VARIANT FAR *Flags,
  541.                                        VARIANT FAR *TargetFrameName,
  542.                                        VARIANT FAR *PostData,
  543.                                        VARIANT FAR *Headers,
  544.                                        VARIANT_BOOL *Cancel)
  545. {
  546. Unused(Flags); // Note: flags value is reserved
  547.     if (lpDisp == _BrowserDispatch) {
  548. CString URL_string;
  549. CString frame;
  550. unsigned char *post_data = NULL;
  551. int post_data_size = 0;
  552. CString headers;
  553.         if ((URL       != NULL) && 
  554.     (V_VT(URL) == VT_BSTR)) {
  555.             URL_string = V_BSTR(URL);
  556.         }
  557. if ((TargetFrameName       != NULL) &&
  558.             (V_VT(TargetFrameName) == VT_BSTR)) {
  559. frame = V_BSTR(TargetFrameName);
  560.         }
  561. if ((PostData       != NULL)                    &&
  562.     (V_VT(PostData) == (VT_VARIANT | VT_BYREF))) {
  563. VARIANT *PostData_variant = V_VARIANTREF(PostData);
  564. if ((PostData_variant       != NULL) &&
  565.     (V_VT(PostData_variant) != VT_EMPTY)) {
  566. SAFEARRAY *PostData_safearray = V_ARRAY(PostData_variant);
  567. if (PostData_safearray != NULL) {
  568. char *post_data_array = NULL;
  569. SafeArrayAccessData(PostData_safearray,(void HUGEP **)&post_data_array);
  570. long lower_bound = 1;
  571. long upper_bound = 1;
  572. SafeArrayGetLBound(PostData_safearray,1,&lower_bound);
  573. SafeArrayGetUBound(PostData_safearray,1,&upper_bound);
  574. post_data_size = (int)(upper_bound - lower_bound + 1);
  575. post_data = new unsigned char[post_data_size];
  576. memcpy(post_data,post_data_array,post_data_size);
  577. SafeArrayUnaccessData(PostData_safearray);
  578. }
  579. }
  580. }
  581. bool cancel = OnBeforeNavigate2(URL_string,
  582.                                 frame,
  583. post_data,post_data_size,
  584. headers);
  585. if (Cancel != NULL) {
  586. if (cancel) *Cancel = VARIANT_TRUE;
  587. else        *Cancel = VARIANT_FALSE;
  588. }
  589. delete []post_data;
  590.     }    
  591. }
  592. bool SimpleBrowser::OnBeforeNavigate2(CString URL,
  593.                                       CString frame,
  594.   void    *post_data,int post_data_size,
  595.   CString headers)
  596. {
  597. bool cancel = false;
  598. CWnd *parent = GetParent();
  599. if (parent != NULL) {
  600. Notification notification(m_hWnd,GetDlgCtrlID(),BeforeNavigate2);
  601. notification.URL = URL;
  602. notification.frame = frame;
  603. notification.post_data = post_data;
  604. notification.post_data_size = post_data_size;
  605. notification.headers = headers;
  606. LRESULT result = parent->SendMessage(WM_NOTIFY,
  607.                                      notification.hdr.idFrom,
  608.  (LPARAM)&notification);
  609. if (result) {
  610. cancel = true;
  611. }
  612. }
  613.     return cancel;
  614. }
  615. // Document loaded and initialized
  616. void SimpleBrowser::_OnDocumentComplete(LPDISPATCH lpDisp,VARIANT *URL)
  617. {
  618.     if (lpDisp == _BrowserDispatch) {
  619. CString URL_string;
  620. if ((URL       != NULL) &&
  621.             (V_VT(URL) == VT_BSTR)) {
  622. URL_string = V_BSTR(URL);
  623.         }
  624.         OnDocumentComplete(URL_string);
  625.     }    
  626. }
  627. void SimpleBrowser::OnDocumentComplete(CString URL)
  628. {
  629. CWnd *parent = GetParent();
  630. if (parent != NULL) {
  631. Notification notification(m_hWnd,GetDlgCtrlID(),DocumentComplete);
  632. notification.URL = URL;
  633. LRESULT result = parent->SendMessage(WM_NOTIFY,
  634.                                      notification.hdr.idFrom,
  635.  (LPARAM)&notification);
  636. }
  637. }
  638. // Navigation/download progress
  639. void SimpleBrowser::_OnDownloadBegin()
  640. {
  641.     OnDownloadBegin();
  642. }
  643. void SimpleBrowser::OnDownloadBegin()
  644. {
  645. CWnd *parent = GetParent();
  646. if (parent != NULL) {
  647. Notification notification(m_hWnd,GetDlgCtrlID(),DownloadBegin);
  648. LRESULT result = parent->SendMessage(WM_NOTIFY,
  649.                                      notification.hdr.idFrom,
  650.  (LPARAM)&notification);
  651. }
  652. }
  653. void SimpleBrowser::_OnProgressChange(long progress,long progress_max)
  654. {
  655.     OnProgressChange((int)progress,(int)progress_max);
  656. }
  657. void SimpleBrowser::OnProgressChange(int progress,int progress_max)
  658. {
  659. CWnd *parent = GetParent();
  660. if (parent != NULL) {
  661. Notification notification(m_hWnd,GetDlgCtrlID(),ProgressChange);
  662. notification.progress     = progress;
  663. notification.progress_max = progress_max;
  664. LRESULT result = parent->SendMessage(WM_NOTIFY,
  665.                                      notification.hdr.idFrom,
  666.  (LPARAM)&notification);
  667. }
  668. }
  669. void SimpleBrowser::_OnDownloadComplete()
  670. {
  671.     OnDownloadComplete();
  672. }
  673. void SimpleBrowser::OnDownloadComplete()
  674. {
  675. CWnd *parent = GetParent();
  676. if (parent != NULL) {
  677. Notification notification(m_hWnd,GetDlgCtrlID(),DownloadComplete);
  678. LRESULT result = parent->SendMessage(WM_NOTIFY,
  679.                                      notification.hdr.idFrom,
  680.  (LPARAM)&notification);
  681. }
  682. }
  683. // Navigation to a link has completed
  684. void SimpleBrowser::_OnNavigateComplete2(LPDISPATCH lpDisp,VARIANT *URL)
  685. {
  686.     if (lpDisp == _BrowserDispatch) {
  687. CString URL_string;
  688. if ((URL       != NULL) &&
  689.             (V_VT(URL) == VT_BSTR)) {
  690. URL_string = V_BSTR(URL);
  691.         }
  692. OnNavigateComplete2(URL_string);
  693.     }    
  694. }
  695. void SimpleBrowser::OnNavigateComplete2(CString URL)
  696. {
  697. CWnd *parent = GetParent();
  698. if (parent != NULL) {
  699. Notification notification(m_hWnd,GetDlgCtrlID(),NavigateComplete2);
  700. notification.URL = URL;
  701. LRESULT result = parent->SendMessage(WM_NOTIFY,
  702.                                      notification.hdr.idFrom,
  703.  (LPARAM)&notification);
  704. }
  705. }
  706. // Status text has changed
  707. void SimpleBrowser::_OnStatusTextChange(BSTR bstrText)
  708. {
  709.     CString text;
  710. if (bstrText != NULL) {
  711. text = (LPCTSTR)bstrText;
  712. }
  713.     OnStatusTextChange(text);
  714. }
  715. void SimpleBrowser::OnStatusTextChange(CString text)
  716. {
  717. CWnd *parent = GetParent();
  718. if (parent != NULL) {
  719. Notification notification(m_hWnd,GetDlgCtrlID(),StatusTextChange);
  720. notification.text = text;
  721. LRESULT result = parent->SendMessage(WM_NOTIFY,
  722.                                      notification.hdr.idFrom,
  723.  (LPARAM)&notification);
  724. }
  725. }
  726. // Title text has changed
  727. void SimpleBrowser::_OnTitleChange(BSTR bstrText)
  728. {
  729.     CString text;
  730. if (bstrText != NULL) {
  731. text = (LPCTSTR)bstrText;
  732. }
  733.     OnTitleChange(text);
  734. }
  735. void SimpleBrowser::OnTitleChange(CString text)
  736. {
  737. CWnd *parent = GetParent();
  738. if (parent != NULL) {
  739. Notification notification(m_hWnd,GetDlgCtrlID(),TitleChange);
  740. notification.text = text;
  741. LRESULT result = parent->SendMessage(WM_NOTIFY,
  742.                                      notification.hdr.idFrom,
  743.  (LPARAM)&notification);
  744. }
  745. }