/*
*******************************************************************************
* Copyright (c) 1996-97 Digi International Inc., All Rights Reserved          *
*                                                                             *
* This  software  contains  proprietary  and  confidential   information   of *
* Digi International  Inc.   By  accepting  transfer  of this copy, Recipient *
* agrees to retain this software in  confidence,  to  prevent  disclosure  to *
* others,  and  to make no  use of this software other than that for which it *
* was delivered.  This is an unpublished copyright work  Digi   International *
* Inc.    Except   as   permitted   by  federal  law,  17 USC 117, copying is *
* strictly prohibited.                                                        *
*                                                                             *
* Digi  International  Inc.  CONFIDENTIAL  -   (Digi    International    Inc. *
* Confidential  when   combined   with   the   aggregated  modules  for  this *
* product) OBJECT CODE ONLY SOURCE MATERIALS                                  *
*                                                                             *
* (C) COPYRIGHT Digi International Inc. 1996-97                               *
*                                                                             *
* All Rights Reserved                                                         *
*                                                                             *
* US   Government   Users   Restricted   Rights   -   Use,   duplication   or *
* disclosure  restricted by GSA ADP Schedule Contract with Digi International *
* Inc.                                                                        *
*                                                                             *
*******************************************************************************

// FRVCStatView.cpp : implementation file
//

Revision History:

   $History: FRVCStatView.cpp $
 * 
 * *****************  Version 23  *****************
 * User: Mikez        Date: 7/11/97    Time: 5:02p
 * Branched in $/Components/Windows/NT/Sync/Build/Diag
 * 
 * *****************  Version 22  *****************
 * User: Rodgerb      Date: 2/20/97    Time: 11:43a
 * Updated in $/Components/Windows/NT/Sync/rev2diag
 * channels are sorted by channel number
 * 
 * *****************  Version 21  *****************
 * User: Rodgerb      Date: 1/27/97    Time: 1:18p
 * Updated in $/Components/Windows/NT/Sync/rev2diag
 * 1) documentation
 * 2) added some new params to Physical layer stats
*/

#include "stdafx.h"
#include "Diagnostic.h"

#include "DataSource.h"
#include "FRVCStatView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFRVCStatView

IMPLEMENT_DYNCREATE(CFRVCStatView, CTracePage)

CFRVCStatView::CFRVCStatView() : CTracePage(CFRVCStatView::IDD)
{
}

CFRVCStatView::~CFRVCStatView()
{
}


void CFRVCStatView::DoDataExchange(CDataExchange* pDX)
{
	CTracePage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLAPBLineView)
	DDX_Control(pDX, IDC_VCStatList, m_ListCtrl);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFRVCStatView, CTracePage)
	//{{AFX_MSG_MAP(CFRVCStatView)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFRVCStatView message handlers

inline int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM)
{
	return (int)(lParam1 - lParam2);
}

