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

//put your own support static functions here
// Kyle 09/15/2009 ADD_WARNING_MESSAGE_WHEN_SHRINKAGE_FACTOR_IS_INVALID

#define		STR_SHRINKAGE_FACTOR		_L("Shrinkage Factors")


void mshrink(const MatrixObject& m, const int& cols, const int& rows, MatrixObject& om)
{
	if(!matobj_copy(om,m))
	{
		XF_THROW(MATOBJ_COPY_FAILED);
	}	
	
	matrixbase& mat = om.GetDataObject();
	
	if(!mat.Shrink(cols,rows))
		XF_THROW(CER_MATRIX_FAILED_IN_SHRINK);
	
}

//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 mshrink_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return mshrink_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	int nErr = check_numeric_matrix(trGetN.m.strVal);	
	if (nErr != CER_NO_ERROR)
	{
		strErrMsg = nErr;
		bOKEnable = false;
	}
	///Kyle 09/15/2009 ADD_WARNING_MESSAGE_WHEN_SHRINKAGE_FACTOR_IS_INVALID
	if(bOKEnable && (trGetN.cols.nVal < 1 || trGetN.rows.nVal < 1))
	{
		strErrMsg = CER_INVALID_POSITIVE_INTEGER;
		strErrMsg += ":" + STR_SHRINKAGE_FACTOR;
		bOKEnable = false;
	}
	///End ADD_WARNING_MESSAGE_WHEN_SHRINKAGE_FACTOR_IS_INVALID
		
	return true;
	

}

int mshrink_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 mshrink_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if ( nGetNDialog == 0 )
	{
		int nErr = check_numeric_matrix(trGetN.m.strVal);
	
		if ( nErr != CER_NO_ERROR )
		{
			warning_msg_box(nErr, false);
			nRet = XFEVT_ABORT;			
		}
		
		///Kyle 09/15/2009 ADD_WARNING_MESSAGE_WHEN_SHRINKAGE_FACTOR_IS_INVALID
		if(nErr == CER_NO_ERROR && (trGetN.cols.nVal < 1 || trGetN.rows.nVal < 1))
		{
			warning_msg_box(CER_INVALID_POSITIVE_INTEGER, _L("Shrinkage Factor"), false);
			nRet = XFEVT_ABORT;
		}
		///End ADD_WARNING_MESSAGE_WHEN_SHRINKAGE_FACTOR_IS_INVALID
	}
}
//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 mshrink_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
