Click here to Skip to main content
15,886,199 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
//*******************************************************************************
// CTangramDesignView.cpp : implementation file
//

#include "stdafx.h"
#include "TangramDesignView.h"
//#include "DockingTangramBar.h"
#include "TangramCntrItem.h"
#include "TangramServerObj.h"
#include "TangramObject.h"
#include "TangramContainerWnd.h"
#include "custsite.h"
#include "Tangramres.h"
#include "TangramDoc.h"
#include "TabWnd.h"
#include "System.h"
#include "TabInfo.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTangramDesignView

IMPLEMENT_DYNCREATE(CTangramDesignView, CFormView)

CTangramDesignView::CTangramDesignView()
	: CFormView((LPCTSTR) NULL)
{
	if(g_pTangramServer)
	{
		m_pDoc=g_pTangramServer->m_pCurDoc;
		m_pFrame = m_pDoc->m_pFrame;
		ASSERT(m_pFrame);
	}
	pWndObj = NULL;
	hComWnd = NULL;
	m_pDesignerWnd = NULL;
	m_pExternalDisp = NULL;
	nViewType = Unknow;
	bObjCreated = false;
	m_pOleItem = NULL;
	m_nRow = 1;
	m_nCol = 1;
	m_pTangramObject = NULL;
}

CTangramDesignView::~CTangramDesignView()
{
	if (m_pExternalDisp != NULL)
		m_pExternalDisp->Release();
	if(m_pDesignerWnd)delete m_pDesignerWnd;
}


BEGIN_MESSAGE_MAP(CTangramDesignView, CView)
	//{{AFX_MSG_MAP(CTangramDesignView)
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_MOUSEACTIVATE()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTangramDesignView drawing

void CTangramDesignView::OnDraw(CDC* pDC)
{
	ASSERT(FALSE);
}

#ifndef _DEBUG  // debug version in TestVba2View.cpp
inline CTangramDoc* CTangramDesignView::GetDocument()
   { m_pDocument = m_pDoc; return (CTangramDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////
// CTangramDesignView diagnostics

#ifdef _DEBUG
void CTangramDesignView::AssertValid() const
{
	CView::AssertValid();
}

void CTangramDesignView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTangramDoc* CTangramDesignView::GetDocument() // non-debug version is inline
{
	m_pDocument = m_pDoc;
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTangramDoc)));
	return (CTangramDoc*)m_pDocument;
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTangramDesignView message handlers

void CTangramDesignView::OnInitialUpdate() 
{
	if(m_pOleItem)
	{
		if(m_pOleItem->bDesignMode)
		{
			if(!m_pOleItem->bShow)
			{
				m_pOleItem->DoVerb(OLEIVERB_SHOW,this);
				m_pOleItem->bShow=true;
			}
		}
		CRect rect;
		GetClientRect(rect);
		if(m_pOleItem->IsKindOf(RUNTIME_CLASS(CTangramDocCntrItem)))
			((CTangramDocCntrItem*)m_pOleItem)->SetDocViewRect(rect);
		else
			m_pOleItem->SetItemRects(rect);
	}	
	else if(hComWnd==NULL)
	{
		if(!m_pDesignerWnd)
		{
			if(m_pTangramObject)
			{
				m_pDesignerWnd = new CWnd();
				CreateAxCtrl(m_pTangramObject->m_strObjID);
				if(m_pTangramObject&&m_pTangramObject->m_TangramObjType==WebBrowser)
				{
					m_pTangramObject->m_pDisp = m_pExternalDisp;
					ASSERT(m_pTangramObject->m_pDisp);
				}
			}
		}
	}
	if(g_pTangramServer)
	{
		m_pDoc = g_pTangramServer->m_pCurDoc;
		if(m_pDoc->m_pFrame==NULL&&m_pDoc->m_pSDIFrame==NULL)
			m_pDoc->m_pSDIFrame = GetParentFrame();
	}
	CView::OnInitialUpdate();
}

void CTangramDesignView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	if(bActivate)
	{
		SetResource();
		if(g_pTangramServer&&g_pTangramServer->m_bMDI)
		{
			g_pTangramServer->m_pCurFrame = m_pDoc->m_pFrame;
			g_pTangramServer->m_pCurDoc = m_pDoc;
			if(m_pDoc)g_pTangramServer->m_pCurDoc->m_pActiveDesignView = this;
		}
		else
			if(m_pDoc)m_pDoc->m_pActiveDesignView = this;
	}

	if(g_pTangramServer&&!g_pTangramServer->m_bClose)
	{
		if(m_pTangramObject&&m_pTangramObject->InnerDoc)
		{
			if(m_pOleItem&&m_pOleItem->IsKindOf(RUNTIME_CLASS(CTangramCntrItem))&&g_pTangramServer)
				g_pTangramServer->m_pCurItem = (CTangramDocCntrItem*)m_pOleItem;
			if(m_pOleItem&&m_pOleItem->bDocument)
			{
				if(bActivate)
				{
					if(g_pTangramServer->m_pActiveItem&&g_pTangramServer->m_pActiveItem!=m_pOleItem)
					{
						g_pTangramServer->m_pActiveItem->OnDeactivate();
						g_pTangramServer->m_pActiveItem=(CTangramDocCntrItem*)m_pOleItem;
					}

					POSITION pos;
					CString key;
					CTangramDocCntrItem* pAxDocItem;
					for( pos = g_pTangramServer->m_AxDocDictionary.GetStartPosition(); pos != NULL; )
					{
						g_pTangramServer->m_AxDocDictionary.GetNextAssoc( pos, key, (CObject*&)pAxDocItem );
						ASSERT(pAxDocItem);
						if(pAxDocItem->IsInPlaceActive()&&pAxDocItem!=m_pOleItem)
							pAxDocItem -> OnDeactivate();
					}   
				}
				else
				{
					if(g_pTangramServer&&g_pTangramServer->m_pActiveItem&&g_pTangramServer->m_pActiveItem==m_pOleItem)
						g_pTangramServer->m_pActiveItem=NULL;
					m_pOleItem->OnDeactivate();
				}
			}
		}
	}
	CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
	if(bActivate&&hComWnd&&::IsWindow(hComWnd))
		::SetFocus(hComWnd);
}

void CTangramDesignView::OnDestroy() 
{
	if(m_pOleItem)
	{
		if(g_pTangramServer&&g_pTangramServer->m_pActiveItem==m_pOleItem)
			g_pTangramServer->m_pActiveItem = NULL;
		if(m_pOleItem->m_lpObject)
		{
			m_pOleItem->Close();
			m_pOleItem->Release();
		}
	}
	if(m_pDesignerWnd)
	{
		m_pDesignerWnd->DestroyWindow();
		delete m_pDesignerWnd;
		m_pDesignerWnd=NULL;
	}
	TRACE("CTangramDesignView:%x CTangramDesignView::OnDestroy\n",this);
	if (m_pFrame != NULL && ::IsWindow(m_pFrame->m_hWnd) && m_pFrame->GetActiveView() == this)
		m_pFrame->SetActiveView(NULL);    // deactivate during death
	if(hComWnd&&::IsWindow(hComWnd))
	{
		g_pTangramServer->ProcessDlgMsg(false,hComWnd);
		::DestroyWindow(hComWnd);
	}
	CWnd::OnDestroy();
	//CView::OnDestroy();
}

