/*
*******************************************************************************
* 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.                                                                        *
*                                                                             *
*******************************************************************************

// SessionStatView.cpp : implementation file
//

Revision History:

   $History: SessionStatView.cpp $
 * 
 * *****************  Version 21  *****************
 * User: Mikez        Date: 7/11/97    Time: 5:03p
 * Branched in $/Components/Windows/NT/Sync/Build/Diag
 * 
 * *****************  Version 20  *****************
 * User: Rodgerb      Date: 1/27/97    Time: 1:19p
 * 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 "SessionStatView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSessionStatView property page

IMPLEMENT_DYNCREATE(CSessionStatView, CTracePage)

CSessionStatView::CSessionStatView() : CTracePage(CSessionStatView::IDD)
{
	//{{AFX_DATA_INIT(CSessionStatView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CSessionStatView::~CSessionStatView()
{
}

void CSessionStatView::DoDataExchange(CDataExchange* pDX)
{
	CTracePage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSessionStatView)
	DDX_Control(pDX, IDC_SessionList, m_ListCtrl);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CSessionStatView message handlers
BOOL CSessionStatView::OnInitDialog() 
{
	CTracePage::OnInitDialog();
	
	// TODO: Add your specialized code here and/or call the base class
	LPCSTR str[4] = { GetTraceData()->GetEndPointName(), "Frames tx",
										"Frames rx", "Remote ID" };
	int size[4] = { 55, 70, 70, 124 };
	
	for (short i = 0; i < 4; i++)
		m_ListCtrl.InsertColumn(i, str[i], LVCFMT_LEFT, size[i], i);

	return TRUE;
}

void CSessionStatView::OnUpdate(WPARAM lHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (lHint == uavTraceFasterPeriod) {
		const CTraceData *td = GetTraceData();
		if (td) {
			int index = m_ListCtrl.GetItemCount(), i = index;
			if (index < td->m_Sessions.GetSize())
				for (i = index; i < td->m_Sessions.GetSize(); i++)
					m_ListCtrl.SetItemData(m_ListCtrl.InsertItem(i, LPSTR_TEXTCALLBACK), i);
			else if (index > td->m_Sessions.GetSize()) {
				m_ListCtrl.DeleteAllItems();
				return;
				}
			
			CDC *pDC = m_ListCtrl.GetDC();
			CFont *font = pDC->SelectObject(GetFont());

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

			for (i = m_ListCtrl.GetTopIndex(); i < limit; i++) {
				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 = i;

				m_ListCtrl.DrawItem(&DrawItemStruct);
				}

			pDC->SelectObject(font);
			ReleaseDC(pDC);
			}
		}
	if (lHint == uavClearStatistics && IsWindow(m_ListCtrl.GetSafeHwnd()))
		m_ListCtrl.DeleteAllItems();
}

/////////////////////////////////////////////////////////////////////////////
// CSessionListCtrl

CSessionListCtrl::CSessionListCtrl()
{
}

CSessionListCtrl::~CSessionListCtrl()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// CSessionListCtrl message handlers

extern int DrawText(CDC *, LPCSTR, CRect);

void CSessionListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CTracePage *pPage = static_cast<CTracePage *>(GetParent());
	CDC *dc = CDC::FromHandle(lpDrawItemStruct->hDC);
	CRect rect = CRect(lpDrawItemStruct->rcItem);
	const CTraceData *td = pPage->GetTraceData();
	ULONG_PTR index = lpDrawItemStruct->itemData;
	
	if (td == NULL || (int)index > td->m_Sessions.GetUpperBound() || (int)index < 0)
		return;

	sSession *session = td->m_Sessions[index];
	CStringArray Text;
	char buf[64];

	wsprintf(buf, "%10d", session->EndPoint);
	Text.Add(buf);

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

	Text.Add(session->Remote);

	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 CSessionListCtrl::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;
}
