#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <..\originlab\file_utils.h>
#include <..\originlab\fu_utils.h>// Hong 9/14/06 REMOVE_IMPORT_RELATED_TO_FU_UTILS
#include <..\originlab\MDFFile.h>
#include <..\originlab\XFunctionEx.h> ///---Sim 09-26-2006 SUPPORT_SPARKLINES
#include <GetNBox.h> ///---Sim 09-29-2006 SHOW_FILE_INFO
#include <..\originlab\theme_utils.h> ///---Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
//put additional include files here

//put your own support static functions here
///////////////////////////////////////////////////////////////////////
//////// modification log
/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
/// Hong 04/10/07 v8.0597b IGNORE_FNAME$_WHEN_FROM_MENU
/// Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
/// Sim 05-18-2007 REMOVE_UID_VARIABLE
/// Sim 06-12-2007 ADD_CHECK_ALL_UNCHECK_ALL_OPTION
/// Hong 06/15/07 QA80-9251-P10 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
/// Sim 06-25-2007 UPDATE_ORGANIZER
/// Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
/// Hong QA80-10911 FIX_MDF_FAIL_IN_GVER_CAUSE_BY_ERROR_CODE_OF_LOCALIZATION
/// Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
/// Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
/// Hong 10/29/09 QA80-13884-P1 FIX_SELECTION_MODE_FAIL_WHEN_APPLY_THEME
/// Hong 07/07/09 QA80-13884-P2 FIX_MDF_IMPOR_BY_GROUP_FAIL_ADD_SPARKLINE_TO_ALL_SHEET
///////////////////////////////////////////////////////////////////////

#define			STR_OLD_SEL_MODE_VALUE			"Old_Sel_Mode"	/// Hong 10/29/09 QA80-13884-P1 FIX_SELECTION_MODE_FAIL_WHEN_APPLY_THEME
///---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,
};
#define IML_NUM_MODES 3
///---END SHOW_IMPORT_MODE_BOX

//#define MDF_IMPORT_MODE_LIST	LIST_IMPORT_MODE_REPLACE_BOOK_SHEET_COLUMN 	///---Sim 02-01-2007 MORE_OPTIONS_FOR_IMPORT

///---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
 
/// Hong 10/18/07 v8.0728 REMOVE_REPLACE_OUT_OF_MULTI_FILE_IMPORT_MODE
#define MDF_1ST_FILE_IMPORT_MODE_LIST 	LIST_IMPORT_MODE_REPLACE_BOOK_SHEET_COLUMN
#define MDF_IMPORT_MODE_LIST	(MDF_1ST_FILE_IMPORT_MODE_LIST & (~IW_FLAG_IMPMODE_REPLACE))
/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
#define MDF_1ST_FILE_IMPORT_MODE_LIST_DG 	LIST_IMPORT_MODE_BOOK_SHEET
#define MDF_IMPORT_MODE_LIST_DG				LIST_IMPORT_MODE_BOOK_SHEET
/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
/// end REMOVE_REPLACE_OUT_OF_MULTI_FILE_IMPORT_MODE

///---Sim 09-29-2006 SHOW_MULTI_FILE_INFO
int get_open_box_by_file_group(string& strFile, LPCSTR lpcszGroup, LPCSTR lpcszTitle, BOOL bMultiFiles);

