// EDACDgME.cpp : implementation file
/////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "CTL_DgME.hpp"

#include "Controls\CTL_Ctl.hpp"
#include "Others\CTL_Mask.hpp"
#include "Data\CTL_DatM.hpp"
#include "Others\CTL_Pri.hpp"

//External Modules
#include "CTL_ErO.hpp"
#include "IncTUT.h"
//End of External Modules

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

#define EdActors_C_FirstIDForListBoxCheckButtons	1000

/////////////////////////////////////////////////////////////////////////////
// CTL_DialogMaskedDataEdition dialog

BEGIN_MESSAGE_MAP(CTL_DialogMaskedDataEdition, CDialog)
	//{{AFX_MSG_MAP(CTL_DialogMaskedDataEdition)
	ON_WM_DRAWITEM()
	ON_BN_CLICKED(IDC_BUTTON_ALL_MASKS, OnButtonAllMasks)
	ON_BN_CLICKED(IDC_BUTTON_NO_MASK, OnButtonNoMask)
	ON_BN_CLICKED(IDC_BUTTON_INVERT_MASKS, OnButtonInvertMasks)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//***************************************************************************
CTL_DialogMaskedDataEdition::CTL_DialogMaskedDataEdition(CTL_Editor_Control *_pclParentControl,
																																	 CWnd* _pParent /*= NULL*/)

																: CDialog(CTL_DialogMaskedDataEdition::IDD, _pParent)
{
	hOldInstance = AfxGetResourceHandle();
	AfxSetResourceHandle(CTL_g_hModule);

	//{{AFX_DATA_INIT(CTL_DialogMaskedDataEdition)
	//}}AFX_DATA_INIT

	m_pri_pclParentControl = _pclParentControl;
	m_csDataName = m_pri_pclParentControl->m_fn_pclGetEditedData()->m_pub_fn_csGetDataName();
	m_pri_pclMaskDescriptor = ((CTL_Editor_MaskedData *)m_pri_pclParentControl->m_fn_pclGetEditedData())->m_pub_fn_pclGetMaskdescriptor();
}

//***************************************************************************
CTL_DialogMaskedDataEdition::~CTL_DialogMaskedDataEdition()
{
	AfxSetResourceHandle(hOldInstance);
}

//***************************************************************************
void CTL_DialogMaskedDataEdition::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTL_DialogMaskedDataEdition)
	DDX_Text(pDX, IDC_STATIC_DATA_NAME, m_csDataName);
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// CTL_DialogMaskedDataEdition message handlers

//***************************************************************************
BOOL CTL_DialogMaskedDataEdition::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//Fills list box with masks
	CListBox *pclListBox = (CListBox *)GetDlgItem(IDC_LIST_MASKS);

	CTL_Editor_Mask *pclCurrentMask;
	short wIndex;
	POSITION pos = m_pri_pclMaskDescriptor->GetHeadPosition();
	while ( pos != NULL )
	{
		pclCurrentMask = m_pri_pclMaskDescriptor->GetNext(pos);

		struct m_pri_stMaskInListBox *p_stNewMaskForLB = new struct m_pri_stMaskInListBox;
		p_stNewMaskForLB->st_m_pclButton = NULL;
		p_stNewMaskForLB->st_m_Mask = pclCurrentMask;
		p_stNewMaskForLB->st_m_MaskIsOn = m_pri_fn_bDataHasThisMask(pclCurrentMask->m_pub_fn_ulGetMaskValue());

		wIndex = pclListBox->AddString(pclCurrentMask->m_pub_fn_csGetString());
		if ( wIndex != LB_ERR )
			pclListBox->SetItemDataPtr(wIndex, (void *)p_stNewMaskForLB);
	}

