Click here to Skip to main content
15,881,789 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.2K   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
//*******************************************************************************
// ObjExtManager.cpp : implementation file
//

#include "stdafx.h"
#include "ObjExtManager.h"
#include "OXTabClientWnd.h"
#include "TangramServerObj.h"
#include "TangramObject.h"
//#include "DockingTangramBar.h"
#include "TangramDoc.h"
#include "TabWnd.h"
#include "TabInfo.h"
#include "DialogMessageHook.h"
#include "TangramDesignView.h"
//#include "DesignerDoc.h"
#include "ObjExt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CRuntimeClass* g_pLibClass = NULL;
CWnd* m_pClientWnd = NULL;
CTangramServerObj* g_pTangramServer=NULL;

void SetResource()
{
	AfxSetResourceHandle(AfxGetApp()->m_hInstance);
}

HRESULT ProcessDlgMsg(BOOL bProcess, HWND hWnd)
{
	if(bProcess)
		return CDialogMessageHook::InstallHook(hWnd);
	else
		return CDialogMessageHook::UninstallHook(hWnd);
};

void InitLib()
{
	AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
	g_pLibClass = pModuleState->m_classList;
};

void ConnectClsInfo()
{
	AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
	CRuntimeClass* pClass;
	AfxLockGlobals(CRIT_RUNTIMECLASSLIST);
	for (pClass = pModuleState->m_classList; pClass != NULL;
		pClass = pClass->m_pNextClass)
	{
		if(pClass->m_pNextClass==NULL)
		{
			if(g_pLibClass)
			{
				pClass->m_pNextClass = g_pLibClass;
				g_pLibClass = NULL;
				break;
			}
		}
	}
	AfxUnlockGlobals(CRIT_RUNTIMECLASSLIST);
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(CConnectObj, CObject)

CConnectObj::CConnectObj()
{
	m_pViewClass = NULL;
	m_pParentWnd = NULL;
	m_pObjExtManager = NULL;
	m_pMfcObj = NULL;
	m_pObjDisp = NULL;
	m_pDocDisp = NULL;
	m_pAppDisp = NULL;
	nChildID = AFX_IDW_PANE_FIRST;
	m_pContext = NULL;
}

void CConnectObj::Connect(BOOL bLoading)
{
}

CConnectObj::~CConnectObj()
{

}
/////////////////////////////////////////////////////////////////////////////
// CTangramManager

//IMPLEMENT_SERIAL(CTangramManager, CObject, 0)

CTangramManager::CTangramManager(CFrameWnd *pFrameWnd, CRuntimeClass* pFrameClass, CRuntimeClass* pDockBarClass, CString strServerID, IDispatch* pAppDisp,CRuntimeClass* pDesignClsInfo, BOOL bMDI, BOOL bMDITab)
{
	AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
	m_pLibClass = pModuleState->m_classList;
	
	m_pMDITabClientWnd=NULL;
	m_bMdiTab = bMDITab;
	m_pApp=NULL;
	m_pTGObj = new CTangramServerObj();
	m_pTGObj->m_pObjExtManager=this;
	m_pTGObj->m_pMainFrame = pFrameWnd;
	m_bMdi = bMDI;
	if(bMDI&&bMDITab)
	{
		m_pMDITabClientWnd = new COXTabClientWnd();
		((COXTabClientWnd*)m_pMDITabClientWnd)->Attach((CMDIFrameWnd*)pFrameWnd);
		((COXTabClientWnd*)m_pMDITabClientWnd)->m_pObjExtManager = this;
	}
	else
		m_pTGObj->m_pSDIFrame = pFrameWnd;
	m_pTangramServerDisp.CoCreateInstance(strServerID.AllocSysString());

	DISPID ObjectID;
	LPWSTR wszObject = OLESTR("Connect");
	if (SUCCEEDED(m_pTangramServerDisp->GetIDsOfNames(IID_NULL,&wszObject,1,LOCALE_NEUTRAL,&ObjectID))) 
	{
		CComVariant avarParams[1];
		avarParams[0] = (long)m_pTGObj;
		CComVariant varResult;

		DISPPARAMS params = { avarParams, NULL, 1, 0 };
		HRESULT hr = m_pTangramServerDisp->Invoke(ObjectID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &params, &varResult, NULL, NULL);
	}
	m_pTGObj->m_pTangramDockingBarRuntimeClass = pDockBarClass;
	m_pTGObj->m_pTangramMDIFrameRuntimeClass = pFrameClass;
	pDesignClsInfo?m_pTGObj->m_pDesignViewRuntimeClass = pDesignClsInfo:m_pTGObj->m_pDesignViewRuntimeClass = RUNTIME_CLASS(CTangramDesignView);
	if(bMDI&&bMDITab)
		m_pTGObj->m_pClientWnd = m_pMDITabClientWnd;
	else
		m_pTGObj->m_bMDI=m_bMdi;
}

CTangramManager::~CTangramManager()
{
	g_pTangramServer=NULL;
	delete m_pTGObj;
	m_pTGObj=NULL;
}


/////////////////////////////////////////////////////////////////////////////
// CTangramManager message handlers

void CTangramManager::NewDocument(CString strDocType)
{
 	POSITION curTemplatePos = AfxGetApp()->GetFirstDocTemplatePosition();

 	while(curTemplatePos != NULL)
 	{
 		CDocTemplate* curTemplate =
 			AfxGetApp()->GetNextDocTemplate(curTemplatePos);
 		CString str;
 		curTemplate->GetDocString(str, CDocTemplate::docName);
		strDocType.MakeLower();
		str.MakeLower();
 		if(str == strDocType)
 		{
 			curTemplate->OpenDocumentFile(NULL);
 			return;
 		}
 	}
}

BOOL CTangramManager::SetBitmap(HBITMAP hBitmap)
{
	if(m_bMdiTab)
		((COXTabClientWnd*)m_pMDITabClientWnd)->SetBitmap(hBitmap,0);	
	return 0;
}

HBITMAP CTangramWindowObj::GetBitmap()
{
	TRACE0("Call me here\n");
	return NULL;
}

void CTangramWindowObj::ShowPropertyPage()
{
	CComQIPtr<IDispatch, &IID_IDispatch> spControl(m_pOleItem->m_lpObject);
    ISpecifyPropertyPages  *pISPP;    
	CAUUID                  caGUID;    
	HRESULT                 hr;    
	if (FAILED(spControl->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pISPP)))return;     
	hr=pISPP->GetPages(&caGUID);    
	pISPP->Release();   
	if (FAILED(hr))return;            
	IDispatch* m_pIDispatch = spControl;
	hr=OleCreatePropertyFrame(m_pBorderWnd->m_hWnd, 10, 10, L"TabWnd" , 1, (IUnknown **)&m_pIDispatch, caGUID.cElems  , caGUID.pElems, LOCALE_USER_DEFAULT, 0L, NULL);   
	if (FAILED(hr))   
	{
	}
	CoTaskMemFree((void *)caGUID.pElems);    
	return;
}

