// DepthDiag.cpp : implementation file
//

#include "stdafx.h"
#include "imagegrinder.h"
#include "DepthDiag.h"
#include "imagegrinderDoc.h"


/////////////////////////////////////////////////////////////////////////////
// CDepthDiag dialog


CDepthDiag::CDepthDiag(CWnd* pParent /*=NULL*/)
	: CDialog(CDepthDiag::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDepthDiag)
	//}}AFX_DATA_INIT
}


void CDepthDiag::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDepthDiag)
	DDX_Control(pDX, IDC_DLIST, m_DList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDepthDiag, CDialog)
	//{{AFX_MSG_MAP(CDepthDiag)
	ON_LBN_SELCHANGE(IDC_DLIST, OnSelchangeDlist)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDepthDiag message handlers

int CDepthDiag::DoModal(CImageGrinderDoc *Doc,CPoint &pnt) 
{
	int i,depth;
	pDoc=Doc;
	depth=pDoc->FGrid->ShootRay((unsigned short)pnt.x,(unsigned short)pnt.y);
	SelCount=0;
	for(i=depth-1;i>0;i--)
	{
	if(!pDoc->IsLocked(pDoc->FGrid->GetCollectedNr(i)))
		SelectedFeature[SelCount++]=pDoc->FGrid->GetCollectedNr(i);
	}

	return CDialog::DoModal();
}

BOOL CDepthDiag::OnInitDialog() 
{
	CDialog::OnInitDialog();
	int itm;
	// TODO: Add extra initialization here
	for(int i=0;i<SelCount;i++)
	{
	itm=m_DList.AddString((char *)(*SelectedFeature[i]->feature->GetName()));
	m_DList.SetItemDataPtr(itm,(void *)SelectedFeature[i]);
	if(SelectedFeature[i]->StatFlags&FIF_SELECTED)
		m_DList.SetSel(itm,TRUE);	
	BackupSel[i]=*(SelectedFeature[i]);
	}

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

void CDepthDiag::OnOK() 
{

	CDialog::OnOK();
}

void CDepthDiag::OnSelchangeDlist() 
{
	// TODO: Add your control notification handler code here
	for(int i=0;i<m_DList.GetCount();i++)
	{
	if(m_DList.GetSel(i))
		pDoc->AddSelection((Compost::FeatureInstance *)m_DList.GetItemDataPtr(i));
	else
		pDoc->SubSelection((Compost::FeatureInstance *)m_DList.GetItemDataPtr(i));
	}

pDoc->UpdateAllViews(NULL);	
}

void CDepthDiag::OnCancel() 
{
//	pDoc->SelectionCount=BackupCount;
	for(int i=0;i<BackupCount;i++)
		*(SelectedFeature[i])=BackupSel[i];

	pDoc->UpdateAllViews(NULL);	
	
	CDialog::OnCancel();
}
