// PvcFRCfg.cpp : implementation file
//

#include "stdafx.h"
#include "PvcFRCfg.h"
#include "defs.h"
#include "Pvc.h"
#include <stdio.h>

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

static LPCTSTR tszSubnetFormat = _T("%d.%d.%d.%d");
static LPCTSTR tszNetworkAddressFormat = _T("%X-%X-%X-%X-%X-%X");

extern BOOL FieldPreTranslateMessage(CWnd* pWnd,MSG* pMsg);
/////////////////////////////////////////////////////////////////////////////
// CFRPVC property page

IMPLEMENT_DYNCREATE(CFRPVC, CPropertyPage)

CFRPVC::CFRPVC(int * const pnCIR) : CPropertyPage(CFRPVC::IDD)
{
   ASSERT(NULL != pnCIR);
   m_pnCIR = pnCIR;

	//{{AFX_DATA_INIT(CFRPVC)
	m_nCIR = 0;
	m_nDLCI = MIN_DLCI;
	m_bCompression = FALSE;
	//}}AFX_DATA_INIT
	m_bConfig = FALSE;
   m_fCancelling = FALSE;
}

CFRPVC::~CFRPVC()
{
}

void CFRPVC::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFRPVC)
	DDX_Control(pDX, IDC_DLCI, m_EditDLCI);
	DDX_Text(pDX, IDC_CIR, m_nCIR);
	DDV_MinMaxDWord(pDX, m_nCIR, MIN_CIR, MAX_CIR);
	DDX_Text(pDX, IDC_DLCI, m_nDLCI);
	DDV_MinMaxDWord(pDX, m_nDLCI, MIN_DLCI, MAX_DLCI);
	DDX_Check(pDX, IDC_FRFCompressionEnable, m_bCompression);
	//}}AFX_DATA_MAP
	if (pDX->m_bSaveAndValidate) 
	{
		if (m_bConfig)
		{
			// validate the DLCI value. 
			if (!m_pPort->ValidateDLCI(m_nDLCI))
			{
				CString msg;
				msg.Format(IDS_UNIQUEDLCIERROR, m_nDLCI);
				pDX->PrepareEditCtrl(IDC_DLCI);
				AfxMessageBox(msg);
				pDX->Fail();
			}
		}
		if (m_nCIR > (DWORD)m_nBaudRate) 
		{
			CString msg;

			pDX->PrepareEditCtrl(IDC_CIR);
			msg.Format(IDS_CIRLINESPEED, m_nBaudRate);
			AfxMessageBox(msg);
			pDX->Fail();
		}
		
	}

}


BEGIN_MESSAGE_MAP(CFRPVC, CPropertyPage)
	//{{AFX_MSG_MAP(CFRPVC)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFRPVC message handlers

void CFRPVC::OnOK() 
{
	CPropertyPage::OnOK();
}

BOOL CFRPVC::OnInitDialog() 
{
	*m_pnCIR = m_nCIR;
	CPropertyPage::OnInitDialog();
	// disable editing of DLCI if modifying an existing PVC
	m_EditDLCI.EnableWindow(m_bConfig);
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CFRPVC::OnKillActive() 
{
   // Don't do the DDX if we're being cancelled...
   if (m_fCancelling)
      return TRUE;

   BOOL bReturn = FALSE;
   if (TRUE == (bReturn = CPropertyPage::OnKillActive()))
      *m_pnCIR = m_nCIR;

   return bReturn;
}

void CFRPVC::OnCancel() 
{
   m_fCancelling = TRUE;
	CPropertyPage::OnCancel();
}

/////////////////////////////////////////////////////////////////////////////
// CFRProtocol property page

IMPLEMENT_DYNCREATE(CFRProtocol, CPropertyPage)

CFRProtocol::CFRProtocol() : CPropertyPage(CFRProtocol::IDD)
{
	//{{AFX_DATA_INIT(CFRProtocol)
	m_Ethernet = -1;
	m_IPX = -1;
	m_TCPIP = -1;
	//}}AFX_DATA_INIT
	m_IPAddress[0] = 0;
	m_IPAddress[1] = 0;
	m_IPAddress[2] = 0;
	m_IPAddress[3] = 0;
	m_SubnetMask[0] = 0;
	m_SubnetMask[1] = 0;
	m_SubnetMask[2] = 0;
	m_SubnetMask[3] = 0;
}

CFRProtocol::~CFRProtocol()
{
}

void CFRProtocol::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFRProtocol)
	DDX_Control(pDX, IDC_TCPIP, m_TCPIPBox);
	DDX_CBIndex(pDX, IDC_Ethernet, m_Ethernet);
	DDX_CBIndex(pDX, IDC_IPX, m_IPX);
	DDX_CBIndex(pDX, IDC_TCPIP, m_TCPIP);
	//}}AFX_DATA_MAP

