// ListStamp.cpp : implementation file
//

#include "stdafx.h"
#include "BemStream.h"
#include "geom.h"
#include "ListStamp.h"
#include "StampList.h"

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

#define ITEM_HEIGHT   20
/////////////////////////////////////////////////////////////////////////////
// CListStamp

CListStamp::CListStamp()
{
}

CListStamp::~CListStamp()
{
}


BEGIN_MESSAGE_MAP(CListStamp, CListBox)
	//{{AFX_MSG_MAP(CListStamp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CListStamp message handlers

int CListStamp::CompareItem(LPCOMPAREITEMSTRUCT lpCIS) 
{
	// TODO: Add your code to determine the sorting order of the specified items
	// return -1 = item 1 sorts before item 2
	// return 0 = item 1 and item 2 sort the same
	// return 1 = item 1 sorts after item 2
	
	CStamp* tr1=(CStamp*) lpCIS->itemData1;
	CStamp* tr2=(CStamp*) lpCIS->itemData2;
	if (tr1->time>tr2->time) return 1;
	if (tr1->time<tr2->time) return -1;
	return 0;
}

void CListStamp::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
{
	CDC* pDC = CDC::FromHandle(lpDIS->hDC);
	CStamp* stp=(CStamp*) lpDIS->itemData;
	COLORREF cr = stp->clr; // RGB in item data
	stp->xi=lpDIS->itemID;
	RECT rc=lpDIS->rcItem;
	int ix=(rc.left+rc.right)/2;
	rc.left=ix;
	ix=lpDIS->rcItem.left+2;
	CString msg;
	msg.Format ("%6.3f",stp->time);
	pDC->TextOut (ix,rc.top,msg);

	if (lpDIS->itemAction & ODA_DRAWENTIRE)
	{
		// Paint the color item in the color requested
		CBrush br(cr);
		pDC->FillRect(&rc, &br);
	}

	if ((lpDIS->itemState & ODS_SELECTED) &&
		(lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
	{
		// item has been selected - hilite frame
		COLORREF crHilite = RGB(255-GetRValue(cr),
						255-GetGValue(cr), 255-GetBValue(cr));
		CBrush br(crHilite);
		pDC->FrameRect(&rc, &br);
	}

	if (!(lpDIS->itemState & ODS_SELECTED) &&
		(lpDIS->itemAction & ODA_SELECT))
	{
		// Item has been de-selected -- remove frame
		CBrush br(cr);
		pDC->FrameRect(&rc, &br);
	}
}

void CListStamp::MeasureItem(LPMEASUREITEMSTRUCT lpMIS) 
{
	lpMIS->itemHeight = ITEM_HEIGHT;
}

void CListStamp::AddStampItem(CStamp *st)
{
	AddString((LPCTSTR) st);
}

void CListStamp::DeleteItem(LPDELETEITEMSTRUCT lpDIS) 
{
	int i,ij;
	CStamp* stp=(CStamp*) lpDIS->itemData;
	if (!(pList->IsDel)) return;
	ij=-1;
	for (i=0;i<pList->Stamps.GetSize();i++)
	{
		CStamp* st=pList->GetStamp(i);
		if (st->time==stp->time) {ij=i;break;}
	}
	if (ij>=0)
	{
		pList->Stamps.RemoveAt(ij);
	}
	delete stp;
}