BOOL CFRVCStatView::OnInitDialog() 
{
	CTracePage::OnInitDialog();
	
	// TODO: Add extra initialization here
	LPCSTR str[8] = { "DLCI", "Frames tx", "Frames rx",
										"Bytes tx", "Bytes rx",
										"DE", "BECN", "FECN"
									};
	int size[8] = { 45, 70, 70, 70, 70, 50, 50, 50 };
	
	for (short i = 0; i < 8; i++)
		m_ListCtrl.InsertColumn(i, str[i], LVCFMT_LEFT, size[i], i);

	const CFRTraceData *td = (CFRTraceData *)GetTraceData();

	if (td != NULL)
	{
		POSITION pos = td->m_VCStats.GetStartPosition();
		int index = 0;

		while (pos)
		{
			DWORD ch;
			sFRVCStats *vc;

			td->m_VCStats.GetNextAssoc(pos, ch, vc);
			m_ListCtrl.SetItemData(m_ListCtrl.InsertItem(index++, LPSTR_TEXTCALLBACK), ch);
		}

		m_ListCtrl.SortItems(CompareFunc, NULL);
	}

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

void CFRVCStatView::OnUpdate(WPARAM lHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (lHint == uavTraceFasterPeriod) {
		const CFRTraceData *td = (const CFRTraceData *)GetTraceData();

		CDC *pDC = m_ListCtrl.GetDC();
		CFont *font = pDC->SelectObject(GetFont());

		int limit = __min(m_ListCtrl.GetItemCount(), 
											m_ListCtrl.GetTopIndex() + m_ListCtrl.GetCountPerPage());

		for (short i = m_ListCtrl.GetTopIndex(); i < limit; i++) {
			DWORD_PTR channel = m_ListCtrl.GetItemData(i);
			sFRVCStats *vc = NULL;
			
			td->m_VCStats.Lookup((DWORD)channel, vc);
			if (vc) {
				DRAWITEMSTRUCT DrawItemStruct = { ODT_LISTVIEW,
																					0,
																					0,
																					ODA_DRAWENTIRE,
																					0,
																					NULL,
																					*pDC };
	
				RECT rect;
				m_ListCtrl.GetItemRect(i, &rect, LVIR_BOUNDS);
				DrawItemStruct.rcItem = rect;

				DrawItemStruct.itemData = channel;

				m_ListCtrl.DrawItem(&DrawItemStruct);
				}
			}

		pDC->SelectObject(font);
		ReleaseDC(pDC);
		}
}

/////////////////////////////////////////////////////////////////////////////
// CFRVCListCtrl

CFRVCListCtrl::CFRVCListCtrl()
{
}

CFRVCListCtrl::~CFRVCListCtrl()
{
}


BEGIN_MESSAGE_MAP(CFRVCListCtrl, CListCtrl)
	//{{AFX_MSG_MAP(CFRVCListCtrl)
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFRVCListCtrl message handlers

extern int DrawText(CDC *, LPCSTR, CRect);
extern LPSTR NumSlashNum(char *, DWORD, DWORD);
extern LPSTR NumInBytes(char *buf, DWORD num);

void CFRVCListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CTracePage *pPage = static_cast<CTracePage *>(GetParent());
	const CFRTraceData *td = (const CFRTraceData *)pPage->GetTraceData();
	CDC *dc = CDC::FromHandle(lpDrawItemStruct->hDC);
	CRect rect = CRect(lpDrawItemStruct->rcItem);
	DWORD_PTR ch = lpDrawItemStruct->itemData;
	sFRVCStats *vc = NULL;

	if (td == NULL || !td->m_VCStats.Lookup((DWORD)ch, vc))
		return;
	
	CStringArray Text;
	char buf[64];

	wsprintf(buf, "%8d", ch);
	Text.Add(buf);

	wsprintf(buf, "%d", vc->Frames[0]);
	Text.Add(buf);
	wsprintf(buf, "%d", vc->Frames[1]);
	Text.Add(buf);

	Text.Add(NumInBytes(buf, vc->Bytes[0]));
	Text.Add(NumInBytes(buf, vc->Bytes[1]));

	Text.Add(NumSlashNum(buf, vc->DE[0], vc->DE[1]));
	Text.Add(NumSlashNum(buf, vc->BECN[0], vc->BECN[1]));
	Text.Add(NumSlashNum(buf, vc->FECN[0], vc->FECN[1]));

	int LastWidth = 0;
	for (short i = 0; i < Text.GetSize(); i++) {
		rect.left += LastWidth;
		rect.right = rect.left + GetColumnWidth(i);
		LastWidth = rect.Width();
		
		DrawText(dc, Text[i], rect);
		}
}

void CFRVCListCtrl::OnRclick(NMHDR*, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	POINT pt;

	GetCursorPos(&pt);
	ScreenToClient(&pt);

	GetParent()->SendMessage(WM_RBUTTONUP, MK_RBUTTON, (pt.y << 16) + pt.x);
	*pResult = 0;
}
