#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
#include <..\Originlab\grobj_utils.h>
#include <..\Originlab\GraphObjTools.h>
#include <..\Originlab\QuickStats.h>
/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *------------------------------------------------------------------------------*/
//put your own support static functions here
#include <o8dlg.h>

static	bool	_check_appendwks_event(TreeNode& tr, int nRow, int nType, Dialog& Dlg)
{
	TreeNode trGUI = tr.trGUI;
	if ( !trGUI )
		trGUI = tr;
	trGUI.output.wksName.Show = trGUI.output.appendwks.nVal == 1;
	return true;
}



void addtool_region_stats(const TreeNode& trGUI)
{
	int nErr = AddtoolRegionStats(trGUI);
	if ( nErr < 0 )
	{
		XF_THROW(XFERR_INVALID_GRAPH_LAYER);
	}
	if ( nErr != 0 )
		error_report("addtool_region_stats 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 addtool_region_stats_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return addtool_region_stats_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int addtool_region_stats_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	if ( tr.IsValid() )
	{
		int	nBranchID = 1;
		int	nUserID = GET_USER_DATAID(0);
		if ( 0 == strcmp(lpcszVarName, "trGUI") )
		{
			GETN_USE(tr)
			GETN_OPTION_BRANCH(GETNBRANCH_OPEN)
			GETN_BEGIN_BRANCH(roi, STR_ROI_LABEL)	GETN_ID(nBranchID++)
				GETN_STR(toolname, STR_TOOLNAME, TOOLNAME)	GETN_ID(nUserID++)	GETN_CONTROL_OPTION_BOX(0)
				GETN_COLOR(rectcolor, STR_FILL_COLOR, SYSCOLOR_LTYELLOW)	GETN_COLOR_CHOICE_OPTIONS(COLORLIST_CUSTOM | COLORLIST_SINGLE)	GETN_ID(nUserID++)
				GETN_LIST(shape, _L("Shape"), 0, _L("Rectangle|Circle"))	GETN_ID(nUserID++)
				GETN_BEGIN_BRANCH(shapeProp, _L("Shape Properties"))	GETN_ID(nBranchID++)
					GETN_NUM(xCenter, _L("X Center"), 20)	GETN_ID(nUserID++)
					GETN_NUM(yCenter, _L("Y Center"), 10)	GETN_ID(nUserID++)
					GETN_NUM(width, _L("Width"), 15)		GETN_ID(nUserID++)
					GETN_NUM(height, _L("Height"), 15)		GETN_ID(nUserID++)
				GETN_END_BRANCH(shapeProp)
			GETN_END_BRANCH(roi)
			//GETN_BEGIN_BRANCH(showtop, _L("Show on Center-Top of ROI"))	GETN_ID(nBranchID++)
				//GETN_CHECK(xCenter, _L("X Center"), true)	GETN_ID(nUserID++)
				//GETN_CHECK(yCenter, _L("Y Center"), true)	GETN_ID(nUserID++)
				//GETN_CHECK(n, _L("N"), true)				GETN_ID(nUserID++)
				//GETN_CHECK(mean, _L("Mean"), true)			GETN_ID(nUserID++)
				//GETN_CHECK(sd, _L("SD"), true)				GETN_ID(nUserID++)
				//GETN_CHECK(sum, _L("Sum"), true)			GETN_ID(nUserID++)
			//GETN_END_BRANCH(showtop)
			GETN_BEGIN_BRANCH(output, _L("Output To"))		GETN_ID(nBranchID++)
				GETN_CHECK(script, _L("Script Window"), 1)	GETN_ID(nUserID++)
				GETN_CHECK(reslog, _L("Result Log"), 1)		GETN_ID(nUserID++)
				GETN_CHECK(appendwks, _L("Append to Worksheet"), 1)	GETN_ID(nUserID++)	GETN_OPTION_EVENT(_check_appendwks_event)
				GETN_STR(wksName, _L("Result Worksheet Name"), "[RegionStats]Result")	GETN_ID(nUserID++)
			GETN_END_BRANCH(output)
			GETN_BEGIN_BRANCH(quantities, _L("Quantities"))	GETN_OPTION_BRANCH(GETNBRANCH_OPEN)	GETN_ID(nBranchID++)
				GETN_CHECK(name, _L("Input Legend"), true)	GETN_ID(nUserID++)
				GETN_CHECK(shape, _L("Shape"), false)		GETN_ID(nUserID++)
				//GETN_CHECK(xCenter, _L("X Center"), true)	GETN_ID(nUserID++)
				//GETN_CHECK(yCenter, _L("Y Center"), true)	GETN_ID(nUserID++)
				GETN_CHECK(dpIndices, _L("Data Points' Indices"), false)	GETN_ID(nUserID++)
				GETN_CHECK(dpValues, _L("Data Points' Values"), false)	GETN_ID(nUserID++)
				GETN_CHECK(n, _L("N"), true)				GETN_ID(nUserID++)
				GETN_CHECK(sum, _L("Sum"), true)			GETN_ID(nUserID++)
				GETN_CHECK(mean, _L("Mean"), true)			GETN_ID(nUserID++)
				GETN_CHECK(median, _L("Median"), true)		GETN_ID(nUserID++)
				GETN_CHECK(sd, _L("SD"), true)				GETN_ID(nUserID++)
				GETN_CHECK(min, _L("Minimum"), true)		GETN_ID(nUserID++)
				GETN_CHECK(max, _L("Maximum"), true)		GETN_ID(nUserID++)
			GETN_END_BRANCH(quantities)
		}
	}
	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 addtool_region_stats_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if ( is_xf_startup(nGetNDialog, dwCntrl) )
	{
		if(is_use_origin_chm())
			trGetN.SetAttribute(STR_HELPID_ATTRIB, IDD_XF_addtool_region_stats);
	}
	
	///Sophy 1/29/2010 QA80-14995-P2 CHECK_UPDATE_REPORT_WORKSHEET_NAME_FOR_ROI_TOOLS
	if ( 0 == nGetNDialog || -1 == nGetNDialog )
	{
		if ( !O_QUERY_BOOL(dwCntrl, LTXF_SHOW_DIALOG) )
		{
			string strBookSheetName = trGetN.trGUI.output.wksName.strVal;
			if ( !check_update_worksheet_name(strBookSheetName) )
			{
				trGetN.trGUI.output.wksName.strVal = strBookSheetName; //update report book sheet name
				dwCntrl |= LTXF_SHOW_DIALOG;
			}			
		}
	}
	///end CHECK_UPDATE_REPORT_WORKSHEET_NAME_FOR_ROI_TOOLS
	init_shape_position(trGetN.trGUI);
	
}
//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 addtool_region_stats_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
