#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
#include <sys_utils.h>
#include <..\originlab\fu_utils.h>
#include <..\originlab\XFunctionEx.h>
#include <..\System\oExtFile.h>
#include <..\originlab\theme_utils.h>

#ifdef _FOR_SMART_LOADING_ONLY
	#include <..\Originlab\file_utils.h>
#endif

#include <..\Originlab\hekafile.h>
//put your own support static functions here
////////////////////////////////////////////////////////////////////
/// Modification Log:
/// Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
/// Kenny 12/24/2009 QA81-14875-P2 PROGRESS_BAR_IN_BUILTIN_TOOL_SHOULD_NOT_KEEP_TOPMOST
/// Hong 02/10/10 QA80-15094 HEKA_SUPPORT_BIGENDIAN_FILE_CREATED_IN_MAC
////////////////////////////////////////////////////////////////////

static bool s_bFileValid = true;
#define OK_SURE(_OK_ENABLE) _OK_ENABLE = s_bFileValid; //also can be _OK_ENABLE = s_bFileValid && s_bSetting

enum{
	XF_HEKA_VER_NOT_SUPPORT = -1,
	XF_HEKA_OPENED = -2,
	///Sophy 8/12/2010 ORG-782 BETTER_ERR_MSG_FOR_HEKA_IMPORT_WHEN_PUL_FILE_MISSING
	XF_HEKA_PUL_FILE_MISSING = -3,
	XF_HEKA_PGF_FILE_MISSING = -4,
	///end BETTER_ERR_MSG_FOR_HEKA_IMPORT_WHEN_PUL_FILE_MISSING
	XF_HEKA_OK = 1
};

void update_sparkline_ex2(int nSparklines)
{
	if(SPARKLINE_NO_CHANGE == nSparklines)
		return;
	
	int nCols=0, iWks;
	Worksheet wks;
	bool bIsAbort = false;
	
	Folder fld = Project.ActiveFolder();
	foreach(PageBase pb in fld.Pages)
	{
		if(pb.GetType() != EXIST_WKS )continue;
		WorksheetPage wksPage(pb);
		
		iWks=-1;
		while(1)
		{
			wks = wksPage.Layers(++iWks);
			if(!wks)break;
		
			nCols += wks.GetNumCols();
		}
	}
	
	if(nCols > 50 && SPARKLINE_ADD_IF_LESS_THAN_50_COLS == nSparklines)
		return;
	
	///Kenny 12/24/2009 QA81-14875-P2 PROGRESS_BAR_IN_BUILTIN_TOOL_SHOULD_NOT_KEEP_TOPMOST
	//progressBox aa("Adding sparklines, please wait:", PBOX_TOPMOST, false);
	progressBox aa("Adding sparklines, please wait:", PBOX_DEFAULT_STYLE, false);
	///End QA81-14875-P2 PROGRESS_BAR_IN_BUILTIN_TOOL_SHOULD_NOT_KEEP_TOPMOST
	aa.SetRange(0, nCols);
	
	DWORD dwCntrl = EMBEDGRAPH_IN_LABELS | EMBEDGRAPH_HIDE_AXES | EMBEDGRAPH_HIDE_LEGENDS | EMBEDGRAPH_HIDE_SPECTRUMS | EMBEDGRAPH_HIDE_TEXT_OBJS;
	int cc=0;
	
	foreach(pb in fld.Pages)
	{
		if(pb.GetType() != EXIST_WKS )continue;
		WorksheetPage wksPage(pb);
		
		iWks=-1;
		while(1)
		{
			wks = wksPage.Layers(++iWks);
			if(!wks)break;
		
			wks_set_col_label_heights(wks, RCLT_SPARKLINE, 200, false);
		
			for(int nC = 0; nC < wks.GetNumCols(); ++nC)
			{
				string str;
				str.Format("Current at %s:%s:%s", wksPage.GetName(), wks.GetName(), wks.Columns(nC).GetName());
				if(aa.Set(cc++))
				{
					aa.SetText(str, PBOXT_TITLE);
				}
				else
				{
					if(!bIsAbort)
					{
						bIsAbort = true;
						printf("User abort!");
					}
					break;
				}
				SetStatusBarText(str);
				
				GraphPage gp;
				DataPlot dp = plot_col(wks, nC, gp, IDM_PLOT_LINE, true, "sparkline_label", true, 0);
				wks.EmbedGraph(RCLT_SPARKLINE, nC, gp, dwCntrl);
			}
		}
	}
}


