#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	method_col2v,
	method_row2v,
	method_m2v,
};
enum{
	direction_row,
	direction_col,
};
#include <GetNBox.h> 
//#include <event_utils.h>
//put additional include files here

//put your own support static functions here
enum{
	COPY_A_COLUMN_TO_VECTOR,
	COPY_A_ROW_TO_VECTOR,
	COPY_ALL_MATRIX_TO_VECTOR,
};

enum{
	BY_ROW,
	BY_COLUMN,
};
static void _init_tree_show(TreeNode& trGetN)
{
	int nMethod = trGetN.method.nVal;
	switch(nMethod)
	{
	case COPY_A_COLUMN_TO_VECTOR:
		trGetN.direction.Show = false;
		break;
	case COPY_A_ROW_TO_VECTOR:
		trGetN.direction.Show = false;
		break;
	case COPY_ALL_MATRIX_TO_VECTOR:
		trGetN.index.Show = false;
		break;	
	}
}
static void _update_tree_show_by_selection(TreeNode& trGetN)
{
	int nMethod = trGetN.method.nVal;
	switch(nMethod)
	{
	case COPY_A_COLUMN_TO_VECTOR:
		trGetN.direction.Show = false;
		trGetN.index.Show = true;
		break;
	case COPY_A_ROW_TO_VECTOR:
		trGetN.direction.Show = false;
		trGetN.index.Show = true;
		break;
	case COPY_ALL_MATRIX_TO_VECTOR:
		trGetN.index.Show = false;
		trGetN.direction.Show = true;
		break;	
	}
}

void m2v(const MatrixObject& im, const int& method, const int& index, const int& direction, vector& ox)
{
	matrixbase& mat = im.GetDataObject();
	
	switch(method)
	{
	case COPY_A_COLUMN_TO_VECTOR:
		int nCols = mat.GetNumCols();
		if(index <= 0 || index > nCols)
			XF_THROW(XFERR_INVALID_COLUMN);
		mat.GetColumn(ox, index-1);
		break;
	case COPY_A_ROW_TO_VECTOR:
		int nRows = mat.GetNumRows();
		if(index <= 0 || index > nRows)
			XF_THROW(XFERR_INVALID_ROW);
		mat.GetRow(ox, index-1);
		break;
	case COPY_ALL_MATRIX_TO_VECTOR:
		if(direction == BY_ROW)
			mat.GetAsVector(ox);
		else
			mat.GetAsVector(ox, 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 m2v_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return m2v_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	if(nEventID == GETNE_ON_INIT)
		_init_tree_show(trGetN);
	
	bool bIsChangingData = lstrcmpi(lpcszNodeName,"method") == 0 ? true:false;
	if(bIsChangingData)
		_update_tree_show_by_selection(trGetN);
	
	MatrixObject mo;
	if(okxf_resolve_string_get_origin_object(trGetN.im.strVal, &mo) && mo.IsValid())
	{
		matrixbase &mat = mo.GetDataObject();
		int nRows = mat.GetNumRows();
		int nCols = mat.GetNumCols();
		int nMethod = trGetN.method.nVal;
		int index = trGetN.index.nVal;
		if((nMethod == COPY_A_COLUMN_TO_VECTOR) && (index <= 0 || index > nCols))
		{
			bOKEnable = false;
			strErrMsg = XFERR_INVALID_COLUMN;
		}
		
		if((nMethod == COPY_A_ROW_TO_VECTOR) && (index <= 0 || index > nRows))
		{
			bOKEnable = false;
			strErrMsg = XFERR_INVALID_ROW;
		}
	}
	return true;

}

int m2v_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 m2v_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	//if(nGetNDialog > 0)
	//	_init_tree_show(trGetN);
}
//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 m2v_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
