#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	axis_all,
	axis_x,
	axis_y,
	axis_z,
};
//put additional include files here
#include <..\Originlab\graph_utils.h>
#include <event_utils.h>
//put your own support static functions here
#define		AXIS_LIST	"ALL|X|Y|Z"

static void _check_update_axis_list(TreeNode& trGetN)
{
	GraphLayer gl;
	if(okxf_resolve_string_get_origin_object(trGetN.igl.strVal, &gl))
	{
		string 	strCombo;
		if( trGetN.axis.GetAttribute(STR_COMBO_ATTRIB, strCombo) )
		{
			string		strCopy = strCombo;
			update_axis_list(gl, strCombo);
			if(0 != strCombo.CompareNoCase(strCopy))
				trGetN.axis.SetAttribute(STR_COMBO_ATTRIB, strCombo);
		}
	}
}

static int _check_error(TreeNode& trGetN)
{
	GraphLayer gl;
	GraphPage gp;
	okxf_resolve_string_get_origin_object(trGetN.igl.strVal, &gl);
	
	if(!gl.IsValid())
		return CER_INVALID_GRAPH;
	
	vector<int> vnNums;
	okxf_resolve_string_get_origin_object(trGetN.igp.strVal, &gp);
	int nErr = get_indices_from_origin_object_index_range_string(trGetN.dest.strVal, vnNums, gp.Layers.Count());
	//if(CER_NO_ERROR != nErr)
			//strErrMsg = nErr;
	return nErr;
}



void laycopyscale(const GraphPage& igp, const GraphLayer& igl, const string& dest, const int& axis)
{
	//----Jake 05/10/07 ADD_GET_LAYERS_FROM_STRDEST
	//if(	igl && dest )
	//{
		
		//--- Iris 02/15/2007 v8.0563 SUPPORT_GET_Z_SCALE
		/*
		if(0 == axis || 1 == axis)
			layer_copy_axis_scales(igl, dest, AXIS_X);
		
		if(0 == axis || 2 == axis)
			layer_copy_axis_scales(igl, dest, AXIS_Y);
		
		if(0 == axis || 3 == axis)
			layer_copy_axis_scales(igl, dest, AXIS_Z);
		*/	
		
		//string 	strAxisList = AXIS_LIST;
		//string 	strAxis = strAxisList.GetToken(axis, '|');
		//if(0 == strAxis.CompareNoCase("ALL"))
		//{
			//layer_copy_axis_scales(igl, dest);
		//}
		//else
		//{
			//layer_copy_axis_scales(igl, dest, strAxis);
		//}
		//---
		
	//}
	if( (!igl) || (!igp) )
		XF_THROW(CER_INVALID_ARGUMENT);
	
	vector<int> vnLayers;
	int nMax = igp.Layers.Count();
	int nErr = get_indices_from_origin_object_index_range_string(dest, vnLayers, nMax);
	if(CER_NO_ERROR != nErr)
		XF_THROW(nErr);
	
	for(int ii = 0; ii < vnLayers.GetSize(); ii++)
	{
		int index = vnLayers[ii] -1;
		GraphLayer glDest = igp.Layers(index);
		if(glDest.IsValid())
		{
			string 	strAxisList = AXIS_LIST;
			string 	strAxis = strAxisList.GetToken(axis, '|');
			if(0 == strAxis.CompareNoCase("ALL"))
			{
				layer_copy_axis_scales(igl, glDest);
			}
			else
			{
				layer_copy_axis_scales(igl, glDest, strAxis);
			}
		}
	}
	//----end ADD_GET_LAYERS_FROM_STRDEST
}

//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 laycopyscale_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return laycopyscale_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	_check_update_axis_list(trGetN);
	int nErr = _check_error(trGetN);
	if(CER_NO_ERROR != nErr)
	{
		bOKEnable = false;
		strErrMsg = nErr;
	}
	return true;

}

int laycopyscale_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 laycopyscale_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	_check_update_axis_list(trGetN);
}
//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 laycopyscale_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
