// StampList.cpp : implementation file
//

#include "stdafx.h"
#include "BemStream.h"
#include "ListStamp.h"
#include "Geom.h"
#include "StampList.h"
#include "StampDial.h"

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

/////////////////////////////////////////////////////////////////////////////
// CStampList dialog


CStampList::CStampList(CWnd* pParent /*=NULL*/)
	: CDialog(CStampList::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStampList)
	m_efpo = 0.0f;
	//}}AFX_DATA_INIT
	int i=0;
	while (i<Stamps.GetSize())
	{
		delete Stamps.GetAt(i++);
	}	                     
	Stamps.RemoveAll();
}

CStampList::~CStampList()
{
	int i=0;
	while (i<Stamps.GetSize())
	{
		delete Stamps.GetAt(i++);
	}	                     
	Stamps.RemoveAll();
}

void CStampList::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStampList)
	DDX_Text(pDX, IDC_EFPO, m_efpo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CStampList, CDialog)
	//{{AFX_MSG_MAP(CStampList)
	ON_BN_CLICKED(IDC_ADD_STAMP, OnAddStamp)
	ON_BN_CLICKED(IDC_DEL_STAMP, OnDelStamp)
	ON_BN_CLICKED(IDC_ED_STAMP, OnEdStamp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStampList message handlers

void CStampList::OnAddStamp() 
{
	CStampDial stdlg;
	stdlg.clr=RGB(0,0,0);
	if (stdlg.DoModal()==IDOK)
	{
		CStamp* st=AddStamp(stdlg.m_tim,stdlg.clr);
		m_list.AddStampItem(st);
	}
}

void CStampList::OnDelStamp() 
{
	int ic=m_list.GetCurSel();
	if (ic!=LB_ERR)
	{
		IsDel=1;
		m_list.DeleteString(ic);
		IsDel=0;
	}
}

CStamp* CStampList::GetStamp(int i)
{
	return (CStamp*)Stamps.GetAt(i);
}

CStamp* CStampList::AddStamp(float tm,COLORREF clr)
{
	CStamp* st=new CStamp(0.0f,0.0f,tm);
	st->clr=clr;
	Stamps.Add(st);
	return st;
}

CStamp* CStampList::AddStamp(CStamp* stp)
{
	CStamp* st=new CStamp(stp);
	Stamps.Add(st);
	return st;
}

BOOL CStampList::OnInitDialog() 
{
	int i;
	CDialog::OnInitDialog();
	m_list.SubclassDlgItem(IDC_LIST1,this);
	m_list.pList=this;
	IsDel=0;
	m_list.ResetContent();
	for (i=0;i<Stamps.GetSize();i++)
	{
		CStamp* st=GetStamp(i);
		m_list.AddStampItem(st);
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CStampList::OnEdStamp() 
{
	int ij=m_list.GetCurSel();
	int i,iv;
	if (ij!=LB_ERR)
	{
		iv=-1;
		for (i=0;i<Stamps.GetSize();i++)
		{
			CStamp* st=GetStamp(i);
			if (st->xi==ij) {iv=i;break;}
		}
		if (iv>=0)
		{
			CStamp* st=GetStamp(iv);
			CStampDial std;
			std.m_tim=st->time;std.clr=st->clr;
			if (std.DoModal()==IDOK)
			{
				st->time=std.m_tim;st->clr=std.clr;
				Invalidate();
			}
		}
	}
}
