#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	shape_ellipse,
	shape_rect,
};
//put additional include files here
#include <GetNBox.h> 
#include <image_utils.h>
//#include <..\originlab\ImgChannelData.h>
#include <Nag_utils.h>
#include <..\Originlab\matdata_utils.h>
#include <..\originlab\grobj_utils.h>
#include <..\originlab\gridding_utils.h>
#include <..\originlab\ocImgRoiPreview.h>
#include <..\originlab\ocimg2xyzdata.h>
//put your own support static functions here

///temp add by Sandy
#include <xfgraph_utils.h>

//#define IMAGE_COPY_PAGE_NAME "Data2ImageCopy"

//////////////////////////////////////////////////////
//static OCImg2XYZData* pImg2xyz = NULL;
//static DWORD dwImg2XYZ = 0;
int _events_from_img2xyz_go()
{
	//OCImg2XYZData* pImg2xyz = (OCImg2XYZData*)dwImg2XYZ;
	//pImg2xyz->UpdatePreviewData();
	
	/// Iris 12/28/2006 v8.0533 CALL_EVENT1_ON_GRAPH_OBJ_MOVING
	post_getn_dialog_message(WM_USER_ON_GRAPH_OBJECT_CHANGE, 0);
	/// end CALL_EVENT1_ON_GRAPH_OBJ_MOVING
	return 0;
}
//
//static int _create_image_copy(MatrixObject& imgCopy, MatrixObject& mData)
//{
	//MatrixLayer mlParent;
	//mData.GetParent(mlParent);
	//
	//MatrixPage mp = mlParent.GetPage();
	//int nLay = mp.AddLayer(IMAGE_COPY_PAGE_NAME);
	//
	//MatrixLayer mlImg = mp.Layers(nLay);
	//imgCopy = mlImg.MatrixObjects(0);	
	//
	//if(!imgCopy.SetInternalDataType(FSI_SHORT, OCD_RESET_VIEW))
			//return MATOBJ_FAILED_LT_BMP;
	//
	//matrixbase& mat = mData.GetDataObject();
	//double min = mat.GetMin();
	//double max = mat.GetMax();
	//
	//imgCopy.Show();
	//
	//return image_convert_from_data(mData, imgCopy, min, max, 8);
//
//}

void img2xyz(MatrixObject& im, const int& shape, XYZRange& xyz, MatrixObject& om, TreeNode& trROI, int& p)
{
	
	OCImg2XYZData* pImg2xyz = (OCImg2XYZData*)p;
	
	if(om.IsValid())
		pImg2xyz->GetROIData(om);
	pImg2xyz->CropROI(xyz);	
	
}

//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 img2xyz_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return img2xyz_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	/// Iris 01/04/2007 v8.0536 FIX_AFTER_SAVEAS_THEME_MOVE_GRAPH_OBJ_FAIL_TO_ENTER_EVENT1
	if(GETNE_ON_INIT == nEventID)
	{
		trGetN.SetAttribute(STR_ATTRIB_POST_USER_MESSAGE, 1);
	}
	///end FIX_AFTER_SAVEAS_THEME_MOVE_GRAPH_OBJ_FAIL_TO_ENTER_EVENT1	
	
	///Jasmine 08/28/08 QA80-12100 XF_IN_XFBAR_MAY_CLOSE_AFTER_PAGE_DESTROY
	OCImg2XYZData* pImg2xyz = (OCImg2XYZData*)(trGetN.p.nVal);
	
	/// Max 8/27/08
	if(GETNE_ON_CLOSE == nEventID)
	{
		
		delete pImg2xyz;
		return true;
	}
	///End XF_IN_XFBAR_MAY_CLOSE_AFTER_PAGE_DESTROY
	
	MatrixObject mimg;
	if(!okxf_resolve_string_get_origin_object(trGetN.im.strVal, &mimg) )	
		return false;
	
	if(!mimg.IsValid())
		return false;
	
	
	/// Iris 12/28/2006 v8.0533 CALL_EVENT1_ON_GRAPH_OBJ_MOVING
	if(GETNE_ON_GRAPH_OBJECT_CHANGE == nEventID)
	{
		pImg2xyz->UpdatePreviewData();
		pImg2xyz->SaveAnchor(trGetN.trROI);
		//printf("Graph Object moved or resized\n");
		
		return true;		
	}
	///end CALL_EVENT1_ON_GRAPH_OBJ_MOVING
	
	
	if(!lstrcmpi(lpcszNodeName, "shape"))
	{
		pImg2xyz->UpdateAnchorShape(trGetN.shape.nVal);
		pImg2xyz->SaveAnchor(trGetN.trROI);
	}
	
	/// Max 8/27/08 variable name is changed to "im"
	// if(GETNE_ON_INIT == nEventID || !lstrcmpi(lpcszNodeName, "mi") )//!lstrcmpi(lpcszNodeName, "")) 
	if(GETNE_ON_INIT == nEventID || !lstrcmpi(lpcszNodeName, "im") )//!lstrcmpi(lpcszNodeName, ""))
	{
		
		trGetN.nr.Show = false;
		trGetN.trROI.Show = false;
		trGetN.p.Show = false;
		if(pImg2xyz!= 0)
			delete pImg2xyz;
		
		if(mimg.HasData(false) || !is_image_grayscale(mimg))
		{
			strErrMsg = CWARN_IMAGE_SHOULD_ANALYSIS_ON_GRAY_SCALE;
		}
		
		pImg2xyz = new OCImg2XYZData(mimg, IMG2XYZ_PREVIEW_DATA);
		trGetN.p.nVal = (DWORD)pImg2xyz;
		
		int nc, nr;
		nc = mimg.GetNumCols();
		nr = mimg.GetNumRows();
		if(nr<=4||nc<=4)
		{
			strErrMsg = CERR_TOO_SMALL_MATRIX_SIZE;
			bOKEnable = false;
			return false;
		}
		
	}
	
	/// Max 8/27/08 variable name is changed to "im"
	// if(nEventID == GETNE_ON_THEME || GETNE_ON_INIT == nEventID || !lstrcmpi(lpcszNodeName, "mi") )
	if(nEventID == GETNE_ON_THEME || GETNE_ON_INIT == nEventID || !lstrcmpi(lpcszNodeName, "im"))
	{
	
		pImg2xyz->InitAnchor(trGetN.trROI);
		trGetN.trROI.SetAttribute(STR_ATTRIB_TEMPORY_TREENODE, true); ///Joseph 12/29/06 RESET_TREENODE_VARIABLE_ON_THEME_FACTORY_DEFAULT
		pImg2xyz->SetupGraphPreview();		
	}
	
	pImg2xyz->UpdatePreviewData();
	//
	//if(GETNE_ON_APPLY == nEventID )
					
	//if(nEventID == GETNE_ON_APPLY)
	//{
	//
		//return PEVENT_GETN_RET_TO_CLOSE;
	//}	
	return true;

}

int img2xyz_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	string strVarName = lpcszVarName;
	
	if ( "trROI" == strVarName )
	{
		make_anchors_tree_without_info(tr);
		return 0;
	}
	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 img2xyz_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 img2xyz_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