int heka_detect_file_type(string fname, bool &bBundle)
{
	file fin;
	if( !fin.Open(fname, file::modeRead))
		return false;
	
	int nMagicNum;
	fin.Read(&nMagicNum, sizeof(int));
	
	// Hard code
	if(nMagicNum == HEKA_BUN_SWAPDAT2 || nMagicNum == HEKA_BUN_DAT2)
		bBundle = 1;
	else
		bBundle = 0;
	
	fin.Close();
	
	return true;
}

#define NUM_LEVELS	2

void con_tr_heka_to_selection(TreeNode &tr1, TreeNode &tr2, int depth)
{
	string str;
	int val;
	foreach(TreeNode cNode in tr1.Children){
		if (cNode.GetNodeCount() > 0 && depth < NUM_LEVELS-1){
			//if(depth>=2)return;
			TreeNode cNode2 = tr2.AddNode();
			cNode.GetAttribute(STR_LABEL_ATTRIB, str);
			cNode2.SetAttribute(STR_LABEL_ATTRIB, str);
			cNode2.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_CHECK_CONTROL | GETNBRANCH_OPEN|GETNBRANCH_CHECK_CONTROL_ONE_EVENT);
			con_tr_heka_to_selection(cNode, cNode2, depth+1);
		}
		else if(depth == NUM_LEVELS-1){
			TreeNode cNode2 = tr2.AddNumericNode(1, "", TRGP_CHECK);
			cNode.GetAttribute(STR_LABEL_ATTRIB, str);
			
			ASSERT(0 != cNode.GetNodeCount());
			
			string strName;			
			if(1 == cNode.GetNodeCount())
				strName = str;
			else
				strName.Format("%s(#1-#%d)", str, cNode.GetNodeCount());
			
			cNode2.SetAttribute(STR_LABEL_ATTRIB, strName);
		}
	}

}


bool read_heka_info(file &fin, TreeNode &tr, string &strInfo)
{
	/// Hong 02/10/10 QA80-15094 HEKA_SUPPORT_BIGENDIAN_FILE_CREATED_IN_MAC
	BOOL			bNeedReverse = false;
	tr.GetAttribute(STR_HEKA_PUL_NEED_REVERSE, bNeedReverse);
	/// end HEKA_SUPPORT_BIGENDIAN_FILE_CREATED_IN_MAC
	string strTmp;
	int nVer, nBase;
	tr.GetAttribute(STR_HEKA_VER, nVer);
	tr.GetAttribute(STR_HEKA_BASE, nBase);
	switch(nVer)
	{
	case 8:
		PulRootRecord8 prr8;
		/// Hong 02/10/10 QA80-15094 HEKA_SUPPORT_BIGENDIAN_FILE_CREATED_IN_MAC
		//heka_read_parameter(&fin, nBase, HEKA_PARAM_ROOT_V8, &prr8);
		heka_read_parameter(&fin, nBase, HEKA_PARAM_ROOT_V8, &prr8, bNeedReverse);
		/// end HEKA_SUPPORT_BIGENDIAN_FILE_CREATED_IN_MAC
		strTmp.Format("Protocol: %d\n", prr8.Version);
		strInfo += strTmp;
		strTmp.Format("Program Version: %s\n", prr8.VersionName);
		strInfo += strTmp;
		strTmp.Format("AuxFileName: %s\n", prr8.FileName);
		strInfo += strTmp;		
		strTmp.Format("Comment: %s\n", prr8.Comments);
		strInfo += strTmp;	
		break;
	case 9:
		PulRootRecord9 prr9;
		/// Hong 02/10/10 QA80-15094 HEKA_SUPPORT_BIGENDIAN_FILE_CREATED_IN_MAC
		//heka_read_parameter(&fin, nBase, HEKA_PARAM_ROOT_V8, &prr9);
		heka_read_parameter(&fin, nBase, HEKA_PARAM_ROOT_V9, &prr9, bNeedReverse);
		/// end HEKA_SUPPORT_BIGENDIAN_FILE_CREATED_IN_MAC
		strTmp.Format("Protocol: %d\n", prr9.RoVersion);
		strInfo += strTmp;
		strTmp.Format("Program Version: %s\n", prr9.RoVersionName);
		strInfo += strTmp;
		strTmp.Format("AuxFileName: %s\n", prr9.RoAuxFileName);
		strInfo += strTmp;		
		strTmp.Format("Root Text: %s\n", prr9.RoRootText);
		strInfo += strTmp;		
		break;
	default:
		ASSERT(FALSE);
		return false;
	}
	
	return true;
}


