// ChooseDir.cpp : implementation file
//

#include "stdafx.h"
#include "fontedit.h"
#include "ChooseDir.h"
#include "direct.h"

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

/////////////////////////////////////////////////////////////////////////////
// CChooseDir dialog


CChooseDir::CChooseDir(CWnd* pParent /*=NULL*/)
	: CDialog(CChooseDir::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChooseDir)
	m_Dir = _T("");
	//}}AFX_DATA_INIT
	szCurrDir[0] = 0;
	szTitle[0] = 0;
}


void CChooseDir::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChooseDir)
	DDX_Control(pDX, IDC_FILELIST, m_FileList);
	DDX_Control(pDX, IDC_DRIVES, m_Drives);
	DDX_Control(pDX, IDC_DIRPATH, m_DirPath);
	DDX_Control(pDX, IDC_DIRLIST, m_DirList);
	DDX_Text(pDX, IDC_DIRPATH, m_Dir);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChooseDir, CDialog)
	//{{AFX_MSG_MAP(CChooseDir)
	ON_LBN_DBLCLK(IDC_DIRLIST, OnDblclkDirlist)
	ON_CBN_SELCHANGE(IDC_DRIVES, OnSelchangeDrives)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChooseDir message handlers

BOOL CChooseDir::OnInitDialog() 
{
	CDialog::OnInitDialog();
	// fill drive list
	if (szTitle[0])
		SetWindowText(szTitle);
	if (!szCurrDir[0]) // if no directory has been set then choose the current drive and directory
		_getcwd(szCurrDir,sizeof(szCurrDir));
	char szDrive[]="A:\\";
	nCurrDrive = _getdrive()-1;
	for (int nDrive=0; nDrive<26; nDrive++)
	{
		szDrive[0] = 'A' + nDrive;
		char szDescription[260];
		DWORD dwType = GetDriveType(szDrive);
		sprintf(szDescription,"[%c:]",nDrive+'A');
		switch(dwType)
		{
			case 0:
			case 1:
				continue; // invalid drives
			case DRIVE_REMOVABLE:
				strcat(szDescription," - removable");				
				break;
			case DRIVE_FIXED:
			case DRIVE_REMOTE:
				{
					char szName[260];
					char szFileSystemName[260];
					DWORD dwJunk;
					GetVolumeInformation(szDrive,szName,sizeof(szName),&dwJunk,&dwJunk,&dwJunk,szFileSystemName,sizeof(szFileSystemName));
					strcat(szDescription," - ");
					strcat(szDescription,szName);
				}
				break;
				break;
			case DRIVE_CDROM:
				strcat(szDescription," - CDROM");				
				break;
			case DRIVE_RAMDISK:
				strcat(szDescription," - RAMDISK");				
				break;
		}
		int nIndex = m_Drives.AddString(szDescription);
		m_Drives.SetItemData(nIndex,nDrive);
		if (nDrive == nCurrDrive)
			m_Drives.SetCurSel(nIndex);
	}

	// fill in directory list
	FillDirList();

	m_Dir	= szCurrDir;
	m_DirPath.SetWindowText(szCurrDir);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CChooseDir::FillDirList()
{
	WIN32_FIND_DATA find;
	char szFind[260];
	strcpy(szFind,szCurrDir);
	int nLen = strlen(szFind);
	if (szFind[nLen-1] != '\\')
		strcat(szFind,"\\");
	strcat(szFind,"*.*");
	HANDLE hFind = FindFirstFile(szFind,&find);
	BOOL bContinue=hFind!=INVALID_HANDLE_VALUE;
	m_DirList.ResetContent();
	m_FileList.ResetContent();
	while	(bContinue)
	{
		if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (find.cFileName[0] == '.' && find.cFileName[1]==NULL)
				; // don't show the single dot
			else
				m_DirList.AddString(find.cFileName);
		}	
		else
		{
			m_FileList.AddString(find.cFileName);
		}
		bContinue = FindNextFile(hFind,&find);
	}
	FindClose(hFind);
}

void CChooseDir::OnDblclkDirlist() 
{
	int nItem = m_DirList.GetCurSel();
	char szDirName[260];
	m_DirList.GetText(nItem,szDirName);
	if (szDirName[0] == '.')
	{ // back up one directory
		int nLastSlash = -1;
		int nChar=0;
		while (szCurrDir[nChar])
		{
			if (szCurrDir[nChar] == '\\')
				nLastSlash = nChar;
			nChar++;
		}	
		if (nLastSlash >=0)
			szCurrDir[nLastSlash] = 0;
		FillDirList();
		m_DirPath.SetWindowText(szCurrDir);
	}
	else
	{ // go into the next directory
		int nLen = strlen(szCurrDir);
		if (szCurrDir[nLen-1] != '\\')
			strcat(szCurrDir,"\\");
		strcat(szCurrDir,szDirName);
	}
	FillDirList();
	m_DirPath.SetWindowText(szCurrDir);		
}

void CChooseDir::OnSelchangeDrives() 
{
	int nItem = m_Drives.GetCurSel();
	int nDrive = m_Drives.GetItemData(nItem);
	sprintf(szCurrDir,"%c:\\",nDrive+'A');
	FillDirList();
	m_DirPath.SetWindowText(szCurrDir);
}

void CChooseDir::OnOK() 
{
	char szTemp[260];
	m_DirPath.GetWindowText(szTemp,sizeof(szTemp));
	if (!strcmp(szTemp,".."))
	{ // back up one dir
		int nLastSlash = -1;
		int nChar=0;
		while (szCurrDir[nChar])
		{
			if (szCurrDir[nChar] == '\\')
				nLastSlash = nChar;
			nChar++;
		}	
		if (nLastSlash >=0)
			szCurrDir[nLastSlash] = 0;
		FillDirList();
		m_DirPath.SetWindowText(szCurrDir);
		return;
	}
	if (strcmp(szTemp,szCurrDir))
	{ // the user has typed in the entry.  try to set to the directory they typed
		if (_chdir(szTemp)==0) // success
		{
			strcpy(szCurrDir,szTemp);
			FillDirList();
			return;
		}
		else
		{
			// do a little file name validation
			if (!isalpha(szTemp[0]) || szTemp[1] != ':' || szTemp[2] != '\\')
			{
				m_DirPath.SetWindowText(szCurrDir);
				return;
			}
			int nResult = MessageBox("The selected directory does not exist.  Would you like to create it?","Question",MB_YESNO);
			if (nResult == IDNO)
				return;
			else
			{
				int nChar=0;
				int nSlashCount=-1;
				strcpy(szCurrDir,szTemp);
				while (szCurrDir[nChar])
				{
					if (szCurrDir[nChar] == '\\')
					{
						nSlashCount++;
						if (nSlashCount > 0)
						{
							szCurrDir[nChar] = 0;
							CreateDirectory(szCurrDir,NULL);	
							szCurrDir[nChar] = '\\';
						}
					}
					nChar++;
				}
				CreateDirectory(szCurrDir,NULL);	
			}
		}
	}

	CDialog::OnOK();
}

void CChooseDir::OnCancel() 
{
	CDialog::OnCancel();
}
