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

//put additional include files here
#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\XFunctionEx.h> ///---Sim 04-18-2007 ADD_SPARKLINES
#include <..\OriginLab\fu_utils.h>
#include <..\originlab\import_utils.h> 
#include <..\OriginLab\DiademFile.h>

///////////////////////////////////////////////////////////////////////
//////// 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-18-07 MORE_CHANGE_FOR_OPTIONS
/// Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
/// Sim 05-08-2007 ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
/// Sim 05-18-2007 REMOVE_UID_VARIABLE
/// Sim 05-28-2007 QA80-9828-P3 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
/// Sim 06-25-2007 UPDATE_ORGANIZER
/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
/// Hong 07/17/08 v8.0903 FIX_SOME_IMPORT_HAVE_DUPLICATE_SHEET_NAME_NO_AUTO_ENUM
/// Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
///////////////////////////////////////////////////////////////////////

//put your own support static functions here
//enum {
	//IML_REPLACE = 0, // ASCIMP_MODE_REPLACE_DATA,
	//IML_NEW_BK, // ASCIMP_MODE_NEW_BOOKS,
	//IML_NEW_SHT, // ASCIMP_MODE_NEW_SHEETS,
//};
//

#define DIADEM_1ST_FILE_IMPORT_MODE_LIST 	LIST_IMPORT_MODE_REPLACE_BOOK_SHEET_COLUMN ///---Sim 05-08-2007 ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
#define DIADEM_IMPORT_MODE_LIST	(DIADEM_1ST_FILE_IMPORT_MODE_LIST & (~IW_FLAG_IMPMODE_REPLACE)) ///---Sim 05-08-2007 REMOVE_REPLACE_OUT_OF_MULTI_FILE_IMPORT_MODE

static bool s_bFileValid = true;
static bool s_bRangeValid = true; ///---Sim 03-17-2007 CHECK_ERROR_FOR_INVALID_RANGE
#define OK_SURE(_OK_ENABLE) _OK_ENABLE = s_bFileValid & s_bRangeValid; //also can be _OK_ENABLE = s_bFileValid && s_bSetting

static void _shift_error_code(int& nRet)
{
	switch ( nRet )
	{
	case DIADEM_ERR_INVALID_FILE:
		nRet = IMPERR_INVALID_FILE;
		break;
	case DIADEM_ERR_INVALID_LABEL:
	case DIADEM_ERR_INVALID_DATA_TYPE:
	case DIADEM_ERR_EXIST_ILLEGAL_LABEL_VAL:
	case DIADEM_ERR_ILLEGAL_DATA_STORE_TYPE:
		nRet = IMPERR_DIADEM_ILLEGAL_KEYWORD;
		break;
	case DIADEM_ERR_FAIL_TO_READ_EXPLICIT_DATA:
		nRet = IMPERR_DIADEM_FAIL_TO_READ_EXPLICIT_DATA;
		break;
	case DIADEM_ERR_INVALID_DATA_FILE:
		nRet = IMPERR_DIADEM_INVALID_DATA_FILE;
		break;
	case DIADEM_ERR_LACK_CHANNEL_NAME:
		nRet = IMPERR_DIADEM_LACK_CHANNEL_NAME;
		break;
	case DIADEM_ERR_UNSUPPORT_FILE:
		nRet = IMPERR_UNSUPPORT_VERSION;
		break;
	/// Hong 07/18/07 QA80-9828-P6 REIMPORT_NOT_SUPPORT_SOURCE_FILE_CHANGE_CHANNEL_NUM
	case DIADEM_ERR_UNSUPPOORT_CHANNEL_CHANGE:
		nRet = XFERR_REIMP_NOT_SUPPORT_CHANNEL_NUM_CHANGE;
		break;
	/// end REIMPORT_NOT_SUPPORT_SOURCE_FILE_CHANGE_CHANNEL_NUM
	default:
		nRet = CER_UNKNOWN_ERROR;
	}
}

