// iadvrvw.cpp : implementation file
//

#include "stdafx.h"
#define HieFriend

// BEGIN ROMTEAM Cristi Petrescu 98-06-
#include "iadedttr.hpp"
#include "IADglob.hpp"

#include "x:/cpa/tempgrp/tia/inc/ai_intf.hpp"
#include "x:/cpa/main/inc/_EditId.h"
// END ROMTEAM Cristi Petrescu 98-06-

#include "IADVrVw.hpp"

#include "_IADItrf.hpp"

#include "help_def.h"	//for Context Help

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

/////////////////////////////////////////////////////////////////////////////
// IAD_VariableView

IMPLEMENT_DYNCREATE(IAD_VariableView, CFormView)

IAD_VariableView::IAD_VariableView()
	: CFormView(IAD_VariableView::IDD)
{
	//{{AFX_DATA_INIT(IAD_VariableView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

IAD_VariableView::~IAD_VariableView()
{
}

void IAD_VariableView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(IAD_VariableView)
	DDX_Control(pDX, IDC_LIST_VARIABLES, m_clListVariable);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(IAD_VariableView, CFormView)
	//{{AFX_MSG_MAP(IAD_VariableView)
	ON_WM_HELPINFO()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// IAD_VariableView diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// IAD_VariableView message handlers

void IAD_VariableView::OnInitialUpdate () 
{
	CFormView::OnInitialUpdate ();
	
	g_pclInterface -> m_fn_pclGetDocument () -> m_pclVariableView = this;

	//Prepares List Control
	CListCtrl *pclListCtrl = (CListCtrl *)GetDlgItem(IDC_LIST_VARIABLES);
	if ( pclListCtrl != NULL )
	{
		pclListCtrl->InsertColumn(0, "Name");
		pclListCtrl->InsertColumn(1, "Value");
	}	
}

BOOL IAD_VariableView::OnHelpInfo(HELPINFO* pHelpInfo) 
{
	::WinHelp(m_hWnd,
						LPCTSTR(g_pclInterface -> m_fn_pclGetDocument () -> m_csHelpFileNameAndPath),
						HELP_CONTEXT,
						IDH_GENERAL);

	return FALSE;
}

void IAD_VariableView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	//Re-sizes ListCtrl
	CListCtrl *pclListCtrl = (CListCtrl *)GetDlgItem(IDC_LIST_VARIABLES);
	
	if ( pclListCtrl != NULL )
	{
		CRect crCtrlRect;
		pclListCtrl->GetWindowRect(crCtrlRect);

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

		pclListCtrl->MoveWindow(crCtrlRect);

		//Re_sizes Columns
		short wColumnWidth = crCtrlRect.Width() / 2 - 2;
		pclListCtrl->SetColumnWidth(0, wColumnWidth);
		wColumnWidth = crCtrlRect.Width() / 2 - 2;
		pclListCtrl->SetColumnWidth(1, wColumnWidth);
	}	
}

// BEGIN ROMTEAM Cristi Petrescu 98-06-
void IAD_VariableView::m_fn_vAddVariable (CString csName, CString csValue)
// END ROMTEAM Cristi Petrescu 98-06-
{
	CListCtrl *pclListCtrl = (CListCtrl *)GetDlgItem(IDC_LIST_VARIABLES);

	//csName . MakeLower ();
	LV_ITEM lvItem;
	// BEGIN ROMTEAM Cristi Petrescu 98-06-
	lvItem.mask = LVIF_TEXT | LVIF_STATE;
	// END ROMTEAM Cristi Petrescu 98-06-
	lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
	lvItem.state = 0;

	// check if there is already inserted
	LV_FINDINFO lvFindInfo;
	int lItem;
	lvFindInfo . flags = LVFI_STRING;
	lvFindInfo . psz = (char *) LPCTSTR (csName);
	lItem = pclListCtrl -> FindItem (& lvFindInfo);
	if (lItem == -1)
	{
		// add it to the list
		lvItem.iItem = pclListCtrl -> GetItemCount ();
		lvItem. iSubItem = 0;
		lvItem. pszText = (char *) LPCTSTR (csName);
		pclListCtrl -> InsertItem (& lvItem);
	}
	else
	{
		// we will update the value
		lvItem . iItem = lItem;
	}

	pclListCtrl ->SetItemText (lvItem . iItem, 1, (char *) LPCTSTR (csValue));
}

// BEGIN ROMTEAM Cristi Petrescu 98-06-
void IAD_VariableView::m_fn_vUpdateVariables (IAD_EditorTreeNode *pclEngineLoop)
{
	if (! pclEngineLoop)
		return;

	CListCtrl *pclListCtrl = (CListCtrl *) GetDlgItem (IDC_LIST_VARIABLES);
	long i;
	long n = pclListCtrl -> GetItemCount ();
	CString csVariableName;
	long lVariableIndex;
	CString csValue;
	tdstGetSetParam *p_stValue;
	for (i = 0; i < n; i ++)
	{
		csVariableName = pclListCtrl -> GetItemText (i, 0);
		
		CPA_DLLBase *p_clDll = g_pclInterface->GetMainWorld()->GetToolDLLWithName(C_szDLLAIEditorName);
		if ( p_clDll == NULL )
			continue;
		lVariableIndex = p_clDll -> OnQueryAction (g_pclInterface, C_uiAI_GetVariableIndex, (long) & csVariableName);
		if (lVariableIndex < 0)
			continue;
		p_stValue = pclEngineLoop -> m_fn_p_tdstGetVariableValue (lVariableIndex);
		if (! p_stValue)
			continue;

		csValue = IAD_fn_csComputeValueFromGetSetParam (p_stValue);

		pclListCtrl -> SetItemText (i, 1, (char *) LPCTSTR (csValue));
	}
}
// END ROMTEAM Cristi Petrescu 98-06-
