#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	condition_eq,
	condition_lt,
	condition_le,
	condition_gt,
	condition_ge,
	condition_ne,
	condition_none,
};
#include <event_utils.h>
//----- CPY 1/4/2008 ADD_CLEAR_SEL_AND_OTHER_CLEAN_UP
/*
#include <..\originlab\XFunctionEx.h>
#define		EQU(a,b)	abs((a)-(b)) < 1e-6

enum{
	_CONDITION_EQUAL = 0,
	_CONDITION_LESSTHAN,
	_CONDITION_LESSTHAN_EQUAL,
	_CONDITION_GREATER,
	_CONDITION_GREATER_EQUAL,
	_CONDITION_LESSTHAN_GREATER,
};
static void _select_rows_in_col(Worksheet& wks,vector<uint>& vnRowIndices,int r1,int r2,int nCol, double dVal,int nCondition)
*/
///Kyle 10/23/08 QA80-12430 REWRITE_XF_TO_USE_VC_LOW_LEVEL_CELL_SELECTION_FUNCTION
//static bool _is_equal(double v1, double v2, double tol)
//{
	//if(tol > 0)
		//return fabs(v1-v2) < tol? true:false;
	//
	//return v1 == v2? true:false;
//}	
///End REWRITE_XF_TO_USE_VC_LOW_LEVEL_CELL_SELECTION_FUNCTION
// added dTol
static void _select_rows_in_col(Worksheet& wks,vector<uint>& vnRowIndices,int r1,int r2,int nCol, double dVal,int nCondition, double dTol)
//----- end ADD_CLEAR_SEL_AND_OTHER_CLEAN_UP
{
	Column cc(wks, nCol);
	//vector<double> vData = cc.GetDataObject();
	vectorbase &vb = cc.GetDataObject();
	///Kyle 10/23/08 QA80-12430 REWRITE_XF_TO_USE_VC_LOW_LEVEL_CELL_SELECTION_FUNCTION
	//vector<double> vData;
	//vData = vb;
	//if ( -1 == r2 )
		//r2 = vData.GetSize() -1;
	//for(int nr = r1; nr <= r2; nr++)
	//{
		//switch (nCondition)
		//{
		//case condition_eq :
			////----- CPY 1/4/2008 ADD_CLEAR_SEL_AND_OTHER_CLEAN_UP
			////if(EQU(vData[nr],dVal))
			//if(_is_equal(vData[nr], dVal, dTol))
			////-----
				//vnRowIndices.Add(nr);
			//break;
		//case condition_lt : 
			//if(vData[nr] < dVal)
				//vnRowIndices.Add(nr);
			//break;
		//case condition_le : 
			//if(vData[nr] <= dVal)
				//vnRowIndices.Add(nr);
			//break;
		//case condition_gt : 
			//if(vData[nr] > dVal)
				//vnRowIndices.Add(nr);
			//break;
		//case condition_ge : 
			//if(vData[nr] >= dVal)
				//vnRowIndices.Add(nr);
			//break;
	    //case condition_ne : 
	    	//if(vData[nr] != dVal)
	    		//vnRowIndices.Add(nr);
	    	//break;
		//default:
			//break;
		//}
	//}
	vnRowIndices.RemoveAll();
	uint wBitwiseOption = 0;
	switch(nCondition)
	{
	case condition_eq:
		wBitwiseOption = MATREPL_TEST_EQUAL;
		break;
	case condition_lt:
		wBitwiseOption = MATREPL_TEST_LESSTHAN;
		break;
	case condition_le:
		wBitwiseOption = MATREPL_TEST_LESSTHAN | MATREPL_TEST_EQUAL;
		break;
	case condition_gt:
		wBitwiseOption = MATREPL_TEST_GREATER;
		break;
	case condition_ge:
		wBitwiseOption = MATREPL_TEST_GREATER | MATREPL_TEST_EQUAL;
		break;
	case condition_ne:
		wBitwiseOption = MATREPL_TEST_LESSTHAN | MATREPL_TEST_GREATER;
		break;
	default:
		break;
	}
	vb.Find(wBitwiseOption, dVal, vnRowIndices, r1, r2, dTol);

	///End REWRITE_XF_TO_USE_VC_LOW_LEVEL_CELL_SELECTION_FUNCTION
}

//----Jake 06/06/07 MODIFY_THIS_XF_FOR_CELL_SELECTION
///Kyle 10/23/08 QA80-12430 REWRITE_XF_TO_USE_VC_LOW_LEVEL_CELL_SELECTION_FUNCTION
//static void _get_cell_sel(vector<uint> vnRowIndices, Worksheet& wks,vector<int>& vnRowBegin,vector<int>& vnColBegin,vector<int>& vnRowEnd,vector<int>& vnColEnd, int nCol)
//{
	//vector<uint> vuBegin, vuEnd;
	//int nRange = find_contiguous_range(vnRowIndices, vuBegin, vuEnd);
	//for(int ii = 0; ii < nRange; ii++)
	//{
		//vnRowBegin.Add(vuBegin[ii]);
		//vnRowEnd.Add(vuEnd[ii]);
		//vnColBegin.Add(nCol);
		//vnColEnd.Add(nCol);
	//}	