#ifndef NT5
	if (m_TCPIP > 0) {
		DDX_Text(pDX, IDC_IPAddress, m_IPAddress[0]);
		DDV_MinMaxDWord(pDX, m_IPAddress[0], 0, 255);
		DDX_Text(pDX, IDC_IPAddress3, m_IPAddress[1]);
		DDV_MinMaxDWord(pDX, m_IPAddress[1], 0, 255);
		DDX_Text(pDX, IDC_IPAddress4, m_IPAddress[2]);
		DDV_MinMaxDWord(pDX, m_IPAddress[2], 0, 255);
		DDX_Text(pDX, IDC_IPAddress5, m_IPAddress[3]);
		DDV_MinMaxDWord(pDX, m_IPAddress[3], 0, 255);

		DDX_Text(pDX, IDC_SubnetMask, m_SubnetMask[0]);
		DDV_MinMaxDWord(pDX, m_SubnetMask[0], 0, 255);
		DDX_Text(pDX, IDC_SubnetMask3, m_SubnetMask[1]);
		DDV_MinMaxDWord(pDX, m_SubnetMask[1], 0, 255);
		DDX_Text(pDX, IDC_SubnetMask4, m_SubnetMask[2]);
		DDV_MinMaxDWord(pDX, m_SubnetMask[2], 0, 255);
		DDX_Text(pDX, IDC_SubnetMask5, m_SubnetMask[3]);
		DDV_MinMaxDWord(pDX, m_SubnetMask[3], 0, 255);
		}
	else {
		DDX_Text(pDX, IDC_IPAddress, m_IPAddress[0]);
		DDX_Text(pDX, IDC_IPAddress3, m_IPAddress[1]);
		DDX_Text(pDX, IDC_IPAddress4, m_IPAddress[2]);
		DDX_Text(pDX, IDC_IPAddress5, m_IPAddress[3]);
		DDX_Text(pDX, IDC_SubnetMask, m_SubnetMask[0]);
		DDX_Text(pDX, IDC_SubnetMask3, m_SubnetMask[1]);
		DDX_Text(pDX, IDC_SubnetMask4, m_SubnetMask[2]);
		DDX_Text(pDX, IDC_SubnetMask5, m_SubnetMask[3]);
		}
#endif

	if (pDX->m_bSaveAndValidate)
	{
		if (m_TCPIP == 0 && m_IPX == 0 && m_Ethernet == 0)
		{
			AfxMessageBox(IDS_MustUseProtocol);
			pDX->Fail();
		}
	}
}


BEGIN_MESSAGE_MAP(CFRProtocol, CPropertyPage)
	//{{AFX_MSG_MAP(CFRProtocol)
	ON_CBN_SELCHANGE(IDC_TCPIP, OnSelchangeTcpip)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFRProtocol message handlers

void CFRProtocol::OnSelchangeTcpip() 
{

#ifndef NT5
	for (int id =  IDC_IPAddress; id <= IDC_SubnetMask5; id++)
		GetDlgItem(id)->EnableWindow(m_TCPIPBox.GetCurSel() > 0);
#endif

}

