// Implementation of the IAD_ControlView class
///////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#define HieFriend
#define D_ZdxStuff_StructureDefine

#include "IADVwMn.hpp"

#include "IADFmMn.hpp"
#include "IADVwRst.hpp"
#include "IADDoc.hpp"
#include "IADCnst.hpp"
#include "IADDgStp.hpp"
#include "IADDgQu.hpp"
#include "_IADItrf.hpp"
#include "IADLstCt.hpp"
#include "IADMacro.hpp"

#include "help_def.h"								//For Context help

//External modules
#include "x:\cpa\main\inc\_EditID.h"
#include "TFA.h"
#include "OAc.h"
//End External modules

#define IAD_C_NUMBER_OF_COLUMNS_IN_LIST_OF_ACTORS		5

#define IAD_C_WIDTH_PERCENT_COLUMNS_0								25
#define IAD_C_WIDTH_PERCENT_COLUMNS_1								10
#define IAD_C_WIDTH_PERCENT_COLUMNS_2								25
#define IAD_C_WIDTH_PERCENT_COLUMNS_3								25
#define IAD_C_WIDTH_PERCENT_COLUMNS_4								15

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

/////////////////////////////////////////////////////////////////////////////
// IAD_ControlView

IMPLEMENT_DYNCREATE(IAD_ControlView, CFormView)

BEGIN_MESSAGE_MAP(IAD_ControlView, CFormView)
	//{{AFX_MSG_MAP(IAD_ControlView)
	ON_WM_HELPINFO()
	ON_BN_CLICKED(IDC_CHECK_IA_DEBUGGER_ON, OnCheckIaDebuggerOn)
	ON_WM_SIZE()
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_ACTORS, OnSelchangedListActors)
	ON_BN_CLICKED(IDC_VIEW_ALL, OnViewAll)
	//}}AFX_MSG_MAP

	ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipNotify )

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// IAD_ControlView construction/destruction

//***************************************************************************
IAD_ControlView::IAD_ControlView()
								: CFormView( IAD_ControlView::IDD )
{
	//{{AFX_DATA_INIT(IAD_ControlView)
	//}}AFX_DATA_INIT

//	m_pri_pclListControl = NULL;
}

//***************************************************************************
IAD_ControlView::~IAD_ControlView()
{

}

//***************************************************************************
void IAD_ControlView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);

	//{{AFX_DATA_MAP(IAD_ControlView)
	DDX_Control(pDX, IDC_LIST_ACTORS, m_clListControl);
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// IAD_ControlView message handlers

//***************************************************************************
void IAD_ControlView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();

	EnableToolTips(TRUE);

	m_pub_fn_pclGetDocument()->m_pclControlView = this;

	//Prepares List Control
	CListCtrl *pclListCtrl = (CListCtrl *)GetDlgItem(IDC_LIST_ACTORS);
	if ( pclListCtrl != NULL )
	{
		pclListCtrl->InsertColumn(0, "Actor");
		pclListCtrl->InsertColumn(1, "On");
		pclListCtrl->InsertColumn(2, "Model");
		pclListCtrl->InsertColumn(3, "Family");
		pclListCtrl->InsertColumn(4, "Max Size");
	}

	((CButton *)GetDlgItem(IDC_CHECK_IA_DEBUGGER_ON))->SetCheck(TRUE);
	OnCheckIaDebuggerOn();

	// BEGIN ROMTEAM Cristi Petrescu 98-08-
	m_bViewAll = TRUE;
	((CButton *)GetDlgItem(IDC_VIEW_ALL))->SetCheck(TRUE);
	// END ROMTEAM Cristi Petrescu 98-08-
}

//*******************************************************************************
//Gets the (only existing) document
IAD_MyDocument* IAD_ControlView::m_pub_fn_pclGetDocument()
{
	return g_pclInterface->m_fn_pclGetDocument();
}