//}
///End REWRITE_XF_TO_USE_VC_LOW_LEVEL_CELL_SELECTION_FUNCTION
//----end MODIFY_THIS_XF_FOR_CELL_SELECTION

void wcellsel(Range& rng, const int& condition, const double& val, const double& tol)
{
	int r1, c1, r2, c2;
	
	Worksheet wksTemp;
	
	//----- CPY 1/4/2008 ADD_CLEAR_SEL_AND_OTHER_CLEAN_UP
	if(condition_none == condition)
	{
		if( rng.IsValid() )
		{
			if( rng.GetNumRanges() > 0 )
			{
				rng.GetRange(0, r1, c1, r2, c2, wksTemp);
				if(wksTemp)
					wksTemp.SetSelectedRange(NULL);
			}
		}
		return;
	}
	//----
	
	//------ Folger 03/07/08 QA80-10946 SUPPORT_MULTIPLE_SHEETS
	SheetRangesList rngList(rng);
	int nNumSheet = rngList.GetNumSheets();
	
	for ( int ii=0; ii<nNumSheet; ++ii )
	{
		DataRange drSub;
		if ( rngList.GetWksSubRange(ii, drSub) )
		{
	//------
			vector<int> vnRowBegin, vnRowEnd, vnColBegin, vnColEnd;
			//vector<string> vsSigns = {"==", "<", "<=", ">", ">=", "!="};
			
			
			int nNumData;
			
			//------ Folger 03/07/08 QA80-10946 SUPPORT_MULTIPLE_SHEETS
			//if(rng)
				//nNumData= rng.GetNumData();
			if(drSub)
				nNumData= drSub.GetNumData();
			//------
			
			for(int nSubRange = 0; nSubRange < nNumData; nSubRange++)
			{
				///---Sim 12-14-2007 FIX_INPUT_ACTIVE_RANGE
				//rng.GetRange(nSubRange, r1, c1, r2, c2, wksTemp);
				
				//------ Folger 03/07/08 QA80-10946 SUPPORT_MULTIPLE_SHEETS
				//if ( !rng.GetRange(nSubRange, r1, c1, r2, c2, wksTemp) )
				if ( !drSub.GetRange(nSubRange, r1, c1, r2, c2, wksTemp) )
				//------
				
					continue;
				if ( c2 < 0 )
					c2 = wksTemp.GetNumCols() - 1;
				///---END FIX_INPUT_ACTIVE_RANGE
				for(int nc = c1; nc <= c2; nc++)
				{
					vector<uint> vnRowIndices;
					_select_rows_in_col(wksTemp,vnRowIndices, r1, r2, nc, val, condition, tol);
					///Kyle 10/23/08 QA80-12430 REWRITE_XF_TO_USE_VC_LOW_LEVEL_CELL_SELECTION_FUNCTION
					//_get_cell_sel(vnRowIndices, wksTemp, vnRowBegin, vnColBegin, vnRowEnd, vnColEnd,nc);
					vnRowIndices.UpdateRowColRanges( vnRowBegin, vnColBegin, vnRowEnd, vnColEnd, nc, nc);
					///End REWRITE_XF_TO_USE_VC_LOW_LEVEL_CELL_SELECTION_FUNCTION
				}	
			}
			if (wksTemp)	///---Sim 12-14-2007 FIX_INPUT_ACTIVE_RANGE
				wksTemp.SetSelectedRange(vnRowBegin,vnColBegin,vnRowEnd,vnColEnd);
		}
	}
}

//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int wcellsel_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return wcellsel_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	///Kyle 12/25/2008 QA80-12430 ENABLE_TOL_ONLY_WHEN_CONDITION_EQ_SELETED
	trGetN.tol.Enable = (trGetN.condition.nVal == condition_eq);
	///End ENABLE_TOL_ONLY_WHEN_CONDITION_EQ_SELETED
	
	///Jake 06/06/07 ADD_ERROR_CHECKING 
	Range rRange;
	TreeNode tr = trGetN.rng;
	okxf_resolve_tree_construct_range(&tr,&rRange);
	int nNumData= rRange.GetNumData();
	if(0 == nNumData)
	{
		strErrMsg = XFERR_INVALID_COL_RANGE;
		bOKEnable = false;
	}
	///end ADD_ERROR_CHECKING
	return true;
	

}

int wcellsel_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//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 wcellsel_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///Kyle 12/25/2008 QA80-12430 ENABLE_TOL_ONLY_WHEN_CONDITION_EQ_SELETED
	trGetN.tol.Enable = (trGetN.condition.nVal == condition_eq);
	///End ENABLE_TOL_ONLY_WHEN_CONDITION_EQ_SELETED
}
//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int wcellsel_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
