// Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "sxbmem.h"
#include "Dlg.h"

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

extern CSxbmemApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDataSourceDlg dialog


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


void CDataSourceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDataSourceDlg)
	DDX_Control(pDX, IDC_SOURCELIST, m_SourceList);
	DDX_Text(pDX, IDC_SELTEXT, m_SelText);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDataSourceDlg, CDialog)
	//{{AFX_MSG_MAP(CDataSourceDlg)
	ON_BN_CLICKED(IDC_Summary, OnSummary)
	ON_LBN_SELCHANGE(IDC_SOURCELIST, OnSelchangeSourcelist)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDataSourceDlg message handlers

void CDataSourceDlg::OnSummary() 
{
	// TODO: Add your control notification handler code here
	int sel = m_SourceList.GetCurSel();
	ASSERT(sel > -1);

	CAdapter *ad = (CAdapter *)m_SourceList.GetItemDataPtr(sel);
	CString txt;
	m_SourceList.GetText(sel, txt);
	ad->DoSummaryDlg(txt);
}

void CDataSourceDlg::OnSelchangeSourcelist() 
{
	// TODO: Add your control notification handler code here
	m_SelText = "Memory data will come from ";
	
	CString txt;
	int sel = m_SourceList.GetCurSel();
	ASSERT(sel > -1);
	m_SourceList.GetText(sel, txt);
	m_SelText += txt + '.';
	UpdateData(FALSE);
}

BOOL CDataSourceDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	// TODO: Add extra initialization here
	for (short i = 0; i < theApp.m_Adapters.GetSize(); i++) {
		int index = m_SourceList.AddString(theApp.m_Adapters[i]->GetName());
		m_SourceList.SetItemDataPtr(index, theApp.m_Adapters[i]);

		if (theApp.m_Adapters[i] == (LPVOID)theApp.m_CurDataSource)
			m_SourceList.SetCurSel(i);
		}

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

void CDataSourceDlg::OnOK() 
{
	// TODO: Add extra validation here
	int sel = m_SourceList.GetCurSel();
	ASSERT(sel > -1);
	CAdapter *newad = (CAdapter *)m_SourceList.GetItemDataPtr(sel);

	if (newad != theApp.m_CurDataSource) {
		theApp.m_CurDataSource = newad;
		CDialog::OnOK();
		}
	else
		CDialog::OnCancel();
}

/////////////////////////////////////////////////////////////////////////////
// CSummaryDlg dialog


CSummaryDlg::CSummaryDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSummaryDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSummaryDlg)
	m_FirmwareVersion = _T("");
	m_Comment = _T("");
	m_Title = _T("");
	//}}AFX_DATA_INIT
	m_ModDate[0] = _T("");
	m_Version[0] = _T("");
	m_ModDate[1] = _T("");
	m_Version[1] = _T("");
}


void CSummaryDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSummaryDlg)
	DDX_Control(pDX, IDC_Title2, m_TitleBox);
	DDX_Text(pDX, IDC_FirmwareVersion, m_FirmwareVersion);
	DDX_Text(pDX, IDC_Comment, m_Comment);
	DDX_Text(pDX, IDC_Title1, m_Title);
	//}}AFX_DATA_MAP
	DDX_Text(pDX, IDC_ModDate1, m_ModDate[0]);
	DDX_Text(pDX, IDC_Version1, m_Version[0]);
	DDX_Text(pDX, IDC_ModDate2, m_ModDate[1]);
	DDX_Text(pDX, IDC_Version2, m_Version[1]);
}


BEGIN_MESSAGE_MAP(CSummaryDlg, CDialog)
	//{{AFX_MSG_MAP(CSummaryDlg)
	ON_CBN_SELCHANGE(IDC_Title2, OnSelchangeTitle2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSummaryDlg message handlers

void CSummaryDlg::OnSelchangeTitle2() 
{
	// TODO: Add your control notification handler code here
	int index = CB_ERR;

	int sel = m_TitleBox.GetCurSel();
	if (sel > -1)
		index = (int)(m_TitleBox.GetItemData(sel));

	if (index > CB_ERR) {
		m_Version[1] = m_pDrivers->GetAt(index).m_DriverVersion;
		m_ModDate[1] = m_pDrivers->GetAt(index).m_DriverDate;
		UpdateData(FALSE);
		}
}

BOOL CSummaryDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	if (m_pDrivers->GetSize() > 0) {
		m_Title = m_pDrivers->GetAt(0).m_DriverName;
		m_Version[0] = m_pDrivers->GetAt(0).m_DriverVersion;
		m_ModDate[0] = m_pDrivers->GetAt(0).m_DriverDate;

		for (short i = 1; i < m_pDrivers->GetSize(); i++) {
			int index = m_TitleBox.AddString(m_pDrivers->GetAt(i).m_DriverName);
			
			m_TitleBox.SetItemData(index, i);
			}

		m_TitleBox.SetCurSel(0);
		OnSelchangeTitle2();
		}
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
