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


/*
 *	Modification Log:
 * Iris 10/26/06 CHECK_SHOW_ERR_INFO
 * CPY 10/26/06 QA70-9063 DYNAMIC_ERR_MSG_IN_GETN_DIALOGS
 * Jasmine 11/16/06 DISABLE_EMBED_IF_GRAPH_EMBEDDED_ALREADY
 * Jasmine 01/26/07 SUPPORT_MULTIPLE_GRAPHES  
 * Jasmine 06/22/07 INSERT_GRAPH_ERR_CHECK
*/

//put additional include files here
#include <..\originlab\theme_utils.h>
#include <event_utils.h>
//put your own support static functions here
enum
{
	SET_CELL_LINK,
	SET_CELL_EMBED
};
///Jasmine 01/26/07 SUPPORT_MULTIPLE_GRAPHES  
static bool _check_get_pages(string& strPage, vector<string>& vsPages) 
{
	if(strPage.Find("|")>0)
		strPage.GetTokens(vsPages, '|');
	else
		strPage.GetTokens(vsPages, STR_PAGE_LIST_SEPARATOR);//'/n'
	int nSize = vsPages.GetSize();
	for(int ii = 0; ii < nSize; ii++)
	{
		Page pg(vsPages[ii]);
		if(!pg.IsValid())
			break;
	}
	strPage.SetTokens(vsPages, STR_PAGE_LIST_SEPARATOR);
	return (nSize && nSize == ii);
}
///End SUPPORT_MULTIPLE_GRAPHES


void insertGraph(const string& gname, const int& embed, const int& resizecell, const int& keepAspect, const int& axes, const int& label, Range& cell)
{
	///Jasmine 06/22/07 INSERT_GRAPH_ERR_CHECK
	int nErr = check_1_cell(cell);
	if(nErr != CER_NO_ERROR)
	{
		//strErrMsg = nErr;
		if(CER_NO_DATA == nErr)
			XF_THROW_EX(XFERR_INVALID_MSG, _L("top left cell"));
		XF_THROW(nErr);
	}
	///End INSERT_GRAPH_ERR_CHECK
	Worksheet wks;
	int r1,r2,c1,c2, ii;
	if(!cell.GetNumRanges())
		XF_THROW(XFERR_INVALID_ARG)
	cell.GetRange(0, r1, c1, r2, c2, wks);
	if(!wks)
		XF_THROW(XFERR_INVALID_ARG)
	///Jasmine 01/26/07 SUPPORT_MULTIPLE_GRAPHES  
	//GraphPage gp(gname);
	//if(!gp)
		//XF_THROW_EX(CER_INVALID_GRAPH, gname);
	DWORD dwCntrl = EMBEDGRAPH_DO_UNDO;
	if(keepAspect) dwCntrl |= EMBEDGRAPH_KEEP_ASPECT_RATIO;
	if(axes)
		dwCntrl |= EMBEDGRAPH_HIDE_AXES|EMBEDGRAPH_HIDE_LEGENDS | EMBEDGRAPH_HIDE_SPECTRUMS; 
	if(label)
		dwCntrl |= EMBEDGRAPH_HIDE_TEXT_OBJS;
	//---- CPY 12/9/06
	if(!resizecell)
		dwCntrl |= EMBEDGRAPH_NO_RESIZE_CELL;
	//----		
	vector<string> vsPages;
	if(!_check_get_pages(gname, vsPages))
		XF_THROW(CER_PAGE_NAME_WRONG);
	
	UndoBlock block;/// TD 01-28-2008 QA80-10734  UNDO_SUPPORT_FOR_ADD_LINKS_TO_GRAPH
	for(ii = 0, r2 = r1; ii < vsPages.GetSize(); r2++)
	{
		for(c2 = c1; c2 < wks.GetNumCols(); c2++)
		{
			
			GraphPage gp(vsPages[ii]);
			if(SET_CELL_EMBED == embed && !gp.GetEmbeddingInfo())///Jasmine 11/16/06 DISABLE_EMBED_IF_GRAPH_EMBEDDED_ALREADY
			{
				wks.EmbedGraph(r2, c2, gp, dwCntrl);
			}
			else
			{
				//set graph:// to worksheet cell
				string str = "graph://" + vsPages[ii];
				if(dwCntrl > 0)
					str += " " + (int)dwCntrl;
				wks.SaveRangeUndo(r2, c2, r2, c2);/// TD 01-28-2008 QA80-10734  UNDO_SUPPORT_FOR_ADD_LINKS_TO_GRAPH
				wks.SetCell(r2, c2, str);
			}
			ii++;
			if(ii >= vsPages.GetSize())
				break;
		}
		if(ii >= vsPages.GetSize())
			break;
		c2 = c1;
	}
	if(resizecell)
	{
		wks_set_cell_heights(wks, r1, r2, 600, false);
		if(r2 != r1)
			c2 = -1;
		wks_set_cell_heights(wks, c1, c2, 200, true);
	}
	///End SUPPORT_MULTIPLE_GRAPHES
}

