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

/*------------------------------------------------------------------------------*
* Modification Log:																*
*	Folger 09/23/08 QA80-12240 SHOULD_COPY_NUMERIC_VALUES_FOR_COLUMN_DATE_FORMAT*
*	Folger 10/06/08 QA80-12240 SHOULD_SET_FORMAT_AND_DISPLAY_SEPARATELY_FROM_OTHER_SETTINGS
*	Folger 08/10/09 GENERAL_COLUMN_COPY_UTILS_NEEDED_IN_OC						*
*	Kenny 10/12/2009 QA81-14435 NO_ERROR_CHECKING_FOR_OUTPUT_COLUMN				*
*	Folger 11/17/09 QA81-11105 COPY_COL_SUPPORT_UDLS							*
*------------------------------------------------------------------------------*/

/// ML 2/6/2007 QA70-9350 PROPER_SETTING_UPPER_INDEX_DEST_COLUMN

///------ Folger 08/10/09 GENERAL_COLUMN_COPY_UTILS_NEEDED_IN_OC
////------ Folger 02/18/08 QA80-11105 COPY_COLUMN_NEED_TO_DO_WITH_ADDITIONAL_PARAMETERS
//static void _col_copy_parameter_label(const Column &ccSrc, Column &ccDes, int nType)
//{
	//string strLabel;
	//ccSrc.GetExtendedLabel(strLabel, nType);
	//ccDes.SetExtendedLabel(strLabel, nType);
