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

#include <GetNBox.h>
#include <..\originlab\theme_utils.h> ///---Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE

#include <..\originlab\file_utils.h>
//#include <..\originlab\Filter_Utils.h>  /// AW 08/16/06 BACK_ONE_SPE_XF
//#include <..\originlab\import_utils.h>  /// AW 08/16/06 BACK_ONE_SPE_XF
#include <..\originlab\fu_utils.h> ///---Sim 09-11-2006 SUPPORT_MULTI_FILES_INXF
#include <..\originlab\XFunctionEx.h> ///---Sim 09-26-2006 SUPPORT_SPARKLINES
#include <iw_xf.h>

#include <oErrMsg.h> ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
#include <..\originlab\SPE_Utils.h>

///////////////////////////////////////////////////////////////////////
//////// modification log
/// Sim 03-22-2007 FIX_CHECK_FILE_NAME_PASS_TO_XF
/// Hong 04/10/07 v8.0597b IGNORE_FNAME$_WHEN_FROM_MENU
/// Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
/// Hong 05/14/07 v8.0617 NO_NEED_DUMP_THIS_INFO_AS_SAVE_TO_ORGANIZER
/// Sim 06-25-2007 UPDATE_ORGANIZER
/// Hong 08/01/07 v8.0671b MATRIX_START_NEW_COLUMNS_CHANGE_NEW_SHEET
/// Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
/// Hong 01/08/08 QA80-10541 FIX_REDUNDANT_MOBJ_WHEN_IMPORT_TO_MATRIX_WKS
/// Hong 07/21/08 v8.0905 REMOVE_REPLACE_MODE_FROM_MULTI_IMPORT_MODE
/// Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
/// Hong 11/11/09 FIX_TDM_RUNTIME_ERROR
///////////////////////////////////////////////////////////////////////

///---Sim 09-27-2006 SHOW_IMPORT_MODE_BOX
enum {
	IML_REPLACE = 0, // ASCIMP_MODE_REPLACE_DATA,
	IML_NEW_BK, // ASCIMP_MODE_NEW_BOOKS,
	IML_NEW_SHT, // ASCIMP_MODE_NEW_SHEETS,
	/// Hong 01/27/07 ADD_APPEND_COLUMN_MODE
	IML_NEW_COL, // ASCIMP_MODE_APPEND_COLS
	/// end ADD_APPEND_COLUMN_MODE
};
//#define IML_NUM_MODES 3
#define IML_NUM_MODES 4 // Hong 01/27/07 ADD_APPEND_COLUMN_MODE
///---END SHOW_IMPORT_MODE_BOX

///---Sim 11-16-2006 RELIABLE_OK_ENABLE
static bool s_bFileValid = true;
#define OK_SURE(_OK_ENABLE) _OK_ENABLE = s_bFileValid; //also can be _OK_ENABLE = s_bFileValid && s_bSetting
///---END RELIABLE_OK_ENABLE

///---Sim 09-29-2006 SHOW_MULTI_FILE_INFO

static BOOL construct_tree_setting(StringArray& vsFileNames, TreeNode& trInfo)
{
	int nCount = vsFileNames.GetSize();
	string strFileName, strTag, strInfo;
	//TreeNode trFile, trFileInfo;
	if ( trInfo )
		trInfo.RemoveChildrenWithPrefix();
	GETN_USE(trInfo)
	for ( int nIndex = 0; nIndex < nCount; nIndex++ )
	{
		strFileName = vsFileNames[nIndex];
		if ( !strFileName.IsFile() )
			return false;
		///---Sim 11-08-2006 FIX_SET_TAGNAME_AS_FILE_NAME
		//string strTag = GetFileName(strFileName, true);
		//strTag.MakeValidCName('a');
		strTag.Format("file%d", nIndex+1);
		///---END FIX_SET_TAGNAME_AS_FILE_NAME
//		trFile = trInfo.AddNode(strTag);
//		trFile.SetAttribute(STR_LABEL_ATTRIB, GetFileName(strFileName, false));
//		trFileInfo = trFile.AddNode("FileInfo");
		strInfo.Format("File Size: %s ", GetFileSize(strFileName));
		GETN_STR(strTag, GetFileName(strFileName, false), strInfo )
		GETN_READ_ONLY
//		GETN_MULTILINE_TEXT(INFO, "HeaderInfo", strInfo) 
	}
	return true;
}
///---END SHOW_MULTI_FILE_INFO

