#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	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	"X|Y|Z"
//------ Folger 08/08/07 GROUP_OPERATION_AS_BLOCK_TO_UNDO_ONCE
#define BEFORE_UNDO_BLOCK				int idBefore = Project.UndoBlockId(); { UndoBlock block;
#define AFTER_UNDO_BLOCK				} int idAfter = Project.UndoBlockId();
//------ End GROUP_OPERATION_AS_BLOCK_TO_UNDO_ONCE

//---Jake 05/17/07 ADD_ERROR_CHECKING
static int _check_error(TreeNode& trGetN)
{
	GraphPage gp;
	okxf_resolve_string_get_origin_object(trGetN.igp.strVal, &gp);
	if(!gp.IsValid())
		return CER_INVALID_GRAPH;

	vector<int> vnNums;
	int nErr = get_indices_from_origin_object_index_range_string(trGetN.layer.strVal, vnNums, gp.Layers.Count());

	return nErr;
}
//end ADD_ERROR_CHECKING


void laysetscale(const GraphPage& igp, const string& layer, const int& axis, const double& from, const double& to, const double& inc, const int& type)
{
	//---- Jake 04/14/07 ADD_GET_LAYERS
	if( !igp )
		XF_THROW(CER_INVALID_ARGUMENT);
	//----end ADD_GET_LAYERS
	
	///Kyle 08/15/08 QA80-12022 ADD_INC_AND_TYPE_TO_FUNCTION
	string strLayer = layer;
	if(strLayer.CompareNoCase(STR_ACTIVE) == 0)
	{
		GraphLayer gl = Project.ActiveLayer();
		if(gl)
		{
			int nIndex = gl.GetIndex() +1;
			strLayer = nIndex;
		}
	}
	///End ADD_INC_AND_TYPE_TO_FUNCTION
	
	/*	
	//---Iris 02/15/2007 v8.0562 GOT_RUNTIMEERR_IF_NOT_CHECK_UNIT
	//axis_set_sacle(layer, axis, from, to);
	string	strAxisList = AXIS_LIST;
	string	strAxis = strAxisList.GetToken(axis, '|');		
	axis_set_sacle(layer, strAxis, from, to);
	//---
	*/
	string	strAxisList = AXIS_LIST;
	string	strAxis = strAxisList.GetToken(axis, '|');	
	vector<int> vnLayers;
	int nMax = igp.Layers.Count();
	///Kyle 08/15/08 QA80-12022 ADD_INC_AND_TYPE_TO_FUNCTION 
	//int nErr = get_indices_from_origin_object_index_range_string(layer, vnLayers, nMax);
	int nErr = get_indices_from_origin_object_index_range_string(strLayer, vnLayers, nMax);
	///End ADD_INC_AND_TYPE_TO_FUNCTION
	if(nErr != CER_NO_ERROR)
		XF_THROW(nErr);
	
	BEFORE_UNDO_BLOCK	//------ Folger 08/08/07 GROUP_OPERATION_AS_BLOCK_TO_UNDO_ONCE
	for(int ii = 0; ii < vnLayers.GetSize(); ii++)
	{
		int nIndex = vnLayers[ii] -1;
		GraphLayer glDest = igp.Layers(nIndex);
		if(glDest.IsValid())
		{
			///Kyle 08/15/08 QA80-12022 ADD_INC_AND_TYPE_TO_FUNCTION
			//axis_set_sacle(glDest, strAxis, from, to);
			axis_set_sacle(glDest, strAxis, from, to, inc);
			if(type>0)
				axis_set_scale_type(glDest, strAxis, type-1);
			///End ADD_INC_AND_TYPE_TO_FUNCTION
		}
	}
	AFTER_UNDO_BLOCK	//------ Folger 08/08/07 GROUP_OPERATION_AS_BLOCK_TO_UNDO_ONCE
}

//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 laysetscale_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	//GraphLayer gl;
	//if(okxf_resolve_string_get_origin_object(trGetN.layer.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);
		//}
	//}
	///Jake 06/21/07 GET_ACTIVE_LAYER_INDEX	
	if(trGetN.layer.strVal.CompareNoCase(STR_ACTIVE) == 0)
	{
	GraphLayer layer = Project.ActiveLayer();
	if(layer)
	{
		int nIndex = layer.GetIndex() +1;
		trGetN.layer.strVal = nIndex;
	}
	}
	///end GET_ACTIVE_LAYER_INDEX
}