void CTangramDesignView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	if (m_pOleItem != NULL)
	{
		CRect rect(CPoint(0,0),CSize(cx,cy));
		if(m_pOleItem->IsKindOf(RUNTIME_CLASS(CTangramDocCntrItem)))
			((CTangramDocCntrItem*)m_pOleItem)->SetDocViewRect(rect);
		else
		{
			CRect rect;
			GetClientRect(rect);
			rect.bottom+=2;
			rect.top -=2;
			rect.left -=2;
			rect.right +=2;
			m_pOleItem->SetItemRects(rect);
		}
	}
	if(m_pDesignerWnd)m_pDesignerWnd->MoveWindow(-2,-2,cx+3,cy+3);
	if(pWndObj)pWndObj->MoveWindow(0,0,cx,cy);
	if(hComWnd&&::IsWindow(hComWnd))::MoveWindow(hComWnd,0,0,cx,cy,true);
}

int CTangramDesignView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message) 
{
	if(m_pDoc)m_pDoc->m_pActiveDesignView = this;
	if(m_pTangramObject&&m_pTangramObject->InnerDoc)
	{
		if(m_pOleItem)
		{
			if(!m_pOleItem->IsInPlaceActive())
			{
				if(m_pOleItem->CanActivate())
				{
					if(m_pTangramObject->m_strObjID!=_T("forms.form.1"))
						m_pOleItem->Activate(0,this);
				}
				if(g_pTangramServer->m_pActiveItem&&g_pTangramServer->m_pActiveItem!=m_pOleItem)
					g_pTangramServer->m_pActiveItem->OnDeactivate();
				g_pTangramServer->m_pActiveItem = (CTangramDocCntrItem*)m_pOleItem;
			}
			else 
			{
				if(!g_pTangramServer->m_pActiveItem)
					g_pTangramServer->m_pActiveItem = (CTangramDocCntrItem*)m_pOleItem;
				else if(g_pTangramServer->m_pActiveItem!=m_pOleItem)
				{
					g_pTangramServer->m_pActiveItem->OnDeactivate();
					g_pTangramServer->m_pActiveItem = (CTangramDocCntrItem*)m_pOleItem;
				}
			}
		}
	}

	return CView::OnMouseActivate(pDesktopWnd, nHitTest, message);
}

void CTangramDesignView::CreateActiveItem(CString strDocID, CString strObjName, CString strCaption, BOOL bDocking)
{
	if(m_pTangramObject->m_TangramObjType!=Unknow)return;
	CWnd* m_pParentWnd = GetParent();
	CRect rc;
	GetClientRect(&rc);
	strObjName.MakeLower();
	CTangramObject* pTempObj;
	if(m_pDoc->m_TangramObjectDictionary.Lookup(strObjName,(CObject*&)pTempObj))
	{
		if(pTempObj->m_pDisp)
		{
			CString s = _T("");
			s.Format(_T("There exist an Object have the same Name: \"%s\" \nPlease give another Name for this object!"),strObjName);
			AfxMessageBox(s);
			return;
		}
	}
	
	m_pDoc->m_TangramObjectDictionary[strObjName] = m_pTangramObject;
	m_pTangramObject->m_strObjName = strObjName;
	if(bDocking)
	{
		CFrameWnd *pFrame = g_pTangramServer->m_pCurFrame;
		pFrame = pFrame->GetParentFrame();
		CTangramDockingObjImpl* m_pDockingBarObj = (CTangramDockingObjImpl*)g_pTangramServer->m_pTangramDockingBarRuntimeClass->CreateObject();
		m_pDockingBarObj->SetTangramObj();
		CFrameWnd* pDockFrame = m_pDockingBarObj->CreateDockingFrame(strCaption,pFrame);
		g_pTangramServer->m_pCurDoc->m_Context.m_pNewViewClass = g_pTangramServer->m_pDesignViewRuntimeClass;
		CTangramDesignView* m_pView = (CTangramDesignView*)(pDockFrame->CreateView(&g_pTangramServer->m_pCurDoc->m_Context));
		m_pView->m_pTangramObject = m_pDockingBarObj->m_pTangramObject;
		m_pView->m_pTangramObject->isDocking=TRUE;
		m_pView->OnInitialUpdate();
		g_pTangramServer->m_pCurDoc->m_bAutoDelete = true;
		return ;
	}

	strDocID.Trim();
	strDocID.MakeLower();
	m_pOleItem = NULL;
	CRuntimeClass* pClsInfo = NULL;
	CRuntimeClass* pConnectObjClsInfo = NULL;
	if(g_pTangramServer->m_MfcObjInfoDictionary.Lookup(LPCTSTR(strDocID),(void *&)pClsInfo)==TRUE)
	{
		ASSERT(pClsInfo);
		if(pClsInfo->IsDerivedFrom(RUNTIME_CLASS(CView)))
		{
			m_pTangramObject->m_TangramObjType=MfcView;
			nViewType = MfcView;
		}
		else if(pClsInfo->IsDerivedFrom(RUNTIME_CLASS(CWnd)))
		{
			m_pTangramObject->m_TangramObjType=MfcWnd;
			nViewType = MfcWnd;
		}
	}
	else if(strDocID==_T("shell.explorer"))
	{
		m_pTangramObject->m_TangramObjType=WebBrowser;
		m_pTangramObject->m_pDisp=m_pExternalDisp;
		nViewType = WebBrowser;
	}
	else
	{
		m_pTangramObject->m_TangramObjType=(TangramObjType)QueryProgIDType(strDocID);
		nViewType = m_pTangramObject->m_TangramObjType;
		if(nViewType==Unknow)return;
	}
	if(nViewType!=WebBrowser&&m_pDesignerWnd)
	{
		m_pDesignerWnd->DestroyWindow();
		delete m_pDesignerWnd;
		m_pDesignerWnd=NULL;
		if (m_pExternalDisp != NULL)
		{
			m_pExternalDisp->Release();
			m_pExternalDisp=NULL;
		}
	}
	m_pTangramObject->m_TangramObjType = nViewType;
	m_pTangramObject->m_strObjID = strDocID;
	switch(nViewType)
	{
	case AxDoc:
	case AxCtrl:
	//case WinCtrl:
		{
			TRY
			{
				(nViewType==AxDoc)?m_pOleItem = new CTangramDocCntrItem(g_pTangramServer->m_pCurDoc):m_pOleItem = new CTangramCntrItem(g_pTangramServer->m_pCurDoc);
				//if(nViewType==WinCtrl)m_pOleItem -> bWinCtrl=TRUE;
				m_pOleItem -> bDocument=TRUE;
				//m_pOleItem -> m_strObjID = strDocID;
				m_pOleItem -> m_pDoc=g_pTangramServer->m_pCurDoc;
				m_pOleItem -> m_strObjName = m_pTangramObject->m_strName;
				m_pOleItem -> m_hParentWnd = m_hWnd;
				
				CLSID id;
				CLSIDFromProgID(strDocID.AllocSysString(),&id);
				if (!m_pOleItem->CreateNewItem(id))
					AfxThrowMemoryException();
				
				m_pOleItem -> m_pDocument = g_pTangramServer->m_pCurDoc;
				ASSERT_VALID(m_pOleItem);
			}
			CATCH(CException, e)
			{
				if (m_pOleItem != NULL)
				{
					ASSERT_VALID(m_pOleItem);
					m_pOleItem->Delete();
				}
				return ;
			}
			END_CATCH
			m_pOleItem->GetCtrlWndHandle();
			m_pOleItem->m_pMfcView = this;
			m_pOleItem->m_pView = this;
			CComQIPtr<IDispatch, &IID_IDispatch> spControl(m_pOleItem->m_lpObject);
			m_pTangramObject->m_pDisp=spControl;
			m_pTangramObject->m_pObjCntrItem = m_pOleItem;
			//if(nViewType==WinCtrl)m_pTangramObject->m_pObjCntrItem ->bWinCtrl = true;
				
			m_pOleItem->bDesignMode=true;
			OnInitialUpdate();
			if(nViewType==AxCtrl||nViewType==WinCtrl)
				((CTangramCntrItem*)m_pOleItem)->ShowPropertyPage();
		}
		break;
	case WinCtrl:
		{
			if(!m_pDesignerWnd)
				m_pDesignerWnd = new CWnd();
			CreateAxCtrl(strDocID);
			//pWnd->SetFocus();
			OnInitialUpdate();
		}
		break;
	case ComWnd:
		{
			CWnd* pWnd = CWnd::FromHandle(hComWnd);
			pWnd->ModifyStyle(WS_BORDER|WS_THICKFRAME|WS_POPUP,WS_CHILD);
			::SetParent(hComWnd,m_hWnd);
			pWnd->ShowWindow(SW_SHOW);
			GetWindowRect(&rc);
			::MoveWindow(hComWnd,0,0,rc.Width(),rc.Height(),TRUE);
			g_pTangramServer->ProcessDlgMsg(true,hComWnd);
			pWnd->SetFocus();
			OnInitialUpdate();
		}
		break;
	case MfcView:
	case MfcWnd:
		{
			g_pTangramServer->m_pCurTangramObj = m_pTangramObject;
			CWnd* pView = NULL;
			m_pTangramObject->m_pRuntimeClass = pClsInfo;
			CString strExternalCnn=_T("");
			if(g_pTangramServer->m_MfcConnectExternalInfoDictionary.Lookup(LPCTSTR(strDocID),strExternalCnn)==TRUE)
				m_pTangramObject->m_strExternalCnn=strExternalCnn;
			CRect rect(0,0,100,100);
			CCreateContext *pContext = &g_pTangramServer->m_pCurDoc->m_Context;
			pContext->m_pNewViewClass = pClsInfo;
			pView = (CWnd*)pClsInfo->CreateObject();
			if(nViewType==MfcView)
			{
				if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, rect, this, (UINT)this, pContext))
				{
					TRACE0("Warning: couldn't create client area for tab view\n");
					return ;
				}
				((CView*)pView)->OnInitialUpdate();
			}
			else
			{
				if(g_pTangramServer->m_MfcConnectObjInfoDictionary.Lookup(LPCTSTR(strDocID),(void *&)pConnectObjClsInfo)==TRUE)
				{
					m_pTangramObject->m_pConObj = (CConnectObj*)pConnectObjClsInfo->CreateObject();
					m_pTangramObject->m_pConObj->m_pContext = &(g_pTangramServer->m_pCurDoc->m_Context);
					m_pTangramObject->m_pConObj->m_pObjExtManager = g_pTangramServer->m_pObjExtManager;
					m_pTangramObject->m_pConObj->m_pMfcObj = pView;
				}
				if(m_pTangramObject->m_pConObj)
				{
					m_pTangramObject->m_pConObj->m_pParentWnd = this;					
					m_pTangramObject->m_pConObj->Connect(false);
					if(m_pTangramObject->m_pConObj->m_pObjDisp)
						m_pTangramObject->m_pDisp = m_pTangramObject->m_pConObj->m_pObjDisp;
				}
			}
			pWndObj = pView;
			pView->ModifyStyle(WS_BORDER,0);
			GetWindowRect(&rc);
			::MoveWindow(pView->m_hWnd,0,0,rc.Width(),rc.Height(),TRUE);
			g_pTangramServer->m_pCurFrame->RecalcLayout();
			g_pTangramServer->m_pCurDoc->SetModifiedFlag(true);
			g_pTangramServer->m_pCurTangramObj = NULL;
		}
		break;
	case WebBrowser:
		g_pTangramServer->m_pCurFrame->RecalcLayout();
		g_pTangramServer->m_pCurDoc->SetModifiedFlag(true);
		g_pTangramServer->m_pCurTangramObj = NULL;
		break;
	case MfcWndContainer:
		break;
	default:
		break;
	}

	bObjCreated = true;
	g_pTangramServer->m_pCurDoc->SetModifiedFlag(true);
	SetResource();
	return ;
}

