/*****************************************************************************
* Copyright (c) 1990-98 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 copyrighted work of Digi International
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
* prohibited.
* 
* Restricted Rights Legend
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7031 or
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -
* Restricted Rights at 48 CFR 52.227-19, as applicable.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
*
*****************************************************************************/

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

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);

/////////////////////////////////////////////////////////////////////////////
// CX25PVC property page

IMPLEMENT_DYNCREATE(CX25PVC, CPropertyPage)

CX25PVC::CX25PVC() : CPropertyPage(CX25PVC::IDD)
{
	//{{AFX_DATA_INIT(CX25PVC)
	m_szDescription = _T("");
	m_nChannel = 0;
	//}}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;

//	m_CurSelPVC = NULL;
}

CX25PVC::~CX25PVC()
{
}

void CX25PVC::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CX25PVC)
	DDX_Control(pDX, IDC_Channel, m_EditChannel);
	DDX_Text(pDX, IDC_Description, m_szDescription);
	DDV_MaxChars(pDX, m_szDescription, 63);
	DDX_Text(pDX, IDC_Channel, m_nChannel);
	DDV_MinMaxDWord(pDX, m_nChannel, 0, 4095);
	//}}AFX_DATA_MAP

	if (pDX->m_bSaveAndValidate) 
	{
		if (m_nChannel < m_nChannelStart || m_nChannel > m_nChannelEnd) 
		{
			CString err;

			pDX->PrepareEditCtrl(IDC_Channel);
			err.Format(IDS_BADPVCRange, m_nChannelStart, m_nChannelEnd);
			AfxMessageBox(err);
			pDX->Fail();
		}

		if (m_bConfig && !m_pPort->ValidateChannel(m_nChannel)) 
		{
			pDX->PrepareEditCtrl(IDC_Channel);
			AfxMessageBox(IDS_ChannelInUse);
			pDX->Fail();
		}
	}

#ifndef NT5
	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);
#endif

}

BEGIN_MESSAGE_MAP(CX25PVC, CPropertyPage)
	//{{AFX_MSG_MAP(CX25PVC)
	ON_EN_KILLFOCUS(IDC_Channel, OnKillfocusChannel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CX25PVC message handlers

void CX25PVC::PostNcDestroy() 
{
	CPropertyPage::PostNcDestroy();
}

BOOL CX25PVC::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 CX25PVC::OnOK() 
{

	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 CX25PVC::OnInitDialog() 
{

	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();
	m_EditChannel.EnableWindow(m_bConfig);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CX25PVC::OnKillfocusChannel() 
{
	
	// TODO: Add your control notification handler code here
	if (UpdateData(TRUE))
	{
		m_szDescription = m_szDescription.Left(1+m_szDescription.Find(_T(":")));
		CString sztmp;
		sztmp.Format(_T("Channel %d"),m_nChannel);
		m_szDescription += sztmp;
		GetDlgItem(IDC_Description)->SetWindowText(m_szDescription);
	}
	
}

/////////////////////////////////////////////////////////////////////////////
// CX25PVCAdvanced property page

IMPLEMENT_DYNCREATE(CX25PVCAdvanced, CPropertyPage)

CX25PVCAdvanced::CX25PVCAdvanced() : CPropertyPage(CX25PVCAdvanced::IDD)
{
	//{{AFX_DATA_INIT(CX25PVCAdvanced)
	m_AutoManage = FALSE;
	m_LocPacket = 0;
	m_LocWindow = 2;
	m_RemWindow = 2;
	m_RemPacket = 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("");
}

CX25PVCAdvanced::~CX25PVCAdvanced()
{
}

void CX25PVCAdvanced::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);

	//{{AFX_DATA_MAP(CX25PVCAdvanced)
	DDX_Check(pDX, IDC_AutoManage, m_AutoManage);
	DDX_CBIndex(pDX, IDC_LOCPKTSIZE, m_LocPacket);
	DDX_Text(pDX, IDC_LOCWSIZE, m_LocWindow);
	DDV_MinMaxDWord(pDX, m_LocWindow, 1, m_nMaxWsize);
	DDX_Text(pDX, IDC_REMWSIZE, m_RemWindow);
	DDV_MinMaxDWord(pDX, m_RemWindow, 1, m_nMaxWsize);
	DDX_CBIndex(pDX, IDC_REMPKTSIZE, m_RemPacket);
	//}}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);
}


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

/////////////////////////////////////////////////////////////////////////////
// CX25PVCAdvanced message handlers

BOOL CX25PVCAdvanced::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
}

BOOL CX25PVCAdvanced::OnInitDialog() 
{
	m_LocPacket -= 7;
	m_RemPacket -= 7;

	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();
	CButton *button = (CButton *)GetDlgItem(IDC_AutoManage);
	int check = button->GetCheck();

	for (CWnd *wnd = button->GetWindow(GW_HWNDNEXT); wnd; wnd = wnd->GetWindow(GW_HWNDNEXT))
		wnd->EnableWindow(!check);		
	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CX25PVCAdvanced::OnAutoManage() 
{
	CButton *button = (CButton *)GetDlgItem(IDC_AutoManage);

/*	if (button->GetCheck() == TRUE) {
		m_LocPacket = g_X25pvc.LOCDEFPKTSIZE - 7;
		m_RemPacket = g_X25pvc.REMDEFPKTSIZE - 7;
		m_LocWindow = g_X25pvc.LOCDEFWSIZE;
		m_RemWindow = g_X25pvc.REMDEFWSIZE;
		m_AutoManage = TRUE;
		UpdateData(FALSE);
		}	
*/
	int check = button->GetCheck();

	for (CWnd *wnd = button->GetWindow(GW_HWNDNEXT); wnd; wnd = wnd->GetWindow(GW_HWNDNEXT))
		wnd->EnableWindow(!check);		
	
}

void CX25PVCAdvanced::OnOK() 
{

	m_LocPacket += 7;
	m_RemPacket += 7;


	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();
}