void CTangramManager::AddMfcObject(CRuntimeClass *pMfcObjInfo,CRuntimeClass *pMfcConnectObjInfo,CString strHeader)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
	CString strAppName = _T("");
	strAppName = pModuleState->m_lpszCurrentAppName;
	ASSERT(pMfcObjInfo);
	void* pObjInfo = NULL;

	CString strClsInfo = _T("");
	if(strAppName==_T(""))
		strClsInfo = pMfcObjInfo->m_lpszClassName;
	else
	{
		int nPos = -1;
		nPos = m_pTGObj->m_strCurExternCnn.Find(_T("."));
		CString str = _T(""); 
		if(!(nPos==-1))
			str = m_pTGObj->m_strCurExternCnn.Left(nPos);
		if(str == _T(""))str = strHeader;
		strClsInfo.Format("%s.%s",str,pMfcObjInfo->m_lpszClassName);
	}
	strClsInfo.MakeLower();
	if(!m_pTGObj->m_MfcObjInfoDictionary.Lookup(LPCTSTR(strClsInfo),pObjInfo))
	{
		if(!(m_pTGObj->m_strCurExternCnn==_T("")))
			m_pTGObj->m_MfcConnectExternalInfoDictionary[LPCTSTR(strClsInfo)] = m_pTGObj->m_strCurExternCnn;
		m_pTGObj->m_MfcObjInfoDictionary[strClsInfo] = pMfcObjInfo;
		if(pMfcConnectObjInfo&&pMfcConnectObjInfo->IsDerivedFrom(RUNTIME_CLASS(CConnectObj)))
			m_pTGObj->m_MfcConnectObjInfoDictionary[strClsInfo] = pMfcConnectObjInfo;
	}
}

