// ProjectIconsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AsCfgBuilder.h"
#include "ProjectIconsDlg.h"
#include "XmlProject.h"
#include <map>

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

/////////////////////////////////////////////////////////////////////////////
// CProjectIconsDlg dialog


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


void CProjectIconsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProjectIconsDlg)
	DDX_Control(pDX, IDC_CMD_REMOVE, cmdRemove_);
	DDX_Control(pDX, IDOK, cmdOk_);
	DDX_Control(pDX, IDC_ICONS, lvwIcons_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CProjectIconsDlg, CDialog)
	//{{AFX_MSG_MAP(CProjectIconsDlg)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_CMD_ADD, OnCmdAdd)
	ON_BN_CLICKED(IDC_CMD_REMOVE, OnCmdRemove)
	ON_NOTIFY(NM_CLICK, IDC_ICONS, OnClickIcons)
	ON_WM_DROPFILES()
	ON_NOTIFY(NM_DBLCLK, IDC_ICONS, OnDblclkIcons)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProjectIconsDlg message handlers

void CProjectIconsDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	
}

void CProjectIconsDlg::OnCmdAdd() 
{
	TCHAR curDir[MAX_PATH];
	TCHAR tmpDir[MAX_PATH];
	cmString startDir;
	int ret;

	GetCurrentDirectory(MAX_PATH, curDir);

	if (lastIconDir_.GetLength())
		startDir = lastIconDir_;
	else
		startDir = curDir;
	
	SetCurrentDirectory(startDir);
	CFileDialog dlg(TRUE, _T("*.ico"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Icon Files (*.ico)|*.ico|"), this);

	ret = dlg.DoModal();
	if (ret == IDOK) {
		cmString iconFileName;
		CXmlIcon* newIcon;
		
		GetCurrentDirectory(MAX_PATH, tmpDir);
		lastIconDir_ = tmpDir;
		iconFileName = dlg.GetPathName();

		newIcon = project_->GetProjectIcons()->AddIcon(iconFileName);
		selectedIcon_ = iconFileName;
		AddImage(newIcon->GetIconHandle32(), newIcon->GetFileName());
		lvwIcons_.SetFocus();
	}

	SetCurrentDirectory(curDir);
}

void CProjectIconsDlg::OnCmdRemove() 
{
	int selIndex;

	selIndex = lvwIcons_.GetNextItem(-1, LVNI_SELECTED);
	if (selIndex != -1) {
		cmString iconFileName;

		iconFileName = (const TCHAR*)lvwIcons_.GetItemText(selIndex, 0);
		project_->GetProjectIcons()->RemoveIcon(iconFileName);
		lvwIcons_.DeleteItem(selIndex);
		selectedIcon_ = _T("");;
	}
	cmdOk_.EnableWindow(FALSE);
	cmdRemove_.EnableWindow(FALSE);
}

void CProjectIconsDlg::OnOK() 
{
	int selIndex;

	selIndex = lvwIcons_.GetNextItem(-1, LVNI_SELECTED);
	if (selIndex == -1)
		selectedIcon_ = _T("");
	else
		selectedIcon_ = lvwIcons_.GetItemText(selIndex, 0);

	CDialog::OnOK();
}

void CProjectIconsDlg::SetProject(CXmlProject* project)
{
	project_ = project;
}

CXmlProject* CProjectIconsDlg::GetProject() const
{
	return project_;
}

BOOL CProjectIconsDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	imageList_.Create(32, 32, ILC_COLOR32 | ILC_MASK, 50, 50);
	
	// load project icons
	//
	if (project_) {
		IconMap::iterator it;
		IconMap* icons;
		cmString iconFileName;
		CXmlIcon* icon;

		icons = project_->GetProjectIcons()->GetIconList();
		
		for (it = icons->begin(); it != icons->end(); it++) {
			icon = it->second;
					
			AddImage(icon->GetIconHandle32(), icon->GetFileName());
		}	
	
	}
	lvwIcons_.SetImageList(&imageList_, TVSIL_NORMAL);

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

void CProjectIconsDlg::AddImage(const cmString& iconFileName)
{
	int imageIndex;
	int itemIndex;
	HICON hIcon;
	
	hIcon = (HICON)::LoadImage(AfxGetInstanceHandle(), iconFileName, IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
	
	imageIndex = imageList_.Add(hIcon);

	itemIndex = lvwIcons_.InsertItem(0, iconFileName, imageIndex);
	if (iconFileName == selectedIcon_)
		lvwIcons_.SetItemState(itemIndex, LVNI_SELECTED | LVIS_FOCUSED, 0x000F/*LVIF_STATE*/);
}

void CProjectIconsDlg::AddImage(HICON hIcon, const cmString& iconFileName)
{
	int imageIndex;
	int itemIndex;

	imageIndex = imageList_.Add(hIcon);

	itemIndex = lvwIcons_.InsertItem(lvwIcons_.GetItemCount(), iconFileName, imageIndex);
	if (iconFileName == selectedIcon_)
		lvwIcons_.SetItemState(itemIndex, LVNI_SELECTED | LVIS_FOCUSED, 0x000F/*LVIF_STATE*/);
	lvwIcons_.EnsureVisible(itemIndex, FALSE);

}

cmString CProjectIconsDlg::GetSelectedIcon() const
{
	return selectedIcon_;
}

void CProjectIconsDlg::SetSelectedIcon(const cmString& iconFileName)
{
	selectedIcon_ = iconFileName;
}

void CProjectIconsDlg::OnClickIcons(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int selIndex;

	selIndex = lvwIcons_.GetNextItem(-1, LVNI_SELECTED);
	
	cmdOk_.EnableWindow(selIndex != -1);
	cmdRemove_.EnableWindow(selIndex != -1);
	
	*pResult = 0;
}

void CProjectIconsDlg::OnDropFiles(HDROP hDropInfo) 
{
	TCHAR buf[MAX_PATH];
	int fileCount;
	cmString fileName;
	cmString ext;
	CXmlIcon* icon;

	fileCount = DragQueryFile(hDropInfo, 0xffffffff, buf, sizeof(buf));
	
	for (int i = 0; i < fileCount; i++) {
		DragQueryFile(hDropInfo, i, buf, sizeof(buf));

		fileName = buf;
		ext = fileName.Mid(fileName.GetLength() - 4);
		
		if (fileName.GetLength() < 4 || (ext.CompareNoCase(_T(".ICO"))))
			continue;
		icon = project_->GetProjectIcons()->AddIcon(fileName);
		AddImage(icon->GetIconHandle32(), icon->GetFileName());
	}
	CDialog::OnDropFiles(hDropInfo);
}

void CProjectIconsDlg::OnDblclkIcons(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int selIndex;

	selIndex = lvwIcons_.GetNextItem(-1, LVNI_SELECTED);
	
	cmdOk_.EnableWindow(selIndex != -1);
	cmdRemove_.EnableWindow(selIndex != -1);
	
	OnOK();

	*pResult = 0;
}
