/*****************************************************************************
* 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
*
*****************************************************************************/// ISAResources.cpp : implementation file
// CISAResources: implementation file

#include "stdafx.h"
#include "digifwrk.h"
#include "resource.h"
#include "ISAResources.h"
#include <stdio.h>

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

/////////////////////////////////////////////////////////////////////////////
// CISAResources property page

IMPLEMENT_DYNCREATE(CISAResources, CConfigWizPage)

CISAResources::CISAResources() 
{
	//{{AFX_DATA_INIT(CISAResources)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_bInit = FALSE;
	CConfigWizPage::Construct(IDD_ISA_RESOURCES);
}

CISAResources::CISAResources(CConfigObject const * const pOwner, int const nState)
{
	m_bInit = FALSE;
	CConfigWizPage::Construct(IDD_WIZ_ISA_RESOURCES,0,pOwner,nState);
}

CISAResources::~CISAResources()
{
}

void CISAResources::DoDataExchange(CDataExchange* pDX)
{
	CConfigWizPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CISAResources)
	DDX_Control(pDX, IDC_MEMADDRESS, m_comboMemBase);
	DDX_Control(pDX, IDC_IRQNUMBER, m_comboIRQ);
	DDX_Control(pDX, IDC_IOADDRESS, m_comboIOBase);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CISAResources, CConfigWizPage)
	//{{AFX_MSG_MAP(CISAResources)
	ON_BN_CLICKED(IDC_DETECT, OnDetect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CISAResources message handlers

// called by the owner of the dialog object. provide the 3 arrays holding IO,memory
// and Interrupt #'s along with current IO,MEM and IRQ choice.
void CISAResources::SetChoices(CStringArray& astrIO,DWORD dwIO,
							   CStringArray& astrMem,DWORD dwMem,
							   CStringArray& astrIrq,DWORD dwIrq)
{
	ASSERT(astrIO.GetUpperBound() > -1);
	ASSERT(astrMem.GetUpperBound() > -1);
	ASSERT(astrIrq.GetUpperBound() > -1);
	m_astrIO.Copy(astrIO);
	m_astrMem.Copy(astrMem);
	m_astrIrq.Copy(astrIrq);
	
	m_dwIO = dwIO;
	m_dwMem = dwMem;
	m_dwIrq = dwIrq;
}

// return currently selected choices for IO,Memory and IRQ
BOOL CISAResources::GetChoices(DWORD& dwIO,DWORD& dwMem, DWORD& dwIrq)
{
	if (m_bInit)
	{
		GetChoice(m_comboIOBase,m_dwIO);
		GetChoice(m_comboMemBase,m_dwMem);
		GetChoice(m_comboIRQ,m_dwIrq,FALSE);
	}	
	dwIO = m_dwIO;
	dwMem = m_dwMem;
	dwIrq = m_dwIrq;
	return TRUE;
}

BOOL CISAResources::OnInitDialog() 
{
	CConfigWizPage::OnInitDialog();
	BuildList(m_comboIOBase,m_astrIO,m_dwIO);
	BuildList(m_comboMemBase,m_astrMem,m_dwMem);
	BuildList(m_comboIRQ,m_astrIrq,m_dwIrq,FALSE);
	UpdateData(FALSE);
	m_bInit = TRUE;
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CISAResources::OnOK() 
{

	GetChoice(m_comboIOBase,m_dwIO);
	GetChoice(m_comboMemBase,m_dwMem);
	GetChoice(m_comboIRQ,m_dwIrq,FALSE);
	m_bInit = FALSE;
	CConfigWizPage::OnOK();
}

// populate a given combbox with the provided contents and select the combobox entry that matches
// the given selection
// bHexFormat is a boolean which defines whether the provided data is in Hex or Decimal
void CISAResources::BuildList(CComboBox& ctrl,CStringArray& list,DWORD dwSel,BOOL bHexFormat)
{
	INT_PTR nUpperBound = list.GetUpperBound();
	ASSERT(ctrl);
	ASSERT(nUpperBound > -1);
	ctrl.ResetContent();
	CString strchoice;
	strchoice.Format((bHexFormat)?"%x":"%d",dwSel);
	int sel = -1;

	for (int i=0;i<=nUpperBound;i++)
	{
		CString stritem(list[i]);
		ctrl.AddString(stritem);
		if (!stritem.CompareNoCase(strchoice))
			sel = i;
	}
	ASSERT(sel > -1);
	ctrl.SetCurSel(sel);
}

// return current choice 
void CISAResources::GetChoice(CComboBox& ctrl,DWORD& dwSel,BOOL bHexFormat)
{
	DWORD sel = (DWORD)ctrl.GetCurSel();
	ASSERT(sel != CB_ERR);
	CString strSel;
	ctrl.GetLBText((int)sel,strSel);
	if (!strSel.IsEmpty())
	{
		int result = sscanf((LPCTSTR)strSel,(bHexFormat)?"%x":"%d",&sel);
		if (result != 0 && result != EOF)
			dwSel = sel;
		
	}

}

void CISAResources::OnDetect() 
{
	// TODO: Add your control notification handler code here

	// Spawn off the Windows Diagnostics Program
	STARTUPINFO			si;
	PROCESS_INFORMATION	pi;
	TCHAR				szCmd[MAX_PATH];
		
	// [system directory drive]:\sig -> szSigDir
	
	GetSystemDirectory(szCmd, MAX_PATH * sizeof(*szCmd));
	lstrcat(szCmd, TEXT("\\winmsd.exe"));

	// Execute the command with a call to the CreateProcess API call.
	memset(&si,0,sizeof(STARTUPINFO));
	si.cb = sizeof(STARTUPINFO);
	si.wShowWindow = SW_SHOW;
	if (!CreateProcess(NULL,szCmd,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
	{
		
		// Error
		AfxMessageBox(IDS_CANNOT_OPEN_DIAGS);

	} 
	else
	{
        /* wait for completion. */
        WaitForSingleObject(pi.hProcess, INFINITE);

        /* close process and thread handles */
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
	}
			
}
