#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
#include <GetNBox.h>
#include <image_utils.h>
#include <..\Originlab\matdata_utils.h>
//put your own support static functions here
static bool	_update_auto_white_black(TreeNode& trGetN)
{
	MatrixObject mo;
	if(!okxf_resolve_string_get_origin_object(trGetN.im.strVal, &mo)||!mo.HasData(false))
	{
	    error_report("Invalid Input Matrix Object");
		return false;
	}

	matrixbase& mat = mo.GetDataObject();
	TreeNode trMin = trGetN.min;
	if ( 1 == octree_get_auto_support(&trMin) )
	{
		trMin.dVal = mat.GetMin();
	}
	
	TreeNode trMax = trGetN.max;
	if ( 1 == octree_get_auto_support(&trMax) )
	{
		trMax.dVal = mat.GetMax();
	}
	
	return true;
}

void m2img(const MatrixObject& im, MatrixObject& om, const int& bits, const double& min, const double& max)
{
	
	int nRet;
	
	MatrixPage mpTemp;
	MatrixObject mi2;
	if(im == om)
	{
		
		mpTemp.Create("Origin", CREATE_HIDDEN|CREATE_SET_MISSING_IN_MANAGER );
		MatrixLayer ml = mpTemp.Layers(0);
		mi2 = ml.MatrixObjects(0);
		if(!matobj_copy(mi2,im))
		{
			XF_THROW(MATOBJ_COPY_FAILED);
		}		
	}	
	
	
	if(bits == 8)
	{
		if(!om.SetInternalDataType(FSI_BYTE, OCD_RESET_VIEW))
			XF_THROW(MATOBJ_FAILED_LT_BMP)
	}
	else
	{
		if(!om.SetInternalDataType(FSI_SHORT, OCD_RESET_VIEW))
			XF_THROW(MATOBJ_FAILED_LT_BMP)	
	}		
	
	if(im == om)
	{
		nRet = image_convert_from_data(mi2, om, min, max, bits);
		mpTemp.Destroy();
	}
	else
		nRet = image_convert_from_data(im, om, min, max, bits);	
	if(nRet != 0)
		XF_THROW(nRet);
	
	
	
	return;
}

//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 m2img_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return m2img_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	MatrixObject mi;
	if(!okxf_resolve_string_get_origin_object(trGetN.im.strVal, &mi)||!mi.HasData(false))
	{
		strErrMsg = CER_INVALID_MATOBJ;
		bOKEnable = false;
		return true;
	}
	
	/*
	if(nEventID == GETNE_ON_INIT|| !lstrcmpi(lpcszNodeName, "im"))
	{
		matrixbase& mat = mi.GetDataObject();
		/// EJP 2008-04-07 v8.0839 QA80-11237 ROI_NOT_WORKING_ON_NOT_IMAGE_MATRIX
		///trGetN.min.nVal = mat.GetMin();
		///trGetN.max.nVal = mat.GetMax();
		trGetN.min.dVal = mat.GetMin();
		trGetN.max.dVal = mat.GetMax();
		/// end ROI_NOT_WORKING_ON_NOT_IMAGE_MATRIX
	}
	*/
	
	if(GETNE_ON_THEME == nEventID || !lstrcmpi(lpcszNodeName, "im")|| !lstrcmpi(lpcszNodeName, "min")|| !lstrcmpi(lpcszNodeName, "max"))
	{
		if(!_update_auto_white_black(trGetN))
		{
			strErrMsg = CER_INVALID_MATOBJ;
			bOKEnable = false;
			return true;
		}	
	}
	
	int nMin = trGetN.min.dVal;
	int nMax = trGetN.max.dVal;
	if(nMin > nMax)
	{
		strErrMsg = XFERR_MIN_LARGER_THAN_MAX;
		bOKEnable = false;
	}
	return true;

}

int m2img_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 m2img_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	if(nGetNDialog >= 0)
	{
		if(trGetN.bits.nVal!= 8 && trGetN.bits.nVal!= 12 && trGetN.bits.nVal!= 16)
		{
			nRet = XFEVT_ABORT;
			return;
		}
		
		if(!_update_auto_white_black(trGetN))
			nRet = XFEVT_ABORT;
	
	}
	
	
}
//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 m2img_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
