// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1997 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#ifndef __AFXHTML_H__
#define __AFXHTML_H__
#ifndef __AFXDISP_H__
#include <afxdisp.h>
#endif
#ifndef __exdisp_h__
#include <exdisp.h>
#endif
//REVIEW: do this or get it out of here
// #include "..\src\occimpl.h"
#ifdef _AFX_MINREBUILD
#pragma component(minrebuild, off)
#endif
#ifndef _AFX_FULLTYPEINFO
#pragma component(mintypeinfo, on)
#endif
#ifdef _AFX_PACKING
#pragma pack(push, _AFX_PACKING)
#endif
/////////////////////////////////////////////////////////////////////////////
// AFXHTML - MFC Visual HTML classes
// Classes declared in this file
//CObject
//CCmdTarget;
//CWnd
//CView
class CHtmlView;
#undef AFX_DATA
#define AFX_DATA AFX_OLE_DATA
#if 0
//REVIEW
class CIEControlSite : public COleControlSite
{
public:
CIEControlSite();
~CIEControlSite();
};
#endif
/////////////////////////////////////////////////////////////////////////////
// CHtmlView
class CHtmlView : public CView
{
protected: // create from serialization only
CHtmlView();
DECLARE_DYNCREATE(CHtmlView)
DECLARE_EVENTSINK_MAP()
// Attributes
public:
CString GetType() const;
long GetLeft() const;
void SetLeft(long nNewValue);
long GetTop() const;
void SetTop(long nNewValue);
long GetHeight() const;
void SetHeight(long nNewValue);
void SetVisible(BOOL bNewValue);
BOOL GetVisible() const;
CString GetLocationName() const;
READYSTATE GetReadyState() const;
BOOL GetOffline() const;
void SetOffline(BOOL bNewValue);
BOOL GetSilent() const;
void SetSilent(BOOL bNewValue);
BOOL GetTopLevelContainer() const;
CString GetLocationURL() const;
BOOL GetBusy() const;
LPDISPATCH GetApplication() const;
LPDISPATCH GetParentBrowser() const;
LPDISPATCH GetContainer() const;
LPDISPATCH GetHtmlDocument() const;
CString GetFullName() const;
int GetToolBar() const;
void SetToolBar(int nNewValue);
BOOL GetMenuBar() const;
void SetMenuBar(BOOL bNewValue);
BOOL GetFullScreen() const;
void SetFullScreen(BOOL bNewValue);
OLECMDF QueryStatusWB(OLECMDID cmdID) const;
BOOL GetRegisterAsBrowser() const;
void SetRegisterAsBrowser(BOOL bNewValue);
BOOL GetRegisterAsDropTarget() const;
void SetRegisterAsDropTarget(BOOL bNewValue);
BOOL GetTheaterMode() const;
void SetTheaterMode(BOOL bNewValue);
BOOL GetAddressBar() const;
void SetAddressBar(BOOL bNewValue);
BOOL GetStatusBar() const;
void SetStatusBar(BOOL bNewValue);
// Operations
public:
void GoBack();
void GoForward();
void GoHome();
void GoSearch();
void Navigate(LPCTSTR URL, DWORD dwFlags = 0,
LPCTSTR lpszTargetFrameName = NULL,
LPCTSTR lpszHeaders = NULL, LPVOID lpvPostData = NULL,
DWORD dwPostDataLen = 0);
void Navigate2(LPITEMIDLIST pIDL, DWORD dwFlags = 0,
LPCTSTR lpszTargetFrameName = NULL);
void Navigate2(LPCTSTR lpszURL, DWORD dwFlags = 0,
LPCTSTR lpszTargetFrameName = NULL, LPCTSTR lpszHeaders = NULL,
LPVOID lpvPostData = NULL, DWORD dwPostDataLen = 0);
void Navigate2(LPCTSTR lpszURL, DWORD dwFlags,
CByteArray& baPostedData,
LPCTSTR lpszTargetFrameName = NULL, LPCTSTR lpszHeader = NULL);
void Refresh();
void Refresh2(int nLevel);
void Stop();
void PutProperty(LPCTSTR lpszProperty, const VARIANT& vtValue);
void PutProperty(LPCTSTR lpszPropertyName, double dValue);
void PutProperty(LPCTSTR lpszPropertyName, LPCTSTR lpszValue);
void PutProperty(LPCTSTR lpszPropertyName, long lValue);
void PutProperty(LPCTSTR lpszPropertyName, short nValue);
BOOL GetProperty(LPCTSTR lpszProperty, CString& strValue);
COleVariant GetProperty(LPCTSTR lpszProperty);
void ExecWB(OLECMDID cmdID, OLECMDEXECOPT cmdexecopt, VARIANT* pvaIn,
VARIANT* pvaOut);
BOOL LoadFromResource(LPCTSTR lpszResource);
BOOL LoadFromResource(UINT nRes);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CHtmlView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID,
CCreateContext* pContext = NULL);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL
// Events
virtual void OnNavigateComplete2(LPCTSTR strURL);
virtual void OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags,
LPCTSTR lpszTargetFrameName, CByteArray& baPostedData,
LPCTSTR lpszHeaders, BOOL* pbCancel);
virtual void OnStatusTextChange(LPCTSTR lpszText);
virtual void OnProgressChange(long nProgress, long nProgressMax);
virtual void OnCommandStateChange(long nCommand, BOOL bEnable);
virtual void OnDownloadBegin();
virtual void OnDownloadComplete();
virtual void OnTitleChange(LPCTSTR lpszText);
virtual void OnPropertyChange(LPCTSTR lpszProperty);
virtual void OnNewWindow2(LPDISPATCH* ppDisp, BOOL* Cancel);
virtual void OnDocumentComplete(LPCTSTR lpszURL);
virtual void OnQuit();
virtual void OnVisible(BOOL bVisible);
virtual void OnToolBar(BOOL bToolBar);
virtual void OnMenuBar(BOOL bMenuBar);
virtual void OnStatusBar(BOOL bStatusBar);
virtual void OnFullScreen(BOOL bFullScreen);
virtual void OnTheaterMode(BOOL bTheaterMode);
// Implementation
public:
virtual ~CHtmlView();
CWnd m_wndBrowser;
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
IWebBrowser2* m_pBrowserApp;
// Event reflectors (not normally overridden)
protected:
virtual void NavigateComplete2(LPDISPATCH pDisp, VARIANT* URL);
virtual void BeforeNavigate2(LPDISPATCH pDisp, VARIANT* URL,
VARIANT* Flags, VARIANT* TargetFrameName, VARIANT* PostData,
VARIANT* Headers, BOOL* Cancel);
virtual void DocumentComplete(LPDISPATCH pDisp, VARIANT* URL);
// Generated message map functions
protected:
//{{AFX_MSG(CHtmlView)
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnPaint();
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
// Inline function declarations
#ifdef _AFX_PACKING
#pragma pack(pop)
#endif
#ifdef _AFX_ENABLE_INLINES
#define _AFXHTML_INLINE inline
#include <afxhtml.inl>
#endif
#undef AFX_DATA
#define AFX_DATA
#ifdef _AFX_MINREBUILD
#pragma component(minrebuild, on)
#endif
#ifndef _AFX_FULLTYPEINFO
#pragma component(mintypeinfo, off)
#endif
#endif // __AFXHTML_H__
Figure 3 viewhtml.cpp
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1997 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#ifdef AFX_HTML_SEG
#pragma code_seg(AFX_HTML_SEG)
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHtmlView
BEGIN_MESSAGE_MAP(CHtmlView, CView)
//{{AFX_MSG_MAP(CHtmlView)
ON_WM_SIZE()
ON_WM_PAINT()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
BEGIN_EVENTSINK_MAP(CHtmlView, CView)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 102 /* StatusTextChange */,
OnStatusTextChange, VTS_BSTR)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 108 /* ProgressChange */,
OnProgressChange, VTS_I4 VTS_I4)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 105 /* CommandStateChange */,
OnCommandStateChange, VTS_I4 VTS_BOOL)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 106 /* DownloadBegin */,
OnDownloadBegin, VTS_NONE)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 104 /* DownloadComplete */,
OnDownloadComplete, VTS_NONE)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 113 /* TitleChange */, OnTitleChange,
VTS_BSTR)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 252 /* NavigateComplete2 */,
NavigateComplete2, VTS_DISPATCH VTS_PVARIANT)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 250 /* BeforeNavigate2 */,
BeforeNavigate2, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT
VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 112 /* PropertyChange */,
OnPropertyChange, VTS_BSTR)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 251 /* NewWindow2 */, OnNewWindow2,
VTS_PDISPATCH VTS_PBOOL)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 259 /* DocumentComplete */,
DocumentComplete, VTS_DISPATCH VTS_PVARIANT)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 253 /* OnQuit */, OnQuit, VTS_NONE)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 254 /* OnVisible */, OnVisible,
VTS_BOOL)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 255 /* OnToolBar */, OnToolBar,
VTS_BOOL)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 256 /* OnMenuBar */, OnMenuBar,
VTS_BOOL)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 257 /* OnStatusBar */, OnStatusBar,
VTS_BOOL)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 258 /* OnFullScreen */, OnFullScreen,
VTS_BOOL)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 260 /* OnTheaterMode */, OnTheaterMode,
VTS_BOOL)
END_EVENTSINK_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHtmlView construction/destruction
CHtmlView::CHtmlView()
{
m_pBrowserApp = NULL;
}
CHtmlView::~CHtmlView()
{
if (m_pBrowserApp != NULL)
m_pBrowserApp->Release();
}
/////////////////////////////////////////////////////////////////////////////
// CHtmlView message handlers
void CHtmlView::OnDestroy()
{
RELEASE(m_pBrowserApp);
}
void CHtmlView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if (::IsWindow(m_wndBrowser.m_hWnd))
{
// need to push non-client borders out of the client area
CRect rect;
GetClientRect(rect);
::AdjustWindowRectEx(rect,
m_wndBrowser.GetStyle(), FALSE, WS_EX_CLIENTEDGE);
m_wndBrowser.SetWindowPos(NULL, rect.left, rect.top,
rect.Width(), rect.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
}
}
void CHtmlView::OnPaint()
{
Default();
}
/////////////////////////////////////////////////////////////////////////////
// CHtmlView operations
BOOL CHtmlView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
UINT nID, CCreateContext* pContext)
{
// create the view window itself
if (!CView::Create(lpszClassName, lpszWindowName,
dwStyle, rect, pParentWnd, nID, pContext))
{
return FALSE;
}
AfxEnableControlContainer();
RECT rectClient;
GetClientRect(&rectClient);
// create the control window
// AFX_IDW_PANE_FIRST is a safe but arbitrary ID
if (!m_wndBrowser.CreateControl(CLSID_WebBrowser, lpszWindowName,
WS_VISIBLE | WS_CHILD, rectClient, this, AFX_IDW_PANE_FIRST))
{
DestroyWindow();
return FALSE;
}
LPUNKNOWN lpUnk = m_wndBrowser.GetControlUnknown();
HRESULT hr = lpUnk->QueryInterface(IID_IWebBrowser2, (void**) &m_pBrowserApp);
if (!SUCCEEDED(hr))
{
m_pBrowserApp = NULL;
m_wndBrowser.DestroyWindow();
DestroyWindow();
return FALSE;
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CHtmlView operations
BOOL CHtmlView::LoadFromResource(LPCTSTR lpszResource)
{
HINSTANCE hInstance = AfxGetResourceHandle();
ASSERT(hInstance != NULL);
CString strResourceURL;
BOOL bRetVal = TRUE;
LPTSTR lpszModule = new TCHAR[_MAX_PATH];
if (GetModuleFileName(hInstance, lpszModule, _MAX_PATH))
{
strResourceURL.Format(_T("res://%s/%s"), lpszModule, lpszResource);
Navigate(strResourceURL, 0, 0, 0);
}
else
bRetVal = FALSE;
delete [] lpszModule;
return bRetVal;
}
BOOL CHtmlView::LoadFromResource(UINT nRes)
{
HINSTANCE hInstance = AfxGetResourceHandle();
ASSERT(hInstance != NULL);
CString strResourceURL;
BOOL bRetVal = TRUE;
LPTSTR lpszModule = new TCHAR[_MAX_PATH];
if (GetModuleFileName(hInstance, lpszModule, _MAX_PATH))
{
strResourceURL.Format(_T("res://%s/%d"), lpszModule, nRes);
Navigate(strResourceURL, 0, 0, 0);
}
else
bRetVal = FALSE;
delete [] lpszModule;
return bRetVal;
}
void CHtmlView::Navigate(LPCTSTR lpszURL, DWORD dwFlags /* = 0 */,
LPCTSTR lpszTargetFrameName /* = NULL */ ,
LPCTSTR lpszHeaders /* = NULL */, LPVOID lpvPostData /* = NULL */,
DWORD dwPostDataLen /* = 0 */)
{
CString strURL(lpszURL);
BSTR bstrURL = strURL.AllocSysString();
COleSafeArray vPostData;
if (lpvPostData != NULL)
{
if (dwPostDataLen == 0)
dwPostDataLen = lstrlen((LPCTSTR) lpvPostData);
vPostData.CreateOneDim(VT_UI1, dwPostDataLen, lpvPostData);
}
m_pBrowserApp->Navigate(bstrURL,
COleVariant((long) dwFlags, VT_I4),
COleVariant(lpszTargetFrameName, VT_BSTR),
vPostData,
COleVariant(lpszHeaders, VT_BSTR));
}
void CHtmlView::Navigate2(LPITEMIDLIST pIDL, DWORD dwFlags /* = 0 */,
LPCTSTR lpszTargetFrameName /* = NULL */)
{
ASSERT(m_pBrowserApp != NULL);
COleVariant vPIDL(pIDL);
COleVariant empty;
m_pBrowserApp->Navigate2(vPIDL,
COleVariant((long) dwFlags, VT_I4),
COleVariant(lpszTargetFrameName, VT_BSTR),
empty, empty);
}
void CHtmlView::Navigate2(LPCTSTR lpszURL, DWORD dwFlags /* = 0 */,
LPCTSTR lpszTargetFrameName /* = NULL */,
LPCTSTR lpszHeaders /* = NULL */,
LPVOID lpvPostData /* = NULL */, DWORD dwPostDataLen /* = 0 */)
{
ASSERT(m_pBrowserApp != NULL);
COleSafeArray vPostData;
if (lpvPostData != NULL)
{
if (dwPostDataLen == 0)
dwPostDataLen = lstrlen((LPCTSTR) lpvPostData);
vPostData.CreateOneDim(VT_UI1, dwPostDataLen, lpvPostData);
}
COleVariant vURL(lpszURL, VT_BSTR);
COleVariant vHeaders(lpszHeaders, VT_BSTR);
COleVariant vTargetFrameName(lpszTargetFrameName, VT_BSTR);
COleVariant vFlags((long) dwFlags, VT_I4);
m_pBrowserApp->Navigate2(vURL, vFlags, vTargetFrameName, vPostData, vHeaders);
}
void CHtmlView::Navigate2(LPCTSTR lpszURL, DWORD dwFlags,
CByteArray& baPostData, LPCTSTR lpszTargetFrameName /* = NULL */,
LPCTSTR lpszHeaders /* = NULL */)
{
UNUSED_ALWAYS(lpszURL);
UNUSED_ALWAYS(dwFlags);
UNUSED_ALWAYS(lpszTargetFrameName);
UNUSED_ALWAYS(baPostData);
UNUSED_ALWAYS(lpszHeaders);
ASSERT(m_pBrowserApp != NULL);
/*
COleVariant vURL;
m_pBrowserApp->Navigate2(bstrURL,
COleVariant((long) dwFlags, VT_I4),
COleVariant(lpszTargetFrameName, VT_BSTR),
COleVariant(),
COleVariant(lpszHeaders, VT_BSTR));
*/
}
void CHtmlView::PutProperty(LPCTSTR lpszProperty, const VARIANT& vtValue)
{
ASSERT(m_pBrowserApp != NULL);
CString strProp(lpszProperty);
BSTR bstrProp = strProp.AllocSysString();
m_pBrowserApp->PutProperty(bstrProp, vtValue);
::SysFreeString(bstrProp);
}
BOOL CHtmlView::GetProperty(LPCTSTR lpszProperty, CString& strValue)
{
ASSERT(m_pBrowserApp != NULL);
CString strProperty(lpszProperty);
BSTR bstrProperty = strProperty.AllocSysString();
BOOL bResult = FALSE;
VARIANT vReturn;
vReturn.vt = VT_BSTR;
vReturn.bstrVal = NULL;
HRESULT hr = m_pBrowserApp->GetProperty(bstrProperty, &vReturn);
if (SUCCEEDED(hr))
{
strValue = CString(vReturn.bstrVal);
bResult = TRUE;
}
::SysFreeString(bstrProperty);
return bResult;
}
COleVariant CHtmlView::GetProperty(LPCTSTR lpszProperty)
{
COleVariant result;
static BYTE parms[] =
VTS_BSTR;
m_wndBrowser.InvokeHelper(0x12f, DISPATCH_METHOD,
VT_VARIANT, (void*)&result, parms, lpszProperty);
return result;
}
CString CHtmlView::GetFullName() const
{
ASSERT(m_pBrowserApp != NULL);
BSTR bstr;
m_pBrowserApp->get_FullName(&bstr);
CString retVal(bstr);
return retVal;
}
/////////////////////////////////////////////////////////////////////////////
// CHtmlView event reflectors
void CHtmlView::NavigateComplete2(LPDISPATCH /* pDisp */, VARIANT* URL)
{
ASSERT(V_VT(URL) == VT_BSTR);
USES_CONVERSION;
CString str = OLE2T(V_BSTR(URL));
OnNavigateComplete2(str);
}
void CHtmlView::BeforeNavigate2(LPDISPATCH /* pDisp */, VARIANT* URL,
VARIANT* Flags, VARIANT* TargetFrameName,
VARIANT* PostData, VARIANT* Headers, BOOL* Cancel)
{
ASSERT(V_VT(URL) == VT_BSTR);
ASSERT(V_VT(TargetFrameName) == VT_BSTR);
ASSERT(V_VT(PostData) == (VT_VARIANT | VT_BYREF));
ASSERT(V_VT(Headers) == VT_BSTR);
ASSERT(Cancel != NULL);
USES_CONVERSION;
VARIANT* vtPostedData = V_VARIANTREF(PostData);
CByteArray array;
if (V_VT(vtPostedData) & VT_ARRAY)
{
// must be a vector of bytes
ASSERT(vtPostedData->parray->cDims == 1 &&
vtPostedData->parray->cbElements == 1);
vtPostedData->vt |= VT_UI1;
COleSafeArray safe(vtPostedData);
DWORD dwSize = safe.GetOneDimSize();
LPVOID pVoid;
safe.AccessData(&pVoid);
array.SetSize(dwSize);
LPBYTE lpByte = array.GetData();
memcpy(lpByte, pVoid, dwSize);
safe.UnaccessData();
}
// make real parameters out of the notification
CString strTargetFrameName(V_BSTR(TargetFrameName));
CString strURL = V_BSTR(URL);
CString strHeaders = V_BSTR(Headers);
DWORD nFlags = V_I4(Flags);
// notify the user's class
OnBeforeNavigate2(strURL, nFlags, strTargetFrameName,
array, strHeaders, Cancel);
}
void CHtmlView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
UNUSED_ALWAYS(pDisp);
ASSERT(V_VT(URL) == VT_BSTR);
CString str(V_BSTR(URL));
OnDocumentComplete(str);
}
/////////////////////////////////////////////////////////////////////////////
// Inline function declarations expanded out-of-line
#ifndef _AFX_ENABLE_INLINES
// expand inlines for Html functions
static char _szAfxHtmlInl[] = "afxhtml.inl";
#undef THIS_FILE
#define THIS_FILE _szAfxHtmlInl
#define _AFXHTML_INLINE
#include "afxhtml.inl"
#endif //!_AFX_ENABLE_INLINES
/////////////////////////////////////////////////////////////////////////////
// Pre-startup code
#ifdef AFX_INIT_SEG
#pragma code_seg(AFX_INIT_SEG)
#endif
IMPLEMENT_DYNCREATE(CHtmlView, CView)