// TextureListDlg.cpp : implementation file
//

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

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

/////////////////////////////////////////////////////////////////////////////
// CTextureListDlg dialog


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


void CTextureListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTextureListDlg)
	DDX_Control(pDX, IDC_LIST_NOHINT, m_listNoHint);
	DDX_Control(pDX, IDC_LIST_HINT, m_listHint);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTextureListDlg, CDialog)
	//{{AFX_MSG_MAP(CTextureListDlg)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTextureListDlg message handlers

BOOL CTextureListDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	Stuff::NotationFile::PageIterator *pages = m_pHintFile->MakePageIterator();
	Stuff::Page *page;
	while ((page = pages->ReadAndNext()) != NULL)
	{
		int index = m_listHint.AddString(page->GetName());
		m_listHint.SetItemData(index,(DWORD)page);
	}
	delete pages;
	
	CFileFind find;
	if (find.FindFile("content\\textures\\*.*",0))
	{
		BOOL bDone = false;
		while (!bDone)
		{
			bDone = !find.FindNextFile();
			if (!find.IsDots() && !find.IsDirectory())
			{
				CString fullname = find.GetFileName();
				if (stricmp(".png",fullname.Right(4))==0)
				{
					CString name = find.GetFileTitle();
					int index = m_listHint.FindStringExact(-1,name);
					if (LB_ERR == index)
					{
						m_listNoHint.AddString(name);
					}
					else
					{
//						int x=1;
					}
				}

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

void CTextureListDlg::OnAdd() 
{
	CPropertiesDlg dlg;
	int index = m_listNoHint.GetCurSel();
	CString name;
	m_listNoHint.GetText(index,name);
	Stuff::Page *page = m_pHintFile->AddPage(name);
	dlg.AddPage(page);

	HRSRC hRSRC = FindResource(NULL,MAKEINTRESOURCE(IDD_PROPERTIES),RT_DIALOG);
	HGLOBAL hGlobal = LoadResource(NULL,hRSRC);
	DLGTEMPLATE *pTemplate = (DLGTEMPLATE *)hGlobal;
	pTemplate->style &= ~WS_CHILD;
	pTemplate->style |= WS_POPUPWINDOW;

	//dlg.InitModalIndirect(pTemplate,this);
	dlg.DoModal();
	//m_dlgProp.ShowWindow(SW_SHOW);





	m_listNoHint.DeleteString(index);
	index = m_listHint.AddString(name);
	m_listHint.SetCurSel(index);
}