///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
static bool file_name_event(TreeNode& trGetN, string& strErrMsg)
{
	string strFileName = trGetN.fname.strVal;
	TreeNode trNode = trGetN.info;

	// clean the trFiles on GUI
	trNode.RemoveChildrenWithPrefix();
	
	if ( !check_imported_file_name(strFileName, strErrMsg) )
		return false;
	
	StringArray vsFileNames;
	separate_multiline_files(strFileName, vsFileNames);
	if ( !construct_tree_setting(vsFileNames, trNode) )
	{
		// clean the trFiles on GUI
		trNode.RemoveChildrenWithPrefix();
		strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
		return false;
	}
	
	return true;
}
///---END SUPPORT_DND_OPEN_DIALOG


void impSPE(const string& fname, const TreeNode& info, const TreeNode& options, Range& orng, TreeNode& finfo)
{
	//ImportSPE
	//---- CPY 7/18/06 MATRIXLAYER_SUPPORT_FOR_IMPORT
	Page pg;
	int c1,c2; 
	Datasheet lyTarget;
	///---Sim 03-22-2007 CHECK_RANGE
	//if(orng.GetRange(lyTarget, c1, c2))
	//lyTarget.GetParent(pg);
	if ( !GetRangeInfo(orng, lyTarget, pg, c1, c2) )
		XF_THROW(XFERR_INVALID_RANGE);
	///---END CHECK_RANGE
	if(!pg)
		XF_THROW(CER_INVALID_PAGE); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
	//----
		
	///---Sim 03-09-2007 TEMP_FIX_HIGHLINE_REPLACE_DATA	
	/// Hong 09/08/07 v8.0698b FIX_FAIL_DUMP_OUTPUT_RANGE_ORNG
	/*
	if ( orng )
	{
		orng.Reset();
		orng.Add(lyTarget, 0);
	}
	*/
	reset_range_to_first_obj(orng, lyTarget);
	/// end FIX_FAIL_DUMP_OUTPUT_RANGE_ORNG
	///---END TEMP_FIX_HIGHLINE_REPLACE_DATA
	
	//fname = GetOpenBox("[Princeton Instruments (*.SPE)] *.SPE");
	
	///---Sim 10-30-2006 MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE	
	/*
	Tree trInfo;
	if ( finfo )
		trInfo = finfo;
	*/
	Tree trBaseInfo; 
	/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
	//if ( finfo )
		//trBaseInfo = finfo;
	/// end FINFO_IS_ONLY_OUTPUT
	///---END MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE	
	
	///---Sim 09-26-2006 SUPPORT_IMPORT_MODE_BY_SIMPLE
	// XF impmode list is "Replace Data, new books, new sheets", value is 0,1,2
	/// Hong 06/13/05 v8.0639 ADD_OPTION_TREE_FOR_MAKE_FILTER
	//int nListIndexToImportMode[] = {
		//ASCIMP_MODE_REPLACE_DATA,
		//ASCIMP_MODE_NEW_BOOKS,
		//ASCIMP_MODE_NEW_SHEETS,
		//ASCIMP_MODE_APPEND_COLS	// Hong 01/27/07 ADD_APPEND_COLUMN_MODE
	//};
	//int nFileImportMode = ASCIMP_MODE_NEW_SHEETS;
	//if( 0 <= impmode && impmode < IML_NUM_MODES )
		//nFileImportMode = nListIndexToImportMode[impmode];
	/// end ADD_OPTION_TREE_FOR_MAKE_FILTER
	///---END SUPPORT_IMPORT_MODE_BY_SIMPLE
	
	///---Sim 09-12-2006 SUPPPORT_DEFAULT_FILTER_IS_EMPTY
	TreeNode tnUsingFilter;
	int nVer;
	/// Hong 06/13/05 v8.0639 ADD_OPTION_TREE_FOR_MAKE_FILTER
	//if ( fuGetDouble(trFilter, IDE_DISP_IMP_ORIGINVER, nVer) )
	//	tnUsingFilter = trFilter;
	if ( fuGetDouble(options, IDE_DISP_IMP_ORIGINVER, nVer) )
		tnUsingFilter = options;
	
	int nFileImportMode = fuGetImportMode(tnUsingFilter);
	/// end ADD_OPTION_TREE_FOR_MAKE_FILTER
	///---END SUPPPORT_DEFAULT_FILTER_IS_EMPTY
	
	////---- AW 09/29/06 MORE_WORK_ON_XF_IMPORT_FILES
	StringArray vsFileNames;
	separate_multiline_files(fname, vsFileNames);
	
	int nRet;
	int nFileCount = vsFileNames.GetSize();
	///---Sim 09-28-2006 CHECK_VALID_FILE_NAME
	if ( nFileCount <= 0 )
		XF_THROW(CER_INVALID_FILENAME); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
	///---END CHECK_VALID_FILE_NAME
	///---Sim 09-29-2006 SHOW_MULTI_FILE_INFO
	if ( info && 0 == info.GetNodeCount()  )
	{
		if ( !construct_tree_setting(vsFileNames, info) )
		{
			XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
			return;
		}
	}
	////---- END MORE_WORK_ON_XF_IMPORT_FILES
	
	
	///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
	/*
	///---Sim 09-26-2006 SUPPORT_SPARKLINES
	XFunction xfSparkLine;
	TreeNode trXFSparkLine;
	/// Hong 06/13/05 v8.0639 ADD_OPTION_TREE_FOR_MAKE_FILTER
	bool bSparkLines = fuGetSparkLine(tnUsingFilter);
	/// end ADD_OPTION_TREE_FOR_MAKE_FILTER
	if ( bSparkLines )
	{
		if(!xfSparkLine.Load(&trXFSparkLine, "sparklines", 0, true, true, true))
		{
			XF_THROW(CER_FAILED_LOAD_SPK_LINE_XF); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
				return;
		}
		xfSparkLine.SetArg("sel", 0);
	}
	///---END SUPPORT_SPARKLINES
	*/
	int nSparklines = fuGetSparkLine(tnUsingFilter);
	///---END SUPPORT_SMART_SPARKLINE
	
	SPEFile mySPE();
	///---Sim 09-11-2006 SUPPORT_MULTI_FILES_INXF
	/*
	int nRet;
	if ( nRet = mySPE.Import(fname, pg, trInfo, trFilter) )
	{
		XF_THROW(nRet);
	}
	else //if( !trFilter )//Hong 8/22/06 MORE_ON_TRINFO
	{
		///Hong 8/22/06 ADD_FILTER_INFO
		//impinfo_AddFile(pg.Layers(), fname, trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC);
		impinfo_AddFile(pg.Layers(), fname, trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, trFilter);
		///end ADD_FILTER_INFO
	}
	*/
	////---- AW 09/29/06 MORE_WORK_ON_XF_IMPORT_FILES
	/*
	StringArray vsFileNames;
	separate_multiline_files(fname, vsFileNames);
	
	int nRet;
	int nFileCount = vsFileNames.GetSize();
	///---Sim 09-28-2006 CHECK_VALID_FILE_NAME
	if ( nFileCount <= 0 )
		XF_THROW(CER_INVALID_FILEPATH);
	///---END CHECK_VALID_FILE_NAME
	///---Sim 09-29-2006 SHOW_MULTI_FILE_INFO
	if ( info && 0 == info.GetNodeCount()  )
	{
		if ( !construct_tree_setting(vsFileNames, info) )
		{
			XF_THROW(CER_INVALID_FILEPATH);
			return;
		}
	}
	*/
	////---- END MORE_WORK_ON_XF_IMPORT_FILES
	///---END SHOW_MULTI_FILE_INFO
	
	///---Sim 10-23-2006 REMOVE_REDUNDANT_EMPTY_WORKSHEET
	Layer lyOlder;
	bool bNewLayer, bEmptyLayer;
	///---END REMOVE_REDUNDANT_EMPTY_WORKSHEET
	
	///---Sim 10-23-2006 LET_FOCUSED_LAYER_BE_ACTIVE
	//int nIndexLayer = pg.Layers.Count() - 1;
	int nIndexLayer = lyTarget.GetIndex();
	set_active_layer(lyTarget);
	///---END LET_FOCUSED_LAYER_BE_ACTIVE
	for ( int ii = 0; ii < nFileCount; ii++ )
	{
		///Sophy 7/14/2010 ORG-568 IAEA_SPE_FILES_LEAD_TO_INFINITE_LOOP
		if ( !mySPE.IsValidPrincetonFile(vsFileNames[ii]) )
		{
			string strErrMsg;
			strErrMsg.Format(_L("%s is not a valid Princeton Instruments file and cannot not imported."), vsFileNames[ii]);
			warning_msg_box(strErrMsg, false, 'E');
			continue;
		}
		///end IAEA_SPE_FILES_LEAD_TO_INFINITE_LOOP
		lyOlder = pg.Layers(); ///---Sim 10-23-2006 REMOVE_REDUNDANT_EMPTY_WORKSHEET mabye nIndexLayer could not correct point to current layer after importing.
		//lyTarget = GetTargetLayer(pg, nIndexLayer, ii, tnUsingFilter); ///---Sim 09-06-2006 FIX_EXCESSIVE_EMPTY_SHEET
		///---Sim 09-26-2006 SUPPORT_GET_TARGET_LAYER_FROM_GUI_SETTING
		//lyTarget = GetTargetLayer(pg, nIndexLayer, ii, tnUsingFilter, true, orng, ASCIMP_MODE_NEW_SHEETS); ///---Sim 09-19-2006 SUPPORT_DATA_RANGE
		/// Hong 01/27/07 ADD_APPEND_COLUMN_MODE
		//lyTarget = GetTargetLayer(pg, nIndexLayer, ii, tnUsingFilter, true, orng, nFileImportMode);
		/// Hong 06/13/05 v8.0639 ADD_OPTION_TREE_FOR_MAKE_FILTER
		/* remove temp code
		///---Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
		if ( 0 == ii )
		{
			int nImportMode = get_first_file_import_mode(FILE_TYPE_SPE);
			TreeNode trInValid;
			
			lyTarget = GetTargetLayer(pg, nIndexLayer, ii, trInValid, true, orng, nImportMode, c1);
		}		
		else
		{
			lyTarget = GetTargetLayer(pg, nIndexLayer, ii, tnUsingFilter, true, orng, nFileImportMode, c1);
		}
		///---END TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
		*/
		/// Hong 08/01/07 v8.0671b MATRIX_START_NEW_COLUMNS_CHANGE_NEW_SHEET
		//lyTarget = GetTargetLayer(pg, nIndexLayer, ii, tnUsingFilter, true, orng, nFileImportMode, c1);	
		int nTempMutilMode = nFileImportMode;
		int nTemp1stMode = fuGet1stImportMode(tnUsingFilter);
		/// Hong 01/08/08 QA80-10541 FIX_REDUNDANT_MOBJ_WHEN_IMPORT_TO_MATRIX_WKS
		int nOldPageType = fuGetTargetPage(tnUsingFilter);		
		int nTargetPageType = nOldPageType;
		/// end FIX_REDUNDANT_MOBJ_WHEN_IMPORT_TO_MATRIX_WKS
		int nDim = mySPE.GetDimesion(vsFileNames[ii], nRet);
		if ( nDim  <= 0 )
		{
			XF_THROW_EX(nRet, GetFileName(vsFileNames[ii], false));		
		}
		/// Hong 01/08/08 QA80-10541 FIX_REDUNDANT_MOBJ_WHEN_IMPORT_TO_MATRIX_WKS
		else if ( nDim == 1 ) // for worksheet data
		{
			nTargetPageType = EXIST_WKS;
		}
		/// end FIX_REDUNDANT_MOBJ_WHEN_IMPORT_TO_MATRIX_WKS
		else if ( nDim > 1 ) // for matrix data, need change "Start New Columns" -> "Start New Sheet"
		{
			nTargetPageType = EXIST_MATRIX; /// Hong 01/08/08 QA80-10541 FIX_REDUNDANT_MOBJ_WHEN_IMPORT_TO_MATRIX_WKS
			if ( ASCIMP_MODE_APPEND_COLS == nTemp1stMode )
				fuSet1stImportMode(tnUsingFilter, ASCIMP_MODE_NEW_SHEETS);
			
			if ( ASCIMP_MODE_APPEND_COLS == nTempMutilMode )
				fuSetImportMode(tnUsingFilter, ASCIMP_MODE_NEW_SHEETS);
		}
		fuSetTargetPage(tnUsingFilter, nTargetPageType); /// Hong 01/08/08 QA80-10541 FIX_REDUNDANT_MOBJ_WHEN_IMPORT_TO_MATRIX_WKS
		
		lyTarget = GetTargetLayer(pg, nIndexLayer, ii, tnUsingFilter, true, orng, nFileImportMode, c1);
		
		fuSetTargetPage(tnUsingFilter, nOldPageType); /// Hong 01/08/08 QA80-10541 FIX_REDUNDANT_MOBJ_WHEN_IMPORT_TO_MATRIX_WKS
		
		if ( nDim > 1 )
		{
			if ( ASCIMP_MODE_APPEND_COLS == nTemp1stMode )
				fuSet1stImportMode(tnUsingFilter, nTemp1stMode);
			
			if ( ASCIMP_MODE_APPEND_COLS == nTempMutilMode )
				fuSetImportMode(tnUsingFilter, nTempMutilMode);
		}
		/// end MATRIX_START_NEW_COLUMNS_CHANGE_NEW_SHEET
		/// end ADD_OPTION_TREE_FOR_MAKE_FILTER
		/// end ADD_APPEND_COLUMN_MODE
		///---END SUPPORT_GET_TARGET_LAYER_FROM_GUI_SETTING
		bNewLayer = !is_same_layer(lyOlder, lyTarget); ///---Sim 10-23-2006 REMOVE_REDUNDANT_EMPTY_WORKSHEET
		
		///---Sim 10-30-2006 MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE
		/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
		//Tree trInfo;
		//trInfo.Replace(trBaseInfo);
		string strFile;
		strFile.Format("File%d", ii + 1); // file index to be 1
		TreeNode trInfo = trBaseInfo.AddNode(strFile);
		/// end FINFO_IS_ONLY_OUTPUT
		///---END MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE
		//if ( nRet = mySPE.Import(vsFileNames[ii], pg, trInfo, tnUsingFilter) )
		if ( nRet = mySPE.Import(vsFileNames[ii], pg, trInfo, tnUsingFilter, c1) ) // Hong 01/27/07 ADD_APPEND_COLUMN_MODE 
		{
			XF_THROW_EX(nRet, GetFileName(vsFileNames[ii], false)); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
		}
		else// if( !trFilter ) ///---Sim 09-27-2006 MORE_ON_TRINFO CAN"T SEE any reason need trFilter be invalid here
		{
			///---Sim 10-23-2006 REMOVE_REDUNDANT_EMPTY_WORKSHEET	
			// current layer be imported maybe is not the prepared layer
			bEmptyLayer = is_sheet_empty(lyTarget);
			if ( bNewLayer && bEmptyLayer )
			{
				lyTarget.Destroy();
			}
			///---END REMOVE_REDUNDANT_EMPTY_WORKSHEET
			
			///---Sim 11-10-2006 FIX_BEHIND_PAGE
			set_active_layer(pg.Layers()); // let current layer show on top
			///---END FIX_BEHIND_PAGE
			
			///---Sim 09-26-2006 SUPPORT_SPARKLINES
			if ( EXIST_WKS == pg.GetType() )
			{
				//int nLine4 = -1;
				//int nLine5 = -1;
				///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
				/*
				if( bSparkLines )
				{
					xfSparkLine.SetArg("c1", c1+1);
					xfSparkLine.SetArg("wks", (Worksheet)pg.Layers());
					if ( !xfSparkLine.Evaluate() )
					{
						XF_THROW(CER_SPK_LINE_RUN_ERROR); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
						return;	
					}
					//nLine4 = RCLT_SPARKLINE;
				}
				*/
				bool bSparklines = update_sparkline(nSparklines, (Worksheet)pg.Layers(), c1);
				///---END SUPPORT_SMART_SPARKLINE
				/*
				if( timecol )
				{
					if ( sparklines )
						nLine4 = RCLT_SPARKLINE;	
				}
				else
				{
					nLine4 = RCLT_SAMPLE_RATE;		
					if ( sparklines ) 
						nLine5 = RCLT_SPARKLINE;	
				}
				*/
				//wks_set_show_labels(ws, RCLT_LONG_NAME, RCLT_UNIT, RCLT_COMMENT, nLine4);
			}
			///---END SUPPORT_SPARKLINES
			
			///Hong 8/22/06 ADD_FILTER_INFO
			//impinfo_AddFile(pg.Layers(), fname, trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC);
			/// Hong 06/13/05 v8.0639 ADD_OPTION_TREE_FOR_MAKE_FILTER
			//impinfo_AddFile(pg.Layers(), vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, trFilter);
			impinfo_AddFile(pg.Layers(), vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter);
			/// end ADD_OPTION_TREE_FOR_MAKE_FILTER
			///end ADD_FILTER_INFO
		}
		
	}
	///---END SUPPORT_MULTI_FILES_INXF	
	//refresh_organizer_gui(pg); ///---Sim 06-25-2007 UPDATE_ORGANIZER
	/// Hong 05/14/07 v8.0617 NO_NEED_DUMP_THIS_INFO_AS_SAVE_TO_ORGANIZER
	//if( finfo )
		//finfo.Replace(trBaseInfo.Clone(), true, true);/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
	/// END NO_NEED_DUMP_THIS_INFO_AS_SAVE_TO_ORGANIZER
}