void CTangramWindowObj::IniContainer(BOOL isLoading)
{
	if(m_pOleItem->strObjID!=_T(""))
	{
		CString  strObjID = m_pOleItem->strObjID;
		CComPtr<IDispatch> pDisp = NULL;
		pDisp.CoCreateInstance(strObjID.AllocSysString());
		m_pOleItem->m_pTangramWindowObj->m_pWindowDisp=pDisp.p;
		m_pOleItem->m_pTangramWindowObj->m_pWindowDisp->AddRef();
		if(!m_pTangramObject)
			m_pTangramObject = ((CDocCntrItem*)m_pOleItem)->m_pTangramObject;

		CComQIPtr<IMfcWndContainer,&__uuidof(IMfcWndContainer)>SpMfcContainer(pDisp);
		long pConnectionInfo;
		SpMfcContainer->GetViewClsInfo(L"", &pConnectionInfo);
		m_pTangramObject->m_pConObj = (CConnectObj*)pConnectionInfo;
		ASSERT(m_pTangramObject->m_pConObj);
		m_pTangramObject->m_pRuntimeClass = (CRuntimeClass*)m_pTangramObject->m_pConObj->m_pViewClass;
		m_pBorderWnd = (CWnd*)m_pTangramObject->m_pRuntimeClass->CreateObject();
		g_pTangramServer->m_pCurBoardWnd = m_pBorderWnd;
		///////������Ӷ���
		m_pTangramObject->m_pConObj->m_pMfcObj = g_pTangramServer->m_pCurBoardWnd;
		ASSERT(m_pTangramObject->m_pConObj->m_pMfcObj);
		m_pTangramObject->m_pConObj->m_pObjExtManager = g_pTangramServer->m_pObjExtManager;
		m_pTangramObject->m_pConObj->m_pTangramObj = m_pTangramObject;
		m_pTangramObject->m_pConObj->Connect(false);
		m_pOleItem->m_pWnd = m_pBorderWnd;
		m_pTangramObject->m_pWnd = g_pTangramServer->m_pCurBoardWnd;
		((CDocCntrItem*)m_pOleItem)->m_strName = m_pTangramObject->m_strName;
		((CDocCntrItem*)m_pOleItem)->m_pWnd = m_pBorderWnd;
	}
	else
	{
		m_pBorderClass = RUNTIME_CLASS(CTabWnd);
		m_pBorderWnd = (CWnd*)m_pBorderClass->CreateObject();
		((CTabWnd*)m_pBorderWnd)->m_pBoard = this;
		m_pOleItem->m_pWnd = m_pBorderWnd;
	}
	g_pTangramServer->m_pCurBoardWnd = m_pBorderWnd;
	g_pTangramServer->m_pCurTangramCntrItem ->m_pWnd = m_pBorderWnd;
}

void CTangramWindowObj::SetTangramObj(CObject *m_pObj)
{
	if(m_pObj->IsKindOf(RUNTIME_CLASS(CTangramObject)))
		m_pTangramObject = (CTangramObject*)m_pObj;
	else if(m_pObj->IsKindOf(RUNTIME_CLASS(CTangramObj)))
		m_pTangramObj = (CTangramObj*)m_pObj;
}