static int _construct_branch(TreeNode& trFile, string strFileName, string &strInfo)
{
	Tree trPul, trStim ;
		
	bool bBundle;
	if( !heka_detect_file_type(strFileName, bBundle))
		return false;
	
	if(!bBundle){
		int nPos1, nPos2;
		nPos2 = strFileName.ReverseFind('.');			
		nPos1 = strFileName.ReverseFind('\\');

		if(nPos1 < 0 || nPos2 < 0){
			printf("Fail to open file!");
			return false;
		}

		string strPath = strFileName.Mid(0, nPos1+1);
		string strName = strFileName.Mid(nPos1+1, nPos2-nPos1-1);

		if( !FindFile(strName+".pul", strPath.GetBuffer(0), "Originlab", FALSE) ){
			printf("Fail to open file!");
			///Sophy 8/12/2010 ORG-782 BETTER_ERR_MSG_FOR_HEKA_IMPORT_WHEN_PUL_FILE_MISSING
			//return false;
			return XF_HEKA_PUL_FILE_MISSING;
			///end BETTER_ERR_MSG_FOR_HEKA_IMPORT_WHEN_PUL_FILE_MISSING
		}
		
		file fin;
		if( !fin.Open(strPath+strName+".pul", file::modeRead))
			return XF_HEKA_OPENED;
		
		if(fin.IsOpen())
			heka_read_pulse(&fin, 0, &trPul, -1);
		else
			ASSERT(false);
		
		if(!read_heka_info(fin, trPul, strInfo))
			return XF_HEKA_VER_NOT_SUPPORT;
		
		fin.Close();
	}
	else
	{
		file fin;
		if( !fin.Open(strFileName, file::modeRead))
			return XF_HEKA_OPENED;
		heka_read_bundle_header(&fin, &trPul, &trStim);
		
		if(!read_heka_info(fin, trPul, strInfo))
			return XF_HEKA_VER_NOT_SUPPORT;
		
		fin.Close();
	}
	
	con_tr_heka_to_selection(trPul, trFile, 0);
		
	return XF_HEKA_OK;
}