// BEGIN ROMTEAM Cristi Petrescu 98-08-
//*******************************************************************************
//Displays the list of Actors
void IAD_ControlView::m_fn_vDisplayActorsList()
{
//Fills list control
//	m_clListControl.SetTextColor(RGB(175, 150, 255));
//	m_clListControl.SetTextBkColor(RGB(64, 64, 64));

	IAD_td_stInternalActorDescription *p_stCurrentInternalActor;
	m_clListControl.DeleteAllItems();

	POSITION pos = m_pri_clListOfActors.GetHeadPosition();
	while ( pos != NULL )
	{
		p_stCurrentInternalActor = m_pri_clListOfActors.GetNext(pos);

		if (! m_bViewAll && ! p_stCurrentInternalActor -> bTraceIsEnabled)
			continue;

		short wIndex = m_clListControl.GetItemCount() ;
		LV_ITEM lvItem;
		
		lvItem.mask = LVIF_TEXT | LVIF_STATE;
		lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
		lvItem.state = 0;
		lvItem.iItem = wIndex;

		lvItem. iSubItem = 0;
		lvItem. pszText = (char *)LPCTSTR(p_stCurrentInternalActor->pclActor->GetName());
		m_clListControl.InsertItem(&lvItem);

		lvItem. iSubItem = 1;
		lvItem. pszText = p_stCurrentInternalActor->bTraceIsEnabled ? "X" : "";
		m_clListControl.SetItem(&lvItem);

		lvItem. iSubItem = 2;
		lvItem. pszText = (char *)LPCTSTR(p_stCurrentInternalActor->pclActor->m_fn_pclGetModel()->GetName());
		m_clListControl.SetItem(&lvItem);

		lvItem. iSubItem = 3;
		lvItem. pszText = (char *)LPCTSTR(p_stCurrentInternalActor->pclActor->m_pub_fn_pclGetFamily()->GetName());
		m_clListControl.SetItem(&lvItem);

		lvItem. iSubItem = 4;
		CString csText;
		csText.Format("%ld", p_stCurrentInternalActor->lNumberOfEntries);
		lvItem. pszText = (char *)LPCTSTR(csText);
		m_clListControl.SetItem(&lvItem);

		m_clListControl.SetItemData(wIndex, (DWORD)p_stCurrentInternalActor);
	}
}
// END ROMTEAM Cristi Petrescu 98-08-

//*******************************************************************************
//Constructs the list of Actors
void IAD_ControlView::m_pub_fn_vBuildActorsList()
{
	//Gets the global Actors List
	CPA_BaseObjectList *pclActorsList = g_pclInterface->GetMainWorld()->fn_p_oGetOriginalObjectList(C_szActorInstanceTypeName);

	//Fills internal list
	Position ActorPos = pclActorsList->GetHeadPosition();
	while ( ActorPos != 0 )
	{
		CPA_Actor *pclCurrentActor = (CPA_Actor *)pclActorsList->GetNext(ActorPos);
		
		//If actor is not an Always Model 
		if ( !pclCurrentActor->m_fn_bIsAnAlways() )
		{
			//If Actor has an IA
			if ( ((tdstEngineObject *)pclCurrentActor->GetStruct())->h_Brain != NULL )
			{
				if ( !m_pri_fn_bActorIsInList(pclCurrentActor) )
				{
					IAD_td_stInternalActorDescription *p_stNewInternalActor;
					p_stNewInternalActor = new IAD_td_stInternalActorDescription;

					p_stNewInternalActor->pclActor = pclCurrentActor;
					
// CG PB DEBUGGER 23/06/98 {
#ifdef ACTIVE_AIDEBUG
					p_stNewInternalActor->bTraceIsEnabled = AIDebug_fn_bGetEnable(M_pstGetMindOfBrain( ((tdstEngineObject *)pclCurrentActor->GetStruct())->h_Brain ));
					p_stNewInternalActor->lNumberOfEntries = AIDebug_fn_uwGetSizeOfTrace(M_pstGetMindOfBrain( ((tdstEngineObject *)pclCurrentActor->GetStruct())->h_Brain ));
#else
					p_stNewInternalActor->bTraceIsEnabled = FALSE;
					p_stNewInternalActor->lNumberOfEntries = 0;
#endif 
// CG PB DEBUGGER
					p_stNewInternalActor->pclRoot = NULL;

					m_pri_clListOfActors.AddTail(p_stNewInternalActor);
				}
 			}
		}
	}

	m_fn_vDisplayActorsList ();
}

//*******************************************************************************
void IAD_ControlView::m_pub_fn_vUnselectAllItems()
{
	//
	long c_lI;
	for (c_lI = 0; c_lI < m_clListControl.GetItemCount(); c_lI ++)
	{
		UINT nState = m_clListControl.GetItemState(c_lI, LVIF_STATE);
		if ( (nState & LVIS_SELECTED) != 0 )
			nState = nState - LVIS_SELECTED;
		m_clListControl.SetItemState(c_lI, nState, LVIF_STATE);
	}
}

