#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	recalculate_none,
	recalculate_auto,
	recalculate_manual,
};
#include <xfutils.h>
//put additional include files here

/***************************************************************************************
/// Modification Log :
*	Gareth 7/5/2010 ORG-468-P2 ADD_SET_MATRIX_VALUES_BY_LABTALK
*	Gareth 8/6/2010 ORG-468-S2 USE_NEW_MECHANISM_TO_OPEN_DLG
*	Gareth 8/6/2010 ORG-468-S1 VARIABLE_MATRIXOBJECT'S_NAME_CHANGE_FROM_MTXOBJ_TO_IM
*	Gareth 8/9/2010 ORG-468-S2 FIX_MISSING_SCRIPT
*	Gareth 8/10/2010 CODE_CLEANING
***************************************************************************************/

///Gareth 7/5/2010 ORG-468-P2 ADD_SET_MATRIX_VALUES_BY_LABTALK
static void ParseFormula(const string& strOrgFormula, string& strFormula, string& strScript)
{
	int nFirst = strOrgFormula.Find(STR_COL_FORMULAR_SEPARATOR);
	if(nFirst == -1)
	{
		strFormula = strOrgFormula;
		strScript = "";
	}
	else
	{

		strFormula = strOrgFormula.Mid(0, nFirst);
		///Gareth 8/9/2010 ORG-468-S2 FIX_MISSING_SCRIPT
		//int nSecond = strOrgFormula.Find(STR_COL_FORMULAR_SEPARATOR);
		int nSecond = strOrgFormula.Find(STR_COL_FORMULAR_SEPARATOR, nFirst+1);
		///END FIX_MISSING_SCRIPT
		if(nSecond != -1) // has second STR_COL_FORMULAR_SEPARATOR
		{
			string strFlag = STR_COL_FORMULAR_SEPARATOR;
			//int nLen = sizeof(strFlag);
			strScript = strOrgFormula.Mid(nFirst+sizeof(strFlag), nSecond-nFirst-sizeof(strFlag));
		}
		else
		{
			if(strOrgFormula[nFirst+5] == '[' && strOrgFormula[strOrgFormula.GetLength()-1] == ']')//It is range, not script. Return ""
			{
				strScript = "";
			}
			else
			{
				string strFlag = STR_COL_FORMULAR_SEPARATOR;
				strScript = strOrgFormula.Mid(nFirst+sizeof(strFlag), strOrgFormula.GetLength()-nFirst-sizeof(strFlag));
			}
		}

	}

}
///END ADD_SET_MATRIX_VALUES_BY_LABTALK

void msetvalue(const MatrixObject& im, const string& formula, const string& script, const int& recalculate)
{
	///Gareth 7/5/2010 ORG-468-P2 ADD_SET_MATRIX_VALUES_BY_LABTALK
	
	//FUNC_INT_DWORD_LPVOID pfn = Project.FindFunction("ColMatValues", NULL, true);
	//if(pfn)
	//{
		//pfn(0,0,NULL);
	//}
	
	///Gareth 8/6/2010 ORG-468-S2 USE_NEW_MECHANISM_TO_OPEN_DLG	
	if(formula == "?" && script == "?") //Both formula and script are no changing.Just open dlg
	{
		FUNC_INT_DWORD_LPVOID pfn = Project.FindFunction("ColMatValues", NULL, true);
		if(pfn)
		{
			pfn(0,0,NULL);
		}
		return;
	}
	
	string strFormula = formula;
	string strScript = script;
	
	if(formula == "?" || script == "?")
	{
		string strOrgFormula = im.GetFormula();
		string strtempFormula, strtempScript;
		ParseFormula(strOrgFormula, strtempFormula, strtempScript);
		if(formula == "?")
		{
			strFormula = strtempFormula;
		}
		if(script == "?")
		{
			strScript = strtempScript;
		}
	}
	///END USE_NEW_MECHANISM_TO_OPEN_DLG	
	
	
	if(!im.IsValid())
	{
	
		XF_THROW(XFERR_INVALID_MATOBJ);
	}
	
	
	
	strFormula.TrimLeft();
	strFormula.TrimRight();
	strScript.TrimLeft();
	strScript.TrimRight();
	if(!strScript.IsEmpty())
	{
		strFormula += STR_COL_FORMULAR_SEPARATOR;
		strFormula += strScript;
	}
	
	///Gareth 8/6/2010 ORG-468-S1 VARIABLE_MATRIXOBJECT'S_NAME_CHANGE_FROM_MTXOBJ_TO_IM
	
	//if(mtxobj.SetFormula(strFormula, recalculate))
	//{
		//mtxobj.ExecuteFormula();
	//
	//}
	
	
	
	if(im.SetFormula(strFormula, recalculate))
	{
		im.ExecuteFormula();
	
	}
	// END VARIABLE_MATRIXOBJECT'S_NAME_CHANGE_FROM_MTXOBJ_TO_IM
	
	///END_ADD_SET_MATRIX_VALUES_BY_LABTALK
		
}

//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void msetvalue_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