static int _construct_GUI_tree(TreeNode& trGUI, vector<string>& vsFileNames)
{
	int nCount = vsFileNames.GetSize();
	TreeNode trFile;

	trGUI.RemoveChildrenWithPrefix();

	string strTagName, strInfo, strTmp;
	for ( int nIndex = 0; nIndex < nCount; nIndex++ )
	{
		strTagName.Format("file%d", nIndex+1);
		trFile = trGUI.AddNode(strTagName);
		trFile.SetAttribute(STR_LABEL_ATTRIB, GetFileName(vsFileNames[nIndex], false));
		trFile.SetAttribute(STR_ATTRIB_BRANCH,  GETNBRANCH_CHECK_CONTROL| GETNBRANCH_OPEN|GETNBRANCH_CHECK_CONTROL_ONE_EVENT);
		
		// Add file info
		GETN_USE(trFile)
		GETN_MULTILINE_TEXT(InfoFileNormal, _L("File protocol"), "") GETN_OPTION_MUTILINE_TEXT_BOX(true, "2-15", true)
	
		strInfo = "";
		int nPos = vsFileNames[nIndex].ReverseFind('\\');
		strTmp.Format("Data File: %s\n", vsFileNames[nIndex].Mid(nPos+1, strlen(vsFileNames[nIndex])-nPos-1));
		strInfo += strTmp;
		strTmp.Format("File Size: %s\n", GetFileSize(vsFileNames[nIndex]));
		strInfo += strTmp;

		// Constract selection tree
		int nRet = _construct_branch(trFile, vsFileNames[nIndex], strInfo);
		if ( nRet != XF_HEKA_OK )
		{
			if( nRet == XF_HEKA_OPENED )
			{
				// Check if file exist
				WIN32_FIND_DATAA FindFileData;
				HANDLE hFind = FindFirstFile(vsFileNames[nIndex], &FindFileData);
				if (hFind != INVALID_HANDLE_VALUE) 
	   			{
					warning_msg_box(_L("Some files are currently opened by other software\n"), false, 'E');
					return XF_HEKA_OPENED;
	   			}
			}
	   		else if( nRet ==  XF_HEKA_VER_NOT_SUPPORT )
	   		{
				warning_msg_box(_L("Unsupported data version, please contact OriginLab Corp. for further information\n"), false, 'E');
				return XF_HEKA_VER_NOT_SUPPORT;	   			
	   		}
	   		///Sophy 8/12/2010 ORG-782 BETTER_ERR_MSG_FOR_HEKA_IMPORT_WHEN_PUL_FILE_MISSING
	   		else if ( nRet == XF_HEKA_PUL_FILE_MISSING )
	   		{
	   			string strPulFileName = vsFileNames[nIndex];
	   			LPSTR lpFileName = strPulFileName.GetBuffer(strPulFileName.GetLength() + 4);
	   			check_add_file_ext(lpFileName, "pul");
	   			strPulFileName.ReleaseBuffer();
	   			string strErrMsg;
	   			strErrMsg.Format("%s is needed for import %s", strPulFileName, vsFileNames[nIndex]);
	   			warning_msg_box(strErrMsg, false, 'E');
	   			return XF_HEKA_PUL_FILE_MISSING;
	   		}
	   		///end BETTER_ERR_MSG_FOR_HEKA_IMPORT_WHEN_PUL_FILE_MISSING
			string strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
			int nErrMsg = atoi(strErrMsg);
			warning_msg_box(nErrMsg, false, 'E');
			return -1;

		}
				
		TreeNode trTmp = trGUI.GetNode(strTagName);
		trTmp.InfoFileNormal.strVal = strInfo;

	}
	trGUI.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN);
	
	return 1;
}


static int construct_tree_setting(const string& strMultiFileName, TreeNode& trSetting)
{
	StringArray vsFileNames;
	separate_multiline_files(strMultiFileName, vsFileNames); 

	return _construct_GUI_tree(trSetting, vsFileNames);
}


static int file_name_event(TreeNode& trGetN, string& strErrMsg, bool bClearSetting = true)
{
	string strFileName = trGetN.fname.strVal;
	TreeNode trNode = trGetN.trfiles;

	if ( bClearSetting )
		trNode.RemoveChildrenWithPrefix();

	int nRet = construct_tree_setting(strFileName, trNode);
	if(nRet != XF_HEKA_OK)
	{
		trNode.RemoveChildrenWithPrefix();
		strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
		return nRet;
	}

	return 1;
}



