#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>

//put additional include files here

//put your own support static functions here

/// Iris 2/09/2009 QA80-12845 ADD_CORRECT_ERR_MSG
static bool _has_intersected_rect(const MatrixObject& im, const RECT& rect)
{
	RECT rectImage;
	rectImage.left = 0;
	rectImage.top = 0;
	rectImage.right = im.GetNumCols();
	rectImage.bottom = im.GetNumRows();
	
	RECT intersectRect;
	return IntersectRect(&intersectRect, &rect, &rectImage); // return false if not intersect	
}
///end ADD_CORRECT_ERR_MSG


void mcopy(const MatrixObject& im, MatrixObject& om, const int& fullcopy, const int& x, const int& y, const int& w, const int& h)
{
	
	/// EJP 2008-04-08 v8.0840 QA80-11237 MAP_ROI_MATRIX_RECT_TO_IMAGE_RECT
	if( NULL == om )
	{
		RECT rect;
		rect.left = x;
		rect.top = y;
		/// YuI 07/15/09 QA70-13759 COPY_ROI_IN_MATRIX_IS_NOT_PRECISE
		// MatrixObject::CopyToClipboard is inclusive
		// need so need to subtract 1 from width height
		//	rect.right = x + w;
		//	rect.bottom = y + h;		
		rect.right = x + w - 1;
		rect.bottom = y + h - 1;		
		/// end COPY_ROI_IN_MATRIX_IS_NOT_PRECISE		
		/// Iris 2/09/2009 QA80-12845 ADD_CORRECT_ERR_MSG
		if( !_has_intersected_rect(im, rect) )
			XF_THROW(XFERR_NO_RECT_INTERSECT);
		///end ADD_CORRECT_ERR_MSG
			
		if( false == im.CopyToClipboard(&rect) )
			XF_THROW(XFERR_FAIL_CODE);
	}
	else
	/// end MAP_ROI_MATRIX_RECT_TO_IMAGE_RECT
	if( false == matobj_copy(om, im, false, NULL, fullcopy) )
		XF_THROW(XFERR_OUTPUT_MATRIX_NOT_AVALID);
	
}

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

}

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