//*******************************************************************************
BOOL IAD_ControlView::OnHelpInfo(HELPINFO* pHelpInfo) 
{
	::WinHelp(m_hWnd,
						LPCTSTR(m_pub_fn_pclGetDocument()->m_csHelpFileNameAndPath),
						HELP_CONTEXT,
						IDH_GENERAL);

	return FALSE;
}

//*************************************************************************
//Définition des textes pour les Tool Tips
BOOL IAD_ControlView::OnToolTipNotify(UINT id, NMHDR * pNMHDR, LRESULT * pResult )
{
	TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
	UINT nID = pNMHDR->idFrom;
	nID = ::GetDlgCtrlID((HWND)nID);
    
	BOOL bReturn = FALSE;

/*	switch ( nID )
	{
	default:
 		strcpy(pTTT->szText,"");
		break;
	};
*/
	return (TRUE);
}

// BEGIN ROMTEAM Cristi Petrescu 98-08-
void IAD_ControlView::OnViewAll() 
{
	m_bViewAll = ! m_bViewAll;
	((CButton *) GetDlgItem (IDC_VIEW_ALL))->SetCheck(m_bViewAll);

	m_fn_vDisplayActorsList();
}
// END ROMTEAM Cristi Petrescu 98-08-

//*******************************************************************************
void IAD_ControlView::OnCheckIaDebuggerOn() 
{
	m_pub_fn_pclGetDocument()->m_pub_fn_vSetDebuggerOn( ((CButton *)GetDlgItem(IDC_CHECK_IA_DEBUGGER_ON))->GetCheck() );

	if ( m_pub_fn_pclGetDocument()->m_pub_fn_bIsDebuggerOn() )
		GetDlgItem(IDC_CHECK_IA_DEBUGGER_ON)->SetWindowText("Debugger On");
	else
		GetDlgItem(IDC_CHECK_IA_DEBUGGER_ON)->SetWindowText("Debugger Off");

	//Enables/Disables all Actors
	if ( m_pub_fn_pclGetDocument()->m_pub_fn_bIsDebuggerOn() )
	{
		IAD_td_stInternalActorDescription *p_tdstInternalActor;
		IAD_M_ForEachInList((&m_pri_clListOfActors), pos)
		{
			p_tdstInternalActor = m_pri_clListOfActors.GetNext(pos);
// CG PB DEBUGGER 23/06/98 {
#ifdef ACTIVE_AIDEBUG
			AIDebug_fn_vSetEnable(m_pub_fn_pclGetDocument()->m_pub_fn_p_stGetMindOfActor(p_tdstInternalActor), p_tdstInternalActor->bTraceIsEnabled);
#endif 
// CG PB DEBUGGER
		}
	}
// CG PB DEBUGGER 23/06/98 {
#ifdef ACTIVE_AIDEBUG
	else
	{
		IAD_M_ForEachInList((&m_pri_clListOfActors), pos)
			AIDebug_fn_vSetEnable(m_pub_fn_pclGetDocument()->m_pub_fn_p_stGetMindOfActor(m_pri_clListOfActors.GetNext(pos)), FALSE);
	}
#endif 
// CG PB DEBUGGER
}

//*******************************************************************************
void IAD_ControlView::OnSelchangedListActors(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW *pNMListView = (NM_LISTVIEW *)pNMHDR;

	if ( (pNMListView->uNewState & LVIS_SELECTED) != 0 )
	{
		switch ( pNMListView->iSubItem )
		{
			//Column "Name" has been clicked
			case  0:
			{
				CListCtrl *pclListCtrl = (CListCtrl *)GetDlgItem(IDC_LIST_ACTORS);
				IAD_td_stInternalActorDescription *p_stSelectedInternalActor;
				p_stSelectedInternalActor = (IAD_td_stInternalActorDescription *)pclListCtrl->GetItemData(pNMListView->iItem);

				if ( p_stSelectedInternalActor->bTraceIsEnabled )
					m_pub_fn_pclGetDocument()->m_pclResultView->m_pub_fn_vDisplayTraceForActor(p_stSelectedInternalActor);

				break;
			}

			//Column "On" has been clicked	--> DONE ELSEWHERE
			case  1:
				break;

			//Column "Size" has been clicked	--> DONE ELSEWHERE
			case  4:
				break;
		}
	}

	*pResult = 0;
}