void impHEKA(const string& fname, const TreeNode& trfiles, const TreeNode& options, Range& orng, TreeNode& trheader, const int& reimp)
{
	
	DWORD dwCtrl=0;
	int nImpMode, nMode;
	TreeNode trTemp = options.GetNode("ImpLeak");
	if(trTemp.nVal == true)		
		dwCtrl = dwCtrl | HEKA_IMP_LEAK;
		
	trTemp = options.GetNode("SubLeak");
	if(trTemp.nVal == true)		
		dwCtrl = dwCtrl | HEKA_SUB_LEAK;
		
	trTemp = options.GetNode("SubZero");
	if(trTemp.nVal == true)
		dwCtrl = dwCtrl | HEKA_SUB_ZERO;
	
	trTemp = options.GetNode("ShowX");
	if(trTemp.nVal == true)		
		dwCtrl = dwCtrl | HEKA_SHOW_X;
	
	trTemp = options.GetNode("ImpMode");
	if(!trTemp)
		nImpMode = 0;
	else
		nImpMode = trTemp.nVal;
	
	StringArray vsFileNames;
	separate_multiline_files(fname, vsFileNames); 
	int nCount = vsFileNames.GetSize();
	
	TreeNode trFile = trfiles.FirstNode; 
	
	switch(nImpMode)
	{
	case 0:
		nMode = nCount > 1 ? HEKA_MODE_SINGLE_BOOK:HEKA_MODE_MULTI_BOOK;
		break;
	case 1:
		nMode = HEKA_MODE_MULTI_BOOK;
		break;
	case 2:
		nMode = HEKA_MODE_SINGLE_BOOK;
		break;
	default:
		nMode = 0;
		break;
	}
	
	string strRootPath = Project.ActiveFolder().GetPath();
		
	for(int ii=0; ii<nCount; ++ii)
	{
		bool bBundle;
		if( !heka_detect_file_type(vsFileNames[ii], bBundle)){
			// Check if file exist
			WIN32_FIND_DATAA FindFileData;
			HANDLE hFind = FindFirstFile(vsFileNames[ii], &FindFileData);
			if (hFind != INVALID_HANDLE_VALUE) 
	   		{
				warning_msg_box(_L("Some files are currently opened by other software\n"), false, 'E');
	   		}
	   		else
	   		{
				string strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
				int nErrMsg = atoi(strErrMsg);
				warning_msg_box(nErrMsg, false, 'E');
	   		}
			continue;
		}
		
		if(HEKA_MODE_MULTI_BOOK == nMode){
			Project.ActivateFolder(strRootPath);
			Folder fld = Project.ActiveFolder();
			string strName = _get_file_name_heka(vsFileNames[ii]);		
			fld.AddSubfolder(strName);
			Project.ActivateFolder(strRootPath+strName+"/");
		}
		
		if(!bBundle){
			int nPos1, nPos2;
			nPos2 = vsFileNames[ii].ReverseFind('.');			
			nPos1 = vsFileNames[ii].ReverseFind('\\');
			if(nPos1 < 0 || nPos2 < 0){
				printf("Fail to open file!");
				continue;
			}
	
			string strPath = vsFileNames[ii].Mid(0, nPos1+1);
			string strName = vsFileNames[ii].Mid(nPos1+1, nPos2-nPos1-1);
	
			///Sophy 8/12/2010 ORG-782 BETTER_ERR_MSG_FOR_HEKA_IMPORT_WHEN_PUL_FILE_MISSING
			//if( !FindFile(strName+".pul", strPath.GetBuffer(0), "Originlab", FALSE) || !FindFile(strName+".pgf", strPath.GetBuffer(0), "Originlab", FALSE) ){
			//	printf("Fail to find file!");
			//	continue;
			//}
	
			int nHekaFileState = XF_HEKA_OK;
			if ( !FindFile(strName+".pul", strPath.GetBuffer(0), "Originlab", FALSE) )
			{
				nHekaFileState = XF_HEKA_PUL_FILE_MISSING;
			}
			else if ( !FindFile(strName+".pgf", strPath.GetBuffer(0), "Originlab", FALSE) )
			{
				nHekaFileState = XF_HEKA_PGF_FILE_MISSING;
			}
			if ( XF_HEKA_OK != nHekaFileState )
			{
				printf("Fail to find file!");
				string strPulFileName = vsFileNames[ii];
	   			LPSTR lpFileName = strPulFileName.GetBuffer(strPulFileName.GetLength() + 4);
	   			check_add_file_ext(lpFileName, XF_HEKA_PUL_FILE_MISSING == nHekaFileState ? "pul" : "pgf");
	   			strPulFileName.ReleaseBuffer();
	   			string strErrMsg;
	   			strErrMsg.Format("%s is needed for import %s", strPulFileName, vsFileNames[ii]);
	   			warning_msg_box(strErrMsg, false, 'E');
	   			continue;
			}
			///end BETTER_ERR_MSG_FOR_HEKA_IMPORT_WHEN_PUL_FILE_MISSING
			
			HekaFile heFile;
			if( !heFile.Open(strPath+strName+".dat", strPath+strName+".pul", strPath+strName+".pgf"))
				printf("fail to open files\n");
			else
				heFile.Import(dwCtrl, trFile, nMode);
		}
		else{
			
			HekaFile heFile(vsFileNames[ii]);
			heFile.Open();
			heFile.Import(dwCtrl, trFile, nMode);
		}
		
		// Suport Sparklines
		WorksheetPage pg = Project.WorksheetPages(-1);
		Worksheet wks;
		Column col;
		
		if(!trFile)
			return;
		
		int nSparklines = options.Sparklines.nVal;
		
		ASSERT(pg);
	
		if(HEKA_MODE_SINGLE_BOOK == nMode)
		{
			update_sparkline_ex(nSparklines, pg);
		}
		else if(HEKA_MODE_MULTI_BOOK == nMode)
		{
			update_sparkline_ex2(nSparklines);
		}
			
		trFile = trFile.NextNode;
	}
	
	//Project.ActivateFolder(strRootPath);
	
}

