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

//put your own support static functions here

static bool _check_range_ok(const DataRange& drRef, Worksheet& wks, int& c1, int& c2)
{
	if(!drRef)
		return false;

	int r1, r2;
	int nNum = drRef.GetNumRanges();
	if(nNum == 1)
	{
		drRef.GetRange(0, r1, c1, r2, c2, wks);
		if(c2-c1+1 != 2)
			return false;
	}
	else
	{	
		if(nNum != 2)
			return false;
		int tempc1, tempc2;
		drRef.GetRange(0, r1, tempc1, r2, tempc2, wks);
		drRef.GetRange(1, r1, c1, r2, c2, wks);
		if(tempc2!=tempc1 || c2!=c1)
			return false;
		c1 = tempc1; 
	}
	return true;
}

void colswap(Range& rng)
{
	
	if(!rng)
	{
		//warning_msg_box(XFERR_NO_ACTIVE_COLUMN, true); ///Echo 11/2/06
		XF_THROW(XFERR_NO_ACTIVE_COLUMN);
	}
	int c1, c2;
	Worksheet wks;
	if(!_check_range_ok(rng, wks, c1, c2))
	{
		//warning_msg_box(CER_COL_NUM_NOT_TWO, true); ///Echo 11/2/06
		XF_THROW(CER_COL_NUM_NOT_TWO);	
	}
	/*
	int nNum = dr.GetNumRanges();
	if(nNum == 1)
	{
		dr.GetRange(0, r1, c1, r2, c2, wks);
		if(c2-c1+1 != 2)
			XF_THROW(XFERR_COLS_UNEQUAL_TWO);
	}
	else
	{	
		if(nNum != 2)
			XF_THROW(XFERR_COLS_UNEQUAL_TWO);
		int tempc1, tempc2;
		dr.GetRange(0, r1, tempc1, r2, tempc2, wks);
		dr.GetRange(1, r1, c1, r2, c2, wks);
		if(tempc2!=tempc1 || c2!=c1)
			XF_THROW(XFERR_COLS_UNEQUAL_TWO);
		c1 = tempc1;
	}
	*/
	/// SY 2007-02-19 QA70-9387 v8.0566 MOVE_COLUMN_UNDO
	///	if(!wks.SwapColumns(c1, c2))
	if(!wks.SwapColumns(c1, c2, TRUE))
	/// end MOVE_COLUMN_UNDO
		XF_THROW(XFERR_SWAP_COL_FAILED);
	
}

//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 colswap_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return colswap_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	int nErr = CER_NO_ERROR;
	
	int c1,c2;
	Worksheet wks;
	
	DataRange drRef;
	TreeNode tr = trGetN.rng;
	bool bRet = okxf_resolve_tree_construct_range(&tr, &drRef);
	
	if(!bRet || !_check_range_ok(drRef, wks, c1, c2))
		nErr = CER_COL_NUM_NOT_TWO;
	
	if(nErr != CER_NO_ERROR)
	{
		bOKEnable = false;
		strErrMsg = nErr;
	}
	return false;

}

int colswap_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 colswap_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	int c1,c2;
	Worksheet wks;
	
	DataRange drRef;
	TreeNode tr = trGetN.rng;
	bool bRet = okxf_resolve_tree_construct_range(&tr, &drRef);
	
	if(bRet && _check_range_ok(drRef, wks, c1, c2) && (dwCntrl & LTXF_FROM_GUI_MENU))
		nRet = XFEVT_PROCEED_NO_DLG;
	else
		nRet = XFEVT_PROCEED;
}
//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 colswap_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
