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

///Jasmine 09/27/07 FIX_RUNTIME_ERROR_AND_IMPROVE_UPDATE_SPARKLINES
//#include <GetNBox.h>
//put your own support static functions here
#include <..\originlab\theme_utils.h>




void updateSparklines(const string& template, const int& keepAspect, const int& label, const int& endpts, Range& orng)
{
	
	Worksheet wks;
	vector<uint> vnCols, vnIndeces, uBegins, uEnds;
	int nNumData = orng.GetNumData();
	for(int nSubRange = 0; nSubRange < nNumData; nSubRange++)
	{
		int r1,r2,c1,c2;
		orng.GetRange(nSubRange, r1, c1, r2, c2, wks);
		if(-1 == c2)//all columns
			break;//doing
		for(int ii = c1; ii <= c2; ii++)
		{//get col from each subrange
			if(1 > vnCols.Find(vnIndeces, ii))
				vnCols.Add(ii);			
		}
	}
	if(!wks)
		XF_THROW(XFERR_INVALID_ARG); 
	int nRange;
	if(nSubRange == nNumData)
	{
		vnCols.Sort();
		nRange = find_contiguous_range(vnCols, uBegins, uEnds);
	}
	else//all columns
	{
		nRange = 1;
		uBegins.Add(0);
		uEnds.Add(wks.GetNumCols()-1);
	}
	
	//int nn = 0;
	DWORD dwCntrl = EMBEDGRAPH_IN_LABELS | EMBEDGRAPH_HIDE_AXES | EMBEDGRAPH_HIDE_LEGENDS | EMBEDGRAPH_HIDE_SPECTRUMS;
	if(label)
		dwCntrl |= EMBEDGRAPH_HIDE_TEXT_OBJS;
	if(keepAspect) dwCntrl |= EMBEDGRAPH_KEEP_ASPECT_RATIO;
	
	//for(int nC = c1; nC <= c2; nC++)
	for(int nn = 0; nn < nRange; nn++)
	{
		int nBegin = uBegins[nn];
		int nEnd = uEnds[nn];
		for(int nC = nBegin; nC <= nEnd; nC++)
		{
			if(wks.Columns(nC).GetType() != OKDATAOBJ_DESIGNATION_Y || is_col_all_text(wks, nC) > 0)
				continue;
			
			GraphPage gp;
			DataPlot dp = plot_col(wks, nC, gp, IDM_PLOT_LINE, true, template);
			if(dp)
			{
				if(endpts) add_end_points(dp);
								
				//nn++;
				wks.EmbedGraph(RCLT_SPARKLINE, nC, gp, dwCntrl);
			}
		}
	}
	if(!wks.IsLabelTypeShown(RCLT_SPARKLINE))	//sjow sparkline row if not show
		//wks_set_col_label_heights(wks, RCLT_SPARKLINE, 200);
		wks_set_col_label_heights(wks, RCLT_SPARKLINE, 200, false); /// Hong 11/01/07 v8.0738 SPARKLINES_SHOULD_NOT_HAVE_THEME_UNDOABLE
}

//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 updateSparklines_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return updateSparklines_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	/*	///Jasmine 09/27/07 move to before_execute
	if(GETNE_ON_INIT == nEventID)
	{
		DataRange dr;
		TreeNode tr = trGetN.orng;
		if(okxf_resolve_tree_construct_range(&tr, &dr) && 0 < dr.GetNumData())
		{
			Worksheet wks;
			int r1,r2,c1,c2;
			dr.GetRange(0, r1, c1, r2, c2, wks);
			if(wks)
			{
				DWORD dwCntrl = 0;
				if(wks.GetEmbeddingParams(r1, c1, dwCntrl))
				{
					if(EMBEDGRAPH_KEEP_ASPECT_RATIO & dwCntrl)
						trGetN.keepAspect.nVal = 1;
					else
						trGetN.keepAspect.nVal = 0;
				}
			}
	
		}
	}*/
	return true;
	

}

int updateSparklines_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return -1;
}
//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 updateSparklines_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if(0 <= nGetNDialog)
	{
		int r1,r2,c1,c2;
		Worksheet wks;
		DataRange dr;
		TreeNode tr = trGetN.orng;
		if(okxf_resolve_tree_construct_range(&tr, &dr) && 0 < dr.GetNumData() && dr.GetRange(0, r1, c1, r2, c2, wks) && wks)
		{
			if(1 == nGetNDialog)//before open dlg
			{
				DWORD dwCntrl = 0;
				//if(wks.GetEmbeddingParams(r1, c1, dwCntrl))	///Jasmine 09/27/07 same logic as wks.EmbedGraph(RCLT_SPARKLINE, nC, gp, dwCntrl); 
				if(wks.GetEmbeddingParams(RCLT_SPARKLINE, c1, dwCntrl))
				{
					if(EMBEDGRAPH_KEEP_ASPECT_RATIO & dwCntrl)
						trGetN.keepAspect.nVal = 1;
					else
						trGetN.keepAspect.nVal = 0;
				}
			}	
		}
		else
		{
			warning_msg_box(XFERR_INVALID_RANGE, false, 'E');
			nRet = XFEVT_ABORT;
		}
	}	
}
//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 updateSparklines_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
