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

//put your own support static functions here

typedef void (*PFNEQ) (OriginObject& oo, int nAnchorID, double dd, TreeNode& trUserInfo);

static int OnInitMarker(OriginObject& oo, MarkerInfo& mi, TreeNode& trUserData)
{
	///Sandy 2007-1-18 support initialize with position
	vector vx={0.5}, vy = {0.5};
//---- CPY 2/14/07 SLIDER_POSITION
//	if(trUserData.percent)
//		vy[0] = vx[0] = trUserData.percent.dVal;
	if(trUserData.Position)
		vy[0] = vx[0] = trUserData.Position.dVal;
//---

	//end 
	
	mi.SetWidth(0);
	mi.Symbol.Size.dVal = 12;
	mi.Symbol.Shape.nVal = 1;		// square
	mi.Symbol.Interior.nVal = 0;	// solid
	mi.Symbol.EdgeColor.nVal = 1;	// red
	mi.FollowParent.nVal = 1;
	mi.Data.X.dVals = vx;
	mi.Data.Y.dVals = vy;
	// test changing user data
	trUserData.Test.nVal = 123;
	return OCD_MODIFY|OCD_REPAINT;
}

static int OnEditMarker(OriginObject& oo, MarkerInfo& mi, TreeNode& trUserData)
{
	vector vx;
	vx = mi.Data.X.dVals;
	//string strName = oo.Info.Slider.Handler.Name$;
	//string strPath = oo.Info.Slider.Handler.Path$;
	if(trUserData.Handler)
	{
		//out_tree(trUserData);
		Function fn = Project.FindFunction(trUserData.Handler.strVal, "*"); // we have to assume func already loaded, as typically from XF and path might be difficult to get
		PFNEQ pfn = fn;
		if( pfn )
		{
			pfn(oo, mi.GetID(), vx[0], trUserData.Info);
			return 0;
		}
	}
	printf("callback not found, id=%d, pos = %f\n", mi.GetID(), vx[0]);
	return 0;
}


int gslider(EventInfo& ei, MarkerInfo& mi)
{
	int iRet = 0;
	
	OriginObject oo;
	mi.GetParent(oo);
	
	if( !oo )
		return 0;
	int nEvent = ei.GetEvent();
	TreeNode tr = ei.GetUserData();
	if(tr)
	{
	//		out_int("has user data in ei, event =", nEvent);
	//		out_tree(tr);
	}
	/*
	TreeNode tr1 = mi.GetUserData();
	if(tr1)
	{
		out_int("has user data in mi, event=", nEvent);
		out_tree(tr1);
	}
	*/
	switch(nEvent )
	{
	case OE_INIT_ANCHORS:
		iRet = OnInitMarker(oo, mi, tr);
		break;
		
	case OE_GRAPHOBJ_EDIT:
		if( ei.GetSenderId() == mi.GetID() )
			iRet = OnEditMarker(oo, mi, tr);
		else
			out_int("edit msg, not marker ", ei.GetSenderId());
		break;
	default:
		//out_int("other event, id = ", nEvent);
		
	}
	
	return iRet;
}

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

}
