Click here to Skip to main content
15,884,425 members
Articles / Desktop Programming / ATL

Create a Universal Document Template which supports Dynamic Frame Window Layout

Rate me:
Please Sign up or sign in to vote.
3.00/5 (8 votes)
14 Dec 20024 min read 49.3K   668   24  
Introduce a programming technology to design a very complex, rich document type.
//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This source code is a part of Tangram library.
// You may use, compile or redistribute it as part of your application 
// for free. You cannot redistribute it as a part of a software development 
// library without the agreement of the author. If the sources are 
// distributed along with the application, you should leave the original 
// copyright notes in the source code without any changes.
// This code can be used WITHOUT ANY WARRANTIES on your own risk.
// 
// For the latest updates to this library, check site:
// http://www.tangramdev.com
// 
// sunhui
//*******************************************************************************
// TangramDoc.cpp : implementation of the CTangramDoc class
//

#include "stdafx.h"
#include "TangramDoc.h"
#include "TangramDesignView.h"
#include "TangramCntrItem.h"
#include "TangramObject.h"
#include "TangramServerObj.h"
#include "ObjExtManager.h"
#include "custsite.h"

#include "TangramSubDoc.h"

#include <afxdocob.h>

//#include "..\SRC\AFXIMPL.H"       

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern void SetResource();
/////////////////////////////////////////////////////////////////////////////

BEGIN_INTERFACE_MAP(CAXDocContFrameHook, COleFrameHook)
  INTERFACE_PART(CAXDocContFrameHook, IID_IOleCommandTarget, OleCommandTarget)
END_INTERFACE_MAP()


STDMETHODIMP_(ULONG) CAXDocContFrameHook::XOleCommandTarget::AddRef()
{
	METHOD_PROLOGUE_EX_(CAXDocContFrameHook, OleCommandTarget)

	ULONG nResult = (ULONG)pThis->ExternalAddRef();

	return nResult;
}

STDMETHODIMP_(ULONG) CAXDocContFrameHook::XOleCommandTarget::Release()
{
	METHOD_PROLOGUE_EX_(CAXDocContFrameHook, OleCommandTarget)

	ULONG nResult = (ULONG)pThis->ExternalRelease();

	return nResult;
}

STDMETHODIMP CAXDocContFrameHook::XOleCommandTarget::QueryInterface(
	REFIID iid, LPVOID* ppvObj)
{
	METHOD_PROLOGUE_EX_(CAXDocContFrameHook, OleCommandTarget)

	HRESULT hr = (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);

	return hr;
}

/*
 * IOleCommandTarget methods, provided to make PowerPoint happy
 * with this frame.
 */

STDMETHODIMP CAXDocContFrameHook::XOleCommandTarget::QueryStatus(  const GUID* pguidCmdGroup, ULONG cCmds, OLECMD rgCmds[],
   OLECMDTEXT* pcmdtext)
{
	METHOD_PROLOGUE_EX_(CAXDocContFrameHook, OleCommandTarget)

	HRESULT	hr=NOERROR;

	COleCmdUI state(rgCmds, cCmds, pguidCmdGroup);
	if (pcmdtext == NULL)
		state.m_nCmdTextFlag = 0;
	else
		state.m_nCmdTextFlag = pcmdtext->cmdtextf;
				
	for (state.m_nIndex = 0; state.m_nIndex < cCmds; state.m_nIndex++)
	{
		state.m_nID = rgCmds[state.m_nIndex].cmdID;
		state.DoUpdate(g_pTangramServer->m_pCurFrame->GetParentFrame(), TRUE);
	}
				
	if (pcmdtext != NULL && pcmdtext->rgwz != NULL &&
		(pcmdtext->cmdtextf != OLECMDTEXTF_NONE))
	{
		USES_CONVERSION;
		ASSERT(cCmds == 1);
		state.m_strText = state.m_strText.Right(pcmdtext->cwBuf-1);
		pcmdtext->cwActual = state.m_strText.GetLength();
					
#ifdef _UNICODE
		lstrcpyW(pcmdtext->rgwz, (LPCTSTR) state.m_strText);
#elif defined(OLE2ANSI)
		lstrcpy(pcmdtext->rgwz, state.m_strText);
#else
		lstrcpyW(pcmdtext->rgwz, T2W((LPCTSTR) state.m_strText));
#endif
	}

	return hr;
}

        
STDMETHODIMP CAXDocContFrameHook::XOleCommandTarget::Exec(const GUID* pguidCmdGroup, DWORD nCmdID, DWORD nCmdExecOpt,
   VARIANTARG* pvarargIn, VARIANTARG* pvarargOut)
{
	METHOD_PROLOGUE_EX_(CAXDocContFrameHook, OleCommandTarget);

	HRESULT		hr=NOERROR;
	OLECMD		cmd;

	COleCmdUI	state(&cmd, 1, pguidCmdGroup);

	state.m_nIndex = 0;
	cmd.cmdf = 0;
	cmd.cmdID = nCmdID;
	state.m_nID = nCmdID;

	// help via Doc Object targeting is not supported

	if (nCmdExecOpt == OLECMDEXECOPT_SHOWHELP)
		hr = OLECMDERR_E_DISABLED;
	else
	{
		// is the command supported?

		if (!state.DoUpdate(g_pTangramServer->m_pCurFrame->GetParentFrame(), TRUE))
			hr = OLECMDERR_E_NOTSUPPORTED;
		else
		{
			if (cmd.cmdf & OLECMDF_ENABLED)
			{
				if (g_pTangramServer->m_pCurFrame->GetParentFrame()->OnCmdMsg(state.m_nID, CN_COMMAND, NULL, NULL))
					hr = S_OK;
				else
					hr = E_FAIL;
			}
			else
				hr = OLECMDERR_E_DISABLED;
		}
	}
	
	return hr;
}

