// KeyTypeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ContentTrack.h"
#include "KeyTypeDlg.h"
#include "EditKeyType.h"

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

/////////////////////////////////////////////////////////////////////////////
// CKeyTypeDlg dialog


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


void CKeyTypeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKeyTypeDlg)
	DDX_Control(pDX, IDC_MODIFY, m_btModify);
	DDX_Control(pDX, IDC_LIST_TYPES, m_listTypes);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CKeyTypeDlg, CDialog)
	//{{AFX_MSG_MAP(CKeyTypeDlg)
	ON_NOTIFY(NM_CLICK, IDC_LIST_TYPES, OnClickListTypes)
	ON_BN_CLICKED(IDC_MODIFY, OnModify)
	ON_BN_CLICKED(IDC_REMOVE, OnRemove)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKeyTypeDlg message handlers
extern CStringMap g_StringMap;

BOOL CKeyTypeDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
CDialog::OnInitDialog();
	
	m_listTypes.InsertColumn(0,"Key Name",LVCFMT_LEFT,300);
	m_listTypes.InsertColumn(1,"Key Type",LVCFMT_LEFT,100);

	CPtrList list;
	g_StringMap.MakeList(list);
	POSITION pos = list.GetHeadPosition();
	while (pos)
	{
		POSITION lpos = (POSITION)list.GetNext(pos);

		CString key = g_StringMap.GetName(lpos);
		BYTE val = g_StringMap.GetValue(lpos);
		int index = m_listTypes.InsertItem(0,key);

		CString szValue = NAME_KEYTYPE[0] + CString("(NULL)");
		szValue = NAME_KEYTYPE[(int)val];

		m_listTypes.SetItemText(index,1,szValue);
		m_listTypes.SetItemData(index,(DWORD)lpos);
	}	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CKeyTypeDlg::OnClickListTypes(NMHDR* pNMHDR, LRESULT* pResult) 
{
	POSITION pos = this->m_listTypes.GetFirstSelectedItemPosition();
	if (pos)
		this->m_btModify.EnableWindow(true);
	else 
		this->m_btModify.EnableWindow(false);	
	*pResult = 0;
}

void CKeyTypeDlg::OnModify() 
{
	CEditKeyType dlg;
	int count = this->m_listTypes.GetSelectedCount();
	POSITION pos = this->m_listTypes.GetFirstSelectedItemPosition();
	while (pos)
	{
		int index = m_listTypes.GetNextSelectedItem(pos);
		POSITION lpos = (POSITION)m_listTypes.GetItemData(index);
		CString str = m_listTypes.GetItemText(index,0);
	
		dlg.m_editName = str;
		dlg.m_iCurSel = g_StringMap.GetValue(lpos);

		if (IDOK == dlg.DoModal())
		{
			m_bDirty = true;
			CString str = dlg.m_editName;
			//this->m_map[str] = (void *)dlg.m_iCurSel;
			g_StringMap.SetValue(lpos,dlg.m_iCurSel);

			m_listTypes.SetItemText(index,0,str);
			m_listTypes.SetItemText(index,1,NAME_KEYTYPE[dlg.m_iCurSel]);
			//m_listTypes.SetItemData(index,dlg.m_iCurSel);
		}
	}

	
}

void CKeyTypeDlg::OnRemove() 
{
	// TODO: Add your control notification handler code here
	
}

void CKeyTypeDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	
}


void CKeyTypeDlg::SetMap(CMapPtrToWord *map)
{
	m_pmap = map;
/*	POSITION pos = map.GetStartPosition();
	while (pos)
	{
		CString key;
		void *value;
		map.GetNextAssoc(pos,key,value);

		this->m_map[key] = value;
	}	 */
}

/*
void CKeyTypeDlg::GetMap(CMapStringToPtr &map)
{
	map.RemoveAll();
	POSITION pos = m_map.GetStartPosition();
	while (pos)
	{
		CString key;
		void *value;
		m_map.GetNextAssoc(pos,key,value);

		map[key] = value;
	}	 
}
*/