#include "stdafx.h"
#include "MapLayerManage.h"


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

/////////////////////////////////////////////////////////////////////////////
// CMapLayerManage dialog


CMapLayerManage::CMapLayerManage(CWnd* pParent /*=NULL*/)
	: CDialog(CMapLayerManage::IDD, pParent)
{

}


void CMapLayerManage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMapLayerManage)
	DDX_Control(pDX, IDC_TREE, m_TreeCtrl);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMapLayerManage, CDialog)
	//{{AFX_MSG_MAP(CMapLayerManage)
	ON_NOTIFY(NM_CLICK, IDC_TREE, OnClickTree)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapLayerManage message handlers

BOOL CMapLayerManage::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_ImageList.Create(16,16,ILC_COLOR16,4,4);
	//m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_LAYER));
	//m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_SHAPE));
	//m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_IMAGE));
	m_TreeCtrl.SetImageList(&m_ImageList,TVSIL_NORMAL);
	hRoot=m_TreeCtrl.InsertItem("Layers",0,0);
	m_TreeCtrl.SetItemState(hRoot,INDEXTOSTATEIMAGEMASK(2),TVIS_STATEIMAGEMASK);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CMapLayerManage::AddLayerInfo(CString str,int type)
{
	HTREEITEM hItem = m_TreeCtrl.InsertItem(str,type,type,hRoot);//�˴��õ���HTREEITEM�ᷢ���ı�
	m_TreeCtrl.SetItemState(hItem, INDEXTOSTATEIMAGEMASK(2), TVIS_STATEIMAGEMASK );
	hLayer.Add(hItem);
	m_TreeCtrl.Expand(hRoot, TVE_EXPAND);
	m_TreeCtrl.Invalidate(true);
}

void CMapLayerManage::RemoveAll()
{
	for(int i=0;i<hLayer.GetSize();++i)
	{
		HTREEITEM hItem=hLayer.GetAt(i);
		m_TreeCtrl.DeleteItem(hItem);
	}
	
	hLayer.RemoveAll();
}

void CMapLayerManage::RemoveAt(int index)
{
	HTREEITEM hItem=hLayer.GetAt(index);
	m_TreeCtrl.DeleteItem(hItem);
}

void CMapLayerManage::OnClickTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
	
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	
	CPoint pt;
	CRect rt;
	GetCursorPos(&pt);
	ScreenToClient(&pt);
	UINT flag;
	HTREEITEM hItem = m_TreeCtrl.HitTest(pt, &flag);
	if(!hItem) return;

	int i = 0;
	
	if (flag & TVHT_ONITEMSTATEICON)
	{
		if(hItem==hRoot)
		{
			UINT nState=m_TreeCtrl.GetItemState(hRoot,TVIS_STATEIMAGEMASK)>>12;
			if(nState==1)
			{
				for(i=0;i<hLayer.GetSize();++i)
				{
					//pView->m_Map.SetLayerVisible(i,true);

					if (g_mapHwnd != NULL)
					{
						::PostMessage(g_mapHwnd, WM_SET_LAYER_STATUS, (WPARAM)i, (LPARAM)1);
					}

					m_TreeCtrl.SetItemState(hLayer.GetAt(i),INDEXTOSTATEIMAGEMASK(2),TVIS_STATEIMAGEMASK);
				}
			}
			if(nState==2)
			{
				for(int i=0;i<hLayer.GetSize();++i)
				{
					if (g_mapHwnd != NULL)
					{
						::PostMessage(g_mapHwnd, WM_SET_LAYER_STATUS, (WPARAM)i, (LPARAM)0);
					}

					//pView->m_Map.SetLayerVisible(i,false);
					m_TreeCtrl.SetItemState(hLayer.GetAt(i),INDEXTOSTATEIMAGEMASK(1),TVIS_STATEIMAGEMASK);
				}
			}
		}
		else
		{
			int index=-1;
			for(i=0;i<hLayer.GetSize();++i)
			{
				if(hItem==hLayer.GetAt(i)) {index=i;break;}
			}
			
			UINT nState=m_TreeCtrl.GetItemState(hItem,TVIS_STATEIMAGEMASK)>>12;

			if(nState==1)
			{				
				if (g_mapHwnd != NULL)
				{
					::PostMessage(g_mapHwnd, WM_SET_LAYER_STATUS, (WPARAM)i, (LPARAM)1);
				}
					//pView->m_Map.SetLayerVisible(index,true);
				m_TreeCtrl.SetItemState(hItem,INDEXTOSTATEIMAGEMASK(1),TVIS_STATEIMAGEMASK);
			}
			if(nState==2)
			{
				//pView->m_Map.SetLayerVisible(index,false);

				if (g_mapHwnd != NULL)
				{
					::PostMessage(g_mapHwnd, WM_SET_LAYER_STATUS, (WPARAM)i, (LPARAM)0);
				}
	
				m_TreeCtrl.SetItemState(hItem,INDEXTOSTATEIMAGEMASK(2),TVIS_STATEIMAGEMASK);
			}
		}
	}
	
	*pResult = 0;
}