BOOL CTangramDesignView::PreCreateWindow(CREATESTRUCT& cs) 
{
	cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
	cs.lpszClass = AfxRegisterWndClass(0);//CS_DBLCLKS
	return CView::PreCreateWindow(cs);
}

BOOL CTangramDesignView::CreateAxCtrl(CString strClsID)
{
 	RECT rectClient;
 	GetClientRect(&rectClient);
	rectClient.bottom += 2;
	rectClient.right += 2;
	rectClient.top -= 2;;
	rectClient.left -= 2;

	CString strCtrlID = _T("shell.explorer");
	if(strClsID!=_T(""))
	{
		strCtrlID=strClsID;
	}
	CLSID id;
	CLSIDFromProgID(strCtrlID.AllocSysString(),&id);

	HRESULT hr;
	LPUNKNOWN lpUnk;
	strCtrlID.MakeLower();


	if(strClsID==_T("")||strClsID==_T("shell.explorer"))
	{
 		////////////////////////////////////////////////////////
 		//��չDHTML֧��
 		////////////////////////////////////////////////////////
		//CRuntimeClass* pCustomOccManager = RUNTIME_CLASS(CCustomOccManager);
 		CCustomOccManager *pMgr = new CCustomOccManager();
		CComQIPtr<IDispatch, &IID_IDispatch> spControl(g_pTangramServer->m_pCurDoc->m_pDocItem->m_lpObject);
		ASSERT(spControl);
		pMgr->m_pDocDisp=spControl;
		ASSERT(pMgr->m_pDocDisp);
 		AfxEnableControlContainer(pMgr);
 		///////////////////////////////////////////////////////
 		// create the control window
 		// AFX_IDW_PANE_FIRST is a safe but arbitrary ID CLSID_WebBrowser
 		if (!m_pDesignerWnd->CreateControl(id, strCtrlID,
 					WS_VISIBLE | WS_CHILD, rectClient, this, AFX_IDW_PANE_FIRST))
 		{
 			DestroyWindow();
 			return FALSE;
 		}

 		lpUnk = m_pDesignerWnd->GetControlUnknown();
 		hr = lpUnk->QueryInterface(IID_IWebBrowser2, (void**) &m_pExternalDisp);
 		if (!SUCCEEDED(hr))
 		{
			AfxMessageBox("���������ʧ�ܣ�");
 			m_pExternalDisp = NULL;
 			m_pDesignerWnd->DestroyWindow();
 			DestroyWindow();
 			return FALSE;
 		}
		m_pExternalDisp->AddRef();
		SetDesignerPage();
		
		lpUnk->Release();
		SetResource();
		return TRUE;
	}
	else
	{
 		AfxEnableControlContainer();
 		if (!m_pDesignerWnd->CreateControl(id, strCtrlID,
 					WS_VISIBLE | WS_CHILD, rectClient, this, AFX_IDW_PANE_FIRST))
 		{
 			DestroyWindow();
 			return FALSE;
 		}

 		lpUnk = m_pDesignerWnd->GetControlUnknown();
 		hr = lpUnk->QueryInterface(IID_IDispatch, (void**) &m_pTangramObject->m_pDisp);
 		if (!SUCCEEDED(hr))
 		{
			AfxMessageBox("����ʧ�ܣ�");
 			m_pTangramObject->m_pDisp = NULL;
 			m_pDesignerWnd->DestroyWindow();
 			DestroyWindow();
 			return FALSE;
 		}
		lpUnk->Release();
		return false;
	}
}