static BOOL construct_tree_FileInfo(string strFileName, TreeNode& trInfo)
{
		if ( !strFileName.IsFile() )
			return false;
		string strInfo;
		strInfo.Format("File Size: %s ", GetFileSize(strFileName));
		trInfo.strVal = strInfo;
		trInfo.Enable = 0;
		return true;
}
/*
BOOL construct_tree_FileInfos(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;
		string strTag = GetFileName(strFileName, true);
		strTag.MakeValidCName('a');
//		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 09-29-2006 DATA_SELECTION
///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
//static void _construct_branch(TreeNode& trFile, string strFileName)
/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
//static bool _construct_branch(TreeNode& trFile, string strFileName)
static bool _construct_branch(TreeNode& trFile, string strFileName, bool bEnableDataGroup)
/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
///---END HANDLE_ERROR_MESSAGE
{		
	// Load file header struct to tree
	Tree trStruct;
	MDFFile fMDF;
	/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	fMDF.SetDataGroup(bEnableDataGroup);
	if ( CER_NO_ERROR != fMDF.GetDataSelection(strFileName, trFile) )
		return false;
	return true;
	/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	if ( fMDF.ReadHeader(strFileName, trStruct) )
		return false; ///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
	
 	TreeNode trSelection;
 	/// Hong QA80-10911 FIX_MDF_FAIL_IN_GVER_CAUSE_BY_ERROR_CODE_OF_LOCALIZATION
	//string strSelection = _L("DataSelection");
	//trSelection = trFile.AddNode(strSelection, TRGP_BRANCH);
	//trSelection.SetAttribute(STR_LABEL_ATTRIB, strSelection);		
	trSelection = trFile.AddNode("DataSelection", TRGP_BRANCH);
	trSelection.SetAttribute(STR_LABEL_ATTRIB, "Data Selection");	
	/// end FIX_MDF_FAIL_IN_GVER_CAUSE_BY_ERROR_CODE_OF_LOCALIZATION
	///---Sim 01-17-2007 ADD_CHECK_ALL_UNCHECK_ALL_OPTION
	///---Sim 06-12-2007 ADD_CHECK_ALL_UNCHECK_ALL_OPTION
	//trSelection.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN);
	trSelection.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_CHECK_CONTROL | GETNBRANCH_OPEN|GETNBRANCH_CHECK_CONTROL_ONE_EVENT);
	///---END ADD_CHECK_ALL_UNCHECK_ALL_OPTION
	//trSelection.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_CHECK_CONTROL | GETNBRANCH_OPEN);
	///---END ADD_CHECK_ALL_UNCHECK_ALL_OPTION
	
	// Walk all data group on file struct
	TreeNode trDataGroup;
	string strDataGroupName;
	int nDataGroupIndex = 1;
	TreeNode trDG0 = trStruct.GetNode("DG0");
	if ( !(trDG0 && trDG0.IsValid()) )
		return false; // tree node invalid ///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
	
	int nCGIndex = 0; /// Hong 06/19/07 v8.0644 FIX_FAIL_CORRECT_UNCHECK_ACCORDING_X_COLUMN
	foreach ( TreeNode trDG in trDG0.Children )
	{
		/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
		/*
		strDataGroupName.Format("Data Group #%d", nDataGroupIndex++);
		///---Sim 01-17-2007 ADD_CHECK_ALL_UNCHECK_ALL_OPTION
		//trDataGroup = trSelection.AddNumericNode(1, "DataGroupName", TRGP_CHECK);
		trDataGroup = trSelection.AddNumericNode(1, "DataGroupName");
		//trDataGroup.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_CHECK_CONTROL);
		trDataGroup.SetAttribute(STR_LABEL_ATTRIB, strDataGroupName);
		//trDataGroup.SetAttribute(STR_ATTRIB_DISPFMT, DISPLAY_EDITOR_CHECK_ON_LEFT);
		//trDataGroup.Use = 1;
		///---END ADD_CHECK_ALL_UNCHECK_ALL_OPTION
		*/
		/// end IMPORT_ALL_DATA_TO_ONE_SHEET
		
		// Only support one channel group on data group
		TreeNode trChannelGroup;
		string strChannelGroupName;
		int nChannelGroupIndex = 1;
		TreeNode trCG = trDG.GetNode("CG");
		if ( !(trCG && trCG.IsValid()) )
			continue ; // tree node invalid
		else
		{
			/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
			/*
			strChannelGroupName.Format("Channel Group #%d", nChannelGroupIndex++);
			/// Hong 12/24/06 MORE_INFO
			if ( trCG.Remark.IsValid() && !trCG.Remark.IsEmpty() )
				strChannelGroupName = trCG.Remark.strVal;
			/// end MORE_INFO
			///---Sim 01-17-2007 ADD_CHECK_ALL_UNCHECK_ALL_OPTION
			//trChannelGroup = trDataGroup.AddNumericNode(1, "ChannelGroupName", TRGP_CHECK);
			trChannelGroup = trDataGroup.AddNumericNode(1, "ChannelGroupName");
			trChannelGroup.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_CHECK_CONTROL|GETNBRANCH_CHECK_CONTROL_ONE_EVENT);
			trChannelGroup.SetAttribute(STR_LABEL_ATTRIB, strChannelGroupName);
			//trChannelGroup.SetAttribute(STR_ATTRIB_DISPFMT, DISPLAY_EDITOR_CHECK_ON_LEFT);
			//trChannelGroup.Use = 1;
			///---END ADD_CHECK_ALL_UNCHECK_ALL_OPTION
			*/
			/// end IMPORT_ALL_DATA_TO_ONE_SHEET
			
			// Walk all channel on channel group
			TreeNode trChannel;
			string strChannelName;
			int nChannelIndex = 1;
			TreeNode trCN0 = trCG.GetNode("CN0");
			if ( (trCN0 && trCN0.IsValid()) )
			{ // tree node valid
				foreach ( TreeNode trCN in trCN0.Children )
				{
					strChannelName.Format("Channel #%d", nChannelIndex++);
					/// Hong 12/24/06 MORE_INFO
					if( !trCN.UniqueName.IsEmpty() )
						strChannelName = trCN.UniqueName.strVal;
					/// Hong 06/15/07 QA80-9251-P11 FIX_MISS_LABEL_IF_NO_UNIQUENAME
					else if ( !trCN.SignalName.IsEmpty() )
					{
						strChannelName = trCN.SignalName.strVal;
					}
					/// end FIX_MISS_LABEL_IF_NO_UNIQUENAME						
					/// end MORE_INFO
					/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
					//trChannel = trChannelGroup.AddNumericNode(1, "ChannelName", TRGP_CHECK);
					trChannel = trSelection.AddNumericNode(1, "ChannelName", TRGP_CHECK);
					/// end IMPORT_ALL_DATA_TO_ONE_SHEET
					trChannel.SetAttribute(STR_LABEL_ATTRIB, strChannelName);
					//trChannel.SetAttribute(STR_ATTRIB_DISPFMT, DISPLAY_EDITOR_CHECK_ON_LEFT); // Hong 03/13/07 AVOID_LABEL_TRUNCATED
					//trChannel.Use = 1;
					///---END ADD_CHECK_ALL_UNCHECK_ALL_OPTION
					/// Hong 01/18/07 HIDDEN_TIME_ON_SELECTION
					if( 1 == trCN.ChannelType.nVal ) // x-axis
						trChannel.Show = false;
					/// end HIDDEN_TIME_ON_SELECTION
					/// Hong 06/19/07 v8.0644 FIX_FAIL_CORRECT_UNCHECK_ACCORDING_X_COLUMN
					trChannel.SetAttribute(STR_CHANNEL_GROUP_INDEX, nCGIndex);
					/// end FIX_FAIL_CORRECT_UNCHECK_ACCORDING_X_COLUMN
				}
				nCGIndex++; /// Hong 06/19/07 v8.0644 FIX_FAIL_CORRECT_UNCHECK_ACCORDING_X_COLUMN
			}			
		}
	}
	return true; ///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
}