static bool _construct_tree_setting(StringArray& vsFileNames, TreeNode& trSetting)
{
	//trSetting.RemoveChildrenWithPrefix(); ///---Sim 05-25-2007 QA80-9828-P3 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION remove
	
	for ( int nIndex = 0; nIndex < vsFileNames.GetSize(); nIndex++ )
	{
		TreeNode trFileSetting = trSetting.AddNumericNode(1, "FileSetting", TRGP_CHECK);
		trFileSetting.SetAttribute(STR_LABEL_ATTRIB, GetFileName(vsFileNames[nIndex]));
		//trFileSetting.Use = 1; /// Hong 07/11/07 QA80-9828-P5 v8.0657 REMOVE_UNWANTED_CHECKBOX
		//trFileSetting.SetAttribute(STR_ATTRIB_DISPFMT, DISPLAY_EDITOR_CHECK_ON_LEFT); // Hong 03/13/07 AVOID_LABEL_TRUNCATED
		
		TreeNode trFileInfo = trFileSetting.AddNode("FileInfo");
		trFileInfo.SetAttribute(STR_LABEL_ATTRIB, STR_FILE_INFO);	///Sophy 9/21/2009 SET_LABEL_ON_GUI_FOR_TRANSLATION
		GETN_USE(trFileInfo)
		string strTag, strInfo;
		strTag.Format("file%d", nIndex+1);
		strInfo.Format("File SizRemovee: %s ", GetFileSize(vsFileNames[nIndex]));
		GETN_STR(strTag, GetFileName(vsFileNames[nIndex], false), strInfo )
		GETN_READ_ONLY
			
		DiademFile diademFile(vsFileNames[nIndex]);
		
		//if ASCII file's data string is binary string, will give a option
		//to read as binary or convert it to double
		TreeNode trReadBinary = trFileSetting.AddNumericNode(0, "ReadBinary", TRGP_CHECK);
		trReadBinary.SetAttribute(STR_ATTRIB_DISPFMT, DISPLAY_EDITOR_RIGHT);
		trReadBinary.SetAttribute(STR_LABEL_ATTRIB, _L("Read as Binary"));
			
		Tree trInfo;
		TreeNode trChannelsSetting = trFileSetting.AddNumericNode(1, "Channels", TRGP_CHECK);
		if(diademFile. ReadHeader(trChannelsSetting, trInfo) != DIADEM_NO_ERROR)
			return false;
		
		if(!diademFile.IsBinarySting())
			trReadBinary.Remove();
		
		//add sel all button
		trChannelsSetting.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_CHECK_CONTROL | GETNBRANCH_CHECK_CONTROL_ONE_EVENT);
		
		foreach (TreeNode trNode in trChannelsSetting.Children )
		{
			trNode.nVal = 1;
			trNode.ID = TRGP_CHECK;
		}
	}
	return true;
}

///---Sim 05-25-2007 QA80-9828-P3 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 QA80-9828-P3 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
{
	string strFileName = trGetN.fname.strVal;
	TreeNode trNode = trGetN.trfiles;
	
	if ( !check_imported_file_name(strFileName, strErrMsg) )
		return false;
	
	///---Sim 05-25-2007 QA80-9828-P3 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
	if( bClearSetting )
		trNode.RemoveChildrenWithPrefix(); 
	
	if(trNode && trNode.GetNodeCount() > 0)
		return true; // no need construct tree setting
	///---END QA80-9828-P3 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
	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;
}



