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

/***************************************************************************************
/// Modification Log :
*	Iris 1/04/2010 QA81-14877-2 AUTO_HIGHLIGHT_COLA_IF_NO_SELECTION_IN_WKS
*	Gareth 7/5/2010 ORG-468 ADD_SET_COL_VALUES_BY_LABTALK
*	Gareth 7/22/2010 ORG-468-P5 MAKE_FORMULA_RANGE_CONSISDENT_WITHOUT_SETTING
*	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
***************************************************************************************/

//put your own support static functions here

///Gareth 7/5/2010 ORG-468 ADD_SET_COL_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_COL_VALUES_BY_LABTALK

void csetvalue(const Range& col, const string& formula, const string& script, const int& recalculate)
{
	
	
	
	//---- Iris 1/04/2010 QA81-14877-2 AUTO_HIGHLIGHT_COLA_IF_NO_SELECTION_IN_WKS
	Worksheet wks = Project.ActiveLayer();
	//if( wks )///Gareth 9/10/2010 ORG468-P8 FIX_ABORT_WHEN_ACTIVELAYER_NOT_WORKSHEET
	if( wks.IsValid() || col.GetNumRanges() > 0)///Gareth 9/10/2010 ORG468-P8 FIX_ABORT_WHEN_ACTIVELAYER_NOT_WORKSHEET
	{
		int r1, c1, r2, c2;
		if(col.GetNumRanges() == 0) // when no range input, set first column as default
		{
	
			int nRet = wks.GetSelectedRange(r1, c1, r2, c2);
	
			vector<int> vR1(1), vC1(1), vR2(1), vC2(1);
			bool bKeepCurrentSel = true;
			if( WKS_SEL_NONE == nRet ) // if no selection 
			{			
				vR1[0] = 0;
				vC1[0] = 0;
				vR2[0] = -1;
				vC2[0] = 0;
				bKeepCurrentSel = false;
			}
			else if ( WKS_SEL_COL_LABEL & nRet ) // or select on label.
			{
				vR1[0] = 0;
				vC1[0] = c1;
				vR2[0] = -1;
				vC2[0] = c1;
				bKeepCurrentSel = false;			
			}
			else if( !(WKS_SEL_ONE_COL & nRet) && c1 != c2 ) // more than one col selected, convert selection to only one column, 1st col.
			{
				vR1[0] = r1;
				vC1[0] = c1;
				vR2[0] = r2;
				vC2[0] = c1;
				bKeepCurrentSel = false;
			}
			if( !bKeepCurrentSel )
			{
				wks.SetSelectedRange(vR1, vC1, vR2, vC2);
				col.Add(wks);
			}
		}
		///Gareth 7/5/2010 ORG-468 ADD_SET_COL_VALUES_BY_LABTALK
	
		//FUNC_INT_DWORD_LPVOID pfn = Project.FindFunction("ColMatValues", NULL, true);
	
	
		//if(pfn)
		//{
		//pfn(0,0,NULL); 
		//}	
	
	
		if(!col.IsValid())
		{
		
			XF_THROW(XFERR_INVALID_COL_RANGE);
		}
		///Gareth 8/6/2010 ORG-468-S2 USE_NEW_MECHANISM_TO_OPEN_DLG	
		if(formula == "?" && script == "?")
		{
			FUNC_INT_DWORD_LPVOID pfn = Project.FindFunction("ColMatValues", NULL, true);
			if(pfn)
			{
				pfn(0,0,NULL);
			}
			return;
		}
	
		///END USE_NEW_MECHANISM_TO_OPEN_DLG	
	
		if(col.GetNumRanges() != 1)
		{
			XF_THROW(XFERR_INVALID_COL_RANGE);///Gareth 9/10/2010 ORG468-P8 FIX_ABORT_WHEN_ACTIVELAYER_NOT_WORKSHEET
			return ;
		}
	
		r1 = -1;
		r2 = -1;
	
		bool bGetRng = col.GetRange(0, r1, c1, r2, c2, wks);
		Column column1;
		column1.Attach(wks, c1);
	
		string strFormula = formula;
		string strScript = script;
	
		///Gareth 8/6/2010 ORG-468-S2 USE_NEW_MECHANISM_TO_OPEN_DLG	
		if(formula == "?" || script == "?")
		{
			string strOrgFormula = column1.GetFormula();
			string strtempFormula, strtempScript;
			ParseFormula(strOrgFormula, strtempFormula, strtempScript);
			if(formula == "?")
			{
				strFormula = strtempFormula;
			}
			if(script == "?")
			{
				strScript = strtempScript;
			}
		}
		///END USE_NEW_MECHANISM_TO_OPEN_DLG	
	
		strFormula.TrimLeft();
		strFormula.TrimRight();
		strScript.TrimLeft();
		strScript.TrimRight();
		if(!strScript.IsEmpty())
		{
			strFormula += STR_COL_FORMULAR_SEPARATOR;
			strFormula += strScript;
		}
		///Gareth 7/22/2010 ORG-468-P5 MAKE_FORMULA_RANGE_CONSISDENT_WITHOUT_SETTING
		if(r1==0 && r2==-1)
		{
			r1 = -1;
		}
		if(column1.SetFormula(strFormula, recalculate, r1, r2))
		{
			column1.ExecuteFormula();
		}
		///END_ADD_SET_COL_VALUES_BY_LABTALK
	
	}
	///Gareth 9/10/2010 ORG468-P8 ABORT_WHEN_ACTIVELAYER_NOT_WORKSHEET
	else
	{
		XF_THROW(XFERR_INVALID_COL_RANGE);
	}
	///END FIX_ABORT_WHEN_ACTIVELAYER_NOT_WORKSHEET
	
}

//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 csetvalue_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