BOOL CFRProtocol::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class

#ifdef NT5
	return CPropertyPage::PreTranslateMessage(pMsg);
#else
	return FieldPreTranslateMessage(this, pMsg) ? TRUE : CPropertyPage::PreTranslateMessage(pMsg);
#endif
}

void CFRProtocol::OnOK() 
{
	if (m_Ethernet > 0)
		++m_Ethernet;
	TCHAR temp[256];
	sprintf(temp, tszSubnetFormat, m_IPAddress[0],m_IPAddress[1],
								   m_IPAddress[2],m_IPAddress[3]);
	m_szIPAddress = temp;
	sprintf(temp, tszSubnetFormat,	m_SubnetMask[0],m_SubnetMask[1],
									m_SubnetMask[2],m_SubnetMask[3]);
	m_szSubnetMask = temp;

	CPropertyPage::OnOK();
}

BOOL CFRProtocol::OnInitDialog()
{
	ASSERT(m_TCPIP != -1 && m_IPX != -1 && m_Ethernet != -1);
	if (m_Ethernet > 0)
		--m_Ethernet;
	sscanf(m_szIPAddress, tszSubnetFormat, &m_IPAddress[0],&m_IPAddress[1],
								   &m_IPAddress[2],&m_IPAddress[3]);
	sscanf(m_szSubnetMask, tszSubnetFormat,	&m_SubnetMask[0],&m_SubnetMask[1],
									&m_SubnetMask[2],&m_SubnetMask[3]);
	CPropertyPage::OnInitDialog();
	OnSelchangeTcpip();
	return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CFRPVCAdvanced property page

IMPLEMENT_DYNCREATE(CFRPVCAdvanced, CPropertyPage)

CFRPVCAdvanced::CFRPVCAdvanced(int * const pnCIR) : CPropertyPage(CFRPVCAdvanced::IDD)
{
   ASSERT(NULL != pnCIR);
   m_pnCIR = pnCIR;

   //{{AFX_DATA_INIT(CFRPVCAdvanced)
	m_BE = 0;
	m_CMP = 1;
	m_AutoManage = FALSE;
	m_RRMax = 1;
	m_RRPeriod = 0;
	//}}AFX_DATA_INIT
	m_NetworkAddress[0] = _T("");
	m_NetworkAddress[1] = _T("");
	m_NetworkAddress[2] = _T("");
	m_NetworkAddress[3] = _T("");
	m_NetworkAddress[4] = _T("");
	m_NetworkAddress[5] = _T("");

   m_fCancelling = FALSE;
}

CFRPVCAdvanced::~CFRPVCAdvanced()
{
}

void CFRPVCAdvanced::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFRPVCAdvanced)
	DDX_Text(pDX, IDC_Be, m_BE);
	DDV_MinMaxDWord(pDX, m_BE, 0, 2048000);
	DDX_Text(pDX, IDC_CMP, m_CMP);
	DDV_MinMaxDWord(pDX, m_CMP, 1, 60);
	DDX_Check(pDX, IDC_AutoManage, m_AutoManage);
	DDX_Text(pDX, IDC_ModeOneRRMaximum, m_RRMax);
	DDV_MinMaxDWord(pDX, m_RRMax, 1, 255);
	DDX_Text(pDX, IDC_ModeOneRRPeriod, m_RRPeriod);
	DDV_MinMaxDWord(pDX, m_RRPeriod, 0, 255);
	//}}AFX_DATA_MAP
	DDX_Text(pDX, IDC_NetworkAddress, m_NetworkAddress[0]);
	DDV_MaxChars(pDX, m_NetworkAddress[0], 2);
	DDX_Text(pDX, IDC_NetworkAddress2, m_NetworkAddress[1]);
	DDV_MaxChars(pDX, m_NetworkAddress[1], 2);
	DDX_Text(pDX, IDC_NetworkAddress3, m_NetworkAddress[2]);
	DDV_MaxChars(pDX, m_NetworkAddress[2], 2);
	DDX_Text(pDX, IDC_NetworkAddress4, m_NetworkAddress[3]);
	DDV_MaxChars(pDX, m_NetworkAddress[3], 2);
	DDX_Text(pDX, IDC_NetworkAddress5, m_NetworkAddress[4]);
	DDV_MaxChars(pDX, m_NetworkAddress[4], 2);
	DDX_Text(pDX, IDC_NetworkAddress6, m_NetworkAddress[5]);
	DDV_MaxChars(pDX, m_NetworkAddress[5], 2);

	if (pDX->m_bSaveAndValidate) {


		if ((int)m_BE + *m_pnCIR > m_nBaudRate)	{
			CString msg;

			msg.Format(IDS_BEMAXCIR, *m_pnCIR, m_nBaudRate);
			if (AfxMessageBox(msg, MB_YESNO) == IDYES)
				m_BE = m_nBaudRate - *m_pnCIR;
			else {
				pDX->PrepareEditCtrl(IDC_Be);
				pDX->Fail();
				}
			}
		}

}


