#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	unit_page,
	unit_inch,
	unit_cm,
	unit_mm,
	unit_pixel,
	unit_point,
};
//put additional include files here
#include <..\Originlab\graph_utils.h>
#include <event_utils.h>
//put your own support static functions here
//static void _gui_check_correct_options(TreeNode& trGetN)
//{
	//GraphLayer gl;
	//if(okxf_resolve_string_get_origin_object(trGetN.layer.strVal, &gl))
	//{
		//string 	strCombo;
		//if( trGetN.unit.GetAttribute(STR_COMBO_ATTRIB, strCombo) )
		//{
			//char	chSep = '|';
			//string 	strLinked = "% of Linked Layer";
			//int 	nFind = strCombo.Find(strLinked);
			//string	strBackup = strCombo;
			//if(nFind >= 0 && !is_linked_layer(gl))
			//{
				//strCombo.Delete(nFind, lstrlen(strLinked));
				//strCombo.TrimRight(chSep);
			//}
			//
			//if(nFind < 0 && is_linked_layer(gl))
				//strCombo += (string)chSep + strLinked;
			//
			//if(0 != strBackup.CompareNoCase(strCombo)) //Combo changed
				//trGetN.unit.SetAttribute(STR_COMBO_ATTRIB, strCombo);
		//}
		//
		//if( trGetN.unit.nVal < 0)
		//{
			//double dPos[TOTAL_POS];
			//trGetN.unit.nVal = gl.GetPosition(dPos);
		//}
	//}
//}

//---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 laysetunit(const GraphPage& igp, const string& layer, const int& unit)
{
	//---- Jake 05/14/07 ADD_GET_LAYERS
	if(!igp)
		XF_THROW(CER_INVALID_ARGUMENT);
	
	//--- end ADD_GET_LAYERS
	vector<int> vnLayers;
	int nMax = igp.Layers.Count();
	int nErr = get_indices_from_origin_object_index_range_string(layer, vnLayers, nMax);
	if(nErr != CER_NO_ERROR)
		XF_THROW(nErr);
		
	for(int ii = 0; ii < vnLayers.GetSize(); ii++)
	{
		int nIndex = vnLayers[ii] -1;
		GraphLayer glDest = igp.Layers(nIndex);
		if(glDest && (M_LINK > unit || is_linked_layer(glDest)))
		{
			double dPos[TOTAL_POS];
			glDest.GetPosition(dPos);
			glDest.UnitsConvert(unit, dPos);
			glDest.SetPosition(dPos, unit);
		}
	}
}

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

}

int laysetunit_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 laysetunit_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	//_gui_check_correct_options(trGetN);
	if(trGetN.layer.strVal.CompareNoCase(STR_ACTIVE) == 0)
	{
	GraphLayer layer = Project.ActiveLayer();
	if(layer)
	{
		int nIndex = layer.GetIndex() +1;
		trGetN.layer.strVal = nIndex;
	}
	}
}
//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 laysetunit_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