void impNIDIAdem(const string& fname, const TreeNode& trfiles, const TreeNode& options, Range& orng, TreeNode& trheader, const int& reimp)
{
	///---Sim 04-18-07 MORE_CHANGE_FOR_OPTIONS
	//vector vImportedRangeUID; ///---Sim 02-06-2007 GET_RANGE_UID_OF_IMPORTED_FILE ///---Sim 05-18-2007 REMOVE_UID_VARIABLE remove
	
	Worksheet wks;	
	int c1,c2;	
	/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	//orng.GetRange(wks, c1, c2);
	WorksheetPage wp;
	if ( !GetRangeInfo(orng, wks, wp, c1, c2) )
		XF_THROW(XFERR_INVALID_RANGE);
	/// end CENTRALIZE_CHECK_CALID_RANGE
	if(!wks)
		XF_THROW(CER_INVALID_WKS);
	
	///---Sim 03-09-2007 TEMP_FIX_HIGHLINE_REPLACE_DATA
	///---Sim 04-17-2007 FIX_MODIFY_CURRENT_DATA_RANGE_WILL_CAUSE_THE_RANGE_IN_PAGE_STORE_IS_CHANGED_UNEXPECTED
	//if ( orng )
	{
		//orng.Reset();
		//orng.Add(ds, 0);
	}
	DataRange drTarget;
	orng.Clone(drTarget);
	
	if ( !reimp && drTarget )
	{	// avoid to process highline replace data		
		/// Hong 09/08/07 v8.0698b FIX_FAIL_DUMP_OUTPUT_RANGE_ORNG
		/*
		drTarget.Reset();
		drTarget.Add(wks, 0);
		*/
		reset_range_to_first_obj(drTarget, wks);
		/// end FIX_FAIL_DUMP_OUTPUT_RANGE_ORNG
	}
	///---END FIX_MODIFY_CURRENT_DATA_RANGE_WILL_CAUSE_THE_RANGE_IN_PAGE_STORE_IS_CHANGED_UNEXPECTED
	///---END TEMP_FIX_HIGHLINE_REPLACE_DATA
	
	/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
	Tree trBaseInfo;
	//if ( finfo )
		//trInfo = finfo;
	/// end FINFO_IS_ONLY_OUTPUT
	
	/*
	//int nListIndexToImportMode[] = {
		//ASCIMP_MODE_REPLACE_DATA,
		//ASCIMP_MODE_NEW_BOOKS,
		//ASCIMP_MODE_NEW_SHEETS
	//};
	//int nFileImportMode = ASCIMP_MODE_NEW_SHEETS;
	//if( 0 <= impmode && impmode < (sizeof(nListIndexToImportMode) / sizeof(int)) )
		//nFileImportMode = nListIndexToImportMode[impmode];
	int nFileImportMode = _GetImportModeFromListIndex(impmode + 1);
	
	TreeNode tnUsingFilter;
	double dDefault = 0.;
	if ( fuGetDouble(trFilter, IDE_DISP_IMP_ORIGINVER, dDefault) )
		tnUsingFilter = trFilter;
	*/
	//support default filter is empty
	TreeNode tnUsingFilter;
	double dDefault = 0.;
	if ( fuGetDouble(options, IDE_DISP_IMP_ORIGINVER, dDefault) )
		tnUsingFilter = options;
	///end FIX_IMPORT_MODE_NOT_WORK
	
	int nFileImportMode = fuGetImportMode(tnUsingFilter);
	/// Hong 07/18/07 QA80-9828-P8 FIX_FIRST_IMPORT_MODE_NOT_WORK
	int n1stFileImportMode = fuGet1stImportMode(tnUsingFilter);
	/// end FIX_FIRST_IMPORT_MODE_NOT_WORK
	//bool bSparkLines = fuGetSparkLine(tnUsingFilter);
	
	/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	//WorksheetPage wp;
	//wks.GetParent(wp);
	/// end CENTRALIZE_CHECK_CALID_RANGE
	StringArray vsFileNames;
	separate_multiline_files(fname, vsFileNames); 
	
	int nFileCount = vsFileNames.GetSize();
	if ( nFileCount <= 0 )
		XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
	
	///Cheney 2007-1-17 CONSTRUCT_TREE_SETTING
	//should construc tree setting, because when D&D, if file is invalid, should also show Dlg
	if ( trfiles.IsEmpty())
	{
		if ( !_construct_tree_setting(vsFileNames, trfiles) )
		{
			XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
			return ;
		}
	}
	if( trfiles.IsEmpty())//trfiles was constructed by file
		XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
	///end CONSTRUCT_TREE_SETTING
	
	int nFileIndex = -1;
	int nIndexLayer = wks.GetIndex();
	//Layer lyOlder;
	//bool bNewLayer, bEmptyLayer;
	set_active_layer(wks);
	
	int nRet;	
	waitCursor wc; 
	//for ( int ii = 0; ii < nFileCount; ii++ )
	foreach(TreeNode trFileNode in trfiles.Children )
	{
		nFileIndex++;
		//TreeNode trFileSetting = trfiles.Children.Item(ii);
		//if(trFileSetting.Use != 1)
		/// Hong 07/11/07 QA80-9828-P5 v.80657 REMOVE_UNWANTED_CHECKBOX
		//if(trFileNode.Use != 1)	
		//	continue;
		/// end REMOVE_UNWANTED_CHECKBOX
		DiademFile diademFile(vsFileNames[nFileIndex]);
		//lyOlder = wp.Layers();
		///---Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
		//if ( 0 == nFileIndex && !reimp )
		//{
			//int nImportMode = get_first_file_import_mode(FILE_TYPE_DIADEM);
			//TreeNode trInValid;
			//
			//wks = GetTargetLayer(wp, nIndexLayer, nFileIndex, trInValid, true, drTarget, nImportMode, c1);
		//}
		//else
		//{
		wks = GetTargetLayer(wp, nIndexLayer, nFileIndex, tnUsingFilter, true, drTarget, nFileImportMode, c1);
		//}
		///---END TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
		//bNewLayer = !is_same_layer(lyOlder, wks); 
		/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
		string strFile;
		strFile.Format("File%d", nIndexLayer + 1); // file index to be 1
		TreeNode trInfo = trBaseInfo.AddNode(strFile);
		/// end FINFO_IS_ONLY_OUTPUT
		/// Hong 07/18/07 QA80-9828-P8 FIX_FIRST_IMPORT_MODE_NOT_WORK
		//if ( nRet = diademFile.Import(wks, trFileNode.Channels, trInfo, trFileNode.ReadBinary? trFileNode.ReadBinary.nVal : false, nFileImportMode, c1) )
		int nImportMode = nFileImportMode;
		if ( 0 == nIndexLayer ) // if first file
			nImportMode = n1stFileImportMode;
		/// Hong 07/17/08 v8.0903 FIX_SOME_IMPORT_HAVE_DUPLICATE_SHEET_NAME_NO_AUTO_ENUM
		//if ( nRet = diademFile.Import(wks, trFileNode.Channels, trInfo, trFileNode.ReadBinary? trFileNode.ReadBinary.nVal : false, nImportMode, c1) )
		if ( nRet = diademFile.Import(wks, trFileNode.Channels, trInfo, trFileNode.ReadBinary? trFileNode.ReadBinary.nVal : false, nImportMode, c1, 0) )
		/// end FIX_SOME_IMPORT_HAVE_DUPLICATE_SHEET_NAME_NO_AUTO_ENUM
		/// end FIX_FIRST_IMPORT_MODE_NOT_WORK
		{
			_shift_error_code(nRet);
			XF_THROW_EX(nRet, vsFileNames[nFileIndex]);
		}
		
		/*		
		bEmptyLayer = (1 == is_sheet_empty(wks)) ? true : false;
		if ( bNewLayer && bEmptyLayer )
		{
			wks.Destroy();
		}
		set_active_layer(wp.Layers()); 
		*/
		
		// rename 
		DWORD dwCtrl = 0;
		dwCtrl |= (reimp) ? COVER_LAST_ADDED_COMMENT : 0;
		setPageAndLayerName(diademFile.GetDataRange(), vsFileNames[nFileIndex], tnUsingFilter, dwCtrl);
		
		/* //Sim design is changed
		// sparkline
		if ( bSparkLines )
		{
			XFBase xfSparkLine("sparklines");
			if( !xfSparkLine )
			{
				XF_WARN(CER_FAILED_LOAD_SPK_LINE_XF);
			}
			else
			{
				diademFile.GetDataRange().GetRange(wks, c1, c2);
				xfSparkLine.SetArg("sel", 0);
				xfSparkLine.SetArg("c1", c1+1);
				xfSparkLine.SetArg("c2", c2+1);
				xfSparkLine.SetArg("wks", wks);
				
				if ( !xfSparkLine.Evaluate() )
					XF_WARN(CER_SPK_LINE_RUN_ERROR);
			}
		}
		//vector<int> vn = {RCLT_LONG_NAME, RCLT_UNIT, RCLT_COMMENT};
		vector<int> vnIDs;
		vector<uint> vIndex;
		wks_get_show_labels(wks, vnIDs);
		if ( bSparkLines && vnIDs.Find(vIndex, RCLT_SPARKLINE) <= 0)
			vnIDs.Add(RCLT_SPARKLINE);	
		wks_add_show_labels(wks, vnIDs, false);
		*/
		
		// add data range info
		Tree trDataRange;
		///---Sim 05-18-2007 REMOVE_UID_VARIABLE
		//int UID = getRangeInfo(trDataRange, diademFile.GetDataRange());
		getRangeInfo(trDataRange, diademFile.GetDataRange());
		//vImportedRangeUID.Add(UID);
		///---END REMOVE_UID_VARIABLE
		
		/// Hong 05/24/07 QA80-9828-P1 v8.0626 KEEP_DATA_SELECTION_SEETING_WHEN_REIMPORT
		TreeNode trReimpotInfo = trInfo.AddNode("Import");
		trReimpotInfo.Show = 0;
		TreeNode trSelect = trReimpotInfo.AddNode("trSetting");
		///---Sim 05-25-2007 QA80-9828-P3 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
		//trSelect.Replace(trFileNode.Clone(), true, true);
		trSelect.AddNode(trFileNode); // must keep trSetting's structure same as trfiles
		///---END QA80-9828-P3 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
		/// end KEEP_DATA_SELECTION_SEETING_WHEN_REIMPORT
		
		// if current file cover existing file data, need update info of that files
		if ( !reimp )
			impinfo_UpdateExistingData(wp, diademFile.GetDataRange());
		
		impinfo_AddFile(wks, vsFileNames[nFileIndex], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter, nFileIndex, trDataRange, reimp);
	}
	//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
	///---END MORE_CHANGE_FOR_OPTIONS	
}