///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
//static void _construct_GUI_tree(TreeNode& trGUI, vector<string>& vsFileNames)
/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
//static bool _construct_GUI_tree(TreeNode& trGUI, vector<string>& vsFileNames)
static bool _construct_GUI_tree(TreeNode& trGUI, vector<string>& vsFileNames, bool bEnableDataGroup)
/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
///---END HANDLE_ERROR_MESSAGE
{
	int nCount = vsFileNames.GetSize();
	TreeNode trFile;
	
	trGUI.RemoveChildrenWithPrefix(); ///---Sim 11-20-2006 SET_ATTRIBUTE
	
	string strTagName; ///---Sim 11-08-2006 FIX_SET_TAGNAME_AS_FILENAME
	for ( int nIndex = 0; nIndex < nCount; nIndex++ )
	{
		// Create Tree Node for each file
		///---Sim 09-19-2006 MAKE_TAGNAME_OF_EACH_NODE_AS_FILE_NAME
		///---Sim 11-08-2006 FIX_SET_TAGNAME_AS_FILENAME
		//string strFileName = GetFileName(vsFileNames[nIndex], true);
		//strFileName.MakeValidCName('a');
		//trFile = trGUI.AddNode(strFileName);
		strTagName.Format("file%d", nIndex+1);
		trFile = trGUI.AddNode(strTagName);
		///---END FIX_SET_TAGNAME_AS_FILENAME
		///---END MAKE_TAGNAME_OF_EACH_NODE_AS_FILE_NAME
		trFile.SetAttribute(STR_LABEL_ATTRIB, GetFileName(vsFileNames[nIndex], false));
//		trFile.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN);
//		GETN_OPTION_BRANCH(GETNBRANCH_OPEN)

		// Load File Info
		TreeNode trFileInfo;
		trFileInfo = trFile.AddNode("FileInfo");
		trFileInfo.SetAttribute(STR_LABEL_ATTRIB, STR_FILE_INFO);	///Sophy 9/21/2009 SET_LABEL_ON_GUI_FOR_TRANSLATION		
		///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
		if ( !construct_tree_FileInfo(vsFileNames[nIndex], trFileInfo) )
			return false;
		///---END HANDLE_ERROR_MESSAGE

		// Construct File Data Selection tree node
		///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
		/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		//if ( !_construct_branch(trFile, vsFileNames[nIndex]) )
		if ( !_construct_branch(trFile, vsFileNames[nIndex], bEnableDataGroup) )
		/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
			return false;
		///---END HANDLE_ERROR_MESSAGE
	}
	trGUI.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN); ///---Sim 11-20-2006 SET_ATTRIBUTE
	return true; ///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
}
///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
//static void construct_tree_setting(const string& strMultiFileName, TreeNode& trSetting)
/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
//static bool construct_tree_setting(const string& strMultiFileName, TreeNode& trSetting)
static bool construct_tree_setting(const string& strMultiFileName, TreeNode& trSetting, bool bEnableDataGroup)
/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
///---END HANDLE_ERROR_MESSAGE
{
	/// Hong 06/15/07 QA80-9251-P10 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
	if(trSetting && trSetting.GetNodeCount() > 0)
		return true;
	/// end REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
	StringArray vsFileNames;
	separate_multiline_files(strMultiFileName, vsFileNames); 

	//trSetting.RemoveChildrenWithPrefix(); ///---Sim 11-20-2006 SET_ATTRIBUTE no need
	///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
	//_construct_GUI_tree(trSetting, vsFileNames);
	//trSetting.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN);
	/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	//return _construct_GUI_tree(trSetting, vsFileNames);
	return _construct_GUI_tree(trSetting, vsFileNames, bEnableDataGroup);
	/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	///---END HANDLE_ERROR_MESSAGE
}
///---END DATA_SELECTION

///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
/// Hong 06/15/07 QA80-9251-P10 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
//static bool file_name_event(TreeNode& trGetN, string& strErrMsg)
static bool file_name_event(TreeNode& trGetN, string& strErrMsg, bool bClearSetting = true)
/// end REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
{
	string strFileName = trGetN.fname.strVal;
	TreeNode trNode = trGetN.trfiles;

	// clean the trFiles on GUI
	if ( bClearSetting ) /// Hong 06/15/07 QA80-9251-P10 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
		trNode.RemoveChildrenWithPrefix();
	
	if ( !check_imported_file_name(strFileName, strErrMsg) )
		return false;
	
	/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	//if ( !construct_tree_setting(strFileName, trNode) )
	if ( !construct_tree_setting(strFileName, trNode, trGetN.options.selectionMode.nVal) )
	/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	{
		// clean the trFiles on GUI
		trNode.RemoveChildrenWithPrefix();
		strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
		return false;
	}
	
	return true;
}
///---END SUPPORT_DND_OPEN_DIALOG

/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
static void _update_gui_on_data_group(TreeNode& trGetN)
{
	TreeNode	trOptions = trGetN.options;
	TreeNode 	tr1stImportMode = tree_get_node_by_id(trOptions, IDE_DISPIMP_1STMODE, true);
	TreeNode 	trImportMode = tree_get_node_by_id(trOptions, IDE_IMP_IMPORTMODE, true);
	TreeNode 	trRenameWks = tree_get_node_by_id(trOptions, IDE_IMP_RENAMEWKS, true);
	TreeNode 	trRenameTrimFrom = tree_get_node_by_id(trOptions, IDE_DISPIMP_RENAMEWKSTRIMFNAMEFROM, true);
	TreeNode 	trRenameTrimTo = tree_get_node_by_id(trOptions, IDE_DISPIMP_RENAMEWKSTRIMFNAMETO, true);
	
	BOOL		bInDataGroupMode = trOptions.selectionmode.nVal;
	
	string 		strCombo, strMapping;
	if ( tr1stImportMode )
	{
		strCombo = getImportModeListString(strMapping, bInDataGroupMode ? MDF_1ST_FILE_IMPORT_MODE_LIST_DG : MDF_1ST_FILE_IMPORT_MODE_LIST);
		string strCurrCombo;
		if ( !tr1stImportMode.GetAttribute(STR_COMBO_ATTRIB, strCurrCombo) || 0 != strCurrCombo.Compare(strCombo) )
		{
			tr1stImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			tr1stImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			int	nCurrVal = tr1stImportMode.nVal;
			if ( bInDataGroupMode && ASCIMP_MODE_NEW_SHEETS != nCurrVal && ASCIMP_MODE_NEW_BOOKS != nCurrVal )
				tr1stImportMode.nVal = ASCIMP_MODE_NEW_SHEETS;
		}
	}
	
	if ( trImportMode )
	{
		strCombo = getImportModeListString(strMapping, bInDataGroupMode ? MDF_IMPORT_MODE_LIST_DG : MDF_IMPORT_MODE_LIST);
		string strCurrCombo;
		if ( !trImportMode.GetAttribute(STR_COMBO_ATTRIB, strCurrCombo) || 0 != strCurrCombo.Compare(strCombo) )
		{
			trImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			trImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			int	nCurrVal = trImportMode.nVal;
			if ( bInDataGroupMode && ASCIMP_MODE_NEW_SHEETS != nCurrVal && ASCIMP_MODE_NEW_BOOKS != nCurrVal )
				trImportMode.nVal = ASCIMP_MODE_NEW_SHEETS;
		}
	}
	// Hong, enable status is also controled by treenode event, see OnChangeRenameOptions, so use Show status here
	if ( trRenameWks )
		trRenameWks.Show = !bInDataGroupMode;
	if ( trRenameTrimFrom )
		trRenameTrimFrom.Show = !bInDataGroupMode;
	if ( trRenameTrimTo )
		trRenameTrimTo.Show = !bInDataGroupMode;
}
/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT


