// BlockLibDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DiagramEng.h"
#include "BlockLibDlg.h"
#include "DiagramEngDoc.h"		// rqd. since enum EBlockType defined in DiagramEngDoc.h used by CBlockLibDlg mem fns.
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBlockLibDlg dialog


CBlockLibDlg::CBlockLibDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBlockLibDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBlockLibDlg)
	m_bChkBoxConstBlock = FALSE;
	m_bChkBoxDerivativeBlock = FALSE;
	m_bChkBoxDivideBlock = FALSE;
	m_bChkBoxGainBlock = FALSE;
	m_bChkBoxIntegratorBlock = FALSE;
	m_bChkBoxLinearFnBlock = FALSE;
	m_bChkBoxOutputBlock = FALSE;
	m_bChkBoxSignalGenBlock = FALSE;
	m_bChkBoxSubsysBlock = FALSE;
	m_bChkBoxSubsysInBlock = FALSE;
	m_bChkBoxSubsysIn2Block = FALSE;
	m_bChkBoxSubsysOutBlock = FALSE;
	m_bChkBoxSubsysOut2Block = FALSE;
	m_bChkBoxSumBlock = FALSE;
	m_bChkBoxTransferFnBlock = FALSE;
	//}}AFX_DATA_INIT
}


void CBlockLibDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBlockLibDlg)
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_CONST, m_bChkBoxConstBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_DERIVATIVE, m_bChkBoxDerivativeBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_DIVIDE, m_bChkBoxDivideBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_GAIN, m_bChkBoxGainBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_INTEGRATOR, m_bChkBoxIntegratorBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_LINEARFN, m_bChkBoxLinearFnBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_OUTPUT, m_bChkBoxOutputBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_SIGNALGEN, m_bChkBoxSignalGenBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_SUBSYS, m_bChkBoxSubsysBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_SUBSYSIN, m_bChkBoxSubsysInBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_SUBSYSIN2, m_bChkBoxSubsysIn2Block);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_SUBSYSOUT, m_bChkBoxSubsysOutBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_SUBSYSOUT2, m_bChkBoxSubsysOut2Block);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_SUM, m_bChkBoxSumBlock);
	DDX_Check(pDX, ID_BLOCKLIBDLG_CB_TRANSFERFN, m_bChkBoxTransferFnBlock);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBlockLibDlg, CDialog)
	//{{AFX_MSG_MAP(CBlockLibDlg)
	ON_BN_CLICKED(ID_BLOCKLIBDLG_BTN_ADDALLBLOCKS, OnBlocklibdlgBtnAddallblocks)
	ON_BN_CLICKED(ID_BLOCKLIBDLG_BTN_UNCHECKALLBLOCKS, OnBlocklibdlgBtnUncheckallblocks)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBlockLibDlg message handlers

void CBlockLibDlg::OnCancel() 
{
	// TODO: Add extra cleanup here

	// DiagramEng (start)
	// Nothing to add, since the two fns. OnOK() and OnCancel() are built into the ancestor CDialog class
	// from which the CBlockLibDlg class is inherited, i.e.: CBlockLibDlg : public CDialog.
	// In the CDialog class, the msg. map already has the obj IDs of the OK and Cancel btns attached to the
	// OnOK and OnCancel fns.
	// NOTE: OnCancel() below.
	// DiagramEng (end)
	
	CDialog::OnCancel();
}

