#include "StdAfx.h"
#include "FuncParamCtrl.h"
#include "ParamList.h"
#include "SwiListCtrl.h"

#define COL_Name		0
#define COL_Value		1
#define MIN_WIDTH_Value	345

CFuncParamCtrl::CFuncParamCtrl(void)
{
	m_nNumberOfItems = 0;
}

CFuncParamCtrl::~CFuncParamCtrl(void)
{
}

BOOL CFuncParamCtrl::Initialize(CSwiListCtrl* pFuncParamCtrl)
{
	m_pFuncParamCtrl = pFuncParamCtrl;

	// Format status window control
	m_pFuncParamCtrl->InsertColumn(COL_Name, L"Parameter", LVCFMT_LEFT, 132);
	m_pFuncParamCtrl->InsertColumn(COL_Value, L"Value", LVCFMT_LEFT, MIN_WIDTH_Value);

	m_pFuncParamCtrl->SetExtendedStyle(LVS_EX_FULLROWSELECT);
	m_pFuncParamCtrl->InitControls();
	m_pFuncParamCtrl->SetGetItemPropFn(&GetItemProperties);

	return TRUE;
}

BOOL CFuncParamCtrl::Display(CParamList* pFuncParams)
{
	// clear screen
	m_pFuncParamCtrl->DeleteAllItems();
	m_nNumberOfItems = 0;

	// If nothing to display, exit
	if(!pFuncParams)
		return FALSE;

	CString szLabel, szValue;

	for (int i=0; i<pFuncParams->GetCount(); i++)
	{
		if (pFuncParams->GetAt(i, szLabel, szValue))
		{
			m_pFuncParamCtrl->InsertItem(m_nNumberOfItems, szLabel);
			m_pFuncParamCtrl->SetItemText(m_nNumberOfItems, COL_Value, szValue);
			m_pFuncParamCtrl->SetItemData(m_nNumberOfItems, (DWORD_PTR)m_pFuncParamCtrl);
			m_nNumberOfItems++;
		}
	}

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
//
//   Function   :	PopulateEditBox
//
//   Description:	Callback function used to populate a custom in-place
//					edit box
//
//   Arguments  :	[in]lParam1		- itemdata
//					[in]pEdit		- editcontrol
//
//   Returns:   :	none
//
//   Comments   :	This function is called upon primarily to have the
//					edit control populated properly with the default text.
//					ReadOnly, Password, and Numeric properties are already
//					set
//
/////////////////////////////////////////////////////////////////////////////
void CFuncParamCtrl::PopulateEditBox(LPARAM lParam1, CEdit* pEdit)
{
	// lParam1 is a pointer to the function parameter list control
	CSwiListCtrl* pLstCtrl = (CSwiListCtrl*)lParam1;

	// populate the edit control with the current value
	CString szTemp;
	szTemp.Format(L"%s", pLstCtrl->GetItemText(pLstCtrl->GetSelectionMark(), COL_Value));
	pEdit->SetWindowText(szTemp);
}

/////////////////////////////////////////////////////////////////////////////
//
//   Function   :	GetItemProperties
//
//   Description:	Callback function used to retrieve any custom item properties
//
//   Arguments  :	[in]lParam		- LPARAM
//					[in]nItem		- item index
//					[in]nSubitem	- subitem index
//					[out]prop		- properties
//
//   Returns:   :	bool if properties retrieved successfully
//
//   Comments   :	none
//
/////////////////////////////////////////////////////////////////////////////
bool CFuncParamCtrl::GetItemProperties(LPARAM lParam, int nItem, int nSubItem, S_ITEMPROP& prop)
{
	UNREFERENCED_PARAMETER(nItem);
	UNREFERENCED_PARAMETER(nSubItem);
	UNREFERENCED_PARAMETER(lParam);

	prop.bNumeric	= false;
	prop.bPassword	= false;
	prop.bReadOnly	= false;
	prop.iMaxLen	= 255;
	return true;
}

void CFuncParamCtrl::OnUserClick(NMHDR *pNMHDR, LRESULT *pResult)
{
	UNREFERENCED_PARAMETER(pResult);
#ifndef UNDER_CE
	NMITEMACTIVATE* pItemActivate	= (NMITEMACTIVATE*)pNMHDR;
#else
	NMLISTVIEW* pItemActivate = (NMLISTVIEW*)pNMHDR;
#endif

	// pre-populate edit control with original values
	m_pFuncParamCtrl->SetEditBoxFn(&PopulateEditBox);
	// edit item
	m_pFuncParamCtrl->EditItem(pItemActivate->iItem, COL_Value);
}