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


// PropAdvancedBRI.cpp : implementation file
//

#include "stdafx.h"
#include "PropAdvancedBRI.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPropAdvancedBRI property page

IMPLEMENT_DYNCREATE(CPropAdvancedBRI, CPropertyPage)

CPropAdvancedBRI::CPropAdvancedBRI() : CPropertyPage(CPropAdvancedBRI::IDD)
{
	//{{AFX_DATA_INIT(CPropAdvancedBRI)
	m_iAddressType = -1;
	m_fLoopback = FALSE;
	m_iNumberingPlan = -1;
	m_iSubaddressType = -1;
	m_nLoopbackMode = -1;
	//}}AFX_DATA_INIT

   try
   {
      m_AddressTypeMap.Init(SValueMap::MAP_DCHAN_ADDRTYPE);
      m_NumberingPlanMap.Init(SValueMap::MAP_DCHAN_NUMPLAN);
      m_SubaddressTypeMap.Init(SValueMap::MAP_DCHAN_SUBADDRTYPE);
   }
   catch (CAffirmException ae)
   {
   }
}

CPropAdvancedBRI::~CPropAdvancedBRI()
{
}

void CPropAdvancedBRI::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPropAdvancedBRI)
	DDX_Control(pDX, IDC_SubaddressType, m_ctrlSubaddressType);
	DDX_Control(pDX, IDC_NumberingPlan, m_ctrlNumberingPlan);
	DDX_Control(pDX, IDC_AddressType, m_ctrlAddressType);
	DDX_CBIndex(pDX, IDC_AddressType, m_iAddressType);
	DDX_Check(pDX, IDC_AdvancedLoopback, m_fLoopback);
	DDX_CBIndex(pDX, IDC_NumberingPlan, m_iNumberingPlan);
	DDX_CBIndex(pDX, IDC_SubaddressType, m_iSubaddressType);
	DDX_Radio(pDX, IDC_AdvancedTE, m_nLoopbackMode);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPropAdvancedBRI, CPropertyPage)
	//{{AFX_MSG_MAP(CPropAdvancedBRI)
	ON_BN_CLICKED(IDC_AdvancedLoopback, OnAdvancedLoopback)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropAdvancedBRI message handlers

enum
{
   LOOPBACK_TE = 0,
   LOOPBACK_NT,
};

BOOL CPropAdvancedBRI::SetDChanExtConfig(dchan_ext_config_t const * const pDChanExtConfig)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pDChanExtConfig != NULL);

      m_iAddressType = m_AddressTypeMap.ToIndex(pDChanExtConfig->address_type);
      m_iNumberingPlan = m_NumberingPlanMap.ToIndex(pDChanExtConfig->numbering_plan);
      m_iSubaddressType = m_SubaddressTypeMap.ToIndex(pDChanExtConfig->subaddress_type);

      m_fLoopback = (pDChanExtConfig->mode != DCHAN_NORMAL);
      m_nLoopbackMode = (pDChanExtConfig->mode == DCHAN_LOOPBACK_NT) ? LOOPBACK_NT : LOOPBACK_TE;

      if (m_hWnd != NULL) UpdateData(FALSE);

      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CPropAdvancedBRI::GetDChanExtConfig(dchan_ext_config_t * const pDChanExtConfig)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pDChanExtConfig != NULL);

      if (m_hWnd != NULL) UpdateData();

      pDChanExtConfig->address_type = m_AddressTypeMap.FromIndex(m_iAddressType);
      pDChanExtConfig->numbering_plan = m_NumberingPlanMap.FromIndex(m_iNumberingPlan);
      pDChanExtConfig->subaddress_type = m_SubaddressTypeMap.FromIndex(m_iSubaddressType);

      pDChanExtConfig->mode = !m_fLoopback ? DCHAN_NORMAL :
         ((m_nLoopbackMode == LOOPBACK_TE) ? DCHAN_LOOPBACK_TE : DCHAN_LOOPBACK_NT);

      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

void CPropAdvancedBRI::OnAdvancedLoopback() 
{
   try
   {
      UpdateData();

      CWnd * cwnd = GetDlgItem(IDC_AdvancedTE);
      AffirmThrow(cwnd != NULL);
      cwnd->EnableWindow(m_fLoopback);

      cwnd = GetDlgItem(IDC_AdvancedNT);
      AffirmThrow(cwnd != NULL);
      cwnd->EnableWindow(m_fLoopback);
   }
   catch (CAffirmException ae)
   {
   }
}

BOOL CPropAdvancedBRI::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();

   FillComboBox(m_ctrlAddressType, m_AddressTypeMap.astrDisplayNames, m_iAddressType);
   FillComboBox(m_ctrlNumberingPlan, m_NumberingPlanMap.astrDisplayNames, m_iNumberingPlan);
   FillComboBox(m_ctrlSubaddressType, m_SubaddressTypeMap.astrDisplayNames, m_iSubaddressType);

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