Click here to Skip to main content
15,894,107 members
Articles / Desktop Programming / MFC

Develop MFC Doc/View Application Which Supports Any Number Document Template

Rate me:
Please Sign up or sign in to vote.
1.57/5 (3 votes)
2 Jun 20052 min read 36.5K   1.7K   26  
This article provides an introduction to TangramLittle, a C++ Framework for MFC and the .NET Framework. Knowledge in MFC and the .NET Framework is assumed.
// DocTemplateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "[!output APP_HEADER]"
#include "DocTemplateDlg.h"


// CDocTemplateDlg dialog

IMPLEMENT_DYNAMIC(CDocTemplateDlg, CDialog)
CDocTemplateDlg::CDocTemplateDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDocTemplateDlg::IDD, pParent)
{
	strCurTabText = _T("Normal");
	m_strTemplateDoc = _T("");
}

CDocTemplateDlg::~CDocTemplateDlg()
{
}

void CDocTemplateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TemplateTAB, m_tabCtrl);
	DDX_Control(pDX, IDC_TemplateLIST, m_TemplateList);
}


BEGIN_MESSAGE_MAP(CDocTemplateDlg, CDialog)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_TemplateLIST, OnLvnItemchangedTemplatelist)
	ON_NOTIFY(NM_DBLCLK, IDC_TemplateLIST, OnNMDblclkTemplatelist)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TemplateTAB, OnTcnSelchangeTemplatetab)
END_MESSAGE_MAP()


// CDocTemplateDlg message handlers

BOOL CDocTemplateDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	TCITEM tcItem;
	tcItem.mask = TCIF_TEXT;
	tcItem.pszText = _T("Normal");

	m_TemplateList.InsertColumn(0,_T("Template"),0,100);
	m_TemplateList.InsertColumn(1,_T("Template Script"),0,250);
	m_TemplateList.InsertColumn(2,_T("Create Date"),0,100);
	m_TemplateList.InsertColumn(3,_T("Author"),0,60);
	m_tabCtrl.InsertItem(0, &tcItem);
	
	CFileFind finder;

	// build a string with wildcards
	CString strWildcard = g_pDotNetExtImpl->m_strDocTemplatePath;
	strWildcard += _T("*.*");

	// start working for files
	BOOL bWorking = finder.FindFile(strWildcard);
	int nIndex = 0;
	int nIndex2 = 0;
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		CString strFileName = finder.GetFileName();
		CString strFileName2 = strFileName;
		strFileName2.MakeLower();
		int nPos = strFileName2.Find(_T(".xml"));
		if(nPos!=-1)
		{
			strFileName=strFileName.Left(nPos);
			m_TemplateList.InsertItem(nIndex2,strFileName);
			CTime m_Time;
			finder.GetCreationTime(m_Time);
			CString s = m_Time.Format(_T("%B %d, %Y"));
			m_TemplateList.SetItemText(nIndex2, 2, s);
			nIndex2++;
		}

		if (finder.IsDots())
			continue;

		// if it's a directory, recursively search it

		if (finder.IsDirectory())
		{
			CString str = finder.GetFilePath();
			TCHAR t = '\\';
			nPos = str.ReverseFind(t);
			str = str.Mid(nPos+1);
			CString strWildcard = g_pDotNetExtImpl->m_strDocTemplatePath;
			strWildcard += str;
			strWildcard += _T("\\*.xml");

			CFileFind finder2;
			// start working for files
			BOOL bWorking = finder2.FindFile(strWildcard);
			int nCount = 0;
			while (bWorking)
			{
				nCount++;
				break;
			}
			finder2.Close();
			if(nCount)
			{
				tcItem.pszText = LPSTR(LPCTSTR(str));
				nIndex++;
				m_tabCtrl.InsertItem(nIndex, &tcItem);
			}
		}
	}

	finder.Close();
	RECT  rc;
	GetClientRect(&rc);
	if(::IsWindow(m_tabCtrl.m_hWnd)&&::IsWindow(m_TemplateList.m_hWnd))
	{
		m_tabCtrl.GetWindowRect(&rc);
		m_tabCtrl.AdjustRect(false,&rc);
		ScreenToClient(&rc);
		rc.top+=2;
		m_TemplateList.MoveWindow(&rc,true);
	}
	m_TemplateList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
		 
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDocTemplateDlg::OnLvnItemchangedTemplatelist(NMHDR *pNMHDR, LRESULT *pResult)
{
	POSITION pos = m_TemplateList.GetFirstSelectedItemPosition();
	if (pos == NULL)
		TRACE0("No items were selected!\n");
	else
	{
		while (pos)
		{
			int nItem = m_TemplateList.GetNextSelectedItem(pos);
			CString strItemText = m_TemplateList.GetItemText(nItem,0);
			m_strTemplateDoc = _T("");
			if(strCurTabText==_T("Normal"))
				m_strTemplateDoc = g_pDotNetExtImpl->m_strDocTemplatePath + strItemText + _T(".xml");
			else
			{
				m_strTemplateDoc = g_pDotNetExtImpl->m_strDocTemplatePath;
				m_strTemplateDoc += strCurTabText;
				m_strTemplateDoc += _T("\\");
				m_strTemplateDoc += strItemText;
				m_strTemplateDoc += _T(".xml");
			}
			TRACE("Item %d was selected!-%s\n", nItem,m_strTemplateDoc);
		}
	}	

	// TODO: Add your control notification handler code here
	*pResult = 0;
}

