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

//put additional include files here

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Gareth 23/7/2010 ORG-624 P1 DEL_FIRST_EMPTY_SHEET_IF_THE_PAGE_IS_NEW
 *	Kyle 08/05/2010 ORG-723-P1 COPY_PALETTE_INFO_FOR_MO2S_AND_MS2O
 *------------------------------------------------------------------------------*/

//put your own support static functions here
enum {
	ERR_FAIL_MOVE  = XF_LOCAL_ERR_BEGIN,
};

static bool s_bNew = false;

void mo2s(const MatrixLayer& ims, MatrixPage& omp)
{
	UINT nCount = ims.MatrixObjects.Count();
	if( 0 == nCount )
		return; // no objects to move
	///Gareth 23/7/2010 ORG-624 P1 DEL_FIRST_EMPTY_SHEET_IF_THE_PAGE_IS_NEW
	//Handle the first layer if the Mpage is new.
	
	if(s_bNew)//A new MPage
	{
		
		MatrixLayer mlDest = omp.Layers(0);
		MatrixObject moDest = mlDest.MatrixObjects(0);
		MatrixObject moSrc = ims.MatrixObjects(0);
		
		///Gareth 7/29/2010 ORG-631-S1 KEEP_THE_MEANINGFUL_LONG_NAME_OF_MATOBJ
		string strSrc = moSrc.GetLongName();
		///Kyle 08/05/2010 ORG-723-P1 COPY_PALETTE_INFO_FOR_MO2S_AND_MS2O
		//if( !matobj_move(moSrc, moDest) )
		if( !matobj_move(moSrc, moDest, true) )
		///End COPY_PALETTE_INFO_FOR_MO2S_AND_MS2O
			return;
		
		mlDest.SetName(strSrc);
		///END KEEP_THE_MEANINGFUL_LONG_NAME_OF_MATOBJ
	}
	//END DEL_FIRST_EMPTY_SHEET_IF_THE_PAGE_IS_NEW
	
	while( nCount )
	{
		// When moving all objects in a loop we always move the first
		// object because after it is moved the other objects will
		// shift causing the 2nd object to become the 1st object.
		MatrixObject moSrc = ims.MatrixObjects(0);
		if( !moSrc )
			break; // failed to get source object
	
		// Create a new layer in the destination page
		
				
		int mlIndex = omp.AddLayer();
		if( -1 == mlIndex )
			break;
		MatrixLayer mlDest = omp.Layers(mlIndex);
		if( !mlDest )
			break; // failed to create destination layer
		
		// New layers have one object by default so use it as the destination
		MatrixObject moDest = mlDest.MatrixObjects(0);
		if( !moDest )
			break; // failed to get destination object
	
		///Gareth 7/29/2010 ORG-631-S1 KEEP_THE_MEANINGFUL_LONG_NAME_OF_MATOBJ
		//if( !matobj_move(moSrc, moDest) )
			//break; // failed to move object
		
		string strSrc = moSrc.GetLongName();
		// Move object
		///Kyle 08/05/2010 ORG-723-P1 COPY_PALETTE_INFO_FOR_MO2S_AND_MS2O
		//if( !matobj_move(moSrc, moDest) )
		if( !matobj_move(moSrc, moDest, true) )
		///End COPY_PALETTE_INFO_FOR_MO2S_AND_MS2O
			break; // failed to move object
		
		mlDest.SetName(strSrc);
		///END KEEP_THE_MEANINGFUL_LONG_NAME_OF_MATOBJ
	}
	//------ Folger 08/07/07 MO2S_IMPROVEMENT
	//if( nCount ) // If not all objects moved
		//XF_THROW(ERR_FAIL_MOVE); // error moving object
	int n = ims.Destroy();
	//------ End MO2S_IMPROVEMENT
}

//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 mo2s_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return mo2s_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	//------ Folger 08/07/07 MO2S_IMPROVEMENT
	//if (trGetN.method.nVal == 1)
		//trGetN.option.Show = true;
	//else
		//trGetN.option.Show = false;
	
	MatrixLayer ml;
	okxf_resolve_string_get_origin_object(trGetN.ims.strVal, &ml);
	if (!ml)
	{
		strErrMsg = CER_NO_MATRIX;
		bOKEnable = false;
		return 0;
	}
	
	//------ End MO2S_IMPROVEMENT
	return true;

}

int mo2s_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 mo2s_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///Gareth 23/7/2010 ORG-624 P1 DEL_FIRST_EMPTY_SHEET_IF_THE_PAGE_IS_NEW	
	MatrixPage	pg;
	if ( okxf_resolve_string_get_origin_object(trGetN.omp.strVal, &pg) && pg.IsValid() )
	{
		s_bNew = false;
	}
	else
	{
		s_bNew = true;
	}
	//if(PDS_NEW == okutil_cvt_str_to_predefined_type(trGetN.owp.strVal))
	//{
		//s_bNew = true;
	//}
	//END DEL_FIRST_EMPTY_SHEET_IF_THE_PAGE_IS_NEW
}
//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 mo2s_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