//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 impNIDIAdem_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impNIDIAdem_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	OK_SURE(bOKEnable);
	
	///---Sim 04-18-07 MORE_CHANGE_FOR_OPTIONS
	///support D&D show Dlg, if trFiles no child, should construct it
	if ( (GETNE_ON_INIT == nEventID && trGetN.trFiles.Children.Count() == 0) || GETNE_ON_THEME == nEventID )
	{
		///---Sim 05-25-2007 QA80-9828-P3 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
		//s_bFileValid = _file_name_event(trGetN, strErrMsg);
		s_bFileValid = _file_name_event(trGetN, strErrMsg, false);
		///---END QA80-9828-P3 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
		s_bRangeValid = !trGetN.orng.strVal.IsEmpty();
		if(!s_bRangeValid)
			strErrMsg = XFERR_INVALID_RANGE;
		OK_SURE(bOKEnable);	
		return true;
	}
	
	if( 0 == lstrcmp(lpcszNodeName, "fname") )
	{
		s_bFileValid = _file_name_event(trGetN, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
	}
	///Cheney 2006-12-9 PREPARE_ERR_MESSAGE
	//check range if is invalid
	/*
	if(GETNE_ON_INIT != nEventID)
	{
		s_bFileValid = !trGetN.orng.strVal.IsEmpty();
		OK_SURE(bOKEnable);
		if(!s_bFileValid)
			strErrMsg = CER_INVALID_PAGE;
		
		return true;
	}
	*/
	if( 0 == lstrcmp(lpcszNodeName, "orng") )
	{
		s_bRangeValid = !trGetN.orng.strVal.IsEmpty();
		if(!s_bRangeValid)
			strErrMsg = CER_INVALID_PAGE;
		
		OK_SURE(bOKEnable);
		return true;
	}
	///end PREPARE_ERR_MESSAGE
	///---END MORE_CHANGE_FOR_OPTIONS
	return false;

}

