// ClassInfoDlg_TreeIcons.cpp : implementation file
//

#include "stdafx.h"
#include "..\AsCfgBuilder.h"
#include "ClassInfoDlg_TreeIcons.h"
#include "XmlClassInfo.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClassInfoDlg_TreeIcons dialog


CClassInfoDlg_TreeIcons::CClassInfoDlg_TreeIcons(CWnd* pParent /*=NULL*/)
	: CPropertyEditorDlg(CClassInfoDlg_TreeIcons::IDD, pParent),
	classInfo_(NULL),
	lastIconGroup_(NULL),
	singleInstanceNode_(NULL),
	mapNode_(NULL),
	arrayNode_(NULL)
{
	//{{AFX_DATA_INIT(CClassInfoDlg_TreeIcons)
	//}}AFX_DATA_INIT
}


void CClassInfoDlg_TreeIcons::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClassInfoDlg_TreeIcons)
	DDX_Control(pDX, IDC_BNT_APPLY_TO_CHILDREN, btnApplyToChildren_);
	DDX_Control(pDX, IDC_CHK_EXPANDED, chkExpanded_);
	DDX_Control(pDX, IDC_TRV_GROUPS, trvGroups_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CClassInfoDlg_TreeIcons, CPropertyEditorDlg)
	//{{AFX_MSG_MAP(CClassInfoDlg_TreeIcons)
	ON_WM_SIZE()
	ON_NOTIFY(TVN_SELCHANGED, IDC_TRV_GROUPS, OnSelchangedTrvGroups)
	ON_BN_CLICKED(IDC_CHK_EXPANDED, OnChkExpanded)
	ON_BN_CLICKED(IDC_BNT_APPLY_TO_CHILDREN, OnBntApplyToChildren)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClassInfoDlg_TreeIcons message handlers

void CClassInfoDlg_TreeIcons::StoreData()
{
	if (lastIconGroup_ == NULL)
		return;

	StoreIcons(lastIconGroup_);
	
}

void CClassInfoDlg_TreeIcons::SetData(CXmlBaseElement* element)
{
	classInfo_ = dynamic_cast<CXmlClassInfo*>(element);
	assert(classInfo_);
	if (classInfo_ == NULL)
		return;

	string iconFileName;
	HTREEITEM selNode;

	selNode = trvGroups_.GetSelectedItem();

	if (selNode == singleInstanceNode_ || selNode == NULL)
		lastIconGroup_ = classInfo_->GetTreeIcons();
	else if (selNode == mapNode_)
		lastIconGroup_ = classInfo_->GetMapTreeIcons();
	else 
		lastIconGroup_ = classInfo_->GetArrayTreeIcons();

	DisplayIcons(lastIconGroup_);
		
	if (selNode == NULL)
		trvGroups_.Select(singleInstanceNode_, TVGN_CARET);
}

void CClassInfoDlg_TreeIcons::OnDataUpdate()
{}



void CClassInfoDlg_TreeIcons::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	UpdateWindowSize();	
}

