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

#define STR_VAR_VAL_SEP				"|=|"
typedef bool (*FUNC_HWND_STR_PINT) 	(HWND hWndParent, string& str, int* lpType);


void insertVar(const Range& cell)
{
	Worksheet wks;
	int r1,r2,c1,c2, nType;
	if(!cell.GetNumRanges())
		XF_THROW(XFERR_INVALID_ARG)
	cell.GetRange(0, r1, c1, r2, c2, wks);
	if(!wks)
		XF_THROW(XFERR_INVALID_ARG)
	HWND hParent = wks.GetPage().GetWindow().GetSafeHwnd();
	string str = "cell";
	int* pnType = &nType;
	FUNC_HWND_STR_PINT pfn = Project.FindFunction("InsertUserVar");
	//------ CPY 1/20/07 XF_CANCEL_UI_SHOULD_NOT_GENERATE_ERR_MSG
	//if(!pfn || !pfn(hParent, str, pnType))
	//	XF_THROW(CER_FAIL_OPEN_INSERT_INFO_VAR)
	if(!pfn)
		XF_THROW(CER_FAIL_OPEN_INSERT_INFO_VAR)
	if(!pfn(hParent, str, pnType))
		XF_THROW(XFERR_UI_USER_CANCEL);
	//------
	vector<string> vs;
	int nSize = str.GetTokens(vs, '\n');
	for(int ii = 0; ii < nSize; ii++)
	{
		vs[ii].TrimRight("\r");
		vector<string> vsValue;
		int nCount = str_separate(vs[ii], STR_VAR_VAL_SEP, vsValue);
		for(int nn = 0; nn < nCount; nn++)
		{
			vsValue[nn].TrimLeft(); 
			vsValue[nn].TrimRight();
			if(c1+nn >= wks.GetNumCols())
				wks.AddCol();
			wks.SetCell(r1+ii, c1+nn, vsValue[nn]);
		}
	}
}

//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 insertVar_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
