#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	degree_ccw90,
	degree_ccw180,
	degree_cw90,
};
#include <image_utils.h>

//put additional include files here


/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 07/30/09 QA80-13904-P2 COORDINATE_SHOULD_BE_CHANGED_AFTER_ROTATE		*
 *	Folger 10/15/09 QA81-13904-P3 SET_MATRIX_XY_COORDINATES_FAILS_TO_UNDO		*
 *------------------------------------------------------------------------------*/

//put your own support static functions here

///------ Folger 07/30/09 QA80-13904-P2 COORDINATE_SHOULD_BE_CHANGED_AFTER_ROTATE
static	BOOL	_rotate_coordinates(MatrixObject& om, const MatrixObject& im, int nDegree)
{
	#define			NUM_DIRECTIONS		3
	if ( nDegree >= NUM_DIRECTIONS )
		return FALSE;
	
	vector<int>		vn = {2, 3, 1, 0, 1, 0, 3, 2, 3, 2, 0, 1};
	vector<int>		vnOrders;
	int				nSize = vn.GetSize() / NUM_DIRECTIONS;
	vn.GetSubVector(vnOrders, nDegree * nSize, (nDegree + 1) * nSize - 1);
	
	double	dxMin, dyMin, dxMax, dyMax;
	im.GetXY(dxMin, dyMin, dxMax, dyMax);
	vector	vv(nSize);
	vv[0] = dyMax, vv[1] = dyMin, vv[2] = dxMin, vv[3] = dxMax;
	dyMax = vv[vnOrders[0]], dyMin = vv[vnOrders[1]], dxMin = vv[vnOrders[2]], dxMax = vv[vnOrders[3]];
	///------ Folger 10/15/09 QA81-13904-P3 SET_MATRIX_XY_COORDINATES_FAILS_TO_UNDO
	//om.SetXY(dxMin, dyMin, dxMax, dyMax);
	om.SetXY(dxMin, dyMin, dxMax, dyMax, TRUE);
	///------ End SET_MATRIX_XY_COORDINATES_FAILS_TO_UNDO
	return TRUE;
}
///------ End COORDINATE_SHOULD_BE_CHANGED_AFTER_ROTATE


void mrotate90(const MatrixObject& im, const int& degree, MatrixObject& om)
{
	vector<int> nvRotateDegree = {90,180,270};
	if(degree < 0 || degree >= nvRotateDegree.GetSize())
		XF_THROW(INTOPTN_OUT_OF_RANGE);
	
	if(!matobj_copy(om,im))
		XF_THROW(MATOBJ_COPY_FAILED);
	
	bool bDataMode = om.HasData(false); 
	if( bDataMode )//for Matrix 
	{
		matrixbase& mat = om.GetDataObject();
		if (!mat.Rotate(nvRotateDegree[degree]))
			XF_THROW(XFERR_MATRIX_ROTATE_FAILED);	
	}
	else//for Image
	{
		LPVOID pLBmp = om.GetLeadBitmap(); 
		int nAngle = -nvRotateDegree[degree] * 100.0;
		UINT crFill = color_index_to_rgb(0);
		int nRet = L_RotateBitmap((pBITMAPHANDLE)pLBmp, nAngle, ROTATE_RESIZE, crFill);
		if(nRet!= SUCCESS)
		 	XF_THROW_EX(XFERR_LEADTOOLS_FUNCTION_UNKNOWN_ERR, nRet);
		RECT rc;
		nRet =  L_GetAutoTrimRect((pBITMAPHANDLE)pLBmp, 50, &rc);
		if(nRet!= SUCCESS)
		 	XF_THROW_EX(XFERR_LEADTOOLS_FUNCTION_UNKNOWN_ERR, nRet);
	}
	
	///------ Folger 07/30/09 QA80-13904-P2 COORDINATE_SHOULD_BE_CHANGED_AFTER_ROTATE
	_rotate_coordinates(om, im, degree);
	///------ End COORDINATE_SHOULD_BE_CHANGED_AFTER_ROTATE
}

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

}

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