// FeaturesTab.cpp : implementation file
//
#include "stdafx.h"
#include "tctb.h"
#include "FeaturesTab.h"
#include "FeatureEditDlg.h"
#include "Feature.h"


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

/////////////////////////////////////////////////////////////////////////////
// CFeaturesTab property page

IMPLEMENT_DYNCREATE(CFeaturesTab, CPropertyPage)

CFeaturesTab::CFeaturesTab() : CPropertyPage(CFeaturesTab::IDD)
{
	//{{AFX_DATA_INIT(CFeaturesTab)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	PList=NULL;
}

CFeaturesTab::~CFeaturesTab()
{
}

void CFeaturesTab::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFeaturesTab)
	DDX_Control(pDX, IDC_FEATURELIST, m_FList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFeaturesTab, CPropertyPage)
//{{AFX_MSG_MAP(CFeaturesTab)
ON_NOTIFY(NM_DBLCLK, IDC_FEATURELIST, OnDblclkFeaturelist)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFeaturesTab message handlers

void CFeaturesTab::UpdateList(CListBox *paths)
{
	int i;
	CString cpath;
	
	m_FList.DeleteAllItems();
	for(i=0;i<paths->GetCount();i++)
	{
		paths->GetText(i,cpath);
		FillBranch(cpath);
	}
	
}

void CFeaturesTab::FillBranch(CString cpath)
{
	WIN32_FIND_DATA fd;
	HANDLE han;
	CString ext;
	cpath+="\\";
	han=FindFirstFile(cpath+"*.*",&fd);
	LVFINDINFO lvf;
	lvf.flags=LVFI_STRING;
	Feature fet;
	
	if(INVALID_HANDLE_VALUE!=han)
		do
		{
			if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY
				&& strcmp(fd.cFileName,".") && strcmp(fd.cFileName,".."))
			{
				FillBranch(cpath+fd.cFileName);
			}
			else
			{
				ext=fd.cFileName;
				ext=ext.Right(ext.GetLength()-(ext.ReverseFind('.')+1));
				lvf.psz=fd.cFileName;
				if(ext=="tctf" && m_FList.FindItem(&lvf)==-1)
				{
					fet.Load(cpath+fd.cFileName);
					m_FList.InsertItem(0,fd.cFileName);
					m_FList.SetItemText(m_FList.FindItem(&lvf),1,fet.GetName());
					m_FList.SetItemText(m_FList.FindItem(&lvf),2,cpath);
				}
			}
		} 
		while(FindNextFile(han,&fd));
		
}

BOOL CFeaturesTab::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_FList.InsertColumn(0,"File",LVCFMT_LEFT,100);
	m_FList.InsertColumn(1,"Name",LVCFMT_LEFT,100);
	m_FList.InsertColumn(2,"Path",LVCFMT_LEFT,100);
	
	//	LayList.SetImageList(imlist,LVSIL_SMALL);  
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CFeaturesTab::OnSetActive() 
{
	if(PList && PList->Changed==TRUE)
	{
		UpdateList(&PList->m_PList);
		PList->Changed=FALSE;
	}
	
	return CPropertyPage::OnSetActive();
}

void CFeaturesTab::OnDblclkFeaturelist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	CFeatureEditDlg dlg;
	char nme[MAX_PATH],pth[MAX_PATH];
	CString str;
	
	POSITION p;
	int sel;
	
	p=m_FList.GetFirstSelectedItemPosition();
	if(p!=NULL)
	{
		sel=m_FList.GetNextSelectedItem(p);
		m_FList.GetItemText(sel,2,pth,MAX_PATH);
		m_FList.GetItemText(sel,0,nme,MAX_PATH);
		str=pth;
		str+=nme;
		Feature fet(str);
		dlg.SetData(fet);
		
		if(dlg.DoModal()==IDOK)
		{
			dlg.GetData(&fet);
			fet.Save();
		}
		UpdateList(&PList->m_PList);
	}
	*pResult = 0;
}


