#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	type_mesh,
	type_cmap,
};
//put additional include files here

//put your own support static functions here
void	_set_style(int type, GraphLayer &gl)
{
	if(type_mesh == type)
	{
		int ii = 1;
		
		DataPlot  dp;
		foreach(dp in gl.DataPlots)
		{
			Tree tr;
			tr = dp.GetFormat(FPB_ALL, FPB_ALL, TRUE, TRUE);
			if(tr.IsValid())
			{
				tr.Root.Grids.FrontColor.nVal = ii++;
				tr.Root.Grids.EnableBackColor.nVal = 0;
				
			    int nErr = dp.UpdateThemeIDs(tr.Root);
        		if(0 == nErr)
              		dp.ApplyFormat(tr, true, true);
			}
		}
	}
	else if( type_cmap == type )
	{

	}
}


void plotms(const MatrixLayer& ml, const int& type)
{
	
	int nPlotID;
	string strTemplate;
	
	if( type_mesh == type )
	{
		nPlotID = IDM_PLOT_3D_MESH;	
		strTemplate = "mesh";	 	
	}
	else if( type_cmap == type )
	{
		nPlotID = IDM_PLOT_3D_MESH;	
		strTemplate = "cmap";	 	
	}
	
	if( ml )
	{
		GraphPage gp;
		gp.Create(strTemplate, CREATE_HIDDEN); // use template, create as hidden to avoid unneeded drawing            
		GraphLayer gl = gp.Layers();
		
		if( !gl )
			return;
	
		MatrixObject mobj;
		foreach(mobj in ml.MatrixObjects)
		{
			int nPlot = gl.AddPlot(mobj, nPlotID);    
			if(nPlot < 0)
				return;
		}
		
		_set_style(type, gl);
		
		gp.TitleShow = WIN_TITLE_SHOW_BOTH;
		gl.Rescale();
		gp.SetShow();// show it when all it ready
	}
	
}

//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 plotms_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return plotms_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

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

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