void CBlockLibDlg::OnBlocklibdlgBtnAddallblocks() 
{
	// TODO: Add your control notification handler code here

	// DiagramEng (start)
	CString	sMsg;					// string to be displayed
	UINT nType = MB_OK;				// style of msg. box
	UINT nIDhelp = 0;				// help context ID for the msg.
	EBlockType e_block_type;		// enum block type defined in DiagramEngDoc.cpp	
	CDiagramEngDoc *pDoc = NULL;	// declare pDoc to be a ptr to CDiagramEngDoc.
	
	// -- Display a msg. box 
	sMsg.Format("\n CBlockLibDlg::OnBlocklibdlgBtnAddallblocks()\n\n");
	//AfxMessageBox(sMsg, nType, nIDhelp);

	// -- Update the dlg. wnd. control vals. to the vars.
	UpdateData(TRUE);

	// -- Check to see if no check boxes were checked and inform the user: then return. (blocks listed in alpha order).
	if(!m_bChkBoxConstBlock && !m_bChkBoxDerivativeBlock && !m_bChkBoxDivideBlock && !m_bChkBoxGainBlock &&
	   !m_bChkBoxIntegratorBlock && !m_bChkBoxLinearFnBlock && !m_bChkBoxOutputBlock && !m_bChkBoxSignalGenBlock &&
	   !m_bChkBoxSubsysBlock && !m_bChkBoxSubsysInBlock && !m_bChkBoxSubsysOutBlock && !m_bChkBoxSumBlock && 
	   !m_bChkBoxTransferFnBlock)
	{
		sMsg.Format("\n CBlockLibDlg::OnBlocklibdlgBtnAddallblocks()\n No blocks selected.\n\n");
		AfxMessageBox(sMsg, nType, nIDhelp);
		return;	// return now since no blocks were checked.
	}

	// -- Get a ptr to document, i.e. pDoc. 
	// This can be done calling CDiagramEngView::GetDocument().
	// However, from within the Dlg class, in the absence of a View obj., a global fn is rqd that returns pDoc.
	// This global fn header is declared in DiagramEngDoc.h and defined in DiagramEngDoc.cpp
	
	pDoc = GetDocumentGlobalFn();

	// Invoke a general block creation fn passing an enum EBlockType var.
	if(m_bChkBoxConstBlock)			
	{
		e_block_type = eConstBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxDerivativeBlock)	
	{
		e_block_type = eDerivativeBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxDivideBlock)		
	{
		e_block_type = eDivideBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxGainBlock)			
	{
		e_block_type = eGainBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxIntegratorBlock)
	{
		e_block_type = eIntegratorBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxLinearFnBlock)
	{
		e_block_type = eLinearFnBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxOutputBlock)
	{
		e_block_type = eOutputBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxSignalGenBlock)
	{
		e_block_type = eSignalGenBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxSubsysBlock)
	{
		e_block_type = eSubsysBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxSubsysInBlock)
	{
		e_block_type = eSubsysInBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxSubsysOutBlock)
	{
		e_block_type = eSubsysOutBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxSumBlock)
	{
		e_block_type = eSumBlock;
		pDoc->ConstructBlock(e_block_type);
	}
	if(m_bChkBoxTransferFnBlock)
	{
		e_block_type = eTransferFnBlock;
		pDoc->ConstructBlock(e_block_type);
	}

	// DiagramEng (end)
}

void CBlockLibDlg::OnBlocklibdlgBtnUncheckallblocks() 
{
	// TODO: Add your control notification handler code here
	
	// DiagramEng (start)

	//AfxMessageBox("\n CBlockLibDlg::OnBlocklibdlgBtnUncheckallblocks()\n", MB_OK, 0);
	
	// Mark all the check boxes as unchecked
	m_bChkBoxConstBlock = FALSE;
	m_bChkBoxDerivativeBlock = FALSE;
	m_bChkBoxDivideBlock = FALSE;
	m_bChkBoxGainBlock = FALSE;
	m_bChkBoxIntegratorBlock = FALSE;
	m_bChkBoxLinearFnBlock = FALSE;
	m_bChkBoxOutputBlock = FALSE;
	m_bChkBoxSignalGenBlock = FALSE;
	m_bChkBoxSubsysBlock = FALSE;
	m_bChkBoxSubsysInBlock = FALSE;
	m_bChkBoxSubsysIn2Block = FALSE;
	m_bChkBoxSubsysOutBlock = FALSE;
	m_bChkBoxSubsysOut2Block = FALSE;
	m_bChkBoxSumBlock = FALSE;
	m_bChkBoxTransferFnBlock = FALSE;

	UpdateData(FALSE);	// FALSE => var vals updated to the dlg. wnd.

	// DiagramEng (end)
}

BOOL CBlockLibDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here

	// DiagramEng (start)
	
	// Disable the duplicate check boxes
	GetDlgItem(ID_BLOCKLIBDLG_CB_SUBSYSOUT2)->EnableWindow(FALSE);	// False disables the control
	GetDlgItem(ID_BLOCKLIBDLG_CB_SUBSYSIN2)->EnableWindow(FALSE);	// False disables the control

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