#ifndef CTL_WITH_NO_TUT
	//////////////
	//////////////
	//Registers for TUT Module
	//Registers in TUT
	if ( CTL_p_fn_vRegisterControl != NULL )
	{
		CTL_p_fn_vRegisterControl(GetDlgItem(IDC_LIST_MASKS)->m_hWnd, "CTL_MaskEditionDialog_ListBox", TUT_e_ListBox);
		CTL_p_fn_vRegisterControl(GetDlgItem(IDC_BUTTON_ALL_MASKS)->m_hWnd, "CTL_MaskEditionDialog_AllMasksButton", TUT_e_Button);
		CTL_p_fn_vRegisterControl(GetDlgItem(IDC_BUTTON_NO_MASK)->m_hWnd, "CTL_MaskEditionDialog_NoMaskButton", TUT_e_Button);
		CTL_p_fn_vRegisterControl(GetDlgItem(IDC_BUTTON_INVERT_MASKS)->m_hWnd, "CTL_MaskEditionDialog_InvertButton", TUT_e_Button);
		CTL_p_fn_vRegisterControl(GetDlgItem(IDOK)->m_hWnd, "CTL_MaskEditionDialog_OKButton", TUT_e_Button);
		CTL_p_fn_vRegisterControl(GetDlgItem(IDCANCEL)->m_hWnd, "CTL_MaskEditionDialog_CancelButton", TUT_e_Button);
	}

	//End of Registers for TUT Module
	//////////////
#endif //CTL_WITH_NO_TUT
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//***************************************************************************
void CTL_DialogMaskedDataEdition::OnDestroy() 
{
#ifndef CTL_WITH_NO_TUT
	//////////////
	//////////////
	//UnRegisters for TUT Module
	if ( CTL_p_fn_bUnregisterControl != NULL )
	{
		CTL_p_fn_bUnregisterControl(GetDlgItem(IDC_LIST_MASKS)->m_hWnd);
		CTL_p_fn_bUnregisterControl(GetDlgItem(IDC_BUTTON_ALL_MASKS)->m_hWnd);
		CTL_p_fn_bUnregisterControl(GetDlgItem(IDC_BUTTON_NO_MASK)->m_hWnd);
		CTL_p_fn_bUnregisterControl(GetDlgItem(IDC_BUTTON_INVERT_MASKS)->m_hWnd);
		CTL_p_fn_bUnregisterControl(GetDlgItem(IDOK)->m_hWnd);
		CTL_p_fn_bUnregisterControl(GetDlgItem(IDCANCEL)->m_hWnd);
	}
	
	//End of UnRegisters for TUT Module
	//////////////
#endif //CTL_WITH_NO_TUT

	CDialog::OnDestroy();
}

