#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
#include <..\Originlab\theme_utils.h>
//put your own support static functions here
	/// ML 8/3/2010 ORG-700 ADDING_TABLEOBJ_VIA_CONTEXTMENU_OUGHT_TO_RESPECT_CLICKED_POSITION



void add_table_to_graph(const Layer& igl, const int& cols, const int& rows, const string& title, const int& col, string& tabname)
{
	
	///Cheney 2007-6-20 ADD_INPUT_GRAPH_AND_OUTPUT_TABLE_NAME
	//GraphLayer gl = Project.ActiveLayer();
	//if(!gl)
	//	return;
	if(!igl)
		XF_THROW(XFERR_INVALID_GRAPH_LAYER);
	///end ADD_INPUT_GRAPH_AND_OUTPUT_TABLE_NAME
	
	Worksheet wks;
	wks.Create("Table", CREATE_HIDDEN);//CREATE_VISIBLE);
	
	//------ Folger 12/05/07 LOCALIZE_UPDATE_TABLE_GRAPH_OBJECT
	GraphObject go = wks.GraphObjects("Update");
	if (go)
		go.Text = GetLocalized(E_STR_UPDATE_TABLE);
	//------
	
	WorksheetPage wp = wks.GetPage();
	//	wp.SetShow(PAGE_MINIMIZED);
	wp.Rename("Table1");
	wks.SetSize(rows, cols);
	int nLabel1 = title.GetLength() > 0? RCLT_COMMENT:-1;
	int nLabel2 = col > 0? RCLT_LONG_NAME:-1;
	if(nLabel1 < 0 && nLabel2 >= 0)
	{
		nLabel1 = nLabel2;
		nLabel2 = -1;
	}
		
	if(wks_set_show_labels(wks, nLabel1, nLabel2))
	{
	/*		vector<int> vnCols = {1};
		vector<int> vnRows = {1}; // hide both col and row header but show labels if present
		wks_set_hide_headings(wks, vnCols, vnRows);*/
		
		if(title.GetLength() > 0)
		{
			foreach(Column cc in wks.Columns)
				cc.SetComments(title);
	
			/// EJP 2010-07-07 ORG-475 PROPER_MERGE_COMMENT_CELLS
			int nCommentRow = 0;
		
			ORANGE rng;
			rng.r1 = nCommentRow;
			rng.c1 = 0;
			rng.r2 = nCommentRow;
			rng.c2 = -1;
		
			Grid gg;
		    	gg.Attach(wks);
		    	gg.MergeCells(rng, true); // true = include labels in row offsets
			/// end PROPER_MERGE_COMMENT_CELLS
		}
	
		//CPY 8/9/06, per Snow, better not fill default col name
		/*
		if(col)
		{
			int ii = 1;
			foreach(Column cc in wks.Columns)
				cc.SetLongName("Column " + (ii++));				
		}
		*/
	}
	
	/// ML 8/3/2010 ORG-700 ADDING_TABLEOBJ_VIA_CONTEXTMENU_OUGHT_TO_RESPECT_CLICKED_POSITION
	/////Cheney 2007-6-20 ADD_INPUT_GRAPH_AND_OUTPUT_TABLE_NAME
	////GraphObject grTable = gl.CreateLinkTable(wp.GetName(), wks, -1, CLTBL_NO_FIRE_EVENT);//|CLTBL_NO_AUTO_SIZE);
	//GraphObject grTable = igl.CreateLinkTable(wp.GetName(), wks, -1, CLTBL_NO_FIRE_EVENT);//|CLTBL_NO_AUTO_SIZE);
	/////end ADD_INPUT_GRAPH_AND_OUTPUT_TABLE_NAME
	GraphObject grTable = igl.CreateLinkTable(wp.GetName(), wks, -1, CLTBL_NO_FIRE_EVENT | CLTBL_CURRENT_POS);//|CLTBL_NO_AUTO_SIZE);
	/// end ADDING_TABLEOBJ_VIA_CONTEXTMENU_OUGHT_TO_RESPECT_CLICKED_POSITION
	
	Tree trTheme;
	Grid gg;
	grTable.GetGrid(gg);
	if(gg)
	{
		if(theme_get_from_wks_grid(wks, trTheme, FPB_ALL, FOB_ALL, TH_DEL_GRID_COL_HEADING | TH_DEL_GRID_ROW_HEADING))
			gg.ApplyFormat(trTheme, true, true);
	}
	
	///Cheney 2007-6-20 ADD_INPUT_GRAPH_AND_OUTPUT_TABLE_NAME
	if(tabname)
		tabname = wp.GetName();
	///end ADD_INPUT_GRAPH_AND_OUTPUT_TABLE_NAME
	
	
}

//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 add_table_to_graph_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return add_table_to_graph_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int add_table_to_graph_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 add_table_to_graph_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int 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 add_table_to_graph_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