//*******************************************************************************
void IAD_ControlView::OnSize(UINT nType, int cx, int cy) 
{
	//Moves Button
	CButton *pclButton = (CButton *)GetDlgItem(IDC_CHECK_IA_DEBUGGER_ON);
	CRect crButtonDestRect;

	if ( pclButton != NULL )
	{
		CRect crButtonRect;
		GetDlgItem(IDC_CHECK_IA_DEBUGGER_ON)->GetWindowRect(crButtonRect);
		
		crButtonDestRect.top = 2;
		crButtonDestRect.bottom = crButtonDestRect.top + crButtonRect.Height();
		crButtonDestRect.right = cx - 2;
		crButtonDestRect.left = crButtonDestRect.right - crButtonRect.Width();
	
		GetDlgItem(IDC_CHECK_IA_DEBUGGER_ON)->MoveWindow(crButtonDestRect);
	}

	// BEGIN ROMTEAM Cristi Petrescu 98-08-
	// Moves View All button
	pclButton = (CButton *)GetDlgItem(IDC_VIEW_ALL);
	
	if ( pclButton != NULL )
	{
		CRect crButtonRect;
		GetDlgItem(IDC_VIEW_ALL)->GetWindowRect(crButtonRect);
		
		crButtonDestRect.top = 2;
		crButtonDestRect.bottom = crButtonDestRect.top + crButtonRect.Height();
		crButtonDestRect.right = crButtonDestRect.left - 2;
		crButtonDestRect.left = crButtonDestRect.right - crButtonRect.Width();
	
		GetDlgItem(IDC_CHECK_IA_DEBUGGER_ON)->MoveWindow(crButtonDestRect);
	}
	// END ROMTEAM Cristi Petrescu 98-08-


	//Re-sizes ListCtrl
	CListCtrl *pclListCtrl = (CListCtrl *)GetDlgItem(IDC_LIST_ACTORS);
	
	if ( pclListCtrl != NULL )
	{
		CRect crCtrlRect;
		pclListCtrl->GetWindowRect(crCtrlRect);

		crCtrlRect.top = crButtonDestRect.bottom + 2;
		crCtrlRect.bottom = cy - 2;
		crCtrlRect.left = 2;
		crCtrlRect.right = cx - 2;

		pclListCtrl->MoveWindow(crCtrlRect);

		//Re_sizes Columns
		short wColumnWidth = (crCtrlRect.Width() * IAD_C_WIDTH_PERCENT_COLUMNS_0) / 100 - 2;
		pclListCtrl->SetColumnWidth(0, wColumnWidth);
		wColumnWidth = (crCtrlRect.Width() * IAD_C_WIDTH_PERCENT_COLUMNS_1) / 100 - 2;
		pclListCtrl->SetColumnWidth(1, wColumnWidth);
		wColumnWidth = (crCtrlRect.Width() * IAD_C_WIDTH_PERCENT_COLUMNS_2) / 100 - 2;
		pclListCtrl->SetColumnWidth(2, wColumnWidth);
		wColumnWidth = (crCtrlRect.Width() * IAD_C_WIDTH_PERCENT_COLUMNS_3) / 100 - 2;
		pclListCtrl->SetColumnWidth(3, wColumnWidth);
		wColumnWidth = (crCtrlRect.Width() * IAD_C_WIDTH_PERCENT_COLUMNS_4) / 100 - 2;
		pclListCtrl->SetColumnWidth(4, wColumnWidth);
	}

	CFormView::OnSize(nType, cx, cy);
}

///////////////////////
// Private functions //
///////////////////////

//*******************************************************************************
BOOL IAD_ControlView::m_pri_fn_bActorIsInList(CPA_Actor *_pclSearchedActor)
{
	BOOL bActorFound = FALSE;
	POSITION pos = m_pri_clListOfActors.GetHeadPosition();
	while ( (pos != NULL) && (!bActorFound) )
		bActorFound = ( m_pri_clListOfActors.GetNext(pos)->pclActor == _pclSearchedActor );

	return bActorFound;
}


//*******************************************************************************
//*******************************************************************************
/////////////////////////////////////////////////////////////////////////////
// IAD_ControlView diagnostics

#ifdef _DEBUG
void IAD_ControlView::AssertValid() const
{
	CFormView::AssertValid();
}

void IAD_ControlView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG
