Click here to Skip to main content
15,892,839 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
//*******************************************************************************
// Documents.cpp : Implementation of CDocuments
#include "stdafx.h"
#include "VisualFrameWork.h"
#include "DocumentCtrl.h"
#include "Documents.h"
/////////////////////////////////////////////////////////////////////////////
// CDocuments
STDMETHODIMP CDocuments::get_Count(long *pVal)
{
	if(!pVal) 
		return E_INVALIDARG;

	*pVal = m_listDocs.GetCount();

	return NOERROR;
}

STDMETHODIMP CDocuments::get__NewEnum(IUnknown **pVal)
{
	if(!pVal)
		return E_INVALIDARG;

	EnumDocuments* pNew = new EnumDocuments(this);

	if(!pNew)
		return E_OUTOFMEMORY;

	return pNew->QueryInterface(IID_IUnknown, (LPVOID*)pVal);
}

STDMETHODIMP CDocuments::Item(VARIANT Index, IDocument **pVal)
{
	POSITION pos = NULL;
	HRESULT hr;
	VARIANT varUse;
	VariantInit(&varUse);
	*pVal = NULL;

	if(SUCCEEDED(hr = VariantChangeType(&varUse, &Index, 0, VT_I4)))
	{
		//1 based indexing in Basic.
		pos = m_listDocs.FindIndex(varUse.lVal - 1);
		if(pos)
		{
			CTangramDoc* m_pDoc = (CTangramDoc*)m_listDocs.GetAt(pos);
			CComQIPtr<IDocument,&__uuidof(IDocument)>pDisp(m_pDoc->m_pDocCtrl);
			*pVal = pDisp;
			(*pVal)->AddRef();
			return NOERROR;
		}
	}
	else if(hr == DISP_E_TYPEMISMATCH && Index.vt == VT_BSTR)
	{
		CString strTest(Index.bstrVal);

		pos = m_listDocs.GetHeadPosition();
		while(pos)
		{
			BSTR bstrTest = L"";
			CTangramDoc* m_pDoc = (CTangramDoc*)m_listDocs.GetAt(pos);
			//CComPtr<IDocument> pTest=m_pDoc->m_pDocCtrl;
			//pTest = (IDocument*)m_pDoc->m_pDocCtrl;
			DISPID ObjectID;
			LPWSTR wszObject = OLESTR("Name");
			if (SUCCEEDED(m_pDoc->m_pDocCtrl->GetIDsOfNames(IID_NULL,&wszObject,1,LOCALE_NEUTRAL,&ObjectID))) 
			{
				DISPPARAMS dp = {NULL, NULL, 0, 0};
				VARIANT vRet;
				m_pDoc->m_pDocCtrl->Invoke(ObjectID,IID_NULL,LOCALE_NEUTRAL,DISPATCH_PROPERTYGET,&dp,&vRet,NULL,NULL);
				bstrTest = vRet.bstrVal;
			}

			if(!strTest.CompareNoCase(CString(bstrTest)))
			{
				SysFreeString(bstrTest);
				CComQIPtr<IDocument,&__uuidof(IDocument)>pDisp(m_pDoc->m_pDocCtrl);
				*pVal = pDisp;
				(*pVal)->AddRef();
				return NOERROR;
			}
			SysFreeString(bstrTest);
		}
	}

	return E_INVALIDARG;
}

STDMETHODIMP CDocuments::Open(BSTR FileName, IDocument **ppVal)
{
	CString strFileName(FileName);

	CTangramDoc* pDoc = (CTangramDoc*)AfxGetApp()->OpenDocumentFile(strFileName);

	if(pDoc)
	{
		*ppVal = (IDocument *)pDoc->m_pDocDisp;
	}
	else
	{
		return E_FAIL;
	}
	
	return S_OK;
}

STDMETHODIMP CDocuments::Add(IDocument **ppVal)
{
	return S_OK;//theApp.AutomationFileNew(ppVal);
}

HRESULT CDocuments::AddToCollection(IDocument *pDoc)
{
	//pIDoc->AddRef();
	m_listDocs.AddTail((LPVOID)pDoc);
	return NOERROR;
}

HRESULT CDocuments::RemoveFromCollection(CString strName)
{
	POSITION posCur, posDel;
	posCur = m_listDocs.GetHeadPosition();
	while(posCur)
	{
		IDocument* pDoc;
		posDel = posCur;
		pDoc = (IDocument*)m_listDocs.GetNext(posCur);
		CString s = _T("");
		s.Format(_T("%d"),pDoc);

		if(!strName.CompareNoCase(s))
		{
			m_listDocs.RemoveAt(posDel);
			//pDoc->Release();
			break;
		}
	}

	return NOERROR;
}