int impNIDIAdem_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	///---Sim 04-18-07 MORE_CHANGE_FOR_OPTIONS
	if ( 0 == strcmp(lpcszVarName, "options") )
	{
		constructOptionsForXF(tr);//, true, true, true);
		
		// custom import mode list
		///---Sim 05-08-2007 ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
		TreeNode tr1stImportMode = tree_get_node_by_id(tr, IDE_DISPIMP_1STMODE, true);
		if ( tr1stImportMode )
		{
			string strCombo, strMapping;
			strCombo = getImportModeListString(strMapping, DIADEM_1ST_FILE_IMPORT_MODE_LIST);
			tr1stImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			tr1stImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			tr1stImportMode.nVal = ASCIMP_MODE_REPLACE_DATA;
		}
		///---END ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
		TreeNode trImportMode = tree_get_node_by_id(tr, IDE_IMP_IMPORTMODE, true);
		if ( trImportMode )
		{
			string strCombo, strMapping;
			strCombo = getImportModeListString(strMapping, DIADEM_IMPORT_MODE_LIST);
			trImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			trImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			trImportMode.nVal = ASCIMP_MODE_NEW_SHEETS;
			//trImportMode.nVal = ASCIMP_MODE_REPLACE_DATA;			
		}
		
		fuSetShowTime(tr, 0); // show time column default is none
	
		fuSetXFunctionName(tr, "impNIDiadem");
	
		return 0;
	}
	///---END MORE_OPTIONS_FOR_IMPORT
	
	if ( 0 == lstrcmp(lpcszVarName, "trfiles") )
		return 0;
	
	return -1;
	///---END MORE_CHANGE_FOR_OPTIONS
}
//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 impNIDIAdem_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if ( nGetNDialog > 0 )
	{
		///---Sim 04-18-07 MORE_CHANGE_FOR_OPTIONS
		//trGetN.uid.Show = 0; ///---Sim 05-18-2007 REMOVE_UID_VARIABLE
		trGetN.trheader.Show = 0;
		//trGetN.trFilter.Show = 0;
		
		if(1 == trGetN.reimp.nVal)
		{
			trGetN.fname.Enable = 2;
			trGetN.trfiles.Enable = 2;
			trGetN.orng.Enable = 2;
		}
		///---END MORE_CHANGE_FOR_OPTIONS
	}
	
	if(nGetNDialog >= 0)
	{
		if ( LTXF_FROM_GUI_MENU == dwCntrl )
			trGetN.fname.strVal = "";
		//TreeNode trFilter = trGetN.trFilter;
		//if ( trFilter.IsValid() && trFilter.GetNodeCount()>0 )
		//{
			//int nImportMode = IML_NEW_SHT; // default new sheet
			//switch ( fuGetImportMode(trFilter) )
			//{
			//case ASCIMP_MODE_REPLACE_DATA:
				//nImportMode = IML_REPLACE;
				//break;
			//case ASCIMP_MODE_NEW_BOOKS:
				//nImportMode = IML_NEW_BK;
				//break;
			//case ASCIMP_MODE_NEW_SHEETS:
				//nImportMode = IML_NEW_SHT;
				//break;
			//default:
			//}
			//trGetN.impmode.nVal = nImportMode; 
			//trGetN.sparklines.nVal = fuGetSparkLine(trFilter);
		//}
	}
	
	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, "DIADEMFiles", "Import Diadem(*.dat) file", 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, "DIADEMFiles", _L("Import Diadem(*.dat) file"),
				//	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, "DIADEMFiles", NULL,
					//OPENBOX_FLAG_MULTIFILES, NULL, NULL, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				nFilesRet = get_open_box_by_file_group(strFileName, "DIADEMFiles", 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, "DIADEMFiles", _L("Import Diadem(*.dat) file"), true);
				nFilesRet = get_open_box_by_file_group(strFileName, "DIADEMFiles", NULL, true);
				/// end CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
			}
			/// end MORE_ON_ACCESS_TO_FDLOG
			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
		//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
	}
}
//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 impNIDIAdem_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
