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

#include "WControls\CTL_WCBx.hpp"

#include "Controls\CTL_CtlE.hpp"
#include "Others\CTL_Pub.hpp"

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

//ROMTEAM Selection (Cristian Stegaru 24/03/98)
#include "Others\CTL_EnLi.hpp"
#include "Data\CTL_DatE.hpp"
#include "Controls\CTL_CtlE.hpp"
//ENDROMTEAM Selection (Cristian Stegaru)

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

/////////////////////////////////////////////////////////////////////////////
// CTL_Editor_ComboBox

BEGIN_MESSAGE_MAP(CTL_Editor_ComboBox, CComboBox)
	//{{AFX_MSG_MAP(CTL_Editor_ComboBox)
	ON_CONTROL_REFLECT(CBN_SELCHANGE, OnSelchange)
	ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropdown)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//***************************************************************************
CTL_Editor_ComboBox::CTL_Editor_ComboBox(CTL_tdeComboBoxType _eType,
																				 long _lAdditionnalStyles,
																				 CTL_Editor_Control *_pclControl,
																				 CWnd *_pclParentWnd,
																				 long _lUserDefinedType /*= 0*/,
																				 long _lUserDefinedCode /*= 0*/)

										: CTL_BaseWindowsControl(_pclControl,
																						 _pclParentWnd,
																						 _lUserDefinedType,
																						 _lUserDefinedCode)
{
	m_pri_eType = _eType;

	//Creates control
	DWORD lStyles = WS_CHILD | CBS_DROPDOWNLIST | WS_VSCROLL;
	lStyles |= _lAdditionnalStyles;

	CRect crBidonRect(0,0,10,200);
	if ( Create(lStyles,
							crBidonRect,
							_pclParentWnd,
							CTL_fn_lGetNextAvailableIDForControl()) )
	{
		//Sets the font
		SetFont(_pclParentWnd->GetFont());
	}
	
	m_pri_p_td_fn_vSelChanged_CallBack = NULL;
	m_pri_p_td_fn_vDropDown_CallBack = NULL;

//ROMTEAM Selection (Cristian Stegaru 24/03/98)
	m_iOldSelection = CB_ERR;
	m_pfCanISelectThisItem = NULL;
//ENDROMTEAM Selection (Cristian Stegaru)

}

//***************************************************************************
CTL_Editor_ComboBox::~CTL_Editor_ComboBox()
{
}

/////////////////////////////////////////////////////////////////////////////
// CTL_Editor_ComboBox message handlers

//***************************************************************************
void CTL_Editor_ComboBox::m_pub_fn_vSetSelChanged_CallBack(CTL_tdp_fn_vComboBoxSelChanged _p_fn_vCallBack)
{
	ERROR_ASSERT( m_pri_p_td_fn_vSelChanged_CallBack == NULL );

	m_pri_p_td_fn_vSelChanged_CallBack = _p_fn_vCallBack;
}

//****************************************************************************
void CTL_Editor_ComboBox::m_pub_fn_vSetDropDown_CallBack(CTL_tdp_fn_vComboBoxSelChanged _p_fn_vCallBack)
{
	ERROR_ASSERT( m_pri_p_td_fn_vDropDown_CallBack == NULL );

	m_pri_p_td_fn_vDropDown_CallBack = _p_fn_vCallBack;
}

//***************************************************************************
void CTL_Editor_ComboBox::OnSelchange()
{
	short wIndex = GetCurSel();

//ROMTEAM Selection (Cristian Stegaru 24/03/98)
	if (m_pfCanISelectThisItem)
	{
		//get the data
		CTL_Editor_EnumElement *pNewSelectedElement = (CTL_Editor_EnumElement *)GetItemDataPtr (wIndex);
		ASSERT (pNewSelectedElement);

		CTL_Editor_EnumControl *pEnumCtrl = (CTL_Editor_EnumControl *)m_pub_fn_pclGetParentControl ();
		ASSERT (pEnumCtrl);
		CTL_Editor_EnumData *pEnumData = (CTL_Editor_EnumData *)pEnumCtrl->m_fn_pclGetEditedData ();
		ASSERT (pEnumData);
		if (!(*m_pfCanISelectThisItem)(pEnumData, pNewSelectedElement))
		{
			SetCurSel (m_iOldSelection);
			return;
		}
	}
//ENDROMTEAM Selection (Cristian Stegaru)
	if ( wIndex != LB_ERR )
		if ( m_pri_p_td_fn_vSelChanged_CallBack != NULL )
			m_pri_p_td_fn_vSelChanged_CallBack(this,
																				 m_pub_fn_pclGetParentControl(),
																				 m_pri_eType,
																				 m_pub_fn_lGetUserDefinedType(),
																				 m_pub_fn_lGetUserDefinedCode());
}

//***************************************************************************
void CTL_Editor_ComboBox::OnDropdown() 
{
//ROMTEAM Selection (Cristian Stegaru 24/03/98)
	m_iOldSelection = GetCurSel ();
//ENDROMTEAM Selection (Cristian Stegaru)
	int iSize, iMaxSize = 0;
	
	for (int iIndex = 0; iIndex < GetCount(); iIndex++)
	{
		iSize =  GetLBTextLen(iIndex);
		if (iSize > iMaxSize)
			iMaxSize = iSize;
	}

	SetDroppedWidth(iMaxSize*7);

	if ( m_pri_p_td_fn_vDropDown_CallBack != NULL )
		m_pri_p_td_fn_vDropDown_CallBack(this,
																		 m_pub_fn_pclGetParentControl(),
																		 m_pri_eType,
																		 m_pub_fn_lGetUserDefinedType(),
																		 m_pub_fn_lGetUserDefinedCode());
}

//**********************************************************************************
//For TUT registration
void CTL_Editor_ComboBox::m_pub_fn_vRegisterWindowsControl(CString _csAdditionnalInfo)
{
#ifndef CTL_WITH_NO_TUT
	m_pro_fn_vRegisterWindowsControl(_csAdditionnalInfo, TUT_e_ComboBox, "Combo Box", this);
#endif //CTL_WITH_NO_TUT
}