BOOL CTangramDesignView::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 CTangramDesignView::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_pExternalDisp->Navigate(bstrURL,
		COleVariant((long) dwFlags, VT_I4),
		COleVariant(lpszTargetFrameName, VT_BSTR),
		vPostData,
		COleVariant(lpszHeaders, VT_BSTR));
}

void CTangramDesignView::CreateSplitter(long nRow, long nCol, CString strSplitterTitle, long bDock)
{
	m_nRow = nRow ;
	m_nCol = nCol ;
	if(m_nRow==1&&m_nCol==1)return;
	g_pTangramServer->m_pCurDoc->m_bAutoDelete = FALSE;
	CTangramObject* m_pParentObj = m_pTangramObject->m_pParentObject;
	CWnd* m_pParentWnd = GetParent();
	CRect rc;
	GetClientRect(&rc);

	CSplitterWnd* m_pSplitter = new CSplitterWnd();
	m_pTangramObject -> m_pWnd = m_pSplitter;
	m_pTangramObject -> m_ContainerType = Splitter;

	if(m_pParentWnd->IsKindOf(RUNTIME_CLASS(CFrameWnd))||m_pParentObj->m_ContainerType==Board)
		m_pSplitter->CreateStatic(m_pParentWnd, m_nRow, m_nCol);//�����·ָ��
	else if(m_pParentObj->m_ContainerType==Splitter)
	{
		int Row,Col;
		//ȷ���Ӵ�����ݣ��õ��С���
		((CSplitterWnd*)m_pParentWnd)->IsChildPane(this,&Row,&Col);
		//�����·ִ�����
		m_pSplitter->CreateStatic(m_pParentWnd, m_nRow, m_nCol, WS_CHILD | WS_VISIBLE | WS_BORDER, ((CSplitterWnd*)m_pParentWnd)->IdFromRowCol(Row,Col));
	}

	for(int i = 0; i < m_nRow; i++)
		for(int j = 0; j < m_nCol;j++)
		{
			CTangramObject * pObj = new CTangramObject();
			pObj->m_pParentObject = m_pTangramObject;
			m_pTangramObject ->m_ChildObjectMap.SetAtGrow(i*m_nCol+j, pObj);
			m_pSplitter->CreateView(i, j, g_pTangramServer->m_pDesignViewRuntimeClass, CSize(rc.Width() / m_nCol, rc.Height()/m_nRow),&(m_pDoc->m_Context));
			CTangramDesignView* pNewView = (CTangramDesignView*)m_pSplitter->GetPane(i,j);
			pNewView->m_pTangramObject = pObj;
			pObj->m_pWnd = pNewView;
			pNewView->OnInitialUpdate();
		}
	CTangramDesignView* pNewView = (CTangramDesignView*)m_pSplitter->GetPane(m_nRow-1,m_nCol-1);
	m_pSplitter->GetParentFrame()->SetActiveView(pNewView);
	if(m_pParentObj&&m_pParentObj->m_ContainerType==Board)
	{
		CTangramContainerWnd* m_pTabWnd = ((CTangramContainerWnd*)m_pParentWnd);
		int nTabIndex = m_pTabWnd->GetActiveTab();
		m_pTabWnd->SetTabWnd(nTabIndex,m_pSplitter);
		m_pTabWnd->RecalcLayout();
	}
	
	if(::IsWindow(m_hWnd))DestroyWindow();
	if(m_pParentWnd->IsKindOf(RUNTIME_CLASS(CSplitterWnd)))((CSplitterWnd*)m_pParentWnd)->RecalcLayout();
	if(m_pParentWnd->IsKindOf(RUNTIME_CLASS(CFrameWnd)))((CFrameWnd*)m_pParentWnd)->RecalcLayout();
	g_pTangramServer->m_pCurDoc->m_bAutoDelete = TRUE;			
}

void CTangramDesignView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
{
	return;
}

int CTangramDesignView::QueryProgIDType(CString strClsID)
{
	// TODO: Add your implementation code here
	strClsID.TrimLeft();
	strClsID.TrimLeft();
	strClsID.MakeLower();
	m_pTangramObject->m_TangramObjType = Unknow;
	CComPtr<IDispatch> m_pDisp;
	m_pDisp.CoCreateInstance(strClsID.AllocSysString());
	//����ProgID�������
	if(m_pDisp!=NULL)
	{
		//����ɹ�
		//ObjType = 1;

		if(m_pDisp)
		{
			DISPID ObjectID;
			LPWSTR wszObject = OLESTR("GetWndHandle");
			if (SUCCEEDED(m_pDisp->GetIDsOfNames(IID_NULL,&wszObject,1,LOCALE_NEUTRAL,&ObjectID))) 
			{
				m_pTangramObject->m_TangramObjType=ComWnd;
				m_pDisp.p->AddRef();
				m_pTangramObject->m_pDisp=m_pDisp.p;

				DISPPARAMS dp = {NULL, NULL, 0, 0};
				VARIANT vRet;
				m_pDisp->Invoke(ObjectID,IID_NULL,LOCALE_NEUTRAL,DISPATCH_METHOD,&dp,&vRet,NULL,NULL);
				hComWnd = (HWND)vRet.lVal;
				return m_pTangramObject->m_TangramObjType;
			}
		}
		//�Ƿ�ΪOleDocument����
		if(CComQIPtr<IOleDocument,&__uuidof(IOleDocument)>(m_pDisp))//IID_IOleDocument
		{
			m_pTangramObject->m_TangramObjType=AxDoc;
			return m_pTangramObject->m_TangramObjType;
		}

		//�Ƿ�ΪOleControl����
		if(CComQIPtr<IOleControl,&__uuidof(IOleControl)>(m_pDisp))//IID_IOleControl
		{
			if(CComQIPtr<IComponent,&__uuidof(IComponent)>p(m_pDisp))
			{
				m_pTangramObject->m_TangramObjType=WinCtrl;
				//m_pOleItem->bWinCtrl=true;
				return m_pTangramObject->m_TangramObjType;
			}
			m_pTangramObject->m_TangramObjType=AxCtrl;
			return m_pTangramObject->m_TangramObjType;
		}
		
		if(CComQIPtr<IMfcWndContainer,&__uuidof(IMfcWndContainer)>(m_pDisp))
		{
			m_pTangramObject->m_TangramObjType=MfcWndContainer;
			return m_pTangramObject->m_TangramObjType;
		}

		//�ͷŶ���
		m_pDisp.Release();
		m_pTangramObject->m_TangramObjType = NonGUIObj;
		return m_pTangramObject->m_TangramObjType;
	}
	else
	{
		int x = g_pTangramServer->m_strDocString.Find(strClsID);
		if(x!=-1)
		{
			m_pTangramObject->m_TangramObjType=AxDoc;
			m_pTangramObject->InnerDoc = false;
			return m_pTangramObject->m_TangramObjType;
		}
	}
	return m_pTangramObject->m_TangramObjType;
}