//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 insertGraph_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return insertGraph_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	//---- CPY 10/26/06 QA70-9063 DYNAMIC_ERR_MSG_IN_GETN_DIALOGS
	/*
	//----- Iris 10/26/06 CHECK_SHOW_ERR_INFO
	int nErr = CER_NO_ERROR;
	nErr = check_1_cell(trGetN.cell.strVal);
	if(CER_NO_ERROR == nErr)
	{
		string 		strGraph = trGetN.gname.strVal;			
		GraphPage 	gp(strGraph);
		if(!gp)
			nErr = CER_NO_GRAPH;		
	}
	DynaCntrlContainer.PostMessage(WM_USER_MSG_BOX, nErr);
	//-----
	*/
	int nErr = check_1_cell(trGetN.cell);
	if(CER_NO_ERROR == nErr)
	{
		string 		strGraph = trGetN.gname.strVal;			
		///Jasmine 01/26/07 SUPPORT_MULTIPLE_GRAPHES 
		//GraphPage 	gp(strGraph);
		//if(!gp)
		vector<string> vsPages;
		if(!_check_get_pages(strGraph, vsPages))
			nErr = CER_PAGE_NAME_WRONG;//CER_NO_GRAPH;
		trGetN.gname.strVal = strGraph;		
		///End SUPPORT_MULTIPLE_GRAPHES
		//don't check here, this will do in insertGraph
		/////Jasmine 11/16/06 DISABLE_EMBED_IF_GRAPH_EMBEDDED_ALREADY
		//if(!lstrcmpi(lpcszNodeName, "gname") && gp.IsValid())
		//{
			//trGetN.embed.Enable = !gp.GetEmbeddingInfo();
			//trGetN.embed.nVal = gp.GetEmbeddingInfo()? SET_CELL_LINK : trGetN.embed.nVal;			
		//}
		/////End DISABLE_EMBED_IF_GRAPH_EMBEDDED_ALREADY
	}
	if(nErr != CER_NO_ERROR)
	{
		bOKEnable = false;
		strErrMsg = nErr;
		///Jasmine 06/22/07 INSERT_GRAPH_ERR_CHECK
		if(CER_NO_DATA == nErr)
		{
			strErrMsg = XFERR_INVALID_MSG;
			strErrMsg += ":" + _L("top left cell");
		}
		///End INSERT_GRAPH_ERR_CHECK
		return false;
	}
	//---- end DYNAMIC_ERR_MSG_IN_GETN_DIALOGS
	return true;

}

int insertGraph_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 insertGraph_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	TreeNode 	trGraph = trGetN.gname;
	///Jasmine 01/26/07 SUPPORT_MULTIPLE_GRAPHES
	//trGraph.SetAttribute(STR_MULTI_PAGE_ATTRIB, false);
	trGraph.SetAttribute(STR_MULTI_PAGE_ATTRIB, true);
	///Jasmine 06/22/07 INSERT_GRAPH_ERR_CHECK
	if(1 > nGetNDialog)
	{
		int nErr = check_1_cell(trGetN.cell);
		if(CER_NO_DATA == nErr)
			trGetN.cell.strVal = "";//clean invalid range string before execute
	}
	///End INSERT_GRAPH_ERR_CHECK
}
//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 insertGraph_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
