#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	scope_plot,
	scope_layer,
	scope_page,
};

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 * Kenny 7/28/2009 QA80-14027 IMPROVE_XF_GET_PLOT_SEL_TO_SUPPORT_PLOTS_IN_DIFFERENT_SCOPE*
 * Kenny 8/4/2009 IMPROVE_ERROR_CHECKING_ONLY_ALLOWED_RUN_ON_ACTIVE_GRAPH		*
 *------------------------------------------------------------------------------*/

//put additional include files here

//put your own support static functions here

/// Kenny 7/28/2009 QA80-14027 IMPROVE_XF_GET_PLOT_SEL_TO_SUPPORT_PLOTS_IN_DIFFERENT_SCOPE
static bool _get_plot_ranges_to_str(DataPlot& dp, string& str, const string& sep)
{
	str.Empty();
	if ( dp.IsValid() )
	{
		vector<int> vBegin;
		vector<int> vEnd;
		int nCount = dp.GetDataMarkers(vBegin, vEnd);
		if( nCount <= 0 )
			return false;
		
		string strPlotRangeString;
		dp.GetRangeString(strPlotRangeString);

		string strRowRangeString;
		for( int ii = 0; ii < nCount; ii++ )
		{
			// use LT offset of 1
			okutil_create_row_range_string(&strRowRangeString, vBegin[ii]+1, vEnd[ii]+1);
			str += strPlotRangeString;
			str += strRowRangeString;
			if( ii < nCount - 1 )
				str += sep;
		}
		return true;
	}
	return false;
}

static bool _get_layer_plot_ranges_to_str(GraphLayer& gl, string& str, const string& sep)
{
	if ( gl.IsValid() )
	{
		foreach( DataPlot dp in gl.DataPlots )
		{
			string strPlotRange;
			bool bRet = _get_plot_ranges_to_str(dp, strPlotRange, sep);
			if ( bRet && !strPlotRange.IsEmpty() )
			{
				if( dp.GetIndex() != 0 )
					str += sep;
				str += strPlotRange;
			}
		}
		return true;
	}
	return false;
}

static bool _get_page_plot_ranges_to_str(GraphPage& gp, string& str, const string& sep)
{
	if ( gp.IsValid() )
	{
		foreach( GraphLayer gl in gp.Layers )
		{
			string strLayerPlotRange;
			bool bRet = _get_layer_plot_ranges_to_str(gl, strLayerPlotRange, sep);
			if ( bRet && !strLayerPlotRange.IsEmpty() )
			{
				if( gl.GetIndex() != 0 )
					str += sep;
				str += strLayerPlotRange;
			}
		}
		return true;
	}
	return false;
}
/// End QA80-14027 IMPROVE_XF_GET_PLOT_SEL_TO_SUPPORT_PLOTS_IN_DIFFERENT_SCOPE


void get_plot_sel(string& str, const string& sep, const int& scope)
{
	str.Empty();
	GraphLayer gl = Project.ActiveLayer();
	/// Kenny 7/28/2009 QA80-14027 IMPROVE_XF_GET_PLOT_SEL_TO_SUPPORT_PLOTS_IN_DIFFERENT_SCOPE
	//if( gl )
	//{
		//vector<int> vBegin;
		//vector<int> vEnd;
		//foreach( DataPlot dp in gl.DataPlots )
		//{
			//int nCount = dp.GetDataMarkers(vBegin, vEnd);
			//if( nCount <= 0 )
				//continue;
			//
			//string strPlotRangeString;
			//dp.GetRangeString(strPlotRangeString);
			//string strRowRangeString;
			//for( int ii = 0; ii < nCount; ii++ )
			//{
				//// use LT offset of 1
				//okutil_create_row_range_string(&strRowRangeString, vBegin[ii]+1, vEnd[ii]+1);
				//str += strPlotRangeString;
				//str += strRowRangeString;
				//if( ii < nCount - 1 )
					//str += sep;
			//}
		//}
	//}
	/// Kenny 8/4/2009 IMPROVE_ERROR_CHECKING_ONLY_ALLOWED_RUN_ON_ACTIVE_GRAPH
	if ( !gl )
	{
		XF_THROW(CER_INVALID_PAGE);
		return;
	}
	/// End IMPROVE_ERROR_CHECKING_ONLY_ALLOWED_RUN_ON_ACTIVE_GRAPH
	DataPlot dp = gl.DataPlots();
	GraphPage gp = gl.GetPage();
	
	bool bRet = false;
	switch( scope )
	{
	case scope_plot:
		bRet = _get_plot_ranges_to_str(dp, str, sep);
		break;
	case scope_layer:
		bRet = _get_layer_plot_ranges_to_str(gl, str, sep);
		break;
	case scope_page:
		bRet = _get_page_plot_ranges_to_str(gp, str, sep);
		break;
	default:
		XF_THROW(CER_UNKNOWN_ERROR);
		ASSERT(FALSE);
		break;
	}
	/// End QA80-14027 IMPROVE_XF_GET_PLOT_SEL_TO_SUPPORT_PLOTS_IN_DIFFERENT_SCOPE
}

//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 get_plot_sel_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
