#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
#include <event_utils.h>
#include <..\Originlab\XFTree.h>
#include <..\Originlab\XFunction.h>
//put your own support static functions here

///Sophy 6/25/2010 ORG-322-P1 BETTER_ERR_MSG_INFORM_CVT_IMAGE_TO_DATA
int _check_input(TreeNode& trGetN)
{
	int nErr = CER_NO_ERROR;
	MatrixObject im;
	okxf_resolve_string_get_origin_object(trGetN.im.strVal, &im);
	if ( !im.IsValid() )
		nErr = CER_INVALID_MATOBJ;
	else if ( !im.HasData(false) )
		nErr = CER_NEED_CVT_TO_DATA;
	return nErr;
}
///end BETTER_ERR_MSG_INFORM_CVT_IMAGE_TO_DATA

void msmooth(const MatrixObject& im, MatrixObject& om)
{
	if(!matobj_copy(om, im))
	{
		XF_THROW(MATOBJ_COPY_FAILED);
	}
	
	XFunction xfExpand;
	XFunction xfShrink;
	
	if( !xfExpand.Load(NULL, "mexpand", 0, true, false, true) || !xfShrink.Load(NULL, "mshrink", 0, true, false, true) )
	{
		XF_THROW(XFERR_FAIL_LOAD_XF);
	}
	
	xfExpand.SetArgs(om, 2, 2, om);
	xfShrink.SetArgs(om, 2, 2, om);
	
	int nCols = im.GetNumCols();
	int nRows = im.GetNumRows();
	
	if ( nCols < 32 || nRows < 32 )
	{
		xfExpand.ExecuteLabTalk(NULL, NULL);
		xfShrink.ExecuteLabTalk(NULL, NULL);
	}
	else
	{
		xfShrink.ExecuteLabTalk(NULL, NULL);
		xfExpand.ExecuteLabTalk(NULL, NULL);
	}
	
}

//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 msmooth_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return msmooth_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	///Sophy 6/25/2010 ORG-322-P1 BETTER_ERR_MSG_INFORM_CVT_IMAGE_TO_DATA
	//int nErr = check_numeric_matrix(trGetN.im.strVal);
	int nErr = _check_input(trGetN);
	///end BETTER_ERR_MSG_INFORM_CVT_IMAGE_TO_DATA
	if (nErr != CER_NO_ERROR)
	{
		strErrMsg = nErr;
		bOKEnable = false;
	}
		
	return true;

}

int msmooth_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 msmooth_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if ( nGetNDialog == 0 )
	{
		///Sophy 6/25/2010 ORG-322-P1 BETTER_ERR_MSG_INFORM_CVT_IMAGE_TO_DATA
		//int nErr = check_numeric_matrix(trGetN.im.strVal);
		int nErr = _check_input(trGetN);
		///end BETTER_ERR_MSG_INFORM_CVT_IMAGE_TO_DATA
	
		if ( nErr != CER_NO_ERROR )
		{
			warning_msg_box(nErr, false);
			nRet = XFEVT_ABORT;			
		}
	}
}
//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 msmooth_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
