#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	layout_cline,
	layout_vline,
	layout_hline,
	layout_separate,
};
//put additional include files here
#include <..\originlab\grobj_utils.h>
#include <..\originlab\graph_utils.h>
#include <..\originlab\ImageProfile.h>
///Sophy 5/31/2010 ORG-98-S11 REMINDER_MESSAGE_TO_CONVERT_GRAY_ON_IMAGE_PROFILE
#include <image_utils.h>
#include <..\Originlab\matdata_utils.h>
enum {
	LAYER_NO_IMAGE = 0,
	LAYER_COLOR_IMAGE,
	LAYER_GRAY_IMAGE,
};

static	MatrixObject	_get_active_matrix()
{
	MatrixObject mo;
	MatrixLayer	ml = Project.ActiveLayer();
	if ( ml )
	{
		mo = ml.MatrixObjects();
	}
	return mo;
}
static	int	_get_active_matrix_image_type()
{
	MatrixObject mo = _get_active_matrix();
	if ( !mo || !mo.HasImage() )
		return LAYER_NO_IMAGE;
	if ( !is_image_grayscale(mo) )
		return LAYER_COLOR_IMAGE;
	
	return LAYER_GRAY_IMAGE;
}
///end REMINDER_MESSAGE_TO_CONVERT_GRAY_ON_IMAGE_PROFILE
//put your own support static functions here
typedef BOOL (*BOOL_FUNC_INI_INT)(int nMsg, int nLayoutMode = IMAGE_PROFILE_LAYOUT_SAME_GRAPH_CLINE, DWORD dwOptions = IPROFILE_PROTECT_SOURCE);
//CROSSED_LINES_IMAGE_PROFILE


void imageprofile(const int& layout, const int& protect)
{
	GraphLayer gl = Project.ActiveLayer();
	if( !is_good_layer_for_profile(gl) )
		XF_THROW(XFERR_INVALID_PLOT_FOR_PROFILE);
	
	int nLayoutMode;
	if( layout_separate == layout )
		nLayoutMode = IMAGE_PROFILE_LAYOUT_SEPARATE_GRAPH;
	else
		nLayoutMode = layout;
	
	DWORD dwOptions = 0;
	if( protect )
		dwOptions = IPROFILE_PROTECT_SOURCE;
		
	BOOL_FUNC_INI_INT pfn = (BOOL_FUNC_INI_INT)Project.FindFunction("ImageProfile", NULL, true);
	if(pfn)
	{
		pfn(OMSG_OPEN, nLayoutMode, dwOptions);
	}	
}

//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 imageprofile_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	///Sophy 5/31/2010 ORG-98-S11 REMINDER_MESSAGE_TO_CONVERT_GRAY_ON_IMAGE_PROFILE
	if ( _get_active_matrix_image_type() == LAYER_COLOR_IMAGE )
	{
		int nChoice = warning_msg_box(CWARN_IMAGE_SHOULD_ANALYSIS_ON_GRAY_SCALE, true, 'w', MB_YESNO);
		if ( IDYES == nChoice )
		{
			image_to_gray(_get_active_matrix(), 8);
			LT_execute("run.section(Plot3D,ImagePlot);");
		}
		else
			nRet = XFEVT_ABORT;
	}
	///end REMINDER_MESSAGE_TO_CONVERT_GRAY_ON_IMAGE_PROFILE
}