void CTangramDesignView::CreateTabWnd(CString strClsID, CString strCaption, CString strObjName, BOOL m_bDocking)
{
	CString m_strClsID = strClsID;
	m_strClsID.TrimLeft();
	m_strClsID.TrimRight();
	if(m_strClsID!=_T(""))
	{
		int nType = QueryProgIDType(m_strClsID);
		if(m_strClsID!=_T("")&&nType!=MfcWndContainer)return;
	}
	g_pTangramServer->m_pCurDoc->m_bAutoDelete = FALSE;
	CString strTitle = m_pDoc->GetTitle();
	CTangramObject* m_pParentObj = m_pTangramObject->m_pParentObject;
	//Get Parent CWnd Object:
	CWnd* m_pParentWnd = GetParent();
	////create tab:
	CRect rc;
	GetClientRect(&rc);
	CFrameWnd *pFrame = g_pTangramServer->m_pCurFrame;
	CFrameWnd* pMainFrm = g_pTangramServer->m_pCurFrame->GetParentFrame();

	CDocCntrItem* m_pBoardItem = NULL;
	m_pBoardItem = new CDocCntrItem(g_pTangramServer->m_pCurDoc,m_strClsID);
	m_pBoardItem->m_pTangramObject = new CTangramObject();
	m_pBoardItem->bDoc=false;
	m_strClsID = g_pTangramServer->m_strTabCtrlID;
	m_pBoardItem->m_ContainerType = Board;
	if(m_pTangramObject->m_pParentObject)
		m_pBoardItem->m_pTangramObject->m_pParentObject = m_pTangramObject->m_pParentObject;
	m_pBoardItem->m_pTangramObject->m_strTitle = strCaption;
	m_pBoardItem->m_pTangramObject->m_strObjName=strObjName;
	m_pDoc->m_TangramObjectDictionary[strObjName] = m_pBoardItem->m_pTangramObject;
	m_pBoardItem->m_pTangramObject->m_pDocCntrItem = m_pBoardItem;
	m_pBoardItem->m_pTangramObject->m_ContainerType = Board;
	m_pBoardItem->m_pTangramObject->m_strObjID = m_strClsID;

	CLSID id;
	CLSIDFromProgID(m_strClsID.AllocSysString(),&id);
	m_pBoardItem->CreateNewItem(id,OLERENDER_NONE);
	m_pBoardItem->m_pTangramWindow->IniContainer(false);
	m_pBoardItem->m_pTangramObject->m_pWnd = m_pBoardItem->m_pWnd;
	CComQIPtr<IDispatch, &IID_IDispatch> spControl(m_pBoardItem->m_lpObject);
	m_pBoardItem->m_pTangramObject->m_pDisp=spControl;
	
	int dwStyle = AFX_WS_DEFAULT_VIEW;

	CTangramContainerWnd* m_pTabWnd = (CTangramContainerWnd*)m_pBoardItem->m_pTangramObject->m_pWnd;//g_pTangramServer->m_pCurBoardWnd;
	m_pTabWnd->m_pOleItem = m_pBoardItem;
	m_pBoardItem->m_pWnd = m_pTabWnd;
	m_pBoardItem->m_strName = m_pBoardItem->m_pTangramObject->m_strName;

	CTangramObject* pNewObj = new CTangramObject();
	CCreateContext *pContext = &m_pDoc->m_Context;
	pContext->m_pNewViewClass = g_pTangramServer->m_pDesignViewRuntimeClass;
	CTangramDesignView* m_pWnd = (CTangramDesignView*)pContext->m_pNewViewClass->CreateObject();
	LPCTSTR lpszLabel = strCaption;
	CRect rect(0,0,100,100);
	m_pWnd->m_pTangramObject = pNewObj;
	pNewObj->m_pWnd = m_pWnd;
	pNewObj->uiImageId = 0;
	pNewObj->m_strTitle = strCaption;
	pNewObj->m_pParentObject = m_pBoardItem->m_pTangramObject;
	if (m_pParentWnd->IsKindOf(RUNTIME_CLASS(CFrameWnd)))
	{
		int nID = 0;
		for(int i = 0; i < g_pTangramServer->m_pCurDoc -> m_TangramObjectArray.GetSize(); i++)
		{
			if(g_pTangramServer->m_pCurDoc->m_TangramObjectArray[i] == m_pTangramObject)
			{
				nID = i;
				break;
			}
		}
		m_pBoardItem->m_pTangramObject->isDocking = m_pTangramObject->isDocking;
		delete m_pTangramObject;
		m_pDoc->m_TangramObjectArray[nID] = m_pBoardItem->m_pTangramObject;
		if (!((CWnd*)m_pTabWnd)->Create(NULL, NULL, dwStyle, rc, m_pParentWnd, (UINT)m_pTabWnd, &g_pTangramServer->m_pCurDoc->m_Context))
		{
			TRACE0("Warning: couldn't create client area for tab view\n");
			return ;
		}
		m_pTabWnd->ModifyStyle(WS_BORDER,0);
		m_pBoardItem->m_pTangramObject->m_ChildObjectMap.SetAtGrow(0, pNewObj);
		if (!m_pWnd->Create(NULL, NULL, dwStyle, rect, m_pTabWnd, (UINT)this, pContext))
		{
			TRACE0("Warning: couldn't create client area for tab view\n");
			return ;
		}
		m_pTabWnd->AddTab(m_pWnd,lpszLabel,0);
		DestroyWindow();
	}
	else if(m_pParentWnd->IsKindOf(RUNTIME_CLASS(CSplitterWnd)))
	{
		int Row,Col,nCol;
		nCol = ((CSplitterWnd*)m_pParentWnd)->GetColumnCount();
		((CSplitterWnd*)m_pParentWnd)->IsChildPane(this,&Row,&Col);
		
		delete m_pTangramObject;
		m_pBoardItem->m_pTangramObject->m_pParentObject = m_pParentObj;
		m_pParentObj->m_ChildObjectMap[Row*nCol + Col] = m_pBoardItem->m_pTangramObject;
		if (!((CWnd*)m_pTabWnd)->Create(NULL, NULL, dwStyle, rc, m_pParentWnd, ((CSplitterWnd*)m_pParentWnd)->IdFromRowCol(Row,Col), &g_pTangramServer->m_pCurDoc->m_Context))
		{
			TRACE0("Warning: couldn't create client area for tab view\n");
			return ;
		}
		m_pTabWnd->ModifyStyle(WS_BORDER,0);
		m_pBoardItem->m_pTangramObject->m_ChildObjectMap.SetAtGrow(m_pBoardItem->m_pTangramObject->m_ChildObjectMap.GetSize(), pNewObj);
		if (!m_pWnd->Create(NULL, NULL, dwStyle, rect, m_pTabWnd, (UINT)this, pContext))
		{
			TRACE0("Warning: couldn't create client area for tab view\n");
			return ;
		}
		m_pTabWnd->AddTab(m_pWnd,lpszLabel,0);

		DestroyWindow();
		m_pTabWnd->RecalcLayout(); 
		((CSplitterWnd*)m_pParentWnd)->RecalcLayout();
	}
	else if(m_pParentObj&&m_pParentObj->m_ContainerType==Board)
	{
		m_pParentWnd = m_pParentObj->m_pWnd;
		CDocCntrItem* m_pParentBoardItem = m_pParentObj->m_pDocCntrItem;
		ASSERT(m_pParentBoardItem);
		int nTabIndex = ((CTangramContainerWnd*)m_pParentWnd)->GetActiveTab();
		m_pBoardItem->m_pTangramObject->m_pParentObject = m_pParentBoardItem->m_pTangramObject;
		m_pBoardItem->m_pTangramObject->m_pParentObject->m_strTitle = m_pParentBoardItem->m_pTangramObject->m_ChildObjectMap[0]->m_strTitle;
		delete m_pParentBoardItem->m_pTangramObject ->m_ChildObjectMap[0];
		m_pParentBoardItem->m_pTangramObject ->m_ChildObjectMap[0] = m_pBoardItem->m_pTangramObject ;
		if (!((CWnd*)m_pTabWnd)->Create(NULL, NULL, dwStyle, rc, m_pParentWnd, (UINT)m_pParentWnd, &g_pTangramServer->m_pCurDoc->m_Context))
		{
			TRACE0("Warning: couldn't create client area for tab view\n");
			return ;
		}
		m_pTabWnd->ModifyStyle(WS_BORDER,0);
		m_pBoardItem->m_pTangramObject->m_ChildObjectMap.SetAtGrow(0, pNewObj);
		if (!m_pWnd->Create(NULL, NULL, dwStyle, rect, m_pTabWnd, (UINT)this, pContext))
		{
			TRACE0("Warning: couldn't create client area for tab view\n");
			return ;
		}
		m_pTabWnd->AddTab(m_pWnd,lpszLabel,0);
		//DestroyWindow();
		((CTangramContainerWnd*)m_pParentWnd)->SetTabWnd(nTabIndex,m_pTabWnd);
		m_pTabWnd->RecalcLayout(); 
		((CTangramContainerWnd*)m_pParentWnd)->RecalcLayout();
	}

	m_pWnd->ModifyStyle(WS_BORDER,0);
	m_pWnd->OnInitialUpdate();
	g_pTangramServer->m_pCurDoc->m_bAutoDelete = TRUE;			
	g_pTangramServer->m_pCurDoc->SetTitle(strTitle);
	if(g_pTangramServer->m_bMDI)
	{
		g_pTangramServer->m_pCurFrame->RecalcLayout();
		g_pTangramServer->m_pCurFrame ->SetWindowText(strTitle);
	}
	else
	{
		g_pTangramServer->m_pSDIFrame->RecalcLayout();
		g_pTangramServer->m_pSDIFrame ->SetWindowText(strTitle);
	}
}

