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

//CPY 1/8/07 SPARK_LINES_BROKEN_FOR_MIXED_COL
//CPY 9/12/2007 QA70-10383 SPARKLINES_FOR_ALL_COLS_PLOT_AGAINST_ROW_NUMBERS
/// Max 10/06/08 v8.0951c RYAN_SUGGEST_SELECTION_SHOULD_DISABLE_COL_FROM_TO

//put your own support static functions here
#include <..\originlab\theme_utils.h>

/// Max 10/06/08 move to oErrMsg
//#define STR_ASK_USE_ALL_COLS_WHEN_NO_SEL "No Column selected. You should first select the columns that you want to add sparklines. Do you want to go ahead and add to all Y columns?"

static void	_check_update_from_to(TreeNode& trGetN)
{
	Worksheet iw(trGetN.iw.strVal);
	TreeNode trc1 = trGetN.c1;
	if( 1 == octree_get_auto_support(&trc1) )
		trc1.nVal = 1;
		
	TreeNode trc2 = trGetN.c2;
	if( trc2.nVal < 1 || 1 == octree_get_auto_support(&trc2) )
		trc2.nVal = iw.GetNumCols();
}



void sparklines(const Worksheet& iw, const int& sel, const int& c1, const int& c2, const string& template, const int& row_height, const int& keepAspect, const int& label, const int& endpts, const int& xy)
{
	/// Hong 06/28/07 QA80-9970 ADD_CHECK_FOR_SUPPORTED_WORKSHEET
	if ( !iw )
		return ;
	
	Page pg;
	iw.GetParent(pg);
	if ( EXIST_WKS != pg.GetType() || ( iw.GetSystemParam(0) & WP_SHEET_HIERARCHY ) ) // right now, onlyclean worksheet support sparklines
		return ;
	/// end ADD_CHECK_FOR_SUPPORTED_WORKSHEET
	int nC1 = c1, nC2 = c2;
	if(sel)
	{
		int r1,r2;
		int nSel = iw.GetSelectedRange(r1, nC1, r2, nC2);
		if(!(nSel & WKS_SEL_COLUMN))
		{
			/// Max 10/06/08 move to oErrMsg
			//if(MessageBox(GetWindow(), STR_ASK_USE_ALL_COLS_WHEN_NO_SEL, "Sparklines", MB_YESNO) != IDYES)			
			if(warning_msg_box(XFWARN_ASK_USE_ALL_COLS_WHEN_NO_SEL, true, 'W', MB_YESNO) != IDYES)
				return;// add err msg here
			nSel = WKS_SEL_ALL;
		}
		if(nSel == WKS_SEL_ALL)
		{
			nC1 = 0, nC2 = iw.GetNumCols()-1;
		}
	}
	else
	{
		nC1--;// convert from LT to C
		if(nC2 < 1)
			nC2 = iw.GetNumCols()-1;
		else
			nC2--;
	}
	if(nC1 < 0) nC1 = 0;
	if(nC2 >= iw.GetNumCols()) nC2 = iw.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 = nC1; nC <= nC2; nC++)
	{
		//---- CPY 1/8/07 SPARK_LINES_BROKEN_FOR_MIXED_COL
		//if(iw.Columns(nC).GetType() != OKDATAOBJ_DESIGNATION_Y || OKCOLTYPE_NUMERIC != iw.Columns(nC).GetFormat())
		//------- CPY 9/12/2007 QA70-10383 SPARKLINES_FOR_ALL_COLS_PLOT_AGAINST_ROW_NUMBERS
		//if(iw.Columns(nC).GetType() != OKDATAOBJ_DESIGNATION_Y || is_col_all_text(iw, nC) > 0)
		if((xy && iw.Columns(nC).GetType() != OKDATAOBJ_DESIGNATION_Y) || is_col_all_text(iw, nC) > 0)
		//-------
			continue;
		
		GraphPage gp;
		//------- CPY 9/12/2007 QA70-10383 SPARKLINES_FOR_ALL_COLS_PLOT_AGAINST_ROW_NUMBERS
		//DataPlot dp = plot_col(iw, nC, gp, IDM_PLOT_LINE, true, template);
		DataPlot dp = plot_col(iw, nC, gp, IDM_PLOT_LINE, true, template, true, xy);
		//-------
		if(dp)
		{
			if(endpts) add_end_points(dp);
			
			if(0 == nn)
				//wks_set_col_label_heights(iw, RCLT_SPARKLINE, row_height);
				wks_set_col_label_heights(iw, RCLT_SPARKLINE, row_height, false); /// Hong 11/01/07 v8.0738 SPARKLINES_SHOULD_NOT_HAVE_THEME_UNDOABLE
				
			nn++;
			iw.EmbedGraph(RCLT_SPARKLINE, nC, gp, 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 sparklines_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return sparklines_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	_check_update_from_to(trGetN);
	/// Max 10/06/08 v8.0951c RYAN_SUGGEST_SELECTION_SHOULD_DISABLE_COL_FROM_TO
	bool bChangeSel = lstrcmpi(lpcszNodeName, "sel")==0 ? true : false;
	if (GETNE_ON_INIT==nEventID || GETNE_ON_THEME==nEventID || bChangeSel)
	{	
		trGetN.c1.Enable = !(trGetN.sel.nVal);
		trGetN.c2.Enable = !(trGetN.sel.nVal);
	}
	/// END RYAN_SUGGEST_SELECTION_SHOULD_DISABLE_COL_FROM_TO
	return true;

}

int sparklines_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 sparklines_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	if(nGetNDialog > 0)
	{
		Worksheet iw(trGetN.iw.strVal);
		if(iw && trGetN.sel.nVal)
		{
			int r1,r2,c1,c2;
			int nSel = iw.GetSelectedRange(r1, c1, r2, c2);
			if(nSel & WKS_SEL_COLUMN)
			{
				//---- CPY 9/12/2007 QA70-10383 SPARKLINES_FOR_ALL_COLS_PLOT_AGAINST_ROW_NUMBERS
				// better always opens the dialog
				//nRet = XFEVT_PROCEED_NO_DLG;
				//----
			}
			else
			{
				_check_update_from_to(trGetN);
				Page pg = iw.GetPage();
				if(page_is_active(pg))
					trGetN.iw.Show = 0;
				trGetN.sel.nVal = 0;
				trGetN.sel.Show = 0;
			}
		}
	}
	
	
	
}
//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 sparklines_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