int CTangramDocuments::GetDocsCount()
{
	return 0;
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
BOOL CTangramManager::CloseDocuments()
{
	CFrameWnd* pMainWnd = (CFrameWnd*)AfxGetMainWnd();
	CDocument* pDocument = pMainWnd->GetActiveDocument();
	if (pDocument != NULL && !pDocument->CanCloseFrame(pMainWnd))
	{
		// document can't close right now -- don't close it
		return FALSE;
	}

	// attempt to save all documents
	if (pDocument == NULL && !m_pApp->SaveAllModified())//AfxGetApp()
		return FALSE;     // don't close it

	if(m_bMdi&&m_bMdiTab)
	{
		((COXTabClientWnd*)m_pMDITabClientWnd)->Detach();
		delete m_pMDITabClientWnd;
	}
	// close all documents first
	g_pTangramServer->m_bClose=TRUE;
	if(m_bMdi)
	{
		CTangramDoc* pMainDoc = NULL;
		ASSERT(g_pTangramServer->m_pDocsCollection);
		int nDocCount = g_pTangramServer->m_pDocsCollection->GetDocsCount();
		if(nDocCount)
		{
			POSITION posCur;
			int nCount = 0;
			posCur = g_pTangramServer->m_pDocsCollection->m_listDocs.GetHeadPosition();
			while(posCur&&nCount<nDocCount)
			{
				CTangramDoc* pDoc = NULL;
				pDoc = (CTangramDoc*)g_pTangramServer->m_pDocsCollection->m_listDocs.GetNext(posCur);
				nCount++;
				if(pDoc->m_bMainDoc)
					pMainDoc = pDoc;
				else
					pDoc->OnCloseDocument();
			}
			if(pMainDoc)pMainDoc->OnCloseDocument();
		}
		ASSERT(m_pApp->m_pDocManager != NULL);
		m_pApp->m_pDocManager->CloseAllDocuments(false);
	}
	else
		pDocument->DeleteContents();
	return TRUE;
}

//////////////////////////////////////////////////////////////////////
// CTangramFrameObj Class
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTangramFrameObj::CTangramFrameObj()
{
	m_pDoc=NULL;
	m_pFrameWnd = NULL;
	m_pOwner = NULL;
	m_bActiveFrame = false;
	if(g_pTangramServer)
	{
		m_pDoc = g_pTangramServer->m_pCurDoc;
		g_pTangramServer->m_pCurFrameObj = this;
		m_pDoc->m_pFrameObj = this;
		ASSERT(m_pDoc);
	}
}

CTangramFrameObj::~CTangramFrameObj()
{

}

void CTangramFrameObj::OnDestroy()
{
	if(!m_bActiveFrame)
	{
		for(int i = 0; i < m_pDoc->m_DockingObjArray.GetSize(); i++)
		{
			CWnd* pDockingTangramBar = (CWnd*)(((CTangramDockingObjImpl*)(m_pDoc->m_DockingObjArray[i]))->m_pDockingWnd);
			if(m_pDoc->m_bMainDoc)
			{
				ASSERT(((CTangramDockingObjImpl*)(m_pDoc->m_DockingObjArray[i]))->m_pDockingFrame);
				((CTangramDockingObjImpl*)(m_pDoc->m_DockingObjArray[i]))->m_pDockingFrame->DestroyWindow();
			}
			else
			{
				pDockingTangramBar->DestroyWindow();
				delete pDockingTangramBar;
			}
		}
	}
}

void CTangramFrameObj::OnActivate(BOOL bActivate)
{
	if(m_pDoc&&m_pDoc->m_bOpened&&!m_pDoc->m_bShowAxItem)
	{
		m_pDoc->m_bShowAxItem=true;
		m_pDoc->ShowAxItem();
		m_pDoc->m_pFrame->RecalcLayout();
	}
	if(bActivate)
	{
		SetResource();
		g_pTangramServer->m_pCurFrame = m_pFrameWnd;
		g_pTangramServer->m_pCurFrameObj = this;
		g_pTangramServer->m_pCurDoc = m_pDoc;
	}
	else
		AfxSetResourceHandle(AfxGetApp()->m_hInstance);	
}

void CTangramFrameObj::OnUpdateFrameTitle(BOOL bAddToTitle)
{
	if(!m_bActiveFrame)
	{
		USES_CONVERSION;
		DISPID ObjectID;
		LPWSTR wszObject = OLESTR("Caption");
		BSTR bstrCap = L"";
		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);
			bstrCap = vRet.bstrVal;
		}
		m_pFrameWnd->SetWindowText(OLE2T(bstrCap));
	}
	m_pFrameWnd->GetParentFrame()->OnUpdateFrameTitle(bAddToTitle);
}

void CTangramFrameObj::Create(CFrameWnd *pFrame)
{
	if(m_pDoc)
	{
		m_pDoc->m_pFrame = pFrame;
		m_pFrameWnd = pFrame;
		g_pTangramServer->m_pCurFrame = pFrame;
	}
}

/////////////////////////////////////////////////////////
// CSystemManagerFrameWnd

IMPLEMENT_DYNCREATE(CSystemManagerFrameWnd, CFrameWnd)

CSystemManagerFrameWnd::CSystemManagerFrameWnd()
{
	// TODO���ڴ���ӳ�Ա��ʼ������
	m_TangramFrameObj.Create(this);
}

CSystemManagerFrameWnd::~CSystemManagerFrameWnd()
{
//////////////////$tangramapp$////////////////////////////
	m_TangramFrameObj.m_pDoc = NULL;
//////////////////$tangramapp end$////////////////////////////
}

BEGIN_MESSAGE_MAP(CSystemManagerFrameWnd, CFrameWnd)
	ON_WM_ACTIVATE()
	ON_WM_DESTROY()
END_MESSAGE_MAP()


// CSystemManagerFrameWnd ��Ϣ�������

void CSystemManagerFrameWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
	CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
	BOOL bActive = false;
	if(nState==WA_INACTIVE)
	{
		bActive = false;
	}
	else
	{
		bActive = true;
	}
	m_TangramFrameObj.OnActivate(bActive);
	// TODO: �ڴ������Ϣ����������
}

void CSystemManagerFrameWnd::OnDestroy()
{
	m_TangramFrameObj.OnDestroy();
	CFrameWnd::OnDestroy();

	// TODO: �ڴ������Ϣ����������
}

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