#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	order_row,
	order_col,
};
//put additional include files here
#include <o8dlg.h>
//put your own support static functions here



void wexpand2m(const Worksheet& iw, const int& expand, const int& order, MatrixObject& om)
{
	
	int iR1, iR2;
	iw.GetRange(iR1, iR2, 0, -1, GDR_NO_ADJUST, GDR_NO_ADJUST);
	//int iR1, iC1, iR2, iC2;
	//iw.GetBounds(iR1, iC1,iR2,iC2, true);//BOOL bExcludeMissingValues = TRUE )
	
	    
	matrix matData;
	//matData.CopyFromWks(iw, iC1,iC2, iR1,iR2);
	matData.CopyFromWks(iw, 0, -1, iR1,iR2);
	
	vector vData;
	bool bByRow = (order == order_row ? true:false);
	
	matData.GetAsVector(vData, bByRow);
	
	uint nCol = iw.GetNumCols();
	uint nRow = iR2 - iR1 +1;
	
	uint nNewCols,nNewRows;
	if(bByRow)
	{
		nNewCols =  nCol*expand;
		///Kyle 03/12/2009 FIX_WRONG_NUM_OF_ROWS_OR_COLUMNS
		//nNewRows =  (nRow+1)/expand;
		nNewRows = (int)ceil((double)nRow/expand);
		///End FIX_WRONG_NUM_OF_ROWS_OR_COLUMNS
	}
	else
	{
		nNewRows =  nRow*expand;
		///Kyle 03/12/2009 FIX_WRONG_NUM_OF_ROWS_OR_COLUMNS
		//nNewCols =  (nCol+1)/expand;
		nNewCols = (int)ceil((double)nCol/expand);
		///End FIX_WRONG_NUM_OF_ROWS_OR_COLUMNS
	}
	
	if(!om.SetNumCols(nNewCols)||!om.SetNumRows(nNewRows))
		XF_THROW(CER_MATRIX_FAILED_IN_REPLACE);
	
	matrix& matOut = om.GetDataObject();
	matOut.SetByVector(vData, bByRow);
	
}

//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 wexpand2m_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return wexpand2m_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	Worksheet	wks;
	okxf_resolve_string_get_origin_object(trGetN.iw.strVal, &wks);
	
	if(!wks.IsValid())
	{
		strErrMsg = CER_NO_NUMERIC_WORKSHEET;
		bOKEnable = false;
	}
	
	return 0;

}

int wexpand2m_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 wexpand2m_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	//------ fisher 11/28/07 ADD_MAP_ID_TO_CHM
	if(is_use_origin_chm())
		trGetN.SetAttribute(STR_HELPID_ATTRIB, IDD_XF_wexpand2m);
}
//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 wexpand2m_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