void CDocuments::ClearCollection()
{
	POSITION pos;
	pos = m_listDocs.GetHeadPosition();
	while(pos)
	{
		IDocument* pDoc;
		pDoc = (IDocument*)m_listDocs.GetNext(pos);
		//pDoc->Release();
	}

	m_listDocs.RemoveAll();
}

int CDocuments::GetDocsCount()
{
	return m_listDocs.GetCount();
}

/////////////////////////////////////////////////////////////////////////////
STDMETHODIMP EnumDocuments::QueryInterface(REFIID riid, LPVOID* ppvObj)
{
	if(IsEqualGUID(riid, IID_IUnknown))
	{
		*ppvObj = (LPVOID)(IUnknown*)this;
	}
	else if(IsEqualGUID(riid, IID_IEnumVARIANT))
	{
		*ppvObj = (LPVOID)(IEnumVARIANT*)this;
	}
	else
	{
		return E_NOINTERFACE;
	}

	AddRef();

	return NOERROR;
}

STDMETHODIMP EnumDocuments::Next(ULONG celt, VARIANT* rgvar, ULONG* celtFetched)
{
	ULONG ulFetched = 0;
	IDocument* pDoc;

	for(ULONG i = 0; i < celt; i++)
	{
		if(m_posCur == NULL)
			break;

		VariantInit(&(rgvar[i]));

		pDoc = (IDocument*)m_plistDocs->GetNext(m_posCur);

		V_VT(&(rgvar[i])) = VT_DISPATCH;
		pDoc->QueryInterface(IID_IDispatch, (LPVOID*)&(rgvar[i].pdispVal));

		ulFetched++;
	}

	if(celtFetched != NULL)
		*celtFetched = ulFetched;

	return ulFetched < celt ? S_FALSE : NOERROR;
}

STDMETHODIMP EnumDocuments::Skip(ULONG celt)
{
	ULONG ulSkipped = 0;
	void* pSkip;

	for(ULONG i = 0; i < celt; i++)
	{
		if(m_posCur == NULL)
			break;

		pSkip = m_plistDocs->GetNext(m_posCur);

		ulSkipped++;
	}

	return ulSkipped < celt ? S_FALSE : NOERROR;
}

STDMETHODIMP EnumDocuments::Reset()
{
	m_posCur = m_plistDocs->GetHeadPosition();

	return NOERROR;
}

STDMETHODIMP EnumDocuments::Clone(IEnumVARIANT** ppEnum)
{
	if(!ppEnum)
		return E_INVALIDARG;

	EnumDocuments* pNew = new EnumDocuments(this);

	if( !pNew ) return E_OUTOFMEMORY;

	return pNew->QueryInterface(IID_IEnumVARIANT, (LPVOID*)ppEnum);
}

EnumDocuments::EnumDocuments(CDocuments* pDocs)
{
	m_cRef = 0;
	m_plistDocs = &(pDocs->m_listDocs);
	m_posCur = m_plistDocs->GetHeadPosition();
}

EnumDocuments::EnumDocuments(EnumDocuments* pEnum)
{
	m_cRef = 0;
	m_plistDocs = pEnum->m_plistDocs;
	m_posCur = pEnum->m_posCur;
}

void CDocuments::RemoveFromCollection(CTangramDoc *pDoc)
{
	POSITION posCur, posDel;
	posCur = m_listDocs.GetHeadPosition();
	while(posCur)
	{
		/*IDocument* pDoc;
		posDel = posCur;
		pDoc = (IDocument*)m_listDocs.GetNext(posCur);
		CString s = "";
		s.Format("%d",pDoc);

		if(!strName.CompareNoCase(s))
		{
			m_listDocs.RemoveAt(posDel);
			//pDoc->Release();
			break;
		}*/
		CTangramDoc* m_pDoc = NULL;
		posDel = posCur;
		m_pDoc = (CTangramDoc*)m_listDocs.GetNext(posCur);

		if(m_pDoc == pDoc)
		{
			m_listDocs.RemoveAt(posDel);
			//pDoc->Release();
			break;
		}
	}
}

void CDocuments::AddToCollection(CTangramDoc *pDoc)
{
	IDocument* pIDoc = (IDocument*)pDoc->m_pDocDisp;
	CString strName = _T("");
	if(pDoc == g_pTangramServer->m_pLastDoc)g_pTangramServer->m_pLastDoc = NULL;
	g_pTangramServer->m_pLastDoc = pDoc;
	m_listDocs.AddTail(pDoc);
	//m_pDocsCollection->AddToCollection(pIDoc);
}

STDMETHODIMP CDocuments::Connect(long TangramServerObj)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	g_pTangramServer = (CTangramServerObj*)TangramServerObj;
	g_pTangramServer->m_pDocsCollection = this;
	ConnectClsInfo();

	return S_OK;
}

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