// GenericListDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed.h"
#include "GenericListDlg.h"
#include <direct.h>
#include <io.h>

/////////////////////////////////////////////////////////////////////////////
// CGenericListDlg dialog


CGenericListDlg::CGenericListDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGenericListDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGenericListDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_Title = "";
	m_Strict = true;
	FillCallback = NULL;
}


void CGenericListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGenericListDlg)
	DDX_Control(pDX, IDC_GENLIST, m_List);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CGenericListDlg, CDialog)
	//{{AFX_MSG_MAP(CGenericListDlg)
	ON_WM_CLOSE()
	ON_LBN_DBLCLK(IDC_GENLIST, OnDblclkGenlist)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGenericListDlg message handlers

BOOL CGenericListDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	SetWindowText(m_Title);

	_finddata_t dirinfo,fileinfo;

	char oldDir[MAX_PATH],key[MAX_PATH],*p;
	_getcwd(oldDir,MAX_PATH - 1);
	_chdir(m_Path.GetBuffer(MAX_PATH));

	long dirHandle = _findfirst("*.*",&dirinfo);
	if (dirHandle != -1)
	{
		do
		{
			if (dirinfo.attrib & _A_SUBDIR)
			{
				if (strstr(dirinfo.name,"."))
					continue;

				_chdir(dirinfo.name);
				if (m_Strict)
				{
					sprintf(key,"%s.%s",dirinfo.name,m_Extension);
					if (_findfirst(key,&fileinfo) != -1)
					{
						strcpy(key,fileinfo.name);
						p = strrchr(key,'.');
						*p = 0;

						int index = m_List.AddString(key);
						char* path = new char [strlen(fileinfo.name) + strlen(m_Path) + strlen(dirinfo.name) + 3];
						sprintf(path,"%s\\%s\\%s",m_Path,dirinfo.name,fileinfo.name);

						m_List.SetItemDataPtr(index,(void*)path);
					}
				}
				else
				{
					sprintf(key,"*.%s",m_Extension);
					long handle = _findfirst(key,&fileinfo);
					if (handle != -1)
					{
						strcpy(key,fileinfo.name);
						p = strrchr(key,'.');
						*p = 0;

						int index = m_List.AddString(key);
						char* path = new char [strlen(fileinfo.name) + strlen(m_Path) + strlen(dirinfo.name) + 3];
						sprintf(path,"%s\\%s\\%s",m_Path,dirinfo.name,fileinfo.name);

						m_List.SetItemDataPtr(index,(void*)path);

						while (_findnext(handle,&fileinfo) == 0)
						{
							strcpy(key,fileinfo.name);
							p = strrchr(key,'.');
							*p = 0;

							index = m_List.AddString(key);
							path = new char [strlen(fileinfo.name) + strlen(m_Path) + strlen(dirinfo.name) + 3];
							sprintf(path,"%s\\%s\\%s",m_Path,dirinfo.name,fileinfo.name);

							m_List.SetItemDataPtr(index,(void*)path);
						}
					}
				}
				_chdir("..");
			}
		}
		while(_findnext(dirHandle,&dirinfo) == 0);
	}
	_chdir(oldDir);
	m_List.SetCurSel(0);

	if (FillCallback)
		FillCallback(&m_List);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CGenericListDlg::DeletePathStrings() 
{
	for (int i = 0; i < m_List.GetCount(); i++)
	{
		char* p = (char*)m_List.GetItemDataPtr(i);
		delete [] p;
	}
	
	CDialog::OnClose();
}

void CGenericListDlg::OnOK() 
{
	int sel = m_List.GetCurSel();

	if (sel != -1)
	{
		m_List.GetText(m_List.GetCurSel(),m_Name);
		m_Path = (char*) m_List.GetItemData(m_List.GetCurSel());
	}	
	DeletePathStrings();

	CDialog::OnOK();
}

void CGenericListDlg::OnClose() 
{
	DeletePathStrings();
}

void CGenericListDlg::OnCancel() 
{
	DeletePathStrings();
	CDialog::OnCancel();
}

void CGenericListDlg::OnDblclkGenlist() 
{
	OnOK();
}
