#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <page_utils.h>
#include <event_utils.h>
//put additional include files here

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 10/15/09 QA81-13904-P3 SET_MATRIX_XY_COORDINATES_FAILS_TO_UNDO		*
 *------------------------------------------------------------------------------*/

//#define CER_IMG_NO_RESET_DIM	"Dimension of image can not be reset"
//put your own support static functions here
/*
static bool _has_not_image(const MatrixLayer& mlay)
{
	if(has_image(mlay))
		return false;
	
	return true;
}
*/
/*
static bool _check_reset_to_matrix(TreeNode& trGetN, const MatrixLayer& mlay, string& strErrMsg)
{
	if(!mlay.IsValid())
		return false;
	
	int nCols, nRows;
	if(has_image(mlay))
	{
		///Echo 10/31/06 WARN_MSG_FOR_IMG

		//trGetN.cols.Show = false;
		//trGetN.rows.Show = false;

		trGetN.cols.Enable = false;
		trGetN.rows.Enable = false;
		strErrMsg = CER_IMG_NO_RESET_DIM;
		///end WARN_MSG_FOR_IMG
	}
	//else
	//{
		nCols = mlay.GetNumCols();
		nRows = mlay.GetNumRows();
		trGetN.cols.nVal = nCols;
		trGetN.rows.nVal = nRows;

	//}
	
	double xMin, xMax, yMin, yMax;
	MatrixObject mo = mlay.MatrixObjects();
	mo.GetXY(xMin, yMin, xMax, yMax);
	trGetN.xmin.dVal = xMin;
	trGetN.xmax.dVal = xMax;
	trGetN.ymin.dVal = yMin;
	trGetN.ymax.dVal = yMax;
	return true;
}
*/
static void _initial_tree_show(TreeNode& trGetN, string& strErrMsg)
{
	trGetN.ms.Show = false;
	
	MatrixLayer ml;
	if(okxf_resolve_string_get_origin_object(trGetN.ms.strVal, &ml) && ml.IsValid() )
	{
		if(has_image(ml))
		{
			trGetN.cols.Enable = false;
			trGetN.rows.Enable = false;
			strErrMsg = CER_IMG_NO_RESET_DIM;
		}
	}
}

static void _initial_dimensions(TreeNode& trGetN)
{
	MatrixLayer ml;
	if( okxf_resolve_string_get_origin_object(trGetN.ms.strVal, &ml) && ml.IsValid() )
	{

		MatrixObject mo = ml.MatrixObjects();
		if(mo.IsValid())
		{	
			set_value_for_auto_and_unassigned(trGetN.cols, mo.GetNumCols());
			set_value_for_auto_and_unassigned(trGetN.rows, mo.GetNumRows());
			
			double dxMin, dxMax, dyMin, dyMax;
			mo.GetXY(dxMin, dyMin, dxMax, dyMax);
			set_value_for_auto_and_unassigned(trGetN.x1, dxMin);
			set_value_for_auto_and_unassigned(trGetN.x2, dxMax);
			set_value_for_auto_and_unassigned(trGetN.y1, dyMin);
			set_value_for_auto_and_unassigned(trGetN.y2, dyMax);
		}
	}	
}

void mdim(MatrixLayer& ms, const int& cols, const int& rows, const double& x1, const double& x2, const double& y1, const double& y2)
{
	
	if(!ms)
		XF_THROW(XFERR_INVALID_MATOBJ);
	/*
	if(!_is_not_image(ms))
		XF_THROW(XFERR_SET_IMAGE_DIMENSION);
	*/
	if(!has_image(ms))
	{
		if(!ms.SetSize(-1, rows, cols))
			XF_THROW(XFERR_SET_DIMENSION_FAILED);
	}
	
	MatrixObject mo = ms.MatrixObjects(); // CPY 9/20/06, we have to use MatrixObject for this for now
	///------ Folger 10/15/09 QA81-13904-P3 SET_MATRIX_XY_COORDINATES_FAILS_TO_UNDO
	//if(!mo.SetXY(x1, y1, x2, y2))
	if(!mo.SetXY(x1, y1, x2, y2, TRUE))
	///------ End SET_MATRIX_XY_COORDINATES_FAILS_TO_UNDO
		XF_THROW(XFERR_SET_DIMENSION_FAILED);
		
}

//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 mdim_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return mdim_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	if(GETNE_ON_INIT == nEventID)
	/*
	{
		MatrixLayer mlay;
		string strMsheet=trGetN.mi.strVal;	
		bOKEnable = okxf_resolve_string_get_origin_object(strMsheet, &mlay);
		//return _check_reset_to_matrix(trGetN, mlay);///Echo 10/31/06 WARN_MSG_FOR_IMG
		return _check_reset_to_matrix(trGetN, mlay, strErrMsg);
	
		_initial_tree_show(trGetN, strErrMsg);
	}
	*/
	_initial_tree_show(trGetN, strErrMsg);
	return false;

}

int mdim_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return -1;	
}
//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 mdim_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	/*
	trGetN.mi.Show = false;
	if(nGetNDialog >= XF_GETN_SIMPLE) // before dialog is open
	{
		MatrixLayer mlay;
		string strMsheet=trGetN.mi.strVal;	
		if(okxf_resolve_string_get_origin_object(strMsheet, &mlay))
		{
			if(has_image(mlay))
			{
				///Echo 10/31/06 WARN_MSG_FOR_IMG
	
				//trGetN.cols.Show = false;
				//trGetN.rows.Show = false;
	
				trGetN.cols.Enable = false;
				trGetN.rows.Enable = false;
				///end WARN_MSG_FOR_IMG
			}
		}
	}
	*/
	_initial_dimensions(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 mdim_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