//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 impSPE_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impSPE_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	//printf("nrow=%d, cntrl = %d\n", nRow, nCntrlType);
	//out_tree(trGetN);
	
	///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
	/// Hong 11/11/09 FIX_TDM_RUNTIME_ERROR
	//if ( (GETNE_ON_INIT == nEventID && trGetN.info.Children.Count() == 0) || GETNE_ON_THEME == nEventID )
	if ( GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID )
	/// end FIX_TDM_RUNTIME_ERROR
	{
		s_bFileValid = file_name_event(trGetN, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
	}
	///---END SUPPORT_DND_OPEN_DIALOG
	
	///---Sim 09-29-2006 SHOW_MULTI_FILE_INFO
	// show file info when file name is changed
	string strTagName = lpcszNodeName;
	if(strTagName == "fname") // file name changed, either on start of later user changed
	{
		///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
		s_bFileValid = file_name_event(trGetN, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
		/*
		s_bFileValid = true; ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
		OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	
		///---Sim 10-30-2006 ERROR_MESSAGE
		int nErrCode = CER_NO_ERROR;
		int nInvalidFileIdx = 0;
	
		if ( trGetN.fname.strVal.IsEmpty() )
		{
			nErrCode = CER_EMPTY_FILENAME;
		}
		else if ( !is_valid_filename(trGetN.fname.strVal, nInvalidFileIdx) )
		{
			nErrCode = CER_INVALID_FILENAME_INDEX;
		}
				
		if(CER_NO_ERROR != nErrCode)
		{
			strErrMsg = nErrCode;
			
			if ( CER_INVALID_FILENAME_INDEX == nErrCode )
			{
				strErrMsg += ":";
				strErrMsg += nInvalidFileIdx;
			}
			
			//DynaCntrlContainer.PostMessage(WM_USER_MSG_ON_DLG, nErrCode, nInvalidFileIdx);    
			s_bFileValid = false; ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
			OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	
			// clean the info on GUI
			trGetN.info.RemoveChildrenWithPrefix();
		
			return true;
		}
		///---END ERROR_MESSAGE
		
		StringArray vsFileNames;
		TreeNode trInfo = trGetN.info;
		separate_multiline_files(trGetN.fname.strVal, vsFileNames);
		if ( !construct_tree_setting(vsFileNames, trInfo) )
		{
			///---Sim 10-30-2006 ERROR_MESSAGE
			// clean the trFiles on GUI
			trInfo.RemoveChildrenWithPrefix();
			
			//XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
			//return false;
			strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
			s_bFileValid = false; ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
			OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
			
			return true;
			///---END ERROR_MESSAGE
		}
		return true;
		*/
		///---END SUPPORT_DND_OPEN_DIALOG
	}
	///---END SHOW_MULTI_FILE_INFO	
	return false;

}

int impSPE_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	/*	
	string strTagName = lpcszVarName;
	
	if(strTagName == "tr")
	{
		GETN_USE(tr)
	
		//GETN_OPTION_GRIDLINE(flexGridRaisedVert) // Option to add vertical divider line
		//GETN_OPTION_EDIT_ALIGN("0|0|0") // left align checkbox|numbers|strings
	
		///GETN_STR(Filename, "File Name", lpcszFileName) GETN_READ_ONLY
	
		return 0;
	}
	*/	
	///---Sim 09-29-2006 SHOW_MULTI_FILE_INFO
	string strTagName = lpcszVarName;
	
	if(strTagName == "info")
	{
		return 0;
	}
	///---END SHOW_MULTI_FILE_INFO
	
	/// Hong 06/13/05 v8.0639 ADD_OPTION_TREE_FOR_MAKE_FILTER
	if ( "options" == strTagName )
	{
		constructOptionsForXF(tr, false, true, true, false, false);
		
		fuSetTargetPage(tr, EXIST_NONE);/// Hong 07/30/07 QA80-9252-P7 FIX_ACTIVE_BOOK_NOT_USED
		
		TreeNode tr1stImportMode = tree_get_node_by_id(tr, IDE_DISPIMP_1STMODE, true);
		if ( tr1stImportMode )
		{
			string strCombo, strMapping;
			strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_REPLACE_BOOK_SHEET_COLUMN);
			tr1stImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			tr1stImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			tr1stImportMode.nVal = ASCIMP_MODE_REPLACE_DATA;
		}
	
		TreeNode trImportMode = tree_get_node_by_id(tr, IDE_IMP_IMPORTMODE, true);
		if ( trImportMode )
		{
			string strCombo, strMapping;
			/// Hong 07/21/08 v8.0905 REMOVE_REPLACE_MODE_FROM_MULTI_IMPORT_MODE
			//strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_REPLACE_BOOK_SHEET_COLUMN);
			strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_BOOK_SHEET_COLUMN);
			/// end REMOVE_REPLACE_MODE_FROM_MULTI_IMPORT_MODE
			trImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			trImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			trImportMode.nVal = ASCIMP_MODE_NEW_SHEETS;
		}
				
		fuSetXFunctionName(tr, "impSPE");
		return 0;
	}
	///---END ADD_OPTION_TREE_FOR_MAKE_FILTER
	
	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 impSPE_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///---Sim 03-22-2007 FIX_CHECK_FILE_NAME_PASS_TO_XF
	/////---Sim 09-26-2006 SUPPORT_MORE_PARAMETER
	if(nGetNDialog > 0)
	{
		////--- CPY 9/24/06 DEFAULT_TO_FNAME_STR_VAR_FOR_IMPORT
		//string strFile = trGetN.fname.strVal;
		//if(strFile == "fname$")
			//trGetN.fname.strVal = "";
		////---
		trGetN.finfo.Show = 0;
		//trGetN.trFilter.Show = 0; /// Hong 06/13/05 v8.0639 ADD_OPTION_TREE_FOR_MAKE_FILTER
		/////---Sim 09-29-2006 SHOW_MULTI_FILE_INFO
		//string strFileName;
		////get_open_box_by_file_group(strFileName, "ASCII", "", trGetN.menu.nVal);
		//int nFilesRet = get_open_box_by_file_group(strFileName, "PrincetonInstruments", NULL, true);
		//if ( 0 == nFilesRet )
		//{
			//nRet = XFEVT_ABORT;
			//return;
		//}
		//trGetN.fname.strVal = strFileName;
		//StringArray vsFileNames;
		//separate_multiline_files(strFileName, vsFileNames);
		//TreeNode trInfo = trGetN.info;
		////---Sim 09-29-2006 DEFAULT_OPEN_TREE
		////GETN_USE(trInfo)
		////GETN_OPTION_BRANCH(GETNBRANCH_OPEN)
		////trInfo.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN);
		////---END DEFAULT_OPEN_TREE
		//if ( !construct_tree_setting(vsFileNames, trInfo) )
		//{
			//XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
			//return ;
		//}
		/////---END SHOW_MULTI_FILE_INFO
	}
	///---END FIX_CHECK_FILE_NAME_PASS_TO_XF
	
	/// AW 09/25/06 MORE_WORK_ON_IMPWAV
	//-------------- CPY 9/25/06 FILTER_TO_UPDATE_GUI_AND_LT_INVOKE
	//else if ( 0 == nGetNDialog )
	/// Hong 06/13/05 v8.0639 ADD_OPTION_TREE_FOR_MAKE_FILTER
	/*
	if(nGetNDialog >= 0)
	//--------------
	{
		TreeNode trFilter = trGetN.trFilter;
		if ( trFilter.IsValid() && trFilter.GetNodeCount()>0 )
		{
			///---Sim 09-27-2006 TRANSLATE_FILTER_IMPORT_MODE_TO_XF_PARAM
			//trGetN.FileImpotMode.nVal = fuGetImportMode(trFilter);
			
			// XF impmode list is "Replace Data, new books, new sheets"
			int iMode = IML_NEW_SHT; // default new sheet
			switch ( fuGetImportMode(trFilter) )
			{
			case ASCIMP_MODE_REPLACE_DATA:
				iMode = IML_REPLACE;
				break;
			case ASCIMP_MODE_NEW_BOOKS:
				iMode = IML_NEW_BK;
				break;
			case ASCIMP_MODE_NEW_SHEETS:
				iMode = IML_NEW_SHT;
				break;
			// Hong 01/27/07 ADD_APPEND_COLUMN_MODE	
			case ASCIMP_MODE_APPEND_COLS:
				iMode = IML_NEW_COL;
				break;
			/// end ADD_APPEND_COLUMN_MODE
			}
			trGetN.impmode.nVal = iMode; 
			///---END TRANSLATE_FILTER_IMPORT_MODE_TO_XF_PARAM
			trGetN.sparklines.nVal = fuGetSparkLine(trFilter);
			//trGetN.timecol.nVal = fuGetShowTime(trFilter);
		}
	}
	*/
	/// end ADD_OPTION_TREE_FOR_MAKE_FILTER
	/// END MORE_WORK_ON_IMPWAV
	///---END SUPPORT_MORE_PARAMETER
	/// Hong 12/22/06 CHECK_FNAME_EMPTY_UNASSIGNED
	if(nGetNDialog > -1)
	{
		string strFileName = trGetN.fname.strVal;
		
		/// Hong 04/10/07 v8.0597b IGNORE_FNAME$_WHEN_FROM_MENU
		if ( LTXF_FROM_GUI_MENU & dwCntrl ) // if from menu, ignore default value fname$
			strFileName = "";
		/// end IGNORE_FNAME$_WHEN_FROM_MENU
		
		if(is_empty_or_unassigned(strFileName))
		{
			/// Hong 04/03/07 QA80-6147-P1 v8.0595 ADD_MULTI_FILES_IMPORT
			//int nFilesRet = get_open_box_by_file_group(strFileName, "PrincetonInstruments", "PrincetonInstruments");			
			/// Hong 05/31/07 v8.0630 MORE_ON_ACCESS_TO_FDLOG
			//int nFilesRet = get_open_box_by_file_group(strFileName, "PrincetonInstruments", "PrincetonInstruments", true);
			int nFilesRet;
			if ( 0 != nGetNDialog )
			{
				int nShowOptionsDlg = 1;
				/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				//nFilesRet = get_open_box_by_file_group(strFileName, "PrincetonInstruments", _L("PrincetonInstruments"),
				//	OPENBOX_FLAG_MULTIFILES, NULL, NULL, _L("Show Options Dialog"), &nShowOptionsDlg);
				///---Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
				//nFilesRet = get_open_box_by_file_group(strFileName, "PrincetonInstruments", NULL,
					//OPENBOX_FLAG_MULTIFILES, NULL, NULL, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				nFilesRet = get_open_box_by_file_group(strFileName, "PrincetonInstruments", NULL,
					OPENBOX_FLAG_MULTIFILES, NULL, NULL, get_show_options_label(trGetN), &nShowOptionsDlg);
				///---END QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
				/// end CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				nRet = (nShowOptionsDlg ? XFEVT_PROCEED : XFEVT_PROCEED_NO_DLG);
			}
			else
			{
				/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				//nFilesRet = get_open_box_by_file_group(strFileName, "PrincetonInstruments", _L("PrincetonInstruments"), true);
				nFilesRet = get_open_box_by_file_group(strFileName, "PrincetonInstruments", NULL, true);
				/// end CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
			}
			/// end MORE_ON_ACCESS_TO_FDLOG
			/// end ADD_MULTI_FILES_IMPORT
			/// end THEME_FILENAME_WORKBOOK_AND_WORKSHEET
			if ( 0 == nFilesRet )
			{
				nRet = XFEVT_ABORT;
				//warning_msg_box(XFERR_UI_USER_CANCEL, false);
				return;
			}
			///---END HANDLE_USER_CANCEL_OPEN_FILE_BOX
			trGetN.fname.strVal = strFileName;
		///---Sim 03-22-2007 FIX_CHECK_FILE_NAME_PASS_TO_XF			
		} 
		
		//string strErrMsg;
		//if( !file_name_event(trGetN, strErrMsg) )
		//{
			///// Hong 03/21/07 v8.0586 FIX_FAIL_DUMP_CORRECT_ERROR_STRING
			////nRet = XFEVT_ABORT;
			////warning_msg_box(strErrMsg, false);
			//int nErrMsg = atoi(strErrMsg);
			//nRet = XFEVT_ABORT;
			//warning_msg_box(nErrMsg, false); // should pass an int
			///// end FIX_FAIL_DUMP_CORRECT_ERROR_STRING
			//return;
		//}
		//}
		///---END FIX_CHECK_FILE_NAME_PASS_TO_XF			
	}
	/// end CHECK_FNAME_EMPTY_UNASSIGNED
}
//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 impSPE_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
