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


// WizPageRadioButtons.cpp : implementation file
//

#include "stdafx.h"
#include "Resource.h"
#include "WizPageRadioButtons.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWizPageRadioButtons property page

IMPLEMENT_DYNCREATE(CWizPageRadioButtons, CConfigWizPage)

CWizPageRadioButtons::CWizPageRadioButtons(CConfigObject const * const pOwner /* = NULL */, int const nState /* = -1 */, CString const & strTitle /* = TEXT("") */, CString const & strInstruction /* = TEXT("") */) : CConfigWizPage()
{
	//{{AFX_DATA_INIT(CWizPageRadioButtons)
	m_iButton = -1;
	m_strInstruction = TEXT("");
	//}}AFX_DATA_INIT

   m_astrValues.RemoveAll();
   m_iDefaultValue = -1;

   m_fFirstInit = TRUE;

   Construct(pOwner, nState, strTitle, strInstruction);
}

void CWizPageRadioButtons::Construct(CConfigObject const * const pOwner /* = NULL */, int const nState /* = -1 */, CString const & strTitle /* = TEXT("") */, CString const & strInstruction /* = TEXT("") */)
{
   CConfigWizPage::Construct(IDD_WizPageRadioButtons, 0, pOwner, nState);

   SetTitle(strTitle);
   m_strInstruction = strInstruction;
}

CWizPageRadioButtons::~CWizPageRadioButtons()
{
}

void CWizPageRadioButtons::DoDataExchange(CDataExchange* pDX)
{
	CConfigWizPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWizPageRadioButtons)
	DDX_Radio(pDX, IDC_WizPageRadioButtons_Button0, m_iButton);
	DDX_Text(pDX, IDC_WizPageRadioButtons_Instruction, m_strInstruction);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWizPageRadioButtons, CConfigWizPage)
	//{{AFX_MSG_MAP(CWizPageRadioButtons)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static int const g_anRadioButtons[] =
{
   IDC_WizPageRadioButtons_Button0,
   IDC_WizPageRadioButtons_Button1,
   IDC_WizPageRadioButtons_Button2,
   IDC_WizPageRadioButtons_Button3,
   IDC_WizPageRadioButtons_Button4,
};

BOOL CWizPageRadioButtons::SetValues(CStringArray const & astrValues, int const iDefaultValue /* = 0 */)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(astrValues.GetSize() > 0);
      AffirmThrow(astrValues.GetSize() <= ElementCount(g_anRadioButtons));
      AffirmThrow(iDefaultValue >= 0);
      AffirmThrow(iDefaultValue < astrValues.GetSize());

      m_astrValues.RemoveAll();
      m_astrValues.Copy(astrValues);
      m_iDefaultValue = iDefaultValue;

      RefreshValues(TRUE);

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

   return fSuccess;
}

int CWizPageRadioButtons::GetChoice(void)
{
   if (m_hWnd != NULL) UpdateData();
   return m_iButton;
}

void CWizPageRadioButtons::RefreshValues(BOOL const fUseDefault /* = FALSE */)
{
   try
   {
      AffirmThrow(m_astrValues.GetSize() > 0);
      AffirmThrow(m_astrValues.GetSize() <= ElementCount(g_anRadioButtons));
      AffirmThrow(m_iDefaultValue >= 0);
      AffirmThrow(m_iDefaultValue < m_astrValues.GetSize());

      if (fUseDefault) m_iButton = m_iDefaultValue;

      if (m_hWnd != NULL)
      {
         for (int i = 0; i < ElementCount(g_anRadioButtons); i++)
         {
            CWnd * pwndRadioButton = GetDlgItem(g_anRadioButtons[i]);
            AffirmThrow(pwndRadioButton != NULL);

            BOOL fShown = (i < m_astrValues.GetSize());
            CString strText = fShown ? m_astrValues[i] : TEXT("");

            pwndRadioButton->ShowWindow(fShown ? SW_SHOW : SW_HIDE);
            pwndRadioButton->SetWindowText(strText);
         }

         UpdateData(FALSE);
      }
   }
   catch (CAffirmException ae)
   {
   }

   return;
}

/////////////////////////////////////////////////////////////////////////////
// CWizPageRadioButtons message handlers

BOOL CWizPageRadioButtons::OnInitDialog() 
{
	CConfigWizPage::OnInitDialog();

   //	Because of the way our wizard pages work (ie. they get removed from the sheet when
   // the back button is pushed), we may actually wind up here any number of times.
   // So, ... remember that we have been here, and only RefreshValues() using defaults on
   // the first time!
   RefreshValues(m_fFirstInit);
   m_fFirstInit = FALSE;

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