//*******************************************************************************
// 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
//*******************************************************************************
// Document.cpp : Implementation of CDocumentCtrl
#include "stdafx.h"
#include "VisualFrameWork.h"
#include "DocumentCtrl.h"
#include "VisualFrameWorkDoc.h"
CDocumentCtrl::CDocumentCtrl()
{
m_pDoc = g_pTangramServer->m_pCurDoc;
((CVisualFrameWorkDoc*)m_pDoc)->m_pDocument = this;
m_pDoc->m_pDocDisp = this;
m_pTangramDocDisp = this;
m_pPicture=NULL;
int nDocCount = g_pTangramServer->GetDocsCount();
if(nDocCount==0)
{
m_strCaption = _T("VisualFrameWork");
}
else
m_strCaption.Format(_T("New Document %d"),nDocCount);
m_bstrCaption = m_strCaption.AllocSysString();
m_bMDI = FALSE;
}
CDocumentCtrl::~CDocumentCtrl()
{
g_pTangramServer->m_pDocsCollection->RemoveFromCollection(m_pDoc);
}
void CDocumentCtrl::FinalRelease()
{
}
HRESULT CDocumentCtrl::FinalConstruct()
{
m_pDoc->m_pDocCtrl = this;
return S_OK;
}
/////////////////////////////////////////////////////////////////////////////
ULONG CDocumentCtrl::InternalRelease()
{
return CComObjectRootEx<CComSingleThreadModel>::InternalRelease();
}
STDMETHODIMP CDocumentCtrl::get_ObjectX(BSTR ObjName, IDispatch* *pVal)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
USES_CONVERSION;
CString strName = OLE2T(ObjName);
strName.MakeLower();
CTangramObject* m_pTangramObject = NULL;
m_pDoc->m_TangramObjectDictionary.Lookup(strName,(CObject*&)m_pTangramObject);
if(m_pTangramObject)
{
*pVal = m_pTangramObject->m_pDisp;
//(*pVal)->AddRef();
m_pTangramObject->m_pDisp->AddRef();
m_pTangramObject->m_bHooked = true;
}
return S_OK;
}
STDMETHODIMP CDocumentCtrl::get_DocTitle(BSTR *pVal)
{
USES_CONVERSION;
AFX_MANAGE_STATE(AfxGetStaticModuleState())
// TODO: Add your implementation code here
*pVal = m_pDoc->GetTitle().AllocSysString();
return S_OK;
}
STDMETHODIMP CDocumentCtrl::put_DocTitle(BSTR newVal)
{
USES_CONVERSION;
//AFX_MANAGE_STATE(AfxGetStaticModuleState())
// TODO: Add your implementation code here
m_bstrCaption = newVal;
m_strCaption = OLE2T(m_bstrCaption);
m_pDoc->SetTitle(OLE2T(newVal));
return S_OK;
}
STDMETHODIMP CDocumentCtrl::get_MfcObjectX(BSTR ObjName, long *pVal)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
USES_CONVERSION;
CString strName = OLE2T(ObjName);
CTangramObject* m_pTangramObject = NULL;
m_pDoc->m_TangramObjectDictionary.Lookup(strName,(CObject*&)m_pTangramObject);
if(m_pTangramObject)
{
*pVal = (long)m_pTangramObject->m_pWnd;
}
return S_OK;
}
void CDocumentCtrl::ShowPropertyPage()
{
ISpecifyPropertyPages *pISPP;
CAUUID caGUID;
HRESULT hr;
if (FAILED(QueryInterface(IID_ISpecifyPropertyPages, (void **)&pISPP))) //m_pIDispatch->
{
return;
}
hr=pISPP->GetPages(&caGUID);
pISPP->Release();
if (FAILED(hr))
{
return;
}
IDispatch* m_pIDispatch = this;
hr=OleCreatePropertyFrame(m_pDoc->m_pFrame->m_hWnd, 10, 10, L"TangramDoc" , 1, (IUnknown **)&m_pIDispatch, caGUID.cElems , caGUID.pElems, LOCALE_USER_DEFAULT, 0L, NULL);
if (FAILED(hr))
{
}
CoTaskMemFree((void *)caGUID.pElems);
return;
}
STDMETHODIMP CDocumentCtrl::CreateSplitter(BSTR RowCount, BSTR ColCount, BSTR bstrTitle, BSTR bDocking)
{
USES_CONVERSION;
if(!RowCount||!ColCount)return S_OK;
long nRow = atoi(OLE2T(RowCount));
long nCol = atoi(OLE2T(ColCount));
if(nRow<=0||nCol<=0)return S_OK;
CString s = OLE2T(bDocking);
m_pDoc->m_pActiveDesignView->CreateSplitter(nRow,nCol,OLE2T(bstrTitle), false);
return S_OK;
}
STDMETHODIMP CDocumentCtrl::CreateDockBar(BSTR bstrCaption)
{
USES_CONVERSION;
CString strCaption = OLE2T(bstrCaption);
//if(m_pDoc->m_pActiveDesignView)
//{
//m_pDoc->m_pActiveDesignView->CreateActiveItem(_T(""),_T(""),strCaption,TRUE);
//}
if(m_pDoc->m_bMainDoc)
{
m_pDoc->CreateDockingBar(strCaption);
}
// TODO: Add your implementation code here
return S_OK;
}
STDMETHODIMP CDocumentCtrl::DocProperty()
{
ShowPropertyPage();
return S_OK;
}
STDMETHODIMP CDocumentCtrl::CreateActiveItem(BSTR bstrClsID, BSTR bstrName)
{
USES_CONVERSION;
CString strName = OLE2T(bstrName);
strName.Trim();
if(strName==_T(""))
{
AfxMessageBox(_T("Please Input a Object Name"));
return S_OK;
}
CString strIndex = OLE2T(bstrClsID);
strIndex.MakeLower();
strIndex.TrimLeft();
strIndex.TrimLeft();
if(strIndex==_T(""))return S_OK;
int nPos = -1;
nPos = strIndex.Find(_T("."));
CString sIndex;
if(nPos>0)
{
sIndex = strIndex.Left(nPos);
sIndex+=_T(".cnn");
// TODO: Add your implementation code here
void* pVoid = NULL;
if(!g_pTangramServer->m_ExternalCnnDictionary.Lookup(LPCTSTR(sIndex),pVoid))
{
CComPtr<IDispatch> pDisp = NULL;
pDisp.CoCreateInstance(sIndex.AllocSysString());
if(pDisp!=NULL)
{
g_pTangramServer->m_strCurExternCnn =sIndex;
BSTR szMember=L"Connect";
DISPID dispid;
DISPPARAMS dispparams;
HRESULT hr = pDisp->GetIDsOfNames(IID_NULL, &szMember, 1,LOCALE_USER_DEFAULT, &dispid);
if(hr != S_OK)
{
pDisp.p->Release();
return S_FALSE;
}
VARIANTARG rgvarg[1];
dispparams.cArgs=1;
dispparams.rgvarg=rgvarg;
dispparams.rgvarg[0].vt=VT_I4;
dispparams.rgvarg[0].lVal=(long)g_pTangramServer->m_pObjExtManager;
dispparams.cNamedArgs=0;
hr = pDisp->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,&dispparams, NULL,NULL,NULL);
if(hr != S_OK)
{
pDisp.p->Release();
return S_FALSE;
}
IDispatch* pCnnDisp = pDisp.p;
pCnnDisp->AddRef();
g_pTangramServer->m_ExternalCnnDictionary[LPCTSTR(sIndex)] = pCnnDisp;
nPos = sIndex.Find(_T("."));
CString s1;
s1 = sIndex.Left(nPos);
g_pTangramServer->m_strCurExternCnn=_T("");
m_pDoc->m_CnnDictionary[LPCTSTR(s1)] = sIndex;
}
}
}
// TODO: Add your implementation code here
m_pDoc->m_pActiveDesignView->CreateActiveItem(OLE2T(bstrClsID),OLE2T(bstrName));
return S_OK;
}
STDMETHODIMP CDocumentCtrl::CreateTabWnd(BSTR bstrClsID, BSTR bstrCaption, BSTR bstrObjName)
{
//AFX_MANAGE_STATE(AfxGetStaticModuleState())
USES_CONVERSION;
m_pDoc->m_pActiveDesignView->CreateTabWnd(OLE2T(bstrClsID),OLE2T(bstrCaption),OLE2T(bstrObjName),FALSE);
return S_OK;
}
STDMETHODIMP CDocumentCtrl::DeleteSplitter()
{
m_pDoc->m_pActiveDesignView->DeleteContainer(0);
// TODO: Add your implementation code here
return S_OK;
}
STDMETHODIMP CDocumentCtrl::DeleteTabWnd()
{
m_pDoc->m_pActiveDesignView->DeleteContainer(1);
// TODO: Add your implementation code here
return S_OK;
}
STDMETHODIMP CDocumentCtrl::DeleteTabPage(long nTabIndex)
{
m_pDoc->m_pActiveDesignView->DeleteTabPage(nTabIndex);
// TODO: Add your implementation code here
return S_OK;
}
STDMETHODIMP CDocumentCtrl::AddPage(long nPageIndex, BSTR bstrTitle, long nImageIndex)
{
USES_CONVERSION;
m_pDoc->m_pActiveDesignView->AddPage(nPageIndex,OLE2T(bstrTitle),nImageIndex);
// TODO: Add your implementation code here
return S_OK;
}
HBITMAP CDocumentCtrl::GetBitmapFromDoc()
{
if(m_pPicture)
{
LPPICTURE pPict = NULL;
m_pPicture->QueryInterface(IID_IPicture, (LPVOID*)&pPict);
OLE_HANDLE h;
pPict->get_Handle(&h);
HBITMAP hBmp = (HBITMAP)h;
return hBmp;
}
return NULL;
}
void CDocumentCtrl::ConnectExternalLib(CString strCnn)
{
USES_CONVERSION;
CString strIndex = strCnn;
strIndex.MakeLower();
strIndex.TrimLeft();
strIndex.TrimLeft();
if(strIndex==_T(""))return ;
// TODO: Add your implementation code here
void* pVoid = NULL;
if(!g_pTangramServer->m_ExternalCnnDictionary.Lookup(LPCTSTR(strIndex),pVoid))
{
g_pTangramServer->m_strCurExternCnn =strIndex;
CComPtr<IDispatch> pDisp = NULL;
pDisp.CoCreateInstance(strCnn.AllocSysString());
if(pDisp!=NULL)
{
BSTR szMember=L"Connect";
DISPID dispid;
DISPPARAMS dispparams;
HRESULT hr = pDisp->GetIDsOfNames(IID_NULL, &szMember, 1,LOCALE_USER_DEFAULT, &dispid);
if(hr != S_OK)
{
pDisp.p->Release();
return ;
}
VARIANTARG rgvarg[1];
dispparams.cArgs=1;
dispparams.rgvarg=rgvarg;
dispparams.rgvarg[0].vt=VT_I4;
dispparams.rgvarg[0].lVal=(long)g_pTangramServer->m_pObjExtManager;
dispparams.cNamedArgs=0;
hr = pDisp->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,&dispparams, NULL,NULL,NULL);
if(hr != S_OK)
{
pDisp.p->Release();
return ;
}
IDispatch* pCnnDisp = pDisp.p;
pCnnDisp->AddRef();
g_pTangramServer->m_ExternalCnnDictionary[LPCTSTR(strIndex)] = pCnnDisp;
int nPos = strIndex.Find(_T("."));
CString s1;
s1 = strIndex.Left(nPos);
//Clear ExternalCnn:
g_pTangramServer->m_strCurExternCnn=_T("");
m_pDoc->m_CnnDictionary[LPCTSTR(s1)] = strIndex;
}
}
}