// EdIRBLB.cpp : implementation file
//

#include "stdafx.h"

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRBLB.hpp"

#include "EdIRBVw.hpp"
#include "EdIRBtVw.hpp"
#include "EdIRFrm.hpp"
#include "EdIRBeha.hpp"
#include "EdIRIRD.hpp"
#include "EdIRBeEn.hpp"

#include "incTUT.h"

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

/////////////////////////////////////////////////////////////////////////////
// class CBehaviourListBox
/////////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CPA_EdIR_BehaviourListBox, CListBox)
	//{{AFX_MSG_MAP(CPA_EdIR_BehaviourListBox)
	ON_WM_KEYUP()
	ON_WM_RBUTTONUP()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_IR_BEHAVIOURLIST_DELETE_BEHAVIOUR,m_fn_vOnDeleteBehaviour)
	ON_COMMAND(ID_IR_BEHAVIOURLIST_RENAME_BEHAVIOUR,m_fn_vOnRenameBehaviour)
	ON_COMMAND(ID_IR_BEHAVIOURLIST_ADD_BEHAVIOUR,m_fn_vOnAddBehaviour)
	ON_COMMAND(ID_IR_BEHAVIOURLIST_DEL_ALL_BEHAVIOUR,m_fn_vOnDeleteAllBehaviours)
END_MESSAGE_MAP()

/**********************************************************************************/
CPA_EdIR_BehaviourListBox::CPA_EdIR_BehaviourListBox()
{
}

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

/////////////////////////////////////////////////////////////////////////////
// CBehaviourListBox message handlers

/**********************************************************************************/
void CPA_EdIR_BehaviourListBox::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	((CPA_EdIR_DiagView *)GetParent())->OnKeyDown(nChar, nRepCnt, nFlags);
	
	CListBox::OnKeyDown(nChar, nRepCnt, nFlags);
}

/**********************************************************************************/
void CPA_EdIR_BehaviourListBox::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	((CPA_EdIR_DiagView *)GetParent())->OnKeyUp(nChar, nRepCnt, nFlags);

	CListBox::OnKeyUp(nChar, nRepCnt, nFlags);
}

/**********************************************************************************/
void CPA_EdIR_BehaviourListBox::OnRButtonUp(UINT nFlags, CPoint point) 
{
	CPA_EdIR_ButtonView	*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
	if(p_clBtView->m_fn_tdeGetEditState()!=EditDecl)
	{
		CPA_EdIR_Behaviour	*pclBehaviour=NULL;

		//Selects behaviour under the mouse
		if(GetCount()!=0)
		{
			BOOL	bOutside;
			long	lSelectedIndex=ItemFromPoint(point,bOutside);
			SetCurSel(lSelectedIndex);

			pclBehaviour=(CPA_EdIR_Behaviour *)GetItemDataPtr(lSelectedIndex);
		}

		//Displays context menu
		CMenu	Menu;
		Menu.LoadMenu(IDR_IR_MENU_POPUP_DIAGVIEW);
		
		CMenu	*SousMenu=Menu.GetSubMenu(2);

		if(pclBehaviour==NULL || !pclBehaviour->m_clListOfDependentBehaviours.IsEmpty())
		{
			SousMenu->EnableMenuItem(ID_IR_BEHAVIOURLIST_RENAME_BEHAVIOUR,MF_GRAYED|MF_BYCOMMAND);
			SousMenu->EnableMenuItem(ID_IR_BEHAVIOURLIST_DELETE_BEHAVIOUR,MF_GRAYED|MF_BYCOMMAND);
			SousMenu->EnableMenuItem(ID_IR_BEHAVIOURLIST_DEL_ALL_BEHAVIOUR,MF_GRAYED|MF_BYCOMMAND);
		}

		CRect	cr;
		GetWindowRect(cr);

		//////////////
		//////////////
		//Registers for TUT Module
		TUT_M_vGetTutDll();
		
		TUT_M_vRegisterMenu(m_hWnd,SousMenu->m_hMenu,cr.left+point.x,cr.top+point.y);
		//End of Register for TUT Module
		//////////////

		SousMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,cr.left+point.x,cr.top+point.y,this);
		SousMenu->DestroyMenu();
		Menu.DestroyMenu();
	}
}

/****************************************************************************/
int CPA_EdIR_BehaviourListBox::SetCurSel(int nSelect,BOOL bDeselect)
{
	if(bDeselect)
	{
		//Unselect old selected items
		LPINT	piIndex=new int[GetCount()];
		int		iNbItems=GetSelItems(GetCount(),piIndex);

		for(int iIndex=0;iIndex<iNbItems;iIndex++)
		{
			SetSel(piIndex[iIndex],FALSE);
		}
	}

	//Select the good item
	return SetSel(nSelect);
}

/****************************************************************************/
int CPA_EdIR_BehaviourListBox::GetCurSel() const
{
	//Returns the first selected item
	LPINT	piIndex=new int[GetCount()];
	GetSelItems(GetCount(),piIndex);

	return piIndex[0];
}

/****************************************************************************/
void CPA_EdIR_BehaviourListBox::m_fn_vOnDeleteBehaviour()
{
	((CPA_EdIR_DiagView *)GetParent())->m_fn_vDeleteBehaviour((CPA_EdIR_Behaviour *)GetItemDataPtr(GetCurSel()));
}

/****************************************************************************/
void CPA_EdIR_BehaviourListBox::m_fn_vOnRenameBehaviour()
{
	//Changes behaviour name
	long				lSelectedIndex=GetCurSel();
	CPA_EdIR_Behaviour	*pclBehaviour=(CPA_EdIR_Behaviour *)GetItemDataPtr(lSelectedIndex);

	pclBehaviour->m_fn_vRenameBehaviour();
}

/****************************************************************************/
void CPA_EdIR_BehaviourListBox::m_fn_vOnAddBehaviour()
{
	((CPA_EdIR_DiagView *)GetParent())->m_fn_vCreateNewBehaviour(ListView);
}

/****************************************************************************/
void CPA_EdIR_BehaviourListBox::m_fn_vOnDeleteAllBehaviours()
{
	((CPA_EdIR_DiagView *)GetParent())->m_fn_vDeleteAllBehaviours();
}

#endif //D_ED_IR_ACTIVE
