// LastFileDialog.cpp : implementation file
//

#include "stdafx.h"
#include "EPro.h"
#include "LastFileDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLastFileDialog

IMPLEMENT_DYNAMIC(CLastFileDialog, CFileDialog)

CLastFileDialog::CLastFileDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
		DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
		CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
{
	m_ofn.Flags |= OFN_ENABLETEMPLATE;

	//SAMPLE: get the right module handle
	m_ofn.hInstance = AfxGetResourceHandle();

	//SAMPLE: set the template to load
	m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_EXTERN);

}


BEGIN_MESSAGE_MAP(CLastFileDialog, CFileDialog)
	//{{AFX_MSG_MAP(CLastFileDialog)
	ON_WM_DESTROY()
	ON_LBN_DBLCLK(IDC_LASTLIST, OnDblclkLastlist)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


int CLastFileDialog::DoModal()
{
	int nRes;
	nRes=CFileDialog::DoModal();
	if(m_bLastFile==FALSE)
	{
		if(nRes==IDOK)
		{
			AddLastFile(GetPathName());	
			SaveLastFileName();
		}
		return nRes;
	}
	else
	{
		SaveLastFileName();
		return IDOK;
	}
}

BOOL CLastFileDialog::OnInitDialog() 
{
	CFileDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_pList=(CListBox*)GetDlgItem(IDC_LASTLIST);
	m_bLastFile=FALSE;
	LoadLastFileName();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

extern CString m_strAppPath;

void CLastFileDialog::LoadLastFileName()
{
	CString strKey;
	CString strFile;
	CString strFileName;
	strFile=m_strAppPath+"LastFile.ini";
	int FileNumber;
	FileNumber=GetPrivateProfileInt("File Number","n",0,strFile);
	TRACE("FileNumber:%d\n",FileNumber);
	m_arStr.RemoveAll();
	for(int n=0;n<FileNumber;n++)
	{
		strKey.Format("File %d",n);
		::GetPrivateProfileString("LastFile",strKey,NULL,strFileName.GetBuffer(200),200,strFile);
		m_arStr.Add(strFileName);
		m_pList->InsertString(0,strFileName);
	}
	CString strFileNum;
	strFileNum.Format("%d",m_arStr.GetSize());
	WritePrivateProfileString("File Number","n",strFileNum,strFile);	
}

void CLastFileDialog::SaveLastFileName()
{
	CString strKey;
	CString strFile;
	strFile=m_strAppPath+"LastFile.ini";
	for(int n=0;n<m_arStr.GetSize();n++)
	{
		strKey.Format("File %d",n);
		WritePrivateProfileString("LastFile",strKey,m_arStr.GetAt(n),strFile);
	}
	CString strFileNum;
	strFileNum.Format("%d",m_arStr.GetSize());
	WritePrivateProfileString("File Number","n",strFileNum,strFile);

}

void CLastFileDialog::OnDestroy() 
{

	CFileDialog::OnDestroy();
	// TODO: Add your message handler code here
}

void CLastFileDialog::AddLastFile(CString strFile)
{
	TRACE("AddLastFile: %d\n",m_arStr.GetSize());
		
		int n,m;
		
		for(n=0;n<m_arStr.GetSize();n++)
		{
			if(m_arStr.GetAt(n)==strFile)
			{
				
				for(m=n;m<=m_arStr.GetSize()-2;m++)
				{
					m_arStr.SetAt(m,m_arStr.GetAt(m+1));
				}
				m_arStr.SetAt(m_arStr.GetSize()-1,strFile);
				return;
			}
		}
		if(m_arStr.GetSize()>=10)
		{
			for(n=0;n<=8;n++)
			{
				m_arStr.SetAt(n,m_arStr.GetAt(n+1));
			}
			m_arStr.SetAt(9,strFile);
		}
		else
			m_arStr.Add(strFile);


}

void CLastFileDialog::OnOK()
{
	HWND hWnd;
	hWnd=GetParent()->GetSafeHwnd();
	::PostMessage(hWnd,WM_CLOSE,0,0);
	CFileDialog::OnOK();
}

void CLastFileDialog::OnDblclkLastlist() 
{
	// TODO: Add your control notification handler code here
	int nSel=m_pList->GetCurSel();
	if(nSel<0)
		return;
	CString strFileName;
	m_pList->GetText(nSel,strFileName);
	AddLastFile(strFileName);
	m_bLastFile=TRUE;
	OnOK();
}

CString CLastFileDialog::GetLastFileName()
{
	return m_strLastFileName=m_arStr.GetAt(m_arStr.GetSize()-1);
}

CString CLastFileDialog::GetLastFileExt()
{
	CString strLastFileExt;
	char* pExt;
	int n;
	m_strLastFileName=m_arStr.GetAt(m_arStr.GetSize()-1);
	n=m_strLastFileName.GetLength();
	pExt=m_strLastFileName.GetBuffer(n);
	for(n;n>=0;n--)
	{
		if(pExt[n-1]=='.')
			break;
	}
	return strLastFileExt=&pExt[n];
}