BOOL CTangramDesignView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

void CTangramDesignView::DeleteContainer(int nType)
{
	if(!m_pTangramObject)return;
	CTangramObject* m_pParentObj = m_pTangramObject->m_pParentObject;
	if(m_pParentObj==NULL)return;
	g_pTangramServer->m_pCurDoc->m_bAutoDelete = FALSE;
	CString strTitle = m_pDoc->GetTitle();
	CWnd* m_pParentWnd = GetParent();
	CRect rc;
	GetClientRect(&rc);
	CFrameWnd *pFrame = g_pTangramServer->m_pCurFrame;
	CFrameWnd* pMainFrm = g_pTangramServer->m_pCurFrame->GetParentFrame();
	int xRow = m_nRow;
	int xCol = m_nCol;
	switch(nType)
	{
	case 0:
		{
			if(m_pParentWnd->IsKindOf(RUNTIME_CLASS(CSplitterWnd)))
			{
				CSplitterWnd* m_pSplitterWnd = (CSplitterWnd*)m_pParentWnd;
				CWnd* m_pPWnd = m_pParentWnd->GetParent();
				CTangramObject* m_pPParentObj = m_pParentObj->m_pParentObject;
				CCreateContext *pContext = &g_pTangramServer->m_pCurDoc->m_Context;
				pContext->m_pNewViewClass = g_pTangramServer->m_pDesignViewRuntimeClass;
				if(m_pPWnd->IsKindOf(RUNTIME_CLASS(CSplitterWnd)))
				{
					int Row,Col;
					((CSplitterWnd*)m_pPWnd)->IsChildPane(m_pSplitterWnd,&Row,&Col);//ȷ���Ӵ��ڡ��õ�����
					UINT ID = ((CSplitterWnd*)m_pPWnd)->IdFromRowCol(Row,Col);
					/////////////////��Ӵ���/////////////////
					CTangramObject* pObj = m_pParentObj;
					pObj->DeleteChild();
					int nIndex = Row*m_pPParentObj->m_nCols+Col;
					//////////////////////////////////////////
					//m_pSplitterWnd->DestroyWindow();
					CTangramDesignView* m_pWnd = (CTangramDesignView*)pContext->m_pNewViewClass->CreateObject();
					CTangramObject* pObjNew = new CTangramObject();
					delete pObj;
					pObj=NULL;
					m_pPParentObj->m_ChildObjectMap[nIndex] = pObjNew;
					int dwStyle = AFX_WS_DEFAULT_VIEW;
					if (!((CWnd*)m_pWnd)->Create(NULL, NULL, dwStyle, rc, m_pPWnd, ID, &g_pTangramServer->m_pCurDoc->m_Context))
					{
						TRACE0("Warning: couldn't create client area for tab view\n");
						return ;
					}
					pObjNew->m_pWnd = m_pWnd;
					pObjNew->m_pParentObject = m_pPParentObj;
					if(!m_pWnd->m_pDesignerWnd)
						m_pWnd->m_pDesignerWnd = new CWnd();
					m_pWnd->CreateAxCtrl(_T(""));
					m_pWnd->OnInitialUpdate();
					//m_pWnd->SetDesignerPage();
					((CSplitterWnd*)m_pPWnd)->RecalcLayout();
				}
				else if(m_pPParentObj&&m_pPParentObj->m_ContainerType==Board)
				{
					int nTabIndex = ((CTangramContainerWnd*)m_pPWnd)->GetActiveTab();
					/////////////////��Ӵ���/////////////////
					CTangramObject* pObj = m_pParentObj;
					pObj->DeleteChild();
					CTangramObject* pParentObj = pObj->m_pParentObject;
					int nIndex =0, nObjCount;
					nObjCount = pParentObj -> m_ChildObjectMap.GetSize();
					for(int i = 0; i < nObjCount; i++)
					{
						if(pParentObj->m_ChildObjectMap[i] == pObj)
						{
							nIndex = i;
							break;
						}
					}
					CString strTitle = pObj->m_strTitle;
					delete pObj;
					//////////////////////////////////////////
					CTangramDesignView* m_pWnd = (CTangramDesignView*)pContext->m_pNewViewClass->CreateObject();
					pObj = new CTangramObject();
					pObj->m_pWnd = m_pWnd;
					pObj->m_strTitle = strTitle;
					pObj->m_pParentObject = pParentObj;
					pParentObj->m_ChildObjectMap[nIndex] = pObj;
					int dwStyle = AFX_WS_DEFAULT_VIEW;
					if (!((CWnd*)m_pWnd)->Create(NULL, NULL, dwStyle, rc, m_pPWnd, (UINT)m_pPWnd, &g_pTangramServer->m_pCurDoc->m_Context))
					{
						TRACE0("Warning: couldn't create client area for tab view\n");
						return ;
					}
					g_pTangramServer->m_pCurFrame->SetActiveView((CView*)m_pWnd);
					((CTangramContainerWnd*)m_pPWnd)->SetTabWnd(nTabIndex,m_pWnd);
					//m_pSplitterWnd->DestroyWindow();
					if(!m_pWnd->m_pDesignerWnd)
						m_pWnd->m_pDesignerWnd = new CWnd();
					m_pWnd->CreateAxCtrl(_T(""));
					m_pWnd->OnInitialUpdate();
					((CTangramContainerWnd*)m_pPWnd)->RecalcLayout();
				}
				else if(m_pPWnd->IsKindOf(RUNTIME_CLASS(CMDIChildWnd)))
				{
					/////////////////��Ӵ���/////////////////
					CTangramObject* pObj = m_pParentObj;
					int nIndex =0, nObjCount;
					nObjCount = g_pTangramServer->m_pCurDoc -> m_TangramObjectArray.GetSize();
					for(int i = 0; i < nObjCount; i++)
					{
						if(g_pTangramServer->m_pCurDoc -> m_TangramObjectArray[i] == pObj)
						{
							nIndex = i;
							break;
						}
					}
					pObj->DeleteChild();
					delete pObj;
					//////////////////////////////////////////
					CTangramDesignView* m_pWnd = (CTangramDesignView*)g_pTangramServer->m_pCurFrame->CreateView(pContext,AFX_IDW_PANE_FIRST);
					pObj = new CTangramObject();
					pObj->m_pWnd = m_pWnd;
					if(!m_pWnd->m_pDesignerWnd)
						m_pWnd->m_pDesignerWnd = new CWnd();
					m_pWnd->CreateAxCtrl(_T(""));
					m_pWnd->OnInitialUpdate();
					g_pTangramServer->m_pCurDoc -> m_TangramObjectArray[nIndex] = pObj;
					g_pTangramServer->m_pCurFrame->RecalcLayout();
				}
				else if(m_pPWnd->IsKindOf(RUNTIME_CLASS(CFrameWnd)))
				{
					int nID = 0;
					for(int i = 0; i < g_pTangramServer->m_pCurDoc -> m_TangramObjectArray.GetSize(); i++)
					{
						if(g_pTangramServer->m_pCurDoc->m_TangramObjectArray[i] == m_pPParentObj)
						{
							nID = i;
							break;
						}
					}
					m_pPParentObj->DeleteChild();
					delete m_pPParentObj;

					m_pSplitterWnd->DestroyWindow();
					CTangramDesignView* m_pWnd = (CTangramDesignView*)((CFrameWnd*)m_pPWnd)->CreateView(pContext,AFX_IDW_PANE_FIRST);
					CTangramObject* pObj = new CTangramObject();
					pObj->m_pWnd = m_pWnd;
					m_pWnd->OnInitialUpdate();
					g_pTangramServer->m_pCurDoc -> m_TangramObjectArray[nID] = pObj;
					((CFrameWnd*)m_pPWnd)->RecalcLayout();
				}
			}
		}
		break;
	case 1:
		if(m_pParentObj->m_ContainerType==Board)
		{
			CTangramContainerWnd* m_pTabWnd = (CTangramContainerWnd*)m_pParentWnd;
			CWnd* m_pPWnd = m_pParentWnd->GetParent();
			CTangramObject* pObj = m_pParentObj;
			CTangramObject* pParentObj = m_pParentObj -> m_pParentObject;
			if(m_pPWnd->IsKindOf(RUNTIME_CLASS(CSplitterWnd)))
			{
				int Row,Col;
				((CSplitterWnd*)m_pPWnd)->IsChildPane(m_pTabWnd,&Row,&Col);
				int nIndex = Row*pParentObj->m_nCols + Col;
				pObj->DeleteChild();
				delete pObj;
				UINT ID = ((CSplitterWnd*)m_pPWnd)->IdFromRowCol(Row,Col);
				m_pTabWnd->DestroyWindow();
				CDocCntrItem* pOleItem = (CDocCntrItem*)m_pTabWnd->m_pOleItem;
				pOleItem -> Delete();
				g_pTangramServer->m_pCurDoc->SetModifiedFlag();  // Mark the document as having been modified, for
				g_pTangramServer->m_pCurDoc->UpdateAllViews(NULL);
				CCreateContext *pContext = &g_pTangramServer->m_pCurDoc->m_Context;
				pContext->m_pNewViewClass = g_pTangramServer->m_pDesignViewRuntimeClass;
				CTangramDesignView* m_pWnd = (CTangramDesignView*)pContext->m_pNewViewClass->CreateObject();
				pObj = new CTangramObject();
				m_pWnd->m_pTangramObject = pObj;
				pObj -> m_pWnd = m_pWnd;
				pObj -> m_pParentObject = pParentObj ;
				pParentObj -> m_ChildObjectMap[nIndex] = pObj;
				int dwStyle = AFX_WS_DEFAULT_VIEW;
				if (!((CWnd*)m_pWnd)->Create(NULL, NULL, dwStyle, rc, m_pPWnd, ID, &g_pTangramServer->m_pCurDoc->m_Context))
				{
					TRACE0("Warning: couldn't create client area for tab view\n");
					return ;
				}
				m_pWnd->OnInitialUpdate();
				((CSplitterWnd*)m_pPWnd)->RecalcLayout();
			}
			else if(pParentObj&&pParentObj->m_ContainerType==Board)
			{
				int nTabIndex = ((CTangramContainerWnd*)m_pPWnd)->GetActiveTab();
				CTangramObject* pParentObj = pObj->m_pParentObject;
				int nIndex =0, nObjCount;
				nObjCount = pParentObj -> m_ChildObjectMap.GetSize();
				for(int i = 0; i < nObjCount; i++)
				{
					if(pParentObj->m_ChildObjectMap[i] == pObj)
					{
						nIndex = i;
						break;
					}
				}
				CString strTitle = pObj->m_strTitle;
				CCreateContext *pContext = &g_pTangramServer->m_pCurDoc->m_Context;
				pContext->m_pNewViewClass = g_pTangramServer->m_pDesignViewRuntimeClass;
				CTangramDesignView* m_pWnd = (CTangramDesignView*)pContext->m_pNewViewClass->CreateObject();
				if (!((CWnd*)m_pWnd)->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, rc, m_pPWnd, (UINT)m_pPWnd, &g_pTangramServer->m_pCurDoc->m_Context))
				{
					TRACE0("Warning: couldn't create client area for tab view\n");
					return ;
				}
				pObj->DeleteChild();
				delete pObj;
				m_pTabWnd->DestroyWindow();
				CDocCntrItem* pOleItem = (CDocCntrItem*)m_pTabWnd->m_pOleItem;
				pOleItem -> Delete();
				((CTangramContainerWnd*)m_pPWnd)->SetTabWnd(nTabIndex,m_pWnd);
				
				((CTangramContainerWnd*)m_pPWnd)->RecalcLayout();
				g_pTangramServer->m_pCurFrame->SetActiveView(m_pWnd);
				m_pWnd->OnInitialUpdate();
				CTangramObject* pObjNew = new CTangramObject();
				pObjNew->m_strTitle = strTitle;
				pObjNew->m_pParentObject = pParentObj;
				pParentObj->m_ChildObjectMap[nIndex] = pObj;
				pObjNew->m_pWnd = m_pWnd;
				((CTangramContainerWnd*)m_pPWnd)->RecalcLayout();
			}
			else if(m_pPWnd->IsKindOf(RUNTIME_CLASS(CMDIChildWnd)))
			{
				int nIndex =0, nObjCount;
				nObjCount = g_pTangramServer->m_pCurDoc -> m_TangramObjectArray.GetSize();
				for(int i = 0; i < nObjCount; i++)
				{
					if(g_pTangramServer->m_pCurDoc -> m_TangramObjectArray[i] == pObj)
					{
						nIndex = i;
						break;
					}
				}
				pObj->DeleteChild();
				delete pObj;
				CTangramObject* pObjNew = new CTangramObject();
				m_pTabWnd->DestroyWindow();
				CDocCntrItem* pOleItem = (CDocCntrItem*)m_pTabWnd->m_pOleItem;
				pOleItem -> Delete();
				CCreateContext *pContext = &g_pTangramServer->m_pCurDoc->m_Context;
				pContext->m_pNewViewClass = g_pTangramServer->m_pDesignViewRuntimeClass;
				CTangramDesignView* m_pWnd = (CTangramDesignView*)g_pTangramServer->m_pCurFrame->CreateView(pContext,AFX_IDW_PANE_FIRST);
				pObjNew->m_pWnd = m_pWnd;
				g_pTangramServer->m_pCurDoc -> m_TangramObjectArray[nIndex] = pObjNew;
				m_pWnd->OnInitialUpdate();
				g_pTangramServer->m_pCurFrame->RecalcLayout();
			}
			else if(m_pPWnd->IsKindOf(RUNTIME_CLASS(CFrameWnd)))
			{
				int nID = 0;
				for(int i = 0; i < g_pTangramServer->m_pCurDoc -> m_TangramObjectArray.GetSize(); i++)
				{
					if(g_pTangramServer->m_pCurDoc->m_TangramObjectArray[i] == pObj)
					{
						nID = i;
						break;
					}
				}
				ASSERT(pObj);
				pObj->DeleteChild();
				delete pObj;

				CDocCntrItem* pOleItem = (CDocCntrItem*)m_pTabWnd->m_pOleItem;
				m_pTabWnd->DestroyWindow();
				pOleItem -> Delete();
				CCreateContext *pContext = &g_pTangramServer->m_pCurDoc->m_Context;
				pContext->m_pNewViewClass = g_pTangramServer->m_pDesignViewRuntimeClass;
				CTangramDesignView* m_pWnd = (CTangramDesignView*)((CFrameWnd*)m_pPWnd)->CreateView(pContext,AFX_IDW_PANE_FIRST);
				CTangramObject* pObjNew = new CTangramObject();
				pObjNew->m_pWnd = m_pWnd;
				g_pTangramServer->m_pCurDoc -> m_TangramObjectArray[nID] = pObjNew;
				m_pWnd->OnInitialUpdate();
				((CFrameWnd*)m_pPWnd)->RecalcLayout();
			}
		}
		break;
	}
	g_pTangramServer->m_pCurDoc->m_bAutoDelete = TRUE;			
	g_pTangramServer->m_pCurDoc->SetTitle(strTitle);
	g_pTangramServer->m_pCurFrame ->SetWindowText(strTitle);
	SetResource();
}