//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 impHEKA_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impHEKA_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	OK_SURE(bOKEnable);
	
	if ( GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID )
	{
		s_bFileValid = file_name_event(trGetN, strErrMsg, false);
		OK_SURE(bOKEnable);
		return true;
	}
	
	string strTagName = lpcszNodeName;
	if(strTagName == "fname")
	{
		s_bFileValid = file_name_event(trGetN, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
	}
	
	return false;
	

}

int impHEKA_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	
	string strTagName = lpcszVarName;
	
	if ( 0 == strcmp(lpcszVarName, "options") )
	{
		constructOptionsForXF(tr, false, false, true, false, false);
		
		fuSetShowTime(tr, 0);
		fuSetXFunctionName(tr, "impHEKA");
		
		// Speed issue
		tr.Sparklines.nVal = SPARKLINE_NO_CHANGE;
		
		TreeNode trTemp = tr.AddNode("ImpMode");
		trTemp.SetAttribute(STR_LABEL_ATTRIB, _L("Import Mode"));
		trTemp.SetAttribute(STR_COMBO_ATTRIB, _L("Auto|Import Group as Workbook|Import Group as Worksheet"));
		trTemp.DataID = GET_USER_DATAID(5);
		trTemp.ID = TRGP_ENUM_LIST;
		trTemp.nVal = 1;
		
		// Import Leak Traces
		trTemp = tr.AddNode("ImpLeak");
		trTemp.SetAttribute(STR_LABEL_ATTRIB, _L("Import Leak Traces"));
		trTemp.DataID = GET_USER_DATAID(1);
		trTemp.ID = TRGP_CHECK;
		trTemp.nVal = 0;
		
		// Subtract Leak Traces
		trTemp = tr.AddNode("SubLeak");
		trTemp.SetAttribute(STR_LABEL_ATTRIB, _L("Subtract Leak Traces"));
		trTemp.DataID = GET_USER_DATAID(2);
		trTemp.ID = TRGP_CHECK;
		trTemp.nVal = 1;
		
		// Subtract Zero offset
		trTemp = tr.AddNode("SubZero");
		trTemp.SetAttribute(STR_LABEL_ATTRIB, _L("Subtract Zero offset"));
		trTemp.DataID = GET_USER_DATAID(3);
		trTemp.ID = TRGP_CHECK;
		trTemp.nVal = 1;
			
		// Subtract Zero offset
		trTemp = tr.AddNode("ShowX");
		trTemp.SetAttribute(STR_LABEL_ATTRIB, _L("Show X-Axis"));
		trTemp.DataID = GET_USER_DATAID(4);
		trTemp.ID = TRGP_CHECK;
		trTemp.nVal = 0;
		
		return 0;
	}
	
	if ( strTagName == "trfiles" )
	{
		return 0;
	}
	
	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 impHEKA_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	if(nGetNDialog > 0)
	{
		string strFile = trGetN.fname.strVal;
		if(strFile == "fname$")
			trGetN.fname.strVal = "";
	
		trGetN.trheader.Show = 0;
		if(1 == trGetN.reimp.nVal)
		{
			trGetN.fname.Enable = 2;
			trGetN.orng.Enable = 2;
			trGetN.trfiles.Enable = 2;
		}
	
	}
	
	if(nGetNDialog >= 0)
	{
	
		if ( LTXF_FROM_GUI_MENU == dwCntrl )
			trGetN.fname.strVal = "";
	}
	
	if(nGetNDialog > -1)
	{
		bool bResetFileInfo = false; ///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		
		string strFileName = trGetN.fname.strVal;
	
		if ( LTXF_FROM_GUI_MENU & dwCntrl )
			strFileName = "";
	
		if(is_empty_or_unassigned(strFileName))
		{			
			int nFilesRet;
			if ( 0 != nGetNDialog )
			{
				int nShowOptionsDlg = 1;
				///---Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
				//nFilesRet = get_open_box_by_file_group(strFileName, "HEKA","HEKA",
					//OPENBOX_FLAG_MULTIFILES, NULL, NULL, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				nFilesRet = get_open_box_by_file_group(strFileName, "HEKA","HEKA",
					OPENBOX_FLAG_MULTIFILES, NULL, NULL, get_show_options_label(trGetN), &nShowOptionsDlg);
				///---END QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
				nRet = (nShowOptionsDlg ? XFEVT_PROCEED : XFEVT_PROCEED_NO_DLG);
			}
			else
			{
				nFilesRet = get_open_box_by_file_group(strFileName, "HEKA", "HEKA", true);
			}
	
			if ( 0 == nFilesRet )
			{
				nRet = XFEVT_ABORT;
				return;
			}
			trGetN.fname.strVal = strFileName;
			bResetFileInfo = true; ///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		} ///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION			
		
		string strErrMsg;
		///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		//int nErr = file_name_event(trGetN, strErrMsg);
		int nErr = file_name_event(trGetN, strErrMsg, bResetFileInfo);
		///---END QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		if(nErr != XF_HEKA_OK)
		{
			if(nErr != XF_HEKA_OPENED && nErr != XF_HEKA_VER_NOT_SUPPORT && nErr != XF_HEKA_PUL_FILE_MISSING )
			{
				int nErrMsg = atoi(strErrMsg);
				warning_msg_box(nErrMsg, false, 'E');
			}
			nRet = XFEVT_ABORT;
			return;
		}
		///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION			
		//}
		///---END QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
	}
	
	 
}
//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 impHEKA_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
