// PropertiesDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Megatron.h"
#include "PropertiesDlg.h"
#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPropertiesDlg dialog


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

	
//Mipmap=Default|None
//ReloadFromDisk=true|false
//ReadOnly=true|false
//Memory=Default|AGP|Video
//PageOut=Default|First|Last
//MipFilter=Box|Point
//PinkIsAlpha=false|true
//BlueIsAlpha=false|true
//NoGamma=false|true

	CStringList *pList = new CStringList;
	pList->AddTail("Default");
	pList->AddTail("None");
	m_mapParams["Mipmap"] = pList;

	pList = new CStringList;
	pList->AddTail("true");
	pList->AddTail("false");
	m_mapParams["ReloadFromDisk"] = pList;

	pList = new CStringList;
	pList->AddTail("true");
	pList->AddTail("false");
	m_mapParams["ReadOnly"] = pList;

	pList = new CStringList;
	pList->AddTail("Default");
	pList->AddTail("AGP");
	pList->AddTail("Video");
	m_mapParams["Memory"] = pList;

	pList = new CStringList;
	pList->AddTail("Box");
	pList->AddTail("Point");
	m_mapParams["MipFilter"] = pList;

	pList = new CStringList;
	pList->AddTail("true");
	pList->AddTail("false");
	m_mapParams["PinkIsAlpha"] = pList;

	pList = new CStringList;
	pList->AddTail("true");
	pList->AddTail("false");
	m_mapParams["BlueIsAlpha"] = pList;

	pList = new CStringList;
	pList->AddTail("true");
	pList->AddTail("false");
	m_mapParams["NoGamma"] = pList;

	m_pPage = NULL;
	m_pNote = NULL;
	m_bIsView = false;
}

CPropertiesDlg::~CPropertiesDlg()
{
	POSITION pos = m_mapParams.GetStartPosition();
	while (pos)
	{
		CObject *pObj;
		CString key;
		m_mapParams.GetNextAssoc(pos,key,pObj);
		CStringList *pList = (CStringList *)pObj;
		delete pList;
	}
	m_mapParams.RemoveAll();
}

void CPropertiesDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPropertiesDlg)
	DDX_Control(pDX, IDC_ADD, m_btAdd);
	DDX_Control(pDX, IDOK, m_btOK);
	DDX_Control(pDX, IDC_LIST_VALUES, m_listValues);
	DDX_Control(pDX, IDC_LIST_PARAMS, m_listParams);
	//}}AFX_DATA_MAP

}


BEGIN_MESSAGE_MAP(CPropertiesDlg, CDialog)
	//{{AFX_MSG_MAP(CPropertiesDlg)
	ON_LBN_SELCHANGE(IDC_LIST_PARAMS, OnSelchangeListParams)
	ON_LBN_SELCHANGE(IDC_LIST_VALUES, OnSelchangeListValues)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropertiesDlg message handlers
int CPropertiesDlg::AddPage(Stuff::Page *page)
{
	Stuff::Page::NoteIterator *notes = page->MakeNoteIterator();
	Stuff::Note *note;
	m_pNote = NULL;
	while ((note=notes->ReadAndNext()) != NULL)
	{
		CString name = note->GetName();
		int index = m_listParams.FindStringExact(-1,name);
		int cursel = m_listParams.GetCurSel();
		if (index == cursel)
		{
			const char *value;
			note->GetEntry(&value);
			int findex = m_listValues.FindStringExact(-1,value);
			if (LB_ERR!=findex)
			{
				m_pNote = note;
				m_listValues.SetCurSel(findex);
			}
		}
	}
	delete notes;

	m_pPage = page;
	if (NULL == m_pNote)
	{
		if (m_listValues.GetSafeHwnd())
		{
			m_listValues.SetCurSel(0);
		}
	}
	return 0;
}

int CPropertiesDlg::RemovePage(Stuff::Page *page)
{
	return 0;
}

BOOL CPropertiesDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	if (m_bIsView)
	{
		m_btOK.EnableWindow(false);
	}
	else
	{
		m_btAdd.EnableWindow(false);
	}

	POSITION pos = m_mapParams.GetStartPosition();
	while (pos)
	{
		CObject *pObj;
		CString key;
		m_mapParams.GetNextAssoc(pos,key,pObj);
		CStringList *pList = (CStringList *)pObj;

		int index = m_listParams.AddString(key);
		m_listParams.SetItemData(index,(DWORD)pList);
	}
	m_listParams.SetCurSel(0);
	OnSelchangeListParams();

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

void CPropertiesDlg::OnSelchangeListParams() 
{
	m_listValues.ResetContent();
	int index = m_listParams.GetCurSel();
	DWORD data = m_listParams.GetItemData(index);
	CStringList *pList = (CStringList *)data;

	CString curstr;
	m_listParams.GetText(index,curstr);
	const char *value = NULL;
	bool bset = false;
	if (m_pPage && ((m_pNote = m_pPage->FindNote(curstr)) != NULL))
	{
		m_pNote->GetEntry(&value);
		bset = true;
	}

	bool bfound = false;
	m_listValues.AddString("");
	POSITION pos = pList->GetHeadPosition();
	while(pos)
	{
		CString str = pList->GetNext(pos);
		int cur = m_listValues.AddString(str);
		if (bset)
		{
			if (stricmp(str,value)==0)
			{
				m_listValues.SetCurSel(cur);
				bfound = true;
			}
		}
	}
	if (!bfound)
		m_listValues.SetCurSel(0);
}

void CPropertiesDlg::OnSelchangeListValues() 
{
	int index = m_listValues.GetCurSel();
	CString str;
	m_listValues.GetText(index,str);
	if (index>0)
	{
		if (m_pNote)
			m_pNote->SetEntry(str);
		else
		{
			CString name;
			m_listParams.GetText(m_listParams.GetCurSel(),name);
			m_pNote = m_pPage->AddNote(name);
			m_pNote->SetEntry(str);
		}
	}
	else
	{
		if (m_pNote)
			m_pPage->DeleteNote(str);
	}
}

void CPropertiesDlg::OnAdd() 
{
	CMainFrame *pFrame = (CMainFrame*)AfxGetMainWnd();
	pFrame->Add();
}
