#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	method_v2col,
	method_v2row,
	method_v2m,
};
enum{
	direction_row,
	direction_col,
};
/*------------------------------------------------------------------------------*
 *	Echo 8/13/07 NOT_CHECK_OUTPUT_NOW											*
 *------------------------------------------------------------------------------*/
 
 
#include <GetNBox.h>
//put additional include files here

//put your own support static functions here
//enum{
	//COPY_VECTOR_TO_A_COLUMN,
	//COPY_VECTOR_TO_A_ROW,
	//COPY_VECTOR_TO_WHOLE_MATRIX,
//};
//
//enum{
	//BY_ROW,
	//BY_COLUMN,
//};
static void _init_tree_show(TreeNode& trGetN)
{
	int nMethod = trGetN.method.nVal;
	switch(nMethod)
	{
	case method_v2col:
		trGetN.direction.Show = false;
		//trGetN.rows.Show = false;
		//trGetN.cols.Show = false;
		break;
	case method_v2row:
		trGetN.direction.Show = false;
		//trGetN.rows.Show = false;
		//trGetN.cols.Show = false;
		break;
	case method_v2m:
		trGetN.index.Show = false;
		break;	
	}
}
static void _update_tree_show_by_matrix(TreeNode& trGetN, matrixbase& mat)
{
	int nRows = mat.GetNumRows();
	int nCols = mat.GetNumCols();
	if(is_missing_value(trGetN.rows.nVal))
		trGetN.rows.nVal = nRows;
	if(is_missing_value(trGetN.rows.nVal))
		trGetN.cols.nVal = nCols;
}
static void _update_tree_show_by_selection(TreeNode& trGetN)
{
	int nMethod = trGetN.method.nVal;
	switch(nMethod)
	{
	case method_v2col:
		trGetN.direction.Show = false;
		//trGetN.rows.Show = false;
		//trGetN.cols.Show = false;
		trGetN.index.Show = true;
		break;
	case method_v2row:
		trGetN.direction.Show = false;
		trGetN.index.Show = true;
		break;
	case method_v2m:
		trGetN.index.Show = false;
		trGetN.direction.Show = true;
		break;	
	}
}

void v2m(const vector& ix, const int& method, const int& index, const int& direction, MatrixObject& om)
{
	
	int nRows = om.GetNumRows();
	int nCols = om.GetNumCols();
	switch(method)
	{
	case method_v2col:
		if(index <= 0 || index > nCols)
			XF_THROW(XFERR_INVALID_COLUMN);
		if(ix.GetSize() > nRows)
		{
			//ix.SetSize(nRows);
			om.SetSize(ix.GetSize(), nCols);
			//vector vb;
			//vb.SetSize(nCols);
			//vb = NANUM;
			//for(int ii = nRows; ii < ix.GetSize(); ii++)
				//mat.SetRow(vb, ii);
			
			warning_msg_box(XFWAR_RESIZE_MATRIX, false);
		}
		matrixbase& mat = om.GetDataObject();
		mat.SetColumn(ix, index-1);
		break;
	case method_v2row:
		if(index <= 0 || index > nRows)
			XF_THROW(XFERR_INVALID_ROW);
		if(ix.GetSize() > nCols)
		{
			om.SetSize(nRows, ix.GetSize());
			//vector vb;
			//vb.SetSize(nRows);
			//vb = NANUM;
			//for(int ii = nCols; ii < ix.GetSize(); ii++)
				//mat.SetColumn(vb, ii);
			
			warning_msg_box(XFWAR_RESIZE_MATRIX, false);
		}
		matrixbase& mat = om.GetDataObject();
		mat.SetRow(ix, index-1);
		break;
	case method_v2m:
		if(nRows*nCols < ix.GetSize())
		{
			ix.SetSize(nRows*nCols);
			warning_msg_box(XFWAR_CUT_VECTOR, false);
		}
		matrixbase& mat = om.GetDataObject();
		if(direction == direction_row)
			mat.SetByVector(ix);
		else
			mat.SetByVector(ix, false);
		break;
	}
}

//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 v2m_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return v2m_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	bool bIsChangingData = lstrcmpi(lpcszNodeName,"method") == 0 ? true:false;
	if(bIsChangingData)
		_update_tree_show_by_selection(trGetN);
	
	if(nEventID == GETNE_ON_INIT)
		_init_tree_show(trGetN);
	DataRange dr;
	TreeNode tr = trGetN.ix;
	okxf_resolve_tree_construct_range(&tr, &dr); 
	if(!dr.IsValid())
		bOKEnable = false;
	
	///Echo 8/13/07 NOT_CHECK_OUTPUT_NOW
	///Matric Object can not be got when output is set as <new> in event1. Since in all XF we did not check ouput now. 
	///So I comment the error checking codes related to ouput matrix
	//MatrixObject om;
	//okxf_resolve_string_get_origin_object(trGetN.om.strVal, &om);
	//if(!om.IsValid())
		//bOKEnable = false;
	//
	//if(om && dr)
	//if(dr)	
	//{
		//matrixbase &mat = om.GetDataObject();
		////_update_tree_show_by_matrix(trGetN, mat);
		//int nRows = mat.GetNumRows();
		//int nCols = mat.GetNumCols();
		//int nMethod = trGetN.method.nVal;
		//int index = trGetN.index.nVal;
		//if((nMethod == method_v2col) && (index <= 0 || index > nCols))
		//{
			//bOKEnable = false;
			//strErrMsg = XFERR_INVALID_COLUMN;
		//}
			//
		//if((nMethod == method_v2row) && (index <= 0 || index > nRows))
		//{
			//bOKEnable = false;
			//strErrMsg = XFERR_INVALID_ROW;
		//}
	//
		///*
		//vector ix;
		////int rows = trGetN.rows.nVal;
		////int cols = trGetN.cols.nVal;
		//dr.GetData(ix, 0);	
		//if((nMethod == COPY_VECTOR_TO_WHOLE_MATRIX) && (nRows*nCols < ix.GetSize()))
		//{
			////bOKEnable = false;
			//strErrMsg = XFERR_MATRIX_TOO_SMALL;
		//}
		//*/
	//}
	///end NOT_CHECK_OUTPUT_NOW
	
	return true;

}

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

}
//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 v2m_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