//}
////------
//
//static bool	_copycol(Worksheet& wksSc, int nScCol, Worksheet& wksTg, int nTgCol, bool data, bool format, int lname, int units, int comments, int para)
//{
	//Column ccSc(wksSc, nScCol);
	//Column ccTg(wksTg, nTgCol);
	//if(!ccSc || !ccTg)
		//return false;
	//if(format)
	//{
		//Tree trFmt, trTgFmt;
		//trFmt = ccSc.GetFormat(FPB_ALL, FOB_ALL, true, true);		
		//trTgFmt = ccTg.GetFormat(FPB_ALL, FOB_ALL, true, true);
		//TreeNode trProp = trFmt.Root;
		//TreeNode trTgProp = trTgFmt.Root;
		//octree_copy_values_by_id(&trProp, &trTgProp, true, STR_ID_ATTRIB, STR_ID_ATTRIB);
		//octree_delete_nodes_by_missing_attribute(&trTgProp, STR_CHANGED_ATTRIB);				
		//if(trTgProp.Format.IsValid() && trTgProp.InternalData.IsValid())
		//{
			//if(trTgProp.Format.nVal != OKCOLTYPE_NUMERIC)
			//{
				//trTgProp.InternalData.Remove(); // this is later in the theme tree, so we must remove otherwise it will set col back to Numeric
			//}
		//}
		////TreeNode trRoot = trFmt.Root;
		//if(trTgProp.ShortName)
			//trTgProp.ShortName.Remove();
		//if(trTgProp.LongName)
			//trTgProp.LongName.Remove();
		//if(trTgProp.Comment)
			//trTgProp.Comment.Remove();
		//if(trTgProp.Unit)
			//trTgProp.Unit.Remove();
		////------ Folger 10/06/08 QA80-12240 SHOULD_SET_FORMAT_AND_DISPLAY_SEPARATELY_FROM_OTHER_SETTINGS
		//if ( trTgProp.NumericDisplay && (trTgProp.Format && OKCOLTYPE_NUMERIC != trTgProp.Format.nVal && OKCOLTYPE_TEXT_NUMERIC != trTgProp.Format.nVal) )
			//trTgProp.NumericDisplay.Remove();
		//UndoBlock ub;
		//if ( trTgProp.Format )
		//{
			//if ( trTgProp.CustomFormat )
			//{
				//ccTg.SetCustomDisplay(trTgProp.CustomFormat.strVal);
			//}
			//ccTg.SetFormat(trTgProp.Format.nVal, trTgProp.Display ? trTgProp.Display.nVal : 0, true);
			//trTgProp.Format.Remove();
			//if ( trTgProp.Display )
				//trTgProp.Display.Remove();
		//}
		////------ End SHOULD_SET_FORMAT_AND_DISPLAY_SEPARATELY_FROM_OTHER_SETTINGS
		//ccTg.ApplyFormat(trTgFmt, true, true);
	//}
	//if(data)
	//{
		/////Sophy 04/30/2008 v8.0853 FIX_COPY_NUMERIC_FORMAT_COLUMN_FAILURE
		///*
		//Dataset dsS(wksSc, nScCol);
		//vector<string> vs;
		//if(!dsS.GetStringArray(vs))
		//{
			//DataRange deTemp;
			//deTemp.Create();
			//deTemp.Add("X", wksSc, 0, nScCol, -1, nScCol);
			//deTemp.GetData(vs, 0);
		//}
		///// ML 2/6/2007 QA70-9350 PROPER_SETTING_UPPER_INDEX_DEST_COLUMN
		////for(int nRow = 0; nRow < vs.GetSize(); nRow++)
		////	wksTg.SetCell(nRow, nTgCol, vs[nRow]);
		//int		nSize = vs.GetSize();
		//ccTg.SetUpperIndex(nSize - 1);
		//ccTg.PutStringArray(vs, 0, true);
		///// end PROPER_SETTING_UPPER_INDEX_DEST_COLUMN
		//*/
		////------ Folger 09/23/08 QA80-12240 SHOULD_COPY_NUMERIC_VALUES_FOR_COLUMN_DATE_FORMAT
		////if( OKCOLTYPE_NUMERIC == ccSc.GetFormat() ) //copy column with numeric format
		//if( OKCOLTYPE_NUMERIC == ccSc.GetFormat() || OKCOLTYPE_DATE == ccSc.GetFormat() || OKCOLTYPE_TIME == ccSc.GetFormat() ) 
		////------
		//{
			//vectorbase& vbColSc = ccSc.GetDataObject();
			//vectorbase& vbColTg = ccTg.GetDataObject();
			//vbColTg = vbColSc;
		//}
		//else
		//{
			//Dataset dsS(wksSc, nScCol);
			//vector<string> vs;
			//if(!dsS.GetStringArray(vs))
			//{
				//DataRange deTemp;
				//deTemp.Create();
				//deTemp.Add("X", wksSc, 0, nScCol, -1, nScCol);
				//deTemp.GetData(vs, 0);
			//}
			///// ML 2/6/2007 QA70-9350 PROPER_SETTING_UPPER_INDEX_DEST_COLUMN
			////for(int nRow = 0; nRow < vs.GetSize(); nRow++)
			////	wksTg.SetCell(nRow, nTgCol, vs[nRow]);
			//int		nSize = vs.GetSize();
			//ccTg.SetUpperIndex(nSize - 1);
			//ccTg.PutStringArray(vs, 0, true);
			///// end PROPER_SETTING_UPPER_INDEX_DEST_COLUMN
		//}
		/////end FIX_COPY_NUMERIC_FORMAT_COLUMN_FAILURE
	//}
	////set labels
	//vector<int> vnLabelType;
	//vnLabelType.Add(lname);
	//vnLabelType.Add(units);
	//vnLabelType.Add(comments);
	////------ Folger 02/18/08 QA80-11105 COPY_COLUMN_NEED_TO_DO_WITH_ADDITIONAL_PARAMETERS
	////vnLabelType.Add(para);
	////for(int nType = RCLT_LONG_NAME; nType <= RCLT_PARAM; nType++)
	////{
		////if(!vnLabelType[nType])
			////continue;
		////string strLabel;
		////ccSc.GetExtendedLabel(strLabel, nType);
		////ccTg.SetExtendedLabel(strLabel, nType);
	////}
	//
	//Grid gridSc, gridTg;   
	//if( !gridSc.Attach(wksSc) || !gridTg.Attach(wksTg) )
		//return false;
	//
	//vector<int> vnTypes;
	//gridSc.GetShowLabels(vnTypes);
	//gridTg.SetShowLabels(vnTypes);
	//
	//for ( int nType = RCLT_LONG_NAME; nType <= RCLT_COMMENT; ++nType )
	//{
		//if ( !vnLabelType[nType] )
			//continue;
		//_col_copy_parameter_label(ccSc, ccTg, nType);
	//}
	//
	//if ( para )
	//{
		//for ( int nType = RCLT_PARAM; nType < RCLT_PARAM + PARAMS_MAX_NUM; ++nType )
			//_col_copy_parameter_label(ccSc, ccTg, nType);
	//}
	///*
	//if ( usrdef )
	//{
		//Grid grid;   
		//if(!grid.Attach(wksSc))
			//return false;
		//
		//vector<string> vsUserDefined;
		//grid.GetUserDefinedLabelNames(vsUserDefined);
		//for ( int ii=0; ii<vsUserDefined.GetSize(); ++ii )
			//_col_copy_parameter_label(ccSc, ccTg, RCLT_UDL + ii);
	//}
	//*/
	////------ End COPY_COLUMN_NEED_TO_DO_WITH_ADDITIONAL_PARAMETERS
	//return true;
