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

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Gareth 7/23/2010 ORG-608 CREATE_SIMPLE_XF_MOVE_SHEET
 *	Gareth 7/27/2010 ORG-608 TAKE_CARE_THE_AUTONEW_SITUATION	
 *	Gareth 7/28/2010 ORG-608-S1 CHANGE_RESULT_MESSAGE
 *------------------------------------------------------------------------------*/

static bool s_bNew = false;
//put your own support static functions here



void wmove_sheet(const Worksheet& source, WorksheetPage& target, string& name, const int& index)
{
	
	///Gareth 7/27/2010 ORG-608 TAKE_CARE_THE_AUTONEW_SITUATION
	//if(PDS_NEW == okutil_cvt_str_to_predefined_type(target.strVal))//Check des if created by XF <new>
	//{
		//s_bNew = true;
		//return;
	//}
	//
	//WorksheetPage	wp;//Check des wspage if created by auto when the page name dosen't exist
	//
	//
	//if ( okxf_resolve_string_get_origin_object(target.strVal, &wp) && wp.IsValid() )	
	//{
	//
		//s_bNew = false;
	//}
	//else
	//{
		//s_bNew = true;
	//}
	// END TAKE_CARE_THE_AUTONEW_SITUATION
	if(!source.IsValid())
	{
		XF_THROW(CER_INVALID_WKS);
	}
	if(!target.IsValid())
	{
		XF_THROW(CER_INVALID_WKS);
	}
	
	if ( s_bNew )
	{
		target.Layers().Destroy(OCD_DEL_LINKED_OBJS); // destroy layer generate by <new>
	}
	
	
	int iLayers;
	///Gareth 7/27/2010 ORG-608-P4 MOVR_CORRECT_POSITION
	int nPos = index;
	/// END ORG-608 MOVE_CORRECT_POSITION
	
	///Gareth 9/19/2010 CODE_IMPROVEMENT
	//foreach(Layer ly in target.Layers)
		//iLayers++;
	iLayers = target.Layers.Count();
	///END CODE_IMPROVEMENT	
	if(index > (iLayers+1))
	{
		nPos = iLayers+1;
	}
	    
	nPos--;
	
	
	///Gareth 7/27/2010 ORG-608-S1 SHOW_RESULT_MESSAGE
	
	string strSrc;
	source.GetRangeString(strSrc);
	int nLy = target.AddLayer(source, DCTRL_COPY_DEFAULT, false, nPos);
	
	name = target.Layers(nLy).GetName();
	string strDes;
	source.GetRangeString(strDes);
	
	
	
	///Gareth 9/19/2010 CODE_IMPROVEMENT
		/////Gareth 7/28/2010 ORG-608-S1 CHANGE_RESULT_MESSAGE
	////string strMsg = strSrc + " has moved to " + strDes;
	//string strMsg = strSrc + " was moved to " + strDes;
	////END CHANGE_RESULT_MESSAGE
	//
	//out_str(strMsg);
	string strMsg;
	if(ocu_load_msg_str(MSG_MOVED_SHEET, &strMsg, strSrc, NULL, strDes))
	{
		okoc_out_msg(strMsg, 'I');
	}
	///END CODE_IMPROVEMENT
	
	///END ORG-608-S1 SHOW_RESULT_MESSAGE
}

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

}

int wmove_sheet_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 wmove_sheet_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///Gareth 7/27/2010 ORG-608 TAKE_CARE_THE_AUTONEW_SITUATION	
	
	//if(PDS_NEW == okutil_cvt_str_to_predefined_type(trGetN.target.strVal))//Check des if created by XF <new>
	//{
		//s_bNew = true;
		//return;
	//}
	WorksheetPage	wp;//Check des wspage if created by auto when the page name dosen't exist
	if ( !okxf_resolve_string_get_origin_object(trGetN.target.strVal, &wp) || !wp.IsValid() )	
	{
	
		s_bNew = true;
	}
	else
	{
		s_bNew = false;
	}
	// END TAKE_CARE_THE_AUTONEW_SITUATION	
}
//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 wmove_sheet_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