//***************************************************************************
void CTL_DialogMaskedDataEdition::OnOK() 
{
	//Reads the selected Masks and updates the Data
	CListBox *pclListBox = (CListBox *)GetDlgItem(IDC_LIST_MASKS);

	struct m_pri_stMaskInListBox *p_stCurrentNewMaskForLB;
	short wCurrentIndex = 0;
	for (wCurrentIndex; wCurrentIndex < pclListBox->GetCount(); wCurrentIndex ++ )
	{
		p_stCurrentNewMaskForLB = (struct m_pri_stMaskInListBox *)pclListBox->GetItemDataPtr(wCurrentIndex);

		//If Button has been created, gets its state
		if ( p_stCurrentNewMaskForLB->st_m_pclButton != NULL )
			p_stCurrentNewMaskForLB->st_m_MaskIsOn = p_stCurrentNewMaskForLB->st_m_pclButton->GetCheck();

		m_pri_fn_vSetDataWithMask(p_stCurrentNewMaskForLB->st_m_Mask->m_pub_fn_ulGetMaskValue(), p_stCurrentNewMaskForLB->st_m_MaskIsOn);

#ifndef CTL_WITH_NO_TUT
		//////////////
		//////////////
		//UnRegisters for TUT Module
		if ( (CTL_p_fn_bUnregisterControl != NULL) && (p_stCurrentNewMaskForLB->st_m_pclButton != NULL) )
			CTL_p_fn_bUnregisterControl(p_stCurrentNewMaskForLB->st_m_pclButton->m_hWnd);
		//End of UnRegisters for TUT Module
		//////////////
#endif //CTL_WITH_NO_TUT
		
		delete p_stCurrentNewMaskForLB;
	}

	m_pri_pclParentControl->m_fn_pclGetEditedData()->m_fn_vUpdateMotorData(CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
	
	CDialog::OnOK();
}

//***************************************************************************
void CTL_DialogMaskedDataEdition::OnCancel() 
{
	//Deletes buttons
	CListBox *pclListBox = (CListBox *)GetDlgItem(IDC_LIST_MASKS);

	struct m_pri_stMaskInListBox *p_stCurrentNewMaskForLB;
	short wCurrentIndex = 0;
	for (wCurrentIndex; wCurrentIndex < pclListBox->GetCount(); wCurrentIndex ++ )
	{
		p_stCurrentNewMaskForLB = (struct m_pri_stMaskInListBox *)pclListBox->GetItemDataPtr(wCurrentIndex);

#ifndef CTL_WITH_NO_TUT
		//////////////
		//////////////
		//UnRegisters for TUT Module
		if ( (CTL_p_fn_bUnregisterControl != NULL) && (p_stCurrentNewMaskForLB->st_m_pclButton != NULL) )
			CTL_p_fn_bUnregisterControl(p_stCurrentNewMaskForLB->st_m_pclButton->m_hWnd);
		//End of UnRegisters for TUT Module
		//////////////
#endif //CTL_WITH_NO_TUT

		delete p_stCurrentNewMaskForLB;
	}
	
	CDialog::OnCancel();
}

//***************************************************************************
void CTL_DialogMaskedDataEdition::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	ERROR_ASSERT( nIDCtl == IDC_LIST_MASKS );

	if ( nIDCtl == IDC_LIST_MASKS )
	{
		ERROR_ASSERT( lpDrawItemStruct->CtlType == ODT_LISTBOX );
		
		CDC *pclDC;
		pclDC = pclDC->FromHandle(lpDrawItemStruct->hDC);
		CRect crRect(lpDrawItemStruct->rcItem);

		CFont *pclFont = GetFont();
		pclDC->SelectObject(pclFont);

		struct m_pri_stMaskInListBox *pclElement = (struct m_pri_stMaskInListBox *)lpDrawItemStruct->itemData;
	
		if ( pclElement->st_m_pclButton == NULL )
		{
			//Creates the Check Button
			pclElement->st_m_pclButton = new CButton;

			CWnd *pclWnd;
			pclWnd = pclWnd->FromHandle(lpDrawItemStruct->hwndItem);
			
			pclElement->st_m_pclButton->Create(	pclElement->st_m_Mask->m_pub_fn_csGetString(),
																					WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
																					lpDrawItemStruct->rcItem,
																					pclWnd,
																					EdActors_C_FirstIDForListBoxCheckButtons + lpDrawItemStruct->itemID);
			pclElement->st_m_pclButton->SetFont(pclFont);

			//Initializes Button's state
			pclElement->st_m_pclButton->SetCheck( pclElement->st_m_MaskIsOn );

#ifndef CTL_WITH_NO_TUT
		//////////////
			//////////////
			//Registers for TUT Module
			if ( CTL_p_fn_vRegisterControl != NULL )
				CTL_p_fn_vRegisterControl(pclElement->st_m_pclButton->m_hWnd,
																		CString("CTL_MaskEditionDialog_CheckBox_") + pclElement->st_m_Mask->m_pub_fn_csGetString(), TUT_e_ListBox);

			//End of Registers for TUT Module
			//////////////
#endif //CTL_WITH_NO_TUT
		}
	}
	
	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

//***************************************************************************
void CTL_DialogMaskedDataEdition::OnButtonAllMasks() 
{
	CListBox *pclListBox = (CListBox *)GetDlgItem(IDC_LIST_MASKS);

	struct m_pri_stMaskInListBox *p_stCurrentNewMaskForLB;
	short wCurrentIndex = 0;
	for (wCurrentIndex; wCurrentIndex < pclListBox->GetCount(); wCurrentIndex ++ )
	{
		p_stCurrentNewMaskForLB = (struct m_pri_stMaskInListBox *)pclListBox->GetItemDataPtr(wCurrentIndex);

		p_stCurrentNewMaskForLB->st_m_MaskIsOn = TRUE;

		if ( p_stCurrentNewMaskForLB->st_m_pclButton != NULL )
			p_stCurrentNewMaskForLB->st_m_pclButton->SetCheck(p_stCurrentNewMaskForLB->st_m_MaskIsOn);
	}
}

//***************************************************************************
void CTL_DialogMaskedDataEdition::OnButtonNoMask() 
{
	CListBox *pclListBox = (CListBox *)GetDlgItem(IDC_LIST_MASKS);

	struct m_pri_stMaskInListBox *p_stCurrentNewMaskForLB;
	short wCurrentIndex = 0;
	for (wCurrentIndex; wCurrentIndex < pclListBox->GetCount(); wCurrentIndex ++ )
	{
		p_stCurrentNewMaskForLB = (struct m_pri_stMaskInListBox *)pclListBox->GetItemDataPtr(wCurrentIndex);

		p_stCurrentNewMaskForLB->st_m_MaskIsOn = FALSE;

		if ( p_stCurrentNewMaskForLB->st_m_pclButton != NULL )
			p_stCurrentNewMaskForLB->st_m_pclButton->SetCheck(p_stCurrentNewMaskForLB->st_m_MaskIsOn);
	}
}

//***************************************************************************
void CTL_DialogMaskedDataEdition::OnButtonInvertMasks() 
{
	CListBox *pclListBox = (CListBox *)GetDlgItem(IDC_LIST_MASKS);

	struct m_pri_stMaskInListBox *p_stCurrentNewMaskForLB;
	short wCurrentIndex = 0;
	for (wCurrentIndex; wCurrentIndex < pclListBox->GetCount(); wCurrentIndex ++ )
	{
		p_stCurrentNewMaskForLB = (struct m_pri_stMaskInListBox *)pclListBox->GetItemDataPtr(wCurrentIndex);

		p_stCurrentNewMaskForLB->st_m_MaskIsOn = !p_stCurrentNewMaskForLB->st_m_MaskIsOn;

		if ( p_stCurrentNewMaskForLB->st_m_pclButton != NULL )
			p_stCurrentNewMaskForLB->st_m_pclButton->SetCheck(p_stCurrentNewMaskForLB->st_m_MaskIsOn);
	}
}

//***************************************************************************
BOOL CTL_DialogMaskedDataEdition::m_pri_fn_bDataHasThisMask(unsigned long _ulMaskValue)
{
	//CPA2 Stegaru Cristian 98/06/26
	unsigned long ulCurrentDataValue = ((CTL_Editor_MaskedData *)m_pri_pclParentControl->m_fn_pclGetEditedData())->mfn_ulGetCurrentValue ();
	//End CPA2 Stegaru Cristian 98/06/26

	return ( (ulCurrentDataValue & _ulMaskValue) != 0 );
}

//***************************************************************************
void CTL_DialogMaskedDataEdition::m_pri_fn_vSetDataWithMask(unsigned long _ulMaskValue, BOOL _bMaskOn)
{
	//CPA2 Stegaru Cristian 98/06/26
	unsigned long ulCurrentDataValue = ((CTL_Editor_MaskedData *)m_pri_pclParentControl->m_fn_pclGetEditedData())->mfn_ulGetCurrentValue ();
	//End CPA2 Stegaru Cristian 98/06/26

	if ( _bMaskOn )
	{
		if ( !m_pri_fn_bDataHasThisMask(_ulMaskValue) )
			ulCurrentDataValue += _ulMaskValue;
	}
	else
	{
		if ( m_pri_fn_bDataHasThisMask(_ulMaskValue) )
			ulCurrentDataValue -= _ulMaskValue;
	}

	//CPA2 Stegaru Cristian 98/06/26
	((CTL_Editor_MaskedData *)m_pri_pclParentControl->m_fn_pclGetEditedData())->mfn_ulSetCurrentValue (ulCurrentDataValue);
	//End CPA2 Stegaru Cristian 98/06/26
	m_pri_pclParentControl->m_fn_pclGetEditedData()->m_fn_vUpdateMotorData(CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER, 0);
}