void CTangramDesignView::DeleteTabPage(long nPageIndex)
{
	CTangramObject* m_pParentObj = m_pTangramObject->m_pParentObject;
	if(m_pParentObj==NULL||m_pParentObj->m_ContainerType!=Board)return;
	g_pTangramServer->m_pCurDoc->m_bAutoDelete = FALSE;
	CString strTitle = m_pDoc->GetTitle();
	CWnd* m_pParentWnd = GetParent();
	if(m_pParentObj->m_ContainerType==Board)
	{
		CTangramContainerWnd* m_pTabWnd = (CTangramContainerWnd*)m_pParentWnd;
		long nTabIndex = m_pTabWnd->GetActiveTab();//nPageIndex;
		long nIndex=-1;
		for(int i = 0; i < m_pParentObj->m_ChildObjectMap.GetCount(); i++)
		{
			if(m_pParentObj->m_ChildObjectMap[i] == m_pTangramObject)
			{
				nIndex = i;
				break;
			}
		}
		m_pParentObj->m_ChildObjectMap.RemoveAt(nIndex);
		m_pTabWnd->RemoveTab(nTabIndex);
		//delete m_pTangramObject;
		if(m_pTabWnd->GetTabsNum()==0)
		{
			LPCTSTR lpszLabel = "New Tab";
			CRect rect(0,0,100,100);
			CCreateContext *pContext = &g_pTangramServer->m_pCurDoc->m_Context;
			pContext->m_pNewViewClass = g_pTangramServer->m_pDesignViewRuntimeClass;
			CTangramDesignView* m_pWnd = (CTangramDesignView*)pContext->m_pNewViewClass->CreateObject();
			if (!m_pWnd->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, rect, m_pTabWnd, (UINT)m_pTabWnd, pContext))
			{
				TRACE0("Warning: couldn't create client area for tab view\n");
				return ;
			}
			m_pWnd->ModifyStyle(WS_BORDER,0);
			m_pWnd->OnInitialUpdate();
			m_pTabWnd->AddTab(m_pWnd,lpszLabel,nTabIndex);
		}
		m_pTabWnd->RecalcLayout(); 
	}
	g_pTangramServer->m_pCurDoc->m_bAutoDelete = TRUE;			
	g_pTangramServer->m_pCurDoc->SetTitle(strTitle);
	g_pTangramServer->m_pCurFrame ->SetWindowText(strTitle);
	SetResource();
}