BEGIN_MESSAGE_MAP(CFRPVCAdvanced, CPropertyPage)
	//{{AFX_MSG_MAP(CFRPVCAdvanced)
	ON_BN_CLICKED(IDC_AutoManage, OnAutoManage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFRPVCAdvanced message handlers

BOOL CFRPVCAdvanced::PreTranslateMessage(MSG* pMsg) 
{
#ifdef NT5
	return CPropertyPage::PreTranslateMessage(pMsg);
#else
	return FieldPreTranslateMessage(this, pMsg) ? TRUE : CPropertyPage::PreTranslateMessage(pMsg);
#endif
}


void CFRPVCAdvanced::OnOK() 
{

	CString NetworkAddress;
	NetworkAddress = CString(_T('0'), 2 - m_NetworkAddress[0].GetLength()) + m_NetworkAddress[0];
	for (short i = 1; i < 6; i++)
		NetworkAddress += '-' + CString(_T('0'), 2 - m_NetworkAddress[i].GetLength()) + m_NetworkAddress[i];
	m_NetworkAddress[0] = NetworkAddress;
	CPropertyPage::OnOK();
}

BOOL CFRPVCAdvanced::OnInitDialog() 
{
	int part[6];
	sscanf(	m_NetworkAddress[0],
					tszNetworkAddressFormat,	
					&part[0],
					&part[1],
					&part[2],
					&part[3],
					&part[4],
					&part[5]);
	for (short i = 0; i < 6; i++)
		m_NetworkAddress[i].Format("%02X", part[i]);

	CPropertyPage::OnInitDialog();

	int check = ((CButton *)GetDlgItem(IDC_AutoManage))->GetCheck();
		
	(GetDlgItem(IDC_Be))->EnableWindow(!check);
	(GetDlgItem(IDC_CMP))->EnableWindow(!check);
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CFRPVCAdvanced::OnSetActive()
{
	BOOL bResult = CPropertyPage::OnSetActive();
	if (bResult)
	{
		if (m_AutoManage)
			m_BE = m_nBaudRate - *m_pnCIR;
		UpdateData(FALSE);
	}
	return (bResult);
	
}

BOOL CFRPVCAdvanced::OnKillActive() 
{
   if (m_fCancelling)
      return TRUE;

   return CPropertyPage::OnKillActive();
}


void CFRPVCAdvanced::OnAutoManage() 
{
	int check = ((CButton*)GetDlgItem(IDC_AutoManage))->GetCheck();	
	if (check == 1)
	{
		m_BE = m_nBaudRate - *m_pnCIR;
		m_AutoManage = TRUE;
		UpdateData(FALSE);
	}
	(GetDlgItem(IDC_Be))->EnableWindow(!check);
	(GetDlgItem(IDC_CMP))->EnableWindow(!check);

}

void CFRPVCAdvanced::OnCancel() 
{
   m_fCancelling = TRUE;
	CPropertyPage::OnCancel();
}
