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


// PropCSU.cpp : implementation file
//

#include "stdafx.h"
#include "PropCSU.h"
#include "Helpers.h"

#include "dxbtypes.h"
#include "mgmthlc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPropCSU property page

IMPLEMENT_DYNCREATE(CPropCSU, CPropertyPage)

CPropCSU::CPropCSU() : CPropertyPage(CPropCSU::IDD)
{
	//{{AFX_DATA_INIT(CPropCSU)
	m_nHaul = 0;
	//}}AFX_DATA_INIT

   m_astrFraming.RemoveAll();
   m_iFraming = -1;

   m_astrEncoding.RemoveAll();
   m_iEncoding = -1;

   m_fShowSpan = TRUE;

   m_dwSpan = 0;
   m_dwLBO = 0;
   m_dwEGL = 0;
   m_dwSHLen = 0;
}

CPropCSU::~CPropCSU()
{
}

void CPropCSU::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPropCSU)
	DDX_Control(pDX, IDC_CSUSHLen, m_ctrlSHLen);
	DDX_Control(pDX, IDC_CSULBO, m_ctrlLBO);
	DDX_Control(pDX, IDC_CSUFraming, m_ctrlFraming);
	DDX_Control(pDX, IDC_CSUEncoding, m_ctrlEncoding);
	DDX_Control(pDX, IDC_CSUEGL, m_ctrlEGL);
	DDX_Radio(pDX, IDC_CSULongHaul, m_nHaul);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPropCSU, CPropertyPage)
	//{{AFX_MSG_MAP(CPropCSU)
	ON_BN_CLICKED(IDC_CSULongHaul, OnCSULongHaul)
	ON_BN_CLICKED(IDC_CSUShortHaul, OnCSUShortHaul)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CPropCSU::SetFramingValues(CStringArray const & astrValues, int const iDefaultValue /* = 0 */)
{
   try
   {
      AffirmThrow(iDefaultValue >= 0);
      AffirmThrow(iDefaultValue < astrValues.GetSize());

      m_astrFraming.Copy(astrValues);
      m_iFraming = iDefaultValue;
   }
   catch (CAffirmException ae)
   {
   }
}

void CPropCSU::SetEncodingValues(CStringArray const & astrValues, int const iDefaultValue /* = 0 */)
{
   try
   {
      AffirmThrow(iDefaultValue >= 0);
      AffirmThrow(iDefaultValue < astrValues.GetSize());

      m_astrEncoding.Copy(astrValues);
      m_iEncoding = iDefaultValue;
   }
   catch (CAffirmException ae)
   {
   }
}

void CPropCSU::EnableControls(UINT const * const pnControlIds, int const cControlIds, BOOL const fEnable)
{
   try
   {
      AffirmThrow(pnControlIds != NULL);
      AffirmThrow(cControlIds > 0);

      for (int i = 0; i < cControlIds; i++)
      {
         CWnd * cwnd = GetDlgItem(pnControlIds[i]);
         AffirmThrow(cwnd != NULL);
         cwnd->EnableWindow(fEnable);
      }
   }
   catch (CAffirmException ae)
   {
   }
}

void CPropCSU::ShowControls(UINT const * const pnControlIds, int const cControlIds, BOOL const fShow)
{
   try
   {
      AffirmThrow(pnControlIds != NULL);
      AffirmThrow(cControlIds > 0);

      for (int i = 0; i < cControlIds; i++)
      {
         CWnd * cwnd = GetDlgItem(pnControlIds[i]);
         AffirmThrow(cwnd != NULL);
         cwnd->ShowWindow(fShow ? SW_SHOW : SW_HIDE);
      }
   }
   catch (CAffirmException ae)
   {
   }
}

/////////////////////////////////////////////////////////////////////////////
// CPropCSU message handlers

static UINT const g_anLongHaulIds[] = { IDC_CSULBOLabel, IDC_CSULBO, IDC_CSUEGLLabel, IDC_CSUEGL };
static UINT const g_anShortHaulIds[] = { IDC_CSUSHLenLabel, IDC_CSUSHLen };
static UINT const g_anAdditionalSpanIds[] = { IDC_CSULongHaul, IDC_CSULongHaulGroup, IDC_CSUShortHaul, IDC_CSUShortHaulGroup, IDC_CSUSpanGroup };

void CPropCSU::ShowSpanControls(BOOL const fShow)
{
	ShowControls(g_anLongHaulIds, ElementCount(g_anLongHaulIds), fShow);
	ShowControls(g_anShortHaulIds, ElementCount(g_anShortHaulIds), fShow);
	ShowControls(g_anAdditionalSpanIds, ElementCount(g_anAdditionalSpanIds), fShow);
}

void CPropCSU::OnCSULongHaul() 
{
   try
   {
      EnableControls(g_anLongHaulIds, ElementCount(g_anLongHaulIds), TRUE);
      EnableControls(g_anShortHaulIds, ElementCount(g_anShortHaulIds), FALSE);
   }
   catch (CAffirmException ae)
   {
   }
}

void CPropCSU::OnCSUShortHaul() 
{
   try
   {
      EnableControls(g_anLongHaulIds, ElementCount(g_anLongHaulIds), FALSE);
      EnableControls(g_anShortHaulIds, ElementCount(g_anShortHaulIds), TRUE);
   }
   catch (CAffirmException ae)
   {
   }
}

enum
{
   SPAN_LONG_HAUL = 0,
   SPAN_SHORT_HAUL,
};

static DWORD const g_adwLBOValues[] =
{
   LBO_00_0_DB,
   LBO_07_5_DB,
   LBO_15_0_DB,
   LBO_22_5_DB,
};

static DWORD const g_adwEGLValues[] =
{
   EGL_LOW,
   EGL_HIGH,
};

static DWORD const g_adwSHLenValues[] =
{
   SH_0_133,
   SH_133_266,
   SH_266_399,
   SH_399_533,
   SH_533_655,
};

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

   FillComboBox(m_ctrlFraming, m_astrFraming, m_iFraming);
   FillComboBox(m_ctrlEncoding, m_astrEncoding, m_iEncoding);
   m_nHaul = (m_dwSpan == LONG_HAUL) ? SPAN_LONG_HAUL : SPAN_SHORT_HAUL;
   SetCurSelFromValue(m_ctrlLBO, m_dwLBO, g_adwLBOValues, ElementCount(g_adwLBOValues));
   SetCurSelFromValue(m_ctrlEGL, m_dwEGL, g_adwEGLValues, ElementCount(g_adwEGLValues));
   SetCurSelFromValue(m_ctrlSHLen, m_dwSHLen, g_adwSHLenValues, ElementCount(g_adwSHLenValues));

   UpdateData(FALSE);

   if (m_nHaul == SPAN_LONG_HAUL) OnCSULongHaul();
   else OnCSUShortHaul();

	ShowSpanControls(m_fShowSpan);

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

void CPropCSU::OnOK() 
{
   UpdateData();

   m_iFraming = m_ctrlFraming.GetCurSel();
   m_iEncoding = m_ctrlEncoding.GetCurSel();
   m_dwSpan = (m_nHaul == SPAN_LONG_HAUL) ? LONG_HAUL : SHORT_HAUL;
   m_dwLBO = g_adwLBOValues[m_ctrlLBO.GetCurSel()];
   m_dwEGL = g_adwEGLValues[m_ctrlEGL.GetCurSel()];
   m_dwSHLen = g_adwSHLenValues[m_ctrlSHLen.GetCurSel()];

	CPropertyPage::OnOK();
}
