#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <Operation.h>
#include <Array.h>
//put additional include files here

//put your own support static functions here
#define		PLOT2BIN_XFUNCTION_NAME	"Plot2Bin"
static void	_data_plot_open_bin_wks(DataPlot& dp, int nRecalculate)
{
	if( dp )
	{
		// need to check if plot type is actually statistical
		///Arvin 08/23/07 OPEN_BIN_WKS_NOT_WORK
		//int nPlotType = dp.GetType();
		int nPlotType = dp.GetPlotType();
		///END OPEN_BIN_WKS_NOT_WORK
		BOOL bStatsPlot = (IDM_PLOT_DOT==nPlotType || IDM_PLOT_BOX == nPlotType|| IDM_PLOT_HISTOGRAM_TYPE==nPlotType);
		if( !bStatsPlot )
			return;
		
		XYRange rg;
		if( dp.GetDataRange(rg) )
		{
			OperationBase& opBase = Project.Operations.FindOutgoingOperation(rg, PLOT2BIN_XFUNCTION_NAME);
			if( opBase )
			{
				Operation& op = (Operation&) opBase;
				DataRange dr;
				Datasheet ds;
				op.GetOutput(dr, ds, 1);
				if( ds )
					ds.CheckShowActivate();
			}
			else
			{
				// need to execute Plot2Bin
				string strRange;
				dp.GetRangeString(strRange);
				
				string strReport;
				GraphLayer gl;
				dp.GetParent(gl);
				string strLayer;
				gl.GetRangeString(strLayer);
				okutil_make_valid_c_name(strLayer);
				strLayer = strLayer + " Bins";
				string strPlotName = dp.GetName();
				strPlotName = strPlotName + " Bins";
				okutil_create_complete_range_string_obj(strReport, strLayer, strPlotName, NULL);
				string strLT;
				strLT.Format("%s %s %s -r %d", PLOT2BIN_XFUNCTION_NAME, strRange, strReport, nRecalculate);
				LT_execute(strLT);
			}
		}
	}
}


void OpenBinWks(const int& nRecalculate)
{
	if( Selection.Objects.Count() == 0 )
	{
		GraphLayer gl = Project.ActiveLayer();
		if( gl )
		{
			foreach( DataPlot dp in gl.DataPlots )
			{
				_data_plot_open_bin_wks(dp, nRecalculate);
			}
		}
	}
	else
	{
		// selection may get destroyed during the Plot2Bin execution
		// so we have to get it first
		Array<OriginObject&> arrSelection;
		arrSelection.SetAsOwner(TRUE);
		foreach( OriginObject obj in Selection.Objects )
		{
			OriginObject* pObj = new OriginObject;
			*pObj = obj;
			arrSelection.Add(*pObj);
		}
		
		for( int ii = 0; ii < arrSelection.GetSize(); ii++ )
		{
			GroupPlot group;
			group = arrSelection.GetAt(ii);
			if( group )
			{
				int nCount = group.GetCount();
				for( int jj = 0; jj < nCount; jj++ )
				{
					DataPlot dp = group.GetDataPlot(jj);
					_data_plot_open_bin_wks(dp, nRecalculate);	
				}
			}
			else
			{
				DataPlot dp;
				dp = arrSelection.GetAt(ii);
				_data_plot_open_bin_wks(dp, nRecalculate);	
			}
		}
	}
}

//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 OpenBinWks_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return OpenBinWks_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int OpenBinWks_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 OpenBinWks_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 OpenBinWks_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