void CClassInfoDlg_TreeIcons::UpdateWindowSize()
{
	if (!chooserExpandedSelected_.GetSafeHwnd())
		return;

	CRect rc;
	CRect chooserRect;
	CRect trvRect;
	CRect newTrvRect;
	int top;
	int chooserLeft;
	int chooserWidth;

	GetClientRect(rc);

	trvGroups_.GetWindowRect(trvRect);
	ScreenToClient(trvRect);

	newTrvRect.left = 0;
	newTrvRect.top = 2;
	newTrvRect.right = trvRect.Width();
	newTrvRect.bottom = rc.Height() - 4 + 2;
	if (newTrvRect != trvRect) {
		trvGroups_.SetWindowPos(&CWnd::wndTop, 0, 2, trvRect.Width(), rc.Height() - 4, SWP_SHOWWINDOW);
		trvGroups_.GetWindowRect(trvRect);
		ScreenToClient(trvRect);
	}	
	chooserLeft = trvRect.right + 5;
	chooserWidth = rc.Width() - trvRect.Width() - 5;

	chooserExpandedSelected_.GetClientRect(chooserRect);

	// resize choosers
	//
	vector<CClassInfoDlg_TreeIcons_Chooser*> choosers;

	choosers.push_back(&chooserExpandedSelected_);
	choosers.push_back(&chooserExpandedNotSelected_);
	choosers.push_back(&chooserCollapsedSelected_);
	choosers.push_back(&chooserCollapsedNotSelected_);

	top = rc.top + 2;
	for (int i = 0; i < choosers.size(); i++) {
		choosers[i]->SetWindowPos(&CWnd::wndTop, chooserLeft, top, chooserWidth, chooserRect.Height(), SWP_SHOWWINDOW);
		top += chooserRect.Height() + 5;
	}

	chkExpanded_.SetWindowPos(&CWnd::wndTop, chooserLeft, top, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
	
	CRect btnRect;

	btnApplyToChildren_.GetWindowRect(&btnRect);
	btnApplyToChildren_.SetWindowPos(&CWnd::wndTop, rc.right - btnRect.Width(), top, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE); 

}

BOOL CClassInfoDlg_TreeIcons::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	ModifyStyle(0, WS_CLIPCHILDREN);

	chooserExpandedSelected_.Create(chooserExpandedSelected_.IDD, this);
	chooserExpandedSelected_.SetLabel(_T("Icon to be used when item is selected and expanded"));
	chooserExpandedSelected_.SetProject(GetDocument()->GetProject());

	chooserExpandedNotSelected_.Create(chooserExpandedNotSelected_.IDD, this);
	chooserExpandedNotSelected_.SetLabel(_T("Icon to be used when item is not selected and expanded"));
	chooserExpandedNotSelected_.SetProject(GetDocument()->GetProject());

	chooserCollapsedSelected_.Create(chooserCollapsedSelected_.IDD, this);
	chooserCollapsedSelected_.SetLabel(_T("Icon to be used when item is selected and collapsed"));
	chooserCollapsedSelected_.SetProject(GetDocument()->GetProject());

	chooserCollapsedNotSelected_.Create(chooserCollapsedNotSelected_.IDD, this);
	chooserCollapsedNotSelected_.SetLabel(_T("Icon to be used when item is not selected and collapsed"));
	chooserCollapsedNotSelected_.SetProject(GetDocument()->GetProject());

	imageList_.Create(16, 16, ILC_COLOR32 | ILC_MASK, 4, 4);
	imageList_.Add(AfxGetApp()->LoadIcon(IDI_EMPTY));
	imageList_.Add(AfxGetApp()->LoadIcon(IDI_ITEM_SELECTION));

	trvGroups_.SetImageList(&imageList_, TVSIL_NORMAL);
	singleInstanceNode_ = trvGroups_.InsertItem(_T("Single item node"), 0, 1);
	mapNode_ = trvGroups_.InsertItem(_T("Map container node"), 0, 1);
	arrayNode_ = trvGroups_.InsertItem(_T("Array container node"), 0, 1);

	UpdateWindowSize();

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


void CClassInfoDlg_TreeIcons::OnSelchangedTrvGroups(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	HTREEITEM selItem;

	if (classInfo_) {
		StoreIcons(lastIconGroup_);

		selItem = pNMTreeView->itemNew.hItem;
		if (selItem == singleInstanceNode_)
			lastIconGroup_ = classInfo_->GetTreeIcons();
		else if (selItem == mapNode_)
			lastIconGroup_ = classInfo_->GetMapTreeIcons();
		else
			lastIconGroup_ = classInfo_->GetArrayTreeIcons();
		
		DisplayIcons(lastIconGroup_);
	}

	*pResult = 0;
}

void CClassInfoDlg_TreeIcons::DisplayIcons(CXmlTreeIcons* treeIcons)
{
	if (treeIcons) {
		UpdateChooser(chooserExpandedSelected_, treeIcons->GetIconExpandedSelected());
		UpdateChooser(chooserExpandedNotSelected_, treeIcons->GetIconExpandedNotSelected());
		UpdateChooser(chooserCollapsedSelected_, treeIcons->GetIconCollapsedSelected());
		UpdateChooser(chooserCollapsedNotSelected_, treeIcons->GetIconCollapsedNotSelected());

		chkExpanded_.SetCheck(treeIcons->GetExpandedAtStartup());
	}
}

void CClassInfoDlg_TreeIcons::StoreIcons(CXmlTreeIcons* treeIcons)
{
	if (treeIcons) {
		treeIcons->SetIconExpandedSelected(GetIconFileName(chooserExpandedSelected_));
		treeIcons->SetIconExpandedNotSelected(GetIconFileName(chooserExpandedNotSelected_));
		treeIcons->SetIconCollapsedSelected(GetIconFileName(chooserCollapsedSelected_));
		treeIcons->SetIconCollapsedNotSelected(GetIconFileName(chooserCollapsedNotSelected_));
		treeIcons->SetExpandedAtStartup(chkExpanded_.GetCheck() == TRUE);
	}
}

void CClassInfoDlg_TreeIcons::UpdateChooser(CClassInfoDlg_TreeIcons_Chooser& chooser, 
											const cmString& iconFileName)
{
	CXmlIcon* icon;

	icon = GetDocument()->GetProject()->GetProjectIcons()->GetIcon(iconFileName);

	if (icon) {
		chooser.SetEnabled(iconFileName.GetLength() > 0);
		chooser.SetIconFileName(iconFileName);
		chooser.SetIconHandle(icon->GetIconHandle16());
	} else {
		chooser.SetEnabled(false);
		chooser.SetIconFileName(_T(""));
		chooser.SetIconHandle(NULL);
	}
	chooser.Invalidate();
}

cmString CClassInfoDlg_TreeIcons::GetIconFileName(CClassInfoDlg_TreeIcons_Chooser& chooser)
{
	cmString iconFileName;

	if (chooser.GetEnabled())
		iconFileName = chooser.GetIconFileName();
	else
		iconFileName = _T("");

	return iconFileName;
}

void CClassInfoDlg_TreeIcons::OnChkExpanded() 
{
		
}

void CClassInfoDlg_TreeIcons::OnBntApplyToChildren() 
{
	if (classInfo_) {
			
		bool expanded;

		expanded = (chkExpanded_.GetCheck() == TRUE);

		classInfo_->SetExpandedAtStartup(expanded, true);
	}	
}
