// OpenBoardPortDlg.cpp : implementation file
//

#include "stdafx.h"
#include "rasaudio.h"
#include "OpenBoardPortDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// COpenBoardPortDlg dialog


COpenBoardPortDlg::COpenBoardPortDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COpenBoardPortDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COpenBoardPortDlg)
	m_strBoard = _T("");
	//}}AFX_DATA_INIT

   m_nPort = -1;
	m_strPort = _T("");
}


void COpenBoardPortDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COpenBoardPortDlg)
	DDX_Control(pDX, IDC_OPEN_BOARD_COMBO, m_ctrlBoardCombo);
	DDX_Control(pDX, IDC_OPEN_PORT_LIST, m_ctrlPortList);
	DDX_Control(pDX, IDOK, m_ctrlOpenButton);
	DDX_CBString(pDX, IDC_OPEN_BOARD_COMBO, m_strBoard);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COpenBoardPortDlg, CDialog)
	//{{AFX_MSG_MAP(COpenBoardPortDlg)
	ON_CBN_SELCHANGE(IDC_OPEN_BOARD_COMBO, OnSelchangeBoardCombo)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_OPEN_PORT_LIST, OnItemchangedPortList)
	ON_NOTIFY(NM_DBLCLK, IDC_OPEN_PORT_LIST, OnDblclkPortList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COpenBoardPortDlg message handlers

void COpenBoardPortDlg::OnOK() 
{
	CDialog::OnOK();
}

BOOL COpenBoardPortDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

   m_ImageList.Create(IDB_PORT, 15, 0, RGB(255,255,255));
   m_ctrlPortList.SetImageList(&m_ImageList, LVSIL_SMALL);

   get_mgmt_board_no_response_t mgmtBoardNoRsp;
   if (MgmtCommand(0, GET_MGMT_BOARD_NO, NULL, 0, &mgmtBoardNoRsp, sizeof(mgmtBoardNoRsp)) == MGMT_OK)
   {
      TRACE("Number of boards == %d\n", mgmtBoardNoRsp.NumberOfBoards);
      for (DWORD i = 1; i <= mgmtBoardNoRsp.NumberOfBoards; i++)
      {
         TCHAR szBoardName[32];
         if (cfgGetNameFromBoardNo(i, szBoardName, sizeof(szBoardName)))
         {
            m_ctrlBoardCombo.AddString(szBoardName);

            if ((i == 1) && (m_strBoard.IsEmpty()))
            {
               m_strBoard = szBoardName;
            }
         }
      }
   }

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

void COpenBoardPortDlg::OnSelchangeBoardCombo() 
{
   UpdateData();
   m_Adapter.Attach(m_strBoard);

   m_ctrlPortList.DeleteAllItems();

   DWORD cPorts = m_Adapter.GetPortCount();
   ASSERT(cPorts > 0);

   DWORD cChannelsPerPort = (m_Adapter.GetTotalChannelCount() / cPorts);
   ASSERT(cChannelsPerPort > 0);

   int iItem = 0;

   for (DWORD iPort = 1; iPort <= cPorts ; iPort++)
   {
      for (DWORD iChan = 1; iChan <= cChannelsPerPort; iChan++)
      {
         TCHAR szName[128];
         if (cfgGetCommName(m_Adapter.GetNumber(), iPort, iChan, szName, sizeof(szName)))
         {
            m_ctrlPortList.InsertItem(iItem, szName);
            m_ctrlPortList.SetItemData(iItem, ((iPort * iChan) - 1));
            iItem++;
         }
      }
   }
}

void COpenBoardPortDlg::OnItemchangedPortList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

   if ((pNMListView != NULL) && (pNMListView->uNewState & LVIS_SELECTED))
   {
      TRACE("Index of selected item == %d\n", pNMListView->iItem);

      m_strPort = m_ctrlPortList.GetItemText(pNMListView->iItem, 0);
      TRACE("Selected item is %s\n", m_strPort);
      m_nPort = m_ctrlPortList.GetItemData(pNMListView->iItem);
      TRACE("Port #%d\n", m_nPort);

      m_ctrlOpenButton.EnableWindow(TRUE);
   }
	
	*pResult = 0;
}

void COpenBoardPortDlg::OnDblclkPortList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

   if (pNMListView != NULL)
   {
      UINT fHitTest = 0;
      if ((m_ctrlPortList.HitTest(pNMListView->ptAction, &fHitTest) >= 0) &&
          (fHitTest & (LVHT_ONITEMICON | LVHT_ONITEMLABEL)))
      {
         OnOK();
      }
   }

	*pResult = 0;
}