BOOL CAXDocContFrameHook::OnDocActivate(BOOL bActive)
{
	ASSERT_VALID(this);
	if (m_lpActiveObject == NULL)
		return TRUE;

	// allow server to do document activation related actions
	m_lpActiveObject->OnDocWindowActivate(bActive);

	// make sure window caption gets updated later
	COleFrameHook* pNotifyHook = m_pActiveItem->m_pInPlaceFrame;
	if(pNotifyHook==NULL)return TRUE;
	pNotifyHook->m_pFrameWnd->DelayUpdateFrameTitle();

	if (!bActive)
	{
		// clear border space
		pNotifyHook->m_xOleInPlaceFrame.SetBorderSpace(NULL);
		if (m_pActiveItem->m_pInPlaceDoc != NULL)
			m_pActiveItem->m_pInPlaceDoc->m_xOleInPlaceFrame.SetBorderSpace(NULL);

		// remove the menu hook when the doc is not active
		pNotifyHook->m_xOleInPlaceFrame.SetMenu(NULL, NULL, NULL);

		// unhook top-level frame if not needed
		if (pNotifyHook != this)pNotifyHook->m_pFrameWnd->m_pNotifyHook = NULL;
	}
	else
		pNotifyHook->m_pFrameWnd->m_pNotifyHook = pNotifyHook;

	// don't do default if activating
	return bActive;
}
/////////////////////////////////////////////////////////////////////////////
// CDocCntrItem
IMPLEMENT_SERIAL(CDocCntrItem, COleClientItem, 0)
BEGIN_INTERFACE_MAP(CDocCntrItem, COleClientItem)
  INTERFACE_PART(CDocCntrItem, IID_IOleInPlaceSite, OleIPSiteEx)
END_INTERFACE_MAP()

CDocCntrItem::CDocCntrItem(COleDocument* pContainer, CString sObjID)
	: COleClientItem(pContainer)
{
	if(!pContainer||pContainer->IsKindOf(RUNTIME_CLASS(CTangramDoc)))
		if(g_pTangramServer)
		{
			m_pDoc = g_pTangramServer->m_pCurDoc;
		}
	g_pTangramServer->m_pCurTangramCntrItem = this;
	m_pMfcView = NULL;
	m_pWnd = NULL;
	m_pTangramObject=NULL;
	m_pTangramObj=NULL;
	m_pTangramWindow=NULL;
	m_pTangramWindowObj = NULL;
	m_ContainerType = Doc;
	nID = -1;
	m_strName = _T("");
	bDoc = TRUE;
	strObjID = sObjID;
}

CDocCntrItem::~CDocCntrItem()
{
}

