#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <event_utils.h>
//put your own support static functions here

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 03/13/09 QA80-13262 CONVERT_OBJECT_VARIABLE_OPTIOANL_TO_NEW_IN_XF_DIALOG
 *	Folger 04/10/09 QA80-13262-P2 OPTIONAL_SHOWN_INSTEAD_OF_NEW_AFTER_RESET_THEME
 *  Iris 2/24/2010 QA81-10824 TRIM_UNUSEFUL_MISSING_DATA_IN_OUTPUT_COLUMNS		*
 *------------------------------------------------------------------------------*/

//if vector exist, set size and get its pointer  
static double* _get_pointer(vector& v, int nSize)
{
	double* ptemp = NULL;	
	if(v)
	{	
		v.SetSize(nSize);
		ptemp = v;
	}	
	return	ptemp; 
}



void rowstats(const Range& irng, vector& mean, vector& sd, vector& sem, vector& min, vector& max, vector& n, vector& sum)
{
	
	int nErrCode = check_1_data(irng, true);
	if (CER_NO_ERROR != nErrCode)
	{
		if (nErrCode == CER_COL_NUM_TOO_FEW_EX)
			XF_THROW_EX(nErrCode, 2)
		else
			XF_THROW(nErrCode);
	}
	
	matrix mat;
	///---Sim 12-05-2007 REWRITE_WKS_DATA_RANGE_TO_MAT
	//if(!get_data_from_dr_to_mat(irng, mat))
	/// Iris 2/24/2010 QA81-10824 TRIM_UNUSEFUL_MISSING_DATA_IN_OUTPUT_COLUMNS
	//if(!get_data_from_dr_to_mat(irng, mat, false))
	if(!get_data_from_dr_to_mat(irng, mat, MATRIX_FROM_RANGE_REMOVE_MISSING_FROM_LAST_SEL_ROW))
	///End TRIM_UNUSEFUL_MISSING_DATA_IN_OUTPUT_COLUMNS
	///---END REWRITE_WKS_DATA_RANGE_TO_MAT
		XF_THROW(XFERR_FAILED_GET_DATA_FROM_DATARANGE_TO_MAT);	
	
	int nRows = mat.GetNumRows();
	int nCols = mat.GetNumCols();
	//if(nRows < 1 || nCols < 1)
		//XF_THROW(XFERR_FAILED_GET_DATA_FROM_DATARANGE);	
	
	double	*pMean = _get_pointer(mean, nRows);	
	double	*pSd   = _get_pointer(sd, nRows);
	double	*pSem  = _get_pointer(sem, nRows);
	double	*pMin  = _get_pointer(min, nRows);
	double	*pMax  = _get_pointer(max, nRows);
	double	*pSum  = _get_pointer(sum, nRows);
	double	*pN    = _get_pointer(n, nRows);
	//	dw = GetTickCount();
	int nRet = ocmath_row_desc_stats(nRows, nCols, mat, pMean, pSd, pSem, pMin, pMax, pSum, pN);
	//	printf("ocmath_row_desc_stats time=%g\n",(GetTickCount()-dw)/1000.0);
	
	if ( nRet != OE_NOERROR )
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	
}

//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 rowstats_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return rowstats_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	///------ Folger 04/10/09 QA80-13262-P2 OPTIONAL_SHOWN_INSTEAD_OF_NEW_AFTER_RESET_THEME
	if ( GETNE_ON_THEME == nEventID )
		check_convert_optional_outputs_to_new(trGetN);
	///------ End OPTIONAL_SHOWN_INSTEAD_OF_NEW_AFTER_RESET_THEME
	
	return false;	

}

int rowstats_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 rowstats_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///------ Folger 03/13/09 QA80-13262 CONVERT_OBJECT_VARIABLE_OPTIOANL_TO_NEW_IN_XF_DIALOG
	if ( nGetNDialog > 0 )		/// open dialog
		check_convert_optional_outputs_to_new(trGetN);
	///------ End CONVERT_OBJECT_VARIABLE_OPTIOANL_TO_NEW_IN_XF_DIALOG
}
//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 rowstats_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