void CDocTemplateDlg::OnNMDblclkTemplatelist(NMHDR *pNMHDR, LRESULT *pResult)
{
	POSITION pos = m_TemplateList.GetFirstSelectedItemPosition();
	if (pos == NULL)
	{
		TRACE0("No items were selected!\n");
		return;
	}
	else
	{
		while (pos)
		{
			int nItem = m_TemplateList.GetNextSelectedItem(pos);
			CString strItemText = m_TemplateList.GetItemText(nItem,0);
			m_strTemplateDoc = _T("");
			if(strCurTabText==_T("Normal"))
				m_strTemplateDoc = g_pDotNetExtImpl->m_strDocTemplatePath + strItemText + _T(".xml");
			else
			{
				m_strTemplateDoc = g_pDotNetExtImpl->m_strDocTemplatePath;
				m_strTemplateDoc += strCurTabText;
				m_strTemplateDoc += _T("\\");
				m_strTemplateDoc += strItemText;
				m_strTemplateDoc += _T(".xml");
			}
			TRACE("Item %d was selected!-%s\n", nItem,m_strTemplateDoc);
		}
	}	
	OnBnClickedOk();
	*pResult = 0;
}


void CDocTemplateDlg::OnBnClickedOk()
{
	CString strXMLFile(m_strTemplateDoc);
	TCHAR nChar = '.';
	int nPos = strXMLFile.ReverseFind(nChar);
	strXMLFile = strXMLFile.Left(nPos);
	strXMLFile += _T(".xml");

	CFile file;
	CString csText;
	CString csNotes;
	if (file.Open(strXMLFile, CFile::modeRead))
	{
		int nFileLen = (int)file.GetLength();

		// Allocate buffer for binary file data
		unsigned char* pBuffer = new unsigned char[nFileLen + 2];
		nFileLen = file.Read( pBuffer, nFileLen );
		file.Close();
		pBuffer[nFileLen] = '\0';
		pBuffer[nFileLen+1] = '\0'; // in case 2-byte encoded

		// Windows Unicode file is detected if starts with FEFF
		if ( pBuffer[0] == 0xff && pBuffer[1] == 0xfe )
		{
			// Contains byte order mark, so assume wide char content
			// non _UNICODE builds should perform UCS-2 (wide char) to UTF-8 conversion here
			csText = (LPCWSTR)(&pBuffer[2]);
			csNotes += _T("File starts with hex FFFE, assumed to be wide char format. ");
		}
		else
		{
			// _UNICODE builds should perform UTF-8 to UCS-2 (wide char) conversion here
			csText = (LPCSTR)pBuffer;
		}
		delete [] pBuffer;

		VARIANT_BOOL bSuccess=false;
		g_pDotNetExtImpl->m_pXMLDocDisp->loadXML(CComBSTR(csText),&bSuccess);
		if(bSuccess)
		{
			CComPtr<IXMLDOMElement> iRootElm;
			g_pDotNetExtImpl->m_pXMLDocDisp->get_documentElement(&iRootElm);

			TElem eroot(iRootElm);
			g_pDotNetExtImpl->m_strCurDocTemplateID = eroot.attr(_T("DocViewID"));
			g_pDotNetExtImpl->m_strCurDocObjID = eroot.attr(_T("DocObjID"));
			g_pDotNetExtImpl->m_strCurExtDocObjID = eroot.attr(_T("ExtDocObjID"));
		}
	}
	if(g_pDotNetExtImpl->m_bMDIVisible==false)
	{
		//g_pDotNetExtImpl->m_pMDIClientView->ShowWindow(SW_SHOW);
		g_pDotNetExtImpl->m_pMDIClientView->MoveWindow(g_pDotNetExtImpl->rcOldClient);
		g_pDotNetExtImpl->m_pMDIClientFrameWnd->RecalcLayout();
	}
	OnOK();
}

void CDocTemplateDlg::OnTcnSelchangeTemplatetab(NMHDR *pNMHDR, LRESULT *pResult)
{
	g_pDotNetExtImpl->m_strCurDocTemplateID = _T("");
	m_TemplateList.DeleteAllItems();
	*pResult = 0;
	TCITEM TabCtrlItem ;
	LPTSTR pstr = new TCHAR[MAX_PATH];
	TabCtrlItem.pszText = pstr;
	TabCtrlItem.mask|=TCIF_TEXT;
	TabCtrlItem.cchTextMax = 256;
	int index = m_tabCtrl.GetCurSel();
	m_tabCtrl.GetItem(index,&TabCtrlItem);
	CString strItem = TabCtrlItem.pszText;
	strCurTabText = strItem;
	delete pstr;
	CString strWildcard = g_pDotNetExtImpl->m_strDocTemplatePath;
	if(strItem==_T("Normal"))
	{
		strWildcard+=_T("*.xml");
	}
	else
	{
		strWildcard+=strItem;;
		strWildcard+=_T("\\*.xml");
	}

	CFileFind finder;

	// start working for files
	BOOL bWorking = finder.FindFile(strWildcard);
	int nIndex = 0;
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		CString strFileName = finder.GetFileName();
		CString strFileName2 = strFileName;
		strFileName2.MakeLower();
		int nPos = strFileName2.Find(_T(".xml"));
		if(nPos!=-1)
		{
			strFileName=strFileName.Left(nPos);
			m_TemplateList.InsertItem(nIndex,strFileName);
			CTime m_Time;
			finder.GetCreationTime(m_Time);
			CString s = m_Time.Format(_T("%B %d, %Y"));
			m_TemplateList.SetItemText(nIndex, 2, s);
			nIndex++;
		}
	}

	finder.Close();
}

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



Comments and Discussions