void CDocCntrItem::Serialize(CArchive& ar)
{
	//AFX_MANAGE_STATE(AfxGetStaticModuleState())
	ASSERT_VALID(this);

	if (ar.IsStoring())
	{
		COleDocument* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		ASSERT(m_lpStorage != NULL);
		LPPERSISTSTORAGE lpPersistStorage = NULL;
		HRESULT hr = m_lpObject->QueryInterface(IID_IPersistStorage, (void**) &lpPersistStorage);
		//ASSERT(FAILED(hr) || lpPersistStorage != NULL);

		if (SUCCEEDED(hr) && ((lpPersistStorage->IsDirty() == S_FALSE) && (!ar.m_bForceFlat)))
		{
			// call OleSave now and IPersistStorage::SaveCompleted later
			hr = ::OleSave(lpPersistStorage, m_lpStorage, TRUE);
			if (FAILED(hr)) {
				AfxThrowOleException(hr);
			}
		}

		lpPersistStorage->Release();
	}

	COleClientItem::Serialize(ar);

	if (ar.IsStoring())
	{
		ar << bDoc;
		ar << m_strName;
		ar << m_ContainerType;
		ar << strObjID;
		//
		// WORKAROUND: MFC is not calling the initial OleSave if the IPersistStorage
		// is not dirty.  This results in an error further downstrean when
		// IPersistStorage::SaveCompleted is called. <OLECLI1.cpp>
		//

		// save dirty object
		// determine destination storage
	}
	else
	{
		ar >> bDoc;
		ar >> m_strName;
		int nType;
		ar >> nType;
		m_ContainerType = (ContainerType)nType;
		ar >> strObjID;
	}
	// Call base class first to read in COleClientItem data.
	// Since this sets up the m_pDocument pointer returned from
	//  CApcOleClientItem::GetDocument, it is a good idea to call
	//  the base class Serialize first.
}

/////////////////////////////////////////////////////////////////////////////
// CDocCntrItem::XOleIPSiteEx implementation
STDMETHODIMP_(ULONG) CDocCntrItem::XOleIPSiteEx::AddRef()
{
	METHOD_PROLOGUE_EX_(CDocCntrItem, OleIPSiteEx)
	return pThis->ExternalAddRef();
}