void CTangramDesignView::AddPage(long nPageIndex, CString strPageTitle, long nImageIndex)
{
	CTangramObject* m_pParentObj = m_pTangramObject->m_pParentObject;
	if(m_pParentObj==NULL||m_pParentObj->m_ContainerType!=Board)return;
	g_pTangramServer->m_pCurDoc->m_bAutoDelete = FALSE;
	CString strTitle = m_pDoc->GetTitle();
	CWnd* m_pParentWnd = GetParent();
	if(m_pParentObj->m_ContainerType==Board)
	{
		CTangramObject* pObj = new CTangramObject();
		pObj->uiImageId = nImageIndex;
		pObj->m_strTitle = strPageTitle;
		CTangramContainerWnd* m_pTabWnd = (CTangramContainerWnd*)m_pParentObj->m_pWnd;
		CRect rect(0,0,100,100);
		CCreateContext *pContext = &g_pTangramServer->m_pCurDoc->m_Context;
		pContext->m_pNewViewClass = g_pTangramServer->m_pDesignViewRuntimeClass;
		CTangramDesignView* m_pWnd = (CTangramDesignView*)pContext->m_pNewViewClass->CreateObject();
		int dwStyle = AFX_WS_DEFAULT_VIEW;
		if (!m_pWnd->Create(NULL, NULL, dwStyle, rect, m_pTabWnd, (UINT)m_pTabWnd, pContext))
		{
			TRACE0("Warning: couldn't create client area for tab view\n");
			return ;
		}
		m_pWnd->ModifyStyle(WS_BORDER,0);
		m_pWnd->m_pTangramObject = pObj;
		pObj->m_pWnd = m_pWnd;
		pObj->m_pParentObject = m_pParentObj;
		int nCount = m_pParentObj->m_ChildObjectMap.GetSize();
		m_pParentObj->m_ChildObjectMap.SetAtGrow(nCount,pObj);
		m_pTabWnd->AddTab(m_pWnd,strPageTitle,nImageIndex);
		m_pWnd->OnInitialUpdate();
		m_pTabWnd->RecalcLayout(); 
	}
	g_pTangramServer->m_pCurDoc->m_bAutoDelete = TRUE;			
	g_pTangramServer->m_pCurDoc->SetTitle(strTitle);
	g_pTangramServer->m_pCurFrame ->SetWindowText(strTitle);
	SetResource();
}

void CTangramDesignView::OnPaint() 
{
	Default();
}

void CTangramDesignView::SetDesignerPage()
{
	LoadFromResource(IDR_HTML);
}

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