void impMDF(const string& fname, const TreeNode& trfiles, const TreeNode& options, Range& orng, TreeNode& trheader, const int& reimp)
{
	int nRet = 0; 
	Tree trBaseInfo; // Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
	/// Hong 10/23/06 NEED_INITIAL_INFOTREE_FOR_EVERY_FILE
	/*
	if ( finfo )
		trInfo = finfo;
	*/
	/// end NEED_INITIAL_INFOTREE_FOR_EVERY_FILE
	//vector vImportedRangeUID; ///---Sim 02-06-2007 GET_RANGE_UID_OF_IMPORTED_FILE ///---Sim 05-18-2007 REMOVE_UID_VARIABLE remove
	
	Worksheet ws;
	int r1,r2,c1,c2; 
	///---Sim 12-20-2006 FIX_BUG_WHEN_INPUT_PARA_IS_MATRIX
	Datasheet ds;
	//orng.GetRange(0, r1, c1, r2, c2, ws);  // used to get wkspg
	/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	//orng.GetRange(0, r1, c1, r2, c2, ds);
	WorksheetPage wp;
	if ( !GetRangeInfo(orng, ds, wp, c1, c2) )
		XF_THROW(XFERR_INVALID_RANGE);
	/// end CENTRALIZE_CHECK_CALID_RANGE
	ws = ds;
	///---END FIX_BUG_WHEN_INPUT_PARA_IS_MATRIX
	///---Sim 09-26-2006 IMPORT_FOR_WORK_SHEET
	if(!ws)
		XF_THROW(CER_INVALID_WKS);// a better global err for not matching book would be better
	
	///---END IMPORT_FOR_WORK_SHEET
	//WorksheetPage wp = ws.GetPage(); /// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	/// Hong 9/12/06 SUPPORT_MULTI_FILES_INXF
	/*
	/// AW 07/17/06 MORE_ON_MDF
	if ( 1 != wp.Layers.Count() || 0 != wks_find_empty_column(ws) )
	{
		wp.Create("origin.otw");
		ws = wp.Layers();// Hong 8/03/06 ERROR_ATTACK_WKS_FOR_IMPORT
	}
	/// END MORE_ON_MDF
	
	MDFFile impMDF();
	if ( nRet = impMDF.Import(fname, wp, trInfo) )
	{
		XF_THROW(nRet);
	}
	else //if( !trFilter )   ///AW 08/01/06 MORE_ON_TRINFO CAN"T SEE any reason need trFilter be invalid here
	{
		impinfo_AddFile(ws, fname, trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC); 
	}
	*/
	/// Hong 01/25/07 MDF_SHOULD_ALWAYS_NEW_BOOK
	/*
	///---Sim 09-26-2006 SUPPORT_IMPORT_MODE_BY_SIMPLE
	// XF impmode list is "Replace Data, new books, new sheets", value is 0,1,2
	int nListIndexToImportMode[] = {
		ASCIMP_MODE_REPLACE_DATA,
		ASCIMP_MODE_NEW_BOOKS,
		ASCIMP_MODE_NEW_SHEETS
	};
	int nFileImportMode = ASCIMP_MODE_NEW_BOOKS;
	if( 0 <= impmode && impmode < IML_NUM_MODES )
		nFileImportMode = nListIndexToImportMode[impmode];
	///---END SUPPORT_IMPORT_MODE_BY_SIMPLE
	*/
	//int nFileImportMode = ASCIMP_MODE_NEW_BOOKS;///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT remove
	
	/// end MDF_SHOULD_ALWAYS_NEW_BOOK
	///---Sim 09-12-2006 SUPPPORT_DEFAULT_FILTER_IS_EMPTY
	TreeNode tnUsingFilter;
	int nVer;
	if ( fuGetDouble(options, IDE_DISP_IMP_ORIGINVER, nVer) )
		tnUsingFilter = options;
	///---END SUPPPORT_DEFAULT_FILTER_IS_EMPTY
	/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	TreeNode 	trRenameWks = tree_get_node_by_id(tnUsingFilter, IDE_IMP_RENAMEWKS, true);
	if ( trRenameWks && !tree_is_visible(trRenameWks) )
		trRenameWks.nVal = 0;
	/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	
	///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
	///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT
	int nFileImportMode = fuGetImportMode(tnUsingFilter);
	
	/*
	bool bSparkLines = fuGetSparkLine(tnUsingFilter);
	///---END MORE_OPTIONS_FOR_IMPORT
	/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
	XFunction xfSparkLine;
	TreeNode trXFSparkLine;
	
	if ( bSparkLines )
	{
		if(!xfSparkLine.Load(&trXFSparkLine, "sparklines", 0, true, true, true))
		{
			XF_THROW(CER_FAILED_LOAD_SPK_LINE_XF);///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
			return;
		}
		xfSparkLine.SetArg("sel", 0);
	}
	/// end /// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
	*/
	int nSparklines = fuGetSparkLine(tnUsingFilter);
	///---END SUPPORT_SMART_SPARKLINE
	
	///---Sim 09-26-2006 SUPPORT_SPARKLINES
	/*
	XFunction xfSparkLine;
	TreeNode trXFSparkLine;
	
	if ( sparklines )
	{
		if(!xfSparkLine.Load(&trXFSparkLine, "sparklines", 0, true, true, true))
		{
			XF_THROW(IMPERR_IMPORT_FUNC_ERR);// a better global err for not matching book would be better
				return;
		}
		xfSparkLine.SetArg("sel", 0);
	}
	*/
	///---END SUPPORT_SPARKLINES
	
	StringArray vsFileNames;
	//str_separate(fname, "\n", vsFileNames); 
	separate_multiline_files(fname, vsFileNames);// Hong 9/12/06 FIX_SEPARATE_FILENAME 
	
	int nFileCount = vsFileNames.GetSize();
	///---Sim 09-28-2006 CHECK_VALID_FILE_NAME
	if ( nFileCount <= 0 )
		XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
	///---END CHECK_VALID_FILE_NAME
	///---Sim 09-29-2006 SUPPORT_DATA_SELECTION
	/*
	///---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 SHOW_MULTI_FILE_INFO
	*/
	if ( trfiles.IsEmpty() && !fname.IsEmpty() )
	{
		///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
		///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
		/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		if ( !construct_tree_setting(fname, trfiles, options.selectionMode.nVal) )
		/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		{
			XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
			return ;
		}
		///---END HANDLE_ERROR_MESSAGE
	}
	///---END SUPPORT_DATA_SELECTION
	
	int nIndexLayer = wp.Layers.Count(); ///---Sim 09-26-2006 PROVIDE_FIRST_TARGET_LOCATION
	///---Sim 09-29-2006 SUPPORT_DATA_SELECTION
	//for ( int ii = 0; ii < nFileCount; ii++ )
	int nFileIndex = 0;
	foreach(TreeNode trFileNode in trfiles.Children )
	///---END SUPPORT_DATA_SELECTION
	{
		///---Sim 09-26-2006 GET_TARGET_LAYER
		/*
		/// AW 07/17/06 MORE_ON_MDF
		if ( 1 != wp.Layers.Count() || 0 != wks_find_empty_column(ws) )
		{
			wp.Create("origin.otw");
			ws = wp.Layers();// Hong 8/03/06 ERROR_ATTACK_WKS_FOR_IMPORT
		}
		/// END MORE_ON_MDF
		*/
		//ws = GetTargetLayer(wp, nIndexLayer, ii, tnUsingFilter, true, orng, ASCIMP_MODE_NEW_BOOKS);
		///---Sim 09-29-2006 SUPPORT_DATA_SELECTION
		//ws = GetTargetLayer(wp, nIndexLayer, ii, tnUsingFilter, true, orng, nFileImportMode);
		///---Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
		/*
		if ( 0 == nFileIndex )
		{
			int nImportMode = get_first_file_import_mode(FILE_TYPE_MDF);
			TreeNode trInValid;
			
			ws = GetTargetLayer(wp, nIndexLayer, nFileIndex, trInValid, true, orng, nImportMode);
		}
		else
		{
			ws = GetTargetLayer(wp, nIndexLayer, nFileIndex, tnUsingFilter, true, orng, nFileImportMode);
		}
		*/
		/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
		//ws = GetTargetLayer(wp, nIndexLayer, nFileIndex, tnUsingFilter, true, orng, ASCIMP_MODE_NEW_BOOKS);
		/// Hong 08/07/07 v8.0767 FIX_REIMPORT_RUNTIME_ERROR_BY_DELETE_COLS
		//ws = GetTargetLayer(wp, nIndexLayer, nFileIndex, tnUsingFilter, true, orng, ASCIMP_MODE_NEW_BOOKS, c1, c2);
		ws = GetTargetLayer(wp, nIndexLayer, nFileIndex, tnUsingFilter, true, orng, ASCIMP_MODE_NEW_BOOKS, c1, false, c2);
		/// end FIX_REIMPORT_RUNTIME_ERROR_BY_DELETE_COLS
		/// end IMPORT_ALL_DATA_TO_ONE_SHEET
		///---END TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
		///---END SUPPORT_DATA_SELECTION
		///---END GET_TARGET_LAYER
		/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
		/*
		/// Hong 10/23/06 NEED_INITIAL_INFOTREE_FOR_EVERY_FILE	
		if ( finfo )
			trInfo = finfo;
		else
			trInfo.Reset();
		/// end NEED_INITIAL_INFOTREE_FOR_EVERY_FILE
		*/
		string strFile;
		strFile.Format("File%d", nFileIndex + 1); // file index to be 1
		TreeNode trInfo = trBaseInfo.AddNode(strFile);
		/// end FINFO_IS_ONLY_OUTPUT
		MDFFile impMDF();
		impMDF.SetDataGroup(options.selectionMode.nVal); /// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		///---Sim 09-26-2006 SUPPORT_SPARKLINES
		//if ( nRet = impMDF.Import(vsFileNames[ii], wp, trInfo) )
		///---Sim 09-29-2006 SUPPORT_DATA_SELECTION
		//if ( nRet = impMDF.Import(vsFileNames[ii], wp, trInfo, sparklines) )
		/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
		//if ( nRet = impMDF.Import(vsFileNames[nFileIndex], wp, trInfo, trFileNode, bSparkLines) )
		/// Hong 07/09/07 v8.0655 IMPORT_RAW_DATA_FOR_MAY_FORMULA
		//if ( nRet = impMDF.Import(vsFileNames[nFileIndex], ws, trInfo, trFileNode, c1, c2) )
		if ( nRet = impMDF.Import(vsFileNames[nFileIndex], ws, trInfo, trFileNode, c1, c2, options.rawdata.nVal) )
		/// end IMPORT_RAW_DATA_FOR_MAY_FORMULA
		/// end IMPORT_ALL_DATA_TO_ONE_SHEET
		///---END SUPPORT_DATA_SELECTION
		///---END SUPPORT_SPARKLINES
		{
			XF_THROW_EX(nRet, GetFileName(vsFileNames[nFileIndex], false)); ///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
		}
		else //if( !trFilter )   ///AW 08/01/06 MORE_ON_TRINFO CAN"T SEE any reason need trFilter be invalid here
		{
			///---Sim 02-01-2007 RENAME_CENTRALIZED
			// rename 
			/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
			//setPageAndLayerName(impMDF.GetDataRange(), vsFileNames[nFileIndex], tnUsingFilter);
			DWORD dwCtrl = 0;
			dwCtrl |= (reimp) ? COVER_LAST_ADDED_COMMENT : 0;
			setPageAndLayerName(impMDF.GetDataRange(), vsFileNames[nFileIndex], tnUsingFilter, dwCtrl);
			/// end IMPORT_ALL_DATA_TO_ONE_SHEET
			///---END RENAME_CENTRALIZED
		
			/// Hong 07/07/09 QA80-13884-P2 FIX_MDF_IMPOR_BY_GROUP_FAIL_ADD_SPARKLINE_TO_ALL_SHEET
			/*
			/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
			DataRange drImported;
			drImported = impMDF.GetDataRange();
			int nC1, nC2;
			Worksheet wksTemp;
			drImported.GetRange(wksTemp, nC1, nC2);
			///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
			/ *
			if( bSparkLines )
			{
				xfSparkLine.SetArg("c1", nC1+1);
				xfSparkLine.SetArg("c2", nC2+1);	
				xfSparkLine.SetArg("wks", ws);
				if ( !xfSparkLine.Evaluate() )
				{
					XF_THROW(CER_SPK_LINE_RUN_ERROR);
					return;	
				}
			}
			* /
			bool bSparklines = update_sparkline(nSparklines, ws, nC1, nC2);
			///---END SUPPORT_SMART_SPARKLINE
			
			vector<int> vn = {RCLT_LONG_NAME, RCLT_UNIT, RCLT_COMMENT}; 
	
			///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
			//if ( bSparkLines ) 
			if ( bSparklines )
			///---END SUPPORT_SMART_SPARKLINE
				vn.Add(RCLT_SPARKLINE);
			wks_add_show_labels(ws, vn, false);
			/// end IMPORT_ALL_DATA_TO_ONE_SHEET
			*/
			DataRange 		drImported;
			drImported = impMDF.GetDataRange();
			for ( int ii = 0; ii < drImported.GetNumRanges(); ii++ )
			{
				int 		nC1, nC2;
				Worksheet 	wksTemp;
				drImported.GetRange(wksTemp, nC1, nC2, ii);
				bool 		bSparklines = update_sparkline(nSparklines, wksTemp, nC1, nC2);
				vector<int>	vn = {RCLT_LONG_NAME, RCLT_UNIT, RCLT_COMMENT};
				if ( bSparklines )
					vn.Add(RCLT_SPARKLINE);
				wks_add_show_labels(wksTemp, vn, false);
			}
			/// end FIX_MDF_IMPOR_BY_GROUP_FAIL_ADD_SPARKLINE_TO_ALL_SHEET
			///---Sim 09-26-2006 SUPPORT_SPARKLINES
			/*
			int nLine4 = -1;
			//int nLine5 = -1;
			if( sparklines )
			{
				xfSparkLine.SetArg("wks", ws);
				if ( !xfSparkLine.Evaluate() )
				{
					XF_THROW(XFERR_INVALID_MATOBJ);
					return;	
				}
				nLine4 = RCLT_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
		
			///---Sim 02-01-2007 ADD_RANGE_INFO
			// add data range info
			Tree trDataRange;
			///---Sim 05-18-2007 REMOVE_UID_VARIABLE
			//int UID = getRangeInfo(trDataRange, impMDF.GetDataRange());
			getRangeInfo(trDataRange, impMDF.GetDataRange());
			//vImportedRangeUID.Add(UID);
			///---END REMOVE_UID_VARIABLE
			/// Hong 06/15/07 QA80-9251-P10 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
			TreeNode trImport, trSeclect;
			trImport = trInfo.AddNode("Import");
			trImport.Show = 0;
			trSeclect = trImport.AddNode("trSetting").AddNode(strFile);
			trSeclect.Replace(trFileNode.Clone(), true, true);
			/// end REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
		
			// if current file cover existing file data, need update info of that files
			if ( !reimp ) /// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
				impinfo_UpdateExistingData(wp, impMDF.GetDataRange());
	
			///---Sim 09-29-2006 SUPPORT_DATA_SELECTION
			//impinfo_AddFile(ws, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC); 
			//impinfo_AddFile(ws, vsFileNames[nFileIndex], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter); ///---Sim 02-01-2007 UPDATE_FILTER_TREE
			/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
			//impinfo_AddFile(ws, vsFileNames[nFileIndex], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter, nFileIndex + 1, trDataRange);
			impinfo_AddFile(ws, vsFileNames[nFileIndex], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter, nFileIndex + 1, trDataRange, reimp);
			/// end IMPORT_ALL_DATA_TO_ONE_SHEET
			///---END SUPPORT_DATA_SELECTION
			///---END ADD_RANGE_INFO
		}
		nFileIndex++; ///---Sim 09-29-2006 SUPPORT_DATA_SELECTION
	}
	///	end SUPPORT_MULTI_FILES_INXF
	//refresh_organizer_gui(wp); ///---Sim 06-25-2007 UPDATE_ORGANIZER
	///---Sim 05-18-2007 REMOVE_UID_VARIABLE
	//if ( uid )
		//uid = vImportedRangeUID;
	///---END REMOVE_UID_VARIABLE
	if ( trheader )
		trheader.Replace(trBaseInfo.Clone(), true, true);/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
}

//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 impMDF_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impMDF_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	
	///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
	/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	//if ( GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID )
	if ( GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID || 0 == lstrcmpi(lpcszNodeName, "selectionMode") )
	/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	{
		/// Hong 10/29/09 QA80-13884-P1 FIX_SELECTION_MODE_FAIL_WHEN_APPLY_THEME
		TreeNode		trSelMode = trGetN.options.selectionMode;
		bool			bClearSettings = (0 == lstrcmpi(lpcszNodeName, "selectionMode")) ? true : false;
		if ( GETNE_ON_THEME == nEventID )
		{
			int				nOldValue;
			trSelMode.GetAttribute(STR_OLD_SEL_MODE_VALUE, nOldValue);
			if ( nOldValue != trSelMode.nVal )
				bClearSettings = true;
		}
		trSelMode.SetAttribute(STR_OLD_SEL_MODE_VALUE, trSelMode.nVal);
		/// end FIX_SELECTION_MODE_FAIL_WHEN_APPLY_THEME
		/// Hong 06/15/07 QA80-9251-P10 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
		//s_bFileValid = file_name_event(trGetN, strErrMsg);
		/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		//s_bFileValid = file_name_event(trGetN, strErrMsg, false);
		/// Hong 10/29/09 QA80-13884-P1 FIX_SELECTION_MODE_FAIL_WHEN_APPLY_THEME
		//s_bFileValid = file_name_event(trGetN, strErrMsg, 0 == lstrcmpi(lpcszNodeName, "selectionMode") ? true : false);
		s_bFileValid = file_name_event(trGetN, strErrMsg, bClearSettings);
		/// end FIX_SELECTION_MODE_FAIL_WHEN_APPLY_THEME
		/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		/// end REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
		_update_gui_on_data_group(trGetN); /// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		OK_SURE(bOKEnable);
		/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		//return true;
		return false;
		/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
	}
	///---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 trSetting on GUI
			trGetN.trSetting.RemoveChildrenWithPrefix();
			
			return true;
		}
		///---END ERROR_MESSAGE
		*/
		///---Sim 09-29-2006 SUPPORT_DATA_SELECTION
		/*
		StringArray vsFileNames;
		TreeNode trInfo = trGetN.info;
		separate_multiline_files(trGetN.fname.strVal, vsFileNames);
		if ( !construct_tree_setting(vsFileNames, trInfo) )
		{
			XF_THROW(CER_INVALID_FILEPATH);
			return false;
		}
		*/
		/*
		///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
		//construct_tree_setting(trGetN.fname.strVal, trGetN.trSetting);
		if ( !construct_tree_setting(trGetN.fname.strVal, trGetN.trSetting) )
		{
			// clean the trFiles on GUI
			trGetN.trSetting.RemoveChildrenWithPrefix();
			
			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 HANDLE_ERROR_MESSAGE
		///---END SUPPORT_DATA_SELECTION
		return true; ///---Sim 11-06-2006 UPDATE_GUI
		*/
		///---END SUPPORT_DND_OPEN_DIALOG
	}
	///---END SHOW_MULTI_FILE_INFO	
	
	return false;

}

int impMDF_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	///---Sim 09-29-2006 SHOW_MULTI_FILE_INFO
	string strTagName = lpcszVarName;
	
	///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT
	if ( 0 == strcmp(lpcszVarName, "options") )
	{
		constructOptionsForXF(tr, true, true, true);
		
		/// Hong 07/09/07 v8.0655 IMPORT_RAW_DATA_FOR_MAY_FORMULA
		TreeNode trRawData = tr.InsertNode(tr.FirstNode, "rawdata", TRGP_CHECK);
		trRawData.SetAttribute(STR_LABEL_ATTRIB, _L("Import Raw Data"));
		trRawData.DataID = GET_FILTER_USER_DATAID(1);
		trRawData.nVal = 0;		
		/// end IMPORT_RAW_DATA_FOR_MAY_FORMULA
		
		/// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		TreeNode trSelectionMode = tr.InsertNode(trRawData, "selectionmode", TRGP_CHECK);
		trSelectionMode.SetAttribute(STR_LABEL_ATTRIB, _L("Import with Data Group"));
		trSelectionMode.DataID = GET_FILTER_USER_DATAID(2);
		trSelectionMode.nVal = 0;		
		/// end ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		
		// custom import mode list
		TreeNode tr1stImportMode = tree_get_node_by_id(tr, IDE_DISPIMP_1STMODE, true);
		if ( tr1stImportMode )
		{
			/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
			//tr1stImportMode.nVal = ASCIMP_MODE_NEW_BOOKS;
			//tr1stImportMode.Show = 0;
			string strCombo, strMapping;
			//strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_REPLACE_BOOK_SHEET_COLUMN);
			strCombo = getImportModeListString(strMapping, MDF_1ST_FILE_IMPORT_MODE_LIST); /// Hong 10/18/07 v8.0728 REMOVE_REPLACE_OUT_OF_MULTI_FILE_IMPORT_MODE
			tr1stImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			tr1stImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			tr1stImportMode.nVal = ASCIMP_MODE_REPLACE_DATA;
			/// end IMPORT_ALL_DATA_TO_ONE_SHEET
		}
		
		TreeNode trImportMode = tree_get_node_by_id(tr, IDE_IMP_IMPORTMODE, true);
		if ( trImportMode )
		{
			//string strCombo, strMapping;
			//strCombo = getImportModeListString(strMapping, MDF_IMPORT_MODE_LIST);
			//trImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			//trImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
			//trImportMode.nVal = ASCIMP_MODE_NEW_BOOKS;
			//trImportMode.Show = 0;
			string strCombo, strMapping;
			//strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_REPLACE_BOOK_SHEET_COLUMN);
			strCombo = getImportModeListString(strMapping, MDF_IMPORT_MODE_LIST); /// Hong 10/18/07 v8.0728 REMOVE_REPLACE_OUT_OF_MULTI_FILE_IMPORT_MODE
			trImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			trImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			trImportMode.nVal = ASCIMP_MODE_NEW_SHEETS;
			/// end IMPORT_ALL_DATA_TO_ONE_SHEET
		}
		
		fuSetShowTime(tr, 0); // show time column default is none
	
		fuSetXFunctionName(tr, "impMDF");
	
		return 0;
	}
	///---END MORE_OPTIONS_FOR_IMPORT
	
	/*
	if(strTagName == "info")
	{
		return 0;
	}
	*/
	///---END SHOW_MULTI_FILE_INFO
	///---Sim 09-29-2006 SUPPORT_FILE_SETTING
	if ( strTagName == "trfiles" )
	{
		return 0;
	}
	///---END SUPPORT_FILE_SETTING
	
	///---Sim 09-29-2006 FIX_UNMATCH_RETURN_VALUE
	//return false;
	return -1;
	///---END FIX_UNMATCH_RETURN_VALUE
}
//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 impMDF_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	//	trGetN.orng.Show = 0;
	
	///---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;
		//trGetN.impmode.Show = 0; // Hong 01/25/07 MDF_SHOULD_ALWAYS_NEW_BOOK
		//trGetN.uid.Show = 0; ///---Sim 05-18-2007 REMOVE_UID_VARIABLE
		trGetN.trheader.Show = 0;
		/// Hong 06/08/07 QA80-9890 IMPORT_ALL_DATA_TO_ONE_SHEET
		if(1 == trGetN.reimp.nVal)
		{
			trGetN.fname.Enable = 2;
			trGetN.orng.Enable = 2;
			trGetN.trfiles.Enable = 2;
		}
		/// end IMPORT_ALL_DATA_TO_ONE_SHEET
		
		///---Sim 11-10-2006 NO_NEED_TO_PRESELECT_FILE
		/////---Sim 09-29-2006 SHOW_MULTI_FILE_INFO
		//string strFileName;
		//int nFilesRet = get_open_box_by_file_group(strFileName, "MDFFiles", "", true);
		//if ( 0 == nFilesRet )
		//{
			//nRet = XFEVT_ABORT;
			//return;
		//}
		//trGetN.fname.strVal = strFileName;
		/////---Sim 09-29-2006 SUPPORT_DATA_SELECTION
		///*
		//StringArray vsFileNames;
		//separate_multiline_files(strFileName, vsFileNames);
		//TreeNode trInfo = trGetN.info;
		//if ( !construct_tree_setting(vsFileNames, trInfo) )
		//{
			//XF_THROW(CER_INVALID_FILEPATH);
			//return ;
		//}
		//*/
		//construct_tree_setting(trGetN.fname.strVal, trGetN.trSetting);
		/////---END SUPPORT_DATA_SELECTION
		/////---END SHOW_MULTI_FILE_INFO
		///---END NO_NEED_TO_PRESELECT_FILE
	}
	/// AW 09/25/06 MORE_WORK_ON_IMPWAV
	//-------------- CPY 9/25/06 FILTER_TO_UPDATE_GUI_AND_LT_INVOKE
	//else if ( 0 == nGetNDialog )
	if(nGetNDialog >= 0)
	//--------------
	{
		///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT		
		if ( LTXF_FROM_GUI_MENU == dwCntrl )
			trGetN.fname.strVal = "";
	
		_update_gui_on_data_group(trGetN); /// Hong 07/07/09 QA80-13884 ADD_OPTION_TO_SUPPORT_WHETHER_IMPORT_WITH_DATA_GROUP_OR_NOT
		
		//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);
			///// Hong 01/25/07 MDF_SHOULD_ALWAYS_NEW_BOOK
			///*
			//// XF impmode list is "Replace Data, new books, new sheets"
			//int iMode = IML_NEW_BK; // default new book
			//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;
			//}
			//*/
			//int iMode = IML_NEW_BK;
			///// end MDF_SHOULD_ALWAYS_NEW_BOOK
			//trGetN.impmode.nVal = iMode; 
			/////---END TRANSLATE_FILTER_IMPORT_MODE_TO_XF_PARAM
			//trGetN.sparklines.nVal = fuGetSparkLine(trFilter);
			////trGetN.timecol.nVal = fuGetShowTime(trFilter);
		//}
		///---END MORE_OPTIONS_FOR_IMPORT		
	}
	/// END MORE_WORK_ON_IMPWAV
	///---END SUPPORT_MORE_PARAMETER
	/// Hong 12/22/06 CHECK_FNAME_EMPTY_UNASSIGNED
	if(nGetNDialog > -1)
	{
		bool bResetFileInfo = false; ///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		
		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 05/31/07 v8.0630 MORE_ON_ACCESS_TO_FDLOG
			//int nFilesRet = get_open_box_by_file_group(strFileName, "MDFFiles", "MDFFiles", 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, "MDFFiles", _L("MDFFiles"),
				//	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, "MDFFiles",NULL,
					//OPENBOX_FLAG_MULTIFILES, NULL, NULL, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				nFilesRet = get_open_box_by_file_group(strFileName, "MDFFiles",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
			{
				//nFilesRet = get_open_box_by_file_group(strFileName, "MDFFiles", _L("MDFFiles"), true);
				nFilesRet = get_open_box_by_file_group(strFileName, "MDFFiles", NULL, true); /// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
			}
			/// end MORE_ON_ACCESS_TO_FDLOG
			/// 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;
			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
		//if( !file_name_event(trGetN, strErrMsg) )
		if( !file_name_event(trGetN, strErrMsg, bResetFileInfo) )
		///---END QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		{
			/// 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, 'E'); // should pass an int ///---Sim 06-19-2007 IMPROVE_WARNING_MESSAGE_POST_FUNCTION
			/// end FIX_FAIL_DUMP_CORRECT_ERROR_STRING
			return;
		}
		///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION			
		//}
		///---END QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
	}
	/// 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 impMDF_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