STDMETHODIMP_(ULONG) CDocCntrItem::XOleIPSiteEx::Release()
{
	//AfxMessageBox("CDocCntrItem::XOleIPSiteEx::Release()");
	METHOD_PROLOGUE_EX_(CDocCntrItem, OleIPSiteEx)
	return pThis->ExternalRelease();
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::QueryInterface(
	REFIID iid, LPVOID* ppvObj)
{
	METHOD_PROLOGUE_EX_(CDocCntrItem, OleIPSiteEx)
	return pThis->ExternalQueryInterface(&iid, ppvObj);
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::GetWindow(HWND* lphwnd)
{
	METHOD_PROLOGUE_EX_(CDocCntrItem, OleIPSiteEx)
	*lphwnd = pThis->m_hParentWnd;
	return *lphwnd != NULL ? S_OK : E_FAIL;
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::ContextSensitiveHelp(
	BOOL fEnterMode)
{
	METHOD_PROLOGUE_EX_(CDocCntrItem, OleIPSiteEx)

	if (pThis->m_pInPlaceFrame == NULL)
		return E_UNEXPECTED;

	// simply delegate to frame window implementation
	return pThis->m_pInPlaceFrame->
		m_xOleInPlaceFrame.ContextSensitiveHelp(fEnterMode);
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::CanInPlaceActivate()
{
	METHOD_PROLOGUE_EX(CDocCntrItem, OleIPSiteEx)

	return pThis->CanActivate() ? S_OK : S_FALSE;
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::OnInPlaceActivate()
{
	METHOD_PROLOGUE_EX(CDocCntrItem, OleIPSiteEx)
	ASSERT_VALID(pThis);

	SCODE sc = E_UNEXPECTED;
	TRY
	{
		pThis->OnActivate();
		sc = S_OK;
	}
	END_TRY

	return sc;
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::OnUIActivate()
{
	METHOD_PROLOGUE_EX(CDocCntrItem, OleIPSiteEx)
	ASSERT_VALID(pThis);

	SCODE sc = E_UNEXPECTED;
	TRY
	{
		pThis->OnActivateUI();
		sc = S_OK;
	}
	END_TRY

	return sc;
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::GetWindowContext(
	LPOLEINPLACEFRAME* lplpFrame,
	LPOLEINPLACEUIWINDOW* lplpDoc,
	LPRECT lpPosRect, LPRECT lpClipRect,
	LPOLEINPLACEFRAMEINFO lpFrameInfo)
{
	METHOD_PROLOGUE_EX(CDocCntrItem, OleIPSiteEx)
	ASSERT_VALID(pThis);
	*lplpFrame = NULL;  // init these in-case of mem-alloc failure
	*lplpDoc = NULL;

	CFrameWnd* pMainFrame = NULL;
	CFrameWnd* pDocFrame = NULL;

	SCODE sc = E_UNEXPECTED;
	TRY
	{
		// get position of the item relative to activation view
		CRect rect;
		pThis->OnGetItemPosition(rect);
		::CopyRect(lpPosRect, &rect);
	}
	CATCH_ALL(e)
	{
		// cleanup memory that may be partially allocated
		delete *lplpFrame;
		ASSERT(*lplpDoc == NULL);
		e->Delete();
	}
	END_CATCH_ALL

	return sc;
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::Scroll(SIZE scrollExtent)
{
	METHOD_PROLOGUE_EX(CDocCntrItem, OleIPSiteEx)
	ASSERT_VALID(pThis);

	SCODE sc = E_UNEXPECTED;
	TRY
	{
		if (!pThis->OnScrollBy(CSize(scrollExtent)))
			sc = S_FALSE;
		else
			sc = S_OK;
	}
	END_TRY

	return sc;
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::OnUIDeactivate(BOOL fUndoable)
{
	METHOD_PROLOGUE_EX(CDocCntrItem, OleIPSiteEx)
	ASSERT_VALID(pThis);

	SCODE sc = E_UNEXPECTED;
	pThis->m_pView = pThis->m_pMfcView;
	if(pThis->m_pView&&::IsWindow(pThis->m_pView->m_hWnd))
	{
		TRY
		{
			pThis->GetDocument();
			pThis->OnDeactivateUI(fUndoable);
			sc = S_OK;
		}
		END_TRY
	}

	return sc;
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::OnInPlaceDeactivate()
{
	METHOD_PROLOGUE_EX(CDocCntrItem, OleIPSiteEx)
	ASSERT_VALID(pThis);
	SCODE sc = E_UNEXPECTED;
	TRY
	{
		pThis->OnDeactivate();
		sc = S_OK;
	}
	END_TRY

	return sc;
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::DiscardUndoState()
{
	METHOD_PROLOGUE_EX(CDocCntrItem, OleIPSiteEx)
	ASSERT_VALID(pThis);

	SCODE sc = E_UNEXPECTED;
	TRY
	{
		pThis->OnDiscardUndoState();
		sc = S_OK;
	}
	END_TRY

	return sc;
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::DeactivateAndUndo()
{
	METHOD_PROLOGUE_EX(CDocCntrItem, OleIPSiteEx)
	ASSERT_VALID(pThis);

	SCODE sc = E_UNEXPECTED;
	TRY
	{
		pThis->OnDeactivateAndUndo();
		sc = S_OK;
	}
	END_TRY

	return sc;
}

STDMETHODIMP CDocCntrItem::XOleIPSiteEx::OnPosRectChange(
	LPCRECT lpPosRect)
{
	METHOD_PROLOGUE_EX(CDocCntrItem, OleIPSiteEx)
	ASSERT_VALID(pThis);

	SCODE sc = E_UNEXPECTED;
	TRY
	{
		CRect rect;
		rect.CopyRect(lpPosRect);
		pThis->OnChangeItemPosition(rect);
		sc = S_OK;
	}
	END_TRY

	return sc;
}
/////////////////////////////////////////////////////////////////////////////
// CTangramDoc

IMPLEMENT_DYNCREATE(CTangramDoc, COleDocument)

BEGIN_MESSAGE_MAP(CTangramDoc, COleDocument)
	//{{AFX_MSG_MAP(CTangramDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTangramDoc construction/destruction

CTangramDoc::CTangramDoc()
{
	m_pSubDoc = NULL;
	m_bOpened = false;
	m_bShowAxItem = false;
	hBackBmp = NULL;
	m_pExternalObj = NULL;
	m_bMainDoc = false;
	if(g_pTangramServer)
	{
		g_pTangramServer->m_pCurDoc = this;
		if(g_pTangramServer->m_bMDI==false)
			m_pSDIFrame = g_pTangramServer->m_pSDIFrame;
	}
	SetResource();
	// Use OLE compound files
	EnableCompoundFile(TRUE);

	// TODO: add one-time construction code here
	m_pFrame = NULL;
	m_pFrameObj = NULL;
	m_pSDIFrame = NULL;
	m_bHasDockState = true;
	//TangramObjID = 0;
	m_DockingObjArray.RemoveAll();
	m_MfcObjectArray.RemoveAll();
	m_DockingObjCount = 0;
	m_pDocItem = NULL;
	m_pComObj = NULL;
	m_pDocDisp = NULL;
	m_strInitialInfo = _T("");
	strComObjID = _T("");
	m_Context.m_pCurrentDoc = this;
	m_pActiveDesignView = NULL;
}

CTangramDoc::~CTangramDoc()
{
	m_pFrame = NULL;
}

BOOL CTangramDoc::OnNewDocument()
{
	if (!COleDocument::OnNewDocument())
		return FALSE;

	CTangramObject* pObj = new CTangramObject();
	m_TangramObjectArray.SetAtGrow(0, pObj);

	m_pDocItem = new CDocCntrItem(this);

	CLSID id;
	CLSIDFromProgID(g_pTangramServer->m_strDocCtrlID.AllocSysString(),&id);
	POSITION nPos;
	nPos = GetFirstViewPosition();
	CTangramDesignView* m_pView = (CTangramDesignView*)GetNextView(nPos);
	m_pView->m_pTangramObject = pObj;
	g_pTangramServer->m_pCurDoc = this;
	if(g_pTangramServer->m_pDocsCollection)
		g_pTangramServer->m_pDocsCollection->AddToCollection(this);

	if (!m_pDocItem->CreateNewItem(id,OLERENDER_NONE))
		AfxThrowMemoryException();
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CTangramDoc server implementation

////////////////////////////////////////////////////////////////////////////
// CTangramDoc serialization

void CTangramDoc::Serialize(CArchive& ar)
{
	if(m_bMainDoc)m_bHasDockState=true;
	COleDocument::Serialize(ar);
	if(m_pExternalObj)m_pExternalObj->Serialize(ar);
	int nCount = 0;

    if (ar.IsStoring())
	{
		ar << m_strInitialInfo;
		m_ObArray.RemoveAll();
		int nObjCount = m_TangramObjectArray.GetSize();
		if(nObjCount)
			for(int i = 0; i < nObjCount; i++)
				m_ObArray.SetAtGrow(i,m_TangramObjectArray[i]);

		ar << strComObjID;
		ar << m_bHasDockState;
		if(m_bMainDoc)
		{
			if(g_pTangramServer->m_bMDI==false)
				m_pSDIFrame->GetDockState(m_DockState);
			else
			{
				g_pTangramServer->m_pMainFrame->GetDockState(m_DockState);
			}
			m_DockState.Serialize(ar);
		}
	}
	else
	{
		ar >> m_strInitialInfo;
		ar >> strComObjID;
		ar >> m_bHasDockState;
		if(m_bMainDoc)
		{
			m_DockState.Serialize(ar);
		}
	}
	m_ObArray.Serialize(ar);

	if(ar.IsStoring()==false)
	{
		CreateFrame();
	}
}

/////////////////////////////////////////////////////////////////////////////
// CTangramDoc diagnostics

#ifdef _DEBUG
void CTangramDoc::AssertValid() const
{
	COleDocument::AssertValid();
}

void CTangramDoc::Dump(CDumpContext& dc) const
{
	COleDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTangramDoc commands

void CTangramDoc::OnCloseDocument() 
{
	if(m_pComObj)
	{
		m_pComObj->Release();
		//delete m_pComObj;
	}
	if(g_pTangramServer&&g_pTangramServer->m_bMDI)
		g_pTangramServer->m_pDocsCollection->RemoveFromCollection(this);
	COleDocument::OnCloseDocument();
}

BOOL CTangramDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if(g_pTangramServer->m_pDocsCollection)
		g_pTangramServer->m_pDocsCollection->AddToCollection(this);
	if (!COleDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	m_bAutoDelete = true;
	if(m_pDocDisp)
	{
		hBackBmp = m_pDocDisp->GetBitmapFromDoc();
	}

	// TODO: add loading code here
	m_bOpened = true;
	SetTitle(m_strTitle);
	return TRUE;
}

BOOL CTangramDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
	if (!COleDocument::OnSaveDocument(lpszPathName))
		return FALSE;

	//SetModifiedFlag(FALSE);
	return TRUE;
}

void CTangramDoc::DeleteContents() 
{
	int nObjCount = m_TangramObjectArray.GetSize();
	if(nObjCount)
	{
		for(int i = 0; i < nObjCount; i++)
			delete m_TangramObjectArray[i];
	}
	m_TangramObjectArray.RemoveAll();
	m_ObArray.RemoveAll();
	COleDocument::DeleteContents();
}

void CTangramDoc::SetTitle(LPCTSTR lpszTitle) 
{
	// TODO: Add your specialized code here and/or call the base class
	USES_CONVERSION;
	if(m_pDocDisp)
	{
		BSTR bstrTitle = L"";

		DISPID ObjectID;
		LPWSTR wszObject = OLESTR("Caption");
		BSTR bstrCap = L"";
		if (SUCCEEDED(m_pDocCtrl->GetIDsOfNames(IID_NULL,&wszObject,1,LOCALE_NEUTRAL,&ObjectID))) 
		{
			DISPPARAMS dp = {NULL, NULL, 0, 0};
			VARIANT vRet;
			m_pDocCtrl->Invoke(ObjectID,IID_NULL,LOCALE_NEUTRAL,DISPATCH_PROPERTYGET,&dp,&vRet,NULL,NULL);
			bstrTitle = vRet.bstrVal;
		}
		m_strTitle = OLE2T(bstrTitle);
		COleDocument::SetTitle(m_strTitle);
		m_pFrame->SetWindowText(m_strTitle);
	}
	else
		COleDocument::SetTitle(lpszTitle);
}

BOOL CTangramDoc::SaveModified() 
{	
	return COleDocument::SaveModified();
}

void CTangramDoc::CreateFrame()
{
	ASSERT(m_pDocDisp);
	int nCount = 0;
	// Iterate through the entire map, dumping both name and age.
	POSITION nPos;
	nPos = GetFirstViewPosition();
	CView* pView = GetNextView(nPos);
	ASSERT(pView);
	m_bAutoDelete = false;
	pView->DestroyWindow();
	m_bAutoDelete = true;

	// TODO: add loading code here
	nPos = GetStartPosition();
	COleClientItem* pItem = NULL;
	int nIndex = 0;
	HRESULT hr = NOERROR;

	while ((pItem = GetNextClientItem(nPos)) != NULL)
	{
		if (pItem->IsKindOf(RUNTIME_CLASS(CTangramCntrItem)))
			((CTangramCntrItem*)pItem)->m_pDoc = this;
		if (pItem->IsKindOf(RUNTIME_CLASS(CDocCntrItem)))
		{
			CDocCntrItem* m_pTmpItem = (CDocCntrItem*)pItem;
			if(m_pTmpItem->bDoc)
			{
				m_pDocItem = (CDocCntrItem*)m_pTmpItem;
			}
			if(m_pTmpItem->m_ContainerType==Board)
			{
				m_BoardArray.SetAtGrow(nIndex, m_pTmpItem);
				nIndex++;
			}
		}
	}

	int nObjCount = m_ObArray.GetSize();
	if(nObjCount)
	{
		for(int i = 0; i < nObjCount; i++)
		{
			m_TangramObjectArray.SetAtGrow(i,(CTangramObject*&)m_ObArray[i]);
			m_TangramObjectArray[i]->AttachDocCntrItem();
			m_TangramObjectArray[i]->Create();
		}
	}

	if(strComObjID!=_T(""))
	{
		CComPtr<IDispatch> pDisp;
		HRESULT hr = pDisp.CoCreateInstance(strComObjID.AllocSysString());
		if(hr == S_OK)
		{
			m_pComObj = pDisp;
			m_pComObj->AddRef();
			BSTR szMember1=L"TangramDoc";
			DISPID dispid1;
			DISPPARAMS dispparams1;
			hr = m_pComObj->GetIDsOfNames(IID_NULL, &szMember1, 1, LOCALE_USER_DEFAULT, &dispid1);
			if(hr == S_OK)
			{
				DISPID dispidarg1=DISPID_PROPERTYPUT;
				VARIANTARG rgvarg1[1];
				dispparams1.cArgs=1;
				dispparams1.rgdispidNamedArgs=&dispidarg1;
				dispparams1.rgvarg=rgvarg1;
				dispparams1.rgvarg[0].vt=VT_DISPATCH;
				dispparams1.rgvarg[0].pdispVal=m_pDocCtrl;
				dispparams1.cNamedArgs=1;
				hr = m_pComObj->Invoke(dispid1,	IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYPUT, &dispparams1, NULL,NULL,	NULL);
			
				if(hr != S_OK)
				{
					m_pComObj->Release();
					return ;
				}			
			}
		}
	}

	if(g_pTangramServer->m_bMDI==false)
	{
		m_pSDIFrame->SetDockState(m_DockState);
		m_pSDIFrame->RecalcLayout();	
	}
	m_bAutoDelete = true;
}

void CTangramDoc::CreateDockingBar(CString strCaption)
{
	strCaption.Trim();
	if(strCaption==_T(""))return;
	if(m_bMainDoc)
	{
		CFrameWnd *pFrame = m_pFrame;
		pFrame = pFrame->GetParentFrame();
		CTangramDockingObjImpl* m_pDockingBarObj = (CTangramDockingObjImpl*)g_pTangramServer->m_pTangramDockingBarRuntimeClass->CreateObject();

		m_pDockingBarObj->m_pTangramObject = new CTangramObject();
		m_pDockingBarObj->m_pTangramObject->isDocking = true;

		int nObjCount = m_TangramObjectArray.GetSize();
		m_TangramObjectArray.SetAtGrow( m_TangramObjectArray.GetSize(),m_pDockingBarObj->m_pTangramObject);//�����¶������
		m_DockingObjArray.SetAtGrow(m_DockingObjCount, m_pDockingBarObj);
		m_DockingObjCount++;
		m_pDockingBarObj->m_pWnd = NULL;


		CFrameWnd* pDockFrame = m_pDockingBarObj->CreateDockingFrame(strCaption,pFrame);
		m_Context.m_pNewViewClass = g_pTangramServer->m_pDesignViewRuntimeClass;
		CTangramDesignView* m_pView = (CTangramDesignView*)(pDockFrame->CreateView(&m_Context));
		m_pView->m_pTangramObject = m_pDockingBarObj->m_pTangramObject;
		m_pView->m_pTangramObject->isDocking=TRUE;
		m_pView->OnInitialUpdate();
		m_bAutoDelete = true;
		return ;
	}
}

void CTangramDoc::ShowAxItem(void)
{
	POSITION nPos = GetStartPosition();
	COleClientItem* pItem = NULL;
	int nIndex = 0;
	HRESULT hr = NOERROR;

	while ((pItem = GetNextClientItem(nPos)) != NULL)
	{
		if (pItem->IsKindOf(RUNTIME_CLASS(CTangramCntrItem)))
		{
			((CTangramCntrItem*)pItem)->m_pDoc = this;
			if(!((CTangramCntrItem*)pItem)->bShow)
			{
				((CTangramCntrItem*)pItem)->DoVerb(OLEIVERB_SHOW,((CTangramCntrItem*)pItem)->m_pMfcView);
				((CTangramCntrItem*)pItem)->bShow=true;
			}
		}
	}
}

HRESULT CTangramDoc::CreateSubDocStorage(CTangramSubDoc* pSubDoc)
{
	ASSERT(m_lpRootStg!=NULL);
	ASSERT(pSubDoc!=NULL);
	HRESULT hr = E_FAIL;

	hr = m_lpRootStg->OpenStorage(pSubDoc->m_strStorageName.AllocSysString(),NULL,STGM_READWRITE|STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE,0,0,&pSubDoc->m_lpSubDocStg);
	if(hr!=S_OK)
	{
		hr = m_lpRootStg->CreateStorage(pSubDoc->m_strStorageName.AllocSysString(),STGM_READWRITE|STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_CREATE,0,0,&pSubDoc->m_lpSubDocStg);
		SetModifiedFlag();
	}
	return hr;
}

void CTangramDoc::DeleteSubDoc(CString strDocStorageName)
{
	ASSERT(m_lpRootStg!=NULL);
	HRESULT hr = E_FAIL;
	hr = m_lpRootStg->DestroyElement(strDocStorageName.AllocSysString());
	ASSERT(SUCCEEDED(hr));
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions