#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
#include <GetNBox.h> 
#include <image_utils.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\ImgBackgroundData.h>
#include <..\originlab\OCImgInterpBgData.h>

///temp add by Sandy
#include <xfgraph_utils.h>
//put your own support static functions here

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 08/05/09 QA80-14056 IMPROVE_SUBTRACT_INTERP_BG_ERROR_CHECKING		*
 *------------------------------------------------------------------------------*/


int _events_from_control_go()
{
	
	post_getn_dialog_message(WM_USER_ON_GRAPH_OBJECT_CHANGE, 0);
	return 0;
}



void imgSubtractInterpBG(const MatrixObject& im, const int& npts, const int& bg, MatrixObject& om, const int& shape, const int& al, const int& adj_bg, TreeNode& trROI, int& p)
{
	OCImgInterpBgData* pBGData = (OCImgInterpBgData*)p;
	pBGData->GetSubtractedResult(om);
	
}

//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 imgSubtractInterpBG_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return imgSubtractInterpBG_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	///Jasmine 08/28/08 QA80-12100 XF_IN_XFBAR_MAY_CLOSE_AFTER_PAGE_DESTROY
	OCImgInterpBgData* pBGData = (OCImgInterpBgData*)(trGetN.p.nVal);
	
	if(GETNE_ON_CLOSE == nEventID)
	{
		
		delete pBGData;
		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;
	
	///------ Folger 08/05/09 QA80-14056 IMPROVE_SUBTRACT_INTERP_BG_ERROR_CHECKING
	if(mimg.HasData(false))
	{
		strErrMsg = CER_NOT_SUPPORT_DATA_AND_12BIT_IMAGE;
		bOKEnable = false;
		return true;			
	}
	
	Image img(mimg);
	pBITMAPHANDLE pBmp = img.GetLBmp();
	int nBits = pBmp->BitsPerPixel;
	if(!is_image_grayscale(mimg)||nBits == 12)
	{
		strErrMsg = CER_NOT_SUPPORT_DATA_AND_12BIT_IMAGE;
		bOKEnable = false;
		return true;
	}
	///------ End IMPROVE_SUBTRACT_INTERP_BG_ERROR_CHECKING	
	
	if(GETNE_ON_GRAPH_OBJECT_CHANGE == nEventID)
	{
		pBGData->UpdatePreviewData();
		pBGData->SaveAnchors(trGetN.trROI);
		//printf("Graph Object moved or resized\n");
		
		return true;		
	}
	
	
		/// 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	
	
	
	if(!lstrcmpi(lpcszNodeName, "npts")||!lstrcmpi(lpcszNodeName, "shape"))
	{
		//pBGData->EditAnchors(trGetN.npts.nVal,trGetN.nr.nVal, trGetN.shape.nVal);
		pBGData->EditAnchors(trGetN.npts.nVal, trGetN.shape.nVal, trGetN.trROI);
		pBGData->SaveAnchors(trGetN.trROI);
	}
	
	if(GETNE_ON_INIT == nEventID || !lstrcmpi(lpcszNodeName, "im") )//!lstrcmpi(lpcszNodeName, ""))
	{
		
		trGetN.trROI.Show = false;	
		trGetN.p.Show = false;	
		
		
		if(mimg.HasData(false))
		{
			//strErrMsg = CWARN_IMAGE_SHOULD_ANALYSIS_ON_GRAY_SCALE;
			trGetN.bg.Show = false;
			trGetN.al.Show = false;
		}
		else
		{
			trGetN.bg.Show = true;
			trGetN.al.Show = true;			
		}
		///------ Folger 08/05/09 QA80-14056 IMPROVE_SUBTRACT_INTERP_BG_ERROR_CHECKING
		//if(mimg.HasData(false))
		//{
			//strErrMsg = CER_NOT_SUPPORT_DATA_AND_12BIT_IMAGE;
			//bOKEnable = false;
			//return true;			
		//}
		////if(!mimg.HasData(false) && !is_image_grayscale(mimg))
		//Image img(mimg);
		//pBITMAPHANDLE pBmp = img.GetLBmp();
		//int nBits = pBmp->BitsPerPixel;
		//if(!is_image_grayscale(mimg)||nBits == 12)
		//{
			//strErrMsg = CER_NOT_SUPPORT_DATA_AND_12BIT_IMAGE;
			//bOKEnable = false;
			//return true;
		//}
		///------ End IMPROVE_SUBTRACT_INTERP_BG_ERROR_CHECKING
		if(pBGData!= 0)
			delete pBGData;
		
		pBGData = new OCImgInterpBgData(mimg, IMGSIB_PREVIEW_DATA);
		trGetN.p.nVal = (DWORD)pBGData;		
	
	}
	
	if(GETNE_ON_INIT == nEventID || !lstrcmpi(lpcszNodeName, "im") || GETNE_ON_THEME == nEventID)
	{
		pBGData->InitAnchors(trGetN.trROI);
		trGetN.trROI.SetAttribute(STR_ATTRIB_TEMPORY_TREENODE, 1);
		
		pBGData->SetupGraphPreview();		
	}
	
	pBGData->SetBGBrightness(trGetN.adj_bg.nVal);
	pBGData->SetApplyAutoLevel(trGetN.al.nVal);
	pBGData->SetHiBG(trGetN.bg.nVal);
	
	bOKEnable = pBGData->UpdatePreviewData();
	pBGData->SaveAnchors(trGetN.trROI);
	
	//if(nEventID == GETNE_ON_APPLY)
	//{
		//return PEVENT_GETN_RET_TO_CLOSE;
	//}	
	
	return true;

}

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