// SuffixDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AnimGenerate.h"
#include "SuffixDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSuffixDlg dialog


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


void CSuffixDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSuffixDlg)
	DDX_Control(pDX, IDC_STATIC_ITEMCOUNT, m_ctlItemCount);
	DDX_Control(pDX, IDC_LIST_SUFFIX, m_lbSuffix);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSuffixDlg, CDialog)
	//{{AFX_MSG_MAP(CSuffixDlg)
	ON_BN_CLICKED(IDC_SCAN, OnScan)
	ON_BN_CLICKED(IDC_REMOVE, OnRemove)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSuffixDlg message handlers

void CSuffixDlg::OnScan() 
{
	CString dir = theApp.BrowseDirectory(NULL,this);
	CFileFind finder;
	BOOL bWorking = finder.FindFile(dir+"\\*.Max");
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		CString szName = finder.GetFileTitle();
		for (int length = 0;(szName[length] != '_') && (length < szName.GetLength());length++);
		if (length < szName.GetLength())
			szName.Delete(0,length+1);
		this->m_lbSuffix.AddString(szName);
	}
	CString str;
	str.Format("%i Items.",this->m_lbSuffix.GetCount());
	this->m_ctlItemCount.SetWindowText(str);
}

BOOL CSuffixDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	CString str;
	for (POSITION pos = this->m_strList.GetHeadPosition();pos;this->m_strList.GetNext(pos))
		this->m_lbSuffix.AddString(this->m_strList.GetAt(pos));
	str.Format("%i Items.",this->m_lbSuffix.GetCount());
	this->m_ctlItemCount.SetWindowText(str);
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSuffixDlg::OnRemove() 
{
	int iCount = this->m_lbSuffix.GetSelCount();
	int *iArr = new int[iCount];
	this->m_lbSuffix.GetSelItems(iCount,iArr);
	for (;iCount;iCount--)
	{
		this->m_lbSuffix.DeleteString(iArr[iCount-1]);
	}
	delete[] iArr;
	CString str;
	str.Format("%i Items.",this->m_lbSuffix.GetCount());
	this->m_ctlItemCount.SetWindowText(str);
}

void CSuffixDlg::OnAdd() 
{
	CFileDialog dlg(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT,"Max Files (*.max)|*.max||",this);
	char szDir[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,szDir);
	dlg.m_ofn.lpstrInitialDir = szDir;
	if (IDOK == dlg.DoModal())
	{
		POSITION pos = dlg.GetStartPosition();
		while (pos)
		{
			CString path = dlg.GetNextPathName(pos);
			char fname[20];
			_splitpath((LPCTSTR)path,NULL,NULL,fname,NULL);

			CString szName = CString(fname);
			for (int length = 0;(szName[length] != '_') && (length < szName.GetLength());length++);
				if (length < szName.GetLength())
			szName.Delete(0,length+1);

			this->m_lbSuffix.AddString(szName);
		}
		CString str;
		str.Format("%i Items.",this->m_lbSuffix.GetCount());
		this->m_ctlItemCount.SetWindowText(str);
	}	
}

void CSuffixDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	this->m_strList.RemoveAll();
	for (int x=this->m_lbSuffix.GetCount();x;x--)
	{
		CString str;
		this->m_lbSuffix.GetText(x-1,str);
		this->m_strList.AddHead(str);
	}
}
