// LayerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TCTb.h"
#include "LayerDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLayerDlg dialog

extern CTCTbApp theApp;

CLayerDlg::CLayerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLayerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLayerDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	for(int i=1;i<MAX_LAYERS;i++) LayStatus[i]=LS_UNCREATED;
	LayStatus[0]=LS_VISIBLE;
	LastLay=1;
	imlist=NULL;
}


void CLayerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLayerDlg)
	DDX_Control(pDX, IDC_LAYERLIST, LayList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLayerDlg, CDialog)
//{{AFX_MSG_MAP(CLayerDlg)
ON_WM_CLOSE()
ON_WM_LBUTTONDBLCLK()
ON_NOTIFY(NM_DBLCLK, IDC_LAYERLIST, OnDblclkLayerlist)
ON_BN_CLICKED(IDC_MAKEINVISIBLE, OnMakeinvisible)
ON_BN_CLICKED(IDC_MAKEVISIBLE, OnMakevisible)
ON_BN_CLICKED(IDC_SHOWALL, OnShowall)
ON_BN_CLICKED(IDC_SHOWHEIGHT, OnShowheight)
	ON_WM_RBUTTONDOWN()
	ON_NOTIFY(NM_RCLICK, IDC_LAYERLIST, OnRclickLayerlist)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLayerDlg message handlers

CLayerDlg::~CLayerDlg()
{
	if(imlist)
	{
	delete imlist;
	imlist=NULL;
	}
}

void CLayerDlg::OnClose() 
{
//	ShowWindow(SW_MINIMIZE);
	ShowWindow(SW_HIDE);

		//CDialog::OnClose();
}

void CLayerDlg::OnOK() 
{
	
	//CDialog::OnOK();
}

void CLayerDlg::OnCancel() 
{
	
	//CDialog::OnCancel();
}

void CLayerDlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	
	CDialog::OnLButtonDblClk(nFlags, point);
}

void CLayerDlg::UpdateLayers()
{
	int i;
	LVITEM itm;
	
	for(i=1;i<MAX_LAYERS;i++)	// CHeck that the Height Field and a layer is not visible at the same time
		if(LayStatus[i]==LS_VISIBLE) LayStatus[0]=LS_HIDDEN;
		
		for(i=0;i<MAX_LAYERS;i++)
		{
			//itm.iItem=i;
			//itm.iSubItem=0; 
			//LayList.GetItem(&itm);
			switch(LayStatus[i])
			{
			case LS_HIDDEN:
				LayList.SetItemText(i,1,"NO");
				itm.iImage=(i==0)?1:3;
				break;
				
			case LS_VISIBLE:
				LayList.SetItemText(i,1,"YES");
				itm.iImage=(i==0)?0:2;
				break;
				
			case LS_UNCREATED:
				break;
			}
			//LayList.SetItem(&itm);
		}
		//theApp.UpdateScreen();
		
}

void CLayerDlg::OnDblclkLayerlist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	POSITION p;
	int sel;
	p=LayList.GetFirstSelectedItemPosition();
	sel=LayList.GetNextSelectedItem(p);
	ToggleItem(sel);
	UpdateLayers();	
	*pResult = 0;
}

void CLayerDlg::Showall() 
{
	int i;
	for(i=1;i<MAX_LAYERS;i++)
	{
		if(LayStatus[i]!=LS_UNCREATED)
		{
			LayStatus[i]=LS_VISIBLE;
		}
	}
	UpdateLayers();	
}

void CLayerDlg::Showheight() 
{
	int i;
	for(i=1;i<MAX_LAYERS;i++)
	{		
		if(LayStatus[i]!=LS_UNCREATED)
		{
			LayStatus[i]=LS_HIDDEN;
		}
	}		
	LayStatus[0]=LS_VISIBLE;
	UpdateLayers();	
}

void CLayerDlg::OnMakeinvisible() 
{
	POSITION p;
	int sel;
	p=LayList.GetFirstSelectedItemPosition();
	while(p)
	{
		sel=LayList.GetNextSelectedItem(p);
		if(LayStatus[sel]!=LS_UNCREATED)
			LayStatus[sel]=LS_HIDDEN;
	}
	
	UpdateLayers();	
}

void CLayerDlg::OnMakevisible() 
{
	POSITION p;
	int sel;
	p=LayList.GetFirstSelectedItemPosition();
	while(p)
	{
		sel=LayList.GetNextSelectedItem(p);
		if(LayStatus[sel]!=LS_UNCREATED)
			LayStatus[sel]=LS_VISIBLE;
	}
	
	UpdateLayers();	
}

void CLayerDlg::ToggleItem(int sel)
{
	int i;
	
	switch(LayStatus[sel])
	{
	case LS_HIDDEN:
		LayStatus[sel]=LS_VISIBLE;
		break;
		
	case LS_VISIBLE:
		LayStatus[sel]=LS_HIDDEN;
		break;
		
	case LS_UNCREATED:
		break;
	}
	
	if(sel==0 && LayStatus[0]==LS_VISIBLE)
	{
		for(i=1;i<MAX_LAYERS;i++)
			if(LayStatus[i]!=LS_UNCREATED)
				LayStatus[i]=LS_HIDDEN;
	}
	
	
}


void CLayerDlg::OnShowall() 
{
	Showall();	
}

void CLayerDlg::OnShowheight() 
{
	Showheight();	
}

void CLayerDlg::AddLayer(CString &lname)
{
	LayList.InsertItem(LastLay,lname,0);
	LayStatus[LastLay]=LS_HIDDEN;
	LastLay++;
	UpdateLayers();
}

BOOL CLayerDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	if(!imlist)
	{
		imlist=new CImageList;
		
		imlist->Create(16,16,ILC_COLOR24|ILC_MASK,4,0);
		//		imlist->Add(ExtractIcon(m_hInstance,"shell32.dll",12));
		//		imlist->Add(ExtractIcon(m_hInstance,"shell32.dll",24));
		imlist->Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_HFICON))); 
		imlist->Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_HFICON2))); 
		imlist->Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_BMP))); 
		imlist->Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_BMP2))); 
	}
	
	LayList.InsertItem(0,"Height Map",0);
	LayList.SetItemText(0,1,"YES");
	LayList.SetImageList(imlist,LVSIL_SMALL);  
	LayList.InsertColumn(0,"Layer Name",LVCFMT_LEFT,100);
	LayList.InsertColumn(1,"Visible",LVCFMT_LEFT,100);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CLayerDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	
	CDialog::OnRButtonDown(nFlags, point);
}

void CLayerDlg::OnRclickLayerlist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here

CHFAttribDlg		dlg;

POSITION p;
int sel;
p=LayList.GetFirstSelectedItemPosition();
sel=LayList.GetNextSelectedItem(p);


if(sel==0) //Height field
{
	if(dlg.DoModal()==IDOK)
	{
	
	}
}	

  *pResult = 0;
}