//}
static bool	_copycol(Worksheet& wksSc, int nScCol, Worksheet& wksTg, int nTgCol, bool data, bool format, int lname, int units, int comments, int para, int usrdef)
{
	Column		ccSc(wksSc, nScCol);
	Column		ccTg(wksTg, nTgCol);
	DWORD		dwOptions = 0;
	O_SET_BIT(dwOptions, COPY_COL_DATA, data);
	O_SET_BIT(dwOptions, COPY_COL_FORMAT, format);
	O_SET_BIT(dwOptions, COPY_COL_LONGNAME, lname);
	O_SET_BIT(dwOptions, COPY_COL_UNITS, units);
	O_SET_BIT(dwOptions, COPY_COL_COMMENTS, comments);
	O_SET_BIT(dwOptions, COPY_COL_PARAMETERS, para);
	O_SET_BIT(dwOptions, COPY_COL_UDL, usrdef);		///------ Folger 11/17/09 QA81-11105 COPY_COL_SUPPORT_UDLS
	return copy_col(ccSc, ccTg, dwOptions);
}
///------ End GENERAL_COLUMN_COPY_UTILS_NEEDED_IN_OC


void colcopy(const Range& irng, Range& orng, const int& data, const int& format, const int& lname, const int& units, const int& comments, const int& para, const int& usrdef)
{
	int nSc = irng.GetNumRanges(), nTg = orng.GetNumRanges(), r1, r2, c1, c2;
	if(!nSc || !nTg)
	/// Cloud 7/11/07 UPDATE_XF_ERROR_THROW
		//XF_THROW(XFERR_INVALID_ARG)'
	{
		warning_msg_box(XFERR_INVALID_ARG, false, 'E');
		return;
	}
	/// End UPDATE_XF_ERROR_THROW
	Worksheet wksSc, wksTg;
	vector<int> vnScCols, vnTgCols;
	vector<uint> vnIndeces;
	for(int ii = 0; ii < nSc; ii++)
	{
		irng.GetRange(ii, r1, c1, r2, c2, wksSc);
		for(int nCol = c1; nCol <= c2; nCol++)
		{
			if(1 > vnScCols.Find(vnIndeces, nCol))
				vnScCols.Add(nCol);			
		}		
	}
	for(ii = 0; ii < nSc; ii++)
	{
		orng.GetRange(ii, r1, c1, r2, c2, wksTg);
		for(int nCol = c1; nCol <= c2; nCol++)
		{
			if(1 > vnTgCols.Find(vnIndeces, nCol))
				vnTgCols.Add(nCol);			
		}		
	}
	if(!wksSc || !wksTg)
	/// Cloud 7/11/07 UPDATE_XF_ERROR_THROW
		//XF_THROW(XFERR_INVALID_ARG)
	{
		warning_msg_box(XFERR_INVALID_ARG, false, 'E');
		return;
	}
	/// End UPDATE_XF_ERROR_THROW
	nSc = vnScCols.GetSize();
	nTg = vnTgCols.GetSize();
	int iNumCols = wksTg.GetNumCols();
	for(ii = 0; ii < nSc; ii++)
	{
		if(ii >= nTg)//not enough TgCols
		{
			vnTgCols.Add(vnTgCols[nTg - 1]+1);
			nTg++;
		}
		while(vnTgCols[ii]>=iNumCols)//not enough cols in wksTg
		{
			wksTg.AddCol();
			iNumCols++;
		}
		_copycol(wksSc, vnScCols[ii], wksTg, vnTgCols[ii], data, format, lname, units, comments, para, usrdef);
	}
}

//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 colcopy_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return colcopy_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	//-------Folger 07/13/07 MODIFY_ERROR_CHECKING
	//int nErrCode = check_1_data(trGetN.irng, true);
	int nErrCode = check_1_column(trGetN.irng, true);
	//-------End MODIFY_ERROR_CHECKING
	/// Kenny 10/12/2009 QA81-14435 NO_ERROR_CHECKING_FOR_OUTPUT_COLUMN
	//if(nErrCode == CER_NO_ERROR)
		/////Folger 06/25/07 MODIFY_ERROR_CHECKING
		////nErrCode = check_1_data(trGetN.orng, true);
		//nErrCode = check_1_column(trGetN.orng, true);
		/////End MODIFY_ERROR_CHECKING
	/// End NO_ERROR_CHECKING_FOR_OUTPUT_COLUMN
	if(nErrCode != CER_NO_ERROR)
	{
		bOKEnable = false;
		strErrMsg = nErrCode;
		if(nErrCode == CER_COL_NUM_TOO_FEW_EX)
			strErrMsg += ":" + 1;
	}
	return